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 183 commits into
base: feature/sunstepper
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
183 commits
Select commit Hold shift + click to select a range
9a7d535
move MRIStepInnerStepper to SUNStepper
balos1 Apr 29, 2024
046f415
revert arkode changes
balos1 May 6, 2024
db94c67
start developing datanode
balos1 Apr 5, 2024
016245d
finish spec of basic datanode
balos1 Apr 5, 2024
8c4da04
separate generic and mmap implementation
balos1 Apr 9, 2024
a0d5f45
add include guards
balos1 Apr 9, 2024
a06b948
object node get/add works
balos1 Apr 9, 2024
58972db
implement named children
balos1 Apr 10, 2024
33fe88b
add a implementation of a vector/arraylist
balos1 Apr 10, 2024
d62ea21
fix arraylist and hashmap use of it
balos1 Apr 11, 2024
08c5d0a
implement destroy for mmap node
balos1 Apr 15, 2024
1f1a9cc
change mmap to inmem
balos1 Apr 15, 2024
40c8a4e
update file names
balos1 Apr 15, 2024
8414874
add clangd file
balos1 Apr 25, 2024
5deb32b
update functions that use hashmap that were mpi only
balos1 Apr 25, 2024
05b66ef
comment clean up
balos1 Apr 25, 2024
252c209
skeleton for checkpoint scheme
balos1 Apr 25, 2024
7098fae
adjoint solver skeleton
balos1 Apr 29, 2024
5b967be
progress
balos1 May 6, 2024
98345fc
sunadjoint skeleton and test problem with arkode setup
balos1 May 9, 2024
3c70ba3
more progress with prototyping SUNAdjointSolver and its interface to …
balos1 May 10, 2024
f9058c0
post-merge fix
balos1 May 24, 2024
b73bb93
add ARKStepCreateAdjointSolver routine
balos1 May 14, 2024
176f6ef
fleshed out sunadjointsolver
balos1 May 14, 2024
eb73930
add transpose and matvectranpose to sunmatrix
balos1 May 24, 2024
775e494
implement new sunmatrix function in dense sunmatrix
balos1 May 24, 2024
8cd39c5
further progress in arkstep
balos1 May 25, 2024
ef39ccc
add transpose test
balos1 May 31, 2024
686502d
fix transpose matvec
balos1 Jun 4, 2024
d5fc523
use manyvector
balos1 Jun 4, 2024
1b0214e
implement takestep routine
balos1 Jun 5, 2024
f60605f
print error before abort message in SUNAbortErrHandlerFn
balos1 Jun 6, 2024
c8cee39
setup terminal condition
balos1 Jun 6, 2024
0c2d6e1
fix fatal logger error message
balos1 Jun 7, 2024
fb10d2c
fix SUNLogErrHandlerFn
balos1 Jun 7, 2024
3f3a089
hashmap fixes
balos1 Jun 7, 2024
8f3be15
debugging checkpoint scheme
balos1 Jun 7, 2024
5bcedb7
update math sections of the docs
balos1 Jun 10, 2024
97dccfe
restructure
balos1 Jun 10, 2024
cf179bb
add adjointcheckpointscheme unit test
balos1 Jun 11, 2024
1b6d949
format
balos1 Jun 11, 2024
e713b75
fix hash key error
balos1 Jun 11, 2024
01f3fc8
basic checkpoint scheme working in unit test
balos1 Jun 13, 2024
03fd436
fix struct name
balos1 Jun 13, 2024
10fe266
change ArrayList to StlVector
balos1 Jun 13, 2024
71fb1a6
fix header name convention
balos1 Jun 13, 2024
dc94972
reorganize sundataode
balos1 Jun 13, 2024
0d05e04
use sunmemoryhelper
balos1 Jun 14, 2024
34ab37e
fix datanode segfault temporarily
balos1 Jun 14, 2024
16f4abd
reworked destructors and mem helper usage
balos1 Jun 14, 2024
1a996e1
post rebase fix
balos1 Jul 1, 2024
a9a99a6
more post rebase fixes
balos1 Jul 1, 2024
58657ce
update
balos1 Jul 1, 2024
0fdbc94
Merge branch 'feature/sunstepper' into feature/sunadjoint
balos1 Jul 29, 2024
e47b231
fix build
balos1 Jul 29, 2024
de54b5f
wip loading checkpoint for initial condition breaks things
balos1 Jul 29, 2024
69b6384
fix misaligned checkpoints
balos1 Aug 1, 2024
de6098c
matching my julia prototype
balos1 Aug 6, 2024
665e82f
answer matches petsc ts
balos1 Aug 7, 2024
94fc60d
sensitivies for both ic and params matching
balos1 Aug 8, 2024
ae569f8
remove petsc example
balos1 Aug 8, 2024
ee16765
improvements to test
balos1 Aug 8, 2024
0411579
WIP handle missing checkpoint
balos1 Aug 12, 2024
b012d53
fix jvp and vjp
balos1 Aug 12, 2024
e3e66cb
remove num_cost parameter
balos1 Aug 12, 2024
244731c
rename vars
balos1 Aug 12, 2024
03d12bc
WIP: recomputing missing checkpoint
balos1 Aug 13, 2024
7868c52
[wip] recomputing missing checkpoints
balos1 Aug 15, 2024
274814e
wip: recompute step working in some cases but not with all combos of …
balos1 Aug 16, 2024
aee58a9
works when checkpoints are not deleted
balos1 Aug 19, 2024
cc72d5d
checkpointing and recomputing fixed and working with all orders and i…
balos1 Aug 19, 2024
b856458
parse command line args
balos1 Aug 19, 2024
5f5bd2a
add a printallstats for adjoint solver
balos1 Aug 19, 2024
f66c760
replace setinterval with setdense
balos1 Aug 19, 2024
31a944e
move data stride into SUNMemory
balos1 Aug 20, 2024
49da6e7
remove data_stride from node
balos1 Aug 20, 2024
cc8a94c
enable hashmap resize
balos1 Aug 20, 2024
9360eea
add mem type argument for setdata
balos1 Aug 20, 2024
2c7916e
fix hashmap test
balos1 Aug 20, 2024
ec0170a
minor fixes
balos1 Aug 20, 2024
96503a8
Merge branch 'feature/sunstepper' into feature/sunadjoint
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
b95404b
Merge branch 'feature/sunstepper' into feature/sunadjoint
balos1 Aug 22, 2024
5229e68
Merge branch 'feature/sunstepper' into feature/sunadjoint
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
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
2c4d1a7
Merge branch 'feature/sunstepper' into feature/sunadjoint
balos1 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
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
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
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
53f30c2
fix hashmap
balos1 Oct 2, 2024
905f98f
flush logging queues before aborting
balos1 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
eadc5ea
Merge remote-tracking branch 'origin/feature/sunstepper' into feature…
balos1 Oct 2, 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
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
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
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
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
3 changes: 3 additions & 0 deletions doc/arkode/guide/source/Constants.rst
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,9 @@ contains the ARKODE output constants.
+-------------------------------------+------+------------------------------------------------------------+
| :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
109 changes: 109 additions & 0 deletions doc/arkode/guide/source/Mathematics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2126,3 +2126,112 @@ 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 integation 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_n(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 = 0, \quad k = n - 1, \cdots 0, \\
\lambda_{\ell} &= \lambda_{\ell} + \left( \frac{\partial \varphi_k}{\partial y_{\ell}} (y_{k-1}) \right)^T \lambda_k,\quad \ell = k-1, \cdots, 0, \quad k = n, \cdots 0. \\
\mu_n &= g_p^T(y_n, p), \quad \mu_k = 0, \quad k = n - 1, \cdots 0, \\
\mu_{\ell} &= \mu_{\ell} + \left( \frac{\partial \varphi_k}{\partial p} (y_{k-1}) \right)^T \mu_k,\quad \ell = k-1, \cdots, 0, \quad k = n, \cdots 0.
:label: ARKODE_DISCRETE_ADJOINT

The solution of the discrete adjoint equations :eq:`ARKODE_DISCRETE_ADJOINT` is the sensitivitrwes 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.
57 changes: 57 additions & 0 deletions doc/arkode/guide/source/Usage/ARKStep/ASA.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
.. _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 ASA, users simply setup the forward
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think ASA is defined yet. I only see it in the SUNAdjoint section which is much later in the docs

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 two subvectors.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Note to self: If you only need sensitivities w.r.t. initial conditions, can the subvector for parameters have length 0?

The first subvector must hold :math:`\partial g/\partial y |_{t=t_f} \in \mathbb{R}^N` and the second subvector must hold :math:`\partial g / \partial p |_{t=t_f} \in \mathbb{R}^d`.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Here, $d$ is used as the # of parameters, but earlier is was $N_s$ (although I suggested $N_p$)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also inconsistent usage of evaluation bar vs time as a argument to the Jacobian function

: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.
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
75 changes: 52 additions & 23 deletions doc/arkode/guide/source/Usage/User_callable.rst
Original file line number Diff line number Diff line change
Expand Up @@ -867,29 +867,30 @@ Optional inputs for ARKODE

.. cssclass:: table-bordered

================================================ ======================================= =======================
Optional input Function name Default
================================================ ======================================= =======================
Return ARKODE parameters to their defaults :c:func:`ARKodeSetDefaults` internal
Set integrator method order :c:func:`ARKodeSetOrder` 4
Set dense output interpolation type (SPRKStep) :c:func:`ARKodeSetInterpolantType` ``ARK_INTERP_LAGRANGE``
Set dense output interpolation type (others) :c:func:`ARKodeSetInterpolantType` ``ARK_INTERP_HERMITE``
Set dense output polynomial degree :c:func:`ARKodeSetInterpolantDegree` 5
Disable time step adaptivity (fixed-step mode) :c:func:`ARKodeSetFixedStep` disabled
Supply an initial step size to attempt :c:func:`ARKodeSetInitStep` estimated
Maximum no. of warnings for :math:`t_n+h = t_n` :c:func:`ARKodeSetMaxHnilWarns` 10
Maximum no. of internal steps before *tout* :c:func:`ARKodeSetMaxNumSteps` 500
Maximum absolute step size :c:func:`ARKodeSetMaxStep` :math:`\infty`
Minimum absolute step size :c:func:`ARKodeSetMinStep` 0.0
Set a value for :math:`t_{stop}` :c:func:`ARKodeSetStopTime` undefined
Interpolate at :math:`t_{stop}` :c:func:`ARKodeSetInterpolateStopTime` ``SUNFALSE``
Disable the stop time :c:func:`ARKodeClearStopTime` N/A
Supply a pointer for user data :c:func:`ARKodeSetUserData` ``NULL``
Maximum no. of ARKODE error test failures :c:func:`ARKodeSetMaxErrTestFails` 7
Set inequality constraints on solution :c:func:`ARKodeSetConstraints` ``NULL``
Set max number of constraint failures :c:func:`ARKodeSetMaxNumConstrFails` 10
================================================ ======================================= =======================

================================================= ========================================== =======================
Optional input Function name Default
================================================= ========================================== =======================
Return ARKODE parameters to their defaults :c:func:`ARKodeSetDefaults` internal
Set integrator method order :c:func:`ARKodeSetOrder` 4
Set dense output interpolation type (SPRKStep) :c:func:`ARKodeSetInterpolantType` ``ARK_INTERP_LAGRANGE``
Set dense output interpolation type (others) :c:func:`ARKodeSetInterpolantType` ``ARK_INTERP_HERMITE``
Set dense output polynomial degree :c:func:`ARKodeSetInterpolantDegree` 5
Disable time step adaptivity (fixed-step mode) :c:func:`ARKodeSetFixedStep` disabled
Supply an initial step size to attempt :c:func:`ARKodeSetInitStep` estimated
Maximum no. of warnings for :math:`t_n+h = t_n` :c:func:`ARKodeSetMaxHnilWarns` 10
Maximum no. of internal steps before *tout* :c:func:`ARKodeSetMaxNumSteps` 500
Maximum absolute step size :c:func:`ARKodeSetMaxStep` :math:`\infty`
Minimum absolute step size :c:func:`ARKodeSetMinStep` 0.0
Set a value for :math:`t_{stop}` :c:func:`ARKodeSetStopTime` undefined
Interpolate at :math:`t_{stop}` :c:func:`ARKodeSetInterpolateStopTime` ``SUNFALSE``
Disable the stop time :c:func:`ARKodeClearStopTime` N/A
Supply a pointer for user data :c:func:`ARKodeSetUserData` ``NULL``
Maximum no. of ARKODE error test failures :c:func:`ARKodeSetMaxErrTestFails` 7
Set inequality constraints on solution :c:func:`ARKodeSetConstraints` ``NULL``
Set max number of constraint failures :c:func:`ARKodeSetMaxNumConstrFails` 10
Set the checkpointing scheme to use (for adjoint) :c:func:`ARKodeSetAdjointCheckpointScheme` ``NULL``
Set the checkpointing step index (for adjoint) :c:func:`ARKodeSetAdjointCheckpointIndex` 0
================================================= ========================================== =======================



Expand Down Expand Up @@ -1397,6 +1398,34 @@ Set max number of constraint failures :c:func:`ARKodeSetMaxNumConstr
.. versionadded:: 6.1.0


.. c:function:: int ARKodeSetAdjointCheckpointScheme(void* arkode_mem, SUNAdjointCheckpointScheme checkpoint_scheme)

Specifies the :c:type:`SUNAdjointCheckpointScheme` to use for saving states
during the forward integration, and loading states during backward integration
of an adjoint system.

:param arkode_mem: pointer to the ARKODE memory block.
:param checkpoint_scheme: the checkpoint scheme to use.

:retval ARK_SUCCESS: the function exited successfully.
:retval ARK_MEM_NULL: ``arkode_mem`` was ``NULL``.

.. versionadded:: x.y.z

.. c:function:: int ARKodeSetAdjointCheckpointIndex(void* arkode_mem, long int step_index)
Copy link
Collaborator

Choose a reason for hiding this comment

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

In the SUNAdjointStepper, the step index is of type int64_t rather than long int


Specifies the step index (that is step number) to insert the next checkpoint at.
This is incremented along with the step count, but it is useful to be able to reset
this index during recomputations of missing states during the backward adjoint integration.

:param arkode_mem: pointer to the ARKODE memory block.
:param checkpoint_scheme: the checkpoint scheme to use
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
:param checkpoint_scheme: the checkpoint scheme to use
:param step_index: TODO...


:retval ARK_SUCCESS: the function exited successfully.
:retval ARK_MEM_NULL: ``arkode_mem`` was ``NULL``.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Note to self: error for negative value or something before current step?


.. versionadded:: x.y.z


.. _ARKODE.Usage.ARKodeAdaptivityInputTable:

Expand Down
1 change: 1 addition & 0 deletions doc/arkode/guide/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ with support by the `US Department of Energy <http://www.doe.gov>`_,
sunnonlinsol/index.rst
sunadaptcontroller/index.rst
sunstepper/index.rst
sunadjoint/index.rst
sunmemory/index.rst
sundials/Install_link.rst
Constants
Expand Down
14 changes: 14 additions & 0 deletions doc/arkode/guide/source/sunadjoint/SUNAdjoint_links.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.. ----------------------------------------------------------------
SUNDIALS Copyright Start
Copyright (c) 2002-2024, Lawrence Livermore National Security
and Southern Methodist University.
All rights reserved.

See the top-level LICENSE and NOTICE files for details.

SPDX-License-Identifier: BSD-3-Clause
SUNDIALS Copyright End
----------------------------------------------------------------

.. include:: ../../../../shared/sunadjoint/SUNAdjointCheckpointScheme.rst
.. include:: ../../../../shared/sunadjoint/SUNAdjointStepper.rst
32 changes: 32 additions & 0 deletions doc/arkode/guide/source/sunadjoint/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
..
----------------------------------------------------------------
SUNDIALS Copyright Start
Copyright (c) 2002-2024, Lawrence Livermore National Security
and Southern Methodist University.
All rights reserved.

See the top-level LICENSE and NOTICE files for details.

SPDX-License-Identifier: BSD-3-Clause
SUNDIALS Copyright End
----------------------------------------------------------------

.. _SUNAdjoint:

############################
Adjoint Sensitivity Analysis
############################

.. versionadded:: x.y.z

The ``SUNAdjoint`` API consists of a few customizable modules that provide a framework for adjoint
sensitivity analysis (ASA). The API itself does not implement ASA, but it provides a common
interface for ASA capabilities implemented in the SUNDIALS packages. Right now it supports :ref:`the
ASA capabilities in ARKODE <ARKODE.Mathematics.ASA>`, while the ASA capabilities in :ref:`CVODES
<CVODES.Mathematics.ASA>` and :ref:`IDAS <IDAS.Mathematics.ASA>` must be used directly.
*Users should read the package specific sections on ASA capabilities before this section.*

.. toctree::
:maxdepth: 1

SUNAdjoint_links.rst
1 change: 1 addition & 0 deletions doc/arkode/guide/source/sunmemory/SUNMemory_links.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
----------------------------------------------------------------

.. include:: ../../../../shared/sunmemory/SUNMemory_Description.rst
.. include:: ../../../../shared/sunmemory/SUNMemory_System.rst
.. include:: ../../../../shared/sunmemory/SUNMemory_CUDA.rst
.. include:: ../../../../shared/sunmemory/SUNMemory_HIP.rst
.. include:: ../../../../shared/sunmemory/SUNMemory_SYCL.rst
1 change: 1 addition & 0 deletions doc/cvode/guide/source/sunmemory/SUNMemory_links.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
----------------------------------------------------------------

.. include:: ../../../../shared/sunmemory/SUNMemory_Description.rst
.. include:: ../../../../shared/sunmemory/SUNMemory_System.rst
.. include:: ../../../../shared/sunmemory/SUNMemory_CUDA.rst
.. include:: ../../../../shared/sunmemory/SUNMemory_HIP.rst
.. include:: ../../../../shared/sunmemory/SUNMemory_SYCL.rst
1 change: 1 addition & 0 deletions doc/cvodes/guide/source/sunmemory/SUNMemory_links.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
----------------------------------------------------------------

.. include:: ../../../../shared/sunmemory/SUNMemory_Description.rst
.. include:: ../../../../shared/sunmemory/SUNMemory_System.rst
.. include:: ../../../../shared/sunmemory/SUNMemory_CUDA.rst
.. include:: ../../../../shared/sunmemory/SUNMemory_HIP.rst
.. include:: ../../../../shared/sunmemory/SUNMemory_SYCL.rst
1 change: 1 addition & 0 deletions doc/ida/guide/source/sunmemory/SUNMemory_links.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
----------------------------------------------------------------

.. include:: ../../../../shared/sunmemory/SUNMemory_Description.rst
.. include:: ../../../../shared/sunmemory/SUNMemory_System.rst
.. include:: ../../../../shared/sunmemory/SUNMemory_CUDA.rst
.. include:: ../../../../shared/sunmemory/SUNMemory_HIP.rst
.. include:: ../../../../shared/sunmemory/SUNMemory_SYCL.rst
Loading
Loading