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

openmc-dev / openmc / 20733102133

06 Jan 2026 12:03AM UTC coverage: 81.932% (-0.2%) from 82.174%
20733102133

Pull #3702

github

web-flow
Merge b36a045c7 into 60ddafa9b
Pull Request #3702: Random Ray Kinetic Simulation Mode

17551 of 24380 branches covered (71.99%)

Branch coverage included in aggregate %.

869 of 1117 new or added lines in 21 files covered. (77.8%)

483 existing lines in 19 files now uncovered.

55915 of 65287 relevant lines covered (85.64%)

50695599.69 hits per line

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

86.87
/src/state_point.cpp
1
#include "openmc/state_point.h"
2

3
#include <algorithm>
4
#include <cstdint> // for int64_t
5
#include <string>
6

7
#include "xtensor/xbuilder.hpp" // for empty_like
8
#include "xtensor/xview.hpp"
9
#include <fmt/core.h>
10

11
#include "openmc/bank.h"
12
#include "openmc/bank_io.h"
13
#include "openmc/capi.h"
14
#include "openmc/constants.h"
15
#include "openmc/eigenvalue.h"
16
#include "openmc/error.h"
17
#include "openmc/file_utils.h"
18
#include "openmc/hdf5_interface.h"
19
#include "openmc/mcpl_interface.h"
20
#include "openmc/mesh.h"
21
#include "openmc/message_passing.h"
22
#include "openmc/mgxs_interface.h"
23
#include "openmc/nuclide.h"
24
#include "openmc/output.h"
25
#include "openmc/random_ray/random_ray_simulation.h"
26
#include "openmc/settings.h"
27
#include "openmc/simulation.h"
28
#include "openmc/tallies/derivative.h"
29
#include "openmc/tallies/filter.h"
30
#include "openmc/tallies/filter_mesh.h"
31
#include "openmc/tallies/tally.h"
32
#include "openmc/timer.h"
33
#include "openmc/vector.h"
34

35
namespace openmc {
36

37
extern "C" int openmc_statepoint_write(const char* filename, bool* write_source)
8,545✔
38
{
39
  simulation::time_statepoint.start();
8,545✔
40

41
  // If a nullptr is passed in, we assume that the user
42
  // wants a default name for this, of the form like output/statepoint.20.h5
43
  std::string filename_;
8,545✔
44
  if (filename) {
8,545✔
45
    filename_ = filename;
797✔
46
  } else {
47
    // Determine width for zero padding
48
    int w = std::to_string(settings::n_max_batches).size();
7,748✔
49

50
    // Set filename for state point
51
    filename_ = fmt::format("{0}statepoint.{1:0{2}}.h5", settings::path_output,
14,051✔
52
      simulation::current_batch, w);
7,748✔
53
  }
54

55
  // If a file name was specified, ensure it has .h5 file extension
56
  const auto extension = get_file_extension(filename_);
8,545✔
57
  if (extension != "h5") {
8,545!
58
    warning("openmc_statepoint_write was passed a file extension differing "
×
59
            "from .h5, but an hdf5 file will be written.");
60
  }
61

62
  // Determine whether or not to write the source bank
63
  bool write_source_ = write_source ? *write_source : true;
8,545!
64

65
  // Write message
66
  write_message("Creating state point " + filename_ + "...", 5);
8,545✔
67

68
  hid_t file_id;
69
  if (mpi::master) {
8,545✔
70
    // Create statepoint file
71
    file_id = file_open(filename_, 'w');
7,160✔
72

73
    // Write file type
74
    write_attribute(file_id, "filetype", "statepoint");
7,160✔
75

76
    // Write revision number for state point file
77
    write_attribute(file_id, "version", VERSION_STATEPOINT);
7,160✔
78

79
    // Write OpenMC version
80
    write_attribute(file_id, "openmc_version", VERSION);
7,160✔
81
#ifdef GIT_SHA1
82
    write_attribute(file_id, "git_sha1", GIT_SHA1);
83
#endif
84

85
    // Write current date and time
86
    write_attribute(file_id, "date_and_time", time_stamp());
7,160✔
87

88
    // Write path to input
89
    write_attribute(file_id, "path", settings::path_input);
7,160✔
90

91
    // Write out random number seed
92
    write_dataset(file_id, "seed", openmc_get_seed());
7,160✔
93

94
    // Write out random number stride
95
    write_dataset(file_id, "stride", openmc_get_stride());
7,160✔
96

97
    // Write run information
98
    write_dataset(file_id, "energy_mode",
7,160✔
99
      settings::run_CE ? "continuous-energy" : "multi-group");
100
    if (!settings::run_CE) {
7,160✔
101
      write_dataset(file_id, "n_energy_groups", data::mg.num_energy_groups_);
1,399✔
102
      write_dataset(file_id, "n_delay_groups", data::mg.num_delayed_groups_);
1,399✔
103
    }
104
    switch (settings::run_mode) {
7,160!
105
    case RunMode::FIXED_SOURCE:
2,562✔
106
      write_dataset(file_id, "run_mode", "fixed source");
2,562✔
107
      break;
2,562✔
108
    case RunMode::EIGENVALUE:
4,598✔
109
      write_dataset(file_id, "run_mode", "eigenvalue");
4,598✔
110
      break;
4,598✔
111
    default:
×
112
      break;
×
113
    }
114
    switch (settings::solver_type) {
7,160!
115
    case SolverType::MONTE_CARLO:
6,124✔
116
      write_dataset(file_id, "solver_type", "monte carlo");
6,124✔
117
      break;
6,124✔
118
    case SolverType::RANDOM_RAY:
1,036✔
119
      write_dataset(file_id, "solver_type", "random ray");
1,036✔
120
      write_random_ray_hdf5(file_id);
1,036✔
121
      break;
1,036✔
NEW
122
    default:
×
NEW
123
      break;
×
124
    }
125
    write_attribute(file_id, "photon_transport", settings::photon_transport);
7,160✔
126
    write_dataset(file_id, "n_particles", settings::n_particles);
7,160✔
127
    write_dataset(file_id, "n_batches", settings::n_batches);
7,160✔
128

129
    write_dataset(file_id, "kinetic_simulation",
7,160✔
130
      settings::kinetic_simulation ? true : false);
131
    if (settings::kinetic_simulation) {
7,160✔
132
      hid_t timestep_group = create_group(file_id, "timestep_data");
539✔
133
      write_dataset(timestep_group, "dt", settings::dt);
539✔
134
      write_dataset(
539✔
135
        timestep_group, "current_timestep", simulation::current_timestep);
136
      write_dataset(timestep_group, "current_time", simulation::current_time);
539✔
137
      close_group(timestep_group);
539✔
138
    }
139

140
    // Write out current batch number
141
    write_dataset(file_id, "current_batch", simulation::current_batch);
7,160✔
142

143
    // Indicate whether source bank is stored in statepoint
144
    write_attribute(file_id, "source_present", write_source_);
7,160✔
145

146
    // Write out information for eigenvalue run
147
    if (settings::run_mode == RunMode::EIGENVALUE)
7,160✔
148
      write_eigenvalue_hdf5(file_id);
4,598✔
149

150
    hid_t tallies_group = create_group(file_id, "tallies");
7,160✔
151

152
    // Write meshes
153
    meshes_to_hdf5(tallies_group);
7,160✔
154

155
    // Write information for derivatives
156
    if (!model::tally_derivs.empty()) {
7,160✔
157
      hid_t derivs_group = create_group(tallies_group, "derivatives");
11✔
158
      for (const auto& deriv : model::tally_derivs) {
66✔
159
        hid_t deriv_group =
160
          create_group(derivs_group, "derivative " + std::to_string(deriv.id));
55✔
161
        write_dataset(deriv_group, "material", deriv.diff_material);
55✔
162
        if (deriv.variable == DerivativeVariable::DENSITY) {
55✔
163
          write_dataset(deriv_group, "independent variable", "density");
22✔
164
        } else if (deriv.variable == DerivativeVariable::NUCLIDE_DENSITY) {
33✔
165
          write_dataset(deriv_group, "independent variable", "nuclide_density");
22✔
166
          write_dataset(
22✔
167
            deriv_group, "nuclide", data::nuclides[deriv.diff_nuclide]->name_);
22✔
168
        } else if (deriv.variable == DerivativeVariable::TEMPERATURE) {
11!
169
          write_dataset(deriv_group, "independent variable", "temperature");
11✔
170
        } else {
171
          fatal_error("Independent variable for derivative " +
×
172
                      std::to_string(deriv.id) +
×
173
                      " not defined in state_point.cpp");
174
        }
175
        close_group(deriv_group);
55✔
176
      }
177
      close_group(derivs_group);
11✔
178
    }
179

180
    // Write information for filters
181
    hid_t filters_group = create_group(tallies_group, "filters");
7,160✔
182
    write_attribute(filters_group, "n_filters", model::tally_filters.size());
7,160✔
183
    if (!model::tally_filters.empty()) {
7,160✔
184
      // Write filter IDs
185
      vector<int32_t> filter_ids;
4,367✔
186
      filter_ids.reserve(model::tally_filters.size());
4,367✔
187
      for (const auto& filt : model::tally_filters)
15,404✔
188
        filter_ids.push_back(filt->id());
11,037✔
189
      write_attribute(filters_group, "ids", filter_ids);
4,367✔
190

191
      // Write info for each filter
192
      for (const auto& filt : model::tally_filters) {
15,404✔
193
        hid_t filter_group =
194
          create_group(filters_group, "filter " + std::to_string(filt->id()));
11,037✔
195
        filt->to_statepoint(filter_group);
11,037✔
196
        close_group(filter_group);
11,037✔
197
      }
198
    }
4,367✔
199
    close_group(filters_group);
7,160✔
200

201
    // Write information for tallies
202
    write_attribute(tallies_group, "n_tallies", model::tallies.size());
7,160✔
203
    if (!model::tallies.empty()) {
7,160✔
204
      // Write tally IDs
205
      vector<int32_t> tally_ids;
4,895✔
206
      tally_ids.reserve(model::tallies.size());
4,895✔
207
      for (const auto& tally : model::tallies)
27,049✔
208
        tally_ids.push_back(tally->id_);
22,154✔
209
      write_attribute(tallies_group, "ids", tally_ids);
4,895✔
210

211
      // Write all tally information except results
212
      for (const auto& tally : model::tallies) {
27,049✔
213
        hid_t tally_group =
214
          create_group(tallies_group, "tally " + std::to_string(tally->id_));
22,154✔
215

216
        write_dataset(tally_group, "name", tally->name_);
22,154✔
217

218
        if (tally->writable_) {
22,154✔
219
          write_attribute(tally_group, "internal", 0);
21,116✔
220
        } else {
221
          write_attribute(tally_group, "internal", 1);
1,038✔
222
          close_group(tally_group);
1,038✔
223
          continue;
1,038✔
224
        }
225

226
        if (tally->multiply_density()) {
21,116✔
227
          write_attribute(tally_group, "multiply_density", 1);
21,061✔
228
        } else {
229
          write_attribute(tally_group, "multiply_density", 0);
55✔
230
        }
231

232
        if (tally->higher_moments()) {
21,116✔
233
          write_attribute(tally_group, "higher_moments", 1);
11✔
234
        } else {
235
          write_attribute(tally_group, "higher_moments", 0);
21,105✔
236
        }
237

238
        if (tally->estimator_ == TallyEstimator::ANALOG) {
21,116✔
239
          write_dataset(tally_group, "estimator", "analog");
7,847✔
240
        } else if (tally->estimator_ == TallyEstimator::TRACKLENGTH) {
13,269✔
241
          write_dataset(tally_group, "estimator", "tracklength");
12,405✔
242
        } else if (tally->estimator_ == TallyEstimator::COLLISION) {
864!
243
          write_dataset(tally_group, "estimator", "collision");
864✔
244
        }
245

246
        write_dataset(tally_group, "n_realizations", tally->n_realizations_);
21,116✔
247

248
        // Write the ID of each filter attached to this tally
249
        write_dataset(tally_group, "n_filters", tally->filters().size());
21,116✔
250
        if (!tally->filters().empty()) {
21,116✔
251
          vector<int32_t> filter_ids;
19,785✔
252
          filter_ids.reserve(tally->filters().size());
19,785✔
253
          for (auto i_filt : tally->filters())
59,954✔
254
            filter_ids.push_back(model::tally_filters[i_filt]->id());
40,169✔
255
          write_dataset(tally_group, "filters", filter_ids);
19,785✔
256
        }
19,785✔
257

258
        // Write the nuclides this tally scores
259
        vector<std::string> nuclides;
21,116✔
260
        for (auto i_nuclide : tally->nuclides_) {
49,998✔
261
          if (i_nuclide == -1) {
28,882✔
262
            nuclides.push_back("total");
18,487✔
263
          } else {
264
            if (settings::run_CE) {
10,395✔
265
              nuclides.push_back(data::nuclides[i_nuclide]->name_);
10,285✔
266
            } else {
267
              nuclides.push_back(data::mg.nuclides_[i_nuclide].name);
110✔
268
            }
269
          }
270
        }
271
        write_dataset(tally_group, "nuclides", nuclides);
21,116✔
272

273
        if (tally->deriv_ != C_NONE)
21,116✔
274
          write_dataset(
220✔
275
            tally_group, "derivative", model::tally_derivs[tally->deriv_].id);
220✔
276

277
        // Write the tally score bins
278
        vector<std::string> scores;
21,116✔
279
        for (auto sc : tally->scores_)
51,797✔
280
          scores.push_back(reaction_name(sc));
30,681✔
281
        write_dataset(tally_group, "n_score_bins", scores.size());
21,116✔
282
        write_dataset(tally_group, "score_bins", scores);
21,116✔
283

284
        close_group(tally_group);
21,116✔
285
      }
21,116✔
286
    }
4,895✔
287

288
    if (settings::reduce_tallies) {
7,160✔
289
      // Write global tallies
290
      write_dataset(file_id, "global_tallies", simulation::global_tallies);
7,149✔
291

292
      // Write tallies
293
      if (model::active_tallies.size() > 0) {
7,149✔
294
        // Indicate that tallies are on
295
        write_attribute(file_id, "tallies_present", 1);
4,664✔
296

297
        // Write all tally results
298
        for (const auto& tally : model::tallies) {
26,587✔
299
          if (!tally->writable_)
21,923✔
300
            continue;
818✔
301

302
          // Write results for each bin
303
          std::string name = "tally " + std::to_string(tally->id_);
21,105✔
304
          hid_t tally_group = open_group(tallies_group, name.c_str());
21,105✔
305
          auto& results = tally->results_;
21,105✔
306
          write_tally_results(tally_group, results.shape()[0],
21,105✔
307
            results.shape()[1], results.shape()[2], results.data());
21,105✔
308
          close_group(tally_group);
21,105✔
309
        }
21,105✔
310
      } else {
311
        // Indicate tallies are off
312
        write_attribute(file_id, "tallies_present", 0);
2,485✔
313
      }
314
    }
315

316
    close_group(tallies_group);
7,160✔
317
  }
318

319
  // Check for the no-tally-reduction method
320
  if (!settings::reduce_tallies) {
8,545✔
321
    // If using the no-tally-reduction method, we need to collect tally
322
    // results before writing them to the state point file.
323
    write_tally_results_nr(file_id);
16✔
324

325
  } else if (mpi::master) {
8,529✔
326
    // Write number of global realizations
327
    write_dataset(file_id, "n_realizations", simulation::n_realizations);
7,149✔
328
  }
329

330
  if (mpi::master) {
8,545✔
331
    // Write out the runtime metrics.
332
    using namespace simulation;
333
    hid_t runtime_group = create_group(file_id, "runtime");
7,160✔
334
    write_dataset(
7,160✔
335
      runtime_group, "total initialization", time_initialize.elapsed());
336
    write_dataset(
7,160✔
337
      runtime_group, "reading cross sections", time_read_xs.elapsed());
338
    write_dataset(runtime_group, "simulation",
7,160✔
339
      time_inactive.elapsed() + time_active.elapsed());
7,160✔
340
    write_dataset(runtime_group, "transport", time_transport.elapsed());
7,160✔
341
    if (settings::run_mode == RunMode::EIGENVALUE) {
7,160✔
342
      write_dataset(runtime_group, "inactive batches", time_inactive.elapsed());
4,598✔
343
    }
344
    write_dataset(runtime_group, "active batches", time_active.elapsed());
7,160✔
345
    if (settings::solver_type == SolverType::RANDOM_RAY) {
7,160✔
346
      write_dataset(runtime_group, "source_update", time_update_src.elapsed());
1,036✔
347
      if (settings::kinetic_simulation) {
1,036✔
348
        write_dataset(
539✔
349
          runtime_group, "precursor_update", time_compute_precursors.elapsed());
350
      }
351
    }
352
    if (settings::run_mode == RunMode::EIGENVALUE) {
7,160✔
353
      write_dataset(
4,598✔
354
        runtime_group, "synchronizing fission bank", time_bank.elapsed());
355
      write_dataset(
4,598✔
356
        runtime_group, "sampling source sites", time_bank_sample.elapsed());
357
      write_dataset(
4,598✔
358
        runtime_group, "SEND-RECV source sites", time_bank_sendrecv.elapsed());
359
    }
360
    write_dataset(
7,160✔
361
      runtime_group, "accumulating tallies", time_tallies.elapsed());
362
    write_dataset(runtime_group, "total", time_total.elapsed());
7,160✔
363
    write_dataset(
7,160✔
364
      runtime_group, "writing statepoints", time_statepoint.elapsed());
365
    close_group(runtime_group);
7,160✔
366

367
    file_close(file_id);
7,160✔
368
  }
369

370
#ifdef PHDF5
371
  bool parallel = true;
4,644✔
372
#else
373
  bool parallel = false;
3,901✔
374
#endif
375

376
  // Write the source bank if desired
377
  if (write_source_) {
8,545✔
378
    if (mpi::master || parallel)
3,914!
379
      file_id = file_open(filename_, 'a', true);
3,914✔
380
    write_source_bank(file_id, simulation::source_bank, simulation::work_index);
3,914✔
381
    if (mpi::master || parallel)
3,914!
382
      file_close(file_id);
3,914✔
383
  }
384

385
#if defined(OPENMC_LIBMESH_ENABLED) || defined(OPENMC_DAGMC_ENABLED)
386
  // write unstructured mesh tally files
387
  write_unstructured_mesh_results();
2,557✔
388
#endif
389

390
  simulation::time_statepoint.stop();
8,545✔
391

392
  return 0;
8,545✔
393
}
8,545✔
394

395
void restart_set_keff()
65✔
396
{
397
  if (simulation::restart_batch > settings::n_inactive) {
65!
398
    for (int i = settings::n_inactive; i < simulation::restart_batch; ++i) {
309✔
399
      simulation::k_sum[0] += simulation::k_generation[i];
244✔
400
      simulation::k_sum[1] += std::pow(simulation::k_generation[i], 2);
244✔
401
    }
402
    int n = settings::gen_per_batch * simulation::n_realizations;
65✔
403
    simulation::keff = simulation::k_sum[0] / n;
65✔
404
  } else {
405
    simulation::keff = simulation::k_generation.back();
×
406
  }
407
}
65✔
408

409
void load_state_point()
65✔
410
{
411
  write_message(
65✔
412
    fmt::format("Loading state point {}...", settings::path_statepoint_c), 5);
118✔
413
  openmc_statepoint_load(settings::path_statepoint.c_str());
65✔
414
}
65✔
415

416
void statepoint_version_check(hid_t file_id)
65✔
417
{
418
  // Read revision number for state point file and make sure it matches with
419
  // current version
420
  array<int, 2> version_array;
421
  read_attribute(file_id, "version", version_array);
65✔
422
  if (version_array != VERSION_STATEPOINT) {
65!
423
    fatal_error(
×
424
      "State point version does not match current version in OpenMC.");
425
  }
426
}
65✔
427

428
extern "C" int openmc_statepoint_load(const char* filename)
65✔
429
{
430
  // Open file for reading
431
  hid_t file_id = file_open(filename, 'r', true);
65✔
432

433
  // Read filetype
434
  std::string word;
65✔
435
  read_attribute(file_id, "filetype", word);
65✔
436
  if (word != "statepoint") {
65!
437
    fatal_error("OpenMC tried to restart from a non-statepoint file.");
×
438
  }
439

440
  statepoint_version_check(file_id);
65✔
441

442
  // Read and overwrite random number seed
443
  int64_t seed;
444
  read_dataset(file_id, "seed", seed);
65✔
445
  openmc_set_seed(seed);
65✔
446

447
  // Read and overwrite random number stride
448
  uint64_t stride;
449
  read_dataset(file_id, "stride", stride);
65✔
450
  openmc_set_stride(stride);
65✔
451

452
  // It is not impossible for a state point to be generated from a CE run but
453
  // to be loaded in to an MG run (or vice versa), check to prevent that.
454
  read_dataset(file_id, "energy_mode", word);
65✔
455
  if (word == "multi-group" && settings::run_CE) {
65!
456
    fatal_error("State point file is from multigroup run but current run is "
×
457
                "continous energy.");
458
  } else if (word == "continuous-energy" && !settings::run_CE) {
65!
459
    fatal_error("State point file is from continuous-energy run but current "
×
460
                "run is multigroup!");
461
  }
462

463
  // Read and overwrite run information except number of batches
464
  read_dataset(file_id, "run_mode", word);
65✔
465
  if (word == "fixed source") {
65!
466
    settings::run_mode = RunMode::FIXED_SOURCE;
×
467
  } else if (word == "eigenvalue") {
65!
468
    settings::run_mode = RunMode::EIGENVALUE;
65✔
469
  }
470
  read_attribute(file_id, "photon_transport", settings::photon_transport);
65✔
471
  read_dataset(file_id, "n_particles", settings::n_particles);
65✔
472
  int temp;
473
  read_dataset(file_id, "n_batches", temp);
65✔
474

475
  // Take maximum of statepoint n_batches and input n_batches
476
  settings::n_batches = std::max(settings::n_batches, temp);
65✔
477

478
  // Read batch number to restart at
479
  read_dataset(file_id, "current_batch", simulation::restart_batch);
65✔
480

481
  if (settings::restart_run &&
65!
482
      simulation::restart_batch >= settings::n_max_batches) {
65✔
483
    warning(fmt::format(
11✔
484
      "The number of batches specified for simulation ({}) is smaller "
485
      "than or equal to the number of batches in the restart statepoint file "
486
      "({})",
487
      settings::n_max_batches, simulation::restart_batch));
488
  }
489

490
  // Logical flag for source present in statepoint file
491
  bool source_present;
492
  read_attribute(file_id, "source_present", source_present);
65✔
493

494
  // Read information specific to eigenvalue run
495
  if (settings::run_mode == RunMode::EIGENVALUE) {
65!
496
    read_dataset(file_id, "n_inactive", temp);
65✔
497
    read_eigenvalue_hdf5(file_id);
65✔
498

499
    // Take maximum of statepoint n_inactive and input n_inactive
500
    settings::n_inactive = std::max(settings::n_inactive, temp);
65✔
501

502
    // Check to make sure source bank is present
503
    if (settings::path_sourcepoint == settings::path_statepoint &&
130!
504
        !source_present) {
65!
505
      fatal_error("Source bank must be contained in statepoint restart file");
×
506
    }
507
  }
508

509
  // Read number of realizations for global tallies
510
  read_dataset(file_id, "n_realizations", simulation::n_realizations);
65✔
511

512
  // Set k_sum, keff, and current_batch based on whether restart file is part
513
  // of active cycle or inactive cycle
514
  if (settings::run_mode == RunMode::EIGENVALUE) {
65!
515
    restart_set_keff();
65✔
516
  }
517

518
  // Set current batch number
519
  simulation::current_batch = simulation::restart_batch;
65✔
520

521
  // Read tallies to master. If we are using Parallel HDF5, all processes
522
  // need to be included in the HDF5 calls.
523
#ifdef PHDF5
524
  if (true) {
525
#else
526
  if (mpi::master) {
30!
527
#endif
528
    // Read global tally data
529
    read_dataset_lowlevel(file_id, "global_tallies", H5T_NATIVE_DOUBLE, H5S_ALL,
65✔
530
      false, simulation::global_tallies.data());
65✔
531

532
    // Check if tally results are present
533
    bool present;
534
    read_attribute(file_id, "tallies_present", present);
65✔
535

536
    // Read in sum and sum squared
537
    if (present) {
65!
538
      hid_t tallies_group = open_group(file_id, "tallies");
65✔
539

540
      for (auto& tally : model::tallies) {
223✔
541
        // Read sum, sum_sq, and N for each bin
542
        std::string name = "tally " + std::to_string(tally->id_);
158✔
543
        hid_t tally_group = open_group(tallies_group, name.c_str());
158✔
544

545
        int internal = 0;
158✔
546
        if (attribute_exists(tally_group, "internal")) {
158!
547
          read_attribute(tally_group, "internal", internal);
158✔
548
        }
549
        if (internal) {
158!
550
          tally->writable_ = false;
×
551
        } else {
552
          auto& results = tally->results_;
158✔
553
          read_tally_results(tally_group, results.shape()[0],
316✔
554
            results.shape()[1], results.shape()[2], results.data());
158✔
555

556
          read_dataset(tally_group, "n_realizations", tally->n_realizations_);
158✔
557
          close_group(tally_group);
158✔
558
        }
559
      }
158✔
560
      close_group(tallies_group);
65✔
561
    }
562
  }
563

564
  // Read source if in eigenvalue mode
565
  if (settings::run_mode == RunMode::EIGENVALUE) {
65!
566

567
    // Check if source was written out separately
568
    if (!source_present) {
65!
569

570
      // Close statepoint file
571
      file_close(file_id);
×
572

573
      // Write message
574
      write_message(
×
575
        "Loading source file " + settings::path_sourcepoint + "...", 5);
×
576

577
      // Open source file
578
      file_id = file_open(settings::path_sourcepoint.c_str(), 'r', true);
×
579
    }
580

581
    // Read source
582
    read_source_bank(file_id, simulation::source_bank, true);
65✔
583
  }
584

585
  // Close file
586
  file_close(file_id);
65✔
587

588
  return 0;
65✔
589
}
65✔
590

591
hid_t h5banktype(bool memory)
10,364✔
592
{
593
  // Create compound type for position
594
  hid_t postype = H5Tcreate(H5T_COMPOUND, sizeof(struct Position));
10,364✔
595
  H5Tinsert(postype, "x", HOFFSET(Position, x), H5T_NATIVE_DOUBLE);
10,364✔
596
  H5Tinsert(postype, "y", HOFFSET(Position, y), H5T_NATIVE_DOUBLE);
10,364✔
597
  H5Tinsert(postype, "z", HOFFSET(Position, z), H5T_NATIVE_DOUBLE);
10,364✔
598

599
  // Create bank datatype
600
  //
601
  // If you make changes to the compound datatype here, make sure you update:
602
  // - openmc/source.py
603
  // - openmc/statepoint.py
604
  // - docs/source/io_formats/statepoint.rst
605
  // - docs/source/io_formats/source.rst
606
  auto n = sizeof(SourceSite);
10,364✔
607
  if (!memory)
10,364✔
608
    n = 2 * sizeof(struct Position) + 3 * sizeof(double) + 3 * sizeof(int);
5,118✔
609
  hid_t banktype = H5Tcreate(H5T_COMPOUND, n);
10,364✔
610
  H5Tinsert(banktype, "r", HOFFSET(SourceSite, r), postype);
10,364✔
611
  H5Tinsert(banktype, "u", HOFFSET(SourceSite, u), postype);
10,364✔
612
  H5Tinsert(banktype, "E", HOFFSET(SourceSite, E), H5T_NATIVE_DOUBLE);
10,364✔
613
  H5Tinsert(banktype, "time", HOFFSET(SourceSite, time), H5T_NATIVE_DOUBLE);
10,364✔
614
  H5Tinsert(banktype, "wgt", HOFFSET(SourceSite, wgt), H5T_NATIVE_DOUBLE);
10,364✔
615
  H5Tinsert(banktype, "delayed_group", HOFFSET(SourceSite, delayed_group),
10,364✔
616
    H5T_NATIVE_INT);
10,364✔
617
  H5Tinsert(banktype, "surf_id", HOFFSET(SourceSite, surf_id), H5T_NATIVE_INT);
10,364✔
618
  H5Tinsert(
10,364✔
619
    banktype, "particle", HOFFSET(SourceSite, particle), H5T_NATIVE_INT);
10,364✔
620

621
  H5Tclose(postype);
10,364✔
622
  return banktype;
10,364✔
623
}
624

625
void write_source_point(std::string filename, span<SourceSite> source_bank,
1,242✔
626
  const vector<int64_t>& bank_index, bool use_mcpl)
627
{
628
  std::string ext = use_mcpl ? "mcpl" : "h5";
1,242✔
629
  write_message("Creating source file {}.{} with {} particles ...", filename,
1,242✔
630
    ext, source_bank.size(), 5);
1,242✔
631

632
  // Dispatch to appropriate function based on file type
633
  if (use_mcpl) {
1,242✔
634
    filename.append(".mcpl");
38✔
635
    write_mcpl_source_point(filename.c_str(), source_bank, bank_index);
38✔
636
  } else {
637
    filename.append(".h5");
1,204✔
638
    write_h5_source_point(filename.c_str(), source_bank, bank_index);
1,204✔
639
  }
640
}
1,242✔
641

642
void write_h5_source_point(const char* filename, span<SourceSite> source_bank,
1,204✔
643
  const vector<int64_t>& bank_index)
644
{
645
  // When using parallel HDF5, the file is written to collectively by all
646
  // processes. With MPI-only, the file is opened and written by the master
647
  // (note that the call to write_source_bank is by all processes since slave
648
  // processes need to send source bank data to the master.
649
#ifdef PHDF5
650
  bool parallel = true;
587✔
651
#else
652
  bool parallel = false;
617✔
653
#endif
654

655
  if (!filename)
1,204!
656
    fatal_error("write_source_point filename needs a nonempty name.");
×
657

658
  std::string filename_(filename);
1,204✔
659
  const auto extension = get_file_extension(filename_);
1,204✔
660
  if (extension != "h5") {
1,204!
661
    warning("write_source_point was passed a file extension differing "
×
662
            "from .h5, but an hdf5 file will be written.");
663
  }
664

665
  hid_t file_id;
666
  if (mpi::master || parallel) {
1,204!
667
    file_id = file_open(filename_.c_str(), 'w', true);
1,204✔
668
    write_attribute(file_id, "filetype", "source");
1,204✔
669
  }
670

671
  // Get pointer to source bank and write to file
672
  write_source_bank(file_id, source_bank, bank_index);
1,204✔
673

674
  if (mpi::master || parallel)
1,204!
675
    file_close(file_id);
1,204✔
676
}
1,204✔
677

678
void write_source_bank(hid_t group_id, span<SourceSite> source_bank,
5,118✔
679
  const vector<int64_t>& bank_index)
680
{
681
  hid_t membanktype = h5banktype(true);
5,118✔
682
  hid_t filebanktype = h5banktype(false);
5,118✔
683

684
#ifdef OPENMC_MPI
685
  write_bank_dataset("source_bank", group_id, source_bank, bank_index,
2,756✔
686
    membanktype, filebanktype, mpi::source_site);
687
#else
688
  write_bank_dataset("source_bank", group_id, source_bank, bank_index,
2,362✔
689
    membanktype, filebanktype);
690
#endif
691

692
  H5Tclose(membanktype);
5,118✔
693
  H5Tclose(filebanktype);
5,118✔
694
}
5,118✔
695

696
// Determine member names of a compound HDF5 datatype
697
std::string dtype_member_names(hid_t dtype_id)
256✔
698
{
699
  int nmembers = H5Tget_nmembers(dtype_id);
256✔
700
  std::string names;
256✔
701
  for (int i = 0; i < nmembers; i++) {
2,259✔
702
    char* name = H5Tget_member_name(dtype_id, i);
2,003✔
703
    names = names.append(name);
2,003✔
704
    H5free_memory(name);
2,003✔
705
    if (i < nmembers - 1)
2,003✔
706
      names += ", ";
1,747✔
707
  }
708
  return names;
256✔
709
}
×
710

711
void read_source_bank(
128✔
712
  hid_t group_id, vector<SourceSite>& sites, bool distribute)
713
{
714
  hid_t banktype = h5banktype(true);
128✔
715

716
  // Open the dataset
717
  hid_t dset = H5Dopen(group_id, "source_bank", H5P_DEFAULT);
128✔
718

719
  // Make sure number of members matches
720
  hid_t dtype = H5Dget_type(dset);
128✔
721
  auto file_member_names = dtype_member_names(dtype);
128✔
722
  auto bank_member_names = dtype_member_names(banktype);
128✔
723
  if (file_member_names != bank_member_names) {
128✔
724
    fatal_error(fmt::format(
9✔
725
      "Source site attributes in file do not match what is "
726
      "expected for this version of OpenMC. File attributes = ({}). Expected "
727
      "attributes = ({})",
728
      file_member_names, bank_member_names));
729
  }
730

731
  hid_t dspace = H5Dget_space(dset);
119✔
732
  hsize_t n_sites;
733
  H5Sget_simple_extent_dims(dspace, &n_sites, nullptr);
119✔
734

735
  // Make sure vector is big enough in case where we're reading entire source on
736
  // each process
737
  if (!distribute)
119✔
738
    sites.resize(n_sites);
54✔
739

740
  hid_t memspace;
741
  if (distribute) {
119✔
742
    if (simulation::work_index[mpi::n_procs] > n_sites) {
65!
743
      fatal_error("Number of source sites in source file is less "
×
744
                  "than number of source particles per generation.");
745
    }
746

747
    // Create another data space but for each proc individually
748
    hsize_t n_sites_local = simulation::work_per_rank;
65✔
749
    memspace = H5Screate_simple(1, &n_sites_local, nullptr);
65✔
750

751
    // Select hyperslab for each process
752
    hsize_t offset = simulation::work_index[mpi::rank];
65✔
753
    H5Sselect_hyperslab(
65✔
754
      dspace, H5S_SELECT_SET, &offset, nullptr, &n_sites_local, nullptr);
755
  } else {
756
    memspace = H5S_ALL;
54✔
757
  }
758

759
#ifdef PHDF5
760
  // Read data in parallel
761
  hid_t plist = H5Pcreate(H5P_DATASET_XFER);
65✔
762
  H5Pset_dxpl_mpio(plist, H5FD_MPIO_COLLECTIVE);
65✔
763
  H5Dread(dset, banktype, memspace, dspace, plist, sites.data());
65✔
764
  H5Pclose(plist);
65✔
765
#else
766
  H5Dread(dset, banktype, memspace, dspace, H5P_DEFAULT, sites.data());
54✔
767
#endif
768

769
  // Close all ids
770
  H5Sclose(dspace);
119✔
771
  if (distribute)
119✔
772
    H5Sclose(memspace);
65✔
773
  H5Dclose(dset);
119✔
774
  H5Tclose(banktype);
119✔
775
}
119✔
776

777
void write_unstructured_mesh_results()
2,557✔
778
{
779

780
  for (auto& tally : model::tallies) {
11,495✔
781

782
    vector<std::string> tally_scores;
8,938✔
783
    for (auto filter_idx : tally->filters()) {
26,089✔
784
      auto& filter = model::tally_filters[filter_idx];
17,151✔
785
      if (filter->type() != FilterType::MESH)
17,151!
786
        continue;
17,136✔
787

788
      // check if the filter uses an unstructured mesh
789
      auto mesh_filter = dynamic_cast<MeshFilter*>(filter.get());
2,002!
790
      auto mesh_idx = mesh_filter->mesh();
2,002!
791
      auto umesh =
792
        dynamic_cast<UnstructuredMesh*>(model::meshes[mesh_idx].get());
2,002!
793

794
      if (!umesh)
2,002✔
795
        continue;
1,967✔
796

797
      if (!umesh->output_)
35!
798
        continue;
×
799

800
      if (umesh->library() == "moab") {
35!
801
        if (mpi::master)
20✔
802
          warning(fmt::format(
10!
803
            "Output for a MOAB mesh (mesh {}) was "
804
            "requested but will not be written. Please use the Python "
805
            "API to generated the desired VTK tetrahedral mesh.",
806
            umesh->id_));
10✔
807
        continue;
20✔
808
      }
809

810
      // if this tally has more than one filter, print
811
      // warning and skip writing the mesh
812
      if (tally->filters().size() > 1) {
15!
813
        warning(fmt::format("Skipping unstructured mesh writing for tally "
×
814
                            "{}. More than one filter is present on the tally.",
815
          tally->id_));
×
816
        break;
×
817
      }
818

819
      int n_realizations = tally->n_realizations_;
15✔
820

821
      for (int score_idx = 0; score_idx < tally->scores_.size(); score_idx++) {
30✔
822
        for (int nuc_idx = 0; nuc_idx < tally->nuclides_.size(); nuc_idx++) {
30✔
823
          // combine the score and nuclide into a name for the value
824
          auto score_str = fmt::format("{}_{}", tally->score_name(score_idx),
30!
825
            tally->nuclide_name(nuc_idx));
30!
826
          // add this score to the mesh
827
          // (this is in a separate loop because all variables need to be added
828
          //  to libMesh's equation system before any are initialized, which
829
          //  happens in set_score_data)
830
          umesh->add_score(score_str);
15!
831
        }
15✔
832
      }
833

834
      for (int score_idx = 0; score_idx < tally->scores_.size(); score_idx++) {
30✔
835
        for (int nuc_idx = 0; nuc_idx < tally->nuclides_.size(); nuc_idx++) {
30✔
836
          // combine the score and nuclide into a name for the value
837
          auto score_str = fmt::format("{}_{}", tally->score_name(score_idx),
30!
838
            tally->nuclide_name(nuc_idx));
30!
839

840
          // index for this nuclide and score
841
          int nuc_score_idx = score_idx + nuc_idx * tally->scores_.size();
15✔
842

843
          // construct result vectors
844
          vector<double> mean_vec(umesh->n_bins()),
15!
845
            std_dev_vec(umesh->n_bins());
15!
846
          for (int j = 0; j < tally->results_.shape()[0]; j++) {
146,799✔
847
            // get the volume for this bin
848
            double volume = umesh->volume(j);
146,784!
849
            // compute the mean
850
            double mean = tally->results_(j, nuc_score_idx, TallyResult::SUM) /
146,784!
851
                          n_realizations;
146,784✔
852
            mean_vec.at(j) = mean / volume;
146,784!
853

854
            // compute the standard deviation
855
            double sum_sq =
856
              tally->results_(j, nuc_score_idx, TallyResult::SUM_SQ);
146,784!
857
            double std_dev {0.0};
146,784✔
858
            if (n_realizations > 1) {
146,784!
859
              std_dev = sum_sq / n_realizations - mean * mean;
146,784✔
860
              std_dev = std::sqrt(std_dev / (n_realizations - 1));
146,784✔
861
            }
862
            std_dev_vec[j] = std_dev / volume;
146,784✔
863
          }
864
#ifdef OPENMC_MPI
865
          MPI_Bcast(
10!
866
            mean_vec.data(), mean_vec.size(), MPI_DOUBLE, 0, mpi::intracomm);
10✔
867
          MPI_Bcast(std_dev_vec.data(), std_dev_vec.size(), MPI_DOUBLE, 0,
10!
868
            mpi::intracomm);
869
#endif
870
          // set the data for this score
871
          umesh->set_score_data(score_str, mean_vec, std_dev_vec);
15!
872
        }
15✔
873
      }
874

875
      // Generate a file name based on the tally id
876
      // and the current batch number
877
      size_t batch_width {std::to_string(settings::n_max_batches).size()};
15!
878
      std::string filename = fmt::format("tally_{0}.{1:0{2}}", tally->id_,
15✔
879
        simulation::current_batch, batch_width);
×
880

881
      // Write the unstructured mesh and data to file
882
      umesh->write(filename);
15!
883

884
      // remove score data added for this mesh write
885
      umesh->remove_scores();
15!
886
    }
15✔
887
  }
8,938✔
888
}
2,557✔
889

890
void write_tally_results_nr(hid_t file_id)
16✔
891
{
892
  // ==========================================================================
893
  // COLLECT AND WRITE GLOBAL TALLIES
894

895
  hid_t tallies_group;
896
  if (mpi::master) {
16✔
897
    // Write number of realizations
898
    write_dataset(file_id, "n_realizations", simulation::n_realizations);
11✔
899

900
    tallies_group = open_group(file_id, "tallies");
11✔
901
  }
902

903
  // Get global tallies
904
  auto& gt = simulation::global_tallies;
16✔
905

906
#ifdef OPENMC_MPI
907
  // Reduce global tallies
908
  xt::xtensor<double, 2> gt_reduced = xt::empty_like(gt);
10✔
909
  MPI_Reduce(gt.data(), gt_reduced.data(), gt.size(), MPI_DOUBLE, MPI_SUM, 0,
10✔
910
    mpi::intracomm);
911

912
  // Transfer values to value on master
913
  if (mpi::master) {
10✔
914
    if (simulation::current_batch == settings::n_max_batches ||
5!
915
        simulation::satisfy_triggers) {
916
      std::copy(gt_reduced.begin(), gt_reduced.end(), gt.begin());
5✔
917
    }
918
  }
919
#endif
920

921
  // Write out global tallies sum and sum_sq
922
  if (mpi::master) {
16✔
923
    write_dataset(file_id, "global_tallies", gt);
11✔
924
  }
925

926
  for (const auto& t : model::tallies) {
32✔
927
    // Skip any tallies that are not active
928
    if (!t->active_)
16!
929
      continue;
×
930
    if (!t->writable_)
16!
931
      continue;
×
932

933
    if (mpi::master && !attribute_exists(file_id, "tallies_present")) {
16!
934
      write_attribute(file_id, "tallies_present", 1);
11✔
935
    }
936

937
    // Get view of accumulated tally values
938
    auto values_view = xt::view(t->results_, xt::all(), xt::all(),
16✔
939
      xt::range(static_cast<int>(TallyResult::SUM),
16✔
940
        static_cast<int>(TallyResult::SUM_SQ) + 1));
16✔
941

942
    // Make copy of tally values in contiguous array
943
    xt::xtensor<double, 3> values = values_view;
16✔
944

945
    if (mpi::master) {
16✔
946
      // Open group for tally
947
      std::string groupname {"tally " + std::to_string(t->id_)};
11✔
948
      hid_t tally_group = open_group(tallies_group, groupname.c_str());
11✔
949

950
      // The MPI_IN_PLACE specifier allows the master to copy values into
951
      // a receive buffer without having a temporary variable
952
#ifdef OPENMC_MPI
953
      MPI_Reduce(MPI_IN_PLACE, values.data(), values.size(), MPI_DOUBLE,
5✔
954
        MPI_SUM, 0, mpi::intracomm);
955
#endif
956

957
      // At the end of the simulation, store the results back in the
958
      // regular TallyResults array
959
      if (simulation::current_batch == settings::n_max_batches ||
11!
960
          simulation::satisfy_triggers) {
961
        values_view = values;
11✔
962
      }
963

964
      // Put in temporary tally result
965
      xt::xtensor<double, 3> results_copy = xt::zeros_like(t->results_);
11✔
966
      auto copy_view = xt::view(results_copy, xt::all(), xt::all(),
11✔
967
        xt::range(static_cast<int>(TallyResult::SUM),
11✔
968
          static_cast<int>(TallyResult::SUM_SQ) + 1));
11✔
969
      copy_view = values;
11✔
970

971
      // Write reduced tally results to file
972
      auto shape = results_copy.shape();
11✔
973
      write_tally_results(
11✔
974
        tally_group, shape[0], shape[1], shape[2], results_copy.data());
11✔
975

976
      close_group(tally_group);
11✔
977
    } else {
11✔
978
      // Receive buffer not significant at other processors
979
#ifdef OPENMC_MPI
980
      MPI_Reduce(values.data(), nullptr, values.size(), MPI_DOUBLE, MPI_SUM, 0,
5✔
981
        mpi::intracomm);
982
#endif
983
    }
984
  }
16✔
985

986
  if (mpi::master) {
16✔
987
    if (!object_exists(file_id, "tallies_present")) {
11!
988
      // Indicate that tallies are off
989
      write_dataset(file_id, "tallies_present", 0);
11✔
990
    }
991

992
    close_group(tallies_group);
11✔
993
  }
994
}
16✔
995

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

© 2026 Coveralls, Inc