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

openmc-dev / openmc / 20278515727

16 Dec 2025 06:26PM UTC coverage: 81.822% (-0.1%) from 81.92%
20278515727

Pull #3493

github

web-flow
Merge 7f36869a3 into bbfa18d72
Pull Request #3493: Implement vector fitting to replace external `vectfit` package

17013 of 23575 branches covered (72.17%)

Branch coverage included in aggregate %.

188 of 207 new or added lines in 3 files covered. (90.82%)

3101 existing lines in 56 files now uncovered.

54973 of 64404 relevant lines covered (85.36%)

41385524.25 hits per line

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

87.9
/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()
6,250✔
24
{
25
  FlatSourceDomain::batch_reset();
6,250✔
26
#pragma omp parallel for
3,750✔
27
  for (int64_t sr = 0; sr < n_source_regions(); sr++) {
10,480,440✔
28
    source_regions_.centroid_iteration(sr) = {0.0, 0.0, 0.0};
10,477,940✔
29
    source_regions_.mom_matrix(sr) = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
10,477,940✔
30
  }
31
#pragma omp parallel for
3,750✔
32
  for (int64_t se = 0; se < n_source_elements(); se++) {
12,049,176✔
33
    source_regions_.flux_moments_new(se) = {0.0, 0.0, 0.0};
12,046,676✔
34
  }
35
}
6,250✔
36

37
void LinearSourceDomain::update_single_neutron_source(SourceRegionHandle& srh)
26,941,270✔
38
{
39
  // Reset all source regions to zero (important for void regions)
40
  for (int g = 0; g < negroups_; g++) {
57,866,540✔
41
    srh.source(g) = 0.0;
30,925,270✔
42
  }
43

44
  // Add scattering + fission source
45
  int material = srh.material();
26,941,270✔
46
  if (material != MATERIAL_VOID) {
26,941,270✔
47
    double inverse_k_eff = 1.0 / k_eff_;
26,541,270✔
48
    MomentMatrix invM = srh.mom_matrix().inverse();
26,541,270✔
49

50
    for (int g_out = 0; g_out < negroups_; g_out++) {
57,066,540✔
51
      double sigma_t = sigma_t_[material * negroups_ + g_out];
30,525,270✔
52

53
      double scatter_flat = 0.0f;
30,525,270✔
54
      double fission_flat = 0.0f;
30,525,270✔
55
      MomentArray scatter_linear = {0.0, 0.0, 0.0};
30,525,270✔
56
      MomentArray fission_linear = {0.0, 0.0, 0.0};
30,525,270✔
57

58
      for (int g_in = 0; g_in < negroups_; g_in++) {
99,386,940✔
59
        // Handles for the flat and linear components of the flux
60
        double flux_flat = srh.scalar_flux_old(g_in);
68,861,670✔
61
        MomentArray flux_linear = srh.flux_moments_old(g_in);
68,861,670✔
62

63
        // Handles for cross sections
64
        double sigma_s =
65
          sigma_s_[material * negroups_ * negroups_ + g_out * negroups_ + g_in];
68,861,670✔
66
        double nu_sigma_f = nu_sigma_f_[material * negroups_ + g_in];
68,861,670✔
67
        double chi = chi_[material * negroups_ + g_out];
68,861,670✔
68

69
        // Compute source terms for flat and linear components of the flux
70
        scatter_flat += sigma_s * flux_flat;
68,861,670✔
71
        scatter_linear += sigma_s * flux_linear;
68,861,670✔
72
        if (settings::create_fission_neutrons) {
68,861,670!
73
          fission_flat += nu_sigma_f * flux_flat * chi;
68,861,670✔
74
          fission_linear += nu_sigma_f * flux_linear * chi;
68,861,670✔
75
        }
76
      }
77

78
      // Compute the flat source term
79
      srh.source(g_out) =
61,050,540✔
80
        (scatter_flat + fission_flat * inverse_k_eff) / sigma_t;
30,525,270✔
81

82
      // Compute the linear source terms. In the first 10 iterations when the
83
      // centroids and spatial moments are not well known, we will leave the
84
      // source gradients as zero so as to avoid causing any numerical
85
      // instability. If a negative source is encountered, this region must be
86
      // very small/noisy or have poorly developed spatial moments, so we zero
87
      // the source gradients (effectively making this a flat source region
88
      // temporarily), so as to improve stability.
89
      if (simulation::current_batch > 10 && srh.source(g_out) >= 0.0) {
30,525,270✔
90
        srh.source_gradients(g_out) =
18,690,020✔
91
          invM * ((scatter_linear + fission_linear * inverse_k_eff) / sigma_t);
37,380,040✔
92
      } else {
93
        srh.source_gradients(g_out) = {0.0, 0.0, 0.0};
11,835,250✔
94
      }
95
    }
96
  }
97

98
  // Add external source if in fixed source mode
99
  if (settings::run_mode == RunMode::FIXED_SOURCE) {
26,941,270✔
100
    for (int g = 0; g < negroups_; g++) {
56,080,540✔
101
      srh.source(g) += srh.external_source(g);
29,345,270✔
102
    }
103
  }
104
}
26,941,270✔
105

106
void LinearSourceDomain::normalize_scalar_flux_and_volumes(
6,250✔
107
  double total_active_distance_per_iteration)
108
{
109
  double normalization_factor = 1.0 / total_active_distance_per_iteration;
6,250✔
110
  double volume_normalization_factor =
6,250✔
111
    1.0 / (total_active_distance_per_iteration * simulation::current_batch);
6,250✔
112

113
// Normalize flux to total distance travelled by all rays this iteration
114
#pragma omp parallel for
3,750✔
115
  for (int64_t se = 0; se < n_source_elements(); se++) {
12,360,552✔
116
    source_regions_.scalar_flux_new(se) *= normalization_factor;
12,358,052✔
117
    source_regions_.flux_moments_new(se) *= normalization_factor;
12,358,052✔
118
  }
119

120
// Accumulate cell-wise ray length tallies collected this iteration, then
121
// update the simulation-averaged cell-wise volume estimates
122
#pragma omp parallel for
3,750✔
123
  for (int64_t sr = 0; sr < n_source_regions(); sr++) {
10,766,952✔
124
    source_regions_.centroid_t(sr) += source_regions_.centroid_iteration(sr);
10,764,452✔
125
    source_regions_.mom_matrix_t(sr) += source_regions_.mom_matrix(sr);
10,764,452✔
126
    source_regions_.volume_t(sr) += source_regions_.volume(sr);
10,764,452✔
127
    source_regions_.volume_sq_t(sr) += source_regions_.volume_sq(sr);
10,764,452✔
128
    source_regions_.volume_naive(sr) =
21,528,904✔
129
      source_regions_.volume(sr) * normalization_factor;
10,764,452✔
130
    source_regions_.volume(sr) =
21,528,904✔
131
      source_regions_.volume_t(sr) * volume_normalization_factor;
10,764,452✔
132
    source_regions_.volume_sq(sr) =
21,528,904✔
133
      source_regions_.volume_sq_t(sr) / source_regions_.volume_t(sr);
10,764,452✔
134
    if (source_regions_.volume_t(sr) > 0.0) {
10,764,452✔
135
      double inv_volume = 1.0 / source_regions_.volume_t(sr);
10,764,448✔
136
      source_regions_.centroid(sr) = source_regions_.centroid_t(sr);
10,764,448✔
137
      source_regions_.centroid(sr) *= inv_volume;
10,764,448✔
138
      source_regions_.mom_matrix(sr) = source_regions_.mom_matrix_t(sr);
10,764,448✔
139
      source_regions_.mom_matrix(sr) *= inv_volume;
10,764,448✔
140
    }
141
  }
142
}
6,250✔
143

144
void LinearSourceDomain::set_flux_to_flux_plus_source(
28,263,590✔
145
  int64_t sr, double volume, int g)
146
{
147
  int material = source_regions_.material(sr);
28,263,590✔
148
  if (material == MATERIAL_VOID) {
28,263,590✔
149
    FlatSourceDomain::set_flux_to_flux_plus_source(sr, volume, g);
400,000✔
150
  } else {
151
    source_regions_.scalar_flux_new(sr, g) /= volume;
27,863,590✔
152
    source_regions_.scalar_flux_new(sr, g) += source_regions_.source(sr, g);
27,863,590✔
153
  }
154
  // If a source region is small, then the moments are likely noisy, so we zero
155
  // them. This is reasonable, given that small regions can get by with a flat
156
  // source approximation anyhow.
157
  if (source_regions_.is_small(sr)) {
28,263,590✔
158
    source_regions_.flux_moments_new(sr, g) = {0.0, 0.0, 0.0};
2,871,140✔
159
  } else {
160
    source_regions_.flux_moments_new(sr, g) *= (1.0 / volume);
25,392,450✔
161
  }
162
}
28,263,590✔
163

164
void LinearSourceDomain::set_flux_to_old_flux(int64_t sr, int g)
1,240✔
165
{
166
  source_regions_.scalar_flux_new(sr, g) =
2,480✔
167
    source_regions_.scalar_flux_old(sr, g);
1,240✔
168
  source_regions_.flux_moments_new(sr, g) =
2,480✔
169
    source_regions_.flux_moments_old(sr, g);
1,240✔
170
}
1,240✔
171

172
void LinearSourceDomain::accumulate_iteration_flux()
2,600✔
173
{
174
  // Accumulate scalar flux
175
  FlatSourceDomain::accumulate_iteration_flux();
2,600✔
176

177
  // Accumulate scalar flux moments
178
#pragma omp parallel for
1,560✔
179
  for (int64_t se = 0; se < n_source_elements(); se++) {
5,269,440✔
180
    source_regions_.flux_moments_t(se) += source_regions_.flux_moments_new(se);
5,268,400✔
181
  }
182
}
2,600✔
183

UNCOV
184
double LinearSourceDomain::evaluate_flux_at_point(
×
185
  Position r, int64_t sr, int g) const
186
{
187
  double phi_flat = FlatSourceDomain::evaluate_flux_at_point(r, sr, g);
×
188

189
  Position local_r = r - source_regions_.centroid(sr);
×
UNCOV
190
  MomentArray phi_linear = source_regions_.flux_moments_t(sr, g);
×
191
  phi_linear *= 1.0 / (settings::n_batches - settings::n_inactive);
×
192

UNCOV
193
  MomentMatrix invM = source_regions_.mom_matrix(sr).inverse();
×
194
  MomentArray phi_solved = invM * phi_linear;
×
195

UNCOV
196
  return phi_flat + phi_solved.dot(local_r);
×
197
}
198

199
} // 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