Skip to content

Commit

Permalink
Rename current_source_file -> ssw_current_file
Browse files Browse the repository at this point in the history
  • Loading branch information
paulromano committed Oct 3, 2024
1 parent c44f1fc commit 9e9335d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 39 deletions.
12 changes: 6 additions & 6 deletions include/openmc/simulation.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ constexpr int STATUS_EXIT_ON_TRIGGER {2};

namespace simulation {

extern "C" int current_batch; //!< current batch
extern "C" int current_surface_file; //!< currente surface source file
extern "C" int current_gen; //!< current fission generation
extern "C" bool initialized; //!< has simulation been initialized?
extern "C" double keff; //!< average k over batches
extern "C" double keff_std; //!< standard deviation of average k
extern "C" int current_batch; //!< current batch
extern "C" int current_gen; //!< current fission generation
extern "C" bool initialized; //!< has simulation been initialized?
extern "C" double keff; //!< average k over batches
extern "C" double keff_std; //!< standard deviation of average k
extern "C" double k_col_abs; //!< sum over batches of k_collision * k_absorption
extern "C" double
k_col_tra; //!< sum over batches of k_collision * k_tracklength
Expand All @@ -38,6 +37,7 @@ extern "C" int n_lost_particles; //!< cumulative number of lost particles
extern "C" bool need_depletion_rx; //!< need to calculate depletion rx?
extern "C" int restart_batch; //!< batch at which a restart job resumed
extern "C" bool satisfy_triggers; //!< have tally triggers been satisfied?
extern int ssw_current_file; //!< current surface source file
extern "C" int total_gen; //!< total number of generations simulated
extern double total_weight; //!< Total source weight in a batch
extern int64_t work_per_rank; //!< number of particles per MPI rank
Expand Down
5 changes: 5 additions & 0 deletions src/finalize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ int openmc_finalize()
settings::source_latest = false;
settings::source_separate = false;
settings::source_write = true;
settings::ssw_cell_id = C_NONE;
settings::ssw_cell_type = SSWCellType::None;
settings::ssw_max_particles = 0;
settings::ssw_max_files = 1;
settings::survival_biasing = false;
settings::temperature_default = 293.6;
settings::temperature_method = TemperatureMethod::NEAREST;
Expand All @@ -141,6 +145,7 @@ int openmc_finalize()

simulation::keff = 1.0;
simulation::need_depletion_rx = false;
simulation::ssw_current_file = 1;
simulation::total_gen = 0;

simulation::entropy_mesh = nullptr;
Expand Down
32 changes: 19 additions & 13 deletions src/simulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ int openmc_simulation_init()
// Reset global variables -- this is done before loading state point (as that
// will potentially populate k_generation and entropy)
simulation::current_batch = 0;
simulation::current_surface_file = 1;
simulation::ssw_current_file = 1;
simulation::k_generation.clear();
simulation::entropy.clear();
openmc_reset();
Expand Down Expand Up @@ -297,7 +297,6 @@ namespace openmc {
namespace simulation {

int current_batch;
int current_surface_file;
int current_gen;
bool initialized {false};
double keff {1.0};
Expand All @@ -310,6 +309,7 @@ int n_lost_particles {0};
bool need_depletion_rx {false};
int restart_batch;
bool satisfy_triggers {false};
int ssw_current_file;
int total_gen {0};
double total_weight;
int64_t work_per_rank;
Expand Down Expand Up @@ -456,27 +456,33 @@ void finalize_batch()

// Write out surface source if requested.
if (settings::surf_source_write &&
simulation::current_surface_file <= settings::ssw_max_files) {
if (simulation::surf_source_bank.full() ||
simulation::current_batch == settings::n_batches) {
auto filename = settings::path_output + "surface_source." +
std::to_string(simulation::current_batch);
// no batches specified for writing, write surface source bank
simulation::ssw_current_file <= settings::ssw_max_files) {
bool last_batch = (simulation::current_batch == settings::n_batches);
if (simulation::surf_source_bank.full() || last_batch) {
// Determine appropriate filename
auto filename = fmt::format("{}surface_source.{}", settings::path_output,
simulation::current_batch);
if (settings::ssw_max_files == 1 ||
(simulation::current_surface_file == 1 &&
simulation::current_batch == settings::n_batches))
(simulation::ssw_current_file == 1 && last_batch)) {
filename = settings::path_output + "surface_source";
}

// Get span of source bank and calculate parallel index vector
auto surf_work_index = mpi::calculate_parallel_index_vector(
simulation::surf_source_bank.size());
gsl::span<SourceSite> surfbankspan(simulation::surf_source_bank.begin(),
simulation::surf_source_bank.size());

// Write surface source file
write_source_point(
filename, surfbankspan, surf_work_index, settings::surf_mcpl_write);

// Reset surface source bank and increment counter
simulation::surf_source_bank.clear();
if (simulation::current_batch < settings::n_batches &&
settings::ssw_max_files >= 1)
if (!last_batch && settings::ssw_max_files >= 1) {
simulation::surf_source_bank.reserve(settings::ssw_max_particles);
++simulation::current_surface_file;
}
++simulation::ssw_current_file;
}
}
}
Expand Down
43 changes: 23 additions & 20 deletions tests/unit_tests/test_surface_source_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import pytest
import h5py
import numpy as np
import os


@pytest.fixture(scope="module")
Expand Down Expand Up @@ -52,16 +51,16 @@ def model():
openmc.reset_auto_ids()
model = openmc.Model()

## Material
#material = openmc.Material(name="H1")
#material.add_element("H", 1.0)
# Material
h1 = openmc.Material(name="H1")
h1.add_nuclide("H1", 1.0)
h1.set_density('g/cm3', 1e-7)

# Geometry
radius = 1.0
sphere = openmc.Sphere(r=radius, boundary_type="vacuum")
cell = openmc.Cell(region=-sphere, fill=None)
root = openmc.Universe(cells=[cell])
model.geometry = openmc.Geometry(root)
cell = openmc.Cell(region=-sphere, fill=h1)
model.geometry = openmc.Geometry([cell])

# Settings
model.settings = openmc.Settings()
Expand All @@ -72,27 +71,31 @@ def model():

distribution = openmc.stats.Point()
model.settings.source = openmc.IndependentSource(space=distribution)

return model


@pytest.mark.parametrize(
"parameter",
"max_particles, max_source_files",
[
{"max_particles": 100, "max_source_files": 2},
{"max_particles": 100, "max_source_files": 3},
(100, 2),
(100, 3),
(100, 1),
],
)

def test_number_surface_source_file_created(parameter, run_in_tmpdir, model):
def test_number_surface_source_file_created(max_particles, max_source_files,
run_in_tmpdir, model):
"""Check the number of surface source files written."""
model.settings.surf_source_write = parameter
model.settings.surf_source_write = {
"max_particles": max_particles,
"max_source_files": max_source_files
}
model.run()
for i in range(1,parameter["max_source_files"]):
filename = "surface_source."+str(i)+".h5"
if not os.path.exists(filename):
assert False
if os.path.exists("surface_source.h5"):
assert False
should_be_numbered = max_source_files > 1
for i in range(1, max_source_files + 1):
if should_be_numbered:
assert Path(f"surface_source.{i}.h5").exists()
if not should_be_numbered:
assert Path("surface_source.h5").exists()

ERROR_MSG_1 = (
"A maximum number of particles needs to be specified "
Expand Down

0 comments on commit 9e9335d

Please sign in to comment.