From 32a16e2e906e082dd8391d3836f44d4892fc481a Mon Sep 17 00:00:00 2001 From: Montek Thind Date: Tue, 1 Oct 2024 13:53:19 -0700 Subject: [PATCH] third draft --- include/micm/configure/solver_config.hpp | 15 ++++++++++++--- .../micm/solver/rosenbrock_solver_parameters.hpp | 2 -- include/micm/solver/solver_builder.hpp | 1 + include/micm/solver/solver_builder.inl | 4 ++-- include/micm/solver/state.hpp | 3 ++- include/micm/solver/state.inl | 6 ++++++ test/integration/test_chapman_integration.cpp | 10 +++++----- test/unit/solver/test_solver_builder.cpp | 4 +--- 8 files changed, 29 insertions(+), 16 deletions(-) diff --git a/include/micm/configure/solver_config.hpp b/include/micm/configure/solver_config.hpp index 0358e8cf6..27af08b7e 100644 --- a/include/micm/configure/solver_config.hpp +++ b/include/micm/configure/solver_config.hpp @@ -102,6 +102,7 @@ namespace micm System system_; std::vector processes_; RosenbrockSolverParameters parameters_; + double relative_tolerance_; SolverParameters(const System& system, std::vector&& processes, const RosenbrockSolverParameters&& parameters) : system_(system), @@ -116,6 +117,13 @@ namespace micm parameters_(parameters) { } + SolverParameters(System&& system, std::vector&& processes, RosenbrockSolverParameters&& parameters, double relative_tolerance) + : system_(system), + processes_(processes), + parameters_(parameters), + relative_tolerance_(relative_tolerance) + { + } }; class ConfigReaderPolicy @@ -138,6 +146,7 @@ namespace micm std::unordered_map phases_; std::vector processes_; RosenbrockSolverParameters parameters_; + double relative_tolerance_; // Common YAML inline static const std::string DEFAULT_CONFIG_FILE_JSON = "config.json"; @@ -273,7 +282,7 @@ namespace micm processes_.clear(); // Parse species object array - ParseSpeciesArray(species_objects); + ParseSpeciesArray(species_objects); // Assign the parsed 'Species' to 'Phase' std::vector species_arr; @@ -426,7 +435,7 @@ namespace micm void ParseRelativeTolerance(const objectType& object) { ValidateSchema(object, { "value", "type" }, {}); - //this->parameters_.relative_tolerance_ = object["value"].as(); + this->relative_tolerance_ = object["value"].as(); } void ParseMechanism(const objectType& object) @@ -959,7 +968,7 @@ namespace micm SolverParameters GetSolverParams() { return SolverParameters( - std::move(System(this->gas_phase_, this->phases_)), std::move(this->processes_), std::move(this->parameters_)); + std::move(System(this->gas_phase_, this->phases_)), std::move(this->processes_), std::move(this->parameters_), std::move(this->relative_tolerance_)); } }; } // namespace micm diff --git a/include/micm/solver/rosenbrock_solver_parameters.hpp b/include/micm/solver/rosenbrock_solver_parameters.hpp index 9f7338ec1..8ae4c67b0 100644 --- a/include/micm/solver/rosenbrock_solver_parameters.hpp +++ b/include/micm/solver/rosenbrock_solver_parameters.hpp @@ -9,8 +9,6 @@ #include #include -//#include "../include/micm/solver/solver.hpp" - namespace micm { diff --git a/include/micm/solver/solver_builder.hpp b/include/micm/solver/solver_builder.hpp index c3a8d7a3a..fc5d741fe 100644 --- a/include/micm/solver/solver_builder.hpp +++ b/include/micm/solver/solver_builder.hpp @@ -50,6 +50,7 @@ namespace micm bool reorder_state_ = true; bool valid_system_ = false; bool valid_reactions_ = false; + double relative_tolerance_ = 1e-06; public: SolverBuilder() = delete; diff --git a/include/micm/solver/solver_builder.inl b/include/micm/solver/solver_builder.inl index a3889e56b..e98e43538 100644 --- a/include/micm/solver/solver_builder.inl +++ b/include/micm/solver/solver_builder.inl @@ -316,7 +316,7 @@ namespace micm } } } - } + } template< class SolverParametersPolicy, @@ -399,7 +399,7 @@ namespace micm .nonzero_jacobian_elements_ = nonzero_elements }; - this->SetAbsoluteTolerances(state_parameters.absolute_tolerance_, species_map); //WHERE TO ADD THIS?????? + this->SetAbsoluteTolerances(state_parameters.absolute_tolerance_, species_map); return Solver( SolverPolicy(options, std::move(linear_solver), std::move(rates), jacobian), diff --git a/include/micm/solver/state.hpp b/include/micm/solver/state.hpp index 6e1773d63..cb38eeddc 100644 --- a/include/micm/solver/state.hpp +++ b/include/micm/solver/state.hpp @@ -31,7 +31,7 @@ namespace micm std::vector variable_names_{}; std::vector custom_rate_parameter_labels_{}; std::set> nonzero_jacobian_elements_{}; - double relative_tolerance_; + double relative_tolerance_{ 1e-06 }; std::vector absolute_tolerance_ {}; }; @@ -140,6 +140,7 @@ namespace micm /// @param value new parameter value void SetCustomRateParameter(const std::string& label, double value); void SetCustomRateParameter(const std::string& label, const std::vector& values); + void SetRelativeTolerances(double relativeTolerance); /// @brief Print a header of species to display concentrations with respect to time void PrintHeader(); diff --git a/include/micm/solver/state.inl b/include/micm/solver/state.inl index 654befaed..37f3962b5 100644 --- a/include/micm/solver/state.inl +++ b/include/micm/solver/state.inl @@ -187,6 +187,12 @@ namespace micm for (std::size_t i = 0; i < custom_rate_parameters_.NumRows(); ++i) custom_rate_parameters_[i][param->second] = values[i]; } + + template + inline void State::SetRelativeTolerances(double relativeTolerance) + { + this->relative_tolerance_ = relativeTolerance; + } template inline void State::PrintHeader() diff --git a/test/integration/test_chapman_integration.cpp b/test/integration/test_chapman_integration.cpp index 5cea1e877..8ebdc001f 100644 --- a/test/integration/test_chapman_integration.cpp +++ b/test/integration/test_chapman_integration.cpp @@ -32,17 +32,17 @@ TEST(ChapmanIntegration, CanBuildChapmanSystemUsingConfig) // Get solver parameters ('System', the collection of 'Process') micm::SolverParameters solver_params = solverConfig.GetSolverParams(); - auto options = solver_params.parameters_; + auto options = solver_params.parameters_; auto solver = micm::CpuSolverBuilder(options) .SetSystem(solver_params.system_) .SetReactions(solver_params.processes_) - .SetIgnoreUnusedSpecies(true) - .Build(); + .SetIgnoreUnusedSpecies(true) + .Build(); micm::State state = solver.GetState(); - - EXPECT_EQ(state.relative_tolerance_, 1.0e-6); + state.SetRelativeTolerances(solver_params.relative_tolerance_); + EXPECT_EQ(state.relative_tolerance_, 1.0e-4); for (size_t n_grid_cell = 0; n_grid_cell < state.number_of_grid_cells_; ++n_grid_cell) { diff --git a/test/unit/solver/test_solver_builder.cpp b/test/unit/solver/test_solver_builder.cpp index 5f045cd25..fc9209ef2 100644 --- a/test/unit/solver/test_solver_builder.cpp +++ b/test/unit/solver/test_solver_builder.cpp @@ -105,7 +105,6 @@ TEST(SolverBuilder, CanBuildRosenbrock) .SetNumberOfGridCells(1) .Build(); } - /* TEST(SolverBuilder, MismatchedToleranceSizeIsCaught) { @@ -128,5 +127,4 @@ TEST(SolverBuilder, MismatchedToleranceSizeIsCaught) micm::SparseMatrix>>(params); EXPECT_ANY_THROW(builder.SetSystem(the_system).SetReactions(reactions).SetNumberOfGridCells(1).Build();); -} -*/ \ No newline at end of file +}*/ \ No newline at end of file