From 87765c09308592a4ce354da201373b9e9c7eb637 Mon Sep 17 00:00:00 2001 From: Kyle Shores Date: Tue, 2 Apr 2024 09:17:55 -0500 Subject: [PATCH] Allow custom rate parameters to be directly set (#440) * allowing cutsom rate parameters to be set directly assuming the order is correct * adding checks * only running release tests, and removing duplicate windows tests * storing grid size and using that for iteration * std::size_t --- .github/workflows/mac.yml | 6 +-- .github/workflows/ubuntu.yml | 4 +- .github/workflows/windows.yml | 30 +---------- include/micm/solver/state.hpp | 7 ++- include/micm/solver/state.inl | 18 ++++++- test/integration/analytical_policy.hpp | 6 +-- test/unit/solver/test_state.cpp | 74 ++++++++++++++++++++++++++ 7 files changed, 107 insertions(+), 38 deletions(-) diff --git a/.github/workflows/mac.yml b/.github/workflows/mac.yml index 9e9bc0eb6..c9dce440e 100644 --- a/.github/workflows/mac.yml +++ b/.github/workflows/mac.yml @@ -14,7 +14,7 @@ jobs: matrix: # all available versions of xcode: https://github.com/actions/runner-images/blob/main/images/macos/macos-12-Readme.md#xcode xcode: ['13.1', '14.1'] - build_type: [Debug, Release] + build_type: [Release] env: DEVELOPER_DIR: /Applications/Xcode_${{ matrix.xcode }}.app/Contents/Developer @@ -39,7 +39,7 @@ jobs: matrix: # all available versions of xcode: https://github.com/actions/runner-images/blob/main/images/macos/macos-13-Readme.md#xcode xcode: ['14.1', '15.0'] - build_type: [Debug, Release] + build_type: [Release] env: DEVELOPER_DIR: /Applications/Xcode_${{ matrix.xcode }}.app/Contents/Developer @@ -66,7 +66,7 @@ jobs: - { cpp: g++-11, c: gcc-11} - { cpp: g++-12, c: gcc-12} - { cpp: clang++, c: clang} - build_type: [Debug, Release] + build_type: [Release] env: CC: ${{ matrix.compiler.c }} CXX: ${{ matrix.compiler.cpp }} diff --git a/.github/workflows/ubuntu.yml b/.github/workflows/ubuntu.yml index 64b81798f..d9847071b 100644 --- a/.github/workflows/ubuntu.yml +++ b/.github/workflows/ubuntu.yml @@ -12,7 +12,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - build_type: [Debug, Release] + build_type: [Release] env: CC: gcc CXX: g++ @@ -36,7 +36,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - build_type: [Debug, Release] + build_type: [Release] env: CC: clang CXX: clang++ diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index dd74d1a3f..fe22864d4 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -38,7 +38,7 @@ jobs: runs-on: windows-2019 strategy: matrix: - build_type: [Debug, Release] + build_type: [Release] architecture: [Win32, x64] steps: @@ -53,25 +53,12 @@ jobs: - name: Test run: cd build ; ctest -j 10 -C ${{ matrix.build_type }} --output-on-failure - msvc2019_latest: - if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name - runs-on: windows-2019 - - steps: - - uses: actions/checkout@v3 - - name: Run CMake - run: cmake -S . -B build -G "Visual Studio 16 2019" - - name: Build - run: cmake --build build --config Release --parallel 10 - - name: Test - run: cd build ; ctest -j 10 -C Release --output-on-failure - msvc2022: if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name runs-on: windows-2022 strategy: matrix: - build_type: [Debug, Release] + build_type: [Release] architecture: [Win32, x64] steps: @@ -83,19 +70,6 @@ jobs: - name: Test run: cd build ; ctest -j 10 -C ${{ matrix.build_type }} --output-on-failure - msvc2022_latest: - if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name - runs-on: windows-2022 - - steps: - - uses: actions/checkout@v3 - - name: Run CMake - run: cmake -S . -B build -G "Visual Studio 17 2022" - - name: Build - run: cmake --build build --config Release --parallel 10 - - name: Test - run: cd build ; ctest -j 10 -C Release --output-on-failure - clang: if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name runs-on: windows-2019 diff --git a/include/micm/solver/state.hpp b/include/micm/solver/state.hpp index 87d1d204f..fe355a0d1 100644 --- a/include/micm/solver/state.hpp +++ b/include/micm/solver/state.hpp @@ -48,7 +48,8 @@ namespace micm std::vector variable_names_{}; SparseMatrixPolicy lower_matrix_; SparseMatrixPolicy upper_matrix_; - size_t state_size_; + std::size_t state_size_; + std::size_t number_of_grid_cells_; /// @brief State(); @@ -67,6 +68,10 @@ namespace micm void SetConcentration(const Species& species, double concentration); void SetConcentration(const Species& species, const std::vector& concentration); + /// @brief Set custom parameters assuming the values are properly ordered + /// @param parameters map of custom rate parameters + void UnsafelySetCustomRateParameters(const std::vector>& parameters); + /// @brief Set custom parameters for rate constant calculations by label /// @param parameters map of custom rate parameters void SetCustomRateParameters(const std::unordered_map>& parameters); diff --git a/include/micm/solver/state.inl b/include/micm/solver/state.inl index 6bb2b5143..366740d6e 100644 --- a/include/micm/solver/state.inl +++ b/include/micm/solver/state.inl @@ -26,7 +26,8 @@ namespace micm jacobian_(), lower_matrix_(), upper_matrix_(), - state_size_(parameters.variable_names_.size()) + state_size_(parameters.variable_names_.size()), + number_of_grid_cells_(parameters.number_of_grid_cells_) { std::size_t index = 0; for (auto& name : variable_names_) @@ -81,6 +82,21 @@ namespace micm variables_[i][i_species] = concentration[i]; } + template class MatrixPolicy, template class SparseMatrixPolicy> + inline void State::UnsafelySetCustomRateParameters( + const std::vector>& parameters) + { + if (parameters.size() != variables_.size()) + throw std::invalid_argument("The number of grid cells configured for micm does not match the number of custom rate parameter values passed to multi-gridcell State"); + + if (parameters[0].size() != custom_rate_parameters_[0].size()) + throw std::invalid_argument("The number of custom rate parameters configured for micm does not match the provided number of custom rate parameter values"); + + for(size_t i = 0; i < number_of_grid_cells_; ++i) { + custom_rate_parameters_[i] = parameters[i]; + } + } + template class MatrixPolicy, template class SparseMatrixPolicy> inline void State::SetCustomRateParameters( const std::unordered_map>& parameters) diff --git a/test/integration/analytical_policy.hpp b/test/integration/analytical_policy.hpp index 4ab97da9f..0de4864ba 100644 --- a/test/integration/analytical_policy.hpp +++ b/test/integration/analytical_policy.hpp @@ -1108,7 +1108,6 @@ void test_analytical_arrhenius( times.push_back(0); for (size_t i_time = 1; i_time < nsteps; ++i_time) { - times.push_back(time_step); // Model results auto result = solver.Solve(time_step, state); EXPECT_EQ(result.state_, (micm::SolverState::Converged)); @@ -1119,6 +1118,7 @@ void test_analytical_arrhenius( // Analytical results double time = i_time * time_step; + times.push_back(time); double initial_A = analytical_concentrations[0][idx_A]; analytical_concentrations[i_time][idx_A] = initial_A * std::exp(-(k1)*time); @@ -1129,8 +1129,8 @@ void test_analytical_arrhenius( } std::vector header = { "time", "A", "B", "C" }; - writeCSV("analytical_concentrations.csv", header, analytical_concentrations, times); - writeCSV("model_concentrations.csv", header, model_concentrations, times); + writeCSV("analytical_concentrations-arrhenius.csv", header, analytical_concentrations, times); + writeCSV("model_concentrations-arrhenius.csv", header, model_concentrations, times); auto map = state.variable_map_; diff --git a/test/unit/solver/test_state.cpp b/test/unit/solver/test_state.cpp index db8217afd..d2cd08304 100644 --- a/test/unit/solver/test_state.cpp +++ b/test/unit/solver/test_state.cpp @@ -195,3 +195,77 @@ TEST(State, SetCustomRateParameters) } } } + +TEST(State, UnsafelySetCustomRateParameterOneCell) +{ + micm::State state{ micm::StateParameters{ + .number_of_grid_cells_ = 1, + .number_of_rate_constants_ = 10, + .variable_names_{ "foo", "bar", "baz", "quz" }, + .custom_rate_parameter_labels_{ "O1", "O2", "O3", "AAA", "BBB" }, + } }; + + std::vector> parameters = {{0.1, 0.2, 0.3, 0.4, 0.5}}; + + state.UnsafelySetCustomRateParameters(parameters); + EXPECT_EQ(state.custom_rate_parameters_[0][0], 0.1); + EXPECT_EQ(state.custom_rate_parameters_[0][1], 0.2); + EXPECT_EQ(state.custom_rate_parameters_[0][2], 0.3); + EXPECT_EQ(state.custom_rate_parameters_[0][3], 0.4); + EXPECT_EQ(state.custom_rate_parameters_[0][4], 0.5); +} + +TEST(State, UnsafelySetCustomRateParameterMultiCell) +{ + uint32_t num_grid_cells = 3; + + micm::State state{ micm::StateParameters{ + .number_of_grid_cells_ = num_grid_cells, + .number_of_rate_constants_ = 10, + .variable_names_{ "foo", "bar", "baz", "quz" }, + .custom_rate_parameter_labels_{ "O1", "O2", "O3", "AAA", "BBB" }, + } }; + + std::vector> parameters = { + {0.1, 0.2, 0.3, 0.4, 0.5}, + {0.1, 0.2, 0.3, 0.4, 0.5}, + {0.1, 0.2, 0.3, 0.4, 0.5} + }; + + state.UnsafelySetCustomRateParameters(parameters); + for(size_t i = 0; i < num_grid_cells; i++) { + EXPECT_EQ(state.custom_rate_parameters_[i][0], 0.1); + EXPECT_EQ(state.custom_rate_parameters_[i][1], 0.2); + EXPECT_EQ(state.custom_rate_parameters_[i][2], 0.3); + EXPECT_EQ(state.custom_rate_parameters_[i][3], 0.4); + EXPECT_EQ(state.custom_rate_parameters_[i][4], 0.5); + } +} + +TEST(State, UnsafelySetCustomRateParameterCatchesTooFewGridCells) +{ + micm::State state{ micm::StateParameters{ + .number_of_grid_cells_ = 2, + .number_of_rate_constants_ = 10, + .variable_names_{ "foo", "bar", "baz", "quz" }, + .custom_rate_parameter_labels_{ "O1", "O2", "O3", "AAA", "BBB" }, + } }; + + std::vector> parameters = {{0.1, 0.2, 0.3, 0.4, 0.5}}; + + EXPECT_ANY_THROW(state.UnsafelySetCustomRateParameters(parameters)); +} + +TEST(State, UnsafelySetCustomRateParameterCatchesTooParameters) +{ + micm::State state{ micm::StateParameters{ + .number_of_grid_cells_ = 2, + .number_of_rate_constants_ = 10, + .variable_names_{ "foo", "bar", "baz", "quz" }, + .custom_rate_parameter_labels_{ "O1", "O2", "O3", "AAA", "BBB" }, + } }; + + std::vector> parameters = {{0.1, 0.2, 0.3}}; + + EXPECT_ANY_THROW(state.UnsafelySetCustomRateParameters(parameters)); +} \ No newline at end of file