|
| 1 | +######################################################################### |
| 2 | +## This program is part of 'MOOSE', the |
| 3 | +## Messaging Object Oriented Simulation Environment. |
| 4 | +## Copyright (C) 2014 Upinder S. Bhalla. and NCBS |
| 5 | +## It is made available under the terms of the |
| 6 | +## GNU Lesser General Public License version 2.1 |
| 7 | +## See the file COPYING.LIB for the full notice. |
| 8 | +######################################################################### |
| 9 | + |
| 10 | +import os |
| 11 | +import moose |
| 12 | +print("[INFO ] using moose from %s" % moose.__file__) |
| 13 | +import numpy |
| 14 | +import matplotlib.pyplot as plt |
| 15 | +import sys |
| 16 | + |
| 17 | +sdir_ = os.path.dirname(os.path.realpath(__file__)) |
| 18 | + |
| 19 | + |
| 20 | +def runAndSavePlots( name ): |
| 21 | + runtime = 20.0 |
| 22 | + moose.reinit() |
| 23 | + moose.start( runtime ) |
| 24 | + pa = moose.Neutral( '/model/graphs/' + name ) |
| 25 | + for x in moose.wildcardFind( '/model/#graphs/conc#/#' ): |
| 26 | + if ( x.tick != -1 ): |
| 27 | + tabname = '/model/graphs/' + name + '/' + x.name + '.' + name |
| 28 | + y = moose.Table( tabname ) |
| 29 | + y.vector = x.vector |
| 30 | + y.tick = -1 |
| 31 | + |
| 32 | +# Takes args ee, gsl, or gssa |
| 33 | +def switchSolvers( solver ): |
| 34 | + if ( moose.exists( 'model/kinetics/stoich' ) ): |
| 35 | + moose.delete( '/model/kinetics/stoich' ) |
| 36 | + moose.delete( '/model/kinetics/ksolve' ) |
| 37 | + compt = moose.element( '/model/kinetics' ) |
| 38 | + if ( solver == 'gsl' ): |
| 39 | + ksolve = moose.Ksolve( '/model/kinetics/ksolve' ) |
| 40 | + if ( solver == 'gssa' ): |
| 41 | + ksolve = moose.Gsolve( '/model/kinetics/ksolve' ) |
| 42 | + if ( solver != 'ee' ): |
| 43 | + stoich = moose.Stoich( '/model/kinetics/stoich' ) |
| 44 | + stoich.compartment = compt |
| 45 | + stoich.ksolve = ksolve |
| 46 | + stoich.path = "/model/kinetics/##" |
| 47 | + |
| 48 | +def main(): |
| 49 | + """ |
| 50 | + At zero order, you can select the solver you want to use within the |
| 51 | + function moose.loadModel( filename, modelpath, solver ). |
| 52 | + Having loaded in the model, you can change the solver to use on it. |
| 53 | + This example illustrates how to assign and change solvers for a |
| 54 | + kinetic model. This process is necessary in two situations: |
| 55 | +
|
| 56 | + * If we want to change the numerical method employed, for example, |
| 57 | + from deterministic to stochastic. |
| 58 | + * If we are already using a solver, and we have changed the reaction |
| 59 | + network by adding or removing molecules or reactions. |
| 60 | +
|
| 61 | + Note that we do not have to change the solvers if the volume or |
| 62 | + reaction rates change. |
| 63 | + In this example the model is loaded in with a gsl solver. The |
| 64 | + sequence of solver calculations is: |
| 65 | +
|
| 66 | + #. gsl |
| 67 | + #. ee |
| 68 | + #. gsl |
| 69 | + #. gssa |
| 70 | + #. gsl |
| 71 | +
|
| 72 | + If you're removing the solvers, you just delete the stoichiometry |
| 73 | + object and the associated ksolve/gsolve. Should there be diffusion |
| 74 | + (a dsolve)then you should delete that too. If you're |
| 75 | + building the solvers up again, then you must do the following |
| 76 | + steps in order: |
| 77 | +
|
| 78 | + #. build up the ksolve/gsolve and stoich (any order) |
| 79 | + #. Assign stoich.ksolve |
| 80 | + #. Assign stoich.path. |
| 81 | +
|
| 82 | + See the Reaction-diffusion section should you want to do diffusion |
| 83 | + as well. |
| 84 | +
|
| 85 | + """ |
| 86 | + |
| 87 | + solver = "gsl" # Pick any of gsl, gssa, ee.. |
| 88 | + mfile = os.path.join(sdir_, '../data/kkit_objects_example.g') |
| 89 | + modelId = moose.loadModel( mfile, 'model', solver ) |
| 90 | + # Increase volume so that the stochastic solver gssa |
| 91 | + # gives an interesting output |
| 92 | + compt = moose.element( '/model/kinetics' ) |
| 93 | + compt.volume = 1e-19 |
| 94 | + runAndSavePlots( 'gsl' ) |
| 95 | + ######################################################### |
| 96 | + switchSolvers( 'ee' ) |
| 97 | + runAndSavePlots( 'ee' ) |
| 98 | + ######################################################### |
| 99 | + switchSolvers( 'gsl' ) |
| 100 | + runAndSavePlots( 'gsl2' ) |
| 101 | + ######################################################### |
| 102 | + switchSolvers( 'gssa' ) |
| 103 | + runAndSavePlots( 'gssa' ) |
| 104 | + ######################################################### |
| 105 | + switchSolvers( 'gsl' ) |
| 106 | + runAndSavePlots( 'gsl3' ) |
| 107 | + ######################################################### |
| 108 | + |
| 109 | + # Display all plots. |
| 110 | + fig = plt.figure( figsize = (12, 10) ) |
| 111 | + orig = fig.add_subplot( 511 ) |
| 112 | + gsl = fig.add_subplot( 512 ) |
| 113 | + ee = fig.add_subplot( 513 ) |
| 114 | + gsl2 = fig.add_subplot( 514 ) |
| 115 | + gssa = fig.add_subplot( 515 ) |
| 116 | + plotdt = moose.element( '/clock' ).tickDt[18] |
| 117 | + for x in moose.wildcardFind( '/model/#graphs/conc#/#' ): |
| 118 | + t = numpy.arange( 0, x.vector.size, 1 ) * plotdt |
| 119 | + orig.plot( t, x.vector, label=x.name ) |
| 120 | + for x in moose.wildcardFind( '/model/graphs/gsl/#' ): |
| 121 | + t = numpy.arange( 0, x.vector.size, 1 ) * plotdt |
| 122 | + gsl.plot( t, x.vector, label=x.name ) |
| 123 | + for x in moose.wildcardFind( '/model/graphs/ee/#' ): |
| 124 | + t = numpy.arange( 0, x.vector.size, 1 ) * plotdt |
| 125 | + ee.plot( t, x.vector, label=x.name ) |
| 126 | + for x in moose.wildcardFind( '/model/graphs/gsl2/#' ): |
| 127 | + t = numpy.arange( 0, x.vector.size, 1 ) * plotdt |
| 128 | + gsl2.plot( t, x.vector, label=x.name ) |
| 129 | + for x in moose.wildcardFind( '/model/graphs/gssa/#' ): |
| 130 | + t = numpy.arange( 0, x.vector.size, 1 ) * plotdt |
| 131 | + gssa.plot( t, x.vector, label=x.name ) |
| 132 | + plt.legend() |
| 133 | + plt.show() |
| 134 | + |
| 135 | +if __name__ == '__main__': |
| 136 | + main() |
0 commit comments