InitialAssignments

Testing initial assignments


In [1]:
%matplotlib inline
from __future__ import print_function, division
import tellurium as te
import roadrunner
from roadrunner import SelectionRecord
from matplotlib import pyplot as plt

In [2]:
# load model
import Cell
r = te.loads('results/{}_{}.xml'.format(Cell.mid, Cell.version))
print(r.getCurrentAntimony())


// Created by libAntimony v2.9.0
model *AssignmentTest_1()

  // Assignment Rules:
  Cve := Ave/Vve;
  Vve := BW*FVve;

  // Rate Rules:
  Ave' = -k1*Cve;

  // Variable initializations:
  IVDOSE = 0;
  IVDOSE has mg;
  D = PODOSE;
  D has mg;
  PODOSE = 100;
  PODOSE has mg;
  FVve = 0.0514;
  FVve has litre_per_kg;
  k1 = 0.1;
  k1 has litre_per_h;
  BW = 70;
  BW has kg;
  Ave = IVDOSE;
  Ave has mg;
  Cve has mg_per_litre;
  Vve has litre;

  // Other declarations:
  var D, Ave, Cve, Vve;
  const IVDOSE, PODOSE, FVve, k1, BW;

  // Unit definitions:
  unit mulitre_per_g = 1e-6 litre / gram;
  unit mg = 1e-3 gram;
  unit kg = 1e3 gram;
  unit s_per_h = second / 3600 second;
  unit mg_per_g = 1e-3 gram / gram;
  unit mg_per_h = 1e-3 gram / 3600 second;
  unit ml_per_litre = 1e-3 litre / litre;
  unit m = metre;
  unit litre_per_kg = litre / 1e3 gram;
  unit h = 3600 second;
  unit mulitre_per_min_mg = 1e-6 litre / (60 second * 1e-3 gram);
  unit min_per_h = 60 second / 3600 second;
  unit litre_per_h = litre / 3600 second;
  unit m2 = metre^2;
  unit ml_per_s = 1e-3 litre / second;
  unit mg_per_litre = 1e-3 gram / litre;
  unit per_h = 1 / 3600 second;
  unit length = m;
  unit area = m2;
  unit volume = litre;
  unit substance = mg;
  unit extent = mg;
  unit time_unit = h;
end

Changing the dose

Running simulations with changing dose


In [3]:
for dose in [0, 10, 20, 30]:
    r.setValue('init(IVDOSE)', dose)  # oral dose in [mg]
    r.resetAll()
    print(r.Ave)

    s = r.simulate(0,24,1001)
    plt.plot(s['time'], s['Ave'], color='black')
plt.xlabel('time')
plt.ylabel('amount [mg]')
plt.show()


0.0
10.0
20.0
30.0

Testing the reset stack

r.resetToOrigin()

  • full reset of model to original state
  • resetToOrigin resets initial values (and everything else) from the originally loaded model.

r.reset()

  • reset just loads the initial values
  • values of species are now initial values

r.resetAll()

  • resetAll() loads the species with the current set of initial values (and resets parameters to their original loaded values

In [23]:
from __future__ import print_function, division

import tellurium as te 
import roadrunner 

r = te.loada(""" 
    species S1, S2, S3
    
    J1:S1 -> S2; k1*S1; 
    S1 = 1; k1 = 0.1; 
    
    // S2 via initial assignment
    S2 = S1 + 10
    
    // S3 via rule
    S3 := S1/2
    
    // p1 via initial assignment
    p1 = 10
    // p2 via rule
    p2 := p1/2
""") 
r.timeCourseSelections = r.getFloatingSpeciesIds() + r.getGlobalParameterIds()

def print_values(r, msg):
    print('***', msg, '***')
    print('<initial values>')
    for sid in r.getFloatingSpeciesIds():
        print("\t{sid} = {value}".format(sid=sid, value=r['init({})'.format(sid)]))
    print('<values>')
    for sid in r.getFloatingSpeciesIds() + r.getGlobalParameterIds():
        print("\t{sid} = {value}".format(sid=sid, value=r['{}'.format(sid)]))
    print('\n')

# plot the initial values of models
print_values(r, "Initial values")

# make a change to S1 and p1
r.S1 = 99 
r.p1 = 99
print_values(r, "Change S1 and p1")
# -> S3 and p2 are  updated also via rule

# reset() after change
r.reset()
print_values(r, "reset() after changes")
# -> S1 is reset to intial value (S3 via rule)
# -> parameters are not effected

r.resetToOrigin()
print_values(r, "resetToOrigin()")


# make a change to init(S1) and init(p1)
r['init(S1)'] = 99 
r['init(p1)'] = 99
print_values(r, "Change init(S1) and init(p1)")
# -> the values of species are updated immediatly, 
# -> ! the dependencies of the parameters are not !!!

# resetAll() after  init change
r.resetAll()
print_values(r, "resetAll() to apply changes from changed initial conditions")
# -> the values of the parameters are updated in the model from the init(p1) values

r.resetToOrigin()
print_values(r, "resetToOrigin()")

# This is not possible (the init(p1) is set, but cannot be plotted)
print('init(p1) = ', r['init(p1)'])


*** Initial values ***
<initial values>
	S1 = 1.0
	S2 = 11.0
	S3 = 0.5
<values>
	S1 = 1.0
	S2 = 11.0
	S3 = 0.5
	k1 = 0.1
	p1 = 10.0
	p2 = 5.0


*** Change S1 and p1 ***
<initial values>
	S1 = 1.0
	S2 = 11.0
	S3 = 0.5
<values>
	S1 = 99.0
	S2 = 11.0
	S3 = 49.5
	k1 = 0.1
	p1 = 99.0
	p2 = 49.5


*** reset() after changes ***
<initial values>
	S1 = 1.0
	S2 = 11.0
	S3 = 0.5
<values>
	S1 = 1.0
	S2 = 11.0
	S3 = 0.5
	k1 = 0.1
	p1 = 99.0
	p2 = 49.5


*** resetToOrigin() ***
<initial values>
	S1 = 1.0
	S2 = 11.0
	S3 = 0.5
<values>
	S1 = 1.0
	S2 = 11.0
	S3 = 0.5
	k1 = 0.1
	p1 = 10.0
	p2 = 5.0


*** Change init(S1) and init(p1) ***
<initial values>
	S1 = 99.0
	S2 = 109.0
	S3 = 49.5
<values>
	S1 = 99.0
	S2 = 109.0
	S3 = 49.5
	k1 = 0.1
	p1 = 10.0
	p2 = 5.0


*** resetAll() to apply changes from changed initial conditions ***
<initial values>
	S1 = 99.0
	S2 = 109.0
	S3 = 49.5
<values>
	S1 = 99.0
	S2 = 109.0
	S3 = 49.5
	k1 = 0.1
	p1 = 99.0
	p2 = 49.5


*** resetToOrigin() ***
<initial values>
	S1 = 1.0
	S2 = 11.0
	S3 = 0.5
<values>
	S1 = 1.0
	S2 = 11.0
	S3 = 0.5
	k1 = 0.1
	p1 = 10.0
	p2 = 5.0


---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-23-1b98bcf7f418> in <module>()
     68 
     69 # This is not possible (the init(p1) is set, but cannot be plotted)
---> 70 print('init(p1) = ', r['init(p1)'])

/usr/local/lib/python2.7/dist-packages/roadrunner/roadrunner.pyc in __getitem__(self, *args)
   2546         return _roadrunner.RoadRunner_getValue(self, *args)
   2547 
-> 2548     def __getitem__(self, *args): return _roadrunner.RoadRunner___getitem__(self, *args)
   2549     def __setitem__(self, *args): return _roadrunner.RoadRunner___setitem__(self, *args)
   2550     def getIds(self, *args):

RuntimeError: Invalid id 'p1' for floating initial amount

In [24]:
r.getValue('init(p1)')


---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-24-52fc8a8c989b> in <module>()
----> 1 r.getValue('init(p1)')

/usr/local/lib/python2.7/dist-packages/roadrunner/roadrunner.pyc in getValue(self, *args)
   2544 
   2545         """
-> 2546         return _roadrunner.RoadRunner_getValue(self, *args)
   2547 
   2548     def __getitem__(self, *args): return _roadrunner.RoadRunner___getitem__(self, *args)

RuntimeError: Invalid id 'p1' for floating initial amount

In [ ]: