-
-
Notifications
You must be signed in to change notification settings - Fork 183
Enable setting of Jacobian tags in ODETerm #755
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,10 @@ | ||
| from collections.abc import Callable | ||
| from typing import ClassVar, TypeAlias | ||
|
|
||
| import jax | ||
| import jax.numpy as jnp | ||
| import jax.tree_util as jtu | ||
| import lineax as lx | ||
| import optimistix as optx | ||
| from equinox.internal import ω | ||
|
|
||
|
|
@@ -11,7 +13,7 @@ | |
| from .._local_interpolation import LocalLinearInterpolation | ||
| from .._root_finder import with_stepsize_controller_tols | ||
| from .._solution import is_okay, RESULTS | ||
| from .._term import AbstractTerm | ||
| from .._term import AbstractTerm, WrapTerm | ||
| from .base import AbstractAdaptiveSolver, AbstractImplicitSolver | ||
|
|
||
|
|
||
|
|
@@ -24,6 +26,12 @@ def _implicit_relation(z1, nonlinear_solve_args): | |
| return diff | ||
|
|
||
|
|
||
| def _implicit_op_relation(J: lx.AbstractLinearOperator) -> lx.AbstractLinearOperator: | ||
| # Mirrors `_implicit_relation`: residual(z) ≈ h·vf(y0 + z) - z, so the residual | ||
| # Jacobian is h·J - I. The scalar h is omitted (tag inference is sign-only). | ||
| return J - lx.IdentityLinearOperator(J.in_structure()) | ||
|
|
||
|
|
||
| class ImplicitEuler(AbstractImplicitSolver, AbstractAdaptiveSolver): | ||
| r"""Implicit Euler method. | ||
|
|
||
|
|
@@ -82,6 +90,13 @@ def step( | |
| # (C.f. `AbstractRungeKutta.step`.) | ||
| # If we wanted FSAL then really the correct thing to do would just be to | ||
| # write out a `ButcherTableau` and use `AbstractSDIRK`. | ||
| _inner = terms.term if isinstance(terms, WrapTerm) else terms | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is okay. Kind of the point of having an ABC is that we don't generally care exactly what we get, and can abstract over them. (As a practical matter we do pierce this veil in a couple of places, but almost always just as special-case performance optimizations, not things that affect functional correctness.) |
||
| y_struct = jax.tree_util.tree_map( | ||
| lambda x: jax.ShapeDtypeStruct(x.shape, x.dtype), y0 | ||
| ) | ||
| residual_tags = self._residual_tags( | ||
| getattr(_inner, "tags", frozenset()), y_struct, _implicit_op_relation | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I consider |
||
| ) | ||
| k0 = terms.vf_prod(t0, y0, args, control) | ||
| args = (terms.vf_prod, t1, y0, args, control) | ||
| nonlinear_sol = optx.root_find( | ||
|
|
@@ -91,6 +106,7 @@ def step( | |
| args, | ||
| throw=False, | ||
| max_steps=self.root_find_max_steps, | ||
| tags=residual_tags, | ||
| ) | ||
| k1 = nonlinear_sol.value | ||
| y1 = (y0**ω + k1**ω).ω | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit, underscore indicates privacy to the file it's created in. (Is the convention we use in the Equinox ecosystem.)