Skip to content
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

Exporting and importing physical properties #1852

Merged
merged 24 commits into from
Jul 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
94fc050
Add open_group that works with std::string
paulromano Jun 23, 2021
482848c
Add openmc_properties_export/import to C API (incomplete)
paulromano Jun 23, 2021
ef7b43d
Add material density to properties.h5
paulromano Jun 23, 2021
7a4e32f
Add filetype and other metadata to properties.h5
paulromano Jun 23, 2021
d6f483d
Make sure openmc_properties_export/import appear in global namespace
paulromano Jun 23, 2021
01b023b
Include cell instance in id_map
paulromano Jun 24, 2021
1a1a5a1
Add documentation for properties file format
paulromano Jun 24, 2021
d135cc2
Add consistency checks on number of cells/materials
paulromano Jun 24, 2021
33f7d09
Use names import_properties and export_properties for Python API
paulromano Jun 24, 2021
b63030a
Add Model.import_properties method
paulromano Jun 24, 2021
fd2637c
Add Model.from_xml classmethod
paulromano Jun 24, 2021
87bd987
Make Model available in main openmc namespace
paulromano Jun 24, 2021
8f26aeb
Fix Model docstring
paulromano Jun 24, 2021
51e1b5f
Fix id_map test
paulromano Jun 24, 2021
4449cc4
Make sure properties.h5 gets closed properly upon error
paulromano Jun 24, 2021
329dff2
Add tests for export_properties and import_properties
paulromano Jun 24, 2021
6190ff8
Add test for Model.import_properties
paulromano Jun 24, 2021
803f175
Fix use of get_map<IdData> within plot.cpp and failing properties test
paulromano Jun 24, 2021
876bd92
Add check on num cells/materials for Model.import_properties
paulromano Jul 12, 2021
4484fd6
Add checks during Cell::import_properties_hdf5
paulromano Jul 12, 2021
024b108
Update id_map docstring
paulromano Jul 12, 2021
731f297
Replace lib.Material.density property with get_density method
paulromano Jul 13, 2021
ab80feb
Get rid of try/except block for lib.Material.get_density
paulromano Jul 14, 2021
c9d8431
Merge branch 'develop' into properties-exim
paulromano Jul 14, 2021
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
1 change: 1 addition & 0 deletions docs/source/io_formats/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Output Files
statepoint
source
summary
properties
depletion_results
particle_restart
track
Expand Down
36 changes: 36 additions & 0 deletions docs/source/io_formats/properties.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
.. _io_properties:

======================
Properties File Format
======================

The current version of the properties file format is 1.0.

**/**

:Attributes: - **filetype** (*char[]*) -- String indicating the type of file.
- **version** (*int[2]*) -- Major and minor version of the
statepoint file format.
- **openmc_version** (*int[3]*) -- Major, minor, and release
version number for OpenMC.
- **git_sha1** (*char[40]*) -- Git commit SHA-1 hash.
- **date_and_time** (*char[]*) -- Date and time the summary was
written.
- **path** (*char[]*) -- Path to directory containing input files.

**/geometry/**

:Attributes: - **n_cells** (*int*) -- Number of cells in the problem.

**/geometry/cells/cell <uid>/**

:Datasets: - **temperature** (*double[]*) -- Temperature of the cell in [K].

**/materials/**

:Attributes: - **n_materials** (*int*) -- Number of materials in the problem.

**/materials/material <uid>/**

:Attributes: - **atom_density** (*double*) -- Total density in [atom/b-cm].
- **mass_density** (*double*) -- Total density in [g/cm^3].
27 changes: 19 additions & 8 deletions include/openmc/capi.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ extern "C" {
int openmc_cell_get_id(int32_t index, int32_t* id);
int openmc_cell_get_temperature(int32_t index, const int32_t* instance, double* T);
int openmc_cell_get_name(int32_t index, const char** name);
int openmc_cell_get_num_instances(int32_t index, int32_t* num_instances);
int openmc_cell_set_name(int32_t index, const char* name);
int openmc_cell_set_fill(int32_t index, int type, int32_t n, const int32_t* indices);
int openmc_cell_set_id(int32_t index, int32_t id);
Expand Down Expand Up @@ -144,13 +145,13 @@ extern "C" {
//! \param[in] meshtyally_id id of CMFD Mesh Tally
//! \param[in] cmfd_indices indices storing spatial and energy dimensions of CMFD problem
//! \param[in] norm CMFD normalization factor
extern "C" void openmc_initialize_mesh_egrid(const int meshtally_id, const int* cmfd_indices,
const double norm);
void openmc_initialize_mesh_egrid(const int meshtally_id, const int* cmfd_indices,
const double norm);

//! Sets the mesh and energy grid for CMFD reweight
//! \param[in] feedback whether or not to run CMFD feedback
//! \param[in] cmfd_src computed CMFD source
extern "C" void openmc_cmfd_reweight(const bool feedback, const double* cmfd_src);
void openmc_cmfd_reweight(const bool feedback, const double* cmfd_src);

//! Sets the fixed variables that are used for CMFD linear solver
//! \param[in] indptr CSR format index pointer array of loss matrix
Expand All @@ -161,10 +162,10 @@ extern "C" {
//! \param[in] spectral spectral radius of CMFD matrices and tolerances
//! \param[in] map coremap for problem, storing accelerated regions
//! \param[in] use_all_threads whether to use all threads when running CMFD solver
extern "C" void openmc_initialize_linsolver(const int* indptr, int len_indptr,
const int* indices, int n_elements,
int dim, double spectral,
const int* map, bool use_all_threads);
void openmc_initialize_linsolver(const int* indptr, int len_indptr,
const int* indices, int n_elements,
int dim, double spectral,
const int* map, bool use_all_threads);

//! Runs a Gauss Seidel linear solver to solve CMFD matrix equations
//! linear solver
Expand All @@ -173,9 +174,19 @@ extern "C" {
//! \param[out] x unknown vector
//! \param[in] tol tolerance on final error
//! \return number of inner iterations required to reach convergence
extern "C" int openmc_run_linsolver(const double* A_data, const double* b,
int openmc_run_linsolver(const double* A_data, const double* b,
double* x, double tol);

//! Export physical properties for model
//! \param[in] filename Filename to write to
//! \return Error code
int openmc_properties_export(const char* filename);

//! Import physical properties for model
//! \param[in] filename Filename to read from
// \return Error code
int openmc_properties_import(const char* filename);

// Error codes
extern int OPENMC_E_UNASSIGNED;
extern int OPENMC_E_ALLOCATE;
Expand Down
8 changes: 8 additions & 0 deletions include/openmc/cell.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ class Cell {

virtual void to_hdf5_inner(hid_t group_id) const = 0;

//! Export physical properties to HDF5
//! \param[in] group HDF5 group to read from
void export_properties_hdf5(hid_t group) const;

//! Import physical properties from HDF5
//! \param[in] group HDF5 group to write to
void import_properties_hdf5(hid_t group);

//! Get the BoundingBox for this cell.
virtual BoundingBox bounding_box() const = 0;

Expand Down
1 change: 1 addition & 0 deletions include/openmc/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ constexpr array<int, 2> VERSION_SUMMARY {6, 0};
constexpr array<int, 2> VERSION_VOLUME {1, 0};
constexpr array<int, 2> VERSION_VOXEL {2, 0};
constexpr array<int, 2> VERSION_MGXS_LIBRARY {1, 0};
constexpr array<int, 2> VERSION_PROPERTIES {1, 0};

// ============================================================================
// ADJUSTABLE PARAMETERS
Expand Down
2 changes: 1 addition & 1 deletion include/openmc/hdf5_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ inline hid_t create_group(hid_t parent_id, const std::stringstream& name)


hid_t file_open(const std::string& filename, char mode, bool parallel=false);

hid_t open_group(hid_t group_id, const std::string& name);
void write_string(hid_t group_id, const char* name, const std::string& buffer,
bool indep);

Expand Down
8 changes: 8 additions & 0 deletions include/openmc/material.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ class Material
//! Write material data to HDF5
void to_hdf5(hid_t group) const;

//! Export physical properties to HDF5
//! \param[in] group HDF5 group to write to
void export_properties_hdf5(hid_t group) const;

//! Import physical properties from HDF5
//! \param[in] group HDF5 group to read from
void import_properties_hdf5(hid_t group);

//! Add nuclide to the material
//
//! \param[in] nuclide Name of the nuclide
Expand Down
4 changes: 2 additions & 2 deletions openmc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from openmc.polynomial import *
from . import examples

# Import a few convencience functions that used to be here
from openmc.model import rectangular_prism, hexagonal_prism
# Import a few names from the model module
from openmc.model import rectangular_prism, hexagonal_prism, Model

__version__ = '0.13.0-dev'
18 changes: 18 additions & 0 deletions openmc/lib/cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
c_int32, POINTER(c_int), POINTER(POINTER(c_int32)), POINTER(c_int32)]
_dll.openmc_cell_get_fill.restype = c_int
_dll.openmc_cell_get_fill.errcheck = _error_handler
_dll.openmc_cell_get_num_instances.argtypes = [c_int32, POINTER(c_int32)]
_dll.openmc_cell_get_num_instances.restype = c_int
_dll.openmc_cell_get_num_instances.errcheck = _error_handler
_dll.openmc_cell_get_temperature.argtypes = [
c_int32, POINTER(c_int32), POINTER(c_double)]
_dll.openmc_cell_get_temperature.restype = c_int
Expand Down Expand Up @@ -78,6 +81,14 @@ class Cell(_FortranObjectWithID):
----------
id : int
ID of the cell
fill : openmc.lib.Material or list of openmc.lib.Material
Indicates what the region of space is filled with
name : str
Name of the cell
num_instances : int
Number of unique cell instances
bounding_box : 2-tuple of numpy.ndarray
Lower-left and upper-right coordinates of bounding box

"""
__instances = WeakValueDictionary()
Expand Down Expand Up @@ -159,6 +170,12 @@ def fill(self, fill):
indices = (c_int32*1)(-1)
_dll.openmc_cell_set_fill(self._index, 0, 1, indices)

@property
def num_instances(self):
n = c_int32()
_dll.openmc_cell_get_num_instances(self._index, n)
return n.value

def get_temperature(self, instance=None):
"""Get the temperature of a cell

Expand Down Expand Up @@ -211,6 +228,7 @@ def bounding_box(self):

return llc, urc


class _CellMapping(Mapping):
def __getitem__(self, key):
index = c_int32()
Expand Down
42 changes: 41 additions & 1 deletion openmc/lib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,13 @@ class _SourceSite(Structure):
_dll.openmc_next_batch.restype = c_int
_dll.openmc_next_batch.errcheck = _error_handler
_dll.openmc_plot_geometry.restype = c_int
_dll.openmc_plot_geometry.restype = _error_handler
_dll.openmc_plot_geometry.errcheck = _error_handler
_dll.openmc_properties_export.argtypes = [c_char_p]
_dll.openmc_properties_export.restype = c_int
_dll.openmc_properties_export.errcheck = _error_handler
_dll.openmc_properties_import.argtypes = [c_char_p]
_dll.openmc_properties_import.restype = c_int
_dll.openmc_properties_import.errcheck = _error_handler
_dll.openmc_run.restype = c_int
_dll.openmc_run.errcheck = _error_handler
_dll.openmc_reset.restype = c_int
Expand Down Expand Up @@ -120,6 +126,24 @@ def current_batch():
return c_int.in_dll(_dll, 'current_batch').value


def export_properties(filename=None):
"""Export physical properties.

Parameters
----------
filename : str or None
Filename to export properties to (defaults to "properties.h5")

See Also
--------
openmc.lib.import_properties

"""
if filename is not None:
filename = c_char_p(filename.encode())
_dll.openmc_properties_export(filename)


def finalize():
"""Finalize simulation and free memory"""
_dll.openmc_finalize()
Expand Down Expand Up @@ -178,6 +202,22 @@ def hard_reset():
_dll.openmc_hard_reset()


def import_properties(filename):
"""Import physical properties.

Parameters
----------
filename : str
Filename to import properties from

See Also
--------
openmc.lib.export_properties

"""
_dll.openmc_properties_import(filename.encode())


def init(args=None, intracomm=None):
"""Initialize OpenMC

Expand Down
32 changes: 23 additions & 9 deletions openmc/lib/material.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,6 @@ def nuclides(self):
return self._get_densities()[0]
return nuclides

@property
def density(self):
density = c_double()
try:
_dll.openmc_material_get_density(self._index, density)
except OpenMCError:
return None
return density.value

@property
def densities(self):
return self._get_densities()[1]
Expand Down Expand Up @@ -224,6 +215,29 @@ def add_nuclide(self, name, density):
"""
_dll.openmc_material_add_nuclide(self._index, name.encode(), density)

def get_density(self, units='atom/b-cm'):
"""Get density of a material.

Parameters
----------
units : {'atom/b-cm', 'g/cm3'}
Units for density

Returns
-------
float
Density in requested units

"""
if units == 'atom/b-cm':
return self.densities.sum()
elif units == 'g/cm3':
density = c_double()
_dll.openmc_material_get_density(self._index, density)
return density.value
else:
raise ValueError("Units must be 'atom/b-cm' or 'g/cm3'")

def set_density(self, density, units='atom/b-cm'):
"""Set density of a material.

Expand Down
4 changes: 2 additions & 2 deletions openmc/lib/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,11 @@ def id_map(plot):
Returns
-------
id_map : numpy.ndarray
A NumPy array with shape (vertical pixels, horizontal pixels, 2) of
A NumPy array with shape (vertical pixels, horizontal pixels, 3) of
OpenMC property ids with dtype int32

"""
img_data = np.zeros((plot.v_res, plot.h_res, 2),
img_data = np.zeros((plot.v_res, plot.h_res, 3),
dtype=np.dtype('int32'))
_dll.openmc_id_map(plot, img_data.ctypes.data_as(POINTER(c_int32)))
return img_data
Expand Down
Loading