Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding ability to set additional ww attrs from openmc.lib #2609

Merged
merged 5 commits into from
Jul 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions include/openmc/capi.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,16 @@ int openmc_weight_windows_get_bounds(int32_t index, const double** lower_bounds,
const double** upper_bounds, size_t* size);
int openmc_weight_windows_set_bounds(int32_t index, const double* lower_bounds,
const double* upper_bounds, size_t size);
int openmc_weight_windows_get_survival_ratio(int32_t index, double* ratio);
int openmc_weight_windows_set_survival_ratio(int32_t index, double ratio);
int openmc_weight_windows_get_max_lower_bound_ratio(
int32_t index, double* lb_ratio);
int openmc_weight_windows_set_max_lower_bound_ratio(
int32_t index, double lb_ratio);
int openmc_weight_windows_get_weight_cutoff(int32_t index, double* cutoff);
int openmc_weight_windows_set_weight_cutoff(int32_t index, double cutoff);
int openmc_weight_windows_get_max_split(int32_t index, int* max_split);
int openmc_weight_windows_set_max_split(int32_t index, int max_split);
size_t openmc_weight_windows_size();
int openmc_weight_windows_export(const char* filename = nullptr);
int openmc_weight_windows_import(const char* filename = nullptr);
Expand Down
16 changes: 16 additions & 0 deletions include/openmc/weight_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,22 @@ class WeightWindows {

void set_particle_type(ParticleType p_type);

double survival_ratio() const { return survival_ratio_; }

double& survival_ratio() { return survival_ratio_; }

double max_lower_bound_ratio() const { return max_lb_ratio_; }

double& max_lower_bound_ratio() { return max_lb_ratio_; }

int max_split() const { return max_split_; }

int& max_split() { return max_split_; }

double weight_cutoff() const { return weight_cutoff_; }

double& weight_cutoff() { return weight_cutoff_; }

//----------------------------------------------------------------------------
// Accessors
int32_t id() const { return id_; }
Expand Down
72 changes: 72 additions & 0 deletions openmc/lib/weight_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,38 @@
_dll.openmc_weight_windows_get_bounds.restype = c_int
_dll.openmc_weight_windows_get_bounds.errcheck = _error_handler

_dll.openmc_weight_windows_get_survival_ratio.argtypes = [c_int32, POINTER(c_double)]
_dll.openmc_weight_windows_get_survival_ratio.restype = c_int
_dll.openmc_weight_windows_get_survival_ratio.errcheck = _error_handler

_dll.openmc_weight_windows_set_survival_ratio.argtypes = [c_int32, c_double]
_dll.openmc_weight_windows_set_survival_ratio.restype = c_int
_dll.openmc_weight_windows_set_survival_ratio.errcheck = _error_handler

_dll.openmc_weight_windows_get_max_lower_bound_ratio.argtypes = [c_int32, POINTER(c_double)]
_dll.openmc_weight_windows_get_max_lower_bound_ratio.restype = c_int
_dll.openmc_weight_windows_get_max_lower_bound_ratio.errcheck = _error_handler

_dll.openmc_weight_windows_set_max_lower_bound_ratio.argtypes = [c_int32, c_double]
_dll.openmc_weight_windows_set_max_lower_bound_ratio.restype = c_int
_dll.openmc_weight_windows_set_max_lower_bound_ratio.errcheck = _error_handler

_dll.openmc_weight_windows_get_weight_cutoff.argtypes = [c_int32, POINTER(c_double)]
_dll.openmc_weight_windows_get_weight_cutoff.restype = c_int
_dll.openmc_weight_windows_get_weight_cutoff.errcheck = _error_handler

_dll.openmc_weight_windows_set_weight_cutoff.argtypes = [c_int32, c_double]
_dll.openmc_weight_windows_set_weight_cutoff.restype = c_int
_dll.openmc_weight_windows_set_weight_cutoff.errcheck = _error_handler

_dll.openmc_weight_windows_get_max_split.argtypes = [c_int32, POINTER(c_int)]
_dll.openmc_weight_windows_get_max_split.restype = c_int
_dll.openmc_weight_windows_get_max_split.errcheck = _error_handler

_dll.openmc_weight_windows_set_max_split.argtypes = [c_int32, c_int]
_dll.openmc_weight_windows_set_max_split.restype = c_int
_dll.openmc_weight_windows_set_max_split.errcheck = _error_handler


class WeightWindows(_FortranObjectWithID):
"""WeightWindows stored internally.
Expand Down Expand Up @@ -201,6 +233,46 @@ def bounds(self, bounds):

_dll.openmc_weight_windows_set_bounds(self._index, lower_p, upper_p, lower.size)

@property
def survival_ratio(self):
ratio = c_double()
_dll.openmc_weight_windows_get_survival_ratio(self._index, ratio)
return ratio.value

@survival_ratio.setter
def survival_ratio(self, ratio):
_dll.openmc_weight_windows_set_survival_ratio(self._index, ratio)

@property
def max_lower_bound_ratio(self):
lb_ratio = c_double()
_dll.openmc_weight_windows_get_max_lower_bound_ratio(self._index, lb_ratio)
return lb_ratio.value

@max_lower_bound_ratio.setter
def max_lower_bound_ratio(self, lb_ratio):
_dll.openmc_weight_windows_set_max_lower_bound_ratio(self._index, lb_ratio)

@property
def weight_cutoff(self):
cutoff = c_double()
_dll.openmc_weight_windows_get_weight_cutoff(self._index, cutoff)
return cutoff.value

@weight_cutoff.setter
def weight_cutoff(self, cutoff):
_dll.openmc_weight_windows_set_weight_cutoff(self._index, cutoff)

@property
def max_split(self):
max_split = c_int()
_dll.openmc_weight_windows_get_max_split(self._index, max_split)
return max_split.value

@max_split.setter
def max_split(self, max_split):
_dll.openmc_weight_windows_set_max_split(self._index, max_split)

def update_magic(self, tally, value='mean', threshold=1.0, ratio=5.0):
"""Update weight window values using the MAGIC method

Expand Down
1 change: 1 addition & 0 deletions openmc/weight_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ def __repr__(self) -> str:
string += '{: <16}=\t{}\n'.format('\tMesh', self.mesh)
string += '{: <16}=\t{}\n'.format('\tParticle Type', self._particle_type)
string += '{: <16}=\t{}\n'.format('\tEnergy Bounds', self._energy_bounds)
string += '{: <16}=\t{}\n'.format('\tMax lower bound ratio', self.max_lower_bound_ratio)
string += '{: <16}=\t{}\n'.format('\tLower WW Bounds', self._lower_ww_bounds)
string += '{: <16}=\t{}\n'.format('\tUpper WW Bounds', self._upper_ww_bounds)
string += '{: <16}=\t{}\n'.format('\tSurvival Ratio', self._survival_ratio)
Expand Down
80 changes: 80 additions & 0 deletions src/weight_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,86 @@ extern "C" int openmc_weight_windows_set_bounds(int32_t index,
return 0;
}

extern "C" int openmc_weight_windows_get_survival_ratio(
int32_t index, double* ratio)
{
if (int err = verify_ww_index(index))
return err;
const auto& wws = variance_reduction::weight_windows[index];
*ratio = wws->survival_ratio();
return 0;
}

extern "C" int openmc_weight_windows_set_survival_ratio(
int32_t index, double ratio)
{
if (int err = verify_ww_index(index))
return err;
const auto& wws = variance_reduction::weight_windows[index];
wws->survival_ratio() = ratio;
std::cout << "Survival ratio: " << wws->survival_ratio() << std::endl;
return 0;
}

extern "C" int openmc_weight_windows_get_max_lower_bound_ratio(
int32_t index, double* lb_ratio)
{
if (int err = verify_ww_index(index))
return err;
const auto& wws = variance_reduction::weight_windows[index];
*lb_ratio = wws->max_lower_bound_ratio();
return 0;
}

extern "C" int openmc_weight_windows_set_max_lower_bound_ratio(
int32_t index, double lb_ratio)
{
if (int err = verify_ww_index(index))
return err;
const auto& wws = variance_reduction::weight_windows[index];
wws->max_lower_bound_ratio() = lb_ratio;
return 0;
}

extern "C" int openmc_weight_windows_get_weight_cutoff(
int32_t index, double* cutoff)
{
if (int err = verify_ww_index(index))
return err;
const auto& wws = variance_reduction::weight_windows[index];
*cutoff = wws->weight_cutoff();
return 0;
}

extern "C" int openmc_weight_windows_set_weight_cutoff(
int32_t index, double cutoff)
{
if (int err = verify_ww_index(index))
return err;
const auto& wws = variance_reduction::weight_windows[index];
wws->weight_cutoff() = cutoff;
return 0;
}

extern "C" int openmc_weight_windows_get_max_split(
int32_t index, int* max_split)
{
if (int err = verify_ww_index(index))
return err;
const auto& wws = variance_reduction::weight_windows[index];
*max_split = wws->max_split();
return 0;
}

extern "C" int openmc_weight_windows_set_max_split(int32_t index, int max_split)
{
if (int err = verify_ww_index(index))
return err;
const auto& wws = variance_reduction::weight_windows[index];
wws->max_split() = max_split;
return 0;
}

extern "C" int openmc_extend_weight_windows(
int32_t n, int32_t* index_start, int32_t* index_end)
{
Expand Down
22 changes: 20 additions & 2 deletions tests/unit_tests/weightwindows/test_ww_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,19 +206,37 @@ def test_ww_import_export(run_in_tmpdir, model):

lb_before, up_before = ww.bounds

# set some additional weight windows properties after transport
ww.survival_ratio = 0.7
assert ww.survival_ratio == 0.7

ww.weight_cutoff = 1e-10
assert ww.weight_cutoff == 1e-10

ww.max_lower_bound_ratio = 200.0
assert ww.max_lower_bound_ratio == 200.0

ww.max_split = 26000
assert ww.max_split == 26000

openmc.lib.export_weight_windows()

assert Path('weight_windows.h5').exists()

openmc.lib.import_weight_windows('weight_windows.h5')

ww = openmc.lib.weight_windows[2]
imported_ww = openmc.lib.weight_windows[2]

lb_after, up_after = ww.bounds
lb_after, up_after = imported_ww.bounds

assert np.allclose(lb_before, lb_after)
assert np.allclose(up_before, up_after)

assert ww.survival_ratio == imported_ww.survival_ratio
assert ww.max_lower_bound_ratio == imported_ww.max_lower_bound_ratio
assert ww.weight_cutoff == imported_ww.weight_cutoff
assert ww.max_split == imported_ww.max_split

openmc.lib.finalize()


Expand Down