-
Notifications
You must be signed in to change notification settings - Fork 8
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
Latin Hypercube Neutronic Sampling #10
base: main
Are you sure you want to change the base?
Changes from all commits
5352233
b1f3e4e
5fb7b85
f995797
a9ca809
f6797df
71d51d4
30bba7e
d89deca
b0e8814
786c062
50be3ad
a44887c
abe77b5
ebb7296
2e17eb6
677fa4e
2f53046
667a118
1a5e34a
39216a6
d0101a6
162d809
47cfb2a
88839e8
e2d25cb
a860836
2f3ac24
0d1e51e
c3f6ffc
22d0077
4e511cd
4681c38
202f162
73d4418
7cd54b2
390aec9
49a450e
c73d6c7
af30af4
f0c4c66
82d4db7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
"""This module contains functions to sweep parameter-space, creating MCNP | ||
depletion inputs to calculate keff at EOL. | ||
* AR | ||
* core_z | ||
* cool_r | ||
* PD | ||
* power | ||
* enrich | ||
""" | ||
|
||
from pyDOE import lhs | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PEP8: missing blank line |
||
import os | ||
import glob | ||
import numpy as np | ||
import tarfile | ||
|
||
from mcnp_inputs import HomogeneousInput, build_pyne_matlib | ||
|
||
# reactor parameters to be sampled. Add parameters with caution | ||
_dimensions = ['AR', 'core_r', 'cool_r', 'PD', 'power', 'enrich'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. doctoring to explain what those variables are. |
||
|
||
# number of samples for each sampled dimension | ||
nsamples = 100 | ||
|
||
# Set dimension values. A tuple with range boundaries will sample the dimension | ||
# evenly using LHS, a float will set the dimension to the constant value | ||
dims = {'core_r' : (20, 50), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't |
||
'AR' : (0.7, 1.3), | ||
'PD' : (1.4, 1.6), | ||
'enrich' : (0.3, 0.9), | ||
'cool_r' : 0.5, | ||
'power' : 150 | ||
} | ||
|
||
def get_sampling_params(): | ||
"""Decide which parameters are constants and which are ranges to be sampled. | ||
Convert constants (float) to ranges (tuples) with zero width. | ||
Returns: | ||
-------- | ||
sampled (list): list of keys for sampled parameters | ||
""" | ||
for item in _dimensions: | ||
if type(dims[item]) != tuple: | ||
dims[item] = (dims[item], dims[item]) | ||
|
||
sampled = _dimensions | ||
|
||
return sampled | ||
|
||
def gen_hypercube(samples, N, seed=4654562): | ||
"""Generate N-dimensional latin hypercube to sample dimensional reactor | ||
space. | ||
|
||
Arguments: | ||
---------- | ||
samples (int): number of test cases to try | ||
N (int): number of dimensions to test | ||
|
||
Returns: | ||
-------- | ||
cube (ndarray): normalized, N-D latin hypercube | ||
""" | ||
|
||
np.random.seed(seed) | ||
hypercube = lhs(N, samples=samples) | ||
|
||
return hypercube | ||
|
||
def fill_data_array(swept, cube): | ||
"""Fill an ndarray with the sampling set generated by lhs and bounded by the | ||
ranges provided for each sampled dimension. | ||
|
||
Arguments: | ||
---------- | ||
swept (list): keys for sampled dimensions | ||
const (list): keys for constant dimensions | ||
cube (ndarray): latin hypercube | ||
|
||
Returns: | ||
-------- | ||
rxt_confs (ndarray): array of data for every reactor configuration. This | ||
data is used to build MCNP6 input files. | ||
""" | ||
# initialize array | ||
rxt_confs = np.zeros(nsamples, dtype={'names' : list(dims.keys()), | ||
'formats' : ['f8']*len(dims)}) | ||
|
||
# for all samples in latin hypercube | ||
for didx, parm in enumerate(_dimensions): | ||
l_limit = dims[parm][0] | ||
u_limit = dims[parm][1] | ||
# uniform distribution | ||
width = u_limit - l_limit | ||
|
||
rxt_confs[parm] = np.add(l_limit, cube[:,didx]*width) | ||
|
||
return rxt_confs | ||
|
||
def write_inputs(sampling_data): | ||
"""Write MCNP depletion inputs for each reactor configuration. | ||
|
||
Arguments: | ||
---------- | ||
sampling_data (ndarray): array of reactor configurations, one for each | ||
MCNP6 input file. | ||
""" | ||
# build PyNE material library | ||
pyne_mats = build_pyne_matlib() | ||
# initialize tarball to save input files | ||
tarputs = tarfile.open('smpl_mcnp_depl_inps.tar', 'w') | ||
# write HTC input list | ||
htc_inputs = open('input_list.txt', 'w') | ||
|
||
# generate inputs | ||
for num, sample in enumerate(sampling_data): | ||
input = HomogeneousInput(sample['core_r'], | ||
sample['core_r']*sample['AR'], | ||
sample['power'], pyne_mats) | ||
homog_comp = input.homog_core(sample['enrich'], | ||
sample['cool_r'], | ||
sample['PD']) | ||
input.write_mat_string() | ||
|
||
# identifying header string for post-processing | ||
header_str = '' | ||
for param in _dimensions: | ||
header_str += str(round(sample[param], 5)) + ',' | ||
# write the input and tar it | ||
filename = input.write_input(num, header_str) | ||
tarputs.add(filename) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be cleanest to delete the file as soon as possible after you add it to the tarfile. |
||
os.remove(filename) | ||
htc_inputs.write('{0}.i\n'.format(num)) | ||
|
||
htc_inputs.close() | ||
tarputs.add('input_list.txt') | ||
tarputs.close() | ||
os.remove('input_list.txt') | ||
|
||
if __name__=='__main__': | ||
swept = get_sampling_params() | ||
cube = gen_hypercube(nsamples, len(swept)) | ||
data = fill_data_array(swept, cube) | ||
write_inputs(data) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add arguments to docstring....
Not sure what
file_num
is: is it actually just a number? If so, then not sure about the filename below