Skip to content

Commit 049a852

Browse files
authored
Random Ray main function and autosetup refactoring (#3733)
1 parent 6b43a29 commit 049a852

3 files changed

Lines changed: 193 additions & 215 deletions

File tree

include/openmc/random_ray/random_ray_simulation.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ class RandomRaySimulation {
1919

2020
//----------------------------------------------------------------------------
2121
// Methods
22-
void compute_segment_correction_factors();
2322
void apply_fixed_sources_and_mesh_domains();
2423
void prepare_fixed_sources_adjoint();
24+
void prepare_adjoint_simulation();
2525
void simulate();
2626
void output_simulation_results() const;
2727
void instability_check(
@@ -34,9 +34,15 @@ class RandomRaySimulation {
3434
// Accessors
3535
FlatSourceDomain* domain() const { return domain_.get(); }
3636

37+
//----------------------------------------------------------------------------
38+
// Public data members
39+
40+
// Flag for adjoint simulation;
41+
bool adjoint_needed_;
42+
3743
private:
3844
//----------------------------------------------------------------------------
39-
// Data members
45+
// Private data members
4046

4147
// Contains all flat source region data
4248
unique_ptr<FlatSourceDomain> domain_;
@@ -51,6 +57,9 @@ class RandomRaySimulation {
5157
// Number of energy groups
5258
int negroups_;
5359

60+
// Toggle for first simulation
61+
bool is_first_simulation_;
62+
5463
}; // class RandomRaySimulation
5564

5665
//============================================================================
@@ -60,6 +69,7 @@ class RandomRaySimulation {
6069
void openmc_run_random_ray();
6170
void validate_random_ray_inputs();
6271
void openmc_reset_random_ray();
72+
void print_adjoint_header();
6373

6474
} // namespace openmc
6575

openmc/model/model.py

Lines changed: 88 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,8 +1179,8 @@ def plot(
11791179

11801180
# Convert ID map to RGB image
11811181
img = id_map_to_rgb(
1182-
id_map=id_map,
1183-
color_by=color_by,
1182+
id_map=id_map,
1183+
color_by=color_by,
11841184
colors=colors,
11851185
overlap_color=overlap_color
11861186
)
@@ -1217,7 +1217,7 @@ def plot(
12171217
extent=(x_min, x_max, y_min, y_max),
12181218
**contour_kwargs
12191219
)
1220-
1220+
12211221
# If only showing outline, set the axis limits and aspect explicitly
12221222
if outline == 'only':
12231223
axes.set_xlim(x_min, x_max)
@@ -1685,6 +1685,85 @@ def differentiate_mats(self, diff_volume_method: str = None, depletable_only: bo
16851685
self.geometry.get_all_materials().values()
16861686
)
16871687

1688+
def _auto_generate_mgxs_lib(
1689+
self,
1690+
model: openmc.model.model,
1691+
groups: openmc.mgxs.EnergyGroups,
1692+
correction: str | none,
1693+
directory: pathlike,
1694+
) -> openmc.mgxs.Library:
1695+
"""
1696+
Automatically generate a multi-group cross section libray from a model
1697+
with the specified group structure.
1698+
1699+
Parameters
1700+
----------
1701+
groups : openmc.mgxs.EnergyGroups
1702+
Energy group structure for the MGXS.
1703+
nparticles : int
1704+
Number of particles to simulate per batch when generating MGXS.
1705+
mgxs_path : str
1706+
Filename for the MGXS HDF5 file.
1707+
correction : str
1708+
Transport correction to apply to the MGXS. Options are None and
1709+
"P0".
1710+
directory : str
1711+
Directory to run the simulation in, so as to contain XML files.
1712+
1713+
Returns
1714+
-------
1715+
mgxs_lib : openmc.mgxs.Library
1716+
OpenMC MGXS Library object
1717+
"""
1718+
1719+
# Initialize MGXS library with a finished OpenMC geometry object
1720+
mgxs_lib = openmc.mgxs.Library(model.geometry)
1721+
1722+
# Pick energy group structure
1723+
mgxs_lib.energy_groups = groups
1724+
1725+
# Disable transport correction
1726+
mgxs_lib.correction = correction
1727+
1728+
# Specify needed cross sections for random ray
1729+
if correction == 'P0':
1730+
mgxs_lib.mgxs_types = [
1731+
'nu-transport', 'absorption', 'nu-fission', 'fission',
1732+
'consistent nu-scatter matrix', 'multiplicity matrix', 'chi'
1733+
]
1734+
elif correction is None:
1735+
mgxs_lib.mgxs_types = [
1736+
'total', 'absorption', 'nu-fission', 'fission',
1737+
'consistent nu-scatter matrix', 'multiplicity matrix', 'chi'
1738+
]
1739+
1740+
# Specify a "material" domain type for the cross section tally filters
1741+
mgxs_lib.domain_type = "material"
1742+
1743+
# Specify the domains over which to compute multi-group cross sections
1744+
mgxs_lib.domains = model.geometry.get_all_materials().values()
1745+
1746+
# Do not compute cross sections on a nuclide-by-nuclide basis
1747+
mgxs_lib.by_nuclide = False
1748+
1749+
# Check the library - if no errors are raised, then the library is satisfactory.
1750+
mgxs_lib.check_library_for_openmc_mgxs()
1751+
1752+
# Construct all tallies needed for the multi-group cross section library
1753+
mgxs_lib.build_library()
1754+
1755+
# Create a "tallies.xml" file for the MGXS Library
1756+
mgxs_lib.add_to_tallies(model.tallies, merge=True)
1757+
1758+
# Run
1759+
statepoint_filename = model.run(cwd=directory)
1760+
1761+
# Load MGXS
1762+
with openmc.StatePoint(statepoint_filename) as sp:
1763+
mgxs_lib.load_from_statepoint(sp)
1764+
1765+
return mgxs_lib
1766+
16881767
def _create_mgxs_sources(
16891768
self,
16901769
groups: openmc.mgxs.EnergyGroups,
@@ -1848,52 +1927,8 @@ def _generate_infinite_medium_mgxs(
18481927
model.geometry.root_universe = infinite_universe
18491928

18501929
# Add MGXS Tallies
1851-
1852-
# Initialize MGXS library with a finished OpenMC geometry object
1853-
mgxs_lib = openmc.mgxs.Library(model.geometry)
1854-
1855-
# Pick energy group structure
1856-
mgxs_lib.energy_groups = groups
1857-
1858-
# Disable transport correction
1859-
mgxs_lib.correction = correction
1860-
1861-
# Specify needed cross sections for random ray
1862-
if correction == 'P0':
1863-
mgxs_lib.mgxs_types = [
1864-
'nu-transport', 'absorption', 'nu-fission', 'fission',
1865-
'consistent nu-scatter matrix', 'multiplicity matrix', 'chi'
1866-
]
1867-
elif correction is None:
1868-
mgxs_lib.mgxs_types = [
1869-
'total', 'absorption', 'nu-fission', 'fission',
1870-
'consistent nu-scatter matrix', 'multiplicity matrix', 'chi'
1871-
]
1872-
1873-
# Specify a "cell" domain type for the cross section tally filters
1874-
mgxs_lib.domain_type = "material"
1875-
1876-
# Specify the cell domains over which to compute multi-group cross sections
1877-
mgxs_lib.domains = model.geometry.get_all_materials().values()
1878-
1879-
# Do not compute cross sections on a nuclide-by-nuclide basis
1880-
mgxs_lib.by_nuclide = False
1881-
1882-
# Check the library - if no errors are raised, then the library is satisfactory.
1883-
mgxs_lib.check_library_for_openmc_mgxs()
1884-
1885-
# Construct all tallies needed for the multi-group cross section library
1886-
mgxs_lib.build_library()
1887-
1888-
# Create a "tallies.xml" file for the MGXS Library
1889-
mgxs_lib.add_to_tallies(model.tallies, merge=True)
1890-
1891-
# Run
1892-
statepoint_filename = model.run(cwd=directory)
1893-
1894-
# Load MGXS
1895-
with openmc.StatePoint(statepoint_filename) as sp:
1896-
mgxs_lib.load_from_statepoint(sp)
1930+
mgxs_lib = self._auto_generate_mgxs_lib(
1931+
model, groups, correction, directory)
18971932

18981933
# Create a MGXS File which can then be written to disk
18991934
mgxs_set = mgxs_lib.get_xsdata(domain=material, xsdata_name=name)
@@ -2055,48 +2090,8 @@ def _generate_stochastic_slab_mgxs(
20552090
model.settings.output = {'summary': True, 'tallies': False}
20562091

20572092
# Add MGXS Tallies
2058-
2059-
# Initialize MGXS library with a finished OpenMC geometry object
2060-
mgxs_lib = openmc.mgxs.Library(model.geometry)
2061-
2062-
# Pick energy group structure
2063-
mgxs_lib.energy_groups = groups
2064-
2065-
# Disable transport correction
2066-
mgxs_lib.correction = correction
2067-
2068-
# Specify needed cross sections for random ray
2069-
if correction == 'P0':
2070-
mgxs_lib.mgxs_types = ['nu-transport', 'absorption', 'nu-fission', 'fission',
2071-
'consistent nu-scatter matrix', 'multiplicity matrix', 'chi']
2072-
elif correction is None:
2073-
mgxs_lib.mgxs_types = ['total', 'absorption', 'nu-fission', 'fission',
2074-
'consistent nu-scatter matrix', 'multiplicity matrix', 'chi']
2075-
2076-
# Specify a "cell" domain type for the cross section tally filters
2077-
mgxs_lib.domain_type = "material"
2078-
2079-
# Specify the cell domains over which to compute multi-group cross sections
2080-
mgxs_lib.domains = model.geometry.get_all_materials().values()
2081-
2082-
# Do not compute cross sections on a nuclide-by-nuclide basis
2083-
mgxs_lib.by_nuclide = False
2084-
2085-
# Check the library - if no errors are raised, then the library is satisfactory.
2086-
mgxs_lib.check_library_for_openmc_mgxs()
2087-
2088-
# Construct all tallies needed for the multi-group cross section library
2089-
mgxs_lib.build_library()
2090-
2091-
# Create a "tallies.xml" file for the MGXS Library
2092-
mgxs_lib.add_to_tallies(model.tallies, merge=True)
2093-
2094-
# Run
2095-
statepoint_filename = model.run(cwd=directory)
2096-
2097-
# Load MGXS
2098-
with openmc.StatePoint(statepoint_filename) as sp:
2099-
mgxs_lib.load_from_statepoint(sp)
2093+
mgxs_lib = self._auto_generate_mgxs_lib(
2094+
model, groups, correction, directory)
21002095

21012096
names = [mat.name for mat in mgxs_lib.domains]
21022097

@@ -2146,52 +2141,8 @@ def _generate_material_wise_mgxs(
21462141
model.settings.output = {'summary': True, 'tallies': False}
21472142

21482143
# Add MGXS Tallies
2149-
2150-
# Initialize MGXS library with a finished OpenMC geometry object
2151-
mgxs_lib = openmc.mgxs.Library(model.geometry)
2152-
2153-
# Pick energy group structure
2154-
mgxs_lib.energy_groups = groups
2155-
2156-
# Disable transport correction
2157-
mgxs_lib.correction = correction
2158-
2159-
# Specify needed cross sections for random ray
2160-
if correction == 'P0':
2161-
mgxs_lib.mgxs_types = [
2162-
'nu-transport', 'absorption', 'nu-fission', 'fission',
2163-
'consistent nu-scatter matrix', 'multiplicity matrix', 'chi'
2164-
]
2165-
elif correction is None:
2166-
mgxs_lib.mgxs_types = [
2167-
'total', 'absorption', 'nu-fission', 'fission',
2168-
'consistent nu-scatter matrix', 'multiplicity matrix', 'chi'
2169-
]
2170-
2171-
# Specify a "cell" domain type for the cross section tally filters
2172-
mgxs_lib.domain_type = "material"
2173-
2174-
# Specify the cell domains over which to compute multi-group cross sections
2175-
mgxs_lib.domains = model.geometry.get_all_materials().values()
2176-
2177-
# Do not compute cross sections on a nuclide-by-nuclide basis
2178-
mgxs_lib.by_nuclide = False
2179-
2180-
# Check the library - if no errors are raised, then the library is satisfactory.
2181-
mgxs_lib.check_library_for_openmc_mgxs()
2182-
2183-
# Construct all tallies needed for the multi-group cross section library
2184-
mgxs_lib.build_library()
2185-
2186-
# Create a "tallies.xml" file for the MGXS Library
2187-
mgxs_lib.add_to_tallies(model.tallies, merge=True)
2188-
2189-
# Run
2190-
statepoint_filename = model.run(cwd=directory)
2191-
2192-
# Load MGXS
2193-
with openmc.StatePoint(statepoint_filename) as sp:
2194-
mgxs_lib.load_from_statepoint(sp)
2144+
mgxs_lib = self._auto_generate_mgxs_lib(
2145+
model, groups, correction, directory)
21952146

21962147
names = [mat.name for mat in mgxs_lib.domains]
21972148

@@ -2617,5 +2568,3 @@ def function_calls(self) -> int:
26172568
def total_batches(self) -> int:
26182569
"""Total number of active batches used across all evaluations."""
26192570
return sum(self.batches)
2620-
2621-

0 commit comments

Comments
 (0)