Beispiel #1
0
# current that will start for channels in the n4 conformational state:
OC_CaP = smodel.GHKcurr('OC_CaP', ssys, CaP_m3, Ca, virtual_oconc = Ca_oconc, computeflux = True)

#Set single channel permeability
OC_CaP.setP(CaP_P)



diff_Ca_cyt  = smodel.Diff('diff_Ca_cyt',  vsys0,    Ca,         DCST) # name, where, what, how fast
diff_Ca_ER  = smodel.Diff('diff_Ca_ER',    vsys1,    Ca,         DCST) # name, where, what, how fast

# diff_Ca_sens    = smodel.Diff('diff_Ca_sens', surfsys2, Ca_sens,  DCST) # name, where, what, how fast

# define compartments:
cyto_comp = stetmesh.TmComp('cyto_comp', mesh, CS_tet_IDs)
ER_comp = stetmesh.TmComp('ER_comp', mesh, ER_tet_IDs)

# get surf tris in comps:
CS_memb_tris = meshctrl.findSurfTrisInComp(mesh, cyto_comp)
ER_memb_tris = meshctrl.findOverlapTris(mesh, CS_tet_IDs, ER_tet_IDs)

# patches:
ER_surf = stetmesh.TmPatch('ER_surf', mesh, ER_memb_tris, icomp=ER_comp, ocomp=cyto_comp)
memb_surf = stetmesh.TmPatch('memb_surf', mesh, NotAzTris, icomp=cyto_comp, ocomp=None)
# AZ_surf     = stetmesh.TmPatch('AZ_surf',         mesh, az_tris,     icomp = cyto_comp)

# $-----$-----$-----$-----$-----$-----$-----$-----$-----$-----$-----$-----
# Create the patch and associate with surface system 'ssys'
patch = stetmesh.TmPatch('patch', mesh, NotCaSensTris, icomp = cyto_comp)
Beispiel #2
0
# define model
model = smodel.Model()
A = smodel.Spec('A', model)
surfsys = smodel.Surfsys('ssys', model)
D_a = smodel.Diff('D_a', surfsys, A)
DCST = 0.2e-9
D_a.setDcst(DCST)

mesh = meshio.importAbaqus2("mesh_tet.inp", "mesh_tri.inp", 1e-6,
                            "mesh_conf")[0]

boundary_tris = mesh.getROIData("boundary")
v1_tets = mesh.getROIData("v1_tets")

comp1 = sgeom.TmComp("comp1", mesh, v1_tets)

patch1 = sgeom.TmPatch("patch", mesh, boundary_tris, comp1)
patch1.addSurfsys("ssys")

neigh_tris = mesh.getROIData("neigh_tri")
focus_tri = mesh.getROIData("focus_tri")[0]

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

solver = solv.Tetexact(model, mesh, rng)

print "Set dcst from focus_tri to all neighbor tris to 0..."
for tri in neigh_tris:
    solver.setTriDiffD(focus_tri, "D_a", 0, tri)
Beispiel #3
0
def gen_model():
    
    mdl = smodel.Model()
    X = smodel.Spec('X', mdl)
    ssys = smodel.Surfsys('ssys', mdl)
    diff_X = smodel.Diff('diffX', ssys, X,  DCST)
    
    return mdl

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

mesh = smeshio.loadMesh('meshes/coin_10r_1h_13861')[0]

ntets = mesh.countTets()
comp = stetmesh.TmComp('cyto', mesh, range(ntets))
tet_hosts = gd.linearPartition(mesh, [steps.mpi.nhosts, 1, 1])
alltris = mesh.getSurfTris()
tri_hosts = gd.partitionTris(mesh, tet_hosts, alltris)

# Sort patch triangles as those of positive z: A +ve x, B -ve x
patchA_tris = []
patchB_tris = []
patchA_bars = set()
patchB_bars = set()

for t in alltris:
    vert0, vert1, vert2 = mesh.getTri(t)
    if (mesh.getVertex(vert0)[2] > 0.0 \
        and mesh.getVertex(vert1)[2] > 0.0 \
        and mesh.getVertex(vert2)[2] > 0.0):
def test_masteq_diff():
    "Reaction-diffusion - Production and second order degradation (Parallel TetOpSplit)"

    ### NOW   A+B-> B,  0->A (see Erban and Chapman, 2009)

    ########################################################################
    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_mpi/meshes/' + filename)[0]

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

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

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

    sim.reset()

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

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

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

    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 = 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

    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)) * math.pow(
            (k2 * v * v) / (B0 * k1), m) * math.exp(-((k2 * v * v) /
                                                      (k1 * B0)))
        assert tol_funcs.tolerable(steps_n_res[m], analy, tolerance)
                                      area=Rings_areas[i][j])

#For all patches, add the surface system

for i in range(len(Length)):
    for j in range(int(Nannulis[i])):
        if j == 0:
            rings[i][j].addSurfsys('ssys_chans')
        else:
            rings[i][j].addSurfsys('ssys_diff')

        shells[i][j].addVolsys('vsys_buffs')

########## Create an intracellular compartment i.e. cytosolic compartment

cyto_stoch = sgeom.TmComp('cyto_stoch', mesh_stoch, inner_tets)

########## Finding the triangles comprising the memberane

memb_tris = list(mesh_stoch.getSurfTris())

Compborder_triIDs = [None] * len(Length)
Compborder_tri_areas = [0.0] * len(Length)

for i in range(len(Length)):
    Compborder_triIDs[i] = []
    for j in range(len(Comp_tetIDs[i])):
        tritemp = mesh_stoch.getTetTriNeighb(Comp_tetIDs[i][j])
        for tri in tritemp:
            if tri in memb_tris:
                Compborder_triIDs[i].append(tri)
# The points along (z) axis at which to record potential
pot_pos = numpy.arange(mesh.getBoundMin()[2], mesh.getBoundMax()[2], 10e-6)
pot_n = len(pot_pos)

pot_tet = numpy.zeros(pot_n, dtype = 'uint')

i=0
for p in pot_pos:
    # Axis is aligned with z-axis
    pot_tet[i] = mesh.findTetByPoint([0.0, 0.0, pot_pos[i]])
    i=i+1

# # # # # # # # # # # # # # # GEOMETRY OBJECTS  # # # # # # # # # # # # # # # # #

# Create cytosol compartment
cyto = sgeom.TmComp('cyto', mesh, range(mesh.ntets))

# Create the patch and associate with surface system 'ssys'
patch = sgeom.TmPatch('patch', mesh, memb_tris, cyto)
patch.addSurfsys('ssys')

# Create the membrane across which the potential will be solved
membrane = sgeom.Memb('membrane', mesh, [patch], opt_method = 1)

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

# Create the random number generator
r = srng.create('mt19937',512)
r.initialize(int(time.time()%10000))

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
    def setUp(self):
        self.DCST = 0.08e-12

        self.model = smodel.Model()
        A = smodel.Spec('A', self.model)
        X = smodel.Spec('X', self.model)

        self.ssys1 = smodel.Surfsys('ssys1', self.model)
        self.ssys2 = smodel.Surfsys('ssys2', self.model)

        self.sreac = smodel.SReac('sreac',
                                  self.ssys1,
                                  slhs=[A],
                                  srhs=[A],
                                  kcst=1e5)
        self.diff1 = smodel.Diff('diffX1', self.ssys1, X, self.DCST)
        self.diff2 = smodel.Diff('diffX2', self.ssys2, X, self.DCST)

        if __name__ == "__main__":
            self.mesh = meshio.loadMesh('meshes/coin_10r_1h_13861')[0]
        else:
            self.mesh = meshio.loadMesh(
                'sdiff_bugfix_test/meshes/coin_10r_1h_13861')[0]

        ntets = self.mesh.countTets()
        self.comp = sgeom.TmComp('cyto', self.mesh, range(ntets))

        alltris = self.mesh.getSurfTris()

        patchA_tris = []
        patchB_tris = []
        patchA_bars = set()
        patchB_bars = set()

        for t in alltris:
            vert0, vert1, vert2 = self.mesh.getTri(t)
            if (self.mesh.getVertex(vert0)[2] > 0.0 \
                and self.mesh.getVertex(vert1)[2] > 0.0 \
                and self.mesh.getVertex(vert2)[2] > 0.0):
                if self.mesh.getTriBarycenter(t)[0] > 0.0:
                    patchA_tris.append(t)
                    bar = self.mesh.getTriBars(t)
                    patchA_bars.add(bar[0])
                    patchA_bars.add(bar[1])
                    patchA_bars.add(bar[2])
                else:
                    patchB_tris.append(t)
                    bar = self.mesh.getTriBars(t)
                    patchB_bars.add(bar[0])
                    patchB_bars.add(bar[1])
                    patchB_bars.add(bar[2])

        self.patchA = sgeom.TmPatch('patchA',
                                    self.mesh,
                                    patchA_tris,
                                    icomp=self.comp)
        self.patchA.addSurfsys('ssys1')
        self.patchB = sgeom.TmPatch('patchB',
                                    self.mesh,
                                    patchB_tris,
                                    icomp=self.comp)
        self.patchB.addSurfsys('ssys2')

        # Find the set of bars that connect the two patches as the intersecting bars
        barsDB = patchA_bars.intersection(patchB_bars)
        barsDB = list(barsDB)

        # Create the surface diffusion boundary
        self.diffb = sgeom.SDiffBoundary('sdiffb', self.mesh, barsDB,
                                         [self.patchA, self.patchB])

        ctetidx = self.mesh.findTetByPoint([0.0, 0.0, 0.5e-6])
        ctet_trineighbs = self.mesh.getTetTriNeighb(ctetidx)
        self.ctri_idx = UNKNOWN_TRI
        for t in ctet_trineighbs:
            if t in patchA_tris + patchB_tris:
                self.ctri_idx = t

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

        self.solver = solv.Tetexact(self.model, self.mesh, self.rng)
DCST = 0.2e-9

model = smodel.Model()
A = smodel.Spec('A', model)
volsys = smodel.Volsys('vsys', model)
D_a = smodel.Diff('D_a', volsys, A)
D_a.setDcst(DCST)
    
mesh = meshio.importAbaqus2("mesh_tet.inp", "mesh_tri.inp", 1e-6, "mesh_conf")[0]
    
boundary_tris = mesh.getROIData("boundary")
v1_tets = mesh.getROIData("v1_tets")
v2_tets = mesh.getROIData("v2_tets")
    
comp1 = sgeom.TmComp("comp1", mesh, v1_tets)
comp2 = sgeom.TmComp("comp2", mesh, v2_tets)
    
comp1.addVolsys("vsys")
comp2.addVolsys("vsys")
    
db = sgeom.DiffBoundary("boundary", mesh, boundary_tris)
rng = srng.create('mt19937', 512)
rng.initialize(int(time.time()%4294967295))
    
solver = solv.Tetexact(model, mesh, rng)

print "Set directonal dcst from comp1 to comp2, and from comp2 to comp1 to 0..."
solver.setCompCount("comp1", "A", 100)
solver.setCompCount("comp2", "A", 20)
solver.setDiffBoundaryDcst("boundary", "A", 0)
inner_tets = gettets.getcyl(mesh_stoch, 1e-6, -200e-6, 200e-6)[0]

for i in inner_tets:
    outer_tets.remove(i)
assert (outer_tets.__len__() + inner_tets.__len__() == mesh_stoch.ntets)

print outer_tets.__len__(), " tets in outer compartment"
print inner_tets.__len__(), " tets in inner compartment"

# Record voltage from the central tetrahedron
cent_tet = mesh_stoch.findTetByPoint([0.0, 0.0, 0.0])

########## Create an intracellular compartment i.e. cytosolic compartment

cyto_stoch = sgeom.TmComp('cyto_stoch', mesh_stoch, inner_tets)
cyto_stoch.addVolsys('vsys_stoch')

cyto_det = sgeom.TmComp('cyto_det', mesh_det, inner_tets)
cyto_det.addVolsys('vsys_det')

if cyl160:
    # Ensure that we use points a small distance inside the boundary:
    LENGTH = mesh_stoch.getBoundMax()[2] - mesh_stoch.getBoundMin()[2]
    boundminz = mesh_stoch.getBoundMin()[2] + LENGTH / mesh_stoch.ntets
    boundmaxz = mesh_stoch.getBoundMax()[2] - LENGTH / mesh_stoch.ntets

    memb_tris = list(mesh_stoch.getSurfTris())
    minztris = []
    maxztris = []
    for tri in memb_tris:
Beispiel #10
0
def test_kisilevich():
    "Reaction-diffusion - Degradation-diffusion (Tetexact)"

    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/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)

    sim = ssolv.Tetexact(mdl, mesh, rng)

    sim.reset()

    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)