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

removeInitialAssignment results in many variables changing #1195

Open
dalbabur opened this issue Mar 11, 2024 · 5 comments
Open

removeInitialAssignment results in many variables changing #1195

dalbabur opened this issue Mar 11, 2024 · 5 comments

Comments

@dalbabur
Copy link

dalbabur commented Mar 11, 2024

Context
I have a model with many InitialAssignments, such as C12214= v1*0.001*dilution_factor where C12214 is a species and v1 and dilution_factor are variables. In some instances, I'd like to override the InitialAssignment and set the species concentration to a fixed value, say C12214= 1.0.

Issue
I'm looping through the variables and species initial concentrations that I want to override and calling r.setValue('init('+var+')', new_value). Everything works as expected when setting variables, but when setting species initial concentrations other variables in the model are changing as well. This happens regardless of which species I'm changing, and it always affects the same variables. The issue seems to be with r.removeInitialAssignment()

Workaround
Setting the concentration, r.setValue('['+var+']', new_value), as opposed to the initial concentration works as expected. In this case I don't need to change the initial concentration so this is okay.

@luciansmith
Copy link

The way things are supposed to work is that if you have two initial assignments:

a = b+2
b = c+3
c = 5

and then you set init(b) to 4, a will change to 6, because its initial assignment is still 'b+2'.

Is that what you're seeing? Or is something else going on?

@dalbabur
Copy link
Author

yup that makes sense, and I also see that behavior. my issue is more like this:

model definition:

C12214= v1*0.001*dilution_factor
...
C00356= v275*0.002*dilution_factor
v1 = 876
v275 = 756
dilution_factor = 1 

change initial concentrations:

r.setValue('init([C12214])', 3.0)

results in:
r['init([C12214])'] = 3.0 and suddenly r['v275'] = 1

@luciansmith
Copy link

Well, that's probably a bug, then! Can you provide a working example? A large one is fine if that's what you have.

@luciansmith
Copy link

Did this ever get addressed? Looking at this again, there's another obvious-in-retrospect thing that could be happening: when 'conserved moieties' are on, that means that Roadrunner has detected a reaction network where (say) the total S1+S2 is constant, so if you change S1, you already know what S2 is going to be; you just decrease it by the same amount you increased S1 by.

So if that's active, that would explain why a change in C12214 means that v275 also changes.

Happily, you can turn that off!

r.conservedMoietyAnalysis = False

You can also check the current status of the flag:

print(r.conservedMoietyAnalysis)

@matthiaskoenig
Copy link
Collaborator

One issue seems to be understanding when initial assignments are evaluated and how to properly retrigger their evaluation. From my understanding, initial assignments are evaluated during model import. If you modify variables or parameters that influence these initial assignments, you typically need to run a reset or resetAll on the model to trigger their reevaluation.

For further clarification, this discussion provides helpful insights into how updates are handled. Essentially, running a reset seems to be required to propagate any changes.

I’ve never been able to fully determine the correct way to handle this in libRoadRunner, so I’ve adopted the approach of using assignment rules for everything. While this introduces some computational overhead—since assignment rules are reevaluated at every time step—it provides much clearer behavior. Additionally, the mathematical dependencies are automatically updated at every step, so there’s no need for manual reevaluation. If your model integrates quickly, switching to assignment rules is a straightforward solution for most issues with initial assignments.

Another challenge with initial assignments is that many tools discard them upon model import. In such cases, initial assignments are evaluated numerically during import and are no longer part of the model. This means some tools do not support what you’re attempting to achieve, as the initial assignments simply don’t exist in the loaded model. This limitation makes it challenging to reproduce simulation experiments that rely on initial assignments.

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

No branches or pull requests

3 participants