def main(standalone=False): mfile = os.path.join(os.path.dirname(__file__), 'OSC_diff_vols.g') runtime = 4000.0 modelId = moose.loadModel(mfile, 'model', 'ee') kin = moose.element('/model/kinetics') compt1 = moose.element('/model/compartment_1') compt1.x1 += kin.x1 compt1.x0 += kin.x1 fixXreacs.fixXreacs('/model') #fixXreacs.restoreXreacs( '/model' ) #fixXreacs.fixXreacs( '/model' ) ks1 = moose.Ksolve('/model/kinetics/ksolve') ds1 = moose.Dsolve('/model/kinetics/dsolve') s1 = moose.Stoich('/model/kinetics/stoich') s1.compartment = moose.element('/model/kinetics') s1.ksolve = ks1 s1.dsolve = ds1 s1.path = '/model/kinetics/##' ks2 = moose.Ksolve('/model/compartment_1/ksolve') ds2 = moose.Dsolve('/model/compartment_1/dsolve') s2 = moose.Stoich('/model/compartment_1/stoich') s2.compartment = moose.element('/model/compartment_1') s2.ksolve = ks2 s2.dsolve = ds2 s2.path = '/model/compartment_1/##' ds2.buildMeshJunctions(ds1) moose.reinit() moose.start(runtime) # I don't have an analytic way to assess oscillations assert (countCrossings('/model/graphs/conc2/M.Co', 0.001) == 4) moose.delete('/model')
def makeModel(): # create container for model num = 1 # number of compartments model = moose.Neutral( '/model' ) compartment = moose.CylMesh( '/model/compartment' ) compartment.x1 = 1.0e-6 # Set it to a 1 micron single-voxel cylinder # create molecules and reactions s = moose.Pool( '/model/compartment/s' ) ##################################################################### # Put in endo compartment. Add molecule s endo = moose.EndoMesh( '/model/endo' ) endo.isMembraneBound = True endo.surround = compartment es = moose.Pool( '/model/endo/s' ) rXfer = moose.Reac( '/model/endo/rXfer' ) ##################################################################### moose.connect( rXfer, 'sub', s, 'reac' ) moose.connect( rXfer, 'prd', es, 'reac' ) volRatio = compartment.volume / endo.volume rXfer.Kf = 0.04 # 0.04/sec rXfer.Kb = 0.02 # 0.02/sec ##################################################################### fixXreacs.fixXreacs( '/model' ) #fixXreacs.restoreXreacs( '/model' ) #fixXreacs.fixXreacs( '/model' ) ##################################################################### # Make solvers ksolve = moose.Ksolve( '/model/compartment/ksolve' ) dsolve = moose.Dsolve( '/model/dsolve' ) eksolve = moose.Ksolve( '/model/endo/ksolve' ) edsolve = moose.Dsolve( '/model/endo/dsolve' ) stoich = moose.Stoich( '/model/compartment/stoich' ) stoich.compartment = compartment stoich.ksolve = ksolve stoich.dsolve = dsolve stoich.path = "/model/compartment/##" assert( dsolve.numPools == 1 ) s.vec.concInit = [1]*num estoich = moose.Stoich( '/model/endo/stoich' ) estoich.compartment = endo estoich.ksolve = eksolve estoich.dsolve = edsolve estoich.path = "/model/endo/##" assert( edsolve.numPools == 2 ) edsolve.buildMeshJunctions( dsolve ) plot1 = moose.Table2( '/model/plot1' ) plot2 = moose.Table2( '/model/plot2' ) moose.connect( '/model/plot1', 'requestOut', s, 'getN' ) moose.connect( '/model/plot2', 'requestOut', es, 'getN' ) plot3 = moose.Table2( '/model/plot3' ) plot4 = moose.Table2( '/model/plot4' ) moose.connect( '/model/plot3', 'requestOut', s, 'getConc' ) moose.connect( '/model/plot4', 'requestOut', es, 'getConc' )
def main(): """ This example illustrates loading and running a reaction system that spans two volumes, that is, is in different compartments. It uses a kkit model file. You can tell if it is working if you see nice relaxation oscillations. """ # the kkit reader doesn't know how to do multicompt solver setup. solver = "ee" mfile = '../Genesis_files/OSC_diff_vols.g' runtime = 3000.0 simDt = 1.0 modelId = moose.loadModel(mfile, 'model', solver) #moose.delete( '/model/kinetics/A/Stot' ) compt0 = moose.element('/model/kinetics') compt1 = moose.element('/model/compartment_1') assert (deq(compt0.volume, 2e-20)) assert (deq(compt1.volume, 1e-20)) dy = compt0.dy compt1.y1 += dy compt1.y0 = dy assert (deq(compt1.volume, 1e-20)) # We now have two cubes adjacent to each other. Compt0 has 2x vol. # Compt1 touches it. stoich0 = moose.Stoich('/model/kinetics/stoich') stoich1 = moose.Stoich('/model/compartment_1/stoich') ksolve0 = moose.Ksolve('/model/kinetics/ksolve') ksolve1 = moose.Ksolve('/model/compartment_1/ksolve') stoich0.compartment = compt0 stoich0.ksolve = ksolve0 stoich0.path = '/model/kinetics/##' stoich1.compartment = compt1 stoich1.ksolve = ksolve1 stoich1.path = '/model/compartment_1/##' #stoich0.buildXreacs( stoich1 ) print ksolve0.numLocalVoxels, ksolve0.numPools, stoich0.numAllPools assert (ksolve0.numLocalVoxels == 1) assert (ksolve0.numPools == 7) assert (stoich0.numAllPools == 6) print len(stoich0.proxyPools[stoich1]), print len(stoich1.proxyPools[stoich0]) assert (len(stoich0.proxyPools[stoich1]) == 1) assert (len(stoich1.proxyPools[stoich0]) == 1) print ksolve1.numLocalVoxels, ksolve1.numPools, stoich1.numAllPools assert (ksolve1.numLocalVoxels == 1) assert (ksolve1.numPools == 6) assert (stoich1.numAllPools == 5) stoich0.buildXreacs(stoich1) print moose.element('/model/kinetics/endo') print moose.element('/model/compartment_1/exo') moose.le('/model/compartment_1') moose.reinit() moose.start(runtime) # Display all plots. for x in moose.wildcardFind('/model/#graphs/conc#/#'): t = numpy.arange(0, x.vector.size, 1) * simDt pylab.plot(t, x.vector, label=x.name) pylab.legend() pylab.show()
def _configureSolvers(self): if not hasattr(self, 'chemid'): return if not hasattr(self, 'dendCompt'): raise BuildError("configureSolvers: no chem meshes defined.") dmksolve = moose.Ksolve(self.dendCompt.path + '/ksolve') dmdsolve = moose.Dsolve(self.dendCompt.path + '/dsolve') dmstoich = moose.Stoich(self.dendCompt.path + '/stoich') dmstoich.compartment = self.dendCompt dmstoich.ksolve = dmksolve dmstoich.dsolve = dmdsolve dmstoich.path = self.dendCompt.path + "/##" # Below we have code that only applies if there are spines # Put in spine solvers. Note that these get info from the dendCompt if hasattr(self, 'spineCompt'): if self.useGssa: smksolve = moose.Gsolve(self.spineCompt.path + '/ksolve') else: smksolve = moose.Ksolve(self.spineCompt.path + '/ksolve') smdsolve = moose.Dsolve(self.spineCompt.path + '/dsolve') smstoich = moose.Stoich(self.spineCompt.path + '/stoich') smstoich.compartment = self.spineCompt smstoich.ksolve = smksolve smstoich.dsolve = smdsolve smstoich.path = self.spineCompt.path + "/##" # Put in PSD solvers. Note that these get info from the dendCompt if self.useGssa: pmksolve = moose.Gsolve(self.psdCompt.path + '/ksolve') else: pmksolve = moose.Ksolve(self.psdCompt.path + '/ksolve') pmdsolve = moose.Dsolve(self.psdCompt.path + '/dsolve') pmstoich = moose.Stoich(self.psdCompt.path + '/stoich') pmstoich.compartment = self.psdCompt pmstoich.ksolve = pmksolve pmstoich.dsolve = pmdsolve pmstoich.path = self.psdCompt.path + "/##" # Put in cross-compartment diffusion between ksolvers dmdsolve.buildNeuroMeshJunctions(smdsolve, pmdsolve) # Put in cross-compartment reactions between ksolvers smstoich.buildXreacs(pmstoich) #pmstoich.buildXreacs( smstoich ) smstoich.buildXreacs(dmstoich) dmstoich.filterXreacs() smstoich.filterXreacs() pmstoich.filterXreacs() # set up the connections so that the spine volume scaling can happen self.elecid.setSpineAndPsdMesh(self.spineCompt, self.psdCompt) self.elecid.setSpineAndPsdDsolve(smdsolve, pmdsolve)
def test_xreac2(): mfile = os.path.join(os.path.dirname(__file__), 'OSC_diff_vols.g') runtime = 4000.0 modelId = moose.loadModel(mfile, 'model', 'ee') kin = moose.element('/model/kinetics') compt1 = moose.element('/model/compartment_1') compt1.x1 += kin.x1 compt1.x0 += kin.x1 # for r in moose.wildcardFind('/##[TYPE=Reac]'): # print("---", r) # print(r.neighbors['sub']) # print(r.neighbors['prd']) fixXreacs.fixXreacs('/model') #fixXreacs.restoreXreacs( '/model' ) #fixXreacs.fixXreacs( '/model' ) print('After fixing...') for r in moose.wildcardFind('/##[TYPE=Reac]'): print("---", r) print(r.neighbors['sub']) print(r.neighbors['prd']) ks1 = moose.Ksolve('/model/kinetics/ksolve') ds1 = moose.Dsolve('/model/kinetics/dsolve') s1 = moose.Stoich('/model/kinetics/stoich') s1.compartment = moose.element('/model/kinetics') s1.ksolve = ks1 s1.dsolve = ds1 s1.path = '/model/kinetics/##' ks2 = moose.Ksolve('/model/compartment_1/ksolve') ds2 = moose.Dsolve('/model/compartment_1/dsolve') s2 = moose.Stoich('/model/compartment_1/stoich') s2.compartment = moose.element('/model/compartment_1') s2.ksolve = ks2 s2.dsolve = ds2 s2.path = '/model/compartment_1/##' print(ds1) ds2.buildMeshJunctions(ds1) moose.reinit() moose.start(runtime) # I don't have an analytic way to assess oscillations nCrossings = countCrossings('/model/graphs/conc2/M.Co', 0.001) assert (nCrossings == 4), "Expected 4, got %d" % nCrossings moose.delete('/model')
def main(): makeModel() ''' ''' ksolve = moose.Ksolve('/model/compartment/ksolve') stoich = moose.Stoich('/model/compartment/stoich') stoich.compartment = moose.element('/model/compartment') stoich.ksolve = ksolve stoich.path = "/model/compartment/##" #solver.method = "rk5" #mesh = moose.element( "/model/compartment/mesh" ) #moose.connect( mesh, "remesh", solver, "remesh" ) ''' moose.setClock( 5, 1.0 ) # clock for the solver moose.useClock( 5, '/model/compartment/ksolve', 'process' ) ''' moose.reinit() moose.start(100.0) # Run the model for 100 seconds. func = moose.element('/model/compartment/d/func') if useY: func.expr = "-y0 + 10*y1" else: func.expr = "-x0 + 10*x1" moose.start(100.0) # Run the model for 100 seconds. #moose.showfields( '/model/compartment/d' ) #moose.showfields( '/model/compartment/d/func' ) print((func.x.value)) print((moose.element('/model/compartment/b').n)) # Iterate through all plots, dump their contents to data.plot. displayPlots() quit()
def setup_solvers(): global compt_ s = moose.Stoich('%s/stoich' % compt_.path) k = moose.Ksolve('%s/ksolve' % compt_.path) s.ksolve = k s.compartment = compt_ s.path = '%s/##' % compt_.path
def singleCompt(name, params): mod = moose.copy('/library/' + name + '/' + name, '/model') A = moose.element(mod.path + '/A') Z = moose.element(mod.path + '/Z') Z.nInit = 1 Ca = moose.element(mod.path + '/Ca') CaStim = moose.element(Ca.path + '/CaStim') runtime = params['preStimTime'] + params['stimWidth'] + params[ 'postStimTime'] steptime = 100 CaStim.expr += ' + x2 * (t > ' + str(runtime) + ' ) * ( t < ' + str( runtime + steptime) + ' )' print CaStim.expr tab = moose.Table2('/model/' + name + '/Atab') #for i in range( 10, 19 ): #moose.setClock( i, 0.01 ) ampl = moose.element(mod.path + '/ampl') phase = moose.element(mod.path + '/phase') moose.connect(tab, 'requestOut', A, 'getN') ampl.nInit = params['stimAmplitude'] * 1 phase.nInit = params['preStimTime'] ksolve = moose.Ksolve(mod.path + '/ksolve') stoich = moose.Stoich(mod.path + '/stoich') stoich.compartment = mod stoich.ksolve = ksolve stoich.path = mod.path + '/##' runtime += 2 * steptime moose.reinit() moose.start(runtime) t = np.arange(0, runtime + 1e-9, tab.dt) return name, t, tab.vector
def singleCompt(name, params): print('=============') print('[INFO] Making compartment %s' % name) mod = moose.copy('/library/' + name + '/' + name, '/model') A = moose.element(mod.path + '/A') Z = moose.element(mod.path + '/Z') Z.nInit = 1 Ca = moose.element(mod.path + '/Ca') CaStim = moose.element(Ca.path + '/CaStim') print('\n\n[INFO] CaStim %s' % CaStim.path) runtime = params['preStimTime'] + params['postStimTime'] steptime = 50 CaStim.expr += '+x2*(t>100+' + str(runtime) + ')*(t<100+' + str( runtime + steptime) + ')' print("[INFO] CaStim.expr = %s" % CaStim.expr) tab = moose.Table2('/model/' + name + '/Atab') ampl = moose.element(mod.path + '/ampl') phase = moose.element(mod.path + '/phase') moose.connect(tab, 'requestOut', A, 'getN') ampl.nInit = params['stimAmplitude'] * 1 phase.nInit = params['preStimTime'] ksolve = moose.Ksolve(mod.path + '/ksolve') stoich = moose.Stoich(mod.path + '/stoich') stoich.compartment = mod stoich.ksolve = ksolve stoich.path = mod.path + '/##' print('REINIT AND START') moose.reinit() runtime += 100 + steptime * 2 moose.start(runtime) t = np.arange(0, runtime + 1e-9, tab.dt) return name, t, tab.vector
def setupSteadyState(simdt, plotDt): stoich = moose.Stoich('/model/kinetics/stoich') ksolve = moose.Ksolve('/model/kinetics/ksolve') stoich.compartment = moose.element('/model/kinetics') stoich.ksolve = ksolve #ksolve.stoich = stoich stoich.path = "/model/kinetics/##" state = moose.SteadyState('/model/kinetics/state') #### Set clocks here #moose.useClock(4, "/model/kinetics/##[]", "process") #moose.setClock(4, float(simdt)) #moose.setClock(5, float(simdt)) #moose.useClock(5, '/model/kinetics/ksolve', 'process' ) #moose.useClock(8, '/model/graphs/#', 'process' ) #moose.setClock(8, float(plotDt)) moose.reinit() state.stoich = stoich state.showMatrices() state.convergenceCriterion = 1e-8 return ksolve, state
def main(): makeModel() ksolve = moose.Ksolve('/model/compartment/ksolve') stoich = moose.Stoich('/model/compartment/stoich') stoich.compartment = moose.element('/model/compartment') stoich.ksolve = ksolve stoich.path = "/model/compartment/##" #solver.method = "rk5" #mesh = moose.element( "/model/compartment/mesh" ) #moose.connect( mesh, "remesh", solver, "remesh" ) ''' moose.setClock( 5, 1.0 ) # clock for the solver moose.useClock( 5, '/model/compartment/ksolve', 'process' ) ''' moose.reinit() moose.start(100.0) # Run the model for 100 seconds. a = moose.element('/model/compartment/a') b = moose.element('/model/compartment/b') # move most molecules over to b b.conc = b.conc + a.conc * 0.9 a.conc = a.conc * 0.1 moose.start(100.0) # Run the model for 100 seconds. # move most molecules back to a a.conc = a.conc + b.conc * 0.99 b.conc = b.conc * 0.01 moose.start(100.0) # Run the model for 100 seconds. # Iterate through all plots, dump their contents to data.plot. displayPlots() quit()
def main(): """ This example implements a reaction-diffusion like system which is bistable and propagates losslessly. It is based on the NEURON example rxdrun.py, but incorporates more compartments and runs for a longer time. The system is implemented in a function rather than as a proper system of chemical reactions. Please see rxdReacDiffusion.py for a variant that uses a reaction plus a function object to control its rates. """ dt = 0.1 # define the geometry compt = moose.CylMesh('/cylinder') compt.r0 = compt.r1 = 1 compt.diffLength = 0.2 compt.x1 = 100 assert (compt.numDiffCompts == compt.x1 / compt.diffLength) #define the molecule. Its geometry is defined by its parent volume, cylinder c = moose.Pool('/cylinder/pool') c.diffConst = 1 # define diffusion constant # Here we set up a function calculation func = moose.Function('/cylinder/pool/func') func.expr = "-x0 * (0.3 - x0) * (1 - x0)" func.x.num = 1 #specify number of input variables. #Connect the molecules to the func moose.connect(c, 'nOut', func.x[0], 'input') #Connect the function to the pool moose.connect(func, 'valueOut', c, 'increment') #Set up solvers ksolve = moose.Ksolve('/cylinder/ksolve') dsolve = moose.Dsolve('/cylinder/dsolve') stoich = moose.Stoich('/cylinder/stoich') stoich.ksolve = ksolve stoich.dsolve = dsolve stoich.compartment = compt stoich.reacSystemPath = '/cylinder/##' #initialize x = numpy.arange(0, compt.x1, compt.diffLength) c.vec.nInit = [(q < 0.2 * compt.x1) for q in x] # Run and plot it. moose.reinit() updateDt = 50 runtime = updateDt * 4 plt = pylab.plot(x, c.vec.n, label='t = 0 ') t1 = time.time() for t in range(0, runtime - 1, updateDt): moose.start(updateDt) plt = pylab.plot(x, c.vec.n, label='t = ' + str(t + updateDt)) print(("Time = %f " % (time.time() - t1))) print(("Time = %s " % (time.time() - t1))) pylab.ylim(0, 1.05) pylab.legend() pylab.show()
def makeModel(): if len(sys.argv) == 1: useGsolve = True else: useGsolve = (sys.argv[1] == 'True') # create container for model model = moose.Neutral('model') compartment = moose.CubeMesh('/model/compartment') compartment.volume = 1e-22 # the mesh is created automatically by the compartment moose.le('/model/compartment') mesh = moose.element('/model/compartment/mesh') # create molecules and reactions a = moose.Pool('/model/compartment/a') b = moose.Pool('/model/compartment/b') # create functions of time f1 = moose.Function('/model/compartment/f1') f2 = moose.Function('/model/compartment/f2') # connect them up for reactions moose.connect(f1, 'valueOut', a, 'setConc') moose.connect(f2, 'valueOut', b, 'increment') # Assign parameters a.concInit = 0 b.concInit = 1 #f1.numVars = 1 #f2.numVars = 1 f1.expr = '1 + sin(t)' f2.expr = '10 * cos(t)' # Create the output tables graphs = moose.Neutral('/model/graphs') outputA = moose.Table2('/model/graphs/nA') outputB = moose.Table2('/model/graphs/nB') # connect up the tables moose.connect(outputA, 'requestOut', a, 'getN') moose.connect(outputB, 'requestOut', b, 'getN') # Set up the solvers if useGsolve: gsolve = moose.Gsolve('/model/compartment/gsolve') gsolve.useClockedUpdate = True else: gsolve = moose.Ksolve('/model/compartment/gsolve') stoich = moose.Stoich('/model/compartment/stoich') stoich.compartment = compartment stoich.ksolve = gsolve stoich.path = '/model/compartment/##' ''' ''' # We need a finer timestep than the default 0.1 seconds, # in order to get numerical accuracy. for i in range(10, 19): moose.setClock(i, 0.1) # for computational objects
def _configureSolvers(self): dmksolve = moose.Ksolve(self.dendCompt.path + '/ksolve') dmdsolve = moose.Dsolve(self.dendCompt.path + '/dsolve') dmstoich = moose.Stoich(self.dendCompt.path + '/stoich') dmstoich.compartment = self.dendCompt dmstoich.ksolve = dmksolve dmstoich.dsolve = dmdsolve dmstoich.path = self.dendCompt.path + "/##" print 'Dend solver: numPools = ', dmdsolve.numPools, \ ', nvox= ', self.dendCompt.mesh.num, dmksolve.numAllVoxels # Put in spine solvers. Note that these get info from the dendCompt if self.useGssa: smksolve = moose.Gsolve(self.spineCompt.path + '/ksolve') else: smksolve = moose.Ksolve(self.spineCompt.path + '/ksolve') smdsolve = moose.Dsolve(self.spineCompt.path + '/dsolve') smstoich = moose.Stoich(self.spineCompt.path + '/stoich') smstoich.compartment = self.spineCompt smstoich.ksolve = smksolve smstoich.dsolve = smdsolve smstoich.path = self.spineCompt.path + "/##" print 'spine num Pools = ', smstoich.numAllPools, \ ', nvox= ', self.spineCompt.mesh.num, smksolve.numAllVoxels # Put in PSD solvers. Note that these get info from the dendCompt if self.useGssa: pmksolve = moose.Gsolve(self.psdCompt.path + '/ksolve') else: pmksolve = moose.Ksolve(self.psdCompt.path + '/ksolve') pmdsolve = moose.Dsolve(self.psdCompt.path + '/dsolve') pmstoich = moose.Stoich(self.psdCompt.path + '/stoich') pmstoich.compartment = self.psdCompt pmstoich.ksolve = pmksolve pmstoich.dsolve = pmdsolve pmstoich.path = self.psdCompt.path + "/##" print 'psd num Pools = ', pmstoich.numAllPools, \ ', voxels=', self.psdCompt.mesh.num, pmksolve.numAllVoxels # Put in cross-compartment diffusion between ksolvers dmdsolve.buildNeuroMeshJunctions(smdsolve, pmdsolve) # Put in cross-compartment reactions between ksolvers smstoich.buildXreacs(pmstoich) smstoich.buildXreacs(dmstoich) #smstoich.buildXreacs( pmstoich ) dmstoich.filterXreacs() smstoich.filterXreacs() pmstoich.filterXreacs()
def makeModel(): # create container for model r0 = 2e-6 # m r1 = 1e-6 # m num = 100 diffLength = 1e-6 # m len = num * diffLength # m diffConst = 10e-12 # m^2/sec motorRate = 10e-6 # m/sec concA = 1 # millimolar model = moose.Neutral( 'model' ) compartment = moose.CylMesh( '/model/compartment' ) compartment.r0 = r0 compartment.r1 = r1 compartment.x0 = 0 compartment.x1 = len compartment.diffLength = diffLength assert( compartment.numDiffCompts == num ) # create molecules and reactions a = moose.Pool( '/model/compartment/a' ) b = moose.Pool( '/model/compartment/b' ) c = moose.Pool( '/model/compartment/c' ) d = moose.BufPool( '/model/compartment/d' ) r1 = moose.Reac( '/model/compartment/r1' ) moose.connect( r1, 'sub', b, 'reac' ) moose.connect( r1, 'sub', d, 'reac' ) moose.connect( r1, 'prd', c, 'reac' ) r1.Kf = 1.0 # 1/(mM.sec) r1.Kb = 1.0 # 1/sec # Assign parameters a.diffConst = diffConst b.diffConst = diffConst / 2.0 b.motorConst = motorRate c.diffConst = 0 d.diffConst = diffConst # Make solvers ksolve = moose.Ksolve( '/model/compartment/ksolve' ) dsolve = moose.Dsolve( '/model/compartment/dsolve' ) stoich = moose.Stoich( '/model/compartment/stoich' ) stoich.compartment = compartment stoich.ksolve = ksolve stoich.dsolve = dsolve os.kill( PID, signal.SIGUSR1 ) stoich.path = "/model/compartment/##" print((dsolve.numPools)) assert( dsolve.numPools == 3 ) a.vec[0].concInit = concA b.vec[0].concInit = concA c.vec[0].concInit = concA d.vec.concInit = concA / 5.0 d.vec[num-1].concInit = concA
def main(): """ funcRateHarmonicOsc illustrates the use of function objects to directly define the rates of change of pool concentration. This example shows how to set up a simple harmonic oscillator system of differential equations using the script. In normal use one would prefer to use SBML. The equations are :: p' = v - offset1 v' = -k(p - offset2) where the rates for Pools p and v are computed using Functions. Note the use of offsets. This is because MOOSE chemical systems cannot have negative concentrations. The model is set up to run using default Exponential Euler integration, and then using the GSL deterministic solver. """ makeModel() for i in range( 11, 18 ): moose.setClock( i, 0.01 ) moose.setClock( 18, 0.1 ) moose.reinit() moose.start( runtime ) # Run the model # Iterate through all plots, dump their contents to data.plot. for x in moose.wildcardFind( '/model/graphs/#' ): #x.xplot( 'scriptKineticModel.plot', x.name ) t = numpy.arange( 0, x.vector.size, 1 ) * x.dt # sec pylab.plot( t, x.vector, label=x.name ) pylab.suptitle( "Integration using ee" ) pylab.legend() pylab.figure() compt = moose.element( '/model/harmonic' ) ksolve = moose.Ksolve( '/model/harmonic/ksolve' ) stoich = moose.Stoich( '/model/harmonic/stoich' ) stoich.compartment = compt stoich.ksolve = ksolve stoich.path = '/model/harmonic/##' for i in range( 11, 18 ): moose.setClock( i, 0.1 ) moose.reinit() moose.start( runtime ) # Run the model for x in moose.wildcardFind( '/model/graphs/#' ): t = numpy.arange( 0, x.vector.size, 1 ) * x.dt # sec pylab.plot( t, x.vector, label=x.name ) pylab.suptitle( "Integration using gsl" ) pylab.legend() pylab.show() quit()
def loadChem(): chem = moose.Neutral('/model/chem') modelId = moose.loadModel( os.path.join(scriptDir, '..', 'genesis', 'chanPhosphByCaMKII.g'), '/model/chem', 'ee') stoich = moose.Stoich('/model/chem/kinetics/stoich') ksolve = moose.Ksolve('/model/chem/kinetics/ksolve') stoich.compartment = moose.element('/model/chem/kinetics') stoich.ksolve = ksolve stoich.path = "/model/chem/##"
def main(standalone=False): mfile = os.path.join(os.path.dirname(__file__), 'OSC_diff_vols.g') runtime = 4000.0 modelId = moose.loadModel(mfile, 'model', 'ee') kin = moose.element('/model/kinetics') compt1 = moose.element('/model/compartment_1') compt1.x1 += kin.x1 compt1.x0 += kin.x1 fixXreacs.fixXreacs('/model') #fixXreacs.restoreXreacs( '/model' ) #fixXreacs.fixXreacs( '/model' ) ks1 = moose.Ksolve('/model/kinetics/ksolve') ds1 = moose.Dsolve('/model/kinetics/dsolve') s1 = moose.Stoich('/model/kinetics/stoich') s1.compartment = moose.element('/model/kinetics') s1.ksolve = ks1 s1.dsolve = ds1 s1.path = '/model/kinetics/##' ks2 = moose.Ksolve('/model/compartment_1/ksolve') ds2 = moose.Dsolve('/model/compartment_1/dsolve') s2 = moose.Stoich('/model/compartment_1/stoich') s2.compartment = moose.element('/model/compartment_1') s2.ksolve = ks2 s2.dsolve = ds2 s2.path = '/model/compartment_1/##' ds2.buildMeshJunctions(ds1) moose.reinit() moose.start(runtime) # I don't have an analytic way to assess oscillations assert (countCrossings('/model/graphs/conc2/M.Co', 0.001) == 4) if standalone: # Display all plots. for x in moose.wildcardFind('/model/#graphs/conc#/#'): t = np.arange(0, x.vector.size, 1) * x.dt plt.plot(t, x.vector, label=x.name) plt.legend() plt.show() moose.delete('/model')
def setCompartmentSolver(modelRoot, solver): comptlist = dict( (c, c.volume) for c in moose.wildcardFind(modelRoot + '/##[ISA=ChemCompt]')) comptVol = {} compts = [] vol = [v for k, v in comptlist.items()] volumeSort = sorted(vol) for k, v in comptlist.items(): comptVol[k] = v for volSor in volumeSort: for a, b in comptVol.items(): if b == volSor: compts.append(a) #compts = [key for key, value in sorted(comptlist.items(), key=lambda (k,v): (v,k))] if (len(compts) == '0'): print("Atleast one compartment is required ") return else: if (len(compts) > 3): print("Warning: setSolverOnCompt Cannot handle ", len(compts), " chemical compartments\n") return elif (len(compts) > 1): positionCompt(compts) if solver != 'ee' or solver != "Exponential Euler": fixXreacs(modelRoot) for compt in compts: if solver != 'ee': if (solver == 'gsl') or (solver == 'Runge Kutta'): ksolve = moose.Ksolve(compt.path + '/ksolve') if (solver == 'gssa') or (solver == 'Gillespie'): ksolve = moose.Gsolve(compt.path + '/gsolve') dsolve = moose.Dsolve(compt.path + '/dsolve') stoich = moose.Stoich(compt.path + '/stoich') stoich.compartment = compt stoich.ksolve = ksolve stoich.dsolve = dsolve stoich.path = compt.path + "/##" ksolveList = moose.wildcardFind(modelRoot + '/##[ISA=Ksolve]') dsolveList = moose.wildcardFind(modelRoot + '/##[ISA=Dsolve]') stoichList = moose.wildcardFind(modelRoot + '/##[ISA=Stoich]') i = 0 while (i < len(dsolveList) - 1): dsolveList[i + 1].buildMeshJunctions(dsolveList[i]) i += 1 print("Solver is added to model path %s" % modelRoot) '''
def makeModel(): model = moose.Neutral('/model') # Make neuronal model. It has no channels, just for geometry cell = moose.loadModel('./branching.p', '/model/cell', 'Neutral') # We don't want the cell to do any calculations. Disable everything. for i in moose.wildcardFind('/model/cell/##'): i.tick = -1 # create container for model model = moose.element('/model') chem = moose.Neutral('/model/chem') # The naming of the compartments is dicated by the places that the # chem model expects to be loaded. compt0 = moose.NeuroMesh('/model/chem/compt0') compt0.separateSpines = 0 compt0.geometryPolicy = 'cylinder' #reacSystem = moose.loadModel( 'simpleOsc.g', '/model/chem', 'ee' ) makeChemModel(compt0) # Populate all compt with the chem system. compt0.diffLength = 1e-6 # This will be over 100 compartments. # This is the magic command that configures the diffusion compartments. compt0.subTreePath = "/model/cell/#" #compt0.cell = cell # Build the solvers. No need for diffusion in this version. ksolve0 = moose.Ksolve('/model/chem/compt0/ksolve') dsolve0 = moose.Dsolve('/model/chem/compt0/dsolve') stoich0 = moose.Stoich('/model/chem/compt0/stoich') # Configure solvers stoich0.compartment = compt0 stoich0.ksolve = ksolve0 stoich0.dsolve = dsolve0 stoich0.path = '/model/chem/compt0/#' assert (stoich0.numVarPools == 2) assert (stoich0.numProxyPools == 0) assert (stoich0.numRates == 0) moose.element('/model/chem/compt0/a[0]').concInit = aConcInit twigs = findTwigs(compt0) print(('twigs = ', twigs)) for i in twigs: e = moose.element('/model/chem/compt0/b[' + str(i) + ']') e.concInit = bConcInit # Create the output tables graphs = moose.Neutral('/model/graphs') makeTab('a_soma', '/model/chem/compt0/a[0]') makeTab('b_soma', '/model/chem/compt0/b[0]') num = twigs[0] makeTab('a_apical', '/model/chem/compt0/a[' + str(num) + ']') makeTab('b_apical', '/model/chem/compt0/b[' + str(num) + ']')
def makeModel(): # create container for model r0 = 1e-6 # m r1 = 0.5e-6 # m. Note taper. num = 200 diffLength = 1e-6 # m comptLength = num * diffLength # m diffConst = 20e-12 # m^2/sec concA = 1 # millimolar diffDt = 0.02 # for the diffusion chemDt = 0.2 # for the reaction mfile = '../../genesis/M1719.g' model = moose.Neutral( 'model' ) compartment = moose.CylMesh( '/model/kinetics' ) # load in model modelId = moose.loadModel( mfile, '/model', 'ee' ) a = moose.element( '/model/kinetics/a' ) b = moose.element( '/model/kinetics/b' ) c = moose.element( '/model/kinetics/c' ) ac = a.concInit bc = b.concInit cc = c.concInit compartment.r0 = r0 compartment.r1 = r1 compartment.x0 = 0 compartment.x1 = comptLength compartment.diffLength = diffLength assert( compartment.numDiffCompts == num ) # Assign parameters for x in moose.wildcardFind( '/model/kinetics/##[ISA=PoolBase]' ): #print 'pools: ', x, x.name x.diffConst = diffConst # Make solvers ksolve = moose.Ksolve( '/model/kinetics/ksolve' ) dsolve = moose.Dsolve( '/model/dsolve' ) # Set up clocks. moose.setClock( 10, diffDt ) for i in range( 11, 17 ): moose.setClock( i, chemDt ) stoich = moose.Stoich( '/model/kinetics/stoich' ) stoich.compartment = compartment stoich.ksolve = ksolve stoich.dsolve = dsolve stoich.path = "/model/kinetics/##" print(('dsolve.numPools, num = ', dsolve.numPools, num)) b.vec[num-1].concInit *= 1.01 # Break symmetry.
def makeSolvers(): stoich = moose.Stoich('/model/compartment/stoich') stoich.compartment = moose.element('/model/compartment') if (solver == 'gssa'): gsolve = moose.Gsolve('/model/compartment/ksolve') stoich.ksolve = gsolve else: ksolve = moose.Ksolve('/model/compartment/ksolve') stoich.ksolve = ksolve stoich.path = "/model/compartment/##" moose.setClock(5, 1.0) # clock for the solver moose.useClock(5, '/model/compartment/ksolve', 'process')
def setCompartmentSolver(modelRoot, solver): compt = moose.wildcardFind(modelRoot + '/##[ISA=ChemCompt]') if (solver == 'gsl') or (solver == 'Runge Kutta'): ksolve = moose.Ksolve(compt[0].path + '/ksolve') if (solver == 'gssa') or (solver == 'Gillespie'): ksolve = moose.Gsolve(compt[0].path + '/gsolve') if (solver != 'ee'): stoich = moose.Stoich(compt[0].path + '/stoich') stoich.compartment = compt[0] stoich.ksolve = ksolve stoich.path = compt[0].path + "/##" for x in moose.wildcardFind(modelRoot + '/data/graph#/#'): x.tick = 18
def setupSteadyState(simdt, plotDt): ksolve = moose.Ksolve('/model/kinetics/ksolve') stoich = moose.Stoich('/model/kinetics/stoich') stoich.compartment = moose.element('/model/kinetics') stoich.ksolve = ksolve stoich.path = "/model/kinetics/##" state = moose.SteadyState('/model/kinetics/state') moose.reinit() state.stoich = stoich state.showMatrices() state.convergenceCriterion = 1e-8 return ksolve, state
def setup_solvers( compt, stochastic ): global args stoich = moose.Stoich( "%s/stoich" % compt.path) if stochastic: _logger.info("Setting up Stochastic solver in %s" % compt.path ) s = moose.Gsolve('%s/gsolve' % compt.path) s.useClockedUpdate = True else: s = moose.Ksolve('%s/ksolve' % compt.path) _logger.info( 'Using method %s' % s.method ) stoich.compartment = moose.element(compt.path) stoich.ksolve = s stoich.path = '%s/##' % compt.path
def setCompartmentSolver(modelRoot, solver): """ If Solver type is 'gsl' or 'gssa' do add Solver if 'ee' nothing """ if solver != 'ee': comptlist = dict( (c.volume, c) for c in moose.wildcardFind(modelRoot + '/##[ISA=ChemCompt]')) vollist = sorted(comptlist.keys()) compts = [comptlist[key] for key in vollist] #compts = [key for key, value in sorted(comptlist.items(), key=lambda (k,v): (v,k))] if (len(compts) > 1): positionCompt(compts) fixXreacs(modelRoot) vollist = sorted(comptlist.keys()) compts = [comptlist[key] for key in vollist] #compts = [key for key, value in sorted(comptlist.items(), key=lambda (k,v): (v,k))] for compt in compts: if solver != 'ee': if solver.lower() in ['gsl', 'runge kutta', 'lsoda']: ksolve = moose.Ksolve(compt.path + '/ksolve') elif solver.lower() in ['gssa', 'gillespie']: ksolve = moose.Gsolve(compt.path + '/gsolve') if (len(compts) > 1): dsolve = moose.Dsolve(compt.path + '/dsolve') stoich = moose.Stoich(compt.path + '/stoich') stoich.ksolve = ksolve if (len(compts) > 1): stoich.dsolve = dsolve stoich.compartment = compt stoich.path = compt.path + "/##" dsolveList = moose.wildcardFind(modelRoot + '/##[ISA=Dsolve]') i = 0 while (i < len(dsolveList) - 1): dsolveList[i + 1].buildMeshJunctions(dsolveList[i]) i += 1 if not modelRoot[:1].startswith('/'): modelRoot = '/' + modelRoot print(" Solver is added to model path `%s` with `%s` solver" % (modelRoot, solver))
def switchSolvers(solver): if (moose.exists('model/kinetics/stoich')): moose.delete('/model/kinetics/stoich') moose.delete('/model/kinetics/ksolve') compt = moose.element('/model/kinetics') if (solver == 'gsl'): ksolve = moose.Ksolve('/model/kinetics/ksolve') if (solver == 'gssa'): ksolve = moose.Gsolve('/model/kinetics/ksolve') if (solver != 'ee'): stoich = moose.Stoich('/model/kinetics/stoich') stoich.compartment = compt stoich.ksolve = ksolve stoich.path = "/model/kinetics/##"
def main(): """ This example illustrates how to set up a kinetic solver and kinetic model using the scripting interface. Normally this would be done using the Shell::doLoadModel command, and normally would be coordinated by the SimManager as the base of the entire model. This example creates a bistable model having two enzymes and a reaction. One of the enzymes is autocatalytic. The model is set up to run using Exponential Euler integration. """ makeModel() ksolve = moose.Ksolve('/model/compartment/ksolve') try: ksolve.numThreads = 10 except Exception as e: print('No parallel ksolve') stoich = moose.Stoich('/model/compartment/stoich') stoich.compartment = moose.element('/model/compartment') stoich.ksolve = ksolve stoich.reacSystemPath = "/model/compartment/##" #solver.method = "rk5" #mesh = moose.element( "/model/compartment/mesh" ) #moose.connect( mesh, "remesh", solver, "remesh" ) ''' moose.setClock( 5, 1.0 ) # clock for the solver moose.useClock( 5, '/model/compartment/ksolve', 'process' ) ''' moose.reinit() moose.start(100.0) # Run the model for 100 seconds. a = moose.element('/model/compartment/a') b = moose.element('/model/compartment/b') # move most molecules over to b b.conc = b.conc + a.conc * 0.9 a.conc = a.conc * 0.1 moose.start(100.0) # Run the model for 100 seconds. # move most molecules back to a a.conc = a.conc + b.conc * 0.99 b.conc = b.conc * 0.01 moose.start(100.0) # Run the model for 100 seconds. # Iterate through all plots, dump their contents to data.plot. displayPlots() quit()
def makeSolvers(elecDt): # Put in the solvers, see how they fare. # Here we kludge in a single chem solver for the whole system. ksolve = moose.Ksolve('/model/ksolve') stoich = moose.Stoich('/model/stoich') stoich.compartment = moose.element('/model/chem/neuroMesh') stoich.ksolve = ksolve stoich.path = '/model/chem/##' moose.useClock(5, '/model/ksolve', 'init') moose.useClock(6, '/model/ksolve', 'process') # Here is the elec solver hsolve = moose.HSolve('/model/hsolve') moose.useClock(1, '/model/hsolve', 'process') hsolve.dt = elecDt hsolve.target = '/model/elec/compt'
def main(): compartment = makeModel() ksolve = moose.Ksolve('/model/compartment/ksolve') stoich = moose.Stoich('/model/compartment/stoich') stoich.compartment = compartment stoich.ksolve = ksolve stoich.path = "/model/compartment/##" state = moose.SteadyState('/model/compartment/state') moose.reinit() state.stoich = stoich state.convergenceCriterion = 1e-6 moose.seed(111) # Used when generating the samples in state space moose.reinit() moose.start(10) plot(10)