Change Operator.eval() -> Operator.__call__()

This commit is contained in:
Paul Romano 2018-02-14 12:57:56 -06:00
parent 939d47cffa
commit f494fecf21
6 changed files with 84 additions and 83 deletions

View file

@ -27,33 +27,19 @@ class Settings(object):
class Operator(metaclass=ABCMeta):
"""The Operator metaclass.
This defines all functions that the integrator needs to operate.
"""Abstract class defining all methods needed for the integrator.
Attributes
----------
settings : Settings
Settings object.
"""
"""
def __init__(self, settings):
self.settings = settings
@abstractmethod
def initial_condition(self):
"""Performs final setup and returns initial condition.
Returns
-------
list of numpy.array
Total density for initial conditions.
"""
pass
@abstractmethod
def eval(self, vec, print_out=True):
def __call__(self, vec, print_out=True):
"""Runs a simulation.
Parameters
@ -72,6 +58,17 @@ class Operator(metaclass=ABCMeta):
seed : int
Seed for this simulation.
"""
pass
@abstractmethod
def initial_condition(self):
"""Performs final setup and returns initial condition.
Returns
-------
list of numpy.array
Total density for initial conditions.
"""
pass
@ -114,3 +111,7 @@ class Operator(metaclass=ABCMeta):
"""
pass
@abstractmethod
def finalize(self):
pass

View file

@ -62,7 +62,7 @@ def cecm(operator, print_out=True):
eigvls = []
rates_array = []
eigvl, rates, seed = operator.eval(x[0])
eigvl, rates, seed = operator(x[0])
eigvls.append(eigvl)
seeds.append(seed)
@ -86,7 +86,7 @@ def cecm(operator, print_out=True):
x.append(x_result)
eigvl, rates, seed = operator.eval(x[1])
eigvl, rates, seed = operator(x[1])
eigvls.append(eigvl)
seeds.append(seed)
@ -119,7 +119,7 @@ def cecm(operator, print_out=True):
seeds = []
eigvls = []
rates_array = []
eigvl, rates, seed = operator.eval(x[0])
eigvl, rates, seed = operator(x[0])
eigvls.append(eigvl)
seeds.append(seed)

View file

@ -53,7 +53,7 @@ def predictor(operator, print_out=True):
eigvls = []
rates_array = []
eigvl, rates, seed = operator.eval(x[0])
eigvl, rates, seed = operator(x[0])
eigvls.append(eigvl)
seeds.append(seed)
@ -86,7 +86,7 @@ def predictor(operator, print_out=True):
seeds = []
eigvls = []
rates_array = []
eigvl, rates, seed = operator.eval(x[0])
eigvl, rates, seed = operator(x[0])
eigvls.append(eigvl)
seeds.append(seed)

View file

@ -207,6 +207,58 @@ class OpenMCOperator(Operator):
# Create reaction rate tables
self.initialize_reaction_rates()
def __call__(self, vec, print_out=True):
"""Runs a simulation.
Parameters
----------
vec : list of numpy.array
Total atoms to be used in function.
print_out : bool, optional
Whether or not to print out time.
Returns
-------
mat : list of scipy.sparse.csr_matrix
Matrices for the next step.
k : float
Eigenvalue of the problem.
rates : openmc.deplete.ReactionRates
Reaction rates from this simulation.
seed : int
Seed for this simulation.
"""
# Prevent OpenMC from complaining about re-creating tallies
openmc.reset_auto_ids()
# Update status
self.set_density(vec)
time_start = time.time()
# Update material compositions and tally nuclides
self._update_materials()
openmc.capi.tallies[1].nuclides = self._get_tally_nuclides()
# Run OpenMC
openmc.capi.reset()
openmc.capi.run()
time_openmc = time.time()
# Extract results
k = self.unpack_tallies_and_normalize()
if comm.rank == 0:
time_unpack = time.time()
if print_out:
print("Time to openmc: ", time_openmc - time_start)
print("Time to unpack: ", time_unpack - time_openmc)
return k, copy.deepcopy(self.reaction_rates), self.seed
def extract_mat_ids(self):
"""Extracts materials and assigns them to processes.
@ -365,58 +417,6 @@ class OpenMCOperator(Operator):
self.chain.nuc_to_react_ind = self.burn_nuc_to_ind
def eval(self, vec, print_out=True):
"""Runs a simulation.
Parameters
----------
vec : list of numpy.array
Total atoms to be used in function.
print_out : bool, optional
Whether or not to print out time.
Returns
-------
mat : list of scipy.sparse.csr_matrix
Matrices for the next step.
k : float
Eigenvalue of the problem.
rates : openmc.deplete.ReactionRates
Reaction rates from this simulation.
seed : int
Seed for this simulation.
"""
# Prevent OpenMC from complaining about re-creating tallies
openmc.reset_auto_ids()
# Update status
self.set_density(vec)
time_start = time.time()
# Update material compositions and tally nuclides
self._update_materials()
openmc.capi.tallies[1].nuclides = self._get_tally_nuclides()
# Run OpenMC
openmc.capi.reset()
openmc.capi.run()
time_openmc = time.time()
# Extract results
k = self.unpack_tallies_and_normalize()
if comm.rank == 0:
time_unpack = time.time()
if print_out:
print("Time to openmc: ", time_openmc - time_start)
print("Time to unpack: ", time_unpack - time_openmc)
return k, copy.deepcopy(self.reaction_rates), self.seed
def form_matrix(self, y, mat):
"""Forms the depletion matrix.

View file

@ -21,14 +21,7 @@ class DummyGeometry(Operator):
def __init__(self, settings):
super().__init__(settings)
def finalize(self):
pass
@property
def chain(self):
return self
def eval(self, vec, print_out=False):
def __call__(self, vec, print_out=False):
"""Evaluates F(y)
Parameters
@ -60,6 +53,13 @@ class DummyGeometry(Operator):
# Create a fake rates object
return 0.0, reaction_rates, 0
def finalize(self):
pass
@property
def chain(self):
return self
def form_matrix(self, rates):
"""Forms the f(y) matrix in y' = f(y)y.

View file

@ -11,7 +11,7 @@ from openmc.deplete import utilities
from tests import dummy_geometry
def test_predictor():
def test_predictor(run_in_tmpdir):
"""Integral regression test of integrator algorithm using predictor/corrector"""
settings = openmc.deplete.Settings()