Skip to content
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

Feature/sunadjoint #559

Open
wants to merge 343 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
343 commits
Select commit Hold shift + click to select a range
96503a8
Merge branch 'feature/sunstepper' into feature/sunadjoint
balos1 Aug 20, 2024
3c3a406
move trystep flahs
balos1 Aug 20, 2024
cbfbd79
cleanup
balos1 Aug 20, 2024
e6815d6
add yp arg
balos1 Aug 20, 2024
c7d3f39
add setstoptime
balos1 Aug 20, 2024
fa21304
rename SUNAdjointSolver
balos1 Aug 20, 2024
32832c1
Merge branch 'feature/sunstepper' into feature/sunadjoint
balos1 Aug 20, 2024
f9a2c49
store time with state data
balos1 Aug 21, 2024
9676a70
add reinit function
balos1 Aug 22, 2024
1e5eac7
rename local variables called adj_solver to adj_stepper
balos1 Aug 22, 2024
0b17693
rename adjoint stepper functions to match sunstepper
balos1 Aug 22, 2024
10d9370
add comment
balos1 Aug 22, 2024
532dd48
minor cleanup
balos1 Aug 22, 2024
27fe770
more cleanup
balos1 Aug 22, 2024
b7d7e08
minor fixes
balos1 Aug 22, 2024
b95404b
Merge branch 'feature/sunstepper' into feature/sunadjoint
balos1 Aug 22, 2024
491765f
add missing SetForcing declaration
balos1 Aug 22, 2024
5229e68
Merge branch 'feature/sunstepper' into feature/sunadjoint
balos1 Aug 22, 2024
bc58f0c
Update src/sundials/sundials_stepper.c
balos1 Aug 22, 2024
14be3e5
Merge remote-tracking branch 'origin/feature/sunstepper' into feature…
balos1 Aug 22, 2024
8e7e08a
wip
balos1 Aug 27, 2024
d80ef7a
wip
balos1 Aug 27, 2024
151a271
fix checkpoint throwaway
balos1 Aug 27, 2024
acdaef3
add missing arkode functions
balos1 Aug 27, 2024
dbc0207
Apply suggestions from code review
balos1 Aug 27, 2024
c7a47b4
Apply suggestions from code review
balos1 Aug 27, 2024
f20affe
remove unused constants
balos1 Aug 27, 2024
b797a1a
rename function
balos1 Aug 27, 2024
d3f8bad
add missing function in docs
balos1 Aug 27, 2024
a0348a2
update function names
balos1 Aug 28, 2024
53c9bcc
add docs for the generic classes from other branch and update
balos1 Aug 28, 2024
75d3d66
add matvec transpose docs
balos1 Aug 28, 2024
e262fb6
Revert y prime argument
Steven-Roberts Aug 29, 2024
7881ddc
Fix compilation warnings
Steven-Roberts Aug 29, 2024
5e03e95
Apply formatter
Steven-Roberts Aug 29, 2024
483a53c
add other missing adjoint docs
balos1 Aug 29, 2024
300f8dc
document new sunmemory function
balos1 Aug 29, 2024
25b366d
clarify
balos1 Aug 29, 2024
7d3274d
Merge remote-tracking branch 'origin/feature/sunstepper' into feature…
balos1 Aug 29, 2024
a5c4b32
fix unit test for checkpoint scheme
balos1 Aug 29, 2024
19827f5
copy test to examples
balos1 Aug 29, 2024
ac0adec
add cvodes example
balos1 Aug 31, 2024
0416868
doc clarifications
balos1 Sep 3, 2024
e5af802
get cost at just T
balos1 Sep 3, 2024
e8d0634
add param sensitivites to cvodes example
balos1 Sep 4, 2024
bdcf634
fix comment
balos1 Sep 4, 2024
40ee734
Merge remote-tracking branch 'origin/develop' into feature/sunstepper
balos1 Sep 4, 2024
2c4d1a7
Merge branch 'feature/sunstepper' into feature/sunadjoint
balos1 Sep 4, 2024
a082c34
Fix freeing of fused vectors
Steven-Roberts Sep 4, 2024
3da5408
cleanup headers
balos1 Sep 4, 2024
806774c
remove unused functions
balos1 Sep 4, 2024
f8b19cc
remove unused functions
balos1 Sep 4, 2024
822bb71
remove unused error code
balos1 Sep 4, 2024
4446183
make sundials_datanode private
balos1 Sep 4, 2024
f9c42b2
remove unused header
balos1 Sep 4, 2024
cb6b0fd
Fix unset variable when creating inner stepper
Steven-Roberts Sep 13, 2024
4e0784a
Merge branch 'develop' into feature/sunstepper
Steven-Roberts Sep 13, 2024
7ba3a87
Merge branch 'develop' into feature/sunstepper
gardner48 Sep 16, 2024
5a110e3
apply formatting
gardner48 Sep 16, 2024
32d6f3d
update f2003 interface
gardner48 Sep 16, 2024
5b2b84e
Merge remote-tracking branch 'origin/develop' into feature/sunadjoint
balos1 Sep 16, 2024
97be184
Merge remote-tracking branch 'origin/feature/sunstepper' into feature…
balos1 Sep 16, 2024
df504e1
apply patches
balos1 Sep 16, 2024
9750b43
remove patch files accidentally committed
balos1 Sep 16, 2024
9eb463e
fix all warnings
balos1 Sep 17, 2024
29f698e
update docs
balos1 Sep 17, 2024
986e782
cast int64_t to long long in printf for portability
balos1 Sep 17, 2024
80e9b84
more long long casts
balos1 Sep 17, 2024
b8ba85a
make them unsigned
balos1 Sep 17, 2024
663cdd1
move declaration
balos1 Sep 17, 2024
1d75d3f
no need to check against 0 with size_t
balos1 Sep 17, 2024
4fd8f4a
link sundials_adjoint in unit tests
balos1 Sep 17, 2024
51763f7
link object files
balos1 Sep 17, 2024
5ea7869
cast capacity
balos1 Sep 17, 2024
dad3fbd
fix integer sizes in hashmap
balos1 Sep 18, 2024
1a9c84f
format and spelling
balos1 Sep 18, 2024
8fb60be
remove comparisons to 0
balos1 Sep 18, 2024
9980090
Changelog
Steven-Roberts Sep 18, 2024
b928595
Merge branch 'develop' into feature/sunstepper
Steven-Roberts Sep 18, 2024
d3ca3b8
Doc fixes
Steven-Roberts Sep 19, 2024
1672e09
Reorder implementation specific methods
Steven-Roberts Sep 19, 2024
12e1c2b
fix typo
balos1 Sep 26, 2024
abf37fb
Merge remote-tracking branch 'origin/develop' into feature/sunadjoint
balos1 Sep 26, 2024
08343ce
fix compiler warnings
balos1 Sep 26, 2024
20b6731
Merge remote-tracking branch 'origin/feature/sunstepper' into feature…
balos1 Sep 26, 2024
319d899
ARKstepCreateSUNStepper -> ARKodeCreateSUNStepper
Steven-Roberts Sep 28, 2024
da6e773
Add missing file
Steven-Roberts Sep 28, 2024
a4fc856
Add missing SUNStepper_SetForcing
Steven-Roberts Sep 28, 2024
cd12b43
swig
Steven-Roberts Sep 28, 2024
c7b6cde
Add missing swig file
Steven-Roberts Sep 28, 2024
0c1b84c
Merge branch 'develop' into feature/sunstepper
Steven-Roberts Sep 30, 2024
d43d59a
Merge remote-tracking branch 'origin/feature/sunstepper' into feature…
balos1 Oct 1, 2024
19621a6
post-merge compile fixes
balos1 Oct 1, 2024
cd1891a
add constant for recompute fail
balos1 Oct 1, 2024
9d32e88
Finish updating docs after ARKodeCreateSUNStepper refactor
Steven-Roberts Oct 2, 2024
01eba9e
Merge branch 'develop' into feature/sunstepper
Steven-Roberts Oct 2, 2024
13e444e
Reference ARKodeCreateSUNStepper in the SUNStepper docs
Steven-Roberts Oct 2, 2024
53f30c2
fix hashmap
balos1 Oct 2, 2024
03e26d6
Revert ops check change
Steven-Roberts Oct 2, 2024
9f6baab
Mention MRIStepInnerStepper_CreateFromSUNStepper in changelog
Steven-Roberts Oct 2, 2024
b1a3dad
Typo fixes
Steven-Roberts Oct 2, 2024
50cb5a1
Remove extra include
Steven-Roberts Oct 2, 2024
905f98f
flush logging queues before aborting
balos1 Oct 2, 2024
ec8bbde
more cleanup
Steven-Roberts Oct 2, 2024
2a05e5d
Move function to make diff nicer
Steven-Roberts Oct 2, 2024
92a7f17
Improve error handling for MRI inner stepper wrapping SUNStepper
Steven-Roberts Oct 2, 2024
1461d63
fix problem from reset change
balos1 Oct 2, 2024
fc47959
a couple pr comments addressed
balos1 Oct 2, 2024
7cd59d6
Merge remote-tracking branch 'origin/feature/sunstepper' into feature…
balos1 Oct 2, 2024
d4228ea
merge
balos1 Oct 2, 2024
24114fe
document constant that was added
balos1 Oct 2, 2024
ef116d5
Merge branch 'feature/sunstepper' of github.com:LLNL/sundials into fe…
Steven-Roberts Oct 2, 2024
eadc5ea
Merge remote-tracking branch 'origin/feature/sunstepper' into feature…
balos1 Oct 2, 2024
68e1d1e
Remove SUNStepper_TryStep
Steven-Roberts Oct 2, 2024
e24ed27
Revert trystep changes for xbraid
Steven-Roberts Oct 2, 2024
aa7ccc1
Apply formatter
Steven-Roberts Oct 2, 2024
ec41dfa
Typo fix
Steven-Roberts Oct 3, 2024
106022a
Add missing period
Steven-Roberts Oct 3, 2024
81fa70f
Make return consistent
Steven-Roberts Oct 3, 2024
f00e158
Merge remote-tracking branch 'origin/feature/sunstepper' into feature…
balos1 Oct 3, 2024
173a1cc
format
balos1 Oct 3, 2024
0e85269
run swig
balos1 Oct 3, 2024
9e95184
Update swig/sundials/fsundials_stepper.i
Steven-Roberts Oct 3, 2024
7158562
Doc style fixes
Steven-Roberts Oct 3, 2024
f9dc452
Merge branch 'feature/sunstepper' of github.com:LLNL/sundials into fe…
Steven-Roberts Oct 3, 2024
9cd7450
generate proper swig interface
balos1 Oct 3, 2024
9d332b4
change uint to int
balos1 Oct 3, 2024
e7e25ab
update reset call
balos1 Oct 3, 2024
ca1bc17
fix compiler warnings
balos1 Oct 3, 2024
43e9be0
add profiler statements
balos1 Oct 3, 2024
01515c4
format
balos1 Oct 3, 2024
766c222
remove uses of size_t that were not needed
balos1 Oct 3, 2024
9041421
Merge remote-tracking branch 'origin/feature/sunstepper' into feature…
balos1 Oct 7, 2024
dcb5e7a
fix some more warnings
balos1 Oct 7, 2024
ac20d73
format
balos1 Oct 7, 2024
e8d65b6
fix warning
balos1 Oct 7, 2024
11220bb
more warnings fixed
balos1 Oct 8, 2024
41c468c
Apply suggestions from code review
balos1 Oct 8, 2024
926caa0
Merge branch 'develop' into feature/sunstepper
Steven-Roberts Oct 8, 2024
ef224be
remove .clangd and add it to gitignore
balos1 Oct 9, 2024
ffb0d25
Apply suggestions from code review
balos1 Oct 9, 2024
2d431e4
Apply suggestions from code review
balos1 Oct 9, 2024
93700d3
swig and format
balos1 Oct 9, 2024
09d0fe1
update docs
balos1 Oct 9, 2024
86e388d
use x.y.z
balos1 Oct 9, 2024
d88dc73
use SUN_RCONST
balos1 Oct 9, 2024
e628c68
fix commented out timer code
balos1 Oct 9, 2024
ec024dc
document naming convention for stlvector
balos1 Oct 9, 2024
57b74d2
fixing linking errors on windows
balos1 Oct 9, 2024
8ef4e82
add obj back sunmemsys
balos1 Oct 9, 2024
a17092a
Apply suggestions from code review
Steven-Roberts Oct 14, 2024
13c7c63
Fix copy paste error in MRIStepInnerStepper_CreateFromSUNStepper example
Steven-Roberts Oct 14, 2024
c333f57
Only call SUNStepper_SetForcingFn if stepper provides setforcing func…
Steven-Roberts Oct 14, 2024
d4846b3
Document support for forcing
Steven-Roberts Oct 14, 2024
cb495dc
Merge branch 'develop' into feature/sunstepper
Steven-Roberts Oct 14, 2024
9641457
Apply formatter
Steven-Roberts Oct 14, 2024
6d7c459
Merge remote-tracking branch 'origin/feature/sunstepper' into feature…
balos1 Oct 18, 2024
c52f9dc
comment out no-stages
balos1 Oct 18, 2024
f9b48ad
doc fixes
balos1 Oct 19, 2024
794327b
spelling
balos1 Oct 21, 2024
f62a1a8
simplify indexing
balos1 Oct 21, 2024
cfaab51
define ASA
balos1 Oct 21, 2024
d00e769
uncomment fsal code
balos1 Oct 21, 2024
5210ca7
guard subvector access
balos1 Oct 21, 2024
2c196f6
clarify subvector size
balos1 Oct 21, 2024
6d62b97
fix inconsistent notation
balos1 Oct 21, 2024
3a8920b
fix missing declaration
balos1 Oct 21, 2024
77abed5
update checkpoint_start_idx to int64_t
balos1 Oct 21, 2024
02aa271
fix param description
balos1 Oct 21, 2024
bba7343
couple of small doc fixes
balos1 Oct 21, 2024
9a8d422
Merge branch 'develop' into feature/sunstepper
Steven-Roberts Oct 22, 2024
0549afd
Apply suggestions from code review
Steven-Roberts Oct 23, 2024
5c3cbf6
Add missing header
Steven-Roberts Oct 25, 2024
06e6875
Remove step_supports_forcing
Steven-Roberts Oct 25, 2024
d29b95e
Revert formatting change
Steven-Roberts Oct 25, 2024
cb456e2
Add public SUNStepper_FullRhs function
Steven-Roberts Oct 25, 2024
65eafd4
Apply suggestions from code review for evolve and one step functions
Steven-Roberts Oct 25, 2024
dcc74c8
Use backticks for arguments
Steven-Roberts Oct 25, 2024
e3a9a42
Remove unused SUNStepper_OneStep
Steven-Roberts Oct 25, 2024
127ff2d
Remove t0 arg from SUNStepper_Evolve
Steven-Roberts Oct 25, 2024
9f483e0
Formatter and swig
Steven-Roberts Oct 25, 2024
51406bc
Moved duplicated SUNStepper overview to shared folder
Steven-Roberts Oct 25, 2024
3c6844b
Merge remote-tracking branch 'origin/feature/sunstepper' into feature…
balos1 Oct 28, 2024
f6b7866
Allow users to specify custom destroy function
Steven-Roberts Oct 28, 2024
10869c6
Add missing SUNStepper_SetDestroyFn definition
Steven-Roberts Oct 28, 2024
ae18e60
NULL destroy operation on creation
Steven-Roberts Oct 28, 2024
4893e19
Make argument names consistent in docs and header
Steven-Roberts Oct 28, 2024
4d238f0
Fix anonymous type linking error
Steven-Roberts Oct 29, 2024
42b1f59
Fix copy paste error
Steven-Roberts Oct 29, 2024
fb6a34e
address documentation issues from review
balos1 Oct 29, 2024
8d42749
remove commented out code
balos1 Oct 29, 2024
91900f5
Add arkProcessError calls to ARKodeCreateSUNStepper
Steven-Roberts Oct 29, 2024
3a26598
Rename SUNStepper Jacobian typedefs
Steven-Roberts Oct 29, 2024
ad41aa0
Add mode argument back to SUNStepper full RHS
Steven-Roberts Oct 29, 2024
76e4537
Update docs for full RHS mode
Steven-Roberts Oct 29, 2024
2e7859e
Swig and format
Steven-Roberts Oct 29, 2024
b72dc5a
Merge remote-tracking branch 'origin/feature/sunstepper' into feature…
balos1 Oct 30, 2024
15b0b7c
add onestep function back
balos1 Oct 30, 2024
3f175d0
Merge remote-tracking branch 'origin/feature/sunstepper' into feature…
balos1 Oct 30, 2024
b71fd0a
post merge fixes
balos1 Oct 30, 2024
da261e7
fix unit tests
balos1 Oct 30, 2024
c84f8d1
apply swig and format
balos1 Oct 30, 2024
aecc502
Merge remote-tracking branch 'origin/feature/sunstepper' into feature…
balos1 Oct 30, 2024
068fbd7
Merge branch 'develop' into feature/sunstepper
balos1 Oct 30, 2024
697e870
Merge remote-tracking branch 'origin/feature/sunstepper' into feature…
balos1 Oct 30, 2024
9973277
move arkInit and ARKodePrintMem back
balos1 Oct 30, 2024
015fc55
typo
balos1 Oct 30, 2024
b31a678
remove TTYPE undef from header
balos1 Oct 30, 2024
44e331e
checck node data in unit test
balos1 Oct 30, 2024
633062b
remove stop_reason in favor of last_flag
balos1 Oct 30, 2024
762dc3f
SUNAdjointCheckpointScheme doc clarification
balos1 Oct 31, 2024
c77132f
add generic ASA overview in ASA section
balos1 Oct 31, 2024
7d17534
rename matvectranspose
balos1 Oct 31, 2024
b7f4794
fix find/replace errors from changing 'basic' to 'fixed'
balos1 Oct 31, 2024
5481820
add band matrix transpose
balos1 Nov 1, 2024
e144a9a
format
balos1 Nov 1, 2024
23d2cb0
format
balos1 Nov 1, 2024
9785909
destroy AT
balos1 Nov 1, 2024
a8811d5
add mattransposevec for sparse matrix
balos1 Nov 1, 2024
758287c
fix more basic/fixed find-replace errors
balos1 Nov 1, 2024
cc49a54
update swig readme to remove reference to old commit
balos1 Nov 1, 2024
80bb41d
format
balos1 Nov 1, 2024
c0a0783
Apply suggestions from code review
balos1 Nov 1, 2024
e0ecd2c
fix spelling
balos1 Nov 1, 2024
4acf8ca
rerun swig
balos1 Nov 1, 2024
164933a
format
balos1 Nov 1, 2024
7237b2c
fix fsal indexing
balos1 Nov 1, 2024
1b18e8c
check error codes
balos1 Nov 1, 2024
f2a9b0d
swig
balos1 Nov 1, 2024
a8ab522
Merge branch 'develop' into feature/sunstepper
balos1 Nov 1, 2024
06e99e3
format
balos1 Nov 2, 2024
eba5765
Basic --> Fixed
balos1 Nov 2, 2024
d386417
reorder tests
balos1 Nov 2, 2024
6cf811a
switch order
balos1 Nov 6, 2024
3b7e733
Merge remote-tracking branch 'origin/feature/sunstepper' into feature…
balos1 Nov 6, 2024
6ff3a78
remove unnecessary if do_adjoint
balos1 Nov 6, 2024
59ea02e
fix matrix compatability check
balos1 Nov 6, 2024
5a5fd64
bump answers
balos1 Nov 6, 2024
b2b8254
loosen tolerances for single precision
balos1 Nov 6, 2024
0be20be
support integration in both directions
balos1 Nov 7, 2024
0e2ae77
fix comments
balos1 Nov 8, 2024
2560fa7
factor out lotka volterra functions into their own header
balos1 Nov 8, 2024
059046b
rename test file
balos1 Nov 8, 2024
3b7d7f3
rename test file
balos1 Nov 8, 2024
bd36a0f
bump answers
balos1 Nov 8, 2024
5526f03
make unit test pass/fail
balos1 Nov 9, 2024
93a9957
format
balos1 Nov 11, 2024
5a78a2a
check backwards answer
balos1 Nov 11, 2024
066fc3c
update output files for new cost function
balos1 Nov 12, 2024
abe96c2
comment out backwards adjoint check
balos1 Nov 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
/.pydevproject
.vscode
compile_commands.json
.clangd

# custom test environment setup script
/test/env/env.sh
Expand Down Expand Up @@ -59,6 +60,7 @@ compile_commands.json
/doc/*/*.log

# directories created by/for Sphinx documentation
/doc/.venv
/doc/.pyvenv
/doc/*/guide/build/
/doc/*/examples/build/
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

### New Features and Enhancements

Added the `SUNStepper` base class to represent a generic solution procedure for
IVPs. This is used by the SplittingStep and ForcingStep modules of ARKODE. A
SUNStepper can be created from an ARKODE memory block with the new function
`ARKodeCreateSUNStepper`. To enable interoperability with `MRIStepInnerStepper`,
the function `MRIStepInnerStepper_CreateFromSUNStepper` was added.

The following DIRK schemes now have coefficients accurate to quad precision:
* `ARKODE_BILLINGTON_3_3_2`
* `ARKODE_KVAERNO_4_2_3`
Expand Down
5 changes: 5 additions & 0 deletions doc/arkode/guide/source/Constants.rst
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,11 @@ contains the ARKODE output constants.
| :index:`ARK_STEPPER_UNSUPPORTED` | -48 | An operation was not supported by the current |
| | | time-stepping module. |
+-------------------------------------+------+------------------------------------------------------------+
| :index:`ARK_SUNSTEPPER_ERR` | -49 | An error occurred in the SUNStepper module. |
+-------------------------------------+------+------------------------------------------------------------+
| :index:`ARK_ADJ_RECOMPUTE_FAIL` | -50 | An occurred recomputing steps during the adjoint |
| | | integration. |
+-------------------------------------+------+------------------------------------------------------------+
| :index:`ARK_UNRECOGNIZED_ERROR` | -99 | An unknown error was encountered. |
+-------------------------------------+------+------------------------------------------------------------+
| |
Expand Down
108 changes: 108 additions & 0 deletions doc/arkode/guide/source/Mathematics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2126,3 +2126,111 @@ by :math:`\eta_\text{rf}`.

For more information on utilizing relaxation Runge--Kutta methods, see
:numref:`ARKODE.Usage.Relaxation`.


.. _ARKODE.Mathematics.ASA:

Adjoint Sensitivity Analysis
============================

Consider :eq:`ARKODE_IVP_simple_explicit`, but where the ODE also depends on some parameters
:math:`p` (that is, we have :math:`f(t,y,p)`). Now, suppose we have a functional :math:`g(y(t_f),p)`
for which we would like to compute the gradients :math:`\partial g(y(t_f),p)/\partial y(t_0)`
and/or :math:`\partial g(y(t_f),p)/\partial p`. This most often arises in the form of an
optimization problem such as

.. math::
\min_{\xi} \bar{\Psi}(\xi) = g(y(t_f), p)
:label: ARKODE_OPTIMIZATION_PROBLEM

where :math:`\xi \subset \{y(t_0), p\}`. The adjoint method is one approach to obtaining the
gradients that is particularly efficient when there are relatively few functionals and a large
number of parameters. While :ref:`CVODES <CVODES.Mathematics.ASA>` and
:ref:`IDAS <IDAS.Mathematics.ASA>` *continuous* adjoint methods
(differentiate-then-discretize), ARKODE provides *discrete* adjoint methods
(discretize-then-differentiate). For the continuous approach, we derive and solve the adjoint ODE
backwards in time

.. math::
\lambda'(t) &= -f_y^T(t, y, p) \lambda,\quad \lambda(t_F) = g_y^T(y(t_f), p), \\
\mu'(t) &= -f_p^T(t, y, p) \mu,\quad \mu(t_F) = g_p^T(y(t_f), p), \quad t_f \geq t \geq t_0, \\
:label: ARKODE_CONTINUOUS_ADJOINT_ODE

where :math:`\lambda(t) \in \mathbb{R}^N`, :math:`\mu(t) \in \mathbb{R}^{N_s}`
:math:`f_y \equiv \partial f/\partial y \in \mathbb{R}^{N \times N}` is the Jacobian with respect to the dependent variable,
and :math:`f_p \equiv \partial f/\partial p \in \mathbb{R}^{N \times N_s}` is the Jacobian with respect to the parameters
(:math:`N` is the size of the original ODE, :math:`N_s` is the number of parameters).
When solved with a numerical time integration scheme, the solution to the continuous adjoint ODE
are numerical approximations of the continuous adjoint sensitivities

.. math::
\lambda(t_0) \approx g_y^T(y(t_0), p),\quad \mu(t_0) \approx g_p^T(y(t_0), p)
:label: ARKODE_CONTINUOUS_ADJOINT_SOLUTION

For the discrete adjoint approach, we first numerically discretize the original ODE :eq:`ARKODE_IVP_simple_explicit`.
In the context of ARKODE, this is done with a one-step time integration scheme :math:`\varphi` so that

.. math::
y_0 = y(t_0),\quad y_n = \varphi(y_{n-1}).
:label: ARKODE_DISCRETE_ODE

Reformulating the optimization problem for the discrete case, we have

.. math::
\min_{\xi} \Psi(\xi) = g(y_n, p)
:label: ARKODE_DISCRETE_OPTIMIZATION_PROBLEM

The gradients of :eq:`ARKODE_DISCRETE_OPTIMIZATION_PROBLEM` can be computed using the transposed chain
rule backwards in time to obtain the discete adjoint variables :math:`\lambda_n, \lambda_{n-1}, \cdots, \lambda_0`
and :math:`\mu_n, \mu_{n-1}, \cdots, \mu_0`,

.. math::
\lambda_n &= g_y^T(y_n, p), \quad \lambda_k = \left(\frac{\partial \varphi}{\partial y_k}(y_k, p)\right)^T \lambda_{k+1} \\
\mu_n &= g_p^T(y_n, p), \quad \mu_k = \left(\frac{\partial \varphi}{\partial p}(y_k, p)\right)^T \lambda_{k+1},
\quad k = n - 1, \cdots, 0.
:label: ARKODE_DISCRETE_ADJOINT

The solution of the discrete adjoint equations :eq:`ARKODE_DISCRETE_ADJOINT` is the sensitivities of the discrete cost function
:eq:`ARKODE_DISCRETE_OPTIMIZATION_PROBLEM` with respect to changes in the discretized ODE :eq:`ARKODE_DISCRETE_ODE`.

.. math::
\lambda_0 = g_y^T(y_0, p), \quad \mu_0 = g_p^T(y_0, p).
:label: ARKODE_DISCRETE_ADJOINT_SOLUTION

Given an s-stage explicit Runge--Kutta method (as in :eq:`ARKODE_ERK`, but without the embedding), the discrete adjoint
to compute :math:`\lambda_n` and :math:`\mu_n` starting from :math:`\lambda_{n+1}` and
:math:`\mu_{n+1}` is given by

.. math::
\Lambda_i &= h_n f_y^T(t_{n,i}, z_i) \left(b_i \lambda_{n+1} + \sum_{j=i+1}^s a_{j,i}
\Lambda_j \right), \quad \quad i = s, \dots, 1,\\
\nu_i &= h_n f_p^T(t_{n,i}, z_i, p) \left(b_i \lambda_{n+1} + \sum_{j=i}^{s} a_{ji} \Lambda_j \right), \\
\lambda_n &= \lambda_{n+1} + \sum_{j=1}^{s} \Lambda_j, \\
\mu_n &= \mu_{n+1} + \sum_{j=1}^{s} \nu_j.
:label: ARKODE_ERK_ADJOINT

For more information on performing discrete adjoint sensitivity analysis using ARKODE see,
:numref:`ARKODE.Usage.ARKStep.ASA`.

For a detailed derivation of the discrete adjoint methods see :cite:p:`hager2000runge,sanduDiscrete2006`.
For a detailed derivation of the continuous adjoint method see :ref:`CVODES <CVODES.Mathematics.ASA>`,
or :cite:p:`CLPS:03`.


Discrete vs. Continuous Adjoint Method
--------------------------------------

It is understood that the continuous adjoint method can be problematic in the context of
optimization problems because the continuous adjoint method provides an approximation to the
gradient of a continuous cost function while the optimizer is expecting the gradient of the discrete
cost function. The discrepancy means that the optimizer can fail to converge further once it is near
a local minimum :cite:p:`giles2000introduction`. On the other hand, the discrete adjoint method
provides the exact gradient of the discrete cost function allowing the optimizer to fully converge.
Consequently, the discrete adjoint method is often preferable in optimization despite its own
drawbacks -- such as its (relatively) increased memory usage and the possible introduction of
unphysical computational modes :cite:p:`sirkes1997finite`. This is not to say that the discrete
adjoint approach is always the better choice over the continuous adjoint approach in optimization.
Computational efficiency and stability of one approach over the other can be both problem and method
dependent. Section 8 in the paper :cite:p:`rackauckas2020universal` discusses the tradeoffs further
and provides numerous references that may help inform users in choosing between the discrete and
continuous adjoint approaches.
58 changes: 58 additions & 0 deletions doc/arkode/guide/source/Usage/ARKStep/ASA.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
.. _ARKODE.Usage.ARKStep.ASA:

Adjoint Sensitivity Analysis
============================

The previous sections discuss using ARKStep for the integration of forward ODE models.
This section discusses how to use ARKStep for adjoint sensitivity analysis as introduced
in :numref:`ARKODE.Mathematics.ASA`. To use ARKStep for adjoint sensitivity analysis (ASA), users simply setup the forward
integration as usual (following :numref:`ARKODE.Usage.Skeleton`) with one exception:
a :c:type:`SUNAdjointCheckpointScheme` object must be created and passed to
:c:func:`ARKodeSetAdjointCheckpointScheme` before the call to the :c:func:`ARKodeEvolve`
function. After the forward model integration code, a :c:type:`SUNAdjointStepper` object
can be created for the adjoint model integration by calling :c:func:`ARKStepCreateAdjointStepper`.
The code snippet below demonstrates these steps in brief and the example code
``examples/arkode/C_serial/ark_lotka_volterra_asa.c`` demonstrates these steps in detail.

.. code-block:: C

// 1. Create a SUNAdjointCheckpointScheme object

// 2. Setup ARKStep for forward integration

// 3. Attach the SUNAdjointCheckpointScheme

// 4. Evolve the forward model

// 5. Create the SUNAdjointStepper

// 6. Setup the adjoint model

// 7. Evolve the adjoint model

// 8. Cleanup
Comment on lines +19 to +33
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these supposed to be "code snippets" (as stated in the paragraph above)?




User Callable Functions
-----------------------

This section describes ARKStep-specific user-callable functions for performing
adjoint sensitivity analysis with methods with ARKStep.

.. c:function:: int ARKStepCreateAdjointStepper(void* arkode_mem, N_Vector sf, SUNAdjointStepper* adj_stepper_ptr)

Creates a :c:type:`SUNAdjointStepper` object compatible with the provided ARKStep instance for
integrating the adjoint sensitivity system :eq:`ARKODE_DISCRETE_ADJOINT`.

:param arkode_mem: a pointer to the ARKStep memory block.
:param sf: the sensitivity vector holding the adjoint system terminal condition.
This must be an instance of the ManyVector ``N_Vector`` implementation with at
least one subvector (depending on if sensitivities to parameters should be computed).
The first subvector must be :math:`\partial g_y(y(t_f)) \in \mathbb{R}^N`. If sensitivities to parameters should be computed, then the second subvector must be :math:`g_p(y(t_f), p) \in \mathbb{R}^{N_s}`.
:param adj_stepper_ptr: the newly created :c:type:`SUNAdjointStepper` object.

:return:
* ``ARK_SUCCESS`` if successful
* ``ARK_MEM_FAIL`` if a memory allocation failed
* ``ARK_ILL_INPUT`` if an argument has an illegal value.
5 changes: 5 additions & 0 deletions doc/arkode/guide/source/Usage/ARKStep/User_callable.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ ARKStep supports *all categories*:
* non-identity mass matrices
* relaxation Runge--Kutta methods

ARKStep also has forcing function support when converted to a
:c:type:`SUNStepper` or :c:type:`MRIStepInnerStepper`. See
:c:func:`ARKodeCreateSUNStepper` and :c:func:`ARKStepCreateMRIStepInnerStepper`
for additional details.


.. _ARKODE.Usage.ARKStep.Initialization:

Expand Down
1 change: 1 addition & 0 deletions doc/arkode/guide/source/Usage/ARKStep/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ are specific to ARKStep.
User_callable
Relaxation
XBraid
ASA
4 changes: 4 additions & 0 deletions doc/arkode/guide/source/Usage/ERKStep/User_callable.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ ERKStep supports the following categories:
* temporal adaptivity
* relaxation Runge--Kutta methods

ERKStep does not have forcing function support when converted to a
:c:type:`SUNStepper` or :c:type:`MRIStepInnerStepper`. See
:c:func:`ARKodeCreateSUNStepper` and :c:func:`ARKStepCreateMRIStepInnerStepper`
for additional details.


.. _ARKODE.Usage.ERKStep.Initialization:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,32 @@ Creating and Destroying an Object
for details on how to attach member data and method function pointers.


.. c:function:: int MRIStepInnerStepper_CreateFromSUNStepper(SUNStepper sunstepper, MRIStepInnerStepper* stepper)

This utility function wraps a :c:type:`SUNStepper` as an
:c:type:`MRIStepInnerStepper`.

:param sunctx: the SUNDIALS simulation context.
:param sunstepper: the c:type:`SUNStepper` to wrap.
:param stepper: a pointer to an MRI inner stepper object.

:retval ARK_SUCCESS: if successful
:retval ARK_MEM_FAIL: if a memory allocation error occurs

**Example usage:**

.. code-block:: C

SUNStepper sunstepper = NULL;
SUNStepper_Create(ctx, &sunstepper);
/* Attach content and functions to the SUNStepper... */

MRIStepInnerStepper inner_stepper = NULL;
flag = MRIStepInnerStepper_CreateFromSUNStepper(sunstepper, &inner_stepper);

.. versionadded:: x.y.z


.. c:function:: int MRIStepInnerStepper_Free(MRIStepInnerStepper *stepper)

This function destroys an :c:type:`MRIStepInnerStepper` object.
Expand Down
4 changes: 4 additions & 0 deletions doc/arkode/guide/source/Usage/MRIStep/User_callable.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ MRIStep supports the following categories:

* implicit nonlinear and/or linear solvers

MRIStep does not have forcing function support when converted to a
:c:type:`SUNStepper` or :c:type:`MRIStepInnerStepper`. See
:c:func:`ARKodeCreateSUNStepper` and :c:func:`ARKStepCreateMRIStepInnerStepper`
for additional details.


.. _ARKODE.Usage.MRIStep.Initialization:
Expand Down
5 changes: 5 additions & 0 deletions doc/arkode/guide/source/Usage/SPRKStep/User_callable.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ SPRKStep supports only the basic set of user-callable functions, and
does not support any of the restricted groups (time adaptivity, implicit
solvers, etc.).

SPRKStep does not have forcing function support when converted to a
:c:type:`SUNStepper` or :c:type:`MRIStepInnerStepper`. See
:c:func:`ARKodeCreateSUNStepper` and :c:func:`ARKStepCreateMRIStepInnerStepper`
for additional details.


.. _ARKODE.Usage.SPRKStep.Initialization:

Expand Down
Loading
Loading