-
Notifications
You must be signed in to change notification settings - Fork 654
Expand file tree
/
Copy pathinitialize.cpp
More file actions
587 lines (493 loc) · 17.8 KB
/
Copy pathinitialize.cpp
File metadata and controls
587 lines (493 loc) · 17.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
#include "openmc/initialize.h"
#include <clocale>
#include <cstddef>
#include <cstdlib> // for getenv
#include <cstring>
#include <string>
#ifdef _OPENMP
#include <omp.h>
#endif
#include <fmt/core.h>
#include "openmc/capi.h"
#include "openmc/chain.h"
#include "openmc/constants.h"
#include "openmc/cross_sections.h"
#include "openmc/error.h"
#include "openmc/file_utils.h"
#include "openmc/geometry_aux.h"
#include "openmc/hdf5_interface.h"
#include "openmc/material.h"
#include "openmc/memory.h"
#include "openmc/message_passing.h"
#include "openmc/mgxs_interface.h"
#include "openmc/nuclide.h"
#include "openmc/openmp_interface.h"
#include "openmc/output.h"
#include "openmc/plot.h"
#include "openmc/random_lcg.h"
#include "openmc/settings.h"
#include "openmc/simulation.h"
#include "openmc/string_utils.h"
#include "openmc/summary.h"
#include "openmc/tallies/tally.h"
#include "openmc/thermal.h"
#include "openmc/timer.h"
#include "openmc/vector.h"
#include "openmc/weight_windows.h"
#ifdef OPENMC_LIBMESH_ENABLED
#include "libmesh/libmesh.h"
#endif
int openmc_init(int argc, char* argv[], const void* intracomm)
{
using namespace openmc;
#ifdef OPENMC_MPI
// Check if intracomm was passed
MPI_Comm comm;
if (intracomm) {
comm = *static_cast<const MPI_Comm*>(intracomm);
} else {
comm = MPI_COMM_WORLD;
}
// Initialize MPI for C++
initialize_mpi(comm);
#endif
// Parse command-line arguments
int err = parse_command_line(argc, argv);
if (err)
return err;
#ifdef OPENMC_LIBMESH_ENABLED
const int n_threads = num_threads();
// initialize libMesh if it hasn't been initialized already
// (if initialized externally, the libmesh_init object needs to be provided
// also)
if (!settings::libmesh_init && !libMesh::initialized()) {
#ifdef OPENMC_MPI
// pass command line args, empty MPI communicator, and number of threads.
// Because libMesh was not initialized, we assume that OpenMC is the primary
// application and that its main MPI comm should be used.
settings::libmesh_init =
make_unique<libMesh::LibMeshInit>(argc, argv, comm, n_threads);
#else
// pass command line args, empty MPI communicator, and number of threads
settings::libmesh_init =
make_unique<libMesh::LibMeshInit>(argc, argv, 0, n_threads);
#endif
settings::libmesh_comm = &(settings::libmesh_init->comm());
}
#endif
// Start total and initialization timer
simulation::time_total.start();
simulation::time_initialize.start();
#ifdef _OPENMP
// If OMP_SCHEDULE is not set, default to a static schedule
char* envvar = std::getenv("OMP_SCHEDULE");
if (!envvar) {
omp_set_schedule(omp_sched_static, 0);
}
#endif
// Initialize random number generator -- if the user specifies a seed and/or
// stride, it will be re-initialized later
openmc::openmc_set_seed(DEFAULT_SEED);
openmc::openmc_set_stride(DEFAULT_STRIDE);
// Copy previous locale and set locale to C. This is a workaround for an issue
// whereby when openmc_init is called from the plotter, the Qt application
// framework first calls std::setlocale, which affects how pugixml reads
// floating point numbers due to a bug:
// https://github.com/zeux/pugixml/issues/469
std::string prev_locale = std::setlocale(LC_ALL, nullptr);
if (std::setlocale(LC_ALL, "C") == NULL) {
fatal_error("Cannot set locale to C.");
}
// Read XML input files
if (!read_model_xml())
read_separate_xml_files();
if (!settings::properties_file.empty()) {
openmc_properties_import(settings::properties_file.c_str());
}
// Reset locale to previous state
if (std::setlocale(LC_ALL, prev_locale.c_str()) == NULL) {
fatal_error("Cannot reset locale.");
}
// Write some initial output under the header if needed
initial_output();
// Check for particle restart run
if (settings::particle_restart_run)
settings::run_mode = RunMode::PARTICLE;
// Stop initialization timer
simulation::time_initialize.stop();
simulation::time_total.stop();
return 0;
}
namespace openmc {
#ifdef OPENMC_MPI
void initialize_mpi(MPI_Comm intracomm)
{
mpi::intracomm = intracomm;
// Initialize MPI
int flag;
MPI_Initialized(&flag);
if (!flag)
MPI_Init(nullptr, nullptr);
// Determine number of processes and rank for each
MPI_Comm_size(intracomm, &mpi::n_procs);
MPI_Comm_rank(intracomm, &mpi::rank);
mpi::master = (mpi::rank == 0);
// Create bank datatype
SourceSite b;
MPI_Aint disp[15];
MPI_Get_address(&b.r, &disp[0]);
MPI_Get_address(&b.u, &disp[1]);
MPI_Get_address(&b.E, &disp[2]);
MPI_Get_address(&b.time, &disp[3]);
MPI_Get_address(&b.wgt, &disp[4]);
MPI_Get_address(&b.delayed_group, &disp[5]);
MPI_Get_address(&b.surf_id, &disp[6]);
MPI_Get_address(&b.particle, &disp[7]);
MPI_Get_address(&b.parent_nuclide, &disp[8]);
MPI_Get_address(&b.parent_id, &disp[9]);
MPI_Get_address(&b.progeny_id, &disp[10]);
MPI_Get_address(&b.wgt_born, &disp[11]);
MPI_Get_address(&b.wgt_ww_born, &disp[12]);
MPI_Get_address(&b.n_split, &disp[13]);
MPI_Get_address(&b.n_collision, &disp[14]);
for (int i = 14; i >= 0; --i) {
disp[i] -= disp[0];
}
// Block counts for each field
int blocks[] = {3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
// Types for each field
MPI_Datatype types[] = {
MPI_DOUBLE, // r (3 doubles)
MPI_DOUBLE, // u (3 doubles)
MPI_DOUBLE, // E
MPI_DOUBLE, // time
MPI_DOUBLE, // wgt
MPI_INT, // delayed_group
MPI_INT, // surf_id
MPI_INT, // particle (enum)
MPI_INT, // parent_nuclide
MPI_INT64_T, // parent_id
MPI_INT64_T, // progeny_id
MPI_DOUBLE, // wgt_born
MPI_DOUBLE, // wgt_ww_born
MPI_INT64_T, // n_split
MPI_INT // n_collision
};
MPI_Type_create_struct(15, blocks, disp, types, &mpi::source_site);
MPI_Type_commit(&mpi::source_site);
CollisionTrackSite bc;
MPI_Aint dispc[16];
MPI_Get_address(&bc.r, &dispc[0]); // double
MPI_Get_address(&bc.u, &dispc[1]); // double
MPI_Get_address(&bc.E, &dispc[2]); // double
MPI_Get_address(&bc.dE, &dispc[3]); // double
MPI_Get_address(&bc.time, &dispc[4]); // double
MPI_Get_address(&bc.wgt, &dispc[5]); // double
MPI_Get_address(&bc.event_mt, &dispc[6]); // int
MPI_Get_address(&bc.delayed_group, &dispc[7]); // int
MPI_Get_address(&bc.cell_id, &dispc[8]); // int
MPI_Get_address(&bc.nuclide_id, &dispc[9]); // int
MPI_Get_address(&bc.material_id, &dispc[10]); // int
MPI_Get_address(&bc.universe_id, &dispc[11]); // int
MPI_Get_address(&bc.n_collision, &dispc[12]); // int
MPI_Get_address(&bc.particle, &dispc[13]); // int
MPI_Get_address(&bc.parent_id, &dispc[14]); // int64_t
MPI_Get_address(&bc.progeny_id, &dispc[15]); // int64_t
for (int i = 15; i >= 0; --i) {
dispc[i] -= dispc[0];
}
int blocksc[] = {3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
MPI_Datatype typesc[] = {MPI_DOUBLE, MPI_DOUBLE, MPI_DOUBLE, MPI_DOUBLE,
MPI_DOUBLE, MPI_DOUBLE, MPI_INT, MPI_INT, MPI_INT, MPI_INT, MPI_INT,
MPI_INT, MPI_INT, MPI_INT, MPI_INT64_T, MPI_INT64_T};
MPI_Type_create_struct(
16, blocksc, dispc, typesc, &mpi::collision_track_site);
MPI_Type_commit(&mpi::collision_track_site);
}
#endif // OPENMC_MPI
int parse_command_line(int argc, char* argv[])
{
int last_flag = 0;
for (int i = 1; i < argc; ++i) {
std::string arg {argv[i]};
if (arg[0] == '-') {
if (arg == "-p" || arg == "--plot") {
settings::run_mode = RunMode::PLOTTING;
settings::check_overlaps = true;
} else if (arg == "-n" || arg == "--particles") {
i += 1;
settings::n_particles = std::stoll(argv[i]);
} else if (arg == "-q" || arg == "--verbosity") {
i += 1;
settings::verbosity = std::stoi(argv[i]);
if (settings::verbosity > 10 || settings::verbosity < 1) {
auto msg = fmt::format("Invalid verbosity: {}.", settings::verbosity);
strcpy(openmc_err_msg, msg.c_str());
return OPENMC_E_INVALID_ARGUMENT;
}
} else if (arg == "-e" || arg == "--event") {
settings::event_based = true;
} else if (arg == "-r" || arg == "--restart") {
i += 1;
// Check what type of file this is
hid_t file_id = file_open(argv[i], 'r', true);
std::string filetype;
read_attribute(file_id, "filetype", filetype);
file_close(file_id);
// Set path and flag for type of run
if (filetype == "statepoint") {
settings::path_statepoint = argv[i];
settings::path_statepoint_c = settings::path_statepoint.c_str();
settings::restart_run = true;
} else if (filetype == "particle restart") {
settings::path_particle_restart = argv[i];
settings::particle_restart_run = true;
} else {
auto msg =
fmt::format("Unrecognized file after restart flag: {}.", filetype);
strcpy(openmc_err_msg, msg.c_str());
return OPENMC_E_INVALID_ARGUMENT;
}
// If its a restart run check for additional source file
if (settings::restart_run && i + 1 < argc) {
// Check if it has extension we can read
if (ends_with(argv[i + 1], ".h5")) {
// Check file type is a source file
file_id = file_open(argv[i + 1], 'r', true);
read_attribute(file_id, "filetype", filetype);
file_close(file_id);
if (filetype != "source") {
std::string msg {
"Second file after restart flag must be a source file"};
strcpy(openmc_err_msg, msg.c_str());
return OPENMC_E_INVALID_ARGUMENT;
}
// It is a source file
settings::path_sourcepoint = argv[i + 1];
i += 1;
} else {
// Source is in statepoint file
settings::path_sourcepoint = settings::path_statepoint;
}
} else {
// Source is assumed to be in statepoint file
settings::path_sourcepoint = settings::path_statepoint;
}
} else if (arg == "-g" || arg == "--geometry-debug") {
settings::check_overlaps = true;
} else if (arg == "-c" || arg == "--volume") {
settings::run_mode = RunMode::VOLUME;
} else if (arg == "-s" || arg == "--threads") {
// Read number of threads
if (i + 1 >= argc) {
std::string msg {"Number of threads not specified."};
strcpy(openmc_err_msg, msg.c_str());
return OPENMC_E_INVALID_ARGUMENT;
}
i += 1;
#ifdef _OPENMP
// Read and set number of OpenMP threads
int n_threads = std::stoi(argv[i]);
if (n_threads < 1) {
std::string msg {"Number of threads must be positive."};
strcpy(openmc_err_msg, msg.c_str());
return OPENMC_E_INVALID_ARGUMENT;
}
omp_set_num_threads(n_threads);
#else
if (mpi::master) {
warning("Ignoring number of threads specified on command line.");
}
#endif
} else if (arg == "-?" || arg == "-h" || arg == "--help") {
print_usage();
return OPENMC_E_UNASSIGNED;
} else if (arg == "-v" || arg == "--version") {
print_version();
print_build_info();
return OPENMC_E_UNASSIGNED;
} else if (arg == "-t" || arg == "--track") {
settings::write_all_tracks = true;
} else {
fmt::print(stderr, "Unknown option: {}\n", argv[i]);
print_usage();
return OPENMC_E_UNASSIGNED;
}
last_flag = i;
}
}
// Determine directory where XML input files are
if (argc > 1 && last_flag < argc - 1) {
settings::path_input = std::string(argv[last_flag + 1]);
// check that the path is either a valid directory or file
if (!dir_exists(settings::path_input) &&
!file_exists(settings::path_input)) {
fatal_error(fmt::format(
"The path specified to the OpenMC executable '{}' does not exist.",
settings::path_input));
}
// Add slash at end of directory if it isn't there
if (!ends_with(settings::path_input, "/") &&
dir_exists(settings::path_input)) {
settings::path_input += "/";
}
}
return 0;
}
// TODO: Pulse-height tallies require per-history scoring across the full
// particle tree (parent + all descendants). The shared secondary bank
// transports each secondary as an independent Particle, breaking this
// assumption. A proper fix would defer pulse-height scoring: save
// (root_source_id, cell, pht_storage) per particle, then aggregate by
// root_source_id after all secondary generations complete before scoring
// into the histogram. For now, disable shared secondary when pulse-height
// tallies are present.
static void check_pulse_height_compatibility()
{
if (settings::use_shared_secondary_bank) {
for (const auto& t : model::tallies) {
if (t->type_ == TallyType::PULSE_HEIGHT) {
settings::use_shared_secondary_bank = false;
warning("Pulse-height tallies are not yet compatible with the shared "
"secondary bank. Disabling shared secondary bank.");
break;
}
}
}
}
bool read_model_xml()
{
std::string model_filename = settings::path_input;
// if the current filename is a directory, append the default model filename
if (model_filename.empty() || dir_exists(model_filename))
model_filename += "model.xml";
// if this file doesn't exist, stop here
if (!file_exists(model_filename))
return false;
// try to process the path input as an XML file
pugi::xml_document doc;
if (!doc.load_file(model_filename.c_str())) {
fatal_error(fmt::format(
"Error reading from single XML input file '{}'", model_filename));
}
pugi::xml_node root = doc.document_element();
// Read settings
if (!check_for_node(root, "settings")) {
fatal_error("No <settings> node present in the model.xml file.");
}
auto settings_root = root.child("settings");
// Verbosity
if (check_for_node(settings_root, "verbosity") && settings::verbosity == -1) {
settings::verbosity = std::stoi(get_node_value(settings_root, "verbosity"));
} else if (settings::verbosity == -1) {
settings::verbosity = 7;
}
// To this point, we haven't displayed any output since we didn't know what
// the verbosity is. Now that we checked for it, show the title if necessary
if (mpi::master) {
if (settings::verbosity >= 2)
title();
}
write_message(
fmt::format("Reading model XML file '{}' ...", model_filename), 5);
// Read chain data before settings so DecaySpectrum source distributions can
// resolve nuclides while sources are constructed.
read_chain_file_xml();
read_settings_xml(settings_root);
// If other XML files are present, display warning
// that they will be ignored
auto other_inputs = {"materials.xml", "geometry.xml", "settings.xml",
"tallies.xml", "plots.xml"};
for (const auto& input : other_inputs) {
if (file_exists(settings::path_input + input)) {
warning((fmt::format("Other XML file input(s) are present. These files "
"may be ignored in favor of the {} file.",
model_filename)));
break;
}
}
// Read materials and cross sections
if (!check_for_node(root, "materials")) {
fatal_error(fmt::format(
"No <materials> node present in the {} file.", model_filename));
}
if (settings::run_mode != RunMode::PLOTTING) {
read_cross_sections_xml(root.child("materials"));
}
read_materials_xml(root.child("materials"));
// Read geometry
if (!check_for_node(root, "geometry")) {
fatal_error(fmt::format(
"No <geometry> node present in the {} file.", model_filename));
}
read_geometry_xml(root.child("geometry"));
// Final geometry setup and assign temperatures
finalize_geometry();
// Finalize cross sections having assigned temperatures
finalize_cross_sections();
// Compute cell density multipliers now that material densities
// have been finalized (from geometry_aux.h)
finalize_cell_densities();
if (check_for_node(root, "tallies"))
read_tallies_xml(root.child("tallies"));
check_pulse_height_compatibility();
// Initialize distribcell_filters
prepare_distribcell();
if (check_for_node(root, "plots")) {
read_plots_xml(root.child("plots"));
} else {
// When no <plots> element is present in the model.xml file, check for a
// regular plots.xml file
std::string filename = settings::path_input + "plots.xml";
if (file_exists(filename)) {
read_plots_xml();
}
}
finalize_variance_reduction();
return true;
}
void read_separate_xml_files()
{
// Read chain data before settings so DecaySpectrum source distributions can
// resolve nuclides while sources are constructed.
read_chain_file_xml();
read_settings_xml();
if (settings::run_mode != RunMode::PLOTTING) {
read_cross_sections_xml();
}
read_materials_xml();
read_geometry_xml();
// Final geometry setup and assign temperatures
finalize_geometry();
// Finalize cross sections having assigned temperatures
finalize_cross_sections();
// Compute cell density multipliers now that material densities
// have been finalized (from geometry_aux.h)
finalize_cell_densities();
read_tallies_xml();
check_pulse_height_compatibility();
// Initialize distribcell_filters
prepare_distribcell();
// Read the plots.xml regardless of plot mode in case plots are requested
// via the API
read_plots_xml();
finalize_variance_reduction();
}
void initial_output()
{
// write initial output
if (settings::run_mode == RunMode::PLOTTING) {
// Read plots.xml if it exists
if (mpi::master && settings::verbosity >= 5)
print_plot();
} else {
// Write summary information
if (mpi::master && settings::output_summary)
write_summary();
// Warn if overlap checking is on
if (mpi::master && settings::check_overlaps) {
warning("Cell overlap checking is ON.");
}
}
}
} // namespace openmc