patrick-kidger / patrick-kidger/diffrax

statefully evolving an auxiliary variable

Open
#462 20 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
2.1k
Forks
189
Avg merge
3d 18h
Merged PRs (30d)
1

Description

Given any vector field f(t, y, args), I want to keep track of an auxiliary variable w that depends and evolves with y but non-differentiably in the sense that there is no vector field for w, it's just a state that gets updated every step call. For example, at every step of the integrator I would ideally like to be able to do something like the following pseudocode:

def update_w(t: float, y: Array, w: Array) -> Array:
    return jac(t, y)@w

def step(...):
    ....
    y1 = ....
    y_error = ...
    w = update_w(t, y1, w)
    return ((y1, w), y_error, dense, ...)

I'm currently trying to wrap an arbitrary solver with some success like so:

def wrap_solver(solver_class):

    class WrappedSolver(solver_class):

        def update_aux(self, t0, x, aux):
             ....
             return aux

        def step(self, terms, t0, t1, y0, args, solver_state, made_jump):
            (y, y_error, dense_info, solver_state, solver_result) = super().step(terms, t0, t1, y0, args, solver_state, made_jump)
            x, aux = y
            new_aux = self.update_aux(t0, x, aux)
            return ((x, new_aux), y_error, dense_info, solver_state, solver_result)
      
    return WrappedSolver

But this seems very janky and I'm wondering if there's a cleaner way without exposing the step method. It's as if I need a custom term where one term is an ODETerm with control dt and the other is like a "StateTerm" also with control dt but it doesn't get integrated.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the solver step API and the existing ODETerm handling described in the issue. Compare the wrapped-solver approach with the proposed control-dt state term that is updated but not integrated. Done means arbitrary solvers can maintain and update an auxiliary state without exposing or overriding step.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.