diff --git a/src/dfpm/io/configuration.cpp b/src/dfpm/io/configuration.cpp index 1853573..ad4e5c2 100644 --- a/src/dfpm/io/configuration.cpp +++ b/src/dfpm/io/configuration.cpp @@ -1,127 +1,212 @@ /* ============================================================================= Copyright (c) 2014 - 2016 F. Georget Princeton University All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ============================================================================= */ #include "configuration.hpp" -#include "../../utils/io/yaml.hpp" +#include "utils/io/yaml.hpp" +#include "utils/io/safe_config.hpp" + + +#include "dfpm/meshes/generic_mesh1d.hpp" +#include "dfpm/meshes/axisymmetric_mesh1d.hpp" + -#include "../meshes/generic_mesh1d.hpp" -#include "../meshes/axisymmetric_mesh1d.hpp" #include #define S_MESH "mesh" #define S_MESH_A_TYPE "type" #define S_UNIFMESH "uniform_mesh" #define S_UNIFMESH_A_DX "dx" #define S_UNIFMESH_A_NBNODES "nb_nodes" #define S_UNIFMESH_A_SECTION "section" #define S_RAMPMESH "ramp_mesh" #define S_RAMPMESH_A_MIN_DX "min_dx" #define S_RAMPMESH_A_MAX_DX "max_dx" #define S_RAMPMESH_A_RAMP_LENGTH "length_ramp" #define S_RAMPMESH_A_PLATEAU_LENGTH "length_plateau" #define S_RAMPMESH_A_SECTION "section" #define S_UNIFAXISYMESH "uniform_axisymmetric" #define S_UNIFAXISYMESH_A_RADIUS "radius" #define S_UNIFAXISYMESH_A_NBNODES "nb_nodes" #define S_UNIFAXISYMESH_A_HEIGHT "height" #define S_UNIFAXISYMESH_A_DX "dx" namespace specmicp { namespace io { +// new interface +// ------------- + +//! \brief Configure the mesh +mesh::Mesh1DPtr SPECMICP_DLL_PUBLIC +configure_mesh(YAMLConfigHandle&& mesh_section) +{ + auto type = mesh_section.get_required_attribute(S_MESH_A_TYPE); + + mesh::Mesh1DPtr the_mesh {nullptr}; + if (type == S_UNIFMESH) + the_mesh = configure_uniform_mesh1D( + mesh_section.get_section(S_UNIFMESH)); + else if (type == S_RAMPMESH) + the_mesh = configure_ramp_mesh1D( + mesh_section.get_section(S_RAMPMESH)); + else if (type == S_UNIFAXISYMESH) + the_mesh = configure_uniform_axisymmetric_mesh1D( + mesh_section.get_section(S_UNIFAXISYMESH)); + else + mesh_section.report_error(YAMLConfigError::InvalidArgument, + "'"+type+"' is not a valid type of mesh."); + return the_mesh; +} + +//! \brief Configure an uniform mesh +mesh::Mesh1DPtr +configure_uniform_mesh1D(YAMLConfigHandle&& uniform_section) +{ + mesh::Uniform1DMeshGeometry geometry; + geometry.dx = uniform_section.get_required_attribute( + S_UNIFMESH_A_DX); + geometry.nb_nodes = uniform_section.get_required_attribute( + S_UNIFMESH_A_NBNODES); + geometry.section = uniform_section.get_required_attribute( + S_UNIFMESH_A_SECTION); + return mesh::uniform_mesh1d(geometry); +} + + +//! \brief Configure a 'ramp' mesh +mesh::Mesh1DPtr +configure_ramp_mesh1D(YAMLConfigHandle&& ramp_section) +{ + mesh::Ramp1DMeshGeometry geometry; + geometry.dx_min = ramp_section.get_required_attribute( + S_RAMPMESH_A_MIN_DX); + geometry.dx_max = ramp_section.get_required_attribute( + S_RAMPMESH_A_MAX_DX); + geometry.length_ramp = ramp_section.get_required_attribute( + S_RAMPMESH_A_RAMP_LENGTH); + geometry.length_plateau = ramp_section.get_required_attribute( + S_RAMPMESH_A_PLATEAU_LENGTH); + geometry.section = ramp_section.get_required_attribute( + S_RAMPMESH_A_SECTION); + return mesh::ramp_mesh1d(geometry); +} + + +//! \brief Configure an uniform axisymmetric mesh +mesh::Mesh1DPtr +configure_uniform_axisymmetric_mesh1D(YAMLConfigHandle&& axisymmetric_section) +{ + mesh::UniformAxisymmetricMesh1DGeometry geometry; + geometry.dx = axisymmetric_section.get_optional_attribute( + S_UNIFAXISYMESH_A_DX, -1.0); + geometry.nb_nodes = axisymmetric_section.get_optional_attribute( + S_UNIFAXISYMESH_A_NBNODES, -1.0); + geometry.height = axisymmetric_section.get_required_attribute( + S_UNIFAXISYMESH_A_HEIGHT); + geometry.radius = axisymmetric_section.get_required_attribute( + S_UNIFAXISYMESH_A_RADIUS); + return mesh::uniform_axisymmetric_mesh1d(geometry); +} + +// old interface +// ------------- + mesh::Mesh1DPtr configure_mesh(const YAML::Node& conf) { check_mandatory_yaml_node(conf, S_MESH, "__main__"); const YAML::Node& subconf = conf[S_MESH]; std::string type = subconf[S_MESH_A_TYPE].as(); if (type == S_UNIFMESH) return configure_uniform_mesh1D(subconf); else if (type == S_RAMPMESH) return configure_ramp_mesh1D(subconf); else if (type == S_UNIFAXISYMESH) return configure_uniform_axisymmetric_mesh1D(subconf); else throw std::invalid_argument("Not a valid type of mesh : '"+type+"'."); } + mesh::Mesh1DPtr configure_uniform_mesh1D(const YAML::Node& conf) { check_mandatory_yaml_node(conf, S_UNIFMESH, S_MESH); const YAML::Node& subconf = conf[S_UNIFMESH]; mesh::Uniform1DMeshGeometry geometry; geometry.dx = get_yaml_mandatory(subconf, S_UNIFMESH_A_DX, S_UNIFMESH); geometry.nb_nodes = get_yaml_mandatory(subconf, S_UNIFMESH_A_NBNODES, S_UNIFMESH); geometry.section = get_yaml_mandatory(subconf, S_UNIFMESH_A_SECTION, S_UNIFMESH); return mesh::uniform_mesh1d(geometry); } + + mesh::Mesh1DPtr configure_ramp_mesh1D(const YAML::Node& conf) { check_mandatory_yaml_node(conf, S_RAMPMESH, S_MESH); const YAML::Node& subconf = conf[S_RAMPMESH]; mesh::Ramp1DMeshGeometry geometry; geometry.dx_min = get_yaml_mandatory(subconf, S_RAMPMESH_A_MIN_DX, S_RAMPMESH); geometry.dx_max = get_yaml_mandatory(subconf, S_RAMPMESH_A_MAX_DX, S_RAMPMESH); geometry.length_ramp = get_yaml_mandatory(subconf, S_RAMPMESH_A_RAMP_LENGTH, S_RAMPMESH); geometry.length_plateau = get_yaml_mandatory(subconf, S_RAMPMESH_A_PLATEAU_LENGTH, S_RAMPMESH); geometry.section = get_yaml_mandatory(subconf, S_RAMPMESH_A_SECTION, S_RAMPMESH); return mesh::ramp_mesh1d(geometry); } mesh::Mesh1DPtr configure_uniform_axisymmetric_mesh1D(const YAML::Node& conf) { const YAML::Node& subconf = conf[S_UNIFAXISYMESH]; mesh::UniformAxisymmetricMesh1DGeometry geometry; geometry.dx = get_yaml_optional(subconf, S_UNIFAXISYMESH_A_DX, S_UNIFAXISYMESH, -1.0); geometry.nb_nodes = get_yaml_optional(subconf, S_UNIFAXISYMESH_A_NBNODES, S_UNIFAXISYMESH, -1); geometry.height = get_yaml_mandatory(subconf, S_UNIFAXISYMESH_A_HEIGHT, S_UNIFAXISYMESH); geometry.radius = get_yaml_mandatory(subconf, S_UNIFAXISYMESH_A_RADIUS, S_UNIFAXISYMESH); return mesh::uniform_axisymmetric_mesh1d(geometry); } } //end namespace io } //end namespace specmicp diff --git a/src/dfpm/io/configuration.hpp b/src/dfpm/io/configuration.hpp index e7e4ea7..5972865 100644 --- a/src/dfpm/io/configuration.hpp +++ b/src/dfpm/io/configuration.hpp @@ -1,64 +1,100 @@ /* ============================================================================= Copyright (c) 2014 - 2016 F. Georget Princeton University All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ============================================================================= */ #ifndef SPECMICP_DFPM_IO_CONFIGURATION_HPP #define SPECMICP_DFPM_IO_CONFIGURATION_HPP -#include "../../macros.hpp" -#include "../meshes/mesh1dfwd.hpp" +#include "macros.hpp" +#include "dfpm/meshes/mesh1dfwd.hpp" namespace YAML { class Node; } //end namespace YAML namespace specmicp { namespace io { +class YAMLConfigHandle; + //! \brief Configure the mesh -mesh::Mesh1DPtr SPECMICP_DLL_PUBLIC configure_mesh(const YAML::Node& conf); +//! +//! \deprecated +mesh::Mesh1DPtr SPECMICP_DLL_PUBLIC +configure_mesh(const YAML::Node& conf) +SPECMICP_DEPRECATED("Unsafe YAML interface"); //! \brief Configure an uniform mesh -mesh::Mesh1DPtr SPECMICP_DLL_PUBLIC configure_uniform_mesh1D(const YAML::Node& conf); +//! +//! \deprecated +mesh::Mesh1DPtr SPECMICP_DLL_PUBLIC +configure_uniform_mesh1D(const YAML::Node& conf) +SPECMICP_DEPRECATED("Unsafe YAML interface"); //! \brief Configure a 'ramp' mesh -mesh::Mesh1DPtr SPECMICP_DLL_PUBLIC configure_ramp_mesh1D(const YAML::Node& conf); +//! +//! \deprecated +mesh::Mesh1DPtr SPECMICP_DLL_PUBLIC +configure_ramp_mesh1D(const YAML::Node& conf) +SPECMICP_DEPRECATED("Unsafe YAML interface"); //! \brief Configure an uniform axisymmetric mesh -mesh::Mesh1DPtr SPECMICP_DLL_PUBLIC configure_uniform_axisymmetric_mesh1D(const YAML::Node& conf); +//! +//! \deprecated +mesh::Mesh1DPtr SPECMICP_DLL_PUBLIC +configure_uniform_axisymmetric_mesh1D(const YAML::Node& conf) +SPECMICP_DEPRECATED("Unsafe YAML interface"); + + +//! \brief Configure the mesh +mesh::Mesh1DPtr SPECMICP_DLL_PUBLIC +configure_mesh(YAMLConfigHandle&& mesh_section); + +//! \brief Configure an uniform mesh +mesh::Mesh1DPtr SPECMICP_DLL_PUBLIC +configure_uniform_mesh1D(YAMLConfigHandle&& uniform_section); + +//! \brief Configure a 'ramp' mesh +mesh::Mesh1DPtr SPECMICP_DLL_PUBLIC +configure_ramp_mesh1D(YAMLConfigHandle&& ramp_section); + +//! \brief Configure an uniform axisymmetric mesh +mesh::Mesh1DPtr SPECMICP_DLL_PUBLIC +configure_uniform_axisymmetric_mesh1D(YAMLConfigHandle&& axisymmetric_section); + } //end namespace io } //end namespace specmicp #endif // SPECMICP_DFPM_IO_CONFIGURATION_HPP diff --git a/tests/reactmicp/CMakeLists.txt b/tests/reactmicp/CMakeLists.txt index 54ea252..3364451 100644 --- a/tests/reactmicp/CMakeLists.txt +++ b/tests/reactmicp/CMakeLists.txt @@ -1,99 +1,92 @@ # Mesh # ---- set(test_meshes_files meshes/test_meshes.cpp meshes/test_uniform_mesh_1d.cpp meshes/test_generic1d.cpp meshes/test_axisymmetric_uniform_mesh_1d.cpp meshes/test_configuration.cpp ) # hdf5 if (HDF5_FOUND) list(APPEND test_meshes_files meshes/io_hdf5_meshes.cpp ) include_directories(${HDF5_INCLUDE_DIRS}) set_source_files_properties( meshes/io_hdf5_meshes.cpp PROPERTIES COMPILE_DEFINITIONS HDF5_DEFINITIONS ) endif() add_catch_test(NAME meshes SOURCES "${test_meshes_files}" LINK_LIBRARIES dfpm_static specmicp_common_static ${YAML_LIBRARIES}) -FILE(COPY - meshes/unif_mesh.yaml - meshes/ramp_mesh.yaml - meshes/unif_axisym.yaml - - DESTINATION ${CMAKE_CURRENT_BINARY_DIR} -) # Reactmicp : Reactive transport solver # ------------------------------------- set(test_reactmicp_files solver/test_reactive_transport_solver.cpp solver/test_coupling.cpp ) add_catch_test(NAME reactmicp SOURCES ${test_reactmicp_files} LINK_LIBRARIES ${REACTMICP_STATIC_LIBS}) # Saturated diffusion using new reactive transport solver # ------------------------------------------------------- set(test_reactmicp_saturated_files systems/saturated_react/test_reactmicp_saturated_react.cpp systems/saturated_react/speciation_system.cpp systems/saturated_react/variables.cpp systems/saturated_react/transport_program.cpp systems/saturated_react/equilibrium_stagger.cpp ) add_catch_test( NAME reactmicp_saturated_react SOURCES ${test_reactmicp_saturated_files} LINK_LIBRARIES ${REACTMICP_STATIC_LIBS} ) # Unsaturated reactive transport system # ------------------------------------- set(UNSATURATED_DIR systems/unsaturated) set(test_reactmicp_unsaturated_files ${UNSATURATED_DIR}/test_reactmicp_unsaturated.cpp ${UNSATURATED_DIR}/aqueous_equation.cpp ${UNSATURATED_DIR}/aqueous_pressure_equation.cpp ${UNSATURATED_DIR}/boundary_conditions.cpp ${UNSATURATED_DIR}/configuration.cpp ${UNSATURATED_DIR}/equilibrium_stagger.cpp ${UNSATURATED_DIR}/pressure_equation.cpp ${UNSATURATED_DIR}/saturation_equation.cpp ${UNSATURATED_DIR}/saturation_pressure_equation.cpp ${UNSATURATED_DIR}/transport_stagger.cpp ${UNSATURATED_DIR}/variables.cpp ) set(TEST_CEMDATA_PATH \"../../data/cemdata.yaml\") set_source_files_properties( ${test_reactmicp_unsaturated_files} PROPERTIES COMPILE_DEFINITIONS "TEST_CEMDATA_PATH=${TEST_CEMDATA_PATH}" ) add_catch_test( NAME reactmicp_unsaturated SOURCES ${test_reactmicp_unsaturated_files} LINK_LIBRARIES ${REACTMICP_STATIC_LIBS} ) # unsaturated binary # ------------------ add_subdirectory( binary/unsaturated ) diff --git a/tests/reactmicp/meshes/ramp_mesh.yaml b/tests/reactmicp/meshes/ramp_mesh.yaml deleted file mode 100644 index 32c4133..0000000 --- a/tests/reactmicp/meshes/ramp_mesh.yaml +++ /dev/null @@ -1,9 +0,0 @@ ---- -mesh: - type: ramp_mesh - ramp_mesh: - min_dx: 1.0 - max_dx: 5.0 - section: 2.0 - length_ramp: 10 - length_plateau: 50 diff --git a/tests/reactmicp/meshes/test_configuration.cpp b/tests/reactmicp/meshes/test_configuration.cpp index cc6a74c..5af93e9 100644 --- a/tests/reactmicp/meshes/test_configuration.cpp +++ b/tests/reactmicp/meshes/test_configuration.cpp @@ -1,33 +1,71 @@ #include -#include "utils/io/yaml.hpp" #include "dfpm/meshes/mesh1d.hpp" #include "dfpm/io/configuration.hpp" +#include "utils/io/safe_config.hpp" TEST_CASE("Configuration", "[meshes],[io],[configuration]") { - SECTION("Uniform mesh") { + SECTION("Uniform mesh") + { + const char * conf_str = + R"( + mesh: + type: uniform_mesh + uniform_mesh: + dx: 1.0 + section: 2.0 + nb_nodes: 50 + )"; - YAML::Node conf = specmicp::io::parse_yaml_file("unif_mesh.yaml"); - specmicp::mesh::Mesh1DPtr the_mesh = specmicp::io::configure_mesh(conf); + auto conf = specmicp::io::YAMLConfigFile::load_from_string(conf_str); + auto the_mesh = specmicp::io::configure_mesh(conf.get_section("mesh")); REQUIRE(the_mesh->nb_nodes() == 50); + CHECK(the_mesh->get_dx(0) == 1.0); + CHECK(the_mesh->get_face_area(0) == 2.0); } SECTION("Ramp mesh") { - YAML::Node conf = specmicp::io::parse_yaml_file("ramp_mesh.yaml"); - specmicp::mesh::Mesh1DPtr the_mesh = specmicp::io::configure_mesh(conf); + + const char * conf_str = + R"( + mesh: + type: ramp_mesh + ramp_mesh: + min_dx: 1.0 + max_dx: 5.0 + section: 2.0 + length_ramp: 10 + length_plateau: 50 + + )"; + + auto conf = specmicp::io::YAMLConfigFile::load_from_string(conf_str); + auto the_mesh = specmicp::io::configure_mesh(conf.get_section("mesh")); REQUIRE(the_mesh->get_dx(0) == 1.0); REQUIRE(the_mesh->get_dx(the_mesh->nb_nodes()-2) == 5.0); } SECTION("Uniform axisymmetric mesh") { - YAML::Node conf = specmicp::io::parse_yaml_file("unif_axisym.yaml"); - specmicp::mesh::Mesh1DPtr the_mesh = specmicp::io::configure_mesh(conf); + const char * conf_str = + R"( + mesh: + type: uniform_axisymmetric + uniform_axisymmetric: + dx: 1.0 + radius: 100.0 + nb_nodes: 10 + height: 3.0 + + )"; + + auto conf = specmicp::io::YAMLConfigFile::load_from_string(conf_str); + auto the_mesh = specmicp::io::configure_mesh(conf.get_section("mesh")); REQUIRE(the_mesh->get_dx(0) == 1.0); REQUIRE(the_mesh->nb_nodes() == 10); } } diff --git a/tests/reactmicp/meshes/unif_axisym.yaml b/tests/reactmicp/meshes/unif_axisym.yaml deleted file mode 100644 index ef72fed..0000000 --- a/tests/reactmicp/meshes/unif_axisym.yaml +++ /dev/null @@ -1,9 +0,0 @@ ---- -mesh: - type: uniform_axisymmetric - uniform_axisymmetric: - dx: 1.0 - radius: 100.0 - nb_nodes: 10 - height: 3.0 - diff --git a/tests/reactmicp/meshes/unif_mesh.yaml b/tests/reactmicp/meshes/unif_mesh.yaml deleted file mode 100644 index a2f7c3c..0000000 --- a/tests/reactmicp/meshes/unif_mesh.yaml +++ /dev/null @@ -1,7 +0,0 @@ ---- -mesh: - type: uniform_mesh - uniform_mesh: - dx: 1.0 - section: 2.0 - nb_nodes: 50