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 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 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)
def main(): # Schedule the whole lot moose.setClock(4, 0.1) # for the computational objects moose.setClock(5, 0.2) # clock for the solver moose.setClock(8, 1.0) # for the plots # The wildcard uses # for single level, and ## for recursive. #compartment = makeModel() moose.loadModel('../../genesis/M1719.g', '/model', 'ee') compartment = moose.element('model/kinetics') compartment.name = 'compartment' ksolve = moose.Ksolve('/model/compartment/ksolve') stoich = moose.Stoich('/model/compartment/stoich') stoich.compartment = compartment stoich.ksolve = ksolve #ksolve.stoich = stoich stoich.path = "/model/compartment/##" state = moose.SteadyState('/model/compartment/state') moose.useClock(5, '/model/compartment/ksolve', 'process') moose.useClock(8, '/model/graphs/#', 'process') moose.reinit() state.stoich = stoich #state.showMatrices() state.convergenceCriterion = 1e-7 for i in range(0, 50): getState(ksolve, state) moose.start(100.0) # Run the model for 100 seconds. b = moose.element('/model/compartment/b') c = moose.element('/model/compartment/c') # move most molecules over to b b.conc = b.conc + c.conc * 0.95 c.conc = c.conc * 0.05 moose.start(100.0) # Run the model for 100 seconds. # move most molecules back to a c.conc = c.conc + b.conc * 0.95 b.conc = b.conc * 0.05 moose.start(100.0) # Run the model for 100 seconds. # Iterate through all plots, dump their contents to data.plot. displayPlots() quit()
def main(): # The wildcard uses # for single level, and ## for recursive. #compartment = makeModel() moose.loadModel('../Genesis_files/M1719.cspace', '/model', 'ee') compartment = moose.element('model/kinetics') compartment.name = 'compartment' ksolve = moose.Ksolve('/model/compartment/ksolve') stoich = moose.Stoich('/model/compartment/stoich') stoich.compartment = compartment stoich.ksolve = ksolve #ksolve.stoich = stoich stoich.path = "/model/compartment/##" state = moose.SteadyState('/model/compartment/state') moose.reinit() state.stoich = stoich #state.showMatrices() state.convergenceCriterion = 1e-7 moose.le('/model/graphs') a = moose.element('/model/compartment/a') b = moose.element('/model/compartment/b') c = moose.element('/model/compartment/c') for i in range(0, 100): getState(ksolve, state) moose.start(100.0) # Run the model for 100 seconds. b = moose.element('/model/compartment/b') c = moose.element('/model/compartment/c') # move most molecules over to b b.conc = b.conc + c.conc * 0.95 c.conc = c.conc * 0.05 moose.start(100.0) # Run the model for 100 seconds. # move most molecules back to a c.conc = c.conc + b.conc * 0.95 b.conc = b.conc * 0.05 moose.start(100.0) # Run the model for 100 seconds. # Iterate through all plots, dump their contents to data.plot. displayPlots() quit()
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.showMatrices() state.convergenceCriterion = 1e-6 moose.seed(111) # Used when generating the samples in state space for i in range(0, 50): getState(ksolve, state) # Now display the states of the system at more length to compare. 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(400.0) # Run the model for 200 seconds. # Iterate through all plots, dump their contents to data.plot. displayPlots() quit()
def main(): """ This example sets up the kinetic solver and steady-state finder, on a bistable model. It looks for the fixed points 100 times, as follows: - Set up the random initial condition that fits the conservation laws - Run for 2 seconds. This should not be mathematically necessary, but for obscure numerical reasons it makes it much more likely that the steady state solver will succeed in finding a state. - Find the fixed point - Print out the fixed point vector and various diagnostics. - Run for 10 seconds. This is completely unnecessary, and is done here just so that the resultant graph will show what kind of state has been found. After it does all this, the program runs for 100 more seconds on the last found fixed point (which turns out to be a saddle node), then is hard-switched in the script to the first attractor basin from which it runs for another 100 seconds till it settles there, and then is hard-switched yet again to the second attractor and runs for 100 seconds. Looking at the output you will see many features of note: - the first attractor (stable point) and the saddle point (unstable fixed point) are both found quite often. But the second attractor is found just once. Has a very small basin of attraction. - The values found for each of the fixed points match well with the values found by running the system to steady-state at the end. - There are a large number of failures to find a fixed point. These are found and reported in the diagnostics. They show up on the plot as cases where the 10-second runs are not flat. - The values found for each of the fixed points match well with the values found by running the system to steady-state at the end. - There are a large number of failures to find a fixed point. These are found and reported in the diagnostics. They show up on the plot as cases where the 10-second runs are not flat. If you wanted to find fixed points in a production model, you would not need to do the 10-second runs, and you would need to eliminate the cases where the state-finder failed. Then you could identify the good points and keep track of how many of each were found. There is no way to guarantee that all fixed points have been found using this algorithm! You may wish to sample concentration space logarithmically rather than linearly. """ # The wildcard uses # for single level, and ## for recursive. #compartment = makeModel() moose.loadModel('../genesis/M1719.cspace', '/model', 'ee') compartment = moose.element('model/kinetics') compartment.name = 'compartment' ksolve = moose.Ksolve('/model/compartment/ksolve') stoich = moose.Stoich('/model/compartment/stoich') stoich.compartment = compartment stoich.ksolve = ksolve #ksolve.stoich = stoich stoich.path = "/model/compartment/##" state = moose.SteadyState('/model/compartment/state') moose.reinit() state.stoich = stoich #state.showMatrices() state.convergenceCriterion = 1e-7 moose.le('/model/graphs') a = moose.element('/model/compartment/a') b = moose.element('/model/compartment/b') c = moose.element('/model/compartment/c') for i in range(0, 100): getState(ksolve, state) moose.start(100.0) # Run the model for 100 seconds. b = moose.element('/model/compartment/b') c = moose.element('/model/compartment/c') # move most molecules over to b b.conc = b.conc + c.conc * 0.95 c.conc = c.conc * 0.05 moose.start(100.0) # Run the model for 100 seconds. # move most molecules back to a c.conc = c.conc + b.conc * 0.95 b.conc = b.conc * 0.05 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 builds a dose-response of a bistable model of a chemical system. It uses the kinetic solver *Ksolve* and the steady-state finder *SteadyState*. The model is set up within the script. The basic approach is to increment the control variable, **a** in this case, while monitoring **b**. The algorithm marches through a series of values of the buffered pool **a** and measures resultant values of pool **b**. At each cycle the algorithm calls the steady-state finder. Since **a** is incremented only a small amount on each step, each new steady state is (usually) quite close to the previous one. The exception is when there is a state transition. Here we plot three dose-response curves to illustrate the bistable nature of the system. On the upward going curve in blue, **a** starts low. Here, **b** follows the low arm of the curve and then jumps up to the high value at roughly *log( [a] ) = -0.55*. On the downward going curve in green, **b** follows the high arm of the curve forming a nice hysteretic loop. Eventually **b** has to fall to the low state at about *log( [a] ) = -0.83* Through nasty concentration manipulations, we find the third arm of the curve, which tracks the unstable fixed point. This is in red. We find this arm by setting an initial point close to the unstable fixed point, which the steady-state finder duly locates. We then follow a dose-response curve as with the other arms of the curve. Note that the steady-state solver doesn't always succeed in finding a good solution, despite moving only in small steps. Nevertheless the resultant curves are smooth because it gives up pretty close to the correct value, simply because the successive points are close together. Overall, the system is pretty robust despite the core root-finder computations in GSL being temperamental. In doing a production dose-response series you may wish to sample concentration space logarithmically rather than linearly. """ 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 b = moose.element( '/model/compartment/b' ) a = moose.element( '/model/compartment/a' ) c = moose.element( '/model/compartment/c' ) a.concInit = 0.1 deltaA = 0.002 num = 150 avec = [] bvec = [] moose.reinit() # Now go up. for i in range( 0, num ): moose.start( 1.0 ) # Run the model for 1 seconds. state.settle() # This function finds the steady states. avec.append( a.conc ) bvec.append( b.conc ) a.concInit += deltaA #print i, a.conc, b.conc pylab.plot( numpy.log10( avec ), numpy.log10( bvec ), label='b vs a up' ) # Now go down. avec = [] bvec = [] for i in range( 0, num ): moose.start( 1.0 ) # Run the model for 1 seconds. state.settle() # This function finds the steady states. avec.append( a.conc ) bvec.append( b.conc ) a.concInit -= deltaA #print i, a.conc, b.conc pylab.plot( numpy.log10( avec ), numpy.log10( bvec ), label='b vs a down' ) # Now aim for the middle. We do this by judiciously choosing a # start point that should be closer to the unstable fixed point. avec = [] bvec = [] a.concInit = 0.28 b.conc = 0.15 for i in range( 0, 65 ): moose.start( 1.0 ) # Run the model for 1 seconds. state.settle() # This function finds the steady states. avec.append( a.conc ) bvec.append( b.conc ) a.concInit -= deltaA #print i, a.conc, b.conc pylab.plot( numpy.log10( avec ), numpy.log10( bvec ), label='b vs a mid' ) pylab.ylim( [-1.7, 1.2] ) pylab.legend() pylab.show()
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 b = moose.element('/model/compartment/b') a = moose.element('/model/compartment/a') c = moose.element('/model/compartment/c') a.concInit = 0.1 deltaA = 0.002 num = 150 avec = [] bvec = [] moose.reinit() # Now go up. for i in range(0, num): moose.start(1.0) # Run the model for 1 seconds. state.settle() # This function finds the steady states. avec.append(a.conc) bvec.append(b.conc) a.concInit += deltaA aa, bb = avec, bvec got = np.mean(aa), np.std(aa) expected = 0.24899, 0.08660 assert np.isclose(got, expected, atol=1e-4).all(), "Got %s, expected %s" % (got, expected) print("[INFO ] Test 1 PASSED") # Now go down. avec = [] bvec = [] for i in range(0, num): moose.start(1.0) # Run the model for 1 seconds. state.settle() # This function finds the steady states. avec.append(a.conc) bvec.append(b.conc) a.concInit -= deltaA aa, bb = avec, bvec got = np.mean(aa), np.std(aa) expected = 0.251, 0.0866 assert np.isclose(got, expected, atol=1e-4).all(), "Got %s, expected %s" % (got, expected) print("[INFO ] Test 2 PASSED") # Now aim for the middle. We do this by judiciously choosing a # start point that should be closer to the unstable fixed point. avec = [] bvec = [] a.concInit = 0.28 b.conc = 0.15 for i in range(0, 65): moose.start(1.0) # Run the model for 1 seconds. state.settle() # This function finds the steady states. avec.append(a.conc) bvec.append(b.conc) a.concInit -= deltaA aa, bb = avec, bvec got = np.mean(aa), np.std(aa) expected = 0.216, 0.03752 assert np.isclose(got, expected, atol=1e-4).all(), "Got %s, expected %s" % (got, expected) print("[INFO ] Test 3 PASSED") quit()
def main(): """ This example sets up the kinetic solver and steady-state finder, on a bistable model of a chemical system. The model is set up within the script. The algorithm calls the steady-state finder 50 times with different (randomized) initial conditions, as follows: * Set up the random initial condition that fits the conservation laws * Run for 2 seconds. This should not be mathematically necessary, but for obscure numerical reasons it makes it much more likely that the steady state solver will succeed in finding a state. * Find the fixed point * Print out the fixed point vector and various diagnostics. * Run for 10 seconds. This is completely unnecessary, and is done here just so that the resultant graph will show what kind of state has been found. After it does all this, the program runs for 100 more seconds on the last found fixed point (which turns out to be a saddle node), then is hard-switched in the script to the first attractor basin from which it runs for another 100 seconds till it settles there, and then is hard-switched yet again to the second attractor and runs for 400 seconds. Looking at the output you will see many features of note: * the first attractor (stable point) and the saddle point (unstable fixed point) are both found quite often. But the second attractor is found just once. It has a very small basin of attraction. * The values found for each of the fixed points match well with the values found by running the system to steady-state at the end. * There are a large number of failures to find a fixed point. These are found and reported in the diagnostics. They show up on the plot as cases where the 10-second runs are not flat. If you wanted to find fixed points in a production model, you would not need to do the 10-second runs, and you would need to eliminate the cases where the state-finder failed. Then you could identify the good points and keep track of how many of each were found. There is no way to guarantee that all fixed points have been found using this algorithm! If there are points in an obscure corner of state space (as for the singleton second attractor convergence in this example) you may have to iterate very many times to find them. You may wish to sample concentration space logarithmically rather than linearly. """ 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.showMatrices() state.convergenceCriterion = 1e-6 moose.seed( 111 ) # Used when generating the samples in state space for i in range( 0, 50 ): getState( ksolve, state ) # Now display the states of the system at more length to compare. 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( 400.0 ) # Run the model for 200 seconds. # Iterate through all plots, dump their contents to data.plot. displayPlots() quit()