Skip to content

Refactor ODE analyses to build models in the theory of signed polynomial ODE systems#1251

Draft
tim-at-topos wants to merge 2 commits into
mainfrom
tim/mass-action-polynomial-ode-system
Draft

Refactor ODE analyses to build models in the theory of signed polynomial ODE systems#1251
tim-at-topos wants to merge 2 commits into
mainfrom
tim/mass-action-polynomial-ode-system

Conversation

@tim-at-topos

@tim-at-topos tim-at-topos commented May 7, 2026

Copy link
Copy Markdown
Contributor

This PR includes a massive redesign of the way that we handle ODE semantics, which has the bonus of introducing some new capabilities (#1177) as well as (hopefully) making adding new ODE semantics in the future considerably simpler.

For those reading (or reviewing) this code, my suggested reading order would be:

  1. ode::polynomial_ode::build_system_custom_parameters to see how we interpret models of th_signed_polynomial_ode_system as systems of polynomial ODEs
  2. ode::lotka_volterra to see an example of the main chunk of defining an ODE semantics: giving an implementation for ODESemanticsAnalysis
  3. ode::ode_semantics to see where all the actual new code is
  4. ode::mass_action to see where things are still messy and need some extra finessing
  5. The comments that contain the string FEEDBACK, where I have some specific questions / highlight specific problems with this design.

p.s. I know this PR adds quite a lot of code for something called a "refactor", but (a) I added a lot of documentation and some new tests, and (b) a subsequent PR focussing on the front-end parts should now be possible, allowing for much more code removal!


To-do:

  • Replace the main chunk of PetriNetMassActionAnalysis::mass_action::build_system with polynomial_ode::build_system
  • Replace the main chunk of StockFlowMassActionAnalysis::mass_action::build_system with polynomial_ode::build_system
  • Refactor lotka_volterra.rs and linear_ode.rs to build an ODE system #1275
  • Remove catlog::src::stdlib::analyses::ode::signed_coefficients.rs (and anything else)
  • Create a bunch of structure for ODESemantics
  • Refactor catlog::src::stdlib::analyses::ode::polynomial_ode.rs
  • Implement ODESemantics for linear ODE
  • Rename "linear ODE" to "CCL" internally (i.e. not to the user) (and justify this!)
  • Implement ODESemantics for mass-action
  • Refactor catlog-wasm::src::analyses
  • Refactor catlog-wasm::src::latex (cf. Traits for LaTeX equations throughout analyses #1172)

@tim-at-topos tim-at-topos added frontend TypeScript frontend and Rust-wasm integrations core Rust core for categorical logic and general computation tidying Refactoring or simplifying existing code labels May 7, 2026
@tim-at-topos tim-at-topos force-pushed the tim/mass-action-polynomial-ode-system branch 5 times, most recently from fe17e78 to 90dbf51 Compare May 14, 2026 15:07
@tim-at-topos

Copy link
Copy Markdown
Contributor Author

@epatters Here's a problem: we cannot push the mass-action analysis for signed stock-flow diagrams through the theory of polynomial ODEs because we interpret negative links as negative exponents, meaning we'd need rational ODEs. For now, since I essentially made up the theory of signed stock-flow diagrams, I've just removed its mass-action analysis. I see two options:

  1. Wait until we think about rational ODEs (probably not that far into the future) and then come back to fix this.
  2. Implement mass-action as it was before just for the special case of signed stock-flow diagrams.

I'm in favour of 1, but would appreciate your thoughts.

@tim-at-topos tim-at-topos removed the frontend TypeScript frontend and Rust-wasm integrations label May 14, 2026
@epatters

Copy link
Copy Markdown
Member

Hmm, good question. What would be the code footprint of option (2), i.e., leaving the special-cased ODE analysis code for signed stock-flow as is?

@tim-at-topos

Copy link
Copy Markdown
Contributor Author

For now it would be reasonably small, essentially just duplicating half of the current mass_action.rs. But it would get worse in future PRs, since one goal of this is to refactor a lot of duplicated code between Lotka–Volterra, linear ODE, and mass-action, so mass-action for signed stock-flow diagrams would have entirely unique chunks of code for each part of the analysis (core, wasm, and frontend).

@tim-at-topos tim-at-topos changed the title Refactor mass_action.rs to build a model in the theory of signed polynomial ODE systems Refactor ODE analyses to build models in the theory of signed polynomial ODE systems May 15, 2026
@tim-at-topos tim-at-topos force-pushed the tim/mass-action-polynomial-ode-system branch 7 times, most recently from b5ccd2e to fb0ef7a Compare May 20, 2026 11:38
@epatters

epatters commented May 20, 2026

Copy link
Copy Markdown
Member

For now it would be reasonably small, essentially just duplicating half of the current mass_action.rs. But it would get worse in future PRs, since one goal of this is to refactor a lot of duplicated code between Lotka–Volterra, linear ODE, and mass-action, so mass-action for signed stock-flow diagrams would have entirely unique chunks of code for each part of the analysis (core, wasm, and frontend).

Thanks for explaining. Though I'm not sure how to weigh the tradeoff between keeping existing functionality and maintaining a clean codebase, I tend to value the latter a lot and since you also seem inclined in that direction, I'd say go ahead and remove it.

@tim-at-topos tim-at-topos force-pushed the tim/mass-action-polynomial-ode-system branch 5 times, most recently from 4acac39 to e254a8f Compare May 21, 2026 22:01
@tim-at-topos tim-at-topos force-pushed the tim/mass-action-polynomial-ode-system branch 2 times, most recently from 6fac82b to a5cc41b Compare May 21, 2026 22:21
@tim-at-topos

tim-at-topos commented May 21, 2026

Copy link
Copy Markdown
Contributor Author

@epatters and @tslil-topos this isn't quite ready for review yet, but all that's missing is some extra documentation and then some extra refactoring that I think I might move to a future PR because this one is already quite hefty. (Note, however, that the subsequent PR should hopefully remove as many lines as this one adds!)

Would it be possible for one of you to to start taking a look through this when you get a chance please? I think it's very likely that there will be substantial changes requested, and I'd rather put off writing documentation for things that are liable to change 😎 I've included a suggested path through reading the code in the opening comment of this PR!

@epatters

Copy link
Copy Markdown
Member

Sounds good. I've marked this for review despite your caveats because that will help me remember it needs a review.

@epatters epatters marked this pull request as ready for review May 22, 2026 03:44
@epatters epatters self-requested a review May 22, 2026 03:45
@tim-at-topos tim-at-topos force-pushed the tim/mass-action-polynomial-ode-system branch 4 times, most recently from 5d2c138 to 706e78b Compare May 22, 2026 15:29
@tim-at-topos tim-at-topos marked this pull request as draft May 26, 2026 18:50
@tim-at-topos tim-at-topos mentioned this pull request May 27, 2026
WIP: Rethinking traits

WIP: Some tests only half failing

WIP: Tests passing; time to tidy

ENH: Build derived model of th_signed_polynomial_ode_system in mass_action

ENH: Mass-action for stock-flow; DEL: Mass-action for signed stock-flow

ENH: struct for transition / flow interfaces

WIP: Starting on Lotka-Volterra

WIP: Failing tests

FIX: Lotka-Volterra tests passing

FIX: Working analysis (frontend)

ENH: Lotka-Volterra equations

ENH: Linear ODE refactor

ENH: Linear ODE equations

WIP: Starting on ODESemantics

WIP: lotka_volterra_semantics()

WIP: build_system_from_ode_semantics

WIP: DblModelForODESemantics

WIP: ODESemanticsAnalysis and ODESemanticsProblemData

WIP: ODESemantics trait

WIP: Documentation

WIP: ODESemantics for mass-action

WIP: Cleaning up types, but mass-action still frustrating

WIP: Big reshuffle (moving functions out from a struct)

WIP: Fixing mass-action again

WIP: terrible code

WIP: Changed from ObGen to Ob

WIP: Stock-flow mass-action

FIX: Passing catlog tests

Rename LinearODE -> LCC

FIX: Documentation

TODO: Redesign
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Rust core for categorical logic and general computation tidying Refactoring or simplifying existing code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants