Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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 CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ list(APPEND libopenmc_SOURCES
src/ncrystal_interface.cpp
src/ncrystal_load.cpp
src/nuclide.cpp
src/openmc.cpp
src/output.cpp
src/particle.cpp
src/particle_data.cpp
Expand Down
28 changes: 24 additions & 4 deletions docs/source/capi/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ C/C++ API
=========

The libopenmc shared library that is built when installing OpenMC exports a
number of C interoperable functions and global variables that can be used for
in-memory coupling. While it is possible to directly use the C/C++ API as
documented here for coupling, most advanced users will find it easier to work
with the Python bindings in the :py:mod:`openmc.lib` module.
number of C interoperable functions that can be used for in-memory coupling.
While it is possible to directly use the C/C++ API as documented here for
coupling, most advanced users will find it easier to work with the Python
bindings in the :py:mod:`openmc.lib` module.

.. warning:: The C/C++ API is still experimental and may undergo substantial
changes in future releases.
Expand Down Expand Up @@ -329,6 +329,8 @@ Functions
:return: Return status (negative if an error occurs)
:rtype: int

.. doxygenfunction:: openmc_main

.. c:function:: int openmc_material_add_nuclide(int32_t index, const char name[], double density)

Add a nuclide to an existing material. If the nuclide already exists, the
Expand Down Expand Up @@ -865,6 +867,24 @@ Functions
:return: Return status (negative if an error occurred)
:rtype: int

.. doxygenfunction:: openmc_setting_get_bool

.. doxygenfunction:: openmc_setting_get_double

.. doxygenfunction:: openmc_setting_get_int32

.. doxygenfunction:: openmc_setting_get_int64

.. doxygenfunction:: openmc_setting_get_string

.. doxygenfunction:: openmc_setting_set_bool

.. doxygenfunction:: openmc_setting_set_double

.. doxygenfunction:: openmc_setting_set_int32

.. doxygenfunction:: openmc_setting_set_int64

.. c:function:: int openmc_simulation_finalize()

Finalize a simulation.
Expand Down
97 changes: 97 additions & 0 deletions include/openmc/capi.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ void openmc_get_tally_next_id(int32_t* id);
int openmc_global_tallies(double** ptr);
int openmc_hard_reset();
int openmc_init(int argc, char* argv[], const void* intracomm);

//! Run OpenMC as a command-line application.
//!
//! This function initializes OpenMC, executes the requested run mode, and
//! finalizes the library.
//! \param argc Number of command-line arguments (including command)
//! \param argv Command-line arguments
//! \return Exit status
int openmc_main(int argc, char* argv[]);

bool openmc_is_statepoint_batch();
int openmc_legendre_filter_get_order(int32_t index, int* order);
int openmc_legendre_filter_set_order(int32_t index, int order);
Expand Down Expand Up @@ -361,6 +371,93 @@ int openmc_properties_import(const char* filename);
//! \return Error code
int openmc_get_feature_enabled(const char* feature, bool* enabled);

// Simulation state

//! Get the current batch number.
//!
//! \return Current batch number
int openmc_get_current_batch();

//! Get the number of coordinate levels in the geometry.
//!
//! \return Number of coordinate levels
int openmc_get_n_coord_levels();

//! Get the number of realizations in the global tally results.
//!
//! \return Number of realizations
int32_t openmc_get_n_realizations();

//! Determine whether the current process is the master process.
//!
//! \return True if the current process is the master process
bool openmc_master();

// Settings

//! Get a boolean setting.
//!
//! \param name Name of the setting
//! \param value Value of the setting
//! \return Status (negative if an error occurred)
int openmc_setting_get_bool(const char* name, bool* value);

//! Get a double-precision floating-point setting.
//!
//! \param name Name of the setting
//! \param value Value of the setting
//! \return Status (negative if an error occurred)
int openmc_setting_get_double(const char* name, double* value);

//! Get a 32-bit integer setting.
//!
//! \param name Name of the setting
//! \param value Value of the setting
//! \return Status (negative if an error occurred)
int openmc_setting_get_int32(const char* name, int32_t* value);

//! Get a 64-bit integer setting.
//!
//! \param name Name of the setting
//! \param value Value of the setting
//! \return Status (negative if an error occurred)
int openmc_setting_get_int64(const char* name, int64_t* value);

//! Get a string setting.
//!
//! \param name Name of the setting
//! \param value Value of the setting
//! \return Status (negative if an error occurred)
int openmc_setting_get_string(const char* name, const char** value);

//! Set a boolean setting.
//!
//! \param name Name of the setting
//! \param value Value of the setting
//! \return Status (negative if an error occurred)
int openmc_setting_set_bool(const char* name, bool value);

//! Set a double-precision floating-point setting.
//!
//! \param name Name of the setting
//! \param value Value of the setting
//! \return Status (negative if an error occurred)
int openmc_setting_set_double(const char* name, double value);

//! Set a 32-bit integer setting.
//!
//! \param name Name of the setting
//! \param value Value of the setting
//! \return Status (negative if an error occurred)
int openmc_setting_set_int32(const char* name, int32_t value);

//! Set a 64-bit integer setting.
//!
//! \param name Name of the setting
//! \param value Value of the setting
//! \return Status (negative if an error occurred)
int openmc_setting_set_int64(const char* name, int64_t value);

//! Return the message associated with the most recent C API error.
//!
//! The returned pointer is valid until the next error message is set.
Expand Down
4 changes: 2 additions & 2 deletions include/openmc/geometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ struct OverlapKeyHash {

namespace model {

extern int root_universe; //!< Index of root universe
extern "C" int n_coord_levels; //!< Number of CSG coordinate levels
extern int root_universe; //!< Index of root universe
extern int n_coord_levels; //!< Number of CSG coordinate levels

extern vector<int64_t> overlap_check_count;

Expand Down
49 changes: 21 additions & 28 deletions include/openmc/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,23 @@ extern bool confidence_intervals; //!< use confidence intervals for results?
extern bool
create_fission_neutrons; //!< create fission neutrons (fixed source)?
extern bool create_delayed_neutrons; //!< create delayed fission neutrons?
extern "C" bool cmfd_run; //!< is a CMFD run?
extern bool cmfd_run; //!< is a CMFD run?
extern bool
delayed_photon_scaling; //!< Scale fission photon yield to include delayed
extern "C" bool entropy_on; //!< calculate Shannon entropy?
extern "C" bool
event_based; //!< use event-based mode (instead of history-based)
extern bool ifp_on; //!< Use IFP for kinetics parameters?
delayed_photon_scaling; //!< Scale fission photon yield to include delayed
extern bool entropy_on; //!< calculate Shannon entropy?
extern bool event_based; //!< use event-based mode (instead of history-based)
extern bool ifp_on; //!< Use IFP for kinetics parameters?
extern bool legendre_to_tabular; //!< convert Legendre distributions to tabular?
extern bool material_cell_offsets; //!< create material cells offsets?
extern "C" bool output_summary; //!< write summary.h5?
extern bool output_summary; //!< write summary.h5?
extern bool output_tallies; //!< write tallies.out?
extern bool particle_restart_run; //!< particle restart run?
extern "C" bool photon_transport; //!< photon transport turned on?
extern bool photon_transport; //!< photon transport turned on?
extern bool atomic_relaxation; //!< atomic relaxation enabled?
extern "C" bool reduce_tallies; //!< reduce tallies at end of batch?
extern bool reduce_tallies; //!< reduce tallies at end of batch?
extern bool res_scat_on; //!< use resonance upscattering method?
extern "C" bool restart_run; //!< restart run?
extern "C" bool run_CE; //!< run with continuous-energy data?
extern bool restart_run; //!< restart run?
extern bool run_CE; //!< run with continuous-energy data?
extern bool source_latest; //!< write latest source at each batch?
extern bool source_separate; //!< write source to separate file?
extern bool source_write; //!< write source in HDF5 files?
Expand All @@ -92,14 +91,14 @@ extern bool surf_source_read; //!< read surface source file?
extern bool survival_biasing; //!< use survival biasing?
extern bool survival_normalization; //!< use survival normalization?
extern bool temperature_multipole; //!< use multipole data?
extern "C" bool trigger_on; //!< tally triggers enabled?
extern bool trigger_on; //!< tally triggers enabled?
extern bool trigger_predict; //!< predict batches for triggers?
extern bool uniform_source_sampling; //!< sample sources uniformly?
extern bool ufs_on; //!< uniform fission site method on?
extern bool urr_ptables_on; //!< use unresolved resonance prob. tables?
extern bool use_decay_photons; //!< use decay photons for D1S
extern bool use_shared_secondary_bank; //!< Use shared bank for secondaries
extern "C" bool weight_windows_on; //!< are weight windows are enabled?
extern bool weight_windows_on; //!< are weight windows are enabled?
extern bool weight_window_checkpoint_surface; //!< enable weight window check
//!< upon surface crossing?
extern bool weight_window_checkpoint_collision; //!< enable weight window check
Expand All @@ -119,21 +118,15 @@ extern std::string weight_windows_file; //!< Location of weight window file to
extern std::string properties_file; //!< Location of properties file to
//!< load on simulation initialization

// This is required because the c_str() may not be the first thing in
// std::string. Sometimes it is, but it seems libc++ may not be like that
// on some computers, like the intel Mac.
extern "C" const char* path_statepoint_c; //!< C pointer to statepoint file name

extern "C" int32_t n_inactive; //!< number of inactive batches
extern "C" int32_t max_lost_particles; //!< maximum number of lost particles
extern "C" double
extern int32_t n_inactive; //!< number of inactive batches
extern int32_t max_lost_particles; //!< maximum number of lost particles
extern double
rel_max_lost_particles; //!< maximum number of lost particles, relative to the
//!< total number of particles
extern "C" int32_t
max_write_lost_particles; //!< maximum number of lost particles
//!< to be written to files
extern "C" int32_t gen_per_batch; //!< number of generations per batch
extern "C" int64_t n_particles; //!< number of particles per generation
extern int32_t max_write_lost_particles; //!< maximum number of lost particles
//!< to be written to files
extern int32_t gen_per_batch; //!< number of generations per batch
extern int64_t n_particles; //!< number of particles per generation

extern int64_t
max_particles_in_flight; //!< Max num. event-based particles in flight
Expand All @@ -160,7 +153,7 @@ extern double res_scat_energy_min; //!< Min energy in [eV] for res. upscattering
extern double res_scat_energy_max; //!< Max energy in [eV] for res. upscattering
extern vector<std::string>
res_scat_nuclides; //!< Nuclides using res. upscattering treatment
extern "C" RunMode run_mode; //!< Run mode (eigenvalue, fixed src, etc.)
extern RunMode run_mode; //!< Run mode (eigenvalue, fixed src, etc.)
extern SolverType solver_type; //!< Solver Type (Monte Carlo or Random Ray)
extern std::unordered_set<int>
sourcepoint_batch; //!< Batches when source should be written
Expand Down Expand Up @@ -200,7 +193,7 @@ extern int64_t trace_particle; //!< Particle ID to enable trace on
extern vector<array<int, 3>>
track_identifiers; //!< Particle numbers for writing tracks
extern int trigger_batch_interval; //!< Batch interval for triggers
extern "C" int verbosity; //!< How verbose to make output
extern int verbosity; //!< How verbose to make output
extern double weight_cutoff; //!< Weight cutoff for Russian roulette
extern double weight_survive; //!< Survival weight after Russian roulette

Expand Down
36 changes: 17 additions & 19 deletions include/openmc/simulation.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,24 @@ constexpr int STATUS_EXIT_ON_TRIGGER {2};

namespace simulation {

extern int ct_current_file; //!< current collision track file index
extern "C" int current_batch; //!< current batch
extern "C" int current_gen; //!< current fission generation
extern "C" bool initialized; //!< has simulation been initialized?
extern "C" double keff; //!< average k over batches
extern "C" double keff_std; //!< standard deviation of average k
extern "C" double k_col_abs; //!< sum over batches of k_collision * k_absorption
extern "C" double
k_col_tra; //!< sum over batches of k_collision * k_tracklength
extern "C" double
k_abs_tra; //!< sum over batches of k_absorption * k_tracklength
extern int ct_current_file; //!< current collision track file index
extern int current_batch; //!< current batch
extern int current_gen; //!< current fission generation
extern bool initialized; //!< has simulation been initialized?
extern double keff; //!< average k over batches
extern double keff_std; //!< standard deviation of average k
extern double k_col_abs; //!< sum over batches of k_collision * k_absorption
extern double k_col_tra; //!< sum over batches of k_collision * k_tracklength
extern double k_abs_tra; //!< sum over batches of k_absorption * k_tracklength
extern double log_spacing; //!< lethargy spacing for energy grid searches
extern "C" int n_lost_particles; //!< cumulative number of lost particles
extern "C" bool need_depletion_rx; //!< need to calculate depletion rx?
extern "C" int restart_batch; //!< batch at which a restart job resumed
extern "C" bool satisfy_triggers; //!< have tally triggers been satisfied?
extern int ssw_current_file; //!< current surface source file
extern "C" int total_gen; //!< total number of generations simulated
extern double total_weight; //!< Total source weight in a batch
extern int64_t work_per_rank; //!< number of particles per MPI rank
extern int n_lost_particles; //!< cumulative number of lost particles
extern bool need_depletion_rx; //!< need to calculate depletion rx?
extern int restart_batch; //!< batch at which a restart job resumed
extern bool satisfy_triggers; //!< have tally triggers been satisfied?
extern int ssw_current_file; //!< current surface source file
extern int total_gen; //!< total number of generations simulated
extern double total_weight; //!< Total source weight in a batch
extern int64_t work_per_rank; //!< number of particles per MPI rank

extern const RegularMesh* entropy_mesh;
extern const RegularMesh* ufs_mesh;
Expand Down
2 changes: 1 addition & 1 deletion include/openmc/tallies/tally.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ namespace simulation {
extern tensor::StaticTensor2D<double, N_GLOBAL_TALLIES, 3> global_tallies;

//! Number of realizations for global tallies
extern "C" int32_t n_realizations;
extern int32_t n_realizations;
} // namespace simulation

extern double global_tally_absorption;
Expand Down
4 changes: 3 additions & 1 deletion openmc/lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
_dll.openmc_get_feature_enabled.restype = c_int
_dll.openmc_get_feature_enabled.errcheck = _error_handler

_dll.openmc_get_n_coord_levels.restype = c_int

def feature_enabled(feature: str) -> bool:
"""Return whether OpenMC was built with an optional feature.

Expand All @@ -67,7 +69,7 @@ def feature_enabled(feature: str) -> bool:


def _coord_levels():
return c_int.in_dll(_dll, "n_coord_levels").value
return _dll.openmc_get_n_coord_levels()


from .error import *
Expand Down
16 changes: 2 additions & 14 deletions openmc/lib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class _SourceSite(Structure):
_dll.openmc_get_keff.argtypes = [POINTER(c_double*2)]
_dll.openmc_get_keff.restype = c_int
_dll.openmc_get_keff.errcheck = _error_handler
_dll.openmc_get_current_batch.restype = c_int
_dll.openmc_initialize_mesh_egrid.argtypes = [
c_int, _array_1d_int, c_double
]
Expand Down Expand Up @@ -158,7 +159,7 @@ def current_batch():
Current batch of the simulation

"""
return c_int.in_dll(_dll, 'current_batch').value
return _dll.openmc_get_current_batch()


def export_properties(filename=None, output=True):
Expand Down Expand Up @@ -759,19 +760,6 @@ def __exit__(self, exc_type, exc_value, traceback):
self._tmp_dir.cleanup()


class _DLLGlobal:
"""Data descriptor that exposes global variables from libopenmc."""
def __init__(self, ctype, name):
self.ctype = ctype
self.name = name

def __get__(self, instance, owner):
return self.ctype.in_dll(_dll, self.name).value

def __set__(self, instance, value):
self.ctype.in_dll(_dll, self.name).value = value


class _FortranObject:
def __repr__(self):
return f"<{type(self).__name__}(index={self._index})>"
Expand Down
Loading
Loading