from neuron import h, crxd as rxd from matplotlib import pyplot import numpy import __main__ name = __main__.__file__ if name[-3 :] == '.py': name = name[: -3] h.load_file('stdrun.hoc') dend = h.Section() dend.diam = 2 dend.nseg = 101 dend.L = 100 rxd.set_solve_type(dimension=3) diff_constant = 1 r = rxd.Region(h.allsec(),dx=0.5) ca = rxd.Species(r, d=diff_constant, initial=lambda node: 1 if 0.4 < node.x < 0.6 else 0) h.finitialize() initial_amount = (numpy.array(ca.nodes.concentration) * numpy.array(ca.nodes.volume)).sum() for t in [25, 50, 75, 100, 125]: h.continuerun(t) pyplot.plot([nd.x for nd in ca.nodes], ca.nodes.concentration) final_amount = (numpy.array(ca.nodes.concentration) * numpy.array(ca.nodes.volume)).sum()
def clearAll(): """ Function to clear all sim objects in memory """ from .. import sim import numpy as np # clean up sim.pc.barrier() sim.pc.gid_clear() # clear previous gid settings # clean cells and simData in all nodes if hasattr(sim, 'net'): sim.clearObj([cell.__dict__ if hasattr(cell, '__dict__') else cell for cell in sim.net.cells]) if hasattr(sim, 'simData'): if 'stims' in list(sim.simData.keys()): sim.clearObj([stim for stim in sim.simData['stims']]) for key in list(sim.simData.keys()): del sim.simData[key] if hasattr(sim, 'net'): for c in sim.net.cells: del c for p in sim.net.pops: del p del sim.net.params # clean cells and simData gathered in master node if hasattr(sim, 'rank'): if sim.rank == 0: if hasattr(sim, 'net'): if hasattr(sim.net, 'allCells'): sim.clearObj([cell.__dict__ if hasattr(cell, '__dict__') else cell for cell in sim.net.allCells]) if hasattr(sim, 'allSimData'): for key in list(sim.allSimData.keys()): del sim.allSimData[key] if 'stims' in list(sim.allSimData.keys()): sim.clearObj([stim for stim in sim.allSimData['stims']]) if hasattr(sim.net, 'allCells'): for c in sim.net.allCells: del c del sim.net.allCells if hasattr(sim.net, 'allPops'): for p in sim.net.allPops: del p del sim.net.allPops if hasattr(sim, 'allSimData'): del sim.allSimData import matplotlib matplotlib.pyplot.clf() matplotlib.pyplot.close('all') # clean rxd components if hasattr(sim.net, 'rxd'): sim.clearObj(sim.net.rxd) if 'rxd' not in globals(): try: from neuron import crxd as rxd except: pass #try: for r in rxd.rxd._all_reactions[:]: if r(): rxd.rxd._unregister_reaction(r) for s in rxd.species._all_species: if s(): s().__del__() rxd.region._all_regions = [] rxd.region._region_count = 0 rxd.region._c_region_lookup = None rxd.species._species_counts = 0 rxd.section1d._purge_cptrs() rxd.initializer.has_initialized = False rxd.rxd.free_conc_ptrs() rxd.rxd.free_curr_ptrs() rxd.rxd.rxd_include_node_flux1D(0, None, None, None) rxd.species._has_1d = False rxd.species._has_3d = False rxd.rxd._zero_volume_indices = np.ndarray(0, dtype=np.int_) rxd.set_solve_type(dimension=1) # clear reactions in case next sim does not use rxd rxd.rxd.clear_rates() for obj in rxd.__dict__: sim.clearObj(obj) #except: # pass if hasattr(sim, 'net'): del sim.net import gc; gc.collect()
dend1 = h.Section() dend1.diam = 2 dend1.nseg = 101 dend1.L = 50 dend2 = h.Section() dend2.diam = 2 dend2.nseg = 101 dend2.L = 50 dend2.connect(dend1) diff_constant = 1 h.cvode_active(True) r = rxd.Region(h.allsec(), dx=0.25) rxd.set_solve_type([dend1], dimension=3) ca = rxd.Species( r, d=diff_constant, atolscale=0.1, initial=lambda node: 1 if (0.8 < node.x and node.segment in dend1) or (node.x < 0.2 and node.segment in dend2) else 0, ) bistable_reaction = rxd.Rate(ca, -ca * (1 - ca) * (0.01 - ca)) h.finitialize() for i in range(2): h.fadvance() rxd.nthread(2) for i in range(2, 4):