Example #1
0
    def setUp(self):
        KCST = 10000.0
        DCST = 0.08e-12
        self.model = smodel.Model()
        A = smodel.Spec('A', self.model)
        B = smodel.Spec('B', self.model)
        C = smodel.Spec('C', self.model)
        D = smodel.Spec('D', self.model)
        E = smodel.Spec('E', self.model)
        F = smodel.Spec('F', self.model)

        self.vsys1 = smodel.Volsys('vsys1', self.model)
        self.vsys2 = smodel.Volsys('vsys2', self.model)

        self.reac1 = smodel.Reac('reac1',
                                 self.vsys1,
                                 lhs=[A, B],
                                 rhs=[C],
                                 kcst=KCST)
        self.reac2 = smodel.Reac('reac2',
                                 self.vsys2,
                                 lhs=[D, E],
                                 rhs=[F],
                                 kcst=KCST)

        self.geom = sgeom.Geom()
        self.comp1 = sgeom.Comp('comp1', self.geom, 1e-18)
        self.comp1.addVolsys('vsys1')
        self.comp2 = sgeom.Comp('comp2', self.geom, 1e-18)
        self.comp2.addVolsys('vsys2')

        if __name__ == "__main__":
            self.mesh = meshio.importAbaqus('meshes/brick_40_4_4_1400tets.inp',
                                            1e-6)[0]
        else:
            self.mesh = meshio.importAbaqus(
                'multi_sys_test/meshes/brick_40_4_4_1400tets.inp', 1e-6)[0]

        comp1_tets = []
        comp2_tets = []

        for t in range(self.mesh.ntets):
            cord = self.mesh.getTetBarycenter(t)
            if cord[0] < 0.0:
                comp1_tets.append(t)
            else:
                comp2_tets.append(t)

        self.tmcomp1 = sgeom.TmComp('comp1', self.mesh, comp1_tets)
        self.tmcomp1.addVolsys('vsys1')
        self.tmcomp2 = sgeom.TmComp('comp2', self.mesh, comp2_tets)
        self.tmcomp2.addVolsys('vsys2')

        self.rng = srng.create('r123', 512)
        self.rng.initialize(1000)
Example #2
0
def test_masteq():
    mdl = smod.Model()

    A = smod.Spec('A', mdl)

    volsys = smod.Volsys('vsys', mdl)

    # Production
    R1 = smod.Reac('R1', volsys, lhs=[], rhs=[A], kcst=KCST_f)
    R2 = smod.Reac('R2', volsys, lhs=[A], rhs=[], kcst=KCST_b)

    geom = sgeom.Geom()

    comp1 = sgeom.Comp('comp1', geom, VOL)
    comp1.addVolsys('vsys')

    rng = srng.create('mt19937', 1000)
    rng.initialize(int(time.time() % 4294967295))

    sim = ssolv.Wmdirect(mdl, geom, rng)
    sim.reset()

    tpnts = numpy.arange(0.0, INT, DT)
    ntpnts = tpnts.shape[0]

    res = numpy.zeros([ntpnts])

    sim.restore('./validation_cp/cp/masteq')

    for t in range(0, ntpnts):
        sim.run(tpnts[t])
        res[t] = sim.getCompCount('comp1', 'A')

    def fact(x):
        return (1 if x == 0 else x * fact(x - 1))

    # Do cumulative count, but not comparing them all.
    # Don't get over 50 (I hope)
    steps_n_res = numpy.zeros(50)
    for r in res:
        steps_n_res[int(r)] += 1
    for s in range(50):
        steps_n_res[s] = steps_n_res[s] / ntpnts

    passed = True
    max_err = 0.0

    k1 = KCST_b
    k2 = KCST_f * (6.022e23 * 1.0e-15)

    # Compare 5 to 15
    for m in range(5, 16):
        analy = (1.0 / fact(m)) * math.pow((k2 / k1), m) * math.exp(-(k2 / k1))
        assert (tol_funcs.tolerable(steps_n_res[m], analy, tolerance))
    def setUp(self):
        self.model = smodel.Model()
        A = smodel.Spec("A", self.model)
        B = smodel.Spec("B", self.model)
        C = smodel.Spec("C", self.model)
        D = smodel.Spec("D", self.model)
        E = smodel.Spec("E", self.model)

        self.vsys1 = smodel.Volsys('vsys1', self.model)
        self.vsys2 = smodel.Volsys('vsys2', self.model)
        self.ssys1 = smodel.Surfsys('ssys1', self.model)

        self.reac1 = smodel.Reac('reac1', self.vsys1, lhs = [A], rhs = [A],  kcst = 1e5)
        self.reac2 = smodel.Reac('reac2', self.vsys2, lhs = [E], rhs = [E],  kcst = 1e4)

        self.sreac = smodel.SReac('sreac', self.ssys1, slhs = [B], srhs = [B],  kcst = 1e3)
    
        if __name__ == "__main__":
            self.mesh = meshio.loadMesh('meshes/cyl_len10_diam1')[0]
        else:
            self.mesh = meshio.loadMesh('getROIArea_bugfix_test/meshes/cyl_len10_diam1')[0]

        ntets = self.mesh.countTets()
        comp1Tets, comp2Tets = [], []
        comp1Tris, comp2Tris = set(), set()
        for i in range(ntets):
            if self.mesh.getTetBarycenter(i)[0] > 0:
                comp1Tets.append(i)
                comp1Tris |= set(self.mesh.getTetTriNeighb(i))
            else:
                comp2Tets.append(i)
                comp2Tris |= set(self.mesh.getTetTriNeighb(i))
        patch1Tris = list(comp1Tris & comp2Tris)

        self.comp1 = sgeom.TmComp('comp1', self.mesh, comp1Tets)
        self.comp2 = sgeom.TmComp('comp2', self.mesh, comp2Tets)
        self.comp1.addVolsys('vsys1')
        self.comp2.addVolsys('vsys2')

        self.patch1 = sgeom.TmPatch('patch1', self.mesh, patch1Tris, self.comp1, self.comp2)
        self.patch1.addSurfsys('ssys1')

        self.ROI1 = self.mesh.addROI('ROI1', sgeom.ELEM_TET, comp1Tets)
        self.ROI2 = self.mesh.addROI('ROI2', sgeom.ELEM_TET, comp2Tets)
        self.ROI3 = self.mesh.addROI('ROI3', sgeom.ELEM_TRI, patch1Tris)
    
        self.rng = srng.create('r123', 512)
        self.rng.initialize(1000)
        tet_hosts = gd.linearPartition(self.mesh, [steps.mpi.nhosts, 1, 1])
        tri_hosts = gd.partitionTris(self.mesh, tet_hosts, patch1Tris)
        self.solver = solv.TetOpSplit(self.model, self.mesh, self.rng, solv.EF_NONE, tet_hosts, tri_hosts)
Example #4
0
def get_model():
    mdl  = smod.Model()
    A = smod.Spec('A', mdl)
    volsys = smod.Volsys('vsys',mdl)
    R1 = smod.Reac('R1', volsys, lhs = [], rhs = [A])
    R1.setKcst(1e-3)
    return mdl
Example #5
0
def test_forev():
    mdl = smod.Model()

    A = smod.Spec('A', mdl)
    B = smod.Spec('B', mdl)

    volsys = smod.Volsys('vsys', mdl)

    R1 = smod.Reac('R1', volsys, lhs=[A], rhs=[B], kcst=KCST_f)
    R2 = smod.Reac('R2', volsys, lhs=[B], rhs=[A], kcst=KCST_b)

    geom = sgeom.Geom()

    comp1 = sgeom.Comp('comp1', geom, VOL)
    comp1.addVolsys('vsys')

    rng = srng.create('mt19937', 512)
    rng.initialize(int(time.time() % 4294967295))

    sim = ssolv.Wmdirect(mdl, geom, rng)
    sim.reset()

    tpnts = numpy.arange(0.0, INT, DT)
    ntpnts = tpnts.shape[0]

    res_m = numpy.zeros([NITER, ntpnts, 2])

    for i in range(0, NITER):
        sim.restore('./validation_cp/cp/first_order_rev')
        for t in range(0, ntpnts):
            sim.run(tpnts[t])
            res_m[i, t, 0] = sim.getCompConc('comp1', 'A') * 1e6
            res_m[i, t, 1] = sim.getCompConc('comp1', 'B') * 1e6

    mean_res = numpy.mean(res_m, 0)

    Aeq = COUNT * (KCST_b /
                   KCST_f) / (1 +
                              (KCST_b / KCST_f)) / (VOL * 6.0221415e26) * 1e6
    Beq = (COUNT / (VOL * 6.0221415e26)) * 1e6 - Aeq

    max_err = 0.0
    passed = True
    for i in range(ntpnts):
        if i < 7: continue
        assert (tol_funcs.tolerable(mean_res[i, 0], Aeq, tolerance))
        assert (tol_funcs.tolerable(mean_res[i, 1], Beq, tolerance))
Example #6
0
def gen_model():

    mdl = smodel.Model()

    X = smodel.Spec('X', mdl)
    A = smodel.Spec('A', mdl)
    # Vol/surface systems
    cytosolv = smodel.Volsys('cytosolv', mdl)

    dif_X = smodel.Diff('diffX', cytosolv, X)
    dif_X.setDcst(DCST)

    reac_X = smodel.Reac('reacX', cytosolv, lhs=[A], rhs=[A, X])

    return mdl
Example #7
0
def cbsa2steps(cbsa_model):
    
    import steps.geom as swm
    import steps.model as smodel
    import steps.rng as srng
    import steps.solver as ssolver
    
    mdl = smodel.Model()
    vsys = smodel.Volsys('vsys', mdl)    
    mols = [smodel.Spec('M'+str(i), mdl) for i in range(1,cbsa_model.exp_n_molecules)]    
    reactions = []    
    for i in range(1,cbsa_model.exp_n_reactions):
        reactants = list(np.where(cbsa_model.expS[:,i] < 0)[0])
        reactants_sto = list(cbsa_model.expS[:,i][reactants]*-1)
        modifiers = list(np.where(cbsa_model.expR[:,i] > 0)[0])
        modifiers_sto = list(cbsa_model.expR[:,i][modifiers])
        products = list(np.where(cbsa_model.expS[:,i] > 0)[0])
        products_sto = list(cbsa_model.expS[:,i][products])
        
        reactants += modifiers
        reactants_sto += modifiers_sto
        products += modifiers
        products_sto += modifiers_sto
        
        reactants_objs = [[mols[reactants[j]-1] for k in range(reactants_sto[j])] for j in range(len(reactants))]
        reactants_objs = [item for sublist in reactants_objs for item in sublist]
        
        products_objs = [[mols[products[j]-1] for k in range(products_sto[j])] for j in range(len(products))]
        products_objs = [item for sublist in products_objs for item in sublist]
        
        reactions.append(smodel.Reac("R"+str(i), vsys, lhs=reactants_objs, rhs=products_objs, kcst=cbsa_model.exp_k[i]))
    
    wmgeom = swm.Geom()

    comp = swm.Comp('comp', wmgeom)
    comp.addVolsys('vsys')
    comp.setVol(1.6667e-21)

    r = srng.create('mt19937', 256)
    r.initialize(int(timer()))
    sim = ssolver.Wmdirect(mdl, wmgeom, r)
    sim.reset()

    for i in range(1,cbsa_model.exp_n_molecules):
        sim.setCompConc('comp', 'M'+str(i), cbsa_model.exp_x0[i]*1e-6)
    
    return sim
Example #8
0
def gen_model():
    
    mdl = smod.Model()
    
    # The chemical species
    A = smod.Spec('A', mdl)
    B = smod.Spec('B', mdl)
    C = smod.Spec('C', mdl)
    D = smod.Spec('D', mdl)
    E = smod.Spec('E', mdl)
    F = smod.Spec('F', mdl)
    G = smod.Spec('G', mdl)
    H = smod.Spec('H', mdl)
    I = smod.Spec('I', mdl)
    J = smod.Spec('J', mdl)

    volsys = smod.Volsys('vsys',mdl)


    R1 = smod.Reac('R1', volsys, lhs = [A, B], rhs = [C],  kcst = 1000.0e6)
    R2 = smod.Reac('R2', volsys, lhs = [C],  rhs = [A,B], kcst = 100)
    R3 = smod.Reac('R3', volsys, lhs = [C, D], rhs = [E], kcst = 100e6)
    R4 = smod.Reac('R4', volsys, lhs = [E], rhs = [C,D], kcst = 10)

    R5 = smod.Reac('R5', volsys, lhs = [F, G], rhs = [H], kcst = 10e6)
    R6 = smod.Reac('R6', volsys, lhs = [H], rhs = [F,G], kcst = 1)
    R7 = smod.Reac('R7', volsys, lhs = [H, I], rhs = [J],  kcst = 1e6)
    R8 = smod.Reac('R8', volsys, lhs = [J],  rhs = [H,I], kcst = 0.1*10)


    # The diffusion rules
    D1 = smod.Diff('D1', volsys, A,  100e-12)
    D2 = smod.Diff('D2', volsys, B,  90e-12)
    D3 = smod.Diff('D3', volsys, C, 80e-12)
    D4 = smod.Diff('D4', volsys, D, 70e-12)
    D5 = smod.Diff('D5', volsys, E, 60e-12)
    D6 = smod.Diff('D6', volsys, F,  50e-12)
    D7 = smod.Diff('D7', volsys, G,  40e-12)
    D8 = smod.Diff('D8', volsys, H,  30e-12)
    D9 = smod.Diff('D9', volsys, I,  20e-12)
    D10 = smod.Diff('D10', volsys, J, 10e-12)
    
    return mdl
Example #9
0
def test_foirev():
    mdl = smod.Model()

    A = smod.Spec('A', mdl)
    volsys = smod.Volsys('vsys', mdl)
    R1 = smod.Reac('R1', volsys, lhs=[A], rhs=[], kcst=KCST)

    geom = sgeom.Geom()
    comp1 = sgeom.Comp('comp1', geom, VOL)
    comp1.addVolsys('vsys')

    rng = srng.create('mt19937', 1000)
    rng.initialize(int(time.time() % 4294967295))

    sim = ssolv.Wmdirect(mdl, geom, rng)
    sim.reset()

    tpnts = np.arange(0.0, INT, DT)
    ntpnts = tpnts.shape[0]

    res_m = np.zeros([NITER, ntpnts, 1])
    res_std1 = np.zeros([ntpnts, 1])
    res_std2 = np.zeros([ntpnts, 1])

    for i in range(0, NITER):
        sim.restore('./validation_cp/cp/first_order_irev')
        for t in range(0, ntpnts):
            sim.run(tpnts[t])
            res_m[i, t, 0] = sim.getCompCount('comp1', 'A')

    mean_res = np.mean(res_m, 0)
    std_res = np.std(res_m, 0)

    m_tol = 0
    s_tol = 0

    passed = True
    for i in range(ntpnts):
        if i == 0: continue
        analy = N * np.exp(-KCST * tpnts[i])
        std = np.power((N * (np.exp(-KCST * tpnts[i])) *
                        (1 - (np.exp(-KCST * tpnts[i])))), 0.5)
        if not tol_funcs.tolerable(analy, mean_res[i], tolerance):
            passed = False
        assert (tol_funcs.tolerable(std, std_res[i], tolerance))
    def setUp(self):
        mdl = smodel.Model()

        self.v1 = 1e-20
        self.v2 = 2e-20
        self.a1 = 3e-14

        self.kreac = 200.0
        self.ksreac = 100.0

        S1 = smodel.Spec('S1', mdl)
        S2 = smodel.Spec('S2', mdl)
        S1S2 = smodel.Spec('S1S2', mdl)

        vsys = smodel.Volsys('vsys', mdl)
        ssys = smodel.Surfsys('ssys', mdl)

        smodel.Reac('reac', vsys, lhs=[S1, S2], rhs=[S2, S2], kcst=self.kreac)

        smodel.SReac('sreac', ssys, ilhs=[S1], slhs=[S2], srhs=[S1S2], kcst=self.ksreac)

        geom = sgeom.Geom()

        comp1 = sgeom.Comp('comp1', geom)
        comp1.setVol(self.v1)
        comp1.addVolsys('vsys')

        comp2 = sgeom.Comp('comp2', geom)
        comp2.setVol(self.v2)
        comp1.addVolsys('vsys')

        patch = sgeom.Patch('patch', geom, comp1, comp2)
        patch.addSurfsys('ssys')
        patch.setArea(self.a1)

        self.mdl, self.geom, self.rng = mdl, geom, srng.create('mt19937',512)
        self.rng.initialize(1234)
mdl = smodel.Model()
#mdl variable for discribing model.
#create a top-level container object for our model this top model
#container is required for all simulations in STEPS.

molX1bar = smodel.Spec('molX1bar', mdl)
molY1 = smodel.Spec('molY1', mdl)
#create 2 steps.model.Spec objects corresponding to 2 chamical
#spicies

vsys = smodel.Volsys('vsys', mdl)
#create a volume system
#volume systems art container objects that group a number of
#stoichimetric reaction rules.

c1reac_f = smodel.Reac('c1reac_f', vsys, lhs=[molX1bar], rhs=[molY1], kcst=0.2)
#create the reaction rules themselves
#what is cicst = 0.3e6?

import steps.geom as swm
#import the geometry module that contains the objects used to
#define the geometry, namely steps.geom.

wmgeom = swm.Geom()
#generate parent container object

comp = swm.Comp('comp', wmgeom)
comp.addVolsys('vsys')
comp.setVol(1.6667e-27)
#To this symple model, we only create one compartment and we
#store it in the variable called comp.
Example #12
0
    def setUp(self):
        mdl = smodel.Model()

        S1 = smodel.Spec('S1', mdl)

        vsys = smodel.Volsys('vsys', mdl)
        ssys = smodel.Surfsys('ssys', mdl)

        smodel.Reac('R01', vsys, lhs=[S1], rhs=[S1], kcst=1)
        smodel.SReac('SR01', ssys, slhs=[S1], srhs=[S1], kcst=1)

        vrange = [-200.0e-3, 50e-3, 1e-3]
        vrate = lambda v: 2.0
        Chan1 = smodel.Chan('Chan1', mdl)
        chanop = smodel.ChanState('chanop', mdl, Chan1)
        chancl = smodel.ChanState('chancl', mdl, Chan1)
        smodel.VDepSReac('VDSR01',
                         ssys,
                         slhs=[chancl],
                         srhs=[chanop],
                         k=vrate,
                         vrange=vrange)
        smodel.VDepSReac('VDSR02',
                         ssys,
                         srhs=[chancl],
                         slhs=[chanop],
                         k=vrate,
                         vrange=vrange)

        Chan1_Ohm_I = smodel.OhmicCurr('Chan1_Ohm_I',
                                       ssys,
                                       chanstate=chanop,
                                       g=20e-12,
                                       erev=-77e-3)

        if __name__ == "__main__":
            self.mesh = meshio.importAbaqus('meshes/test.inp', 1e-7)[0]
        else:
            self.mesh = meshio.importAbaqus(
                'missing_solver_methods_test/meshes/test.inp', 1e-7)[0]

        comp1 = sgeom.TmComp('comp1', self.mesh, range(self.mesh.countTets()))
        comp1.addVolsys('vsys')

        patch1 = sgeom.TmPatch('patch1', self.mesh, self.mesh.getSurfTris(),
                               comp1)
        patch1.addSurfsys('ssys')

        self.c1ROIInds = range(10)
        self.p1ROIInds = range(5)
        self.mesh.addROI('comp1ROI', sgeom.ELEM_TET, self.c1ROIInds)
        self.mesh.addROI('patch1ROI', sgeom.ELEM_TRI, self.p1ROIInds)

        membrane = sgeom.Memb('membrane', self.mesh, [patch1], opt_method=1)

        rng = srng.create('mt19937', 512)
        rng.initialize(1234)

        self.sim = ssolver.Tetexact(mdl, self.mesh, rng, True)
        self.sim.setEfieldDT(1e-4)

        self.sim.reset()
Example #13
0
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# STEPS - STochastic Engine for Pathway Simulation
# Copyright (C) 2007-2011 Okinawa Institute of Science and Technology, Japan.
# Copyright (C) 2003-2006 University of Antwerp, Belgium.
#
# See the file AUTHORS for details.
#
# This file is part of STEPS.
#
# STEPS is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# STEPS is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
import steps.model as smod
import steps.geom as sgeom
import steps.rng as srng
import steps.solver as ssolv
import math
import time
import steps.utilities.meshio as smeshio
INT = 1.1  # Sim endtime

# In test runs, with good code, <0.1% will fail with a tolerance of 1%
tolerance = 1.0 / 100

########################################################################

mdl = smod.Model()

A = smod.Spec('A', mdl)
B = smod.Spec('B', mdl)
C = smod.Spec('C', mdl)

volsys = smod.Volsys('vsys', mdl)

R1 = smod.Reac('R1', volsys, lhs=[A, B], rhs=[C], kcst=KCST)

geom = sgeom.Geom()

comp1 = sgeom.Comp('comp1', geom, VOL)
comp1.addVolsys('vsys')

rng = srng.create('mt19937', 512)
import random
#rng.initialize(int(time.time()%4294967295))
import random
rng.initialize(int(random.random() * 1000))

sim = ssolv.Wmdirect(mdl, geom, rng)
sim.reset()
Ob = smodel.Spec('Ob', mdl)
Oc = smodel.Spec('Oc', mdl)
Ia = smodel.Spec('Ia', mdl)
Ib = smodel.Spec('Ib', mdl) 
A01 = smodel.Spec('A01', mdl)
Pa = smodel.Spec('Pa', mdl)
Pb = smodel.Spec('Pb', mdl)
Pc = smodel.Spec('Pc', mdl)
Sa = smodel.Spec('Sa', mdl)
Sb = smodel.Spec('Sb', mdl)

#create the volume system
vsys = smodel.Volsys('vsys', mdl)

#the forward reaction without the forward binding reaction
A11_to_Oa_f = smodel.Reac('A11_to_Oa_f', vsys, lhs = [A11], rhs = [Oa], kcst = 1800)
Oa_to_Ob_f = smodel.Reac('Oa_to_Ob_f', vsys, lhs = [Oa], rhs = [Ob], kcst = 133)
Oc_to_Ia_f = smodel.Reac('Oc_to_Ia_f', vsys, lhs = [Oc], rhs = [Ia], kcst = 630)
A01_to_Pa_f = smodel.Reac('A01_to_Pa_f', vsys, lhs = [A01], rhs = [Pa], kcst = 0.3)
Pa_to_Pb_f = smodel.Reac('Pa_to_Pb_f', vsys, lhs = [Pa], rhs = [Pb], kcst = 500)
Pc_to_Sa_f = smodel.Reac('Pc_to_Sa_f', vsys, lhs = [Pc], rhs = [Sa], kcst = 3000)

#the backward reaction without the backrward unbinding reaction
A11_to_Oa_b = smodel.Reac('A11_to_Oa_b', vsys, lhs = [Oa], rhs = [A11], kcst = 330)
Oa_to_Ob_b = smodel.Reac('Oa_to_Ob_b', vsys, lhs = [Ob], rhs = [Oa], kcst = 1500)
Oc_to_Ia_b = smodel.Reac('Oc_to_Ia_b', vsys, lhs = [Ia], rhs = [Oc], kcst = 400)
A01_to_Pa_b = smodel.Reac('A01_to_Pa_b', vsys, lhs = [Pa], rhs = [A01], kcst = 700)
Pa_to_Pb_b = smodel.Reac('Pa_to_Pb_b', vsys, lhs = [Pb], rhs = [Pa], kcst = 100)
Pc_to_Sa_b = smodel.Reac('Pc_to_Sa_b', vsys, lhs = [Sa], rhs = [Pc], kcst = 250)

#surface system
Example #16
0
PumpD_f.setKcst(P_f_kcst)

PumpD_b = smodel.SReac('PumpD_b',
                       ssys_det,
                       slhs=[CaPump],
                       irhs=[Ca_det],
                       srhs=[Pump])
PumpD_b.setKcst(P_b_kcst)

PumpD_k = smodel.SReac('PumpD_k', ssys_det, slhs=[CaPump], srhs=[Pump])
PumpD_k.setKcst(P_k_kcst)

#iCBsf-fast
iCBsf1_f = smodel.Reac('iCBsf1_f',
                       vsys_det,
                       lhs=[Ca_det, iCBsf],
                       rhs=[iCBsCa],
                       kcst=iCBsf1_f_kcst)
iCBsf1_b = smodel.Reac('iCBsf1_b',
                       vsys_det,
                       lhs=[iCBsCa],
                       rhs=[Ca_det, iCBsf],
                       kcst=iCBsf1_b_kcst)

#iCBsCa
iCBsCa_f = smodel.Reac('iCBsCa_f',
                       vsys_det,
                       lhs=[Ca_det, iCBsCa],
                       rhs=[iCBCaCa],
                       kcst=iCBsCa_f_kcst)
iCBsCa_b = smodel.Reac('iCBsCa_b',
Example #17
0
def getModel():
    # Create model container
    mdl = smod.Model()

    # Create chemical species
    ca = smod.Spec('ca', mdl)
    ip3 = smod.Spec('ip3', mdl)
    plc = smod.Spec('plc', mdl)

    # Create calcium buffers
    GCaMP6s = smod.Spec('GCaMP6s', mdl)
    ca_GCaMP6s = smod.Spec('ca_GCaMP6s', mdl)

    # Create IP3R states species
    unb_IP3R = smod.Spec('unb_IP3R', mdl)
    ip3_IP3R = smod.Spec('ip3_IP3R', mdl)
    caa_IP3R = smod.Spec('caa_IP3R', mdl)
    cai_IP3R = smod.Spec('cai_IP3R', mdl)
    open_IP3R = smod.Spec('open_IP3R', mdl)
    cai_ip3_IP3R = smod.Spec('cai_ip3_IP3R', mdl)
    ca2_IP3R = smod.Spec('ca2_IP3R', mdl)
    ca2_ip3_IP3R = smod.Spec('ca2_ip3_IP3R', mdl)

    # ER surface sys
    ssys = smod.Surfsys('ssys', mdl)

    # plasma membrane surface
    mb_surf = smod.Surfsys('mb_surf', mdl)

    # Create volume system
    # cytosol volume system
    vsys = smod.Volsys('vsys', mdl)

    # ER volume system
    er_vsys = smod.Volsys('er_vsys', mdl)

    ##################################
    ##### DEFINE DIFFUSION RULES #####
    ##################################

    # Diffusion constants
    # Diffusion constant of Calcium (buffered)
    DCST = 0.013e-9
    # Diffusion constant of IP3
    DIP3 = 0.280e-9
    # Diffusion constant of GCaMP6s
    DGCAMP = 0.050e-9

    diff_freeca = smod.Diff('diff_freeca', vsys, ca, DCST)
    diff_ip3 = smod.Diff('diff_ip3', vsys, ip3, DIP3)

    diff_GCaMP6s = smod.Diff('diff_GCaMP6s', vsys, GCaMP6s, DGCAMP)
    diff_ca_GCaMP6s = smod.Diff('diff_ca_GCaMP6s', vsys, ca_GCaMP6s, DGCAMP)

    ##################################
    ######## DEFINE REACTIONS ########
    ##################################
    #### Calcium in and out and Buffering Reactions ####

    # Ca -> null
    ca_deg = smod.Reac('ca_deg', vsys, lhs=[ca])

    # Ca leak
    ca_leak = smod.Reac('ca_leak', vsys, rhs=[ca])

    # Calcium binding to GCaMP6s molecules

    GCaMP6s_bind_ca_f = smod.Reac('GCaMP6s_bind_ca_f', vsys, \
                                lhs=[ca, GCaMP6s], rhs=[ca_GCaMP6s])
    GCaMP6s_bind_ca_b = smod.Reac('GCaMP6s_bind_ca_b', vsys, \
                                lhs=[ca_GCaMP6s], rhs=[GCaMP6s, ca])

    #### IP3 Influx and Buffering Reactions ######
    # IP3 leak
    ip3_leak = smod.Reac('ip3_leak', vsys, rhs=[ip3])

    # IP3 degradation
    ip3_deg = smod.Reac('ip3_deg', vsys, lhs=[ip3])

    # ca activating plc_delta-dependent IP3 synthesis
    plc_ip3_synthesis = smod.SReac('plc_ip3_synthesis', mb_surf, \
                                   slhs=[plc], ilhs= [ca], srhs=[plc], irhs= [ca, ip3])

    ##### IP3R kinetics #####
    # surface/volume reaction ca from cytosol binds activating IP3R site on unbound IP3R
    unb_IP3R_bind_caa_f = smod.SReac('unb_IP3R_bind_caa_f', ssys,\
                                     ilhs=[ca], slhs=[unb_IP3R], srhs=[caa_IP3R])
    unb_IP3R_bind_caa_b = smod.SReac('unb_IP3R_bind_caa_b', ssys, \
                                     slhs=[caa_IP3R], srhs=[unb_IP3R], irhs=[ca])

    # surface/volume reaction ca from cytosol binds inactivating IP3R site on unbound IP3R
    unb_IP3R_bind_cai_f = smod.SReac('unb_IP3R_bind_cai_f', ssys, \
                                     ilhs=[ca], slhs=[unb_IP3R], srhs=[cai_IP3R])
    unb_IP3R_bind_cai_b = smod.SReac('unb_IP3R_bind_cai_b', ssys, \
                                     slhs=[cai_IP3R], srhs=[unb_IP3R], irhs=[ca])

    # surface/volume reaction ca from cytosol binds activating IP3R site on caa_IP3R
    caa_IP3R_bind_ca_f = smod.SReac('caa_IP3R_bind_ca_f', ssys, \
                                    ilhs=[ca], slhs=[caa_IP3R], srhs=[ca2_IP3R])
    caa_IP3R_bind_ca_b = smod.SReac('caa_IP3R_bind_ca_b', ssys, \
                                    slhs=[ca2_IP3R], srhs=[caa_IP3R], irhs=[ca])

    # surface/volume reaction ca from cytosol binds activating IP3R site on ip3_IP3R
    ip3_IP3R_bind_caa_f = smod.SReac('ip3_IP3R_bind_caa_f', ssys, \
                                     ilhs=[ca], slhs=[ip3_IP3R], srhs=[open_IP3R])
    ip3_IP3R_bind_caa_b = smod.SReac('ip3_IP3R_bind_caa_b', ssys, \
                                     slhs=[open_IP3R], srhs=[ip3_IP3R], irhs=[ca])

    # surface/volume reaction ca from cytosol binds inactivating IP3R site on ip3_IP3R
    ip3_IP3R_bind_cai_f = smod.SReac('ip3_IP3R_bind_cai_f', ssys, \
                                     ilhs=[ca], slhs=[ip3_IP3R], srhs=[cai_ip3_IP3R])
    ip3_IP3R_bind_cai_b = smod.SReac('ip3_IP3R_bind_cai_b', ssys, \
                                     slhs=[cai_ip3_IP3R], srhs=[ip3_IP3R], irhs=[ca])

    # surface/volume reaction ca from cytosol binds activating IP3R site on cai_IP3R
    cai_IP3R_bind_ca_f = smod.SReac('cai_IP3R_bind_ca_f', ssys, \
                                    ilhs=[ca], slhs=[cai_IP3R], srhs=[ca2_IP3R])
    cai_IP3R_bind_ca_b = smod.SReac('cai_IP3R_bind_ca_b', ssys, \
                                    slhs=[ca2_IP3R], srhs=[cai_IP3R], irhs=[ca])

    # surface/volume reaction ca from cytosol binds inactivating IP3R site on open_IP3R
    open_IP3R_bind_ca_f = smod.SReac('open_IP3R_bind_ca_f', ssys, \
                                     ilhs=[ca], slhs=[open_IP3R], srhs=[ca2_ip3_IP3R])
    open_IP3R_bind_ca_b = smod.SReac('open_IP3R_bind_ca_b', ssys, \
                                     slhs=[ca2_ip3_IP3R], srhs=[open_IP3R], irhs=[ca])

    # surface/volume reaction ca from cytosol binds activating IP3R site on cai_ip3_IP3R
    cai_ip3_IP3R_bind_ca_f = smod.SReac('cai_ip3_IP3R_bind_ca_f', ssys, \
                                        ilhs=[ca], slhs=[cai_ip3_IP3R], srhs=[ca2_ip3_IP3R])
    cai_ip3_IP3R_bind_ca_b = smod.SReac('cai_ip3_IP3R_bind_ca_b', ssys, \
                                        slhs=[ca2_ip3_IP3R], srhs=[cai_ip3_IP3R], irhs=[ca])

    # surface/volume reaction ip3 from cytosol binds unb_IP3R
    unb_IP3R_bind_ip3_f = smod.SReac('unb_IP3R_bind_ip3_f', ssys, \
                                     ilhs=[ip3], slhs=[unb_IP3R], srhs=[ip3_IP3R])
    unb_IP3R_bind_ip3_b = smod.SReac('unb_IP3R_bind_ip3_b', ssys, \
                                     slhs=[ip3_IP3R], srhs=[unb_IP3R], irhs=[ip3])

    # surface/volume reaction ip3 from cytosol binds caa_IP3R
    caa_IP3R_bind_ip3_f = smod.SReac('caa_IP3R_bind_ip3_f', ssys, \
                                     ilhs=[ip3], slhs=[caa_IP3R], srhs=[open_IP3R])
    caa_IP3R_bind_ip3_b = smod.SReac('caa_IP3R_bind_ip3_b', ssys, \
                                     slhs=[open_IP3R], srhs=[caa_IP3R], irhs=[ip3])

    # surface/volume reaction ip3 from cytosol binds cai_IP3R
    cai_IP3R_bind_ip3_f = smod.SReac('cai_IP3R_bind_ip3_f', ssys, \
                                     ilhs=[ip3], slhs=[cai_IP3R], srhs=[cai_ip3_IP3R])
    cai_IP3R_bind_ip3_b = smod.SReac('cai_IP3R_bind_ip3_b', ssys, \
                                     slhs=[cai_ip3_IP3R], srhs=[cai_IP3R], irhs=[ip3])

    # surface/volume reaction ip3 from cytosol binds ca2_IP3R
    ca2_IP3R_bind_ip3_f = smod.SReac('ca2_IP3R_bind_ip3_f', ssys, \
                                     ilhs=[ip3], slhs=[ca2_IP3R], srhs=[ca2_ip3_IP3R])
    ca2_IP3R_bind_ip3_b = smod.SReac('ca2_IP3R_bind_ip3_b', ssys, \
                                     slhs=[ca2_ip3_IP3R], srhs=[ca2_IP3R], irhs=[ip3])

    ##### Ca ions passing through open IP3R channel to cytosol #####
    Ca_IP3R_flux = smod.SReac('R_Ca_channel_f', ssys, \
                              slhs=[open_IP3R], irhs=[ca], srhs=[open_IP3R])

    ##################################
    #### REACTION CONSTANT VALUES ####
    ##################################

    ##### Calcium Influx and Buffering Reactions #####

    # GCaMP6s mediated calcium buffering
    GCaMP6s_bind_ca_f.setKcst(7.78e6)
    GCaMP6s_bind_ca_b.setKcst(1.12)

    ############# VALUES FOR GCAMP6f #################
    ####    GCaMP6s_bind_ca_f.setKcst(1.05e7)	  ####
    ####    GCaMP6s_bind_ca_b.setKcst(3.93)	  ####
    ##################################################

    # Ca ->  null
    ca_deg.setKcst(30)

    # Ca leak
    ca_leak.setKcst(15e-8)

    ##### IP3 Influx and Buffering Reactions #####
    # IP3 leak does not exist in this model. IP3 synthesis only occurs through PLC activity

    # IP3 -> null
    ip3_deg.setKcst(1.2e-4)

    # ca activating plc_delta-dependent IP3 synthesis
    plc_ip3_synthesis.setKcst(1)

    #### IP3R kinetics #####
    caa_f = 1.2e6
    cai_f = 1.6e4
    ip3_f = 4.1e7
    caa_b = 5e1
    cai_b = 1e2
    ip3_b = 4e2
    unb_IP3R_bind_caa_f.setKcst(caa_f)
    unb_IP3R_bind_caa_b.setKcst(caa_b)

    unb_IP3R_bind_cai_f.setKcst(cai_f)
    unb_IP3R_bind_cai_b.setKcst(cai_b)

    caa_IP3R_bind_ca_f.setKcst(cai_f)
    caa_IP3R_bind_ca_b.setKcst(cai_b)

    ip3_IP3R_bind_caa_f.setKcst(caa_f)
    ip3_IP3R_bind_caa_b.setKcst(caa_b)

    ip3_IP3R_bind_cai_f.setKcst(cai_f)
    ip3_IP3R_bind_cai_b.setKcst(cai_b)

    cai_IP3R_bind_ca_f.setKcst(caa_f)
    cai_IP3R_bind_ca_b.setKcst(caa_b)

    open_IP3R_bind_ca_f.setKcst(cai_f)
    open_IP3R_bind_ca_b.setKcst(cai_b)

    unb_IP3R_bind_ip3_f.setKcst(ip3_f)
    unb_IP3R_bind_ip3_b.setKcst(ip3_b)

    caa_IP3R_bind_ip3_f.setKcst(ip3_f)
    caa_IP3R_bind_ip3_b.setKcst(ip3_b)

    cai_IP3R_bind_ip3_f.setKcst(ip3_f)
    cai_IP3R_bind_ip3_b.setKcst(ip3_b)

    cai_ip3_IP3R_bind_ca_f.setKcst(caa_f)
    cai_ip3_IP3R_bind_ca_b.setKcst(caa_b)

    ca2_IP3R_bind_ip3_f.setKcst(ip3_f)
    ca2_IP3R_bind_ip3_b.setKcst(ip3_b)

    # Ca ions passing through open IP3R channel
    Ca_IP3R_flux.setKcst(6e3)

    return mdl
Example #18
0
NITER = 100000  # The number of iterations
DT = 0.1  # Sampling time-step
INT = 1.1  # Sim endtime

# Tolerance for the comparison:
# In test runs, with good code, < 1%  will fail with a 1.5% tolerance
tolerance = 2.0 / 100

########################################################################

mdl = smod.Model()

A = smod.Spec('A', mdl)
volsys = smod.Volsys('vsys', mdl)
R1 = smod.Reac('R1', volsys, lhs=[A], rhs=[], kcst=KCST)

geom = sgeom.Geom()
comp1 = sgeom.Comp('comp1', geom, VOL)
comp1.addVolsys('vsys')

rng = srng.create('mt19937', 1000)
rng.initialize(int(time.time() % 4294967295))

sim = ssolv.Wmdirect(mdl, geom, rng)
sim.reset()

tpnts = numpy.arange(0.0, INT, DT)
ntpnts = tpnts.shape[0]

res_m = numpy.zeros([NITER, ntpnts, 1])
Example #19
0
def test_kis_ode():
    "Reaction-diffusion - Degradation-diffusion (TetODE)"

    NITER = 1  # The number of iterations
    DT = 0.01  # Sampling time-step
    INT = 0.11  # Sim endtime

    DCSTA = 400 * 1e-12
    DCSTB = DCSTA
    RCST = 100000.0e6

    NA0 = 10000  # Initial number of A molecules
    NB0 = NA0  # Initial number of B molecules

    SAMPLE = 5000

    # create the array of tet indices to be found at random
    tetidxs = np.zeros(SAMPLE, dtype='int')
    # further create the array of tet barycentre distance to centre
    tetrads = np.zeros(SAMPLE)

    #Small expected error
    tolerance = 1.5 / 100

    ########################################################################
    rng = srng.create('r123', 512)
    rng.initialize(1000)  # The max unsigned long

    mdl = smod.Model()

    A = smod.Spec('A', mdl)
    B = smod.Spec('B', mdl)

    volsys = smod.Volsys('vsys', mdl)

    R1 = smod.Reac('R1', volsys, lhs=[A, B], rhs=[])
    R1.setKcst(RCST)

    D_a = smod.Diff('D_a', volsys, A)
    D_a.setDcst(DCSTA)
    D_b = smod.Diff('D_b', volsys, B)
    D_b.setDcst(DCSTB)

    mesh = meshio.loadMesh('validation_rd/meshes/brick_40_4_4_STEPS')[0]
    ntets = mesh.countTets()

    VOLA = mesh.getMeshVolume() / 2.0
    VOLB = VOLA

    comp1 = sgeom.TmComp('comp1', mesh, range(ntets))
    comp1.addVolsys('vsys')

    # Now fill the array holding the tet indices to sample at random
    assert (SAMPLE <= ntets)

    numfilled = 0
    while (numfilled < SAMPLE):
        if (ntets != SAMPLE):
            max = mesh.getBoundMax()
            min = mesh.getBoundMin()

            rnx = rng.getUnfII()
            rny = rng.getUnfII()
            rnz = rng.getUnfII()

            xpnt = min[0] + (max[0] - min[0]) * rnx
            ypnt = min[1] + (max[1] - min[1]) * rny
            zpnt = min[2] + (max[2] - min[2]) * rnz

            idx = mesh.findTetByPoint([xpnt, ypnt, zpnt])

            if (idx == -1): continue
            if (idx not in tetidxs):
                tetidxs[numfilled] = idx
                numfilled += 1
        else:
            tetidxs[numfilled] = numfilled
            numfilled += 1
    tetidxs.sort()

    # Now find the distance of the centre of the tets to the Z lower face

    for i in range(SAMPLE):
        baryc = mesh.getTetBarycenter(int(tetidxs[i]))
        r = baryc[0]
        tetrads[i] = r * 1e6

    Atets = []
    Btets = []

    for t in range(ntets):
        baryx = mesh.getTetBarycenter(t)[0]
        if (baryx < 0.0):
            Atets.append(t)
            continue
        if (baryx >= 0.0):
            Btets.append(t)
            continue
        assert (False)

    sim = ssolv.TetODE(mdl, mesh, rng)
    sim.setTolerances(1.0e-3, 1.0e-3)

    tpnts = np.arange(0.0, INT, DT)
    ntpnts = tpnts.shape[0]

    resA = np.zeros((NITER, ntpnts, SAMPLE))
    resB = np.zeros((NITER, ntpnts, SAMPLE))

    for i in range(0, NITER):
        sim.setCompCount('comp1', 'A', 2 * NA0)
        sim.setCompCount('comp1', 'B', 2 * NB0)

        for t in Btets:
            sim.setTetCount(t, 'A', 0)
        for t in Atets:
            sim.setTetCount(t, 'B', 0)

        for t in range(0, ntpnts):
            sim.run(tpnts[t])
            for k in range(SAMPLE):
                resA[i, t, k] = sim.getTetCount(int(tetidxs[k]), 'A')
                resB[i, t, k] = sim.getTetCount(int(tetidxs[k]), 'B')

    itermeansA = np.mean(resA, axis=0)
    itermeansB = np.mean(resB, axis=0)

    def getdetc(t, x):
        N = 1000  # The number to represent infinity in the exponential calculation
        L = 20e-6

        concA = 0.0
        for n in range(N):
            concA += ((1.0 / (2 * n + 1)) * np.exp(
                (-(DCSTA / (20.0e-6)) * np.power(
                    (2 * n + 1), 2) * np.power(np.pi, 2) * t) / (4 * L)) *
                      np.sin(((2 * n + 1) * np.pi * x) / (2 * L)))
        concA *= ((4 * NA0 / np.pi) / (VOLA * 6.022e26)) * 1.0e6

        return concA

    tpnt_compare = [5, 10]

    for tidx in tpnt_compare:
        NBINS = 50
        radmax = 0.0
        radmin = 10.0
        for r in tetrads:
            if (r > radmax): radmax = r
            if (r < radmin): radmin = r

        rsec = (radmax - radmin) / NBINS
        binmins = np.zeros(NBINS + 1)
        tetradsbinned = np.zeros(NBINS)
        r = radmin
        bin_vols = np.zeros(NBINS)

        for b in range(NBINS + 1):
            binmins[b] = r
            if (b != NBINS): tetradsbinned[b] = r + rsec / 2.0
            r += rsec

        bin_countsA = [None] * NBINS
        bin_countsB = [None] * NBINS
        for i in range(NBINS):
            bin_countsA[i] = []
            bin_countsB[i] = []
        filled = 0

        for i in range(itermeansA[tidx].size):
            irad = tetrads[i]

            for b in range(NBINS):
                if (irad >= binmins[b] and irad < binmins[b + 1]):
                    bin_countsA[b].append(itermeansA[tidx][i])
                    bin_vols[b] += sim.getTetVol(int(tetidxs[i]))
                    filled += 1.0
                    break
        filled = 0
        for i in range(itermeansB[tidx].size):
            irad = tetrads[i]

            for b in range(NBINS):
                if (irad >= binmins[b] and irad < binmins[b + 1]):
                    bin_countsB[b].append(itermeansB[tidx][i])
                    filled += 1.0
                    break

        bin_concsA = np.zeros(NBINS)
        bin_concsB = np.zeros(NBINS)

        for c in range(NBINS):
            for d in range(bin_countsA[c].__len__()):
                bin_concsA[c] += bin_countsA[c][d]
            for d in range(bin_countsB[c].__len__()):
                bin_concsB[c] += bin_countsB[c][d]

            bin_concsA[c] /= (bin_vols[c])
            bin_concsA[c] *= (1.0e-3 / 6.022e23) * 1.0e6
            bin_concsB[c] /= (bin_vols[c])
            bin_concsB[c] *= (1.0e-3 / 6.022e23) * 1.0e6

        for i in range(NBINS):
            rad = abs(tetradsbinned[i]) * 1.0e-6

            if (tetradsbinned[i] < -5):
                # compare A
                det_conc = getdetc(tpnts[tidx], rad)
                steps_conc = bin_concsA[i]
                assert tol_funcs.tolerable(det_conc, steps_conc, tolerance)

            if (tetradsbinned[i] > 5):
                # compare B
                det_conc = getdetc(tpnts[tidx], rad)
                steps_conc = bin_concsB[i]
                assert tol_funcs.tolerable(det_conc, steps_conc, tolerance)
Example #20
0
def model(tissue):
    """ initialises model using tissue array """

    mdl = smodel.Model()
    unique_p = []
    for cell in tissue:
        [unique_p.append(p) for p in cell.prtcl_names if p not in unique_p]

    NP = []
    NPi = []
    NPR = []
    # Create particles and corresponding species
    for p in unique_p:
        # Free NPs
        NP.append(smodel.Spec('N{}'.format(p), mdl))
        # internalised NPs
        NPi.append(smodel.Spec('N{}i'.format(p), mdl))
        # complexes state: NPs bound to a cell receptor
        # NPR.append(smodel.Spec('N{}R'.format(p), mdl))

        # receptor state: 'naive' state (no bound NPs)
    R = smodel.Spec('R', mdl)
    NPR = smodel.Spec('NPR', mdl)
    d = {}
    rxn_ = {}
    dfsn_ = {}
    # Lpop where cell and particle properties are connected to reactions
    for n, cell in enumerate(tissue):
        for p_idx, p in enumerate(unique_p):
            tag = str(n) + p
            prtcl = getattr(cell, p)
            d["surfsys{}".format(tag)] = smodel.Surfsys(
                "surfsys{}".format(tag), mdl)
            d["volsys{}".format(tag)] = smodel.Volsys("volsys{}".format(tag),
                                                      mdl)
            k_diff = prtcl['D'] / (float(cell.S) * float(cell.S))

            dfsn_["frwd_{}".format(tag)] = smodel.SReac(
                "frwd_{}".format(tag),
                d["surfsys{}".format(tag)],
                ilhs=[NP[p_idx]],
                orhs=[NP[p_idx]],
                kcst=k_diff)
            dfsn_["bkwd_{}".format(tag)] = smodel.SReac(
                "bkwd_{}".format(tag),
                d["surfsys{}".format(tag)],
                olhs=[NP[p_idx]],
                irhs=[NP[p_idx]],
                kcst=k_diff)

            # binding reactions:
            if 'k_a' in prtcl:
                k_bind = prtcl['k_a']
                k_unbind = prtcl['k_d']
                k_intern = prtcl['k_i']
                rxn_["bind_{}".format(tag)] = smodel.Reac(
                    "bind_{}".format(tag),
                    d["volsys{}".format(tag)],
                    lhs=[NP[p_idx], R],
                    rhs=[NPR],
                    kcst=k_bind)
                rxn_["unbind_{}".format(tag)] = smodel.Reac(
                    "unbind_{}".format(tag),
                    d["volsys{}".format(tag)],
                    lhs=[NPR],
                    rhs=[NP[p_idx], R],
                    kcst=k_unbind)
                rxn_["intern_{}".format(tag)] = smodel.Reac(
                    "intern_{}".format(tag),
                    d["volsys{}".format(tag)],
                    lhs=[NPR],
                    rhs=[NPi[p_idx], R],
                    kcst=k_intern)

            # Diffusion
            dfsn1 = smodel.Diff('dfsn1',
                                d["volsys{}".format(tag)],
                                NP[p_idx],
                                dcst=k_diff)
            dfsn2 = smodel.Diff('dfsn2',
                                d["volsys{}".format(tag)],
                                NPR,
                                dcst=k_diff)
            dfsn3 = smodel.Diff('dfsn3',
                                d["volsys{}".format(tag)],
                                R,
                                dcst=k_diff)
            dfsn4 = smodel.Diff('dfsn4',
                                d["volsys{}".format(tag)],
                                NPi[p_idx],
                                dcst=k_diff)

    return mdl
########################################################################

mdl  = smod.Model()

A = smod.Spec('A', mdl)
B = smod.Spec('B', mdl)

volsys = smod.Volsys('vsys',mdl)

diffA = smod.Diff('diffA', volsys, A)
diffA.setDcst(DCST_A)
diffB = smod.Diff('diffB', volsys, B)
diffB.setDcst(DCST_B)

# Production
R1 = smod.Reac('R1', volsys, lhs = [A, B], rhs = [B], kcst = KCST_f)
R2 = smod.Reac('R2', volsys, lhs = [], rhs = [A], kcst = KCST_b)

geom = meshio.loadMesh('./validation_rd/meshes/'+filename)[0]

comp1 = sgeom.TmComp('comp1', geom, range(geom.ntets))
comp1.addVolsys('vsys')

rng = srng.create('mt19937', 512)
rng.initialize(int(time.time()%4294967295))

sim = ssolv.Tetexact(mdl, geom, rng)
sim.reset()

tpnts = np.arange(0.0, INT, DT)
ntpnts = tpnts.shape[0]
Example #22
0
                            kcst=k3_pmca)
PMCA_leak = smodel.SReac('PMCA_leak',
                         surfsys0,
                         slhs=[PMCA_P0],
                         srhs=[PMCA_P0],
                         irhs=[Ca],
                         kcst=kl_pmca)

# ////////////////////////////////////////////////////////////////////////////////////////////////////////
PV = smodel.Spec('PV', model)
PV_Ca = smodel.Spec('PV_Ca', model)
PV_2Ca = smodel.Spec('PV_2Ca', model)

kreac_f_PV_Ca = smodel.Reac('kreac_f_PV_Ca',
                            vsys0,
                            lhs=[PV, Ca],
                            rhs=[PV_Ca],
                            kcst=107e6)
kreac_b_PV_Ca = smodel.Reac('kreac_b_PV_Ca',
                            vsys0,
                            lhs=[PV_Ca],
                            rhs=[PV, Ca],
                            kcst=0.95)

kreac_f_PV_2Ca = smodel.Reac('kreac_f_PV_2Ca',
                             vsys0,
                             lhs=[PV_Ca, Ca],
                             rhs=[PV_2Ca],
                             kcst=107e6)
kreac_b_PV_2Ca = smodel.Reac('kreac_b_PV_2Ca',
                             vsys0,
Example #23
0
tetrads = numpy.zeros(SAMPLE)

########################################################################
rng = srng.create('mt19937', 512) 
rng.initialize(int(time.time()%4294967295)) # The max unsigned long


mdl  = smod.Model()

A = smod.Spec('A', mdl)
B = smod.Spec('B', mdl)

volsys = smod.Volsys('vsys',mdl)


R1 = smod.Reac('R1', volsys, lhs = [A,B], rhs = [])

R1.setKcst(RCST)

D_a = smod.Diff('D_a', volsys, A)
D_a.setDcst(DCSTA)
D_b = smod.Diff('D_b', volsys, B)
D_b.setDcst(DCSTB)


mesh = meshio.loadMesh('./validation_rd/meshes/brick_40_4_4_1686tets')[0]

VOLA = mesh.getMeshVolume()/2.0
VOLB = VOLA

ntets = mesh.countTets()
Example #24
0
def test_kisilevich():
    "Reaction-diffusion - Degradation-diffusion (Parallel TetOpSplit)"

    NITER = 50  # The number of iterations
    DT = 0.1  # Sampling time-step
    INT = 0.3  # Sim endtime

    DCSTA = 400 * 1e-12
    DCSTB = DCSTA
    RCST = 100000.0e6

    #NA0 = 100000    # 1000000            # Initial number of A molecules
    NA0 = 1000
    NB0 = NA0  # Initial number of B molecules

    SAMPLE = 1686

    # <1% fail with a tolerance of 7.5%
    tolerance = 7.5 / 100

    # create the array of tet indices to be found at random
    tetidxs = numpy.zeros(SAMPLE, dtype='int')
    # further create the array of tet barycentre distance to centre
    tetrads = numpy.zeros(SAMPLE)

    mdl = smod.Model()

    A = smod.Spec('A', mdl)
    B = smod.Spec('B', mdl)

    volsys = smod.Volsys('vsys', mdl)

    R1 = smod.Reac('R1', volsys, lhs=[A, B], rhs=[])

    R1.setKcst(RCST)

    D_a = smod.Diff('D_a', volsys, A)
    D_a.setDcst(DCSTA)
    D_b = smod.Diff('D_b', volsys, B)
    D_b.setDcst(DCSTB)

    mesh = meshio.loadMesh('validation_rd_mpi/meshes/brick_40_4_4_1686tets')[0]

    VOLA = mesh.getMeshVolume() / 2.0
    VOLB = VOLA

    ntets = mesh.countTets()

    acomptets = []
    bcomptets = []
    max = mesh.getBoundMax()
    min = mesh.getBoundMax()
    midz = 0.0
    compatris = set()
    compbtris = set()
    for t in range(ntets):
        barycz = mesh.getTetBarycenter(t)[0]
        tris = mesh.getTetTriNeighb(t)
        if barycz < midz:
            acomptets.append(t)
            compatris.add(tris[0])
            compatris.add(tris[1])
            compatris.add(tris[2])
            compatris.add(tris[3])
        else:
            bcomptets.append(t)
            compbtris.add(tris[0])
            compbtris.add(tris[1])
            compbtris.add(tris[2])
            compbtris.add(tris[3])

    dbset = compatris.intersection(compbtris)
    dbtris = list(dbset)

    compa = sgeom.TmComp('compa', mesh, acomptets)
    compb = sgeom.TmComp('compb', mesh, bcomptets)
    compa.addVolsys('vsys')
    compb.addVolsys('vsys')

    diffb = sgeom.DiffBoundary('diffb', mesh, dbtris)

    # Now fill the array holding the tet indices to sample at random
    assert (SAMPLE <= ntets)

    numfilled = 0
    while (numfilled < SAMPLE):
        tetidxs[numfilled] = numfilled
        numfilled += 1

    # Now find the distance of the centre of the tets to the Z lower face
    for i in range(SAMPLE):
        baryc = mesh.getTetBarycenter(int(tetidxs[i]))
        r = baryc[0]
        tetrads[i] = r * 1.0e6

    Atets = acomptets
    Btets = bcomptets

    rng = srng.create('r123', 512)
    rng.initialize(1000)

    tet_hosts = gd.binTetsByAxis(mesh, steps.mpi.nhosts)
    sim = solvmod.TetOpSplit(mdl, mesh, rng, False, tet_hosts)

    tpnts = numpy.arange(0.0, INT, DT)
    ntpnts = tpnts.shape[0]

    resA = numpy.zeros((NITER, ntpnts, SAMPLE))
    resB = numpy.zeros((NITER, ntpnts, SAMPLE))

    for i in range(0, NITER):
        sim.reset()

        sim.setDiffBoundaryDiffusionActive('diffb', 'A', True)
        sim.setDiffBoundaryDiffusionActive('diffb', 'B', True)

        sim.setCompCount('compa', 'A', NA0)
        sim.setCompCount('compb', 'B', NB0)

        for t in range(0, ntpnts):
            sim.run(tpnts[t])
            for k in range(SAMPLE):
                resA[i, t, k] = sim.getTetCount(int(tetidxs[k]), 'A')
                resB[i, t, k] = sim.getTetCount(int(tetidxs[k]), 'B')

    itermeansA = numpy.mean(resA, axis=0)
    itermeansB = numpy.mean(resB, axis=0)

    def getdetc(t, x):
        N = 1000  # The number to represent infinity in the exponential calculation
        L = 20e-6

        concA = 0.0
        for n in range(N):
            concA += ((1.0 / (2 * n + 1)) * math.exp(
                (-(DCSTA / (20.0e-6)) * math.pow(
                    (2 * n + 1), 2) * math.pow(math.pi, 2) * t) / (4 * L)) *
                      math.sin(((2 * n + 1) * math.pi * x) / (2 * L)))
        concA *= ((4 * NA0 / math.pi) / (VOLA * 6.022e26)) * 1.0e6

        return concA

    tpnt_compare = [1, 2]
    passed = True
    max_err = 0.0

    for tidx in tpnt_compare:
        NBINS = 10
        radmax = 0.0
        radmin = 10.0
        for r in tetrads:
            if (r > radmax): radmax = r
            if (r < radmin): radmin = r

        rsec = (radmax - radmin) / NBINS
        binmins = numpy.zeros(NBINS + 1)
        tetradsbinned = numpy.zeros(NBINS)
        r = radmin
        bin_vols = numpy.zeros(NBINS)

        for b in range(NBINS + 1):
            binmins[b] = r
            if (b != NBINS): tetradsbinned[b] = r + rsec / 2.0
            r += rsec

        bin_countsA = [None] * NBINS
        bin_countsB = [None] * NBINS
        for i in range(NBINS):
            bin_countsA[i] = []
            bin_countsB[i] = []
        filled = 0

        for i in range(itermeansA[tidx].size):
            irad = tetrads[i]

            for b in range(NBINS):
                if (irad >= binmins[b] and irad < binmins[b + 1]):
                    bin_countsA[b].append(itermeansA[tidx][i])
                    bin_vols[b] += sim.getTetVol(int(tetidxs[i]))
                    filled += 1.0
                    break
        filled = 0
        for i in range(itermeansB[tidx].size):
            irad = tetrads[i]

            for b in range(NBINS):
                if (irad >= binmins[b] and irad < binmins[b + 1]):
                    bin_countsB[b].append(itermeansB[tidx][i])
                    filled += 1.0
                    break

        bin_concsA = numpy.zeros(NBINS)
        bin_concsB = numpy.zeros(NBINS)

        for c in range(NBINS):
            for d in range(bin_countsA[c].__len__()):
                bin_concsA[c] += bin_countsA[c][d]
            for d in range(bin_countsB[c].__len__()):
                bin_concsB[c] += bin_countsB[c][d]

            bin_concsA[c] /= (bin_vols[c])
            bin_concsA[c] *= (1.0e-3 / 6.022e23) * 1.0e6
            bin_concsB[c] /= (bin_vols[c])
            bin_concsB[c] *= (1.0e-3 / 6.022e23) * 1.0e6

        for i in range(NBINS):
            rad = abs(tetradsbinned[i]) * 1.0e-6

            if (tetradsbinned[i] < -5):
                # compare A
                det_conc = getdetc(tpnts[tidx], rad)
                steps_conc = bin_concsA[i]
                assert tol_funcs.tolerable(det_conc, steps_conc, tolerance)

            if (tetradsbinned[i] > 5):
                # compare B
                det_conc = getdetc(tpnts[tidx], rad)
                steps_conc = bin_concsB[i]
                assert tol_funcs.tolerable(det_conc, steps_conc, tolerance)
Example #25
0
def run_sim():
    # Set up and run the simulations once, before the tests
    # analyze the results.

    ##################### First order irreversible #########################

    global KCST_foi, N_foi, tolerance_foi

    KCST_foi = 5  # The reaction constant
    N_foi = 50  # Can set count or conc

    NITER_foi = 1  # The number of iterations

    # Tolerance for the comparison:
    tolerance_foi = 1.0e-4 / 100

    ####################### First order reversible #########################

    global KCST_f_for, KCST_b_for, COUNT_for, tolerance_for

    KCST_f_for = 20.0  # The reaction constant
    KCST_b_for = 5.0

    COUNT_for = 100000  # Can set count or conc

    NITER_for = 1  # The number of iterations

    tolerance_for = 1.0e-4 / 100

    ####################### Second order irreversible A2 ###################

    global KCST_soA2, CONCA_soA2, tolerance_soA2

    KCST_soA2 = 10.0e6  # The reaction constant

    CONCA_soA2 = 10.0e-6

    NITER_soA2 = 1  # The number of iterations

    tolerance_soA2 = 1.0e-4 / 100

    ####################### Second order irreversible AA ###################

    global KCST_soAA, CONCA_soAA, CONCB_soAA, tolerance_soAA

    KCST_soAA = 5.0e6  # The reaction constant

    CONCA_soAA = 20.0e-6
    CONCB_soAA = CONCA_soAA

    NITER_soAA = 1  # The number of iterations

    tolerance_soAA = 1.0e-4 / 100

    ####################### Second order irreversible AB ###################

    global KCST_soAB, CONCA_soAB, CONCB_soAB, tolerance_soAB

    KCST_soAB = 5.0e6  # The reaction constant

    CONCA_soAB = 1.0e-6
    n_soAB = 2
    CONCB_soAB = CONCA_soAB / n_soAB

    NITER_soAB = 1  # The number of iterations

    tolerance_soAB = 1.0e-4 / 100

    ####################### Third order irreversible A3 ###################

    global KCST_toA3, CONCA_toA3, tolerance_toA3

    KCST_toA3 = 1.0e12  # The reaction constant

    CONCA_toA3 = 10.0e-6

    NITER_toA3 = 1  # The number of iterations

    tolerance_toA3 = 1.0e-4 / 100

    ####################### Third order irreversible A2B ###################

    global KCST_toA2B, CONCA_toA2B, CONCB_toA2B, tolerance_toA2B

    KCST_toA2B = 0.1e12  # The reaction constant

    CONCA_toA2B = 30.0e-6
    CONCB_toA2B = 20.0e-6

    NITER_toA2B = 1  # The number of iterations

    tolerance_toA2B = 1.0e-4 / 100

    ####################### Second order irreversible 2D ###################

    global COUNTA_so2d, COUNTB_so2d, CCST_so2d, tolerance_so2d

    COUNTA_so2d = 100.0
    n_so2d = 2.0
    COUNTB_so2d = COUNTA_so2d / n_so2d

    KCST_so2d = 10.0e10  # The reaction constant

    AREA_so2d = 10.0e-12

    NITER_so2d = 1  # The number of iterations

    tolerance_so2d = 1.0e-4 / 100

    ############################ Common parameters ########################

    global VOL

    DT = 0.1  # Sampling time-step
    INT = 1.1  # Sim endtime

    NITER_max = 1

    ########################################################################

    mdl = smod.Model()
    volsys = smod.Volsys('vsys', mdl)
    surfsys = smod.Surfsys('ssys', mdl)

    # First order irreversible
    A_foi = smod.Spec('A_foi', mdl)
    A_foi_diff = smod.Diff('A_foi_diff', volsys, A_foi, 0.01e-12)
    R1_foi = smod.Reac('R1_foi', volsys, lhs=[A_foi], rhs=[], kcst=KCST_foi)

    # First order reversible
    A_for = smod.Spec('A_for', mdl)
    B_for = smod.Spec('B_for', mdl)
    A_for_diff = smod.Diff('A_for_diff', volsys, A_for, 0.01e-12)
    B_for_diff = smod.Diff('B_for_diff', volsys, B_for, 0.01e-12)
    R1_for = smod.Reac('R1_for',
                       volsys,
                       lhs=[A_for],
                       rhs=[B_for],
                       kcst=KCST_f_for)
    R2_for = smod.Reac('R2_for',
                       volsys,
                       lhs=[B_for],
                       rhs=[A_for],
                       kcst=KCST_b_for)

    # Second order irreversible A2
    A_soA2 = smod.Spec('A_soA2', mdl)
    C_soA2 = smod.Spec('C_soA2', mdl)
    A_soA2_diff = smod.Diff('A_soA2_diff', volsys, A_soA2, 1e-12)
    R1_soA2 = smod.Reac('R1_soA2',
                        volsys,
                        lhs=[A_soA2, A_soA2],
                        rhs=[C_soA2],
                        kcst=KCST_soA2)

    # Second order irreversible AA
    A_soAA = smod.Spec('A_soAA', mdl)
    B_soAA = smod.Spec('B_soAA', mdl)
    C_soAA = smod.Spec('C_soAA', mdl)
    A_soAA_diff = smod.Diff('A_soAA_diff', volsys, A_soAA, 0.2e-12)
    B_soAA_diff = smod.Diff('B_soAA_diff', volsys, B_soAA, 0.2e-12)
    R1_soAA = smod.Reac('R1_soAA',
                        volsys,
                        lhs=[A_soAA, B_soAA],
                        rhs=[C_soAA],
                        kcst=KCST_soAA)

    # Second order irreversible AB
    A_soAB = smod.Spec('A_soAB', mdl)
    B_soAB = smod.Spec('B_soAB', mdl)
    C_soAB = smod.Spec('C_soAB', mdl)
    A_soAB_diff = smod.Diff('A_soAB_diff', volsys, A_soAB, 0.1e-12)
    B_soAB_diff = smod.Diff('B_soAB_diff', volsys, B_soAB, 0.1e-12)
    R1_soAB = smod.Reac('R1_soAB',
                        volsys,
                        lhs=[A_soAB, B_soAB],
                        rhs=[C_soAB],
                        kcst=KCST_soAB)

    # Third order irreversible A3
    A_toA3 = smod.Spec('A_toA3', mdl)
    C_toA3 = smod.Spec('C_toA3', mdl)
    A_soA3_diff = smod.Diff('A_soA3_diff', volsys, A_toA3, 0.2e-12)
    R1_toA3 = smod.Reac('R1_toA3',
                        volsys,
                        lhs=[A_toA3, A_toA3, A_toA3],
                        rhs=[C_toA3],
                        kcst=KCST_toA3)

    # Third order irreversible A2B
    A_toA2B = smod.Spec('A_toA2B', mdl)
    B_toA2B = smod.Spec('B_toA2B', mdl)
    C_toA2B = smod.Spec('C_toA2B', mdl)
    A_soA2B_diff = smod.Diff('A_soA2B_diff', volsys, A_toA2B, 0.1e-12)
    B_soA2B_diff = smod.Diff('B_soA2B_diff', volsys, B_toA2B, 0.1e-12)
    R1_toA3 = smod.Reac('R1_toA2B',
                        volsys,
                        lhs=[A_toA2B, A_toA2B, B_toA2B],
                        rhs=[C_toA2B],
                        kcst=KCST_toA2B)

    # Second order irreversible 2D
    A_so2d = smod.Spec('A_so2d', mdl)
    B_so2d = smod.Spec('B_so2d', mdl)
    C_so2d = smod.Spec('C_so2d', mdl)
    A_so2d_diff = smod.Diff('A_so2d_diff', surfsys, A_so2d, 1.0e-12)
    B_so2d_diff = smod.Diff('B_so2d_diff', surfsys, B_so2d, 1.0e-12)
    SR1_so2d = smod.SReac('SR1_so2d',
                          surfsys,
                          slhs=[A_so2d, B_so2d],
                          srhs=[C_so2d],
                          kcst=KCST_so2d)

    mesh = smeshio.importAbaqus('validation_rd/meshes/sphere_rad1_37tets.inp',
                                1e-6)[0]
    VOL = mesh.getMeshVolume()

    comp1 = sgeom.TmComp('comp1', mesh, range(mesh.ntets))
    comp1.addVolsys('vsys')
    patch1 = sgeom.TmPatch('patch1', mesh, mesh.getSurfTris(), comp1)
    patch1.addSurfsys('ssys')

    CCST_so2d = KCST_so2d / (6.02214179e23 * patch1.getArea())

    rng = srng.create('r123', 512)
    rng.initialize(1000)

    sim = ssolv.TetODE(mdl, mesh, rng)
    sim.setTolerances(1e-9, 1e-7)

    global tpnts, ntpnts
    tpnts = numpy.arange(0.0, INT, DT)
    ntpnts = tpnts.shape[0]

    res_m_foi = numpy.zeros([NITER_foi, ntpnts, 1])

    res_m_for = numpy.zeros([NITER_for, ntpnts, 2])

    res_m_soA2 = numpy.zeros([NITER_soA2, ntpnts, 2])

    res_m_soAA = numpy.zeros([NITER_soAA, ntpnts, 3])

    res_m_soAB = numpy.zeros([NITER_soAB, ntpnts, 3])

    res_m_toA3 = numpy.zeros([NITER_toA3, ntpnts, 2])

    res_m_toA2B = numpy.zeros([NITER_toA2B, ntpnts, 3])

    res_m_so2d = numpy.zeros([NITER_so2d, ntpnts, 3])

    for i in range(0, NITER_max):

        if i < NITER_foi:
            sim.setCompCount('comp1', 'A_foi', N_foi)

        if i < NITER_for:
            sim.setCompCount('comp1', 'A_for', COUNT_for)
            sim.setCompCount('comp1', 'B_for', 0.0)

        if i < NITER_soA2:
            sim.setCompConc('comp1', 'A_soA2', CONCA_soA2)

        if i < NITER_soAA:
            sim.setCompConc('comp1', 'A_soAA', CONCA_soAA)
            sim.setCompConc('comp1', 'B_soAA', CONCB_soAA)

        if i < NITER_soAB:
            sim.setCompConc('comp1', 'A_soAB', CONCA_soAB)
            sim.setCompConc('comp1', 'B_soAB', CONCB_soAB)

        if i < NITER_toA3:
            sim.setCompConc('comp1', 'A_toA3', CONCA_toA3)

        if i < NITER_toA2B:
            sim.setCompConc('comp1', 'A_toA2B', CONCA_toA2B)
            sim.setCompConc('comp1', 'B_toA2B', CONCB_toA2B)

        if i < NITER_so2d:
            sim.setPatchCount('patch1', 'A_so2d', COUNTA_so2d)
            sim.setPatchCount('patch1', 'B_so2d', COUNTB_so2d)

        for t in range(0, ntpnts):
            sim.run(tpnts[t])

            if i < NITER_foi:
                res_m_foi[i, t, 0] = sim.getCompCount('comp1', 'A_foi')

            if i < NITER_for:
                res_m_for[i, t, 0] = sim.getCompConc('comp1', 'A_for') * 1e6
                res_m_for[i, t, 1] = sim.getCompConc('comp1', 'B_for') * 1e6

            if i < NITER_soA2:
                res_m_soA2[i, t, 0] = sim.getCompConc('comp1', 'A_soA2')

            if i < NITER_soAA:
                res_m_soAA[i, t, 0] = sim.getCompConc('comp1', 'A_soAA')
                res_m_soAA[i, t, 1] = sim.getCompConc('comp1', 'B_soAA')

            if i < NITER_soAB:
                res_m_soAB[i, t, 0] = sim.getCompConc('comp1', 'A_soAB')
                res_m_soAB[i, t, 1] = sim.getCompConc('comp1', 'B_soAB')

            if i < NITER_toA3:
                res_m_toA3[i, t, 0] = sim.getCompConc('comp1', 'A_toA3')

            if i < NITER_toA2B:
                res_m_toA2B[i, t, 0] = sim.getCompConc('comp1', 'A_toA2B')
                res_m_toA2B[i, t, 1] = sim.getCompConc('comp1', 'B_toA2B')
                res_m_toA2B[i, t, 2] = sim.getCompConc('comp1', 'C_toA2B')

            if i < NITER_so2d:
                res_m_so2d[i, t, 0] = sim.getPatchCount('patch1', 'A_so2d')
                res_m_so2d[i, t, 1] = sim.getPatchCount('patch1', 'B_so2d')

    global mean_res_foi
    mean_res_foi = numpy.mean(res_m_foi, 0)

    global mean_res_for
    mean_res_for = numpy.mean(res_m_for, 0)

    global mean_res_soA2
    mean_res_soA2 = numpy.mean(res_m_soA2, 0)

    global mean_res_soAA
    mean_res_soAA = numpy.mean(res_m_soAA, 0)

    global mean_res_soAB
    mean_res_soAB = numpy.mean(res_m_soAB, 0)

    global mean_res_toA3
    mean_res_toA3 = numpy.mean(res_m_toA3, 0)

    global mean_res_toA2B
    mean_res_toA2B = numpy.mean(res_m_toA2B, 0)

    global mean_res_so2d
    mean_res_so2d = numpy.mean(res_m_so2d, 0)

    global ran_sim
    ran_sim = True
Example #26
0
def gen_model(x, flux):
    ####
    ####
    print "Create sim"
    mdl = smod.Model()

    # Defining chemical compartments
    vsys = smod.Volsys('vsys_Cyt', mdl)
    ssys = smod.Surfsys('ssys', mdl)

    ##
    ## Cytosolic molecule species
    ##
    CYT_MOL_NUM = len(x.CYT_MOL_NAME)
    CYT_MOL = []
    for i in range(CYT_MOL_NUM):
        print i, x.CYT_MOL_NAME[i]
        ns = globals()
        ns[x.CYT_MOL_NAME[i]] = smod.Spec(x.CYT_MOL_NAME[i], mdl)
        # CYT_MOL.append( smod.Spec(CYT_MOL_NAME[i], mdl) )

    ##
    ## Surface molecule species
    ##
    SUR_MOL_NUM = len(x.SUR_MOL_NAME)
    SUR_MOL = []
    for i in range(SUR_MOL_NUM):
        print i, x.SUR_MOL_NAME[i]
        ns = globals()
        ns[x.SUR_MOL_NAME[i]] = smod.Spec(x.SUR_MOL_NAME[i], mdl)

    ##
    ## Cytosolic molecule reactions
    ##
    smod.Reac('on_CB', vsys, lhs=[Ca, CB], rhs=[CBCa], kcst=x.kon_CB)
    smod.Reac('of_CB', vsys, lhs=[CBCa], rhs=[Ca, CB], kcst=x.kof_CB)

    smod.Reac('on_TN1', vsys, lhs=[Ca, N0C0], rhs=[N1C0], kcst=2 * x.kon_TN)
    smod.Reac('of_TN1', vsys, lhs=[N1C0], rhs=[Ca, N0C0], kcst=x.kof_TN)
    smod.Reac('on_RN1', vsys, lhs=[Ca, N1C0], rhs=[N2C0], kcst=x.kon_RN)
    smod.Reac('of_RN1', vsys, lhs=[N2C0], rhs=[Ca, N1C0], kcst=2 * x.kof_RN)

    smod.Reac('on_TN2', vsys, lhs=[Ca, N0C1], rhs=[N1C1], kcst=2 * x.kon_TN)
    smod.Reac('of_TN2', vsys, lhs=[N1C1], rhs=[Ca, N0C1], kcst=x.kof_TN)
    smod.Reac('on_RN2', vsys, lhs=[Ca, N1C1], rhs=[N2C1], kcst=x.kon_RN)
    smod.Reac('of_RN2', vsys, lhs=[N2C1], rhs=[Ca, N1C1], kcst=2 * x.kof_RN)

    smod.Reac('on_TN3', vsys, lhs=[Ca, N0C2], rhs=[N1C2], kcst=2 * x.kon_TN)
    smod.Reac('of_TN3', vsys, lhs=[N1C2], rhs=[Ca, N0C2], kcst=x.kof_TN)
    smod.Reac('on_RN3', vsys, lhs=[Ca, N1C2], rhs=[N2C2], kcst=x.kon_RN)
    smod.Reac('of_RN3', vsys, lhs=[N2C2], rhs=[Ca, N1C2], kcst=2 * x.kof_RN)

    smod.Reac('on_TC1', vsys, lhs=[Ca, N0C0], rhs=[N0C1], kcst=2 * x.kon_TC)
    smod.Reac('of_TC1', vsys, lhs=[N0C1], rhs=[Ca, N0C0], kcst=x.kof_TC)
    smod.Reac('on_RC1', vsys, lhs=[Ca, N0C1], rhs=[N0C2], kcst=x.kon_RC)
    smod.Reac('of_RC1', vsys, lhs=[N0C2], rhs=[Ca, N0C1], kcst=2 * x.kof_RC)

    smod.Reac('on_TC2', vsys, lhs=[Ca, N1C0], rhs=[N1C1], kcst=2 * x.kon_TC)
    smod.Reac('of_TC2', vsys, lhs=[N1C1], rhs=[Ca, N1C0], kcst=x.kof_TC)
    smod.Reac('on_RC2', vsys, lhs=[Ca, N1C1], rhs=[N1C2], kcst=x.kon_RC)
    smod.Reac('of_RC2', vsys, lhs=[N1C2], rhs=[Ca, N1C1], kcst=2 * x.kof_RC)

    smod.Reac('on_TC3', vsys, lhs=[Ca, N2C0], rhs=[N2C1], kcst=2 * x.kon_TC)
    smod.Reac('of_TC3', vsys, lhs=[N2C1], rhs=[Ca, N2C0], kcst=x.kof_TC)
    smod.Reac('on_RC3', vsys, lhs=[Ca, N2C1], rhs=[N2C2], kcst=x.kon_RC)
    smod.Reac('of_RC3', vsys, lhs=[N2C2], rhs=[Ca, N2C1], kcst=2 * x.kof_RC)

    ##
    ## Ca pump and VGCC
    ##

    smod.SReac('PA_To_PA_Ca',
               ssys,
               slhs=[PA],
               ilhs=[Ca],
               srhs=[PA_Ca],
               kcst=150 * 1e6)
    smod.SReac('PA_Ca_To_PA', ssys, slhs=[PA_Ca], srhs=[PA], kcst=12)
    smod.SReac('Leak_To_Leak_Ca',
               ssys,
               slhs=[Leak],
               srhs=[Leak],
               irhs=[Ca],
               kcst=0.015)
    smod.SReac('NR_Glu_To_NR', ssys, slhs=[NR_Glu], srhs=[NR], kcst=50)
    smod.SReac('NR_Glu_To_NR_O', ssys, slhs=[NR_Glu], srhs=[NR_O], kcst=200)
    smod.SReac('NR_O_To_NR_Glu', ssys, slhs=[NR_O], srhs=[NR_Glu], kcst=50)
    smod.SReac('NR_O_To_NR_O_Ca',
               ssys,
               slhs=[NR_O],
               srhs=[NR_O],
               irhs=[Ca],
               kcst=flux)

    ##
    ## Species on left hand side:  Surface (slhs), Outer comp (olhs), Inner comp (ilhs)
    ## Species on right hand side: Surface (srhs), Outer comp (orhs), Inner comp (irhs)
    ##

    # Diffusion rules
    smod.Diff('D_Ca', vsys, Ca, x.DCa)
    smod.Diff('D_N0C0', vsys, N0C0, x.DCaM)
    smod.Diff('D_N0C1', vsys, N0C1, x.DCaM)
    smod.Diff('D_N0C2', vsys, N0C2, x.DCaM)
    smod.Diff('D_N1C0', vsys, N1C0, x.DCaM)
    smod.Diff('D_N1C1', vsys, N1C1, x.DCaM)
    smod.Diff('D_N1C2', vsys, N1C2, x.DCaM)
    smod.Diff('D_N2C0', vsys, N2C0, x.DCaM)
    smod.Diff('D_N2C1', vsys, N2C1, x.DCaM)
    smod.Diff('D_N2C2', vsys, N2C2, x.DCaM)
    smod.Diff('D_CB', vsys, CB, x.DCB)
    smod.Diff('D_CBCa', vsys, CBCa, x.DCB)

    return mdl
Example #27
0
def test_masteq():
    "Reaction - Production and degradation (Wmdirect)"

    ########################################################################

    KCST_f = 100 / (6.022e23 * 1.0e-15)  # The reaction constant, production
    KCST_b = 10  # The reaction constant, degradation
    VOL = 1.0e-18

    DT = 0.1  # Sampling time-step
    INT = 200000.1  # Sim endtime

    # Tolerance for the comparison:
    # In tests with good code <1% fail with tolerance of 1.5%
    tolerance = 1.5 / 100

    ########################################################################

    mdl = smod.Model()

    A = smod.Spec('A', mdl)

    volsys = smod.Volsys('vsys', mdl)

    # Production
    R1 = smod.Reac('R1', volsys, lhs=[], rhs=[A], kcst=KCST_f)
    R2 = smod.Reac('R2', volsys, lhs=[A], rhs=[], kcst=KCST_b)

    geom = sgeom.Geom()

    comp1 = sgeom.Comp('comp1', geom, VOL)
    comp1.addVolsys('vsys')

    rng = srng.create('r123', 1000)
    rng.initialize(1000)

    sim = ssolv.Wmdirect(mdl, geom, rng)
    sim.reset()

    tpnts = numpy.arange(0.0, INT, DT)
    ntpnts = tpnts.shape[0]

    res = numpy.zeros([ntpnts])

    sim.reset()
    sim.setCompCount('comp1', 'A', 0)

    for t in range(0, ntpnts):
        sim.run(tpnts[t])
        res[t] = sim.getCompCount('comp1', 'A')

    def fact(x):
        return (1 if x == 0 else x * fact(x - 1))

    # Do cumulative count, but not comparing them all.
    # Don't get over 50 (I hope)
    steps_n_res = numpy.zeros(50)
    for r in res:
        steps_n_res[int(r)] += 1
    for s in range(50):
        steps_n_res[s] = steps_n_res[s] / ntpnts

    passed = True
    max_err = 0.0

    k1 = KCST_b
    k2 = KCST_f * (6.022e23 * 1.0e-15)

    # Compare 5 to 15
    for m in range(5, 16):
        analy = (1.0 / fact(m)) * math.pow((k2 / k1), m) * math.exp(-(k2 / k1))
        assert tol_funcs.tolerable(steps_n_res[m], analy, tolerance)
Example #28
0
import steps.rng as srng
import steps.solver as ssolver

##############################
# Model Setup
##############################

mdl = smodel.Model()

molA = smodel.Spec('molA', mdl)
molB = smodel.Spec('molB', mdl)
molC = smodel.Spec('molC', mdl)

vsys = smodel.Volsys('vsys', mdl)

kreac_f = smodel.Reac('kreac_f', vsys, lhs=[molA,molB], rhs=[molC], kcst=0.3e6)
kreac_b = smodel.Reac('kreac_b', vsys, lhs=[molC], rhs=[molA,molB])
kreac_b.kcst = 0.7

##############################
# Geom Setup
##############################
wmgeom = swm.Geom()

comp = swm.Comp('comp', wmgeom)
comp.addVolsys('vsys')
comp.setVol(1.6667e-21)

##############################
# RNG Setup
##############################
Example #29
0
def run_sim():
    # Set up and run the simulations once, before the tests
    # analyze the results.

    ##################### First order irreversible #########################

    global KCST_foi, N_foi, tolerance_foi

    KCST_foi = 5  # The reaction constant
    N_foi = 50  # Can set count or conc

    NITER_foi = 100000  # The number of iterations

    # Tolerance for the comparison:
    # In test runs, with good code, < 1%  will fail with a 1.5% tolerance
    tolerance_foi = 2.0 / 100

    ####################### First order reversible #########################

    global KCST_f_for, KCST_b_for, COUNT_for, tolerance_for

    KCST_f_for = 10.0  # The reaction constant
    KCST_b_for = 2.0

    COUNT_for = 100000  # Can set count or conc

    NITER_for = 10  # The number of iterations

    # In test runs, with good code, <0.1% will fail with a tolerance of 1%
    tolerance_for = 1.0 / 100

    ####################### Second order irreversible A2 ###################

    global KCST_soA2, CONCA_soA2, tolerance_soA2

    KCST_soA2 = 10.0e6  # The reaction constant

    CONCA_soA2 = 10.0e-6

    NITER_soA2 = 1000  # The number of iterations

    # In test runs, with good code, <0.1% will fail with a tolerance of 2%
    tolerance_soA2 = 1.0 / 100

    ####################### Second order irreversible AA ###################

    global KCST_soAA, CONCA_soAA, CONCB_soAA, tolerance_soAA

    KCST_soAA = 50.0e6  # The reaction constant

    CONCA_soAA = 20.0e-6
    CONCB_soAA = CONCA_soAA

    NITER_soAA = 1000  # The number of iterations

    # In test runs, with good code, <0.1% will fail with a tolerance of 1%
    tolerance_soAA = 1.0 / 100

    ####################### Second order irreversible AB ###################

    global KCST_soAB, CONCA_soAB, CONCB_soAB, tolerance_soAB

    KCST_soAB = 5.0e6  # The reaction constant

    CONCA_soAB = 1.0e-6
    n_soAB = 2
    CONCB_soAB = CONCA_soAB / n_soAB

    NITER_soAB = 1000  # The number of iterations

    # In test runs, with good code, <0.1% will fail with a tolerance of 1%
    tolerance_soAB = 1.0 / 100

    ####################### Third order irreversible A3 ###################

    global KCST_toA3, CONCA_toA3, tolerance_toA3

    KCST_toA3 = 1.0e12  # The reaction constant

    CONCA_toA3 = 100.0e-6

    NITER_toA3 = 1000  # The number of iterations

    # In test runs, with good code, <0.1% will fail with a tolerance of 1%
    tolerance_toA3 = 1.0 / 100

    ####################### Third order irreversible A2B ###################

    global KCST_toA2B, CONCA_toA2B, CONCB_toA2B, tolerance_toA2B

    KCST_toA2B = 0.1e12  # The reaction constant

    CONCA_toA2B = 30.0e-6
    CONCB_toA2B = 20.0e-6

    NITER_toA2B = 1000  # The number of iterations

    # In test runs, with good code, <0.1% will fail with a tolerance of 1%
    tolerance_toA2B = 1.0 / 100

    ####################### Second order irreversible 2D ###################

    global COUNTA_so2d, COUNTB_so2d, CCST_so2d, tolerance_so2d

    COUNTA_so2d = 100.0
    n_so2d = 2.0
    COUNTB_so2d = COUNTA_so2d / n_so2d

    KCST_so2d = 10.0e10  # The reaction constant

    AREA_so2d = 10.0e-12

    CCST_so2d = KCST_so2d / (6.02214179e23 * AREA_so2d)

    NITER_so2d = 1000  # The number of iterations

    # In tests fewer than 0.1% fail with tolerance of 2%
    tolerance_so2d = 2.0 / 100

    ############################ Common parameters ########################

    global VOL

    DT = 0.1  # Sampling time-step
    INT = 1.1  # Sim endtime
    VOL = 9.0e-18

    NITER_max = 100000

    ########################################################################

    mdl = smod.Model()
    volsys = smod.Volsys('vsys', mdl)
    surfsys = smod.Surfsys('ssys', mdl)

    # First order irreversible
    A_foi = smod.Spec('A_foi', mdl)
    R1_foi = smod.Reac('R1_foi', volsys, lhs=[A_foi], rhs=[], kcst=KCST_foi)

    # First order reversible
    A_for = smod.Spec('A_for', mdl)
    B_for = smod.Spec('B_for', mdl)
    R1_for = smod.Reac('R1_for',
                       volsys,
                       lhs=[A_for],
                       rhs=[B_for],
                       kcst=KCST_f_for)
    R2_for = smod.Reac('R2_for',
                       volsys,
                       lhs=[B_for],
                       rhs=[A_for],
                       kcst=KCST_b_for)

    # Second order irreversible A2
    A_soA2 = smod.Spec('A_soA2', mdl)
    C_soA2 = smod.Spec('C_soA2', mdl)
    R1_soA2 = smod.Reac('R1_soA2',
                        volsys,
                        lhs=[A_soA2, A_soA2],
                        rhs=[C_soA2],
                        kcst=KCST_soA2)

    # Second order irreversible AA
    A_soAA = smod.Spec('A_soAA', mdl)
    B_soAA = smod.Spec('B_soAA', mdl)
    C_soAA = smod.Spec('C_soAA', mdl)
    R1_soAA = smod.Reac('R1_soAA',
                        volsys,
                        lhs=[A_soAA, B_soAA],
                        rhs=[C_soAA],
                        kcst=KCST_soAA)

    # Second order irreversible AB
    A_soAB = smod.Spec('A_soAB', mdl)
    B_soAB = smod.Spec('B_soAB', mdl)
    C_soAB = smod.Spec('C_soAB', mdl)
    R1_soAB = smod.Reac('R1_soAB',
                        volsys,
                        lhs=[A_soAB, B_soAB],
                        rhs=[C_soAB],
                        kcst=KCST_soAB)

    # Third order irreversible A3
    A_toA3 = smod.Spec('A_toA3', mdl)
    C_toA3 = smod.Spec('C_toA3', mdl)
    R1_toA3 = smod.Reac('R1_toA3',
                        volsys,
                        lhs=[A_toA3, A_toA3, A_toA3],
                        rhs=[C_toA3],
                        kcst=KCST_toA3)

    # Third order irreversible A2B
    A_toA2B = smod.Spec('A_toA2B', mdl)
    B_toA2B = smod.Spec('B_toA2B', mdl)
    C_toA2B = smod.Spec('C_toA2B', mdl)
    R1_toA3 = smod.Reac('R1_toA2B',
                        volsys,
                        lhs=[A_toA2B, A_toA2B, B_toA2B],
                        rhs=[C_toA2B],
                        kcst=KCST_toA2B)

    # Second order irreversible 2D
    A_so2d = smod.Spec('A_so2d', mdl)
    B_so2d = smod.Spec('B_so2d', mdl)
    C_so2d = smod.Spec('C_so2d', mdl)
    SR1_so2d = smod.SReac('SR1_so2d',
                          surfsys,
                          slhs=[A_so2d, B_so2d],
                          srhs=[C_so2d],
                          kcst=KCST_so2d)

    geom = sgeom.Geom()
    comp1 = sgeom.Comp('comp1', geom, VOL)
    comp1.addVolsys('vsys')
    patch1 = sgeom.Patch('patch1', geom, comp1, area=AREA_so2d)
    patch1.addSurfsys('ssys')

    rng = srng.create('r123', 512)
    rng.initialize(1000)

    sim = ssolv.Wmdirect(mdl, geom, rng)
    sim.reset()

    global tpnts, ntpnts
    tpnts = numpy.arange(0.0, INT, DT)
    ntpnts = tpnts.shape[0]

    res_m_foi = numpy.zeros([NITER_foi, ntpnts, 1])
    res_std1_foi = numpy.zeros([ntpnts, 1])
    res_std2_foi = numpy.zeros([ntpnts, 1])

    res_m_for = numpy.zeros([NITER_for, ntpnts, 2])

    res_m_soA2 = numpy.zeros([NITER_soA2, ntpnts, 2])

    res_m_soAA = numpy.zeros([NITER_soAA, ntpnts, 3])

    res_m_soAB = numpy.zeros([NITER_soAB, ntpnts, 3])

    res_m_toA3 = numpy.zeros([NITER_toA3, ntpnts, 2])

    res_m_toA2B = numpy.zeros([NITER_toA2B, ntpnts, 3])

    res_m_so2d = numpy.zeros([NITER_so2d, ntpnts, 3])

    for i in range(0, NITER_max):
        sim.reset()

        if i < NITER_foi:
            sim.setCompCount('comp1', 'A_foi', N_foi)

        if i < NITER_for:
            sim.setCompCount('comp1', 'A_for', COUNT_for)
            sim.setCompCount('comp1', 'B_for', 0.0)

        if i < NITER_soA2:
            sim.setCompConc('comp1', 'A_soA2', CONCA_soA2)

        if i < NITER_soAA:
            sim.setCompConc('comp1', 'A_soAA', CONCA_soAA)
            sim.setCompConc('comp1', 'B_soAA', CONCB_soAA)

        if i < NITER_soAB:
            sim.setCompConc('comp1', 'A_soAB', CONCA_soAB)
            sim.setCompConc('comp1', 'B_soAB', CONCB_soAB)

        if i < NITER_toA3:
            sim.setCompConc('comp1', 'A_toA3', CONCA_toA3)

        if i < NITER_toA2B:
            sim.setCompConc('comp1', 'A_toA2B', CONCA_toA2B)
            sim.setCompConc('comp1', 'B_toA2B', CONCB_toA2B)

        if i < NITER_so2d:
            sim.setPatchCount('patch1', 'A_so2d', COUNTA_so2d)
            sim.setPatchCount('patch1', 'B_so2d', COUNTB_so2d)

        for t in range(0, ntpnts):
            sim.run(tpnts[t])

            if i < NITER_foi:
                res_m_foi[i, t, 0] = sim.getCompCount('comp1', 'A_foi')

            if i < NITER_for:
                res_m_for[i, t, 0] = sim.getCompConc('comp1', 'A_for') * 1e6
                res_m_for[i, t, 1] = sim.getCompConc('comp1', 'B_for') * 1e6

            if i < NITER_soA2:
                res_m_soA2[i, t, 0] = sim.getCompConc('comp1', 'A_soA2')

            if i < NITER_soAA:
                res_m_soAA[i, t, 0] = sim.getCompConc('comp1', 'A_soAA')
                res_m_soAA[i, t, 1] = sim.getCompConc('comp1', 'B_soAA')

            if i < NITER_soAB:
                res_m_soAB[i, t, 0] = sim.getCompConc('comp1', 'A_soAB')
                res_m_soAB[i, t, 1] = sim.getCompConc('comp1', 'B_soAB')

            if i < NITER_toA3:
                res_m_toA3[i, t, 0] = sim.getCompConc('comp1', 'A_toA3')

            if i < NITER_toA2B:
                res_m_toA2B[i, t, 0] = sim.getCompConc('comp1', 'A_toA2B')
                res_m_toA2B[i, t, 1] = sim.getCompConc('comp1', 'B_toA2B')
                res_m_toA2B[i, t, 2] = sim.getCompConc('comp1', 'C_toA2B')

            if i < NITER_so2d:
                res_m_so2d[i, t, 0] = sim.getPatchCount('patch1', 'A_so2d')
                res_m_so2d[i, t, 1] = sim.getPatchCount('patch1', 'B_so2d')

    global mean_res_foi, std_res_foi
    mean_res_foi = numpy.mean(res_m_foi, 0)
    std_res_foi = numpy.std(res_m_foi, 0)

    global mean_res_for
    mean_res_for = numpy.mean(res_m_for, 0)

    global mean_res_soA2
    mean_res_soA2 = numpy.mean(res_m_soA2, 0)

    global mean_res_soAA
    mean_res_soAA = numpy.mean(res_m_soAA, 0)

    global mean_res_soAB
    mean_res_soAB = numpy.mean(res_m_soAB, 0)

    global mean_res_toA3
    mean_res_toA3 = numpy.mean(res_m_toA3, 0)

    global mean_res_toA2B
    mean_res_toA2B = numpy.mean(res_m_toA2B, 0)

    global mean_res_so2d
    mean_res_so2d = numpy.mean(res_m_so2d, 0)

    global ran_sim
    ran_sim = True
Example #30
0
def test_masteqdiff():

    SCALE = 1.0

    KCST_f = 100e6 * SCALE  # The reaction constant, degradation
    KCST_b = (20.0e-10 * SCALE)  # The reaction constant, production

    DCST_A = 20e-12
    DCST_B = 20e-12

    B0 = 1  # The number of B moleucles

    DT = 0.1  # Sampling time-step
    INT = 50000.1  # Sim endtime

    filename = 'cube_1_1_1_73tets.inp'

    # A tolerance of 7.5% will fail <1% of the time
    tolerance = 7.5 / 100

    ########################################################################

    mdl = smod.Model()

    A = smod.Spec('A', mdl)
    B = smod.Spec('B', mdl)

    volsys = smod.Volsys('vsys', mdl)

    diffA = smod.Diff('diffA', volsys, A)
    diffA.setDcst(DCST_A)
    diffB = smod.Diff('diffB', volsys, B)
    diffB.setDcst(DCST_B)

    # Production
    R1 = smod.Reac('R1', volsys, lhs=[A, B], rhs=[B], kcst=KCST_f)
    R2 = smod.Reac('R2', volsys, lhs=[], rhs=[A], kcst=KCST_b)

    geom = meshio.loadMesh('./validation_rd/meshes/' + filename)[0]

    comp1 = sgeom.TmComp('comp1', geom, range(geom.ntets))
    comp1.addVolsys('vsys')

    rng = srng.create('mt19937', 512)
    rng.initialize(int(time.time() % 4294967295))

    sim = ssolv.Tetexact(mdl, geom, rng)
    sim.reset()

    tpnts = np.arange(0.0, INT, DT)
    ntpnts = tpnts.shape[0]

    res = np.zeros([ntpnts])
    res_std1 = np.zeros([ntpnts])
    res_std2 = np.zeros([ntpnts])

    sim.restore('./validation_cp/cp/masteq_diff')

    b_time = time.time()
    for t in range(0, ntpnts):
        sim.run(tpnts[t])
        res[t] = sim.getCompCount('comp1', 'A')

    def fact(x):
        return (1 if x == 0 else x * fact(x - 1))

    # Do cumulative count, but not comparing them all.
    # Don't get over 50 (I hope)
    steps_n_res = np.zeros(50)
    for r in res:
        steps_n_res[int(r)] += 1
    for s in range(50):
        steps_n_res[s] = steps_n_res[s] / ntpnts

    passed = True
    max_err = 0.0

    k1 = KCST_f / 6.022e23
    k2 = KCST_b * 6.022e23
    v = comp1.getVol() * 1.0e3  # litres

    for m in range(5, 11):
        analy = (1.0 / fact(m)) * np.power(
            (k2 * v * v) / (B0 * k1), m) * np.exp(-((k2 * v * v) / (k1 * B0)))
        assert (tol_funcs.tolerable(steps_n_res[m], analy, tolerance))