2525namespace openmc {
2626
2727namespace model {
28- std::unordered_map<int32_t , std::unordered_map<int32_t , int32_t >>
29- universe_cell_counts;
3028std::unordered_map<int32_t , int32_t > universe_level_counts;
3129} // namespace model
3230
33- // adds the cell counts of universe b to universe a
34- void update_universe_cell_count (int32_t a, int32_t b)
35- {
36- auto & universe_a_counts = model::universe_cell_counts[a];
37- const auto & universe_b_counts = model::universe_cell_counts[b];
38- for (const auto & it : universe_b_counts) {
39- universe_a_counts[it.first ] += it.second ;
40- }
41- }
42-
4331void read_geometry_xml ()
4432{
4533 // Display output message
@@ -263,7 +251,7 @@ void finalize_geometry()
263251{
264252 // Perform some final operations to set up the geometry
265253 adjust_indices ();
266- count_cell_instances (model::root_universe );
254+ count_universe_instances ( );
267255 partition_universes ();
268256
269257 // Assign temperatures to cells that don't have temperatures already assigned
@@ -356,35 +344,38 @@ void prepare_distribcell(const std::vector<int32_t>* user_distribcells)
356344 Cell& c {*model::cells[i]};
357345
358346 if (c.material_ .size () > 1 ) {
359- if (c.material_ .size () != c.n_instances_ ) {
347+ if (c.material_ .size () != c.n_instances () ) {
360348 fatal_error (fmt::format (
361349 " Cell {} was specified with {} materials but has {} distributed "
362350 " instances. The number of materials must equal one or the number "
363351 " of instances." ,
364- c.id_ , c.material_ .size (), c.n_instances_ ));
352+ c.id_ , c.material_ .size (), c.n_instances () ));
365353 }
366354 }
367355
368356 if (c.sqrtkT_ .size () > 1 ) {
369- if (c.sqrtkT_ .size () != c.n_instances_ ) {
357+ if (c.sqrtkT_ .size () != c.n_instances () ) {
370358 fatal_error (fmt::format (
371359 " Cell {} was specified with {} temperatures but has {} distributed "
372360 " instances. The number of temperatures must equal one or the number "
373361 " of instances." ,
374- c.id_ , c.sqrtkT_ .size (), c.n_instances_ ));
362+ c.id_ , c.sqrtkT_ .size (), c.n_instances () ));
375363 }
376364 }
377365 }
378366
379367 // Search through universes for material cells and assign each one a
380- // unique distribcell array index.
381- int distribcell_index = 0 ;
368+ // distribcell array index according to the containing universe.
382369 vector<int32_t > target_univ_ids;
383370 for (const auto & u : model::universes) {
384371 for (auto idx : u->cells_ ) {
385372 if (distribcells.find (idx) != distribcells.end ()) {
386- model::cells[idx]->distribcell_index_ = distribcell_index++;
387- target_univ_ids.push_back (u->id_ );
373+ if (!contains (target_univ_ids, u->id_ )) {
374+ target_univ_ids.push_back (u->id_ );
375+ }
376+ model::cells[idx]->distribcell_index_ =
377+ std::find (target_univ_ids.begin (), target_univ_ids.end (), u->id_ ) -
378+ target_univ_ids.begin ();
388379 }
389380 }
390381 }
@@ -419,8 +410,7 @@ void prepare_distribcell(const std::vector<int32_t>* user_distribcells)
419410 } else if (c.type_ == Fill::LATTICE) {
420411 c.offset_ [map] = offset;
421412 Lattice& lat = *model::lattices[c.fill_ ];
422- offset +=
423- lat.fill_offset_table (offset, target_univ_id, map, univ_count_memo);
413+ offset += lat.fill_offset_table (target_univ_id, map, univ_count_memo);
424414 }
425415 }
426416 }
@@ -429,32 +419,12 @@ void prepare_distribcell(const std::vector<int32_t>* user_distribcells)
429419
430420// ==============================================================================
431421
432- void count_cell_instances ( int32_t univ_indx )
422+ void count_universe_instances ( )
433423{
434- const auto univ_counts = model::universe_cell_counts.find (univ_indx);
435- if (univ_counts != model::universe_cell_counts.end ()) {
436- for (const auto & it : univ_counts->second ) {
437- model::cells[it.first ]->n_instances_ += it.second ;
438- }
439- } else {
440- for (int32_t cell_indx : model::universes[univ_indx]->cells_ ) {
441- Cell& c = *model::cells[cell_indx];
442- ++c.n_instances_ ;
443- model::universe_cell_counts[univ_indx][cell_indx] += 1 ;
444-
445- if (c.type_ == Fill::UNIVERSE) {
446- // This cell contains another universe. Recurse into that universe.
447- count_cell_instances (c.fill_ );
448- update_universe_cell_count (univ_indx, c.fill_ );
449- } else if (c.type_ == Fill::LATTICE) {
450- // This cell contains a lattice. Recurse into the lattice universes.
451- Lattice& lat = *model::lattices[c.fill_ ];
452- for (auto it = lat.begin (); it != lat.end (); ++it) {
453- count_cell_instances (*it);
454- update_universe_cell_count (univ_indx, *it);
455- }
456- }
457- }
424+ for (auto & univ : model::universes) {
425+ std::unordered_map<int32_t , int32_t > univ_count_memo;
426+ univ->n_instances_ = count_universe_instances (
427+ model::root_universe, univ->id_ , univ_count_memo);
458428 }
459429}
460430
@@ -528,19 +498,16 @@ std::string distribcell_path_inner(int32_t target_cell, int32_t map,
528498
529499 // Material cells don't contain other cells so ignore them.
530500 if (c.type_ != Fill::MATERIAL) {
531- int32_t temp_offset;
532- if (c.type_ == Fill::UNIVERSE) {
533- temp_offset =
534- offset + c.offset_ [map]; // TODO: should also apply to lattice fills?
535- } else {
501+ int32_t temp_offset = offset + c.offset_ [map];
502+ if (c.type_ == Fill::LATTICE) {
536503 Lattice& lat = *model::lattices[c.fill_ ];
537504 int32_t indx = lat.universes_ .size () * map + lat.begin ().indx_ ;
538- temp_offset = offset + lat.offsets_ [indx];
505+ temp_offset += lat.offsets_ [indx];
539506 }
540507
541508 // The desired cell is the first cell that gives an offset smaller or
542509 // equal to the target offset.
543- if (temp_offset <= target_offset - c. offset_ [map] )
510+ if (temp_offset <= target_offset)
544511 break ;
545512 }
546513 }
@@ -570,12 +537,12 @@ std::string distribcell_path_inner(int32_t target_cell, int32_t map,
570537 path << " l" << lat.id_ ;
571538 for (ReverseLatticeIter it = lat.rbegin (); it != lat.rend (); ++it) {
572539 int32_t indx = lat.universes_ .size () * map + it.indx_ ;
573- int32_t temp_offset = offset + lat.offsets_ [indx];
574- if (temp_offset <= target_offset - c. offset_ [map] ) {
540+ int32_t temp_offset = offset + lat.offsets_ [indx] + c. offset_ [map] ;
541+ if (temp_offset <= target_offset) {
575542 offset = temp_offset;
576543 path << " (" << lat.index_to_string (it.indx_ ) << " )->" ;
577- path << distribcell_path_inner (target_cell, map, target_offset,
578- *model::universes[*it], offset + c. offset_ [map] );
544+ path << distribcell_path_inner (
545+ target_cell, map, target_offset, *model::universes[*it], offset);
579546 return path.str ();
580547 }
581548 }
0 commit comments