|
| 1 | +from inspect import cleandoc |
| 2 | + |
| 3 | +from petab.v2.C import * |
| 4 | +from petab.v2 import Problem |
| 5 | +from petabtests import ( |
| 6 | + PetabV2TestCase, |
| 7 | + antimony_to_sbml_str, |
| 8 | +) |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | +DESCRIPTION = cleandoc(""" |
| 12 | +## Objective |
| 13 | +
|
| 14 | +This case tests simultaneous re-initialization of compartment size and |
| 15 | +contained species when an SBML event triggers at the exact same |
| 16 | +time-point as a PEtab condition. |
| 17 | +
|
| 18 | +## Model |
| 19 | +
|
| 20 | +A species `S`, defined in terms of concentrations, with `dS/dt = p = 1`, |
| 21 | +in a compartment `C`. `S` and `C` are changed via the condition table. |
| 22 | +
|
| 23 | +There is a PEtab condition triggered at `t=10`, which is triggered |
| 24 | +at the exact time as an SBML event that re-initializes the compartment |
| 25 | +volume after the condition table is applied. |
| 26 | +""") |
| 27 | + |
| 28 | +# problem -------------------------------------------------------------------- |
| 29 | + |
| 30 | +sbml_file = Path(__file__).parent / "model.xml" |
| 31 | + |
| 32 | +vol0 = 4 |
| 33 | +conc0 = 2 |
| 34 | +conc10 = 8 |
| 35 | +dSdt = 1 |
| 36 | +vol10 = (conc0 + 10 * dSdt) + vol0 # = 16 |
| 37 | + |
| 38 | +ant_model = f""" |
| 39 | +model petab_test_0030 |
| 40 | + compartment C = {vol0} |
| 41 | +
|
| 42 | + species S in C = 3 # this is overwritten by the condition table |
| 43 | +
|
| 44 | + p = {dSdt} |
| 45 | + S' = p |
| 46 | +
|
| 47 | + at time >= 10, fromTrigger=false: C = C * 2 # this is executed after PEtab condition |
| 48 | +end |
| 49 | +""" |
| 50 | +sbml_file.write_text(antimony_to_sbml_str(ant_model)) |
| 51 | + |
| 52 | +problem = Problem() |
| 53 | +problem.add_observable("obs_C", "C", noise_formula="1") |
| 54 | +problem.add_observable("obs_amount_S", "S * C", noise_formula="1") |
| 55 | +problem.add_observable("obs_conc_S", "S", noise_formula="1") |
| 56 | + |
| 57 | +problem.add_parameter("p", lb=0, ub=10, nominal_value=1) |
| 58 | + |
| 59 | +problem.add_experiment("experiment1", 0, "condition1", 10, "condition2") |
| 60 | +# at t=0 |
| 61 | +problem.add_condition( |
| 62 | + "condition1", |
| 63 | + "condition1", |
| 64 | + S=conc0, |
| 65 | +) |
| 66 | +# t=10 |
| 67 | +problem.add_condition( |
| 68 | + "condition2", |
| 69 | + "condition2", |
| 70 | + S="S + C", |
| 71 | + C=conc10, |
| 72 | +) |
| 73 | + |
| 74 | +ts = [0, 5, 10, 15] |
| 75 | +for t in ts: |
| 76 | + for obs in ["obs_C", "obs_amount_S", "obs_conc_S"]: |
| 77 | + problem.add_measurement( |
| 78 | + obs, experiment_id="experiment1", time=t, measurement=t |
| 79 | + ) |
| 80 | + |
| 81 | +# solutions ------------------------------------------------------------------ |
| 82 | + |
| 83 | +simulation_df = problem.measurement_df.copy(deep=True).rename( |
| 84 | + columns={MEASUREMENT: SIMULATION} |
| 85 | +) |
| 86 | +simulation_df[SIMULATION] = [ |
| 87 | + # vol, amount, conc |
| 88 | + # --- t=0 --- |
| 89 | + vol0, |
| 90 | + conc0 * vol0, |
| 91 | + conc0, |
| 92 | + # --- t=5 --- |
| 93 | + vol0, |
| 94 | + (conc0 + 5 * dSdt) * vol0, |
| 95 | + (conc0 + 5 * dSdt), |
| 96 | + # --- t=10 --- |
| 97 | + # condition table: |
| 98 | + # 8, 16 * 8 = 128 , 12 + 4 = 16 |
| 99 | + # event (triggered at same time as PEtab condition) |
| 100 | + # volume is changed by event, amount is preserved, conc changes |
| 101 | + # C = 8 * 2 = 16, 128, 8 |
| 102 | + vol10, |
| 103 | + conc10 * vol10, |
| 104 | + conc10, |
| 105 | + # --- t=15 --- |
| 106 | + vol10, |
| 107 | + (conc10 + 5 * dSdt) * vol10, |
| 108 | + (conc10 + 5 * dSdt) |
| 109 | +] |
| 110 | + |
| 111 | +case = PetabV2TestCase.from_problem( |
| 112 | + id=30, |
| 113 | + brief="Simultaneous trigger of PEtab condition and SBML event.", |
| 114 | + description=DESCRIPTION, |
| 115 | + model=sbml_file, |
| 116 | + problem=problem, |
| 117 | + simulation_df=simulation_df, |
| 118 | +) |
0 commit comments