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

[v14 - Ready] Fix bug where expand_registered_functions mutated original reaction system #929

Merged
merged 11 commits into from
Jun 12, 2024

Conversation

TorkelE
Copy link
Member

@TorkelE TorkelE commented Jun 8, 2024

Previously expand_registered_functions would unintentionally apply the operation on the input system. E.g.

rs_expanded_funcs = Catalyst.expand_registered_functions(rs)

would result in both rs and rs_expanded_funcs having their functions expanded. This fixes this, and adds a test to ensure this remains the case.

end
return expr
end
# If applied to a Reaction, return a reaction with its rate modified.
function expand_registered_functions(rx::Reaction)
Reaction(expand_registered_functions(rx.rate), rx.substrates, rx.products,
Reaction(expand_registered_functions!(deepcopy(rx.rate)), rx.substrates, rx.products,
Copy link
Member Author

Choose a reason for hiding this comment

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

Is this appropriate use of deepcopy? I could not come up with a better way to do it. If we really want to avoid it it would be possible to first check the expression whether there are any registered functions, and only apply the function in this case

Copy link
Member Author

Choose a reason for hiding this comment

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

Also, I think technically this by could be rewritten using @set (since expanding functions really shouldn't change anything more of the reaction), however, I was reluctant to introduce any more changes than necessary.

Copy link
Member

Choose a reason for hiding this comment

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

It would be more memory efficient to first check if the given expression would be expanded, and only then deepcopy and expand, but otherwise this is fine as a less memory efficient version.

Copy link
Member

Choose a reason for hiding this comment

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

(Note, I'm not saying this needs to be done in this PR, just that it would be good to switch to that eventually.)

@TorkelE TorkelE changed the title Fix bug where expand_registered_functions mutated original reaction system [v14 - Ready] Fix bug where expand_registered_functions mutated original reaction system Jun 10, 2024
src/registered_functions.jl Show resolved Hide resolved
end
return expr
end
# If applied to a Reaction, return a reaction with its rate modified.
function expand_registered_functions(rx::Reaction)
Reaction(expand_registered_functions(rx.rate), rx.substrates, rx.products,
Reaction(expand_registered_functions!(deepcopy(rx.rate)), rx.substrates, rx.products,
Copy link
Member

Choose a reason for hiding this comment

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

It would be more memory efficient to first check if the given expression would be expanded, and only then deepcopy and expand, but otherwise this is fine as a less memory efficient version.

@TorkelE
Copy link
Member Author

TorkelE commented Jun 10, 2024

Sorry, totally forgot about this one. I think I will add the checks, it should be fairly straightforward with the new symbolic functions.

@TorkelE
Copy link
Member Author

TorkelE commented Jun 11, 2024

Ok, I have changed around the internals (no longer using deepcopy, although Symbolics might use something similar internally, I am not sure) and also supporting events.

src/registered_functions.jl Outdated Show resolved Hide resolved
src/registered_functions.jl Outdated Show resolved Hide resolved
src/registered_functions.jl Show resolved Hide resolved
@TorkelE TorkelE merged commit 8f8290c into master Jun 12, 2024
8 checks passed
@TorkelE TorkelE deleted the fix_expand_funcs branch June 12, 2024 15:07
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.

None yet

2 participants