• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

openmc-dev / openmc / 13763261523

10 Mar 2025 11:12AM UTC coverage: 85.133% (+0.004%) from 85.129%
13763261523

Pull #3252

github

web-flow
Merge bdc3a6ead into 906548db2
Pull Request #3252: Adding vtkhdf option to write vtk data

83 of 101 new or added lines in 1 file covered. (82.18%)

529 existing lines in 15 files now uncovered.

51616 of 60630 relevant lines covered (85.13%)

37229330.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.59
/src/random_ray/linear_source_domain.cpp
1
#include "openmc/random_ray/linear_source_domain.h"
2

3
#include "openmc/cell.h"
4
#include "openmc/geometry.h"
5
#include "openmc/material.h"
6
#include "openmc/message_passing.h"
7
#include "openmc/mgxs_interface.h"
8
#include "openmc/output.h"
9
#include "openmc/plot.h"
10
#include "openmc/random_ray/random_ray.h"
11
#include "openmc/simulation.h"
12
#include "openmc/tallies/filter.h"
13
#include "openmc/tallies/tally.h"
14
#include "openmc/tallies/tally_scoring.h"
15
#include "openmc/timer.h"
16

17
namespace openmc {
18

19
//==============================================================================
20
// LinearSourceDomain implementation
21
//==============================================================================
22

23
void LinearSourceDomain::batch_reset()
5,885✔
24
{
25
  FlatSourceDomain::batch_reset();
5,885✔
26
#pragma omp parallel for
3,210✔
27
  for (int64_t sr = 0; sr < n_source_regions(); sr++) {
13,168,480✔
28
    source_regions_.centroid_iteration(sr) = {0.0, 0.0, 0.0};
13,165,805✔
29
    source_regions_.mom_matrix(sr) = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
13,165,805✔
30
  }
31
#pragma omp parallel for
3,210✔
32
  for (int64_t se = 0; se < n_source_elements(); se++) {
15,059,080✔
33
    source_regions_.flux_moments_new(se) = {0.0, 0.0, 0.0};
15,056,405✔
34
  }
35
}
5,885✔
36

37
void LinearSourceDomain::update_neutron_source(double k_eff)
5,885✔
38
{
39
  simulation::time_update_src.start();
5,885✔
40

41
  double inverse_k_eff = 1.0 / k_eff;
5,885✔
42

43
// Reset all source regions to zero (important for void regions)
44
#pragma omp parallel for
3,210✔
45
  for (int64_t se = 0; se < n_source_elements(); se++) {
15,059,080✔
46
    source_regions_.source(se) = 0.0;
15,056,405✔
47
  }
48

49
#pragma omp parallel for
3,210✔
50
  for (int64_t sr = 0; sr < n_source_regions(); sr++) {
13,168,480✔
51
    int material = source_regions_.material(sr);
13,165,805✔
52
    if (material == MATERIAL_VOID) {
13,165,805✔
53
      continue;
200,000✔
54
    }
55
    MomentMatrix invM = source_regions_.mom_matrix(sr).inverse();
12,965,805✔
56

57
    for (int g_out = 0; g_out < negroups_; g_out++) {
27,822,210✔
58
      double sigma_t = sigma_t_[material * negroups_ + g_out];
14,856,405✔
59

60
      double scatter_flat = 0.0f;
14,856,405✔
61
      double fission_flat = 0.0f;
14,856,405✔
62
      MomentArray scatter_linear = {0.0, 0.0, 0.0};
14,856,405✔
63
      MomentArray fission_linear = {0.0, 0.0, 0.0};
14,856,405✔
64

65
      for (int g_in = 0; g_in < negroups_; g_in++) {
42,947,010✔
66
        // Handles for the flat and linear components of the flux
67
        double flux_flat = source_regions_.scalar_flux_old(sr, g_in);
28,090,605✔
68
        MomentArray flux_linear = source_regions_.flux_moments_old(sr, g_in);
28,090,605✔
69

70
        // Handles for cross sections
71
        double sigma_s =
72
          sigma_s_[material * negroups_ * negroups_ + g_out * negroups_ + g_in];
28,090,605✔
73
        double nu_sigma_f = nu_sigma_f_[material * negroups_ + g_in];
28,090,605✔
74
        double chi = chi_[material * negroups_ + g_out];
28,090,605✔
75

76
        // Compute source terms for flat and linear components of the flux
77
        scatter_flat += sigma_s * flux_flat;
28,090,605✔
78
        fission_flat += nu_sigma_f * flux_flat * chi;
28,090,605✔
79
        scatter_linear += sigma_s * flux_linear;
28,090,605✔
80
        fission_linear += nu_sigma_f * flux_linear * chi;
28,090,605✔
81
      }
82

83
      // Compute the flat source term
84
      source_regions_.source(sr, g_out) =
29,712,810✔
85
        (scatter_flat + fission_flat * inverse_k_eff) / sigma_t;
14,856,405✔
86

87
      // Compute the linear source terms. In the first 10 iterations when the
88
      // centroids and spatial moments are not well known, we will leave the
89
      // source gradients as zero so as to avoid causing any numerical
90
      // instability. If a negative source is encountered, this region must be
91
      // very small/noisy or have poorly developed spatial moments, so we zero
92
      // the source gradients (effectively making this a flat source region
93
      // temporarily), so as to improve stability.
94
      if (simulation::current_batch > 10 &&
25,589,905✔
95
          source_regions_.source(sr, g_out) >= 0.0) {
10,733,500✔
96
        source_regions_.source_gradients(sr, g_out) =
9,303,010✔
97
          invM * ((scatter_linear + fission_linear * inverse_k_eff) / sigma_t);
18,606,020✔
98
      } else {
99
        source_regions_.source_gradients(sr, g_out) = {0.0, 0.0, 0.0};
5,553,395✔
100
      }
101
    }
102
  }
103

104
  if (settings::run_mode == RunMode::FIXED_SOURCE) {
5,885✔
105
// Add external source to flat source term if in fixed source mode
106
#pragma omp parallel for
2,730✔
107
    for (int64_t se = 0; se < n_source_elements(); se++) {
14,375,480✔
108
      source_regions_.source(se) += source_regions_.external_source(se);
14,373,205✔
109
    }
110
  }
111

112
  simulation::time_update_src.stop();
5,885✔
113
}
5,885✔
114

115
void LinearSourceDomain::normalize_scalar_flux_and_volumes(
5,885✔
116
  double total_active_distance_per_iteration)
117
{
118
  double normalization_factor = 1.0 / total_active_distance_per_iteration;
5,885✔
119
  double volume_normalization_factor =
5,885✔
120
    1.0 / (total_active_distance_per_iteration * simulation::current_batch);
5,885✔
121

122
// Normalize flux to total distance travelled by all rays this iteration
123
#pragma omp parallel for
3,210✔
124
  for (int64_t se = 0; se < n_source_elements(); se++) {
15,343,440✔
125
    source_regions_.scalar_flux_new(se) *= normalization_factor;
15,340,765✔
126
    source_regions_.flux_moments_new(se) *= normalization_factor;
15,340,765✔
127
  }
128

129
// Accumulate cell-wise ray length tallies collected this iteration, then
130
// update the simulation-averaged cell-wise volume estimates
131
#pragma omp parallel for
3,210✔
132
  for (int64_t sr = 0; sr < n_source_regions(); sr++) {
13,452,840✔
133
    source_regions_.centroid_t(sr) += source_regions_.centroid_iteration(sr);
13,450,165✔
134
    source_regions_.mom_matrix_t(sr) += source_regions_.mom_matrix(sr);
13,450,165✔
135
    source_regions_.volume_t(sr) += source_regions_.volume(sr);
13,450,165✔
136
    source_regions_.volume_sq_t(sr) += source_regions_.volume_sq(sr);
13,450,165✔
137
    source_regions_.volume_naive(sr) =
26,900,330✔
138
      source_regions_.volume(sr) * normalization_factor;
13,450,165✔
139
    source_regions_.volume(sr) =
26,900,330✔
140
      source_regions_.volume_t(sr) * volume_normalization_factor;
13,450,165✔
141
    source_regions_.volume_sq(sr) =
26,900,330✔
142
      source_regions_.volume_sq_t(sr) / source_regions_.volume_t(sr);
13,450,165✔
143
    if (source_regions_.volume_t(sr) > 0.0) {
13,450,165✔
144
      double inv_volume = 1.0 / source_regions_.volume_t(sr);
13,450,160✔
145
      source_regions_.centroid(sr) = source_regions_.centroid_t(sr);
13,450,160✔
146
      source_regions_.centroid(sr) *= inv_volume;
13,450,160✔
147
      source_regions_.mom_matrix(sr) = source_regions_.mom_matrix_t(sr);
13,450,160✔
148
      source_regions_.mom_matrix(sr) *= inv_volume;
13,450,160✔
149
    }
150
  }
151
}
5,885✔
152

153
void LinearSourceDomain::set_flux_to_flux_plus_source(
30,854,989✔
154
  int64_t sr, double volume, int g)
155
{
156
  int material = source_regions_.material(sr);
30,854,989✔
157
  if (material == MATERIAL_VOID) {
30,854,989✔
158
    FlatSourceDomain::set_flux_to_flux_plus_source(sr, volume, g);
440,000✔
159
  } else {
160
    source_regions_.scalar_flux_new(sr, g) /= volume;
30,414,989✔
161
    source_regions_.scalar_flux_new(sr, g) += source_regions_.source(sr, g);
30,414,989✔
162
  }
163
  // If a source region is small, then the moments are likely noisy, so we zero
164
  // them. This is reasonable, given that small regions can get by with a flat
165
  // source approximation anyhow.
166
  if (source_regions_.is_small(sr)) {
30,854,989✔
167
    source_regions_.flux_moments_new(sr, g) = {0.0, 0.0, 0.0};
3,158,254✔
168
  } else {
169
    source_regions_.flux_moments_new(sr, g) *= (1.0 / volume);
27,696,735✔
170
  }
171
}
30,854,989✔
172

173
void LinearSourceDomain::set_flux_to_old_flux(int64_t sr, int g)
990✔
174
{
175
  source_regions_.scalar_flux_new(sr, g) =
1,980✔
176
    source_regions_.scalar_flux_old(sr, g);
990✔
177
  source_regions_.flux_moments_new(sr, g) =
1,980✔
178
    source_regions_.flux_moments_old(sr, g);
990✔
179
}
990✔
180

181
void LinearSourceDomain::accumulate_iteration_flux()
2,420✔
182
{
183
  // Accumulate scalar flux
184
  FlatSourceDomain::accumulate_iteration_flux();
2,420✔
185

186
  // Accumulate scalar flux moments
187
#pragma omp parallel for
1,320✔
188
  for (int64_t se = 0; se < n_source_elements(); se++) {
6,554,200✔
189
    source_regions_.flux_moments_t(se) += source_regions_.flux_moments_new(se);
6,553,100✔
190
  }
191
}
2,420✔
192

UNCOV
193
double LinearSourceDomain::evaluate_flux_at_point(
×
194
  Position r, int64_t sr, int g) const
195
{
UNCOV
196
  double phi_flat = FlatSourceDomain::evaluate_flux_at_point(r, sr, g);
×
197

UNCOV
198
  Position local_r = r - source_regions_.centroid(sr);
×
UNCOV
199
  MomentArray phi_linear = source_regions_.flux_moments_t(sr, g);
×
UNCOV
200
  phi_linear *= 1.0 / (settings::n_batches - settings::n_inactive);
×
201

UNCOV
202
  MomentMatrix invM = source_regions_.mom_matrix(sr).inverse();
×
UNCOV
203
  MomentArray phi_solved = invM * phi_linear;
×
204

UNCOV
205
  return phi_flat + phi_solved.dot(local_r);
×
206
}
207

208
} // namespace openmc
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc