forked from openmc-dev/openmc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeometry_aux.cpp
645 lines (554 loc) · 20 KB
/
geometry_aux.cpp
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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
#include "openmc/geometry_aux.h"
#include <algorithm> // for std::max
#include <sstream>
#include <unordered_set>
#include <fmt/core.h>
#include <pugixml.hpp>
#include "openmc/cell.h"
#include "openmc/constants.h"
#include "openmc/container_util.h"
#include "openmc/dagmc.h"
#include "openmc/error.h"
#include "openmc/file_utils.h"
#include "openmc/geometry.h"
#include "openmc/lattice.h"
#include "openmc/material.h"
#include "openmc/settings.h"
#include "openmc/surface.h"
#include "openmc/tallies/filter.h"
#include "openmc/tallies/filter_cell_instance.h"
#include "openmc/tallies/filter_distribcell.h"
namespace openmc {
namespace model {
std::unordered_map<int32_t, std::unordered_map<int32_t, int32_t>>
universe_cell_counts;
std::unordered_map<int32_t, int32_t> universe_level_counts;
} // namespace model
// adds the cell counts of universe b to universe a
void update_universe_cell_count(int32_t a, int32_t b)
{
auto& universe_a_counts = model::universe_cell_counts[a];
const auto& universe_b_counts = model::universe_cell_counts[b];
for (const auto& it : universe_b_counts) {
universe_a_counts[it.first] += it.second;
}
}
void read_geometry_xml()
{
// Display output message
write_message("Reading geometry XML file...", 5);
// Check if geometry.xml exists
std::string filename = settings::path_input + "geometry.xml";
if (!file_exists(filename)) {
fatal_error("Geometry XML file '" + filename + "' does not exist!");
}
// Parse settings.xml file
pugi::xml_document doc;
auto result = doc.load_file(filename.c_str());
if (!result) {
fatal_error("Error processing geometry.xml file.");
}
// Get root element
pugi::xml_node root = doc.document_element();
read_geometry_xml(root);
}
void read_geometry_xml(pugi::xml_node root)
{
// Read surfaces, cells, lattice
read_surfaces(root);
read_cells(root);
read_lattices(root);
// Check to make sure a boundary condition was applied to at least one
// surface
bool boundary_exists = false;
for (const auto& surf : model::surfaces) {
if (surf->bc_) {
boundary_exists = true;
break;
}
}
if (settings::run_mode != RunMode::PLOTTING &&
settings::run_mode != RunMode::VOLUME && !boundary_exists) {
fatal_error("No boundary conditions were applied to any surfaces!");
}
// Allocate universes, universe cell arrays, and assign base universe
model::root_universe = find_root_universe();
// if the root universe is DAGMC geometry, make sure the model is well-formed
check_dagmc_root_univ();
}
//==============================================================================
void adjust_indices()
{
// Adjust material/fill idices.
for (auto& c : model::cells) {
if (c->fill_ != C_NONE) {
int32_t id = c->fill_;
auto search_univ = model::universe_map.find(id);
auto search_lat = model::lattice_map.find(id);
if (search_univ != model::universe_map.end()) {
c->type_ = Fill::UNIVERSE;
c->fill_ = search_univ->second;
} else if (search_lat != model::lattice_map.end()) {
c->type_ = Fill::LATTICE;
c->fill_ = search_lat->second;
} else {
fatal_error(fmt::format("Specified fill {} on cell {} is neither a "
"universe nor a lattice.",
id, c->id_));
}
} else {
c->type_ = Fill::MATERIAL;
for (auto& mat_id : c->material_) {
if (mat_id != MATERIAL_VOID) {
auto search = model::material_map.find(mat_id);
if (search == model::material_map.end()) {
fatal_error(
fmt::format("Could not find material {} specified on cell {}",
mat_id, c->id_));
}
// Change from ID to index
mat_id = search->second;
}
}
}
}
// Change cell.universe values from IDs to indices.
for (auto& c : model::cells) {
auto search = model::universe_map.find(c->universe_);
if (search != model::universe_map.end()) {
c->universe_ = search->second;
} else {
fatal_error(fmt::format("Could not find universe {} specified on cell {}",
c->universe_, c->id_));
}
}
// Change all lattice universe values from IDs to indices.
for (auto& l : model::lattices) {
l->adjust_indices();
}
}
//==============================================================================
//! Partition some universes with many z-planes for faster find_cell searches.
void partition_universes()
{
// Iterate over universes with more than 10 cells. (Fewer than 10 is likely
// not worth partitioning.)
for (const auto& univ : model::universes) {
if (univ->cells_.size() > 10) {
// Collect the set of surfaces in this universe.
std::unordered_set<int32_t> surf_inds;
for (auto i_cell : univ->cells_) {
for (auto token : model::cells[i_cell]->surfaces()) {
surf_inds.insert(std::abs(token) - 1);
}
}
// Partition the universe if there are more than 5 z-planes. (Fewer than
// 5 is likely not worth it.)
int n_zplanes = 0;
for (auto i_surf : surf_inds) {
if (dynamic_cast<const SurfaceZPlane*>(model::surfaces[i_surf].get())) {
++n_zplanes;
if (n_zplanes > 5) {
univ->partitioner_ = make_unique<UniversePartitioner>(*univ);
break;
}
}
}
}
}
}
//==============================================================================
void assign_temperatures()
{
for (auto& c : model::cells) {
// Ignore non-material cells and cells with defined temperature.
if (c->material_.size() == 0)
continue;
if (c->sqrtkT_.size() > 0)
continue;
c->sqrtkT_.reserve(c->material_.size());
for (auto i_mat : c->material_) {
if (i_mat == MATERIAL_VOID) {
// Set void region to 0K.
c->sqrtkT_.push_back(0);
} else {
const auto& mat {model::materials[i_mat]};
c->sqrtkT_.push_back(std::sqrt(K_BOLTZMANN * mat->temperature()));
}
}
}
}
//==============================================================================
void get_temperatures(
vector<vector<double>>& nuc_temps, vector<vector<double>>& thermal_temps)
{
for (const auto& cell : model::cells) {
// Skip non-material cells.
if (cell->fill_ != C_NONE)
continue;
for (int j = 0; j < cell->material_.size(); ++j) {
// Skip void materials
int i_material = cell->material_[j];
if (i_material == MATERIAL_VOID)
continue;
// Get temperature(s) of cell (rounding to nearest integer)
vector<double> cell_temps;
if (cell->sqrtkT_.size() == 1) {
double sqrtkT = cell->sqrtkT_[0];
cell_temps.push_back(sqrtkT * sqrtkT / K_BOLTZMANN);
} else if (cell->sqrtkT_.size() == cell->material_.size()) {
double sqrtkT = cell->sqrtkT_[j];
cell_temps.push_back(sqrtkT * sqrtkT / K_BOLTZMANN);
} else {
for (double sqrtkT : cell->sqrtkT_)
cell_temps.push_back(sqrtkT * sqrtkT / K_BOLTZMANN);
}
const auto& mat {model::materials[i_material]};
for (const auto& i_nuc : mat->nuclide_) {
for (double temperature : cell_temps) {
// Add temperature if it hasn't already been added
if (!contains(nuc_temps[i_nuc], temperature))
nuc_temps[i_nuc].push_back(temperature);
}
}
for (const auto& table : mat->thermal_tables_) {
// Get index in data::thermal_scatt array
int i_sab = table.index_table;
for (double temperature : cell_temps) {
// Add temperature if it hasn't already been added
if (!contains(thermal_temps[i_sab], temperature))
thermal_temps[i_sab].push_back(temperature);
}
}
}
}
}
//==============================================================================
void finalize_geometry()
{
// Perform some final operations to set up the geometry
adjust_indices();
count_cell_instances(model::root_universe);
partition_universes();
// Assign temperatures to cells that don't have temperatures already assigned
assign_temperatures();
// Determine number of nested coordinate levels in the geometry
model::n_coord_levels = maximum_levels(model::root_universe);
}
//==============================================================================
int32_t find_root_universe()
{
// Find all the universes listed as a cell fill.
std::unordered_set<int32_t> fill_univ_ids;
for (const auto& c : model::cells) {
fill_univ_ids.insert(c->fill_);
}
// Find all the universes contained in a lattice.
for (const auto& lat : model::lattices) {
for (auto it = lat->begin(); it != lat->end(); ++it) {
fill_univ_ids.insert(*it);
}
if (lat->outer_ != NO_OUTER_UNIVERSE) {
fill_univ_ids.insert(lat->outer_);
}
}
// Figure out which universe is not in the set. This is the root universe.
bool root_found {false};
int32_t root_univ;
for (int32_t i = 0; i < model::universes.size(); i++) {
auto search = fill_univ_ids.find(model::universes[i]->id_);
if (search == fill_univ_ids.end()) {
if (root_found) {
fatal_error("Two or more universes are not used as fill universes, so "
"it is not possible to distinguish which one is the root "
"universe.");
} else {
root_found = true;
root_univ = i;
}
}
}
if (!root_found)
fatal_error("Could not find a root universe. Make sure "
"there are no circular dependencies in the geometry.");
return root_univ;
}
//==============================================================================
void prepare_distribcell(const std::vector<int32_t>* user_distribcells)
{
write_message("Preparing distributed cell instances...", 5);
std::unordered_set<int32_t> distribcells;
// start with any cells manually specified via the C++ API
if (user_distribcells) {
distribcells.insert(user_distribcells->begin(), user_distribcells->end());
}
// Find all cells listed in a DistribcellFilter or CellInstanceFilter
for (auto& filt : model::tally_filters) {
auto* distrib_filt = dynamic_cast<DistribcellFilter*>(filt.get());
auto* cell_inst_filt = dynamic_cast<CellInstanceFilter*>(filt.get());
if (distrib_filt) {
distribcells.insert(distrib_filt->cell());
}
if (cell_inst_filt) {
const auto& filter_cells = cell_inst_filt->cells();
distribcells.insert(filter_cells.begin(), filter_cells.end());
}
}
// By default, add material cells to the list of distributed cells
if (settings::material_cell_offsets) {
for (gsl::index i = 0; i < model::cells.size(); ++i) {
if (model::cells[i]->type_ == Fill::MATERIAL)
distribcells.insert(i);
}
}
// Make sure that the number of materials/temperatures matches the number of
// cell instances.
for (int i = 0; i < model::cells.size(); i++) {
Cell& c {*model::cells[i]};
if (c.material_.size() > 1) {
if (c.material_.size() != c.n_instances_) {
fatal_error(fmt::format(
"Cell {} was specified with {} materials but has {} distributed "
"instances. The number of materials must equal one or the number "
"of instances.",
c.id_, c.material_.size(), c.n_instances_));
}
}
if (c.sqrtkT_.size() > 1) {
if (c.sqrtkT_.size() != c.n_instances_) {
fatal_error(fmt::format(
"Cell {} was specified with {} temperatures but has {} distributed "
"instances. The number of temperatures must equal one or the number "
"of instances.",
c.id_, c.sqrtkT_.size(), c.n_instances_));
}
}
}
// Search through universes for material cells and assign each one a
// unique distribcell array index.
int distribcell_index = 0;
vector<int32_t> target_univ_ids;
for (const auto& u : model::universes) {
for (auto idx : u->cells_) {
if (distribcells.find(idx) != distribcells.end()) {
model::cells[idx]->distribcell_index_ = distribcell_index++;
target_univ_ids.push_back(u->id_);
}
}
}
// Allocate the cell and lattice offset tables.
int n_maps = target_univ_ids.size();
for (auto& c : model::cells) {
if (c->type_ != Fill::MATERIAL) {
c->offset_.resize(n_maps, C_NONE);
}
}
for (auto& lat : model::lattices) {
lat->allocate_offset_table(n_maps);
}
// Fill the cell and lattice offset tables.
#pragma omp parallel for
for (int map = 0; map < target_univ_ids.size(); map++) {
auto target_univ_id = target_univ_ids[map];
std::unordered_map<int32_t, int32_t> univ_count_memo;
for (const auto& univ : model::universes) {
int32_t offset = 0;
for (int32_t cell_indx : univ->cells_) {
Cell& c = *model::cells[cell_indx];
if (c.type_ == Fill::UNIVERSE) {
c.offset_[map] = offset;
int32_t search_univ = c.fill_;
offset += count_universe_instances(
search_univ, target_univ_id, univ_count_memo);
} else if (c.type_ == Fill::LATTICE) {
c.offset_[map] = offset;
Lattice& lat = *model::lattices[c.fill_];
offset +=
lat.fill_offset_table(offset, target_univ_id, map, univ_count_memo);
}
}
}
}
}
//==============================================================================
void count_cell_instances(int32_t univ_indx)
{
const auto univ_counts = model::universe_cell_counts.find(univ_indx);
if (univ_counts != model::universe_cell_counts.end()) {
for (const auto& it : univ_counts->second) {
model::cells[it.first]->n_instances_ += it.second;
}
} else {
for (int32_t cell_indx : model::universes[univ_indx]->cells_) {
Cell& c = *model::cells[cell_indx];
++c.n_instances_;
model::universe_cell_counts[univ_indx][cell_indx] += 1;
if (c.type_ == Fill::UNIVERSE) {
// This cell contains another universe. Recurse into that universe.
count_cell_instances(c.fill_);
update_universe_cell_count(univ_indx, c.fill_);
} else if (c.type_ == Fill::LATTICE) {
// This cell contains a lattice. Recurse into the lattice universes.
Lattice& lat = *model::lattices[c.fill_];
for (auto it = lat.begin(); it != lat.end(); ++it) {
count_cell_instances(*it);
update_universe_cell_count(univ_indx, *it);
}
}
}
}
}
//==============================================================================
int count_universe_instances(int32_t search_univ, int32_t target_univ_id,
std::unordered_map<int32_t, int32_t>& univ_count_memo)
{
// If this is the target, it can't contain itself.
if (model::universes[search_univ]->id_ == target_univ_id) {
return 1;
}
// If we have already counted the number of instances, reuse that value.
auto search = univ_count_memo.find(search_univ);
if (search != univ_count_memo.end()) {
return search->second;
}
int count {0};
for (int32_t cell_indx : model::universes[search_univ]->cells_) {
Cell& c = *model::cells[cell_indx];
if (c.type_ == Fill::UNIVERSE) {
int32_t next_univ = c.fill_;
count +=
count_universe_instances(next_univ, target_univ_id, univ_count_memo);
} else if (c.type_ == Fill::LATTICE) {
Lattice& lat = *model::lattices[c.fill_];
for (auto it = lat.begin(); it != lat.end(); ++it) {
int32_t next_univ = *it;
count +=
count_universe_instances(next_univ, target_univ_id, univ_count_memo);
}
}
}
// Remember the number of instances in this universe.
univ_count_memo[search_univ] = count;
return count;
}
//==============================================================================
std::string distribcell_path_inner(int32_t target_cell, int32_t map,
int32_t target_offset, const Universe& search_univ, int32_t offset)
{
std::stringstream path;
path << "u" << search_univ.id_ << "->";
// Check to see if this universe directly contains the target cell. If so,
// write to the path and return.
for (int32_t cell_indx : search_univ.cells_) {
if ((cell_indx == target_cell) && (offset == target_offset)) {
Cell& c = *model::cells[cell_indx];
path << "c" << c.id_;
return path.str();
}
}
// The target must be further down the geometry tree and contained in a fill
// cell or lattice cell in this universe. Find which cell contains the
// target.
vector<std::int32_t>::const_reverse_iterator cell_it {
search_univ.cells_.crbegin()};
for (; cell_it != search_univ.cells_.crend(); ++cell_it) {
Cell& c = *model::cells[*cell_it];
// Material cells don't contain other cells so ignore them.
if (c.type_ != Fill::MATERIAL) {
int32_t temp_offset;
if (c.type_ == Fill::UNIVERSE) {
temp_offset =
offset + c.offset_[map]; // TODO: should also apply to lattice fills?
} else {
Lattice& lat = *model::lattices[c.fill_];
int32_t indx = lat.universes_.size() * map + lat.begin().indx_;
temp_offset = offset + lat.offsets_[indx];
}
// The desired cell is the first cell that gives an offset smaller or
// equal to the target offset.
if (temp_offset <= target_offset - c.offset_[map])
break;
}
}
// if we get through the loop without finding an appropriate entry, throw
// an error
if (cell_it == search_univ.cells_.crend()) {
fatal_error(
fmt::format("Failed to generate a text label for distribcell with ID {}."
"The current label is: '{}'",
model::cells[target_cell]->id_, path.str()));
}
// Add the cell to the path string.
Cell& c = *model::cells[*cell_it];
path << "c" << c.id_ << "->";
if (c.type_ == Fill::UNIVERSE) {
// Recurse into the fill cell.
offset += c.offset_[map];
path << distribcell_path_inner(
target_cell, map, target_offset, *model::universes[c.fill_], offset);
return path.str();
} else {
// Recurse into the lattice cell.
Lattice& lat = *model::lattices[c.fill_];
path << "l" << lat.id_;
for (ReverseLatticeIter it = lat.rbegin(); it != lat.rend(); ++it) {
int32_t indx = lat.universes_.size() * map + it.indx_;
int32_t temp_offset = offset + lat.offsets_[indx];
if (temp_offset <= target_offset - c.offset_[map]) {
offset = temp_offset;
path << "(" << lat.index_to_string(it.indx_) << ")->";
path << distribcell_path_inner(target_cell, map, target_offset,
*model::universes[*it], offset + c.offset_[map]);
return path.str();
}
}
throw std::runtime_error {"Error determining distribcell path."};
}
}
std::string distribcell_path(
int32_t target_cell, int32_t map, int32_t target_offset)
{
auto& root_univ = *model::universes[model::root_universe];
return distribcell_path_inner(target_cell, map, target_offset, root_univ, 0);
}
//==============================================================================
int maximum_levels(int32_t univ)
{
const auto level_count = model::universe_level_counts.find(univ);
if (level_count != model::universe_level_counts.end()) {
return level_count->second;
}
int levels_below {0};
for (int32_t cell_indx : model::universes[univ]->cells_) {
Cell& c = *model::cells[cell_indx];
if (c.type_ == Fill::UNIVERSE) {
int32_t next_univ = c.fill_;
levels_below = std::max(levels_below, maximum_levels(next_univ));
} else if (c.type_ == Fill::LATTICE) {
Lattice& lat = *model::lattices[c.fill_];
for (auto it = lat.begin(); it != lat.end(); ++it) {
int32_t next_univ = *it;
levels_below = std::max(levels_below, maximum_levels(next_univ));
}
}
}
++levels_below;
model::universe_level_counts[univ] = levels_below;
return levels_below;
}
bool is_root_universe(int32_t univ_id)
{
return model::universe_map[univ_id] == model::root_universe;
}
//==============================================================================
void free_memory_geometry()
{
model::cells.clear();
model::cell_map.clear();
model::universes.clear();
model::universe_map.clear();
model::lattices.clear();
model::lattice_map.clear();
model::overlap_check_count.clear();
}
} // namespace openmc