From 2ffbb6ea2674bd68da47fb5f485c1106e2152b32 Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Mon, 28 Jan 2019 06:47:33 -0600 Subject: [PATCH] Remove nuclide_dict from Fortran side --- openmc/capi/material.py | 2 +- src/input_xml.F90 | 5 +++-- src/material.cpp | 6 +++--- src/nuclide.cpp | 12 +++++++++++- src/nuclide_header.F90 | 17 ++++++++++------- src/photon_header.F90 | 4 ---- src/tallies/tally_derivative_header.F90 | 11 +++++------ src/tallies/tally_header.F90 | 10 ++++++---- 8 files changed, 39 insertions(+), 28 deletions(-) diff --git a/openmc/capi/material.py b/openmc/capi/material.py index 4bf19a1e0..5057c8ff4 100644 --- a/openmc/capi/material.py +++ b/openmc/capi/material.py @@ -163,7 +163,7 @@ class Material(_FortranObjectWithID): _dll.openmc_material_get_densities(self._index, nuclides, densities, n) # Convert to appropriate types and return - nuclide_list = [Nuclide(nuclides[i]).name for i in range(n.value)] + nuclide_list = [Nuclide(nuclides[i] + 1).name for i in range(n.value)] density_array = as_array(densities, (n.value,)) return nuclide_list, density_array diff --git a/src/input_xml.F90 b/src/input_xml.F90 index f2a304064..a0222b8ec 100644 --- a/src/input_xml.F90 +++ b/src/input_xml.F90 @@ -731,14 +731,15 @@ contains word = sarray(j) ! Search through nuclides - if (.not. nuclide_dict % has(word)) then + k = nuclide_map_get(to_c_string(word)) + if (k == -1) then call fatal_error("Could not find the nuclide " & // trim(word) // " specified in tally " & // trim(to_str(t % id)) // " in any material.") end if ! Set bin to index in nuclides array - t % nuclide_bins(j) = nuclide_dict % get(word) + t % nuclide_bins(j) = k end do ! Set number of nuclide bins diff --git a/src/material.cpp b/src/material.cpp index 8c9ac3f3a..3850d2be8 100644 --- a/src/material.cpp +++ b/src/material.cpp @@ -899,7 +899,7 @@ openmc_get_material_index(int32_t id, int32_t* index) set_errmsg("No material exists with ID=" + std::to_string(id) + "."); return OPENMC_E_INVALID_ID; } else { - *index = it->second; + *index = it->second + 1; return 0; } } @@ -1097,8 +1097,8 @@ openmc_material_set_volume(int32_t index, double volume) extern "C" int openmc_extend_materials(int32_t n, int32_t* index_start, int32_t* index_end) { - if (index_start) *index_start = model::materials.size(); - if (index_end) *index_end = model::materials.size() + n - 1; + if (index_start) *index_start = model::materials.size() + 1; + if (index_end) *index_end = model::materials.size() + n; for (int32_t i = 0; i < n; i++) { model::materials.push_back(new Material()); } diff --git a/src/nuclide.cpp b/src/nuclide.cpp index de3d94007..6c689977b 100644 --- a/src/nuclide.cpp +++ b/src/nuclide.cpp @@ -992,7 +992,17 @@ extern "C" bool multipole_in_range(Nuclide* nuc, double E) E <= nuc->multipole_->E_max_; } -extern "C" void nuclides_clear() { data::nuclides.clear(); } +extern "C" void nuclides_clear() +{ + data::nuclides.clear(); + data::nuclide_map.clear(); +} + +extern "C" int nuclide_map_get(const char* name) +{ + auto it = data::nuclide_map.find(name); + return it == data::nuclide_map.end() ? -1 : it->second + 1; +} extern "C" NuclideMicroXS* micro_xs_ptr(); extern "C" ElementMicroXS* micro_photon_xs_ptr(); diff --git a/src/nuclide_header.F90 b/src/nuclide_header.F90 index 8af1dbdc9..74ccd880e 100644 --- a/src/nuclide_header.F90 +++ b/src/nuclide_header.F90 @@ -5,7 +5,7 @@ module nuclide_header use algorithm, only: sort, find, binary_search use constants - use dict_header, only: DictIntInt, DictCharInt + use dict_header, only: DictIntInt use endf, only: reaction_name, is_fission, is_disappearance, & is_inelastic_scatter use endf_header, only: Function1D, Polynomial, Tabulated1D @@ -150,7 +150,6 @@ module nuclide_header ! Nuclear data for each nuclide type(Nuclide), allocatable, target :: nuclides(:) integer(C_INT), bind(C) :: n_nuclides - type(DictCharInt) :: nuclide_dict ! Cross section caches type(NuclideMicroXS), allocatable, target :: micro_xs(:) ! Cache for each nuclide @@ -213,6 +212,12 @@ module nuclide_header real(C_DOUBLE), value :: E real(C_DOUBLE) :: q end function + + function nuclide_map_get(name) result(idx) bind(C) + import C_CHAR, C_INT + character(kind=C_CHAR), intent(in) :: name(*) + integer(C_INT) :: idx + end function end interface contains @@ -650,7 +655,6 @@ contains end if n_nuclides = 0 - call nuclide_dict % clear() call library_clear() end subroutine free_memory_nuclide @@ -670,11 +674,10 @@ contains ! Copy array of C_CHARs to normal Fortran string name_ = to_f_string(name) + err = 0 if (allocated(nuclides)) then - if (nuclide_dict % has(name_)) then - index = nuclide_dict % get(name_) - err = 0 - else + index = nuclide_map_get(name) + if (index == -1) then err = E_DATA call set_errmsg("No nuclide named '" // trim(name_) // & "' has been loaded.") diff --git a/src/photon_header.F90 b/src/photon_header.F90 index d077b72a2..f5d9aad66 100644 --- a/src/photon_header.F90 +++ b/src/photon_header.F90 @@ -3,12 +3,9 @@ module photon_header use, intrinsic :: ISO_C_BINDING use constants - use dict_header, only: DictCharInt integer :: n_elements ! Number of photon cross section tables - type(DictCharInt) :: element_dict - !=============================================================================== ! ELEMENTMICROXS contains cached microscopic photon cross sections for a ! particular element at the current energy @@ -42,7 +39,6 @@ contains ! Deallocate photon cross section data n_elements = 0 - call element_dict % clear() end subroutine free_memory_photon function micro_photon_xs_ptr() result(ptr) bind(C) diff --git a/src/tallies/tally_derivative_header.F90 b/src/tallies/tally_derivative_header.F90 index 304ae5e45..ce506eb06 100644 --- a/src/tallies/tally_derivative_header.F90 +++ b/src/tallies/tally_derivative_header.F90 @@ -1,10 +1,10 @@ module tally_derivative_header use constants - use dict_header, only: DictIntInt, EMPTY + use dict_header, only: DictIntInt use error, only: fatal_error - use nuclide_header, only: nuclide_dict - use string, only: to_str, to_lower + use nuclide_header, only: nuclide_map_get + use string, only: to_str, to_lower, to_c_string use xml_interface implicit none @@ -74,9 +74,8 @@ contains this % variable = DIFF_NUCLIDE_DENSITY call get_node_value(node, "nuclide", word) - word = trim(to_lower(word)) - val = nuclide_dict % get(word) - if (val == EMPTY) then + val = nuclide_map_get(to_c_string(word)) + if (val == -1) then call fatal_error("Could not find the nuclide " & // trim(word) // " specified in derivative " & // trim(to_str(this % id)) // " in any material.") diff --git a/src/tallies/tally_header.F90 b/src/tallies/tally_header.F90 index e103189a2..f4331130e 100644 --- a/src/tallies/tally_header.F90 +++ b/src/tallies/tally_header.F90 @@ -7,10 +7,10 @@ module tally_header use dict_header, only: DictIntInt use hdf5_interface, only: HID_T, HSIZE_T use message_passing, only: n_procs, master - use nuclide_header, only: nuclide_dict + use nuclide_header, only: nuclide_map_get use settings, only: reduce_tallies, run_mode use stl_vector, only: VectorInt - use string, only: to_lower, to_f_string, str_to_int, to_str + use string, only: to_lower, to_f_string, str_to_int, to_str, to_c_string use tally_filter_header, only: TallyFilterContainer, filters, n_filters use tally_filter use trigger_header, only: TriggerObject @@ -825,6 +825,7 @@ contains integer(C_INT) :: err integer :: i + integer :: idx character(C_CHAR), pointer :: string(:) character(len=:, kind=C_CHAR), allocatable :: nuclide_ @@ -844,8 +845,9 @@ contains case ('total') t % nuclide_bins(i) = -1 case default - if (nuclide_dict % has(nuclide_)) then - t % nuclide_bins(i) = nuclide_dict % get(nuclide_) + idx = nuclide_map_get(to_c_string(nuclide_)) + if (idx /= -1) then + t % nuclide_bins(i) = idx else err = E_DATA call set_errmsg("Nuclide '" // trim(to_f_string(string)) // &