Skip to content

Remake for conservation laws update #1203

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

Merged
merged 13 commits into from
Apr 3, 2025
Merged

Conversation

TorkelE
Copy link
Member

@TorkelE TorkelE commented Mar 29, 2025

Updates docs, history file, and removes warnings, to note users that this now should work.

Still needs further expanded tests to guarantee that this is actually the case.

@TorkelE
Copy link
Member Author

TorkelE commented Mar 30, 2025

I have an doc update, as well, but it requires #1205

@unpack X1, X2, X3 = rn
u0 = [X1 => 1.0, X2 => 1.0, X3 => 1.0]
ps = [:k1 => 0.1, :k2 => 0.2, :k3 => 0.3, :k4 => 0.4]
prob_old = ODEProblem(rn, u0, 1.0, ps; remove_conserved = true)
Copy link
Member

Choose a reason for hiding this comment

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

What about other relevant problem types (i.e, NonlinearProblem or SteadyStaeProblem)?

Copy link
Member Author

Choose a reason for hiding this comment

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

Once we have it working for NonlinearSystems I will add that one in a well here. I can do the other 2, however, this test block takes almost 3 seconds, so it will be like 5-6 second per test run (is this worth it?)

Copy link
Member

Choose a reason for hiding this comment

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

I think it's worth it. Correctness should be our top aim, even if it means slower tests.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds good. And at some point we can paralelise tests as well, at which point the time will be even less of an worry.

@TorkelE
Copy link
Member Author

TorkelE commented Mar 30, 2025

I have added SDEProblem to the loop. Once this is merged I will add Nonlinear and SteadyStateProblems to #1206.

@isaacsas
Copy link
Member

Sounds good. LMK when this is done then (i.e. no longer WIP).

@TorkelE TorkelE changed the title [WIP] Remake for conservation laws update Remake for conservation laws update Mar 30, 2025
@@ -39,7 +39,7 @@ Using the `unknowns` function we can confirm that the ODE only has a single unkn
```@example conservation_laws
unknowns(osys)
```
Next, using `parameters` we note that an additional parameter, `Γ[1]` has been added to the system:
Copy link
Member Author

Choose a reason for hiding this comment

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

Due to MTK updates, the conservaton law parameters are not displayed as Γ[1:1] anymore, but only Γ.
(I prefered the old way and have raised an issue about it)

@@ -1,5 +1,5 @@
# [Working with Conservation Laws](@id conservation_laws)
Catalyst can detect, and eliminate for differential-equation based models, *conserved quantities*, i.e. linear combinations of species which are conserved via the chemistry. This functionality is both automatically utilised by Catalyst (e.g. to [remove singular Jacobians during steady state computations](@ref homotopy_continuation_conservation_laws)), but is also available for users to utilise directly (e.g. to potentially [improve simulation performance](@ref ode_simulation_performance_conservation_laws)).
Copy link
Member Author

Choose a reason for hiding this comment

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

think this is more correct

Copy link
Member

@isaacsas isaacsas left a comment

Choose a reason for hiding this comment

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

Just a few minor things (and one API question).


!!! note
The `remove_conserved = true` option is available when creating `SDEProblem`s, `NonlinearProblem`s, and `SteadyStateProblem`s (and their corresponding systems). However, it cannot be used when creating `JumpProblem`s.

!!! warn
Users of the [ModelingToolkit.jl](https://github.com/SciML/ModelingToolkit.jl) package might be familiar with the `structural_simplify` function. While it can be applied to Catalyst models as well, generally, this should be avoided (as `remove_conserved` performs a similar role, but is better adapted to these models). Furthermore, applying `structural_simplify` will interfere with conservation law removal, preventing users from accessing eliminated quantities.
Copy link
Member

Choose a reason for hiding this comment

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

Is it actually worse than this, i.e. does structural_simplify drop Observed for ODEs, and hence lead to incorrect models? If so, the warning should be more strict about not mixing it with the use of conservation law elimination.

Copy link
Member Author

Choose a reason for hiding this comment

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

So, initially I wrote this. But then I tried to reproduce the removal of observables, and it didn't seem to be a case anymore. At that point things thing had gone so much around in my head I just closed it down. I should try and go to the bottom with it though. Worst case there should not be a silent error (people will just not be able to retrieve the observable values). Happy to say this in the warning, and then we can remove it if things turn out fine.

Copy link
Member

Choose a reason for hiding this comment

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

We should probably add tests about this (i.e. check that structural_simplify converted models don't drop observed). If that works now it would be good to have tests suddenly start failing should MTK change the behavior so we know it has changed.

@TorkelE
Copy link
Member Author

TorkelE commented Apr 3, 2025

100% agree with the proposed API functions. Want me to add them to this PR, or as a follow-up?

Might also be nice to make DSL/ReactionSystem options iniitalise_cons_laws, which enables users to do rs.\Gamma and also puts i in the parameters(rs) vector. That is definitely a follow-up issue, haven't actually thought about the implications of this.

@isaacsas
Copy link
Member

isaacsas commented Apr 3, 2025

Adding the API functions here and updating the docs accordingly sounds good.

Maybe the kwarg should just be eliminate_conserved, which will run conservation law analysis at system construction and store this kwarg? We can then have the convert kwarg be nothing by default, and only override the system kwarg if the user sets a value? (I think this should be in a followup though, or even after V15 releases.)

@isaacsas
Copy link
Member

isaacsas commented Apr 3, 2025

I also wonder if we should stick with immutability more, and make conservation law stuff a transformation that generates a new system (that can have gamma as a parameter). Still will be a bit clunky though since the user won't always have that system returned to them if it is done via convert.

@TorkelE
Copy link
Member Author

TorkelE commented Apr 3, 2025

An operation on e.g. ODESystems (or ReactionSystems, but I am not sure if this makes sense)? Eitehr way it is definitely worth think about if there are better ways of doing this kind of stuff (although I think the current approach mostly works well).

@isaacsas
Copy link
Member

isaacsas commented Apr 3, 2025

Let's leave it for a future PR. I forgot that we do conservation laws on the flattened system, so this info isn't getting stored in the original user reaction system. We'd probably need to make conversation law removal be at completion time for the user to have a reaction system with the symbol in it.

@isaacsas
Copy link
Member

isaacsas commented Apr 3, 2025

Feel free to merge if you are happy with this.

@TorkelE
Copy link
Member Author

TorkelE commented Apr 3, 2025

will merge when tests pass

@TorkelE TorkelE merged commit 61f1fcd into master Apr 3, 2025
13 checks passed
@TorkelE TorkelE deleted the remake_test_n_warn_update branch April 3, 2025 19:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants