-
Notifications
You must be signed in to change notification settings - Fork 10
Add Rosenbrock integrators #479
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 49 commits
Commits
Show all changes
57 commits
Select commit
Hold shift + click to select a range
2722003
Add rosenbrock
alexander-novo bd2c5f9
Add rosenbrock tests
alexander-novo dd9ebb1
Fixed bug setting alpha coefficient in rosenbrock
alexander-novo 9d15c72
Removed extra move
alexander-novo fb45769
Remove some debug outputs
alexander-novo 7c58a56
Properly reset stats in initializeSimulation
alexander-novo 22829e1
Switch back to klu solver
alexander-novo e719466
Add empirical order testing
alexander-novo bfc88c7
Fix dense output indexing issue
alexander-novo b696073
Add RODAS5P tableau
alexander-novo 5f48049
Add rodas5p order test
alexander-novo 99270a2
Add error checking
alexander-novo 5b46fec
Re-organize rosenbrock implementation code
alexander-novo 480da97
Add embedded coefficients for rodas5p
alexander-novo 227780e
Documentation and organization
alexander-novo c5be4bc
Fix cmake when resolve not enabled
alexander-novo 8c5ea66
Fix some documentation mistakes
alexander-novo 33281cb
Make error_estimator_stage more ergonomic
alexander-novo 4b69ae9
Move documentation
alexander-novo 08f100b
More documentation
alexander-novo 7748003
Update ReSolve dependency
alexander-novo c84a7c1
Code review documentation changes
alexander-novo 7eba4f6
Changes from review
alexander-novo 8cf790e
Fix compiler errors and warnings on g++
alexander-novo 8cd71e0
Apply pre-commit fixes
alexander-novo b7a01cb
fix cmake issue
alexander-novo 9b26bdf
Update Rosenbrock to use GridKit Vector and VectorHandler
alexander-novo db7f95d
Apply pre-commit fixes
alexander-novo 9f7055a
[skip ci] Update Documentation
alexander-novo ca9fbfb
Add to changelog
alexander-novo c4ae3de
[skip ci] Add Rodas5p citation
alexander-novo c327f84
Update naming conventions
alexander-novo 0188944
[skip ci] Add comment explaining confusing interaction
alexander-novo 48bf159
[skip ci] Update Documentation
alexander-novo 39d95cd
Made screaming snake case convention explicit (#484)
shakedregev 0ddcd67
Apply suggestion from @shakedregev
shakedregev 4670635
Revert documentation delimiter change
alexander-novo 7c1b1cb
Update constants and templates
alexander-novo c5e345b
Remove SUNDIALS type
alexander-novo 4086a9f
Fix header include templating
alexander-novo fc3f548
Update rosenbrock test cmake target name
alexander-novo 8546dda
Update ReSolve dependency
alexander-novo 21affa7
Remove extra sundials include
alexander-novo bfbd34a
Update test name
alexander-novo 2be100a
remove extra ReSolve memory space
alexander-novo 58e9137
Break Rosenbrock helper classes into own files
alexander-novo abd9071
Apply pre-commit fixes
alexander-novo 7c16628
Update Rosenbrock to use new LinearSolver interface
alexander-novo a69598f
Apply pre-commit fixes
alexander-novo 8cd2a68
Moved Rosenbrock support classes to Native folder
alexander-novo e5226df
Change Integrator namespace
alexander-novo 774f47d
Remove ResolveMemoryUtils.hpp
alexander-novo 2878373
document test_order
alexander-novo 91d0fd8
Apply pre-commit fixes
alexander-novo e4b1206
Add necessary headers to cmake
alexander-novo bcfc4c5
Apply pre-commit fixes
alexander-novo 8941295
Fix compile error.
pelesh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| add_subdirectory(SparseMatrix) | ||
| add_subdirectory(DenseMatrix) | ||
| add_subdirectory(Vector) | ||
| add_subdirectory(Solver) | ||
| add_subdirectory(Solver) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| #pragma once | ||
|
|
||
| #include <GridKit/MemoryUtilities/MemoryUtils.hpp> | ||
|
|
||
| #include <resolve/MemoryUtils.hpp> | ||
|
|
||
| namespace GridKit | ||
| { | ||
| namespace memory | ||
| { | ||
| /** | ||
| * @brief Converts a GridKit \ref MemorySpace to its corresponding Re::Solve MemorySpace | ||
| * | ||
| */ | ||
| inline ReSolve::memory::MemorySpace memorySpaceAsResolve(MemorySpace memspace) | ||
| { | ||
| switch (memspace) | ||
| { | ||
| case HOST: | ||
| return ReSolve::memory::HOST; | ||
| case DEVICE: | ||
| return ReSolve::memory::DEVICE; | ||
| default: | ||
| throw "Memory space not supported"; | ||
| } | ||
| } | ||
| } // namespace memory | ||
| } // namespace GridKit | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| #include "AdaptiveStep.hpp" | ||
|
|
||
| #include <cmath> | ||
|
|
||
| #include <GridKit/Constants.hpp> | ||
|
|
||
| namespace Integrator | ||
| { | ||
| /** | ||
| * @brief Standard textbook adaptive controller. Accept if `err <= 1` and use | ||
| * | ||
| * \f[h_{new} = h * \min \left\{fac_{max}, \max\left\{fac_{min}, fac_{scale} \cdot e ^{-1/p}\right\}\right\}.\f] | ||
| * | ||
| */ | ||
| template <typename RealT> | ||
| StepControl<RealT> AdaptiveStep<RealT>::nextStep(RealT err, StepControl<RealT> prev_step, uint8_t method_order) | ||
| { | ||
| StepControl<RealT> next_step = prev_step; | ||
|
|
||
| double h_mult = std::min(params_.fac_max_, std::max(params_.fac_scale_ * std::pow(err, GridKit::MINUS_ONE<RealT> / method_order), params_.fac_min_)); | ||
|
|
||
| next_step.accept_ = err <= 1; | ||
| next_step.step_size_ *= h_mult; | ||
|
|
||
| return next_step; | ||
| } | ||
|
|
||
| template class AdaptiveStep<double>; | ||
| } // namespace Integrator |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| #pragma once | ||
|
|
||
| #include <GridKit/Solver/Dynamic/StepController.hpp> | ||
|
|
||
| namespace Integrator | ||
| { | ||
|
|
||
| /** | ||
| * @brief A simple textbook adaptive `StepController` which seeks to meet a relative and absolute tolerance | ||
| * based on an error estimate. | ||
| * | ||
| */ | ||
| template <typename RealT> | ||
| class AdaptiveStep : public StepController<RealT> | ||
| { | ||
| /** | ||
| * @brief Parameters for the step controller. | ||
| * | ||
| */ | ||
| struct Parameters | ||
| { | ||
| /** | ||
| * @brief The minimum multiple by which the step size can be multiplied to obtain the new step size. | ||
| * Increasing this can allow the integrator to be slightly more conservative in selecting the step size | ||
| * - decreasing the number of steps taken but increasing the risk of failing the next step. | ||
| * | ||
|
pelesh marked this conversation as resolved.
Outdated
|
||
| * @note Should be between 0 and 1. | ||
| * | ||
| */ | ||
| RealT fac_min_ = 0.2; | ||
| /** | ||
| * @brief The maximum multiple by which the step size can be multiplied to obtain the new step size. | ||
| * Decreasing this will make the integrator more conservative in selecting the step size - | ||
| * increasing the number of steps taken but decreasing the risk of failing the next step. | ||
| * | ||
| * @note Should be greater than 1. | ||
| * | ||
| */ | ||
| RealT fac_max_ = 5.0; | ||
| /** | ||
| * @brief A "fudge factor" introduced to decrease risk of failing a step. The larger the fudge factor, | ||
| * the more likely steps will fail, but fewer steps will be taken. | ||
| * | ||
| * @note Should be between 0 and 1. | ||
| * | ||
| */ | ||
| RealT fac_scale_ = 0.9; | ||
| } params_; | ||
|
|
||
| public: | ||
| AdaptiveStep(const Parameters& params) | ||
| : params_(params) | ||
| { | ||
| } | ||
|
|
||
| StepControl<RealT> nextStep(RealT err, StepControl<RealT> prev_step, uint8_t method_order) final; | ||
|
|
||
| /** | ||
| * @brief This controller uses error estimates. | ||
| * | ||
| * @see `nextStep()` | ||
| * | ||
| */ | ||
| constexpr bool usesError() const final | ||
| { | ||
| return true; | ||
| } | ||
| }; | ||
| } // namespace Integrator | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| #pragma once | ||
|
|
||
| #include <GridKit/LinearAlgebra/Vector/Vector.hpp> | ||
| #include <GridKit/LinearAlgebra/Vector/VectorHandler.hpp> | ||
| #include <GridKit/ScalarTraits.hpp> | ||
|
|
||
| namespace Integrator | ||
| { | ||
| /** | ||
| * @brief Interface for error norms. Used to calculate the `err` parameter in `StepController::nextStep` based on a residual state error vector. | ||
| * | ||
| */ | ||
| template <class ScalarT, typename IdxT> | ||
| class ErrorNorm | ||
| { | ||
| using State = GridKit::LinearAlgebra::Vector<ScalarT, IdxT>; | ||
| using RealT = typename GridKit::ScalarTraits<ScalarT>::RealT; | ||
|
|
||
| public: | ||
| /** | ||
| * @brief Calculate an error to be used by a step controller. Typically, an error > 1 indicates an error which does not meet tolerances, while | ||
| * an error < 1 indicates an error which meets tolerances. For that reason, tolerances should be included in the calculation of the error. | ||
| * | ||
| * @param err The state error residual being measured. | ||
| * @param y The state that the error was calculated from. Can be used for proper relative error normalization. | ||
| * @param yprev The state from the previous step. Can be used for proper relative error normalization. | ||
| * @param handler A vector handler which can be used to facilitate vector operations. | ||
| * @param memspace The memory space which vector operations should be performed in/. | ||
| * @return The error. | ||
| * | ||
| * @todo Allow this method to fail, since it will likely involve linear algebra calls. | ||
| */ | ||
| virtual RealT errorNorm(State& err, State& y, State& yprev, GridKit::LinearAlgebra::VectorHandler<ScalarT, IdxT>& handler, GridKit::memory::MemorySpace memspace) const = 0; | ||
| }; | ||
|
pelesh marked this conversation as resolved.
Outdated
|
||
| } // namespace Integrator | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| #include "FixedStep.hpp" | ||
|
|
||
| namespace Integrator | ||
| { | ||
| /** | ||
| * @brief Fixed step - accept every step, no matter the error, and keep the step size the same. | ||
| * | ||
| */ | ||
| template <typename RealT> | ||
| StepControl<RealT> FixedStep<RealT>::nextStep([[maybe_unused]] RealT err, StepControl<RealT> prev_step, [[maybe_unused]] uint8_t method_order) | ||
| { | ||
| return StepControl<RealT>{ | ||
| .accept_ = true, | ||
| .step_size_ = prev_step.step_size_, | ||
| }; | ||
| } | ||
|
|
||
| template class FixedStep<double>; | ||
| } // namespace Integrator |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| #pragma once | ||
|
|
||
| #include <GridKit/Solver/Dynamic/StepController.hpp> | ||
|
|
||
| namespace Integrator | ||
| { | ||
| /** | ||
| * @brief A fixed step controller which doesn't change the step size and accepts every step. | ||
| * Useful if you know what time scale your simulation operates on apriori and you're | ||
| * using a method without an embedded error controller. | ||
| * | ||
| * To set the fixed size, set the `Rosenbrock::Parameters::starting_step` parameter. | ||
| * | ||
| */ | ||
| template <typename RealT> | ||
| class FixedStep : public StepController<RealT> | ||
| { | ||
| StepControl<RealT> nextStep(RealT err, StepControl<RealT> prev_step, uint8_t method_order) final; | ||
|
|
||
| /** | ||
| * @brief This controller does not use error estimates. | ||
| * | ||
| * @see `nextStep()` | ||
| * | ||
| */ | ||
| constexpr bool usesError() const final | ||
| { | ||
| return false; | ||
| } | ||
| }; | ||
| } // namespace Integrator |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| #include "InfNorm.hpp" | ||
|
|
||
| #include <format> | ||
|
|
||
| #include <GridKit/Constants.hpp> | ||
|
|
||
| namespace Integrator | ||
| { | ||
| /** | ||
| * @brief Calculate the infinity error norm as | ||
| * | ||
| * \f[e = \max_i\frac{|\hat{e}_i|}{Atol_i + Rtol \cdot \max\{|y_{0i}|, |y_{1i}|\}},\f] | ||
| * | ||
| * where \f(y_0\f) is the initial state, \f(y_1\f) is the next state, and \f(\hat{e}\f) is the estimated error made in calculating | ||
| * the next state (typically \f(\hat{e} = y_1 - \hat{y}_1\f) for some different-order approximation \f(\hat{y}_1\f)). | ||
| * | ||
| * @param err \f(\hat{e}\f) in the above formula. | ||
| * @param y \f(y_1\f) in the above formula. | ||
| * @param yprev \f(y_0\f) in the above formula. | ||
| * @param handler The handler to be used for performing linear algebra operations. | ||
| * @param memspace The memory space to be used for performing linear lagebra operations. | ||
| * @see `Rosenbrock::errorEstimate()` | ||
| */ | ||
| template <class ScalarT, typename IdxT> | ||
| InfNorm<ScalarT, IdxT>::RealT InfNorm<ScalarT, IdxT>::errorNorm(State& err, State& y, State& yprev, GridKit::LinearAlgebra::VectorHandler<ScalarT, IdxT>& handler, GridKit::memory::MemorySpace memspace) const | ||
| { | ||
| if (int err_code = workspace_.out_->copyFromExternal(&err, memspace, memspace)) | ||
| { | ||
| throw std::format("GridKit::LinearAlgebra::Vector::copyFromExternal failed with error code {}", err_code); | ||
| } | ||
| if (int err_code = workspace_.scale_->copyFromExternal(&y, memspace, memspace)) | ||
| { | ||
| throw std::format("GridKit::LinearAlgebra::Vector::copyFromExternal failed with error code {}", err_code); | ||
| } | ||
| if (int err_code = workspace_.yprev_abs_->copyFromExternal(&yprev, memspace, memspace)) | ||
| { | ||
| throw std::format("GridKit::LinearAlgebra::Vector::copyFromExternal failed with error code {}", err_code); | ||
| } | ||
|
|
||
| handler.abs(workspace_.scale_.get(), workspace_.scale_.get(), memspace); | ||
| handler.abs(workspace_.yprev_abs_.get(), workspace_.scale_.get(), memspace); | ||
| handler.max(workspace_.yprev_abs_.get(), workspace_.scale_.get(), workspace_.scale_.get(), memspace); | ||
|
|
||
| // TODO: This scal shouldn't be necessary, but axpy doesn't support scaling the y parameter. In the future, | ||
| // the scaling should be able to be put on the next axpy. | ||
| handler.scal(params_.rel_tol_, workspace_.scale_.get(), memspace); | ||
| handler.axpy(GridKit::ONE<RealT>, params_.abs_tol_.get(), workspace_.scale_.get(), memspace); | ||
| handler.diagSolve(workspace_.scale_.get(), workspace_.out_.get(), memspace); | ||
|
|
||
| return handler.amax(workspace_.out_.get(), memspace); | ||
| } | ||
| } // namespace Integrator |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.