From 9fe9c1391706cb3e572f4db930ca3143d5390730 Mon Sep 17 00:00:00 2001 From: church89 Date: Tue, 6 Feb 2024 10:05:34 +0100 Subject: [PATCH] make batchwise get cell attribure method public --- openmc/deplete/abc.py | 14 ++++--- openmc/deplete/batchwise.py | 80 ++++++++++++++++++++++++++++++++++--- 2 files changed, 82 insertions(+), 12 deletions(-) diff --git a/openmc/deplete/abc.py b/openmc/deplete/abc.py index 5f5880866b7..7f3fda76afa 100644 --- a/openmc/deplete/abc.py +++ b/openmc/deplete/abc.py @@ -26,7 +26,7 @@ from .pool import deplete from .transfer_rates import TransferRates from openmc import Material, Cell -from .batchwise import (BatchwiseCellGeometrical, BatchwiseCellTemperature, +from .batchwise import (BatchwisePure, BatchwiseCellGeometrical, BatchwiseCellTemperature, BatchwiseMaterialRefuel, BatchwiseMaterialDilute, BatchwiseMaterialAdd, BatchwiseSchemeStd, BatchwiseSchemeRefuel, BatchwiseSchemeFlex) @@ -814,7 +814,7 @@ def integrate(self, final_step=True, output=True): n, root = self._get_bos_from_batchwise(i, n) else: # Store root at previous timestep - root = self.batchwise._get_cell_attrib() + root = self.batchwise.get_root() else: root = None n, res = self._get_bos_data_from_operator(i, source_rate, n) @@ -884,7 +884,7 @@ def integrate_adaptive(self, material_id, nuclides, final_step=True, with change_directory(self.operator.output_dir): n = self.operator.initial_condition() t, self._i_res = self._get_start_data() - + nuc_ids = [id for nuc,id in self.chain.nuclide_dict.items() if nuc in nuclides] dt = self.timesteps[0] @@ -935,7 +935,7 @@ def integrate_adaptive(self, material_id, nuclides, final_step=True, n = n_list.pop() StepResult.save(self.operator, n_list, res_list, [t, t + dt], source_rate, self._i_res + i, proc_time, root) - + for rank in range(comm.size): number_i = comm.bcast(self.operator.number, root=rank) if material_id in number_i.materials: @@ -947,7 +947,7 @@ def integrate_adaptive(self, material_id, nuclides, final_step=True, tol, err, rho1, rho2, f) comm.barrier() dt = comm.bcast(dt, root=rank_mat) - + t += dt i += 1 # Final simulation -- in the case that final_step is False, a zero @@ -969,7 +969,7 @@ def integrate_adaptive(self, material_id, nuclides, final_step=True, def _adapt_timestep(self, n_pred, n_corr, mat_idx, nuc_ids, tol, err, rho1, rho2, f ): - + filt_pred = take(n_pred[mat_idx], nuc_ids) filt_corr = take(n_corr[mat_idx], nuc_ids) @@ -1072,6 +1072,8 @@ def add_redox(self, mat, buffer, oxidation_states): self.transfer_rates.set_redox(mat, buffer, oxidation_states) def add_material(self, mat, value, mat_vector, timestep, quantity='grams'): + if self.batchwise is None: + self.batchwise = BatchwisePure(self.operator, self.operator.model) self.batchwise.add_material(mat, value, mat_vector, timestep, quantity) @add_params diff --git a/openmc/deplete/batchwise.py b/openmc/deplete/batchwise.py index 0d233734afe..474c06f985a 100644 --- a/openmc/deplete/batchwise.py +++ b/openmc/deplete/batchwise.py @@ -162,6 +162,11 @@ def target(self, value): def from_params(cls, obj, attr, operator, model, **kwargs): return cls(obj, attr, operator, model, **kwargs) + @abstractmethod + def get_root(self): + """ + """ + @abstractmethod def _model_builder(self, param): """ @@ -619,6 +624,61 @@ def _add_material(self, x, mat, values): x[mat_idx][nuc_idx] += val return x +class BatchwisePure(Batchwise): + def __init__(self, operator, model, density_treatment='constant-volume', + redox_vec=None): + super().__init__(operator, model, [0,1], [-100,100]) + self.cell_materials = None + + def get_root(self): + return np.nan + + def search_for_keff(self, x, step_index): + """ + Perform the criticality search on the parametric cell coefficient and + update materials accordingly. + The :meth:`openmc.search.search_for_keff` solution is then set as the + new cell attribute. + Parameters + ---------- + x : list of numpy.ndarray + Total atoms concentrations + Returns + ------- + x : list of numpy.ndarray + Updated total atoms concentrations + """ + x = super()._update_materials(x, step_index) + # if at least one of the cell materials is depletable, calculate new + # volume and update x and number accordingly. + # Alternatively, if material has been edded x needs to be updated + # TODO: improve this + if self.cell_materials: + volumes = self._calculate_volumes() + x = super()._update_x_and_set_volumes(x, volumes) + + return x, np.nan + + def _model_builder(self, param): + """ + Builds the parametric model to be passed to the + :meth:`openmc.search.search_for_keff` method. + Callable function which builds a model according to a passed + parameter. This function must return an openmc.model.Model object. + Parameters + ---------- + param : parameter + model function variable + Returns + ------- + _model : openmc.model.Model + OpenMC parametric model + """ + + def update_from_restart(self, step_index, x, root): + """ + """ + class BatchwiseCell(Batchwise): """Abstract class holding batch wise cell-based functions. @@ -714,6 +774,9 @@ def _set_cell_attrib(self, val): cell coefficient to set """ + def get_root(self): + return self._get_cell_attrib() + def _get_cell(self, val): """Helper method for getting cell from cell instance or cell name or id. Parameters @@ -1173,6 +1236,11 @@ def _model_builder(self, param): Openmc parametric model """ + def get_root(self): + for mat in openmc.lib.materials.values(): + if mat.id in [m.id for m in self.materials]: + return mat.get_density() + def search_for_keff(self, x, step_index): """ Perform the criticality search on parametric material variable. @@ -1935,8 +2003,8 @@ def __init__(self, bw_list, n_timesteps, dilute_interval, restart_level, self.dilute_interval = dilute_interval self.interrupt = interrupt - def _get_cell_attrib(self): - return self.bw_geom._get_cell_attrib() + def get_root(self): + return self.bw_geom.get_root() def set_density_function(self, mats, density_func, oxidation_states): for bw in self.bw_list: @@ -2046,8 +2114,8 @@ def __init__(self, bw_list, restart_level): else: self.restart_level = restart_level - def _get_cell_attrib(self): - return self.bw_geom._get_cell_attrib() + def get_root(self): + return self.bw_geom.get_root() def set_density_function(self, mats, density_func, oxidation_states): for bw in self.bw_list: @@ -2176,8 +2244,8 @@ def __init__(self, bw_list, n_timesteps, nuclide, limit, flex_fraction, span, # self.converged_flex = False - def _get_cell_attrib(self): - return self.bw_geom_trans._get_cell_attrib() + def get_root(self): + return self.bw_geom_trans.get_root() def set_density_function(self, mats, density_func, oxidation_states): for bw in self.bw_list: