Running pde solver more than once

Extending NEURON to handle reaction-diffusion problems.

Moderators: hines, wwlytton, ramcdougal

Post Reply
ren

Running pde solver more than once

Post by ren »

Hello everyone! I am new to Neuron and RxD. Recently , I went through examples and stumbled upon scalar bi stable wave. Im using jupyter for running the code and with the first run it seems totally ok, I got all the plots as in example, but the second run already gives this error message. I am using Ubuntu.

Code: Select all

Traceback (most recent call last):
  File "/home/renat/miniconda2/envs/pde_env/lib/python2.7/site-packages/neuron/nonvint_block_supervisor.py", line 134, in nonvint_block
    c[method](*args)
  File "/home/renat/miniconda2/envs/pde_env/lib/python2.7/site-packages/neuron/rxd/rxd.py", line 586, in _w_fixed_step_solve
    def _w_fixed_step_solve(raw_dt): return _fixed_step_solve(raw_dt)
  File "/home/renat/miniconda2/envs/pde_env/lib/python2.7/site-packages/neuron/rxd/rxd.py", line 356, in _fixed_step_solve
    b = _rxd_reaction(states) - _diffusion_matrix * states
  File "/home/renat/miniconda2/envs/pde_env/lib/python2.7/site-packages/scipy/sparse/base.py", line 337, in __mul__
    raise ValueError('dimension mismatch')
ValueError: dimension mismatch

RuntimeError                              Traceback (most recent call last)
<ipython-input-4-39d358bcedc7> in <module>()
     37 
     38 for i in xrange(1, 5):
---> 39     h.continuerun(i * 25)
     40     plot_it()
     41 

RuntimeError: hoc error
Interestingly the code can be run on my MAC OS, but somehow it stores values (at least it seems so) for the second and next runnings. This can be clearly seen on the artefacts in the image
Image

My aim is to run kind of that solver but for different equation parameters, this is why I need more that one time run, but unfortunately I couldn't solve the problem by far. Can anyone help me with that?
ted
Site Admin
Posts: 5889
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Running pde solver more than once

Post by ted »

Looks like you're writing your own simulation flow control code and/or your own initialization code to launch each new simulation. Can't say anything more specific about the cause of your particular problem without seeing exactly what you're doing. However, you wouldn't be having this problem if your model's mechanisms initialize properly and you use
h.run()
to launch each new simulation.
ren

Re: Running pde solver more than once

Post by ren »

Thank you, ted! I didnt check all other examples of Neuron, because I was mainly interested in the pde part , for that ,as I said, I used the example of scalar bi stable wave, running it twice in my Jupyter notebook. The code was exactly the same, I didnt touch it. After the first run, it started to give the error message for which I had to restart kernel.
I hope its clear. If the h.run() is needed I guess it may worth adding it to the example code and update the documentation. Thank you again!
ted
Site Admin
Posts: 5889
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Running pde solver more than once

Post by ted »

If the h.run() is needed I guess it may worth adding it to the example code and update the documentation.
Lots of examples of all kinds of things are floating around on the Internet. Exactly what example code are you referring to?
ramcdougal
Posts: 224
Joined: Fri Nov 28, 2008 3:38 pm
Location: Yale School of Public Health

Re: Running pde solver more than once

Post by ramcdougal »

I assume you're referring to the Scalar Bistable Wave example in the reaction-diffusion documentation.

My guess is that you're putting the entire code into a single Jupyter cell and rerunning. Should this work? Yes, but apparently it's remembering parts of the configuration from one run to the next and getting confused. Out of curiosity, try replacing the definition of where with

Code: Select all

where = rxd.Region([dend])
and see if that avoids the issue, since it explicitly refers to one section instead of all the sections. (Again, there should only ever be one section if you're rerunning but the errors you're reporting make me think for some reason you have two. I'll try to track down why.)

I'd recommend splitting the example into two Jupyter cells: one for the setup and one for initializing and running the simulation.

Alternatively and probably better, have the script control the multiple runs... e.g. here I run the simulation twice, once with a parameter of 0.2 and once with a parameter of 0.4:

Code: Select all

from neuron import h, rxd
import numpy
from matplotlib import pyplot

# needed for standard run system
h.load_file('stdrun.hoc')

dend = h.Section(name='dend')
dend.nseg = 101

# WHERE the dynamics will take place
where = rxd.Region([dend])

# WHO the actors are
u = rxd.Species(where, d=1, initial=0)

# parameter controlling wave spread; will need to set anew for each simulation after initialization
alpha = rxd.Parameter(where)

# HOW they act
bistable_reaction = rxd.Rate(u, -u * (1 - u) * (alpha - u))

def plot_it(color='k'):
    y = u.nodes.concentration
    x = u.nodes.x

    # convert x from normalized position to microns
    x = dend.L * numpy.array(x)

    pyplot.plot(x, y, color)

def run_sim(myalpha):
    # setup initial conditions for simulation
    h.finitialize(-65)    # not actually using membrane potential here, but good practice to specify
    alpha.nodes.value = myalpha
    h.CVode().re_init()   # only needed for variable step, but harmless in fixed step case
    for node in u.nodes:
        if node.x < .2: node.concentration = 1

    plot_it('r')

    for i in xrange(1, 5):
        h.continuerun(i * 25)
        plot_it()

    pyplot.ylim([0, 1.1])
    pyplot.ylabel('alpha = %g' % myalpha)

pyplot.subplot(2, 1, 1)
run_sim(0.2)
pyplot.subplot(2, 1, 2)
run_sim(0.4)

pyplot.show()
If you want to run a new simulation in Jupyter interactively, you'd just call e.g. run_sim(0.3) and it'll plot the new figure.
Tuoma
Posts: 10
Joined: Mon Apr 23, 2018 6:59 am

Re: Running pde solver more than once

Post by Tuoma »

I've got another issue related to this topic. I used to run my simulator from the command line (even inside a python script) to make sure there are no issues with reinitialization of the RxD model. Now I'm trying to rewrite the code to be run as a normal python function to avoid i/o overload on the hard drive. But this seems to be more difficult than I thought.

My main issue is now that RxD seems to be rather slow when it comes to changing the model parameters. I'm keeping all the reactions fixed but I'm allowing the reaction rates to be changed between the successive simulations. But changing a single reaction rate takes 3-4 seconds, as RxD seems to do some kind of compilation. Is there a way to forbid the compilation and do all the changes to reaction rates and compile only after all parameter changes?

Edit: In case there are many ways to change the reaction rates, the way I'm doing it is by changing the f_rate and b_rate parameters.

Edit: One more thing: if actually a compilation of the rxd model is anyway needed, then I guess running the model will anyway overload the i/o and the whole point of running everything inline within python becomes unnecessary...
ramcdougal
Posts: 224
Joined: Fri Nov 28, 2008 3:38 pm
Location: Yale School of Public Health

Re: Running pde solver more than once

Post by ramcdougal »

When you change f_rate and b_rate, NEURON interprets that as changing the model, triggering a recompilation.

If instead you used an rxd.Parameter, that just changes a parameter value, requiring no change to the model. See the bottom of this example which covers both changing initial conditions and parameters. (Here there's only one location, but the `.value` can be set for any node location.)
Post Reply