From 627ee79f0c6f2dd7977d3fa063f578d27aefebfe Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Wed, 20 Feb 2019 15:45:30 -0600 Subject: [PATCH] Move read_materials_xml and read_plots_xml to C++ --- CMakeLists.txt | 1 - include/openmc/dagmc.h | 10 ++--- include/openmc/material.h | 7 ++++ include/openmc/plot.h | 6 +-- src/api.F90 | 5 +++ src/dagmc.cpp | 9 ++-- src/dagmc_header.F90 | 25 ----------- src/initialize.cpp | 4 +- src/input_xml.F90 | 87 --------------------------------------- src/material.cpp | 31 ++++++++++++-- src/plot.cpp | 43 ++++++++++++------- 11 files changed, 80 insertions(+), 148 deletions(-) delete mode 100644 src/dagmc_header.F90 diff --git a/CMakeLists.txt b/CMakeLists.txt index bbeb774ad..b89b8a982 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -304,7 +304,6 @@ target_compile_options(faddeeva PRIVATE ${cxxflags}) add_library(libopenmc SHARED src/bank_header.F90 src/api.F90 - src/dagmc_header.F90 src/constants.F90 src/dict_header.F90 src/error.F90 diff --git a/include/openmc/dagmc.h b/include/openmc/dagmc.h index 5a944a42d..a2ac4d16f 100644 --- a/include/openmc/dagmc.h +++ b/include/openmc/dagmc.h @@ -15,19 +15,17 @@ extern "C" const bool dagmc_enabled; namespace openmc { namespace model { - -extern moab::DagMC* DAG; - -} // namespace model + extern moab::DagMC* DAG; +} //============================================================================== // Non-member functions //============================================================================== -extern "C" void load_dagmc_geometry(); +void load_dagmc_geometry(); extern "C" void free_memory_dagmc(); void read_geometry_dagmc(); -extern "C" pugi::xml_document* read_uwuw_materials(); +bool read_uwuw_materials(pugi::xml_document& doc); bool get_uwuw_materials_xml(std::string& s); } // namespace openmc diff --git a/include/openmc/material.h b/include/openmc/material.h index 4792e22fc..e033c12bf 100644 --- a/include/openmc/material.h +++ b/include/openmc/material.h @@ -109,6 +109,13 @@ private: // Fortran compatibility //============================================================================== +//! Read material data from materials.xml +void read_materials_xml(); + +//============================================================================== +// Fortran compatibility +//============================================================================== + extern "C" int* material_element(int i_material); extern "C" bool material_isotropic(int i_material, int i_nuc_mat); diff --git a/include/openmc/plot.h b/include/openmc/plot.h index 1096d2484..a6f215cfb 100644 --- a/include/openmc/plot.h +++ b/include/openmc/plot.h @@ -4,6 +4,7 @@ #include #include +#include "pugixml.hpp" #include "xtensor/xarray.hpp" #include "hdf5.h" @@ -160,9 +161,8 @@ void voxel_finalize(hid_t dspace, hid_t dset, hid_t memspace); // External functions //=============================================================================== -//! Read plots from plots.xml node -//! \param[in] plot node of plots.xml -extern "C" void read_plots(pugi::xml_node* plot_node); +//! Read plot specifications from a plots.xml file +void read_plots_xml(); //! Create a ppm image for a plot object //! \param[in] plot object diff --git a/src/api.F90 b/src/api.F90 index eca8d5b28..41a311501 100644 --- a/src/api.F90 +++ b/src/api.F90 @@ -104,6 +104,11 @@ contains subroutine nuclides_clear() bind(C) end subroutine + +#ifdef DAGMC + subroutine free_memory_dagmc() bind(C) + end subroutine +#endif end interface call free_memory_geometry() diff --git a/src/dagmc.cpp b/src/dagmc.cpp index a398c9f75..f417c12f5 100644 --- a/src/dagmc.cpp +++ b/src/dagmc.cpp @@ -65,16 +65,13 @@ bool get_uwuw_materials_xml(std::string& s) { return uwuw_mats_present; } -pugi::xml_document* read_uwuw_materials() { - pugi::xml_document* doc = nullptr; - +bool read_uwuw_materials(pugi::xml_document& doc) { std::string s; bool found_uwuw_mats = get_uwuw_materials_xml(s); if (found_uwuw_mats) { - doc = new pugi::xml_document(); - pugi::xml_parse_result result = doc->load_string(s.c_str()); + pugi::xml_parse_result result = doc.load_string(s.c_str()); } - return doc; + return found_uwuw_mats; } bool write_uwuw_materials_xml() { diff --git a/src/dagmc_header.F90 b/src/dagmc_header.F90 deleted file mode 100644 index 4a97f7f0c..000000000 --- a/src/dagmc_header.F90 +++ /dev/null @@ -1,25 +0,0 @@ -#ifdef DAGMC - -module dagmc_header - - use, intrinsic :: ISO_C_BINDING - - implicit none - - interface - subroutine load_dagmc_geometry() bind(C) - end subroutine load_dagmc_geometry - - subroutine free_memory_dagmc() bind(C) - end subroutine free_memory_dagmc - - function read_uwuw_materials() result(doc) bind(C) - import C_PTR - type(C_PTR) :: doc - end function read_uwuw_materials - - end interface - -end module dagmc_header - -#endif diff --git a/src/initialize.cpp b/src/initialize.cpp index 59662d577..b1caccecb 100644 --- a/src/initialize.cpp +++ b/src/initialize.cpp @@ -17,10 +17,12 @@ #include "openmc/error.h" #include "openmc/geometry_aux.h" #include "openmc/hdf5_interface.h" +#include "openmc/material.h" #include "openmc/message_passing.h" #include "openmc/mgxs_interface.h" #include "openmc/nuclide.h" #include "openmc/output.h" +#include "openmc/plot.h" #include "openmc/random_lcg.h" #include "openmc/settings.h" #include "openmc/simulation.h" @@ -31,8 +33,6 @@ // data/functions from Fortran side extern "C" void read_command_line(); -extern "C" void read_materials_xml(); -extern "C" void read_plots_xml(); extern "C" void read_tallies_xml(); // Paths to various files diff --git a/src/input_xml.F90 b/src/input_xml.F90 index 560d64860..b28380ba3 100644 --- a/src/input_xml.F90 +++ b/src/input_xml.F90 @@ -4,9 +4,6 @@ module input_xml use constants use error, only: fatal_error, warning, write_message, openmc_err_msg -#ifdef DAGMC - use dagmc_header -#endif use material_header use message_passing use settings @@ -23,59 +20,8 @@ module input_xml implicit none save - interface - subroutine read_materials(node_ptr) bind(C) - import C_PTR - type(C_PTR) :: node_ptr - end subroutine read_materials - - subroutine read_plots(node_ptr) bind(C) - import C_PTR - type(C_PTR) :: node_ptr - end subroutine read_plots - end interface - contains - subroutine read_materials_xml() bind(C) - logical :: file_exists ! does materials.xml exist? - character(MAX_LINE_LEN) :: filename ! absolute path to materials.xml - type(XMLDocument) :: doc - type(XMLNode) :: root - - ! Display output message - call write_message("Reading materials XML file...", 5) - - doc % ptr = C_NULL_PTR - -#ifdef DAGMC - if (dagmc) then - doc % ptr = read_uwuw_materials() - end if -#endif - - if (.not. c_associated(doc % ptr)) then - ! Check if materials.xml exists - filename = trim(path_input) // "materials.xml" - inquire(FILE=filename, EXIST=file_exists) - if (.not. file_exists) then - call fatal_error("Material XML file '" // trim(filename) // "' does not & - &exist!") - end if - - ! Parse materials.xml file - call doc % load_file(filename) - - end if - - root = doc % document_element() - call read_materials(root % ptr) - - ! Close materials XML file - call doc % clear() - - end subroutine read_materials_xml - !=============================================================================== ! READ_TALLIES_XML reads data from a tallies.xml file and parses it, checking ! for errors and placing properly-formatted data in the right data structures @@ -522,37 +468,4 @@ contains end subroutine read_tallies_xml -!=============================================================================== -! READ_PLOTS_XML reads data from a plots.xml file -!=============================================================================== - - subroutine read_plots_xml() bind(C) - - logical :: file_exists ! does plots.xml file exist? - character(MAX_LINE_LEN) :: filename ! absolute path to plots.xml - type(XMLDocument) :: doc - type(XMLNode) :: root - - ! Check if plots.xml exists - filename = trim(path_input) // "plots.xml" - inquire(FILE=filename, EXIST=file_exists) - if (.not. file_exists) then - call fatal_error("Plots XML file '" // trim(filename) & - // "' does not exist!") - end if - - ! Display output message - call write_message("Reading plot XML file...", 5) - - ! Parse plots.xml file - call doc % load_file(filename) - root = doc % document_element() - - call read_plots(root % ptr) - - ! Close plots XML file - call doc % clear() - - end subroutine read_plots_xml - end module input_xml diff --git a/src/material.cpp b/src/material.cpp index 41c9d0a01..c436e76a4 100644 --- a/src/material.cpp +++ b/src/material.cpp @@ -13,7 +13,9 @@ #include "openmc/capi.h" #include "openmc/cross_sections.h" #include "openmc/container_util.h" +#include "openmc/dagmc.h" #include "openmc/error.h" +#include "openmc/file_utils.h" #include "openmc/hdf5_interface.h" #include "openmc/math_functions.h" #include "openmc/message_passing.h" @@ -865,11 +867,34 @@ void Material::to_hdf5(hid_t group) const // Non-method functions //============================================================================== -extern "C" void -read_materials(pugi::xml_node* node) +void read_materials_xml() { + write_message("Reading materials XML file...", 5); + + pugi::xml_document doc; + + bool using_dagmc_mats = false; +#ifdef DAGMC + if (settings::dagmc) { + using_dagmc_mats = read_uwuw_materials(doc); + } +#endif + + + if (!using_dagmc_mats) { + // Check if materials.xml exists + std::string filename = settings::path_input + "materials.xml"; + if (!file_exists(filename)) { + fatal_error("Material XML file '" + filename + "' does not exist!"); + } + + // Parse materials.xml file and get root element + doc.load_file(filename.c_str()); + } + // Loop over XML material elements and populate the array. - for (pugi::xml_node material_node : node->children("material")) { + pugi::xml_node root = doc.document_element(); + for (pugi::xml_node material_node : root.children("material")) { model::materials.push_back(new Material(material_node)); } model::materials.shrink_to_fit(); diff --git a/src/plot.cpp b/src/plot.cpp index be217083c..ec486ae27 100644 --- a/src/plot.cpp +++ b/src/plot.cpp @@ -1,22 +1,23 @@ +#include "openmc/plot.h" + #include #include -#include "openmc/plot.h" -#include "openmc/constants.h" -#include "openmc/settings.h" -#include "openmc/error.h" -#include "openmc/particle.h" -#include "openmc/geometry.h" #include "openmc/cell.h" -#include "openmc/material.h" -#include "openmc/message_passing.h" -#include "openmc/string_utils.h" -#include "openmc/mesh.h" -#include "openmc/output.h" +#include "openmc/constants.h" +#include "openmc/file_utils.h" +#include "openmc/geometry.h" +#include "openmc/error.h" #include "openmc/hdf5_interface.h" -#include "openmc/random_lcg.h" +#include "openmc/material.h" +#include "openmc/mesh.h" +#include "openmc/message_passing.h" #include "openmc/output.h" +#include "openmc/particle.h" #include "openmc/progress_bar.h" +#include "openmc/random_lcg.h" +#include "openmc/settings.h" +#include "openmc/string_utils.h" namespace openmc { @@ -65,10 +66,22 @@ int openmc_plot_geometry() } -void -read_plots(pugi::xml_node* plots_node) +void read_plots_xml() { - for (auto node : plots_node->children("plot")) { + // Check if plots.xml exists + std::string filename = settings::path_input + "plots.xml"; + if (!file_exists(filename)) { + fatal_error("Plots XML file '" + filename + "' does not exist!"); + } + + write_message("Reading plot XML file...", 5); + + // Parse plots.xml file + pugi::xml_document doc; + doc.load_file(filename.c_str()); + + pugi::xml_node root = doc.document_element(); + for (auto node : root.children("plot")) { Plot pl(node); model::plots.push_back(pl); model::plot_map[pl.id_] = model::plots.size() - 1;