-
Notifications
You must be signed in to change notification settings - Fork 25
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
How to set multiple model values in a single step? #1260
Comments
There is indeed a 'setValues' function. I think it does exactly what you want ;-) However! It's a function at the python layer that in turn calls 'setValue' a lot, so we're back to where we started. But! You won't have any problems using 'setValue'. Here's a simple example: import tellurium as te
import roadrunner
r = te.loada("""
v = 1
c = 1
x = 1
y := v+c
at v>c: x = 3
at y>10 && y < 15: x = 8
""")
print("pre: x=", r.x, "y=", r.y)
r.v = 10
print("v=10: x=", r.x, "y=", r.y)
r.c = 10
print("c=10; x=", r.x, "y=", r.y)
r.simulate()
print("post simulation, x=", r.x, "y=", r.y) Here's the output:
Essentially, formulas exist in roadrunner's internal state as formulas, and if you request output from them, they'll calculate their current values and tell you what they are. This is what's happening with the 'y' value. But events only trigger during simulations, not while the model is sitting there being processed and queried. Hence, neither event triggers, despite conditions being transiently right to trigger them. I did discover that there's sort of one exception to the 'events don't fire if the model is just sitting there': events that trigger at time zero do fire on model load, and on the model being reset. Models of this sort can be dealt with by setting 'init([variable])' (Remember that calling setValue("init(x)", 5) will set the initial value of x to 5 and then resets the model): r = te.loada("""
v = 10
x = 1
at v>5, t0=false: x = 5
""")
print("Before simulation, x=", r.x)
r.resetAll()
print("After resetAll, x=", r.x)
r.v = 2
print("After setValue, x=", r.x)
r.setValue("init(v)", 2)
print("After setValue init(v), x=", r.x)
r.setValue("init(v)", 10)
print("After setValue init(v) larger, x=", r.x) gives:
The order doesn't matter here, either, because the model gets reset every time you call setValue("init(x)"): r = te.loada("""
v = 1
r = 1
x = 1
at v>5 && r<v, t0=false: x = 5
""")
print("Before simulation, x=", r.x)
r.resetAll()
print("After resetAll, x=", r.x)
r.setValue("init(v)", 10)
print("After setValue init(v), x=", r.x)
r.setValue("init(r)", 10)
print("After setValue init(r), x=", r.x) gives:
There are also other methods present to set a bunch of variables at once! They're hiding in 'r.model': r.model.setBoundarySpeciesAmounts() |
Hi all,
I want to assign multiple values in a single step to ensure the model is in a consistent state after applying the values.
Currently, it is only possible to set a single value at once with
r.setValue
.I have a list of changes which should be applied similar to a
listOfEventAssignments
. I.e. there is no priority of the assignments, but all assignments should be performed in a single step. I.e. the values should all be set in the ASTnode and then the dependent things should be re-evaluated.The main issue with establishing a fixed order for updating multiple variables (such as compartments, species, or parameters) is that mathematical calculations dependent on these variables could produce unexpected results depending on the update sequence. I assume that the ast tree is reevaluated after setValue.
For example, consider a compartment V and a concentration C. Any calculation that relies on both V and C may yield different outcomes depending on the update order. Suppose we start with V=1 and C=1, then update both to V=10 and C=10. If there is a calculation dependent on the relationship between V and C, the result will vary based on the order in which they are updated. For instance, an event triggered when V>C will only occur if V is updated before C. If C is updated first or both are updated simultaneously, the event may not trigger, leading to inconsistencies.
In other words, updating variables sequentially in a fixed order could lead to intermediate steps that place the model in an undefined state. I am not sure this is an issue? Or is this handled correctly by roadrunner? I.e. if I call
r.setValue
multiple times are there side effects depending on the order? If this is the case then there is no reliable way to set multiple changes at once.It would be nice to have a
setValues
function taking a dictionary which would allow to apply multiple changes at once which are then applied in a single go in roadrunner. This would also reduce the number of python calls if many changes have to be applied.Best Matthias
The text was updated successfully, but these errors were encountered: