From e1e4941d8c538eca791bb75c6c538a7cbbb49757 Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Thu, 21 Feb 2019 14:45:59 -0600 Subject: [PATCH] Fix all the bugs introduced --- include/openmc/capi.h | 2 +- include/openmc/hdf5_interface.h | 4 ++-- include/openmc/settings.h | 12 ++++++------ include/openmc/tallies/tally.h | 4 ++-- openmc/capi/filter.py | 12 +++++------- openmc/capi/nuclide.py | 4 ++-- openmc/capi/tally.py | 6 +----- src/hdf5_interface.cpp | 4 +++- src/mgxs_interface.cpp | 6 ++++++ src/nuclide.cpp | 6 ++++++ src/state_point.cpp | 2 +- src/tallies/filter.cpp | 11 +++++++++-- src/tallies/tally.cpp | 9 ++++++++- 13 files changed, 52 insertions(+), 30 deletions(-) diff --git a/include/openmc/capi.h b/include/openmc/capi.h index f66db58423..695ffa6d12 100644 --- a/include/openmc/capi.h +++ b/include/openmc/capi.h @@ -40,7 +40,7 @@ extern "C" { int openmc_extend_meshes(int32_t n, int32_t* index_start, int32_t* index_end); int openmc_extend_tallies(int32_t n, int32_t* index_start, int32_t* index_end); int openmc_filter_get_id(int32_t index, int32_t* id); - int openmc_filter_get_type(int32_t index, const char** type); + int openmc_filter_get_type(int32_t index, char* type); int openmc_filter_set_id(int32_t index, int32_t id); int openmc_finalize(); int openmc_find_cell(const double* xyz, int32_t* index, int32_t* instance); diff --git a/include/openmc/hdf5_interface.h b/include/openmc/hdf5_interface.h index dcf7ab8dde..077d789eb4 100644 --- a/include/openmc/hdf5_interface.h +++ b/include/openmc/hdf5_interface.h @@ -68,7 +68,7 @@ extern "C" { void close_dataset(hid_t dataset_id); void close_group(hid_t group_id); int dataset_ndims(hid_t dset); - size_t dataset_typesize(hid_t dset); + size_t dataset_typesize(hid_t obj_id, const char* name); hid_t file_open(const char* filename, char mode, bool parallel); void file_close(hid_t file_id); void get_name(hid_t obj_id, char* name); @@ -239,7 +239,7 @@ inline void read_dataset(hid_t obj_id, const char* name, std::string& str, bool indep=false) { // Create buffer to read data into - auto n = attribute_typesize(obj_id, name); + auto n = dataset_typesize(obj_id, name); char buffer[n]; // Read attribute and set string diff --git a/include/openmc/settings.h b/include/openmc/settings.h index 918be9c38b..4b76c3dc59 100644 --- a/include/openmc/settings.h +++ b/include/openmc/settings.h @@ -28,7 +28,7 @@ extern bool check_overlaps; //!< check overlaps in geometry? extern bool confidence_intervals; //!< use confidence intervals for results? extern bool create_fission_neutrons; //!< create fission neutrons (fixed source)? extern "C" bool dagmc; //!< indicator of DAGMC geometry -extern bool entropy_on; //!< calculate Shannon entropy? +extern "C" bool entropy_on; //!< calculate Shannon entropy? extern bool legendre_to_tabular; //!< convert Legendre distributions to tabular? extern bool output_summary; //!< write summary.h5? extern bool output_tallies; //!< write tallies.out? @@ -37,7 +37,7 @@ extern "C" bool photon_transport; //!< photon transport turned on? extern "C" bool reduce_tallies; //!< reduce tallies at end of batch? extern bool res_scat_on; //!< use resonance upscattering method? extern bool restart_run; //!< restart run? -extern bool run_CE; //!< run with continuous-energy data? +extern "C" 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? @@ -62,10 +62,10 @@ extern std::string path_statepoint; //!< path to a statepoint file extern int32_t index_entropy_mesh; //!< Index of entropy mesh in global mesh array extern int32_t index_ufs_mesh; //!< Index of UFS mesh in global mesh array -extern int32_t n_batches; //!< number of (inactive+active) batches -extern "C" int32_t n_inactive; //!< number of inactive batches -extern int32_t gen_per_batch; //!< number of generations per batch -extern "C" int64_t n_particles; //!< number of particles per generation +extern "C" int32_t n_batches; //!< number of (inactive+active) batches +extern "C" int32_t n_inactive; //!< number of inactive batches +extern "C" int32_t gen_per_batch; //!< number of generations per batch +extern "C" int64_t n_particles; //!< number of particles per generation extern int electron_treatment; //!< how to treat secondary electrons extern std::array energy_cutoff; //!< Energy cutoff in [eV] for each particle type diff --git a/include/openmc/tallies/tally.h b/include/openmc/tallies/tally.h index ef0eeb5c6e..969c064e75 100644 --- a/include/openmc/tallies/tally.h +++ b/include/openmc/tallies/tally.h @@ -21,7 +21,7 @@ namespace openmc { class Tally { public: - Tally() {} + Tally(); void init_from_xml(pugi::xml_node node); @@ -135,7 +135,7 @@ namespace simulation { extern xt::xtensor_fixed> global_tallies; //! Number of realizations for global tallies - extern int32_t n_realizations; + extern "C" int32_t n_realizations; } // It is possible to protect accumulate operations on global tallies by using an diff --git a/openmc/capi/filter.py b/openmc/capi/filter.py index e75b1a57b6..0f5631519a 100644 --- a/openmc/capi/filter.py +++ b/openmc/capi/filter.py @@ -1,6 +1,6 @@ from collections.abc import Mapping from ctypes import c_int, c_int32, c_double, c_char_p, POINTER, \ - create_string_buffer + create_string_buffer, c_size_t from weakref import WeakValueDictionary import numpy as np @@ -34,13 +34,10 @@ _dll.openmc_energy_filter_get_bins.errcheck = _error_handler _dll.openmc_energy_filter_set_bins.argtypes = [c_int32, c_int32, POINTER(c_double)] _dll.openmc_energy_filter_set_bins.restype = c_int _dll.openmc_energy_filter_set_bins.errcheck = _error_handler -_dll.openmc_extend_filters.argtypes = [c_int32, POINTER(c_int32), POINTER(c_int32)] -_dll.openmc_extend_filters.restype = c_int -_dll.openmc_extend_filters.errcheck = _error_handler _dll.openmc_filter_get_id.argtypes = [c_int32, POINTER(c_int32)] _dll.openmc_filter_get_id.restype = c_int _dll.openmc_filter_get_id.errcheck = _error_handler -_dll.openmc_filter_get_type.argtypes = [c_int32, POINTER(c_char_p)] +_dll.openmc_filter_get_type.argtypes = [c_int32, c_char_p] _dll.openmc_filter_get_type.restype = c_int _dll.openmc_filter_get_type.errcheck = _error_handler _dll.openmc_filter_set_id.argtypes = [c_int32, c_int32] @@ -74,7 +71,7 @@ _dll.openmc_meshsurface_filter_get_mesh.errcheck = _error_handler _dll.openmc_meshsurface_filter_set_mesh.argtypes = [c_int32, c_int32] _dll.openmc_meshsurface_filter_set_mesh.restype = c_int _dll.openmc_meshsurface_filter_set_mesh.errcheck = _error_handler -_dll.openmc_new_filter.argtypes = [c_char_p, POINTER(c_int32_)] +_dll.openmc_new_filter.argtypes = [c_char_p, POINTER(c_int32)] _dll.openmc_new_filter.restype = c_int _dll.openmc_new_filter.errcheck = _error_handler _dll.openmc_spatial_legendre_filter_get_order.argtypes = [c_int32, POINTER(c_int)] @@ -95,6 +92,7 @@ _dll.openmc_zernike_filter_get_order.errcheck = _error_handler _dll.openmc_zernike_filter_set_order.argtypes = [c_int32, c_int] _dll.openmc_zernike_filter_set_order.restype = c_int _dll.openmc_zernike_filter_set_order.errcheck = _error_handler +_dll.tally_filters_size.restype = c_size_t class Filter(_FortranObjectWithID): __instances = WeakValueDictionary() @@ -410,7 +408,7 @@ class _FilterMapping(Mapping): yield _get_filter(i + 1).id def __len__(self): - return c_int32.in_dll(_dll, 'n_filters').value + return _dll.tally_filters_size() def __repr__(self): return repr(dict(self)) diff --git a/openmc/capi/nuclide.py b/openmc/capi/nuclide.py index 824f27e323..4a21a6d2b4 100644 --- a/openmc/capi/nuclide.py +++ b/openmc/capi/nuclide.py @@ -1,5 +1,5 @@ from collections.abc import Mapping -from ctypes import c_int, c_char_p, POINTER +from ctypes import c_int, c_char_p, POINTER, c_size_t from weakref import WeakValueDictionary import numpy as np @@ -23,7 +23,7 @@ _dll.openmc_load_nuclide.errcheck = _error_handler _dll.openmc_nuclide_name.argtypes = [c_int, POINTER(c_char_p)] _dll.openmc_nuclide_name.restype = c_int _dll.openmc_nuclide_name.errcheck = _error_handler -_dll.nuclides_size.restype = c_int +_dll.nuclides_size.restype = c_size_t def load_nuclide(name): diff --git a/openmc/capi/tally.py b/openmc/capi/tally.py index 19656fa478..0102436e08 100644 --- a/openmc/capi/tally.py +++ b/openmc/capi/tally.py @@ -26,13 +26,10 @@ _dll.openmc_get_tally_index.errcheck = _error_handler _dll.openmc_global_tallies.argtypes = [POINTER(POINTER(c_double))] _dll.openmc_global_tallies.restype = c_int _dll.openmc_global_tallies.errcheck = _error_handler -_dll.openmc_tally_allocate.argtypes = [c_int32, c_char_p] -_dll.openmc_tally_allocate.restype = c_int -_dll.openmc_tally_allocate.errcheck = _error_handler _dll.openmc_tally_get_active.argtypes = [c_int32, POINTER(c_bool)] _dll.openmc_tally_get_active.restype = c_int _dll.openmc_tally_get_active.errcheck = _error_handler -_dll.openmc_tally_get_estimator.argtypes = [c_int32, POINTER(int32)] +_dll.openmc_tally_get_estimator.argtypes = [c_int32, POINTER(c_int)] _dll.openmc_tally_get_estimator.restype = c_int _dll.openmc_tally_get_estimator.errcheck = _error_handler _dll.openmc_tally_get_id.argtypes = [c_int32, POINTER(c_int32)] @@ -196,7 +193,6 @@ class Tally(_FortranObjectWithID): index = c_int32() _dll.openmc_extend_tallies(1, index, None) - _dll.openmc_tally_allocate(index, b'generic') index = index.value else: index = mapping[uid]._index diff --git a/src/hdf5_interface.cpp b/src/hdf5_interface.cpp index 980a8688f2..d85a4cc9e9 100644 --- a/src/hdf5_interface.cpp +++ b/src/hdf5_interface.cpp @@ -138,11 +138,13 @@ dataset_ndims(hid_t dset) size_t -dataset_typesize(hid_t dset) +dataset_typesize(hid_t obj_id, const char* name) { + hid_t dset = open_dataset(obj_id, name); hid_t filetype = H5Dget_type(dset); size_t n = H5Tget_size(filetype); H5Tclose(filetype); + close_dataset(dset); return n; } diff --git a/src/mgxs_interface.cpp b/src/mgxs_interface.cpp index 70a5ef6fe3..402799c509 100644 --- a/src/mgxs_interface.cpp +++ b/src/mgxs_interface.cpp @@ -202,6 +202,12 @@ void read_mg_cross_sections_header() ensure_exists(file_id, "energy_groups", true); read_attribute(file_id, "energy_groups", data::num_energy_groups); + if (attribute_exists(file_id, "delayed_groups")) { + read_attribute(file_id, "delayed_groups", data::num_delayed_groups); + } else { + data::num_delayed_groups = 0; + } + ensure_exists(file_id, "group structure", true); read_attribute(file_id, "group structure", data::rev_energy_bins); diff --git a/src/nuclide.cpp b/src/nuclide.cpp index 0198550a16..b58ca2c493 100644 --- a/src/nuclide.cpp +++ b/src/nuclide.cpp @@ -888,6 +888,12 @@ void check_data_version(hid_t file_id) } } +extern "C" size_t +nuclides_size() +{ + return data::nuclides.size(); +} + //============================================================================== // C API //============================================================================== diff --git a/src/state_point.cpp b/src/state_point.cpp index b45e4e1db9..2a3f9d2bbc 100644 --- a/src/state_point.cpp +++ b/src/state_point.cpp @@ -415,7 +415,7 @@ void load_state_point() #endif // Read global tally data read_dataset(file_id, "global_tallies", H5T_NATIVE_DOUBLE, - simulation::global_tallies.data()); + simulation::global_tallies.data(), false); // Check if tally results are present bool present; diff --git a/src/tallies/filter.cpp b/src/tallies/filter.cpp index f003fdc4d7..629f38714b 100644 --- a/src/tallies/filter.cpp +++ b/src/tallies/filter.cpp @@ -1,6 +1,7 @@ #include "openmc/tallies/filter.h" #include // for max +#include // for strcpy #include #include "openmc/capi.h" @@ -102,6 +103,11 @@ allocate_filter(const std::string& type) return model::tally_filters.back().get(); } +extern "C" size_t tally_filters_size() +{ + return model::tally_filters.size(); +} + //============================================================================== // C API functions //============================================================================== @@ -144,13 +150,14 @@ openmc_filter_set_id(int32_t index, int32_t id) } extern "C" int -openmc_filter_get_type(int32_t index, const char** type) +openmc_filter_get_type(int32_t index, char* type) { int err = verify_filter(index); if (err) return err; // TODO: off-by-one - *type = model::tally_filters[index-1]->type().c_str(); + std::strcpy(type, model::tally_filters[index-1]->type().c_str()); + return 0; } extern "C" int diff --git a/src/tallies/tally.cpp b/src/tallies/tally.cpp index 2b7a300f9d..dbd7ddd476 100644 --- a/src/tallies/tally.cpp +++ b/src/tallies/tally.cpp @@ -224,6 +224,11 @@ score_str_to_int(std::string score_str) // Tally object implementation //============================================================================== +Tally::Tally() +{ + this->set_filters(nullptr, 0); +} + void Tally::init_from_xml(pugi::xml_node node) { @@ -1062,7 +1067,8 @@ openmc_get_tally_index(int32_t id, int32_t* index) return OPENMC_E_INVALID_ID; } - *index = it->second; + // TODO: off-by-one + *index = it->second + 1; return 0; } @@ -1143,6 +1149,7 @@ openmc_tally_set_id(int32_t index, int32_t id) // TODO: off-by-one model::tallies[index-1]->id_ = id; + model::tally_map[id] = index - 1; return 0; }