Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
2555b95
Factor out to_xml_element methods for XML classes
pshriwise Nov 3, 2022
ad746cb
Writing all main nodes to a single XML file
pshriwise Nov 4, 2022
a09a61e
Writing all output under a single root node
pshriwise Nov 4, 2022
7a9d8c9
Keeping old option to write separate XMLs so I can still use to tests…
pshriwise Nov 4, 2022
0409652
Adding signatures for reading information from an XML node where nece…
pshriwise Nov 4, 2022
cdd2a6b
Correcting doc scope and some reads
pshriwise Nov 4, 2022
bec9bf6
Refactoring model read into it's own function.
pshriwise Nov 4, 2022
81aa653
Factoring out some common lines between reader functions
pshriwise Nov 4, 2022
9391f17
Adding import for single or multiple XMLs
pshriwise Nov 4, 2022
af6b0af
Fix loop variable overwrite
pshriwise Nov 4, 2022
5ac6e87
Properly call read_plots_xml
pshriwise Nov 4, 2022
2e71658
Other bug fixes
pshriwise Nov 4, 2022
b4dcc30
Removing walrus operator
pshriwise Nov 4, 2022
611475a
Restructureing input function calls a little
pshriwise Nov 5, 2022
021bb28
Adding initial test for exporting model.xmls. Correcting material maps
pshriwise Nov 5, 2022
abd3d51
Correcting doc strings for material mapping sent to
pshriwise Nov 5, 2022
662d83a
Running some existing regression tests using single XML file
pshriwise Nov 10, 2022
1e7dbe9
Making sure meshes are only written once to a model.xml file
pshriwise Nov 10, 2022
d034e1d
Apply suggestions from code review
pshriwise Nov 24, 2022
b76825f
Addressing some comments from PR
pshriwise Nov 24, 2022
d984716
Adding ability to specify an input filename to the executable or to t…
pshriwise Nov 24, 2022
366f9e9
Test comment and updated error messages
pshriwise Nov 24, 2022
1e6cb68
Addressing a few more comments in C++ code
pshriwise Nov 24, 2022
7a203fb
Correcting parenthesis
pshriwise Nov 24, 2022
fe47d56
Spot check in error message from failed run
pshriwise Nov 24, 2022
6f7a6fe
Updating model XML file format w/ indentation.
pshriwise Nov 25, 2022
5d1ead2
Addressing some more PR comments. A couple of typo fixes too.
pshriwise Nov 26, 2022
e7f95b0
Docstring correction and a note about the recursion
pshriwise Nov 28, 2022
b3225be
Adding executor tests
pshriwise Nov 29, 2022
72ff942
Checking RuntimeError message correctly
pshriwise Nov 29, 2022
7f5d2a5
Refactoring model_xml regression tests a bit and adding input checking
pshriwise Nov 29, 2022
3f4a9eb
Reordering geometry XML element attributes
pshriwise Nov 29, 2022
83cf684
Updating expected inputs with reordering
pshriwise Nov 29, 2022
2c27d10
Moving other relevant XML reorder calls and updating expected input a…
pshriwise Nov 29, 2022
cd7c3bf
Updates to import/export methods suggested by @paulromano
pshriwise Dec 6, 2022
ef2f690
Updating handling of single XML input to leverage existing settings::…
pshriwise Dec 6, 2022
574845b
Moving Model.from_separate_xmls back to Model.from_xml.
pshriwise Dec 6, 2022
270331a
Adding function to check if a path is a directory
pshriwise Dec 6, 2022
bc1791d
Updates to simplify model filename checking
pshriwise Dec 6, 2022
5634d83
Changing name of input argument for Python executor
pshriwise Dec 6, 2022
d639d1d
Docstring correction
pshriwise Dec 6, 2022
939adbe
Updating tests to reflect changes to API
pshriwise Dec 6, 2022
7fdfed2
Some cleanup of file utils
pshriwise Dec 7, 2022
55d77cf
Small improvements to read_model_xml
pshriwise Dec 7, 2022
7873b4d
Update tests/regression_tests/model_xml/test.py
pshriwise Dec 7, 2022
ef13643
Some housekeeping in the Python classes
pshriwise Dec 7, 2022
9c942db
Correct use of mesh memo in single XML export
pshriwise Dec 7, 2022
916863f
Renaming and docstring/comment updates
pshriwise Dec 7, 2022
145594e
Apply @paulromano suggestions from code review
pshriwise Dec 13, 2022
f3bcad3
Addressing comments from @paulromano
pshriwise Dec 13, 2022
43687e5
Improve comment on recursion arguments
pshriwise Dec 13, 2022
2eca57a
Adding mesh memo to Settings.from_xml_element
pshriwise Dec 14, 2022
25f2bd5
Several small fixes
paulromano Dec 23, 2022
64aee3b
Merge pull request #16 from paulromano/model-xml-pr
pshriwise Dec 23, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions include/openmc/cross_sections.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ extern vector<Library> libraries;
//! libraries
void read_cross_sections_xml();

//! Read cross sections file (either XML or multigroup H5) and populate data
//! libraries
//! \param[in] root node of the cross_sections.xml
void read_cross_sections_xml(pugi::xml_node root);

//! Load nuclide and thermal scattering data from HDF5 files
//
//! \param[in] nuc_temps Temperatures for each nuclide in [K]
Expand Down
17 changes: 17 additions & 0 deletions include/openmc/file_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,31 @@

#include <fstream> // for ifstream
#include <string>
#include <sys/stat.h>

namespace openmc {

// TODO: replace with std::filesystem when switch to C++17 is made
//! Determine if a path is a directory
//! \param[in] path Path to check
//! \return Whether the path is a directory
inline bool dir_exists(const std::string& path)
{
struct stat s;
if (stat(path.c_str(), &s) != 0) return false;

return s.st_mode & S_IFDIR;
}

//! Determine if a file exists
//! \param[in] filename Path to file
//! \return Whether file exists
inline bool file_exists(const std::string& filename)
{
// rule out file being a path to a directory
if (dir_exists(filename))
return false;

std::ifstream s {filename};
return s.good();
}
Expand Down
6 changes: 6 additions & 0 deletions include/openmc/geometry_aux.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <vector>

#include "openmc/vector.h"
#include "openmc/xml_interface.h"

namespace openmc {

Expand All @@ -19,8 +20,13 @@ extern std::unordered_map<int32_t, std::unordered_map<int32_t, int32_t>>
extern std::unordered_map<int32_t, int32_t> universe_level_counts;
} // namespace model

//! Read geometry from XML file
void read_geometry_xml();

//! Read geometry from XML node
//! \param[in] root node of geometry XML element
void read_geometry_xml(pugi::xml_node root);

//==============================================================================
//! Replace Universe, Lattice, and Material IDs with indices.
//==============================================================================
Expand Down
10 changes: 9 additions & 1 deletion include/openmc/initialize.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef OPENMC_INITIALIZE_H
#define OPENMC_INITIALIZE_H

#include <string>

#ifdef OPENMC_MPI
#include "mpi.h"
#endif
Expand All @@ -11,7 +13,13 @@ int parse_command_line(int argc, char* argv[]);
#ifdef OPENMC_MPI
void initialize_mpi(MPI_Comm intracomm);
#endif
void read_input_xml();

//! Read material, geometry, settings, and tallies from a single XML file
bool read_model_xml();
//! Read inputs from separate XML files
void read_separate_xml_files();
//! Write some output that occurs right after initialization
void initial_output();

} // namespace openmc

Expand Down
4 changes: 4 additions & 0 deletions include/openmc/material.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ double density_effect(const vector<double>& f, const vector<double>& e_b_sq,
//! Read material data from materials.xml
void read_materials_xml();

//! Read material data XML node
//! \param[in] root node of materials XML element
void read_materials_xml(pugi::xml_node root);

void free_memory_material();

} // namespace openmc
Expand Down
4 changes: 4 additions & 0 deletions include/openmc/plot.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,10 @@ void voxel_finalize(hid_t dspace, hid_t dset, hid_t memspace);
//! Read plot specifications from a plots.xml file
void read_plots_xml();

//! Read plot specifications from an XML Node
//! \param[in] XML node containing plot info
void read_plots_xml(pugi::xml_node root);

//! Clear memory
void free_memory_plot();

Expand Down
5 changes: 4 additions & 1 deletion include/openmc/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,12 @@ extern double weight_survive; //!< Survival weight after Russian roulette
//==============================================================================

//! Read settings from XML file
//! \param[in] root XML node for <settings>
void read_settings_xml();

//! Read settings from XML node
//! \param[in] root XML node for <settings>
void read_settings_xml(pugi::xml_node root);

void free_memory_settings();

} // namespace openmc
Expand Down
8 changes: 6 additions & 2 deletions include/openmc/tallies/tally.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ class Tally {
void set_nuclides(const vector<std::string>& nuclides);

//! returns vector of indices corresponding to the tally this is called on
const vector<int32_t>& filters() const { return filters_; }
const vector<int32_t>& filters() const { return filters_; }

//! \brief Returns the tally filter at index i
int32_t filters(int i) const { return filters_[i]; }
int32_t filters(int i) const { return filters_[i]; }

void set_filters(gsl::span<Filter*> filters);

Expand Down Expand Up @@ -178,6 +178,10 @@ extern double global_tally_leakage;
//! Read tally specification from tallies.xml
void read_tallies_xml();

//! Read tally specification from an XML node
//! \param[in] root node of tallies XML element
void read_tallies_xml(pugi::xml_node root);

//! \brief Accumulate the sum of the contributions from each history within the
//! batch to a new random variable
void accumulate_tallies();
Expand Down
32 changes: 25 additions & 7 deletions openmc/_xml.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,40 @@
def clean_indentation(element, level=0, spaces_per_level=2):
"""
copy and paste from https://effbot.org/zone/element-lib.htm#prettyprint
it basically walks your tree and adds spaces and newlines so the tree is
printed in a nice way
def clean_indentation(element, level=0, spaces_per_level=2, trailing_indent=True):
"""Set indentation of XML element and its sub-elements.
Copied and pasted from https://effbot.org/zone/element-lib.htm#prettyprint.
It walks your tree and adds spaces and newlines so the tree is
printed in a nice way.

Parameters
----------
level : int
Indentation level for the element passed in (default 0)
spaces_per_level : int
Number of spaces per indentation level (default 2)
trailing_indent : bool
Whether or not to add indentation after closing the element

"""
i = "\n" + level*spaces_per_level*" "

# ensure there's always some tail for the element passed in
if not element.tail:
element.tail = ""

if len(element):
if not element.text or not element.text.strip():
element.text = i + spaces_per_level*" "
if not element.tail or not element.tail.strip():
if trailing_indent and (not element.tail or not element.tail.strip()):
element.tail = i
for sub_element in element:
# `trailing_indent` is intentionally not forwarded to the recursive
# call. Any child element of the topmost element should add
# indentation at the end to ensure its parent's indentation is
# correct.
clean_indentation(sub_element, level+1, spaces_per_level)
if not sub_element.tail or not sub_element.tail.strip():
sub_element.tail = i
else:
if level and (not element.tail or not element.tail.strip()):
if trailing_indent and level and (not element.tail or not element.tail.strip()):
element.tail = i


Expand Down
2 changes: 1 addition & 1 deletion openmc/cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ def from_xml_element(cls, elem, surfaces, materials, get_universe):
surfaces : dict
Dictionary mapping surface IDs to :class:`openmc.Surface` instances
materials : dict
Dictionary mapping material IDs to :class:`openmc.Material`
Dictionary mapping material ID strings to :class:`openmc.Material`
instances (defined in :math:`openmc.Geometry.from_xml`)
get_universe : function
Function returning universe (defined in
Expand Down
59 changes: 43 additions & 16 deletions openmc/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
def _process_CLI_arguments(volume=False, geometry_debug=False, particles=None,
plot=False, restart_file=None, threads=None,
tracks=False, event_based=None,
openmc_exec='openmc', mpi_args=None):
openmc_exec='openmc', mpi_args=None, path_input=None):
"""Converts user-readable flags in to command-line arguments to be run with
the OpenMC executable via subprocess.

Expand Down Expand Up @@ -42,6 +42,9 @@ def _process_CLI_arguments(volume=False, geometry_debug=False, particles=None,
mpi_args : list of str, optional
MPI execute command and any additional MPI arguments to pass,
e.g. ['mpiexec', '-n', '8'].
path_input : str or Pathlike
Path to a single XML file or a directory containing XML files for the
OpenMC executable to read.

.. versionadded:: 0.13.0

Expand Down Expand Up @@ -82,6 +85,9 @@ def _process_CLI_arguments(volume=False, geometry_debug=False, particles=None,
if mpi_args is not None:
args = mpi_args + args

if path_input is not None:
args += [path_input]

return args


Expand Down Expand Up @@ -118,7 +124,7 @@ def _run(args, output, cwd):
raise RuntimeError(error_msg)


def plot_geometry(output=True, openmc_exec='openmc', cwd='.'):
def plot_geometry(output=True, openmc_exec='openmc', cwd='.', path_input=None):
"""Run OpenMC in plotting mode

Parameters
Expand All @@ -129,17 +135,23 @@ def plot_geometry(output=True, openmc_exec='openmc', cwd='.'):
Path to OpenMC executable
cwd : str, optional
Path to working directory to run in
path_input : str
Path to a single XML file or a directory containing XML files for the
OpenMC executable to read.

Raises
------
RuntimeError
If the `openmc` executable returns a non-zero status

"""
_run([openmc_exec, '-p'], output, cwd)
args = [openmc_exec, '-p']
if path_input is not None:
args += [path_input]
_run(args, output, cwd)


def plot_inline(plots, openmc_exec='openmc', cwd='.'):
def plot_inline(plots, openmc_exec='openmc', cwd='.', path_input=None):
"""Display plots inline in a Jupyter notebook.

.. versionchanged:: 0.13.0
Expand All @@ -155,6 +167,9 @@ def plot_inline(plots, openmc_exec='openmc', cwd='.'):
Path to OpenMC executable
cwd : str, optional
Path to working directory to run in
path_input : str
Path to a single XML file or a directory containing XML files for the
OpenMC executable to read.

Raises
------
Expand All @@ -171,15 +186,16 @@ def plot_inline(plots, openmc_exec='openmc', cwd='.'):
openmc.Plots(plots).export_to_xml(cwd)

# Run OpenMC in geometry plotting mode
plot_geometry(False, openmc_exec, cwd)
plot_geometry(False, openmc_exec, cwd, path_input)

if plots is not None:
images = [_get_plot_image(p, cwd) for p in plots]
display(*images)


def calculate_volumes(threads=None, output=True, cwd='.',
openmc_exec='openmc', mpi_args=None):
openmc_exec='openmc', mpi_args=None,
path_input=None):
"""Run stochastic volume calculations in OpenMC.

This function runs OpenMC in stochastic volume calculation mode. To specify
Expand Down Expand Up @@ -210,6 +226,10 @@ def calculate_volumes(threads=None, output=True, cwd='.',
cwd : str, optional
Path to working directory to run in. Defaults to the current working
directory.
path_input : str or Pathlike
Path to a single XML file or a directory containing XML files for the
OpenMC executable to read.


Raises
------
Expand All @@ -223,14 +243,16 @@ def calculate_volumes(threads=None, output=True, cwd='.',
"""

args = _process_CLI_arguments(volume=True, threads=threads,
openmc_exec=openmc_exec, mpi_args=mpi_args)
openmc_exec=openmc_exec, mpi_args=mpi_args,
path_input=path_input)

_run(args, output, cwd)


def run(particles=None, threads=None, geometry_debug=False,
restart_file=None, tracks=False, output=True, cwd='.',
openmc_exec='openmc', mpi_args=None, event_based=False):
openmc_exec='openmc', mpi_args=None, event_based=False,
path_input=None):
"""Run an OpenMC simulation.

Parameters
Expand All @@ -239,17 +261,17 @@ def run(particles=None, threads=None, geometry_debug=False,
Number of particles to simulate per generation.
threads : int, optional
Number of OpenMP threads. If OpenMC is compiled with OpenMP threading
enabled, the default is implementation-dependent but is usually equal
to the number of hardware threads available (or a value set by the
enabled, the default is implementation-dependent but is usually equal to
the number of hardware threads available (or a value set by the
:envvar:`OMP_NUM_THREADS` environment variable).
geometry_debug : bool, optional
Turn on geometry debugging during simulation. Defaults to False.
restart_file : str, optional
Path to restart file to use
tracks : bool, optional
Enables the writing of particles tracks. The number of particle
tracks written to tracks.h5 is limited to 1000 unless
Settings.max_tracks is set. Defaults to False.
Enables the writing of particles tracks. The number of particle tracks
written to tracks.h5 is limited to 1000 unless Settings.max_tracks is
set. Defaults to False.
output : bool
Capture OpenMC output from standard out
cwd : str, optional
Expand All @@ -258,13 +280,17 @@ def run(particles=None, threads=None, geometry_debug=False,
openmc_exec : str, optional
Path to OpenMC executable. Defaults to 'openmc'.
mpi_args : list of str, optional
MPI execute command and any additional MPI arguments to pass,
e.g. ['mpiexec', '-n', '8'].
MPI execute command and any additional MPI arguments to pass, e.g.
['mpiexec', '-n', '8'].
event_based : bool, optional
Turns on event-based parallelism, instead of default history-based

.. versionadded:: 0.12

path_input : str or Pathlike
Path to a single XML file or a directory containing XML files for the
OpenMC executable to read.

Raises
------
RuntimeError
Expand All @@ -275,6 +301,7 @@ def run(particles=None, threads=None, geometry_debug=False,
args = _process_CLI_arguments(
volume=False, geometry_debug=geometry_debug, particles=particles,
restart_file=restart_file, threads=threads, tracks=tracks,
event_based=event_based, openmc_exec=openmc_exec, mpi_args=mpi_args)
event_based=event_based, openmc_exec=openmc_exec, mpi_args=mpi_args,
path_input=path_input)

_run(args, output, cwd)
Loading