diff --git a/src/reactmicp/systems/saturated_react/equilibrium_stagger.cpp b/src/reactmicp/systems/saturated_react/equilibrium_stagger.cpp index fccdb56..e82d4a7 100644 --- a/src/reactmicp/systems/saturated_react/equilibrium_stagger.cpp +++ b/src/reactmicp/systems/saturated_react/equilibrium_stagger.cpp @@ -1,218 +1,218 @@ /* ============================================================================= 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 "equilibrium_stagger.hpp" #include "variables.hpp" #include "specmicp/adimensional/adimensional_system_solver.hpp" #include "specmicp/adimensional/adimensional_system_solution_extractor.hpp" #include "specmicp_database/data_container.hpp" #include "specmicp_common/log.hpp" #include "reactmicp/solver/staggers_base/stagger_structs.hpp" #include "specmicp_common/config.h" namespace specmicp { namespace reactmicp { namespace systems { namespace satdiff { using TrueConstPtr = SaturatedVariables * const; //!< const Ptr to the variables //! \brief cast base variables to saturated variables inline TrueConstPtr cast_to_var(solver::VariablesBase * const var) { return static_cast(var); } // EquilibriumStagger:: //! \brief Initialize the stagger at the beginning of an iteration void EquilibriumStagger::initialize_timestep( scalar_t dt, VariablesBase * const var) { m_dt = dt; TrueConstPtr true_var = cast_to_var(var); // Initialize velocity using values from previous timestep for (index_t node=0; nodenb_nodes(); ++node) { if (true_var->is_fixed_composition(node)) continue; scalar_t alpha = 1.0; for (index_t component: true_var->get_database()->range_aqueous_component()) { alpha = std::max(alpha, 0.9*dt*true_var->solid_concentration(node, component, true_var->chemistry_rate()) /(true_var->solid_concentration(node, component, true_var->displacement())) ); } auto solid_velocity = true_var->velocity().segment( true_var->offset_node(node)+true_var->offset_solid_concentration(), true_var->nb_component()); auto solid_chemistry_rate = true_var->chemistry_rate().segment( true_var->offset_node(node)+true_var->offset_solid_concentration(), true_var->nb_component()); solid_velocity = 1/alpha * solid_chemistry_rate; } } //! \brief Solve the equation for the timestep solver::StaggerReturnCode EquilibriumStagger::restart_timestep(VariablesBase * const var) { TrueConstPtr true_var = cast_to_var(var); int failed_chemistry = 0; #ifdef SPECMICP_HAVE_OPENMP -#pragma omp parallel default(none) shared(failed_chemistry) +#pragma omp parallel default(none) shared(failed_chemistry, true_var) // node true_var being const is shared by default, can't be mentionned again { #pragma omp for schedule(dynamic, 5) for (index_t node=0; nodenb_nodes(); ++node) { // only solve if necessary if (true_var->is_fixed_composition(node) or failed_chemistry > 0) continue; const auto retcode = solve_one_node(node, true_var); if (retcode > 0) { ++failed_chemistry; } } } #else { for (index_t node=0; nodenb_nodes(); ++node) { if (true_var->is_fixed_composition(node)) continue; const auto retcode = solve_one_node(node, true_var); if (retcode > 0) { ++failed_chemistry; break; } } } #endif // SPECMICP_HAVE_OPENMP if (failed_chemistry > 0) return solver::StaggerReturnCode::UnknownError; return solver::StaggerReturnCode::ResidualMinimized; } //! int EquilibriumStagger::solve_one_node( index_t node, SaturatedVariables * const true_var ) { AdimensionalSystemConstraints constraints(get_constraints(node)); constraints.total_concentrations = true_var->total_concentrations(node); AdimensionalSystemSolver adim_solver(true_var->get_database(), constraints, true_var->equilibrium_solution(node), m_options); Vector variables(true_var->equilibrium_solution(node).main_variables); micpsolver::MiCPPerformance perf = adim_solver.solve(variables); micpsolver::MiCPSolverReturnCode retcode = perf.return_code; if (retcode <= micpsolver::MiCPSolverReturnCode::NotConvergedYet) { ERROR << "Failed to solve chemistry problem at node " << node << ", return code = " << static_cast(retcode) << ", residual = " << perf.current_residual; ERROR << "Total concentration : \n" << constraints.total_concentrations; return 1; } true_var->equilibrium_solution(node) = adim_solver.get_raw_solution(variables); AdimensionalSystemSolutionExtractor extractor(true_var->equilibrium_solution(node), true_var->get_database(), m_options.units_set); for (index_t component=0; componentnb_component(); ++component) { const scalar_t c_aq = extractor.density_water()*extractor.total_aqueous_concentration(component); true_var->aqueous_concentration(node, component, true_var->displacement()) = c_aq; const scalar_t c_aq_0 = true_var->aqueous_concentration(node, component, true_var->predictor()); const scalar_t vel_aq = (c_aq - c_aq_0)/m_dt; true_var->aqueous_concentration(node, component, true_var->velocity()) = vel_aq; const scalar_t c_sol = extractor.total_immobile_concentration(component); true_var->solid_concentration(node, component, true_var->displacement()) = c_sol; const scalar_t c_sol_0 = true_var->solid_concentration(node, component, true_var->predictor()); const scalar_t vel_sol = (c_sol - c_sol_0)/m_dt; true_var->solid_concentration(node, component, true_var->velocity()) = vel_sol; true_var->solid_concentration(node, component, true_var->chemistry_rate()) = vel_sol; } return 0; } //! \brief Solve one node ReturnCode EquilibriumStagger::solve_equilibrium_at_node( index_t node, VariablesBase * const var, AdimensionalSystemSolution& out ) { TrueConstPtr true_var = cast_to_var(var); AdimensionalSystemConstraints constraints(get_constraints(node)); constraints.total_concentrations = true_var->total_concentrations(node); AdimensionalSystemSolver adim_solver(true_var->get_database(), constraints, true_var->equilibrium_solution(node), m_options); Vector variables(true_var->equilibrium_solution(node).main_variables); micpsolver::MiCPPerformance perf = adim_solver.solve(variables); micpsolver::MiCPSolverReturnCode retcode = perf.return_code; if (retcode > micpsolver::MiCPSolverReturnCode::NotConvergedYet) { out = adim_solver.get_raw_solution(variables); } return micpsolver::to_generic_return_code(retcode); } } // end namespace satdiff } // end namespace systems } // end namespace reactmicp } // end namespace specmicp diff --git a/src/specmicp_common/io/hdf5/attribute.cpp b/src/specmicp_common/io/hdf5/attribute.cpp index b4ea8ec..a113320 100644 --- a/src/specmicp_common/io/hdf5/attribute.cpp +++ b/src/specmicp_common/io/hdf5/attribute.cpp @@ -1,71 +1,73 @@ /* ============================================================================= 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 + #include "attribute.hpp" #include "dataspace.hpp" #include "H5Apublic.h" namespace specmicp { namespace io { namespace hdf5 { Attribute::Attribute(hid_t id): IdClass(id) {} Attribute::~Attribute() { H5Aclose(get_id()); } Attribute Attribute::acquire(hid_t id) { if (id < 0) { throw std::invalid_argument("Invalid attribute"); } return Attribute(id); } Dataspace Attribute::get_dataspace() const { hid_t id = H5Aget_space(get_id()); return Dataspace::acquire(id); } } // end namespace hdf5 } // end namespace io } // end namespace specmicp diff --git a/src/specmicp_common/plugins/module_base.cpp b/src/specmicp_common/plugins/module_base.cpp index 0f00752..9276e16 100644 --- a/src/specmicp_common/plugins/module_base.cpp +++ b/src/specmicp_common/plugins/module_base.cpp @@ -1,84 +1,86 @@ /* ============================================================================= 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 + #include "module_base.hpp" #include "specmicp_common/compat.hpp" namespace specmicp { namespace plugins { struct ModuleBase::ModuleBaseImpl { std::unordered_map m_objects; }; ModuleBase::ModuleBase(): m_impl(make_unique()) { } ModuleBase::~ModuleBase() = default; std::unique_ptr ModuleBase::create_module() { auto module = make_unique(); return module; } bool ModuleBase::register_object( const std::string& name, object_factory_f func ) { m_impl->m_objects.insert({name, func}); return true; } void* ModuleBase::get_object(const std::string& name) { void* obj; try { obj = m_impl->m_objects.at(name)(); } catch (const std::out_of_range& e) { throw std::invalid_argument("PluginManager error : " "no plugin with name '" + name + "'."); } return obj; } } //end namespace plugins } //end namespace specmicp