Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
567 lines (463 loc) · 15.8 KB

File metadata and controls

567 lines (463 loc) · 15.8 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#include "openmc/simulation.h"
#include "openmc/capi.h"
#include "openmc/container_util.h"
#include "openmc/eigenvalue.h"
#include "openmc/error.h"
#include "openmc/message_passing.h"
#include "openmc/output.h"
#include "openmc/particle.h"
#include "openmc/random_lcg.h"
#include "openmc/settings.h"
#include "openmc/source.h"
#include "openmc/state_point.h"
#include "openmc/timer.h"
#include "openmc/tallies/filter.h"
#include "openmc/tallies/tally.h"
#include <algorithm>
#include <string>
namespace openmc {
// data/functions from Fortran side
extern "C" bool cmfd_on;
extern "C" void accumulate_tallies();
extern "C" void allocate_banks();
extern "C" void check_triggers();
extern "C" void cmfd_init_batch();
extern "C" void cmfd_tally_init();
extern "C" void configure_tallies();
extern "C" void execute_cmfd();
extern "C" void init_tally_routines();
extern "C" void join_bank_from_threads();
extern "C" void load_state_point();
extern "C" void print_batch_keff();
extern "C" void print_columns();
extern "C" void print_generation();
extern "C" void print_results();
extern "C" void print_runtime();
extern "C" void setup_active_tallies();
extern "C" void simulation_init_f();
extern "C" void simulation_finalize_f();
extern "C" void transport(Particle* p);
extern "C" void write_tallies();
} // namespace openmc
//==============================================================================
// C API functions
//==============================================================================
// OPENMC_RUN encompasses all the main logic where iterations are performed
// over the batches, generations, and histories in a fixed source or k-eigenvalue
// calculation.
int openmc_run()
{
openmc_simulation_init();
int err = 0;
int status = 0;
while (status == 0 && err == 0) {
err = openmc_next_batch(&status);
}
openmc_simulation_finalize();
return err;
}
int openmc_simulation_init()
{
using namespace openmc;
// Skip if simulation has already been initialized
if (simulation::initialized) return 0;
// Determine how much work each process should do
calculate_work();
// Allocate array for matching filter bins
#pragma omp parallel
{
filter_matches.resize(n_filters);
}
// Set up tally procedure pointers
init_tally_routines();
// Allocate source bank, and for eigenvalue simulations also allocate the
// fission bank
allocate_banks();
// Allocate tally results arrays if they're not allocated yet
configure_tallies();
// Activate the CMFD tallies
cmfd_tally_init();
// Call Fortran initialization
simulation_init_f();
// Reset global variables -- this is done before loading state point (as that
// will potentially populate k_generation and entropy)
simulation::current_batch = 0;
simulation::k_generation.clear();
entropy.clear();
simulation::need_depletion_rx = false;
// If this is a restart run, load the state point data and binary source
// file
if (settings::restart_run) {
load_state_point();
write_message("Resuming simulation...", 6);
} else {
initialize_source();
}
// Display header
if (mpi::master) {
if (settings::run_mode == RUN_MODE_FIXEDSOURCE) {
header("FIXED SOURCE TRANSPORT SIMULATION", 3);
} else if (settings::run_mode == RUN_MODE_EIGENVALUE) {
header("K EIGENVALUE SIMULATION", 3);
if (settings::verbosity >= 7) print_columns();
}
}
// Set flag indicating initialization is done
simulation::initialized = true;
return 0;
}
int openmc_simulation_finalize()
{
using namespace openmc;
// Skip if simulation was never run
if (!simulation::initialized) return 0;
// Stop active batch timer and start finalization timer
time_active.stop();
time_finalize.start();
#pragma omp parallel
{
filter_matches.clear();
}
// Deallocate Fortran variables, set tallies to inactive
simulation_finalize_f();
// Increment total number of generations
simulation::total_gen += simulation::current_batch*settings::gen_per_batch;
#ifdef OPENMC_MPI
broadcast_results();
#endif
// Write tally results to tallies.out
if (settings::output_tallies && mpi::master) write_tallies();
// Deactivate all tallies
for (int i = 1; i <= n_tallies; ++i) {
openmc_tally_set_active(i, false);
}
// Stop timers and show timing statistics
time_finalize.stop();
time_total.stop();
if (mpi::master) {
if (settings::verbosity >= 6) print_runtime();
if (settings::verbosity >= 4) print_results();
}
if (settings::check_overlaps) print_overlap_check();
// Reset flags
simulation::need_depletion_rx = false;
simulation::initialized = false;
return 0;
}
int openmc_next_batch(int* status)
{
using namespace openmc;
using openmc::simulation::current_gen;
// Make sure simulation has been initialized
if (!simulation::initialized) {
set_errmsg("Simulation has not been initialized yet.");
return OPENMC_E_ALLOCATE;
}
initialize_batch();
// =======================================================================
// LOOP OVER GENERATIONS
for (current_gen = 1; current_gen <= settings::gen_per_batch; ++current_gen) {
initialize_generation();
// Start timer for transport
time_transport.start();
// ====================================================================
// LOOP OVER PARTICLES
#pragma omp parallel for schedule(runtime)
for (int64_t i_work = 1; i_work <= simulation::work; ++i_work) {
simulation::current_work = i_work;
// grab source particle from bank
Particle p;
initialize_history(&p, simulation::current_work);
// transport particle
transport(&p);
}
// Accumulate time for transport
time_transport.stop();
finalize_generation();
}
finalize_batch();
// Check simulation ending criteria
if (status) {
if (simulation::current_batch == settings::n_max_batches) {
*status = STATUS_EXIT_MAX_BATCH;
} else if (simulation::satisfy_triggers) {
*status = STATUS_EXIT_ON_TRIGGER;
} else {
*status = STATUS_EXIT_NORMAL;
}
}
return 0;
}
namespace openmc {
//==============================================================================
// Global variables
//==============================================================================
namespace simulation {
int current_batch;
int current_gen;
int64_t current_work;
bool initialized {false};
double keff {1.0};
double keff_std;
double k_col_abs {0.0};
double k_col_tra {0.0};
double k_abs_tra {0.0};
double log_spacing;
int n_lost_particles {0};
bool need_depletion_rx {false};
int restart_batch;
bool satisfy_triggers {false};
int total_gen {0};
int64_t work;
std::vector<double> k_generation;
std::vector<int64_t> work_index;
// Threadprivate variables
bool trace; //!< flag to show debug information
#ifdef _OPENMP
int n_threads {-1}; //!< number of OpenMP threads
int thread_id; //!< ID of a given thread
#endif
} // namespace simulation
//==============================================================================
// Non-member functions
//==============================================================================
void initialize_batch()
{
// Increment current batch
++simulation::current_batch;
if (settings::run_mode == RUN_MODE_FIXEDSOURCE) {
int b = simulation::current_batch;
write_message("Simulating batch " + std::to_string(b), 6);
}
// Reset total starting particle weight used for normalizing tallies
total_weight = 0.0;
// Determine if this batch is the first inactive or active batch.
bool first_inactive = false;
bool first_active = false;
if (!settings::restart_run) {
first_inactive = settings::n_inactive > 0 && simulation::current_batch == 1;
first_active = simulation::current_batch == settings::n_inactive + 1;
} else if (simulation::current_batch == simulation::restart_batch + 1){
first_inactive = simulation::restart_batch < settings::n_inactive;
first_active = !first_inactive;
}
// Manage active/inactive timers and activate tallies if necessary.
if (first_inactive) {
time_inactive.start();
} else if (first_active) {
time_inactive.stop();
time_active.start();
for (int i = 1; i <= n_tallies; ++i) {
// TODO: change one-based index
openmc_tally_set_active(i, true);
}
}
// check CMFD initialize batch
if (settings::run_mode == RUN_MODE_EIGENVALUE) {
if (settings::cmfd_run) cmfd_init_batch();
}
// Add user tallies to active tallies list
setup_active_tallies();
}
void finalize_batch()
{
// Reduce tallies onto master process and accumulate
time_tallies.start();
accumulate_tallies();
time_tallies.stop();
// Reset global tally results
if (simulation::current_batch <= settings::n_inactive) {
auto gt = global_tallies();
std::fill(gt.begin(), gt.end(), 0.0);
n_realizations = 0;
}
if (settings::run_mode == RUN_MODE_EIGENVALUE) {
// Perform CMFD calculation if on
if (cmfd_on) execute_cmfd();
// Write batch output
if (mpi::master && settings::verbosity >= 7) print_batch_keff();
}
// Check_triggers
if (mpi::master) check_triggers();
#ifdef OPENMC_MPI
MPI_Bcast(&simulation::satisfy_triggers, 1, MPI_C_BOOL, 0, mpi::intracomm);
#endif
if (simulation::satisfy_triggers || (settings::trigger_on &&
simulation::current_batch == settings::n_max_batches)) {
settings::statepoint_batch.insert(simulation::current_batch);
}
// Write out state point if it's been specified for this batch
if (contains(settings::statepoint_batch, simulation::current_batch)) {
if (contains(settings::sourcepoint_batch, simulation::current_batch)
&& settings::source_write && !settings::source_separate) {
bool b = true;
openmc_statepoint_write(nullptr, &b);
} else {
bool b = false;
openmc_statepoint_write(nullptr, &b);
}
}
// Write out a separate source point if it's been specified for this batch
if (contains(settings::sourcepoint_batch, simulation::current_batch)
&& settings::source_write && settings::source_separate) {
write_source_point(nullptr);
}
// Write a continously-overwritten source point if requested.
if (settings::source_latest) {
auto filename = settings::path_output + "source.h5";
write_source_point(filename.c_str());
}
}
void initialize_generation()
{
if (settings::run_mode == RUN_MODE_EIGENVALUE) {
// Reset number of fission bank sites
n_bank = 0;
// Count source sites if using uniform fission source weighting
if (settings::ufs_on) ufs_count_sites();
// Store current value of tracklength k
keff_generation = global_tallies()(K_TRACKLENGTH, RESULT_VALUE);
}
}
void finalize_generation()
{
auto gt = global_tallies();
// Update global tallies with the omp private accumulation variables
#pragma omp parallel
{
#pragma omp critical(increment_global_tallies)
{
if (settings::run_mode == RUN_MODE_EIGENVALUE) {
gt(K_COLLISION, RESULT_VALUE) += global_tally_collision;
gt(K_ABSORPTION, RESULT_VALUE) += global_tally_absorption;
gt(K_TRACKLENGTH, RESULT_VALUE) += global_tally_tracklength;
}
gt(LEAKAGE, RESULT_VALUE) += global_tally_leakage;
}
// reset threadprivate tallies
if (settings::run_mode == RUN_MODE_EIGENVALUE) {
global_tally_collision = 0.0;
global_tally_absorption = 0.0;
global_tally_tracklength = 0.0;
}
global_tally_leakage = 0.0;
}
if (settings::run_mode == RUN_MODE_EIGENVALUE) {
#ifdef _OPENMP
// Join the fission bank from each thread into one global fission bank
join_bank_from_threads();
#endif
// Distribute fission bank across processors evenly
synchronize_bank();
// Calculate shannon entropy
if (settings::entropy_on) shannon_entropy();
// Collect results and statistics
calculate_generation_keff();
calculate_average_keff();
// Write generation output
if (mpi::master && settings::verbosity >= 7) {
if (simulation::current_gen != settings::gen_per_batch) {
print_generation();
}
}
} else if (settings::run_mode == RUN_MODE_FIXEDSOURCE) {
// For fixed-source mode, we need to sample the external source
fill_source_bank_fixedsource();
}
}
void initialize_history(Particle* p, int64_t index_source)
{
// Get pointer to source bank
Bank* source_bank;
int64_t n;
openmc_source_bank(&source_bank, &n);
// set defaults
p->from_source(&source_bank[index_source - 1]);
// set identifier for particle
p->id = simulation::work_index[mpi::rank] + index_source;
// set random number seed
int64_t particle_seed = (simulation::total_gen + overall_generation() - 1)
* settings::n_particles + p->id;
set_particle_seed(particle_seed);
// set particle trace
simulation::trace = false;
if (simulation::current_batch == settings::trace_batch &&
simulation::current_gen == settings::trace_gen &&
p->id == settings::trace_particle) simulation::trace = true;
// Set particle track.
p->write_track = false;
if (settings::write_all_tracks) {
p->write_track = true;
} else if (settings::track_identifiers.size() > 0) {
for (const auto& t : settings::track_identifiers) {
if (simulation::current_batch == t[0] &&
simulation::current_gen == t[1] &&
p->id == t[2]) {
p->write_track = true;
break;
}
}
}
}
int overall_generation()
{
using namespace simulation;
return settings::gen_per_batch*(current_batch - 1) + current_gen;
}
void calculate_work()
{
// Determine minimum amount of particles to simulate on each processor
int64_t min_work = settings::n_particles / mpi::n_procs;
// Determine number of processors that have one extra particle
int64_t remainder = settings::n_particles % mpi::n_procs;
int64_t i_bank = 0;
simulation::work_index.resize(mpi::n_procs + 1);
simulation::work_index[0] = 0;
for (int i = 0; i < mpi::n_procs; ++i) {
// Number of particles for rank i
int64_t work_i = i < remainder ? min_work + 1 : min_work;
// Set number of particles
if (mpi::rank == i) simulation::work = work_i;
// Set index into source bank for rank i
i_bank += work_i;
simulation::work_index[i + 1] = i_bank;
}
}
#ifdef OPENMC_MPI
void broadcast_results() {
// Broadcast tally results so that each process has access to results
for (int i = 1; i <= n_tallies; ++i) {
// Create a new datatype that consists of all values for a given filter
// bin and then use that to broadcast. This is done to minimize the
// chance of the 'count' argument of MPI_BCAST exceeding 2**31
auto results = tally_results(i);
auto shape = results.shape();
int count_per_filter = shape[1] * shape[2];
MPI_Datatype result_block;
MPI_Type_contiguous(count_per_filter, MPI_DOUBLE, &result_block);
MPI_Type_commit(&result_block);
MPI_Bcast(results.data(), shape[0], result_block, 0, mpi::intracomm);
MPI_Type_free(&result_block);
}
// Also broadcast global tally results
auto gt = global_tallies();
MPI_Bcast(gt.data(), gt.size(), MPI_DOUBLE, 0, mpi::intracomm);
// These guys are needed so that non-master processes can calculate the
// combined estimate of k-effective
double temp[] {simulation::k_col_abs, simulation::k_col_tra,
simulation::k_abs_tra};
MPI_Bcast(temp, 3, MPI_DOUBLE, 0, mpi::intracomm);
simulation::k_col_abs = temp[0];
simulation::k_col_tra = temp[1];
simulation::k_abs_tra = temp[2];
}
#endif
//==============================================================================
// Fortran compatibility
//==============================================================================
extern "C" double k_generation(int i) { return simulation::k_generation.at(i - 1); }
extern "C" int k_generation_size() { return simulation::k_generation.size(); }
extern "C" void k_generation_clear() { simulation::k_generation.clear(); }
extern "C" void k_generation_reserve(int i) { simulation::k_generation.reserve(i); }
extern "C" int64_t work_index(int rank) { return simulation::work_index[rank]; }
} // namespace openmc
Morty Proxy This is a proxified and sanitized view of the page, visit original site.