Search
Functions to assist with optimization problems, like a search for critical. To use mcnpy.search.search_for_keff, the user must provide callable functions for creating an MCNP deck, running MCNP, and extracting the simulation outputs. To make these functions less ambiguous, some examples are provided.
Important
To use search_for_keff, SciPy must be installed. Try running pip install scipy to install the package.
Building a deck requires a function which accepts an optimization parameter and optional keyword arguments. For instance, we could optimize control rod bank height of the MCNPy RCF model (mcnpy.example.RCF).
def make_deck(h, **kwargs):
"""Create RCF model with at rod height `h`.
Parameters
----------
h : float
RCF control rod bank height.
**kwargs : dict
Additional keyword arguments.
Returns
-------
filename : str
Name of the serialized MCNP deck.
"""
name = 'rcf_rods_' + str(height) + '.mcnp'
# RCF model at 68in water height, bank height h
rcf = mp.RCF(name, 68, h)
# Produce an MCTAL file
rcf.deck += mp.PrintDump(print_mctal=1)
# Write the deck to file
filename = 'optimization/' + name
rcf.deck.write(filename)
# Return name of deck file
return filename
While MCNPy does offer the mcnpy.run_mcnp and mcnpy.run_script functions to execute an MCNP simulation, mcnpy.search.search_for_keff requires a run function which only accepts an MCNP file name. However, the included MCNPy run functions can still be utilized.
def run_mcnp(file):
mp.run_mcnp(file, inp=False, threads=6)
Note
Setting inp=False is useful for producing outputs with predicable names such as my_deck.mcnpm for a MCTAL file.
After a simulation completes, the results must be extracted. A basic function for this is included as mcnpy.search.get_keff which utilizes MCNPTools.
def get_keff(file:str):
"""Get final keff from an MCTAL file.
Parameters
----------
file : str
Name of MCTAL file with keff outputs.
Returns
-------
keff_value : float
Final keff value.
keff_std : float
Final keff standard deviation.
"""
from mcnptools import Mctal, MctalKcode
m = Mctal(file+'m')
kc = m.GetKcode()
keff = MctalKcode.AVG_COMBINED_KEFF
keff_std = MctalKcode.AVG_COMBINED_KEFF_STD
return (kc.GetValue(keff, kc.GetCycles()-1),
kc.GetValue(keff_std, kc.GetCycles()-1))
Note
This is the default get_keff function chosen if a user-define option is not sepcified.
Using the previous example functions, executing a keff search then becomes
from mcnpy.search import search_for_keff, get_keff
crit_height, guesses, keffs = search_for_keff(make_deck, run_mcnp, get_keff,
bracket=[0.0, 36.0],
tol=1e-5,
print_iterations=True,
bracketed_method='brentq')
Copyright (c) 2011-2023 Massachusetts Institute of Technology, UChicago Argonne LLC, and OpenMC contributors
Copyright (c) 2023 NuCoMP
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- mcnpy.search.get_keff(file: str)[source]
Get final keff from an MCTAL file.
- Parameters
file (str) – Name of MCTAL file with keff outputs.
- Returns
keff_value (float) – Final keff value.
keff_std (float) – Final keff standard deviation.
- mcnpy.search.search_for_keff(make_deck, run_mcnp, get_keff=<function get_keff>, deck_args={}, initial_guess=None, target=1.0, bracket=None, tol=None, bracketed_method='bisect', print_iterations=False)[source]
Keff optimization search function.
- Parameters
make_deck (collections.Callable) – Callable function which builds a deck according to a passed parameter. This function must return the name of an on-disk MCNP deck. Can be helpful to generate a unique name for each deck, like my_deck.iteration#.mcnp.
run_mcnp (collections.Callable) – Callable function which executes an MCNP simulation. Must accept the name of an on-disk MCNP file. Recommended to run MCNP with the N=input_name convention to create outputs with recognizable names.
get_keff (collections.Callable) – Callable function which retrieves a keff value. Must accept the name of an on-disk MCNP file. Recommended to run MCNP with the N=input_name convention to create outputs with recognizable names. Also recommended to generate an MCTAL file and use MCNPTools to extract keff values.
deck_args (dict) – Keyword-based arguments to pass to the make_deck method.
initial_guess (float) – Initial guess for keff.
target (float) – Target value for keff.
bracket (Iterable of float) – Lower and upper limits for bracketed search method.
tol (float) – Absolute tolerance.
bracketed_method (str) – Choice of bracketed methods. Valid option are bisect, brentq, brenth, and ridder. Names correspond to their SciPy functions.
print_iterations (bool) – Whether to print information for each iteration.
- Returns
zero_value (float) – Value of the optimized parameter.
guesses (list) – List of parameter guesses at each iteration.
results (list) – List of keff result at each iteration.