def __init__(self, regions=None, d=0, name=None, charge=0, initial=None): """s = rxd.Species(regions, d = 0, name = None, charge = 0, initial = None) Declare a species. Parameters: regions -- a Region or list of Region objects containing the species d -- the diffusion constant of the species (optional; default is 0, i.e. non-diffusing) name -- the name of the Species; used for syncing with HOC (optional; default is none) charge -- the charge of the Species (optional; default is 0) initial -- the initial concentration or None (if None, then imports from HOC if the species is defined at finitialize, else 0) Note: charge must match the charges specified in NMODL files for the same ion, if any.""" import neuron import ctypes from . import rxd # if there is a species, then rxd is being used, so we should register # this function may be safely called many times rxd._do_nbs_register() # TODO: check if "name" already defined elsewhere (if non-None) # if so, make sure other fields consistent, expand regions as appropriate self._allow_setting = True self.regions = regions self.d = d self.name = name self.charge = charge self.initial = initial _all_species.append(weakref.ref(self)) # declare an update to the structure of the model (the number of differential equations has changed) neuron.nrn_dll_sym('structure_change_cnt', ctypes.c_int).value += 1 # initialize self if the rest of rxd is already initialized if initializer.is_initialized(): if _has_3d: if isinstance(regions, region.Region) and not regions._secs1d: pass elif hasattr(regions, '__len__') and all(not r._secs1d for r in regions): pass else: # TODO: remove this limitation # one strategy would be to just redo the whole thing; what are the implications of that? # (pointers would be invalid; anything else?) raise RxDException( 'Currently cannot add species containing 1D after 3D species defined and initialized. To work-around: reorder species definition.' ) self._do_init()
def __init__(self, regions=None, d=0, name=None, charge=0, initial=None): """s = rxd.Species(regions, d = 0, name = None, charge = 0, initial = None) Declare a species. Parameters: regions -- a Region or list of Region objects containing the species d -- the diffusion constant of the species (optional; default is 0, i.e. non-diffusing) name -- the name of the Species; used for syncing with HOC (optional; default is none) charge -- the charge of the Species (optional; default is 0) initial -- the initial concentration or None (if None, then imports from HOC if the species is defined at finitialize, else 0) Note: charge must match the charges specified in NMODL files for the same ion, if any.""" import neuron import ctypes from . import rxd # if there is a species, then rxd is being used, so we should register # this function may be safely called many times rxd._do_nbs_register() # TODO: check if "name" already defined elsewhere (if non-None) # if so, make sure other fields consistent, expand regions as appropriate self._allow_setting = True self.regions = regions self.d = d self.name = name self.charge = charge self.initial = initial _all_species.append(weakref.ref(self)) # declare an update to the structure of the model (the number of differential equations has changed) neuron.nrn_dll_sym('structure_change_cnt', ctypes.c_int).value += 1 # initialize self if the rest of rxd is already initialized if initializer.is_initialized(): if _has_3d: if isinstance(regions, region.Region) and not regions._secs1d: pass elif hasattr(regions, '__len__') and all(not r._secs1d for r in regions): pass else: # TODO: remove this limitation # one strategy would be to just redo the whole thing; what are the implications of that? # (pointers would be invalid; anything else?) raise RxDException('Currently cannot add species containing 1D after 3D species defined and initialized. To work-around: reorder species definition.') self._do_init()
def surface_area(v0, v1, v2, v3, v4, v5, v6, v7, x0, x1, y0, y1, z0, z1): results = numpy.array([0.] * 48) find_triangles = nrn_dll_sym('find_triangles') find_triangles.argtypes = [ c_double, c_double, c_double, c_double, c_double, c_double, c_double, c_double, #value0 - value7 c_double, c_double, c_double, c_double, c_double, c_double, # x0, x1, y0, y1, z0, z1 POINTER(c_double) ] llgramarea = nrn_dll_sym('llgramarea') llgramarea.argtypes = [ POINTER(c_double), POINTER(c_double), POINTER(c_double) ] llgramarea.restype = c_double results_ptr = results.ctypes.data_as(ctypes.POINTER(ctypes.c_double)) count = find_triangles(v0, v1, v2, v3, v4, v5, v6, v7, x0, x1, y0, y1, z0, z1, results_ptr) pt0 = numpy.array([0.] * 3) pt1 = numpy.array([0.] * 3) pt2 = numpy.array([0.] * 3) pt0_ptr = pt0.ctypes.data_as(ctypes.POINTER(ctypes.c_double)) pt1_ptr = pt1.ctypes.data_as(ctypes.POINTER(ctypes.c_double)) pt2_ptr = pt2.ctypes.data_as(ctypes.POINTER(ctypes.c_double)) results = results.reshape(16, 3) tri_i = 0 area = 0 for tri_i in range(count): pt0[:] = results[0 + 3 * tri_i] pt1[:] = results[1 + 3 * tri_i] pt2[:] = results[2 + 3 * tri_i] area += llgramarea(pt0_ptr, pt1_ptr, pt2_ptr) / 2. return area
def sub_surface_area(v0, v1, v2, v3, v4, v5, v6, v7, x0, x1, y0, y1, z0, z1): def allornone(lst): return all(lst) or not any(lst) # if all corners are inside -- return 0 if allornone([x <= options.ics_distance_threshold for x in [v0, v1, v2, v3, v4, v5, v6, v7]]): return 0 results = numpy.array([0.] * 48) find_triangles = nrn_dll_sym('find_triangles') find_triangles.argtypes = [c_double,c_double,c_double,c_double,c_double,c_double,c_double,c_double, #value0 - value7 c_double, c_double, c_double, c_double, c_double, c_double, # x0, x1, y0, y1, z0, z1 POINTER(c_double) ] llgramarea = nrn_dll_sym('llgramarea') llgramarea.argtypes = [POINTER(c_double), POINTER(c_double), POINTER(c_double)] llgramarea.restype = c_double results_ptr = results.ctypes.data_as(ctypes.POINTER(ctypes.c_double)) count = find_triangles(v0, v1, v2, v3, v4, v5, v6, v7, x0, x1, y0, y1, z0, z1, results_ptr ) pt0 = numpy.array([0.] * 3) pt1 = numpy.array([0.] * 3) pt2 = numpy.array([0.] * 3) pt0_ptr = pt0.ctypes.data_as(ctypes.POINTER(ctypes.c_double)) pt1_ptr = pt1.ctypes.data_as(ctypes.POINTER(ctypes.c_double)) pt2_ptr = pt2.ctypes.data_as(ctypes.POINTER(ctypes.c_double)) results = results.reshape(16, 3) tri_i = 0 area = 0 for tri_i in range(count): pt0[:] = results[0 + 3 * tri_i] pt1[:] = results[1 + 3 * tri_i] pt2[:] = results[2 + 3 * tri_i] area += llgramarea(pt0_ptr, pt1_ptr, pt2_ptr) / 2. return area
_linmodadd = None _linmodadd_c = None _diffusion_matrix = None _curr_scales = None _curr_ptrs = None _curr_indices = None _all_reactions = [] _zero_volume_indices = [] _nonzero_volume_indices = [] _double_ptr = ctypes.POINTER(ctypes.c_double) _int_ptr = ctypes.POINTER(_ctypes_c_int) nrn_tree_solve = neuron.nrn_dll_sym('nrn_tree_solve') nrn_tree_solve.restype = None _dptr = _double_ptr _dimensions = collections.defaultdict(lambda: 1) _default_dx = 0.25 _default_method = 'deterministic' def set_solve_type(domain=None, dimension=None, dx=None, nsubseg=None, method=None): """Specify the numerical discretization and solver options.
""" rxdfast module interconnection between Python and C for NEURON rxd v2 """ from neuron import nrn_dll_sym, nrn_dll, h import ctypes import rxdmath import itertools import weakref import os import tempfile import uuid set_nonvint_block = nrn_dll_sym('set_nonvint_block') nrn = nrn_dll() dll = ctypes.cdll['./rxd.so'] fptr_prototype = ctypes.CFUNCTYPE(None) set_nonvint_block(dll.rxd_nonvint_block) set_setup = dll.set_setup set_setup.argtypes = [fptr_prototype] set_initialize = dll.set_initialize set_initialize.argtypes = [fptr_prototype] setup_solver = dll.setup_solver setup_solver.argtypes = [ ctypes.py_object, ctypes.py_object, ctypes.c_int,
from .rxdmath import _Arithmeticed import weakref from .section1d import Section1D from neuron import h, nrn import neuron from . import node, nodelist, rxdmath, region import numpy import warnings import itertools from .rxdException import RxDException import ctypes from . import initializer import collections dll = neuron.nrn_dll() set_nonvint_block = neuron.nrn_dll_sym('set_nonvint_block') fptr_prototype = ctypes.CFUNCTYPE(None) set_setup = dll.set_setup set_setup.argtypes = [fptr_prototype] set_initialize = dll.set_initialize set_initialize.argtypes = [fptr_prototype] #setup_solver = nrn.setup_solver #setup_solver.argtypes = [ctypes.py_object, ctypes.py_object, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_double, ctypes.c_double, ctypes.c_double, ctypes.c_double, ctypes.c_double, ctypes.c_double] insert = dll.insert insert.argtypes = [ctypes.c_int, ctypes.py_object, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_double, ctypes.c_double, ctypes.c_double, ctypes.c_double, ctypes.c_double, ctypes.c_double, ctypes.py_object, ctypes.py_object] insert.restype = ctypes.c_int
_linmodadd_cur_c = None _linmodadd_cur_g = None _linmodadd_cur_b = None _linmodadd_cur_y = None _cur_sec_list = None _cur_x_list = None _all_reactions = [] _zero_volume_indices = [] _nonzero_volume_indices = [] _double_ptr = ctypes.POINTER(ctypes.c_double) _int_ptr = ctypes.POINTER(ctypes.c_int) nrn_tree_solve = neuron.nrn_dll_sym('nrn_tree_solve') nrn_tree_solve.restype = None _dptr = _double_ptr def _unregister_reaction(r): global _all_reactions for i, r2 in enumerate(_all_reactions): if r2() == r: del _all_reactions[i] break def _register_reaction(r): # TODO: should we search to make sure that (a weakref to) r hasn't already been added?
from neuron import h, nrn, nrn_dll_sym from . import node, nodelist, rxdmath, region import numpy import warnings import itertools from .rxdException import RxDException from . import initializer import collections import ctypes #Now set in rxd.py #set_nonvint_block = neuron.nrn_dll_sym('set_nonvint_block') fptr_prototype = ctypes.CFUNCTYPE(None) set_setup = nrn_dll_sym('set_setup') set_setup.argtypes = [fptr_prototype] set_initialize = nrn_dll_sym('set_initialize') set_initialize.argtypes = [fptr_prototype] #setup_solver = nrn.setup_solver #setup_solver.argtypes = [ctypes.py_object, ctypes.py_object, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_double, ctypes.c_double, ctypes.c_double, ctypes.c_double, ctypes.c_double, ctypes.c_double] insert = nrn_dll_sym('insert') insert.argtypes = [ctypes.c_int, ctypes.py_object, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_double, ctypes.c_double, ctypes.c_double, ctypes.c_double, ctypes.c_double, ctypes.c_double, ctypes.py_object, ctypes.py_object, ctypes.c_int, ctypes.c_double, ctypes.c_double] insert.restype = ctypes.c_int species_atolscale = nrn_dll_sym('species_atolscale') species_atolscale.argtypes = [ctypes.c_int, ctypes.c_double, ctypes.c_int, ctypes.POINTER(ctypes.c_int)]
import uuid import threading import ctypes import json import weakref import time import neuron from neuron import h _update_thread = None _gui_widgets = [] _shape_plot_list = [] _active_container = None _structure_change_count = neuron.nrn_dll_sym('structure_change_cnt', ctypes.c_int) _diam_change_count = neuron.nrn_dll_sym('diam_change_cnt', ctypes.c_int) _last_diam_change_count = _diam_change_count.value _last_structure_change_count = _structure_change_count.value update_interval = 0.1 # seconds _has_setup = False def _ensure_update_thread(): global _update_thread if _update_thread is None: _update_thread = threading.Thread(target=_do_updates) _update_thread.daemon = True _update_thread.start() def _do_updates(): global _last_diam_change_count, _last_structure_change_count while True: old_diam_changed = h.diam_changed
import uuid import threading import ctypes import json import weakref import time import neuron from neuron import h _update_thread = None _gui_widgets = [] _shape_plot_list = [] _active_container = None _structure_change_count = neuron.nrn_dll_sym('structure_change_cnt', ctypes.c_int) _diam_change_count = neuron.nrn_dll_sym('diam_change_cnt', ctypes.c_int) _last_diam_change_count = _diam_change_count.value _last_structure_change_count = _structure_change_count.value update_interval = 0.1 # seconds _has_setup = False def _ensure_update_thread(): global _update_thread if _update_thread is None: _update_thread = threading.Thread(target=_do_updates) _update_thread.daemon = True _update_thread.start() def _do_updates():
import neuron from neuron import h, nrn, hoc, nrn_dll_sym from . import region from . import rxdsection import numpy import weakref from .rxdException import RxDException import warnings import ctypes import collections #function to change extracellular diffusion set_diffusion = nrn_dll_sym('set_diffusion') set_diffusion.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.c_double, ctypes.c_double, ctypes.c_double] set_diffusion.restype = ctypes.c_int # data storage _volumes = numpy.array([]) _surface_area = numpy.array([]) _diffs = numpy.array([]) _states = numpy.array([]) _node_fluxes = {'indices': [], 'type': [], 'source': [], 'scale': []} _has_node_fluxes = False # node data types _concentration_node = 0 _molecule_node = 1 def _apply_node_fluxes(dy): # TODO: what if the nodes go away because a section is destroyed? if _has_node_fluxes:
_linmodadd_cur_g = None _linmodadd_cur_b = None _linmodadd_cur_y = None _cur_sec_list = None _cur_x_list = None _all_reactions = [] _zero_volume_indices = [] _nonzero_volume_indices = [] _double_ptr = ctypes.POINTER(ctypes.c_double) _int_ptr = ctypes.POINTER(ctypes.c_int) nrn_tree_solve = neuron.nrn_dll_sym('nrn_tree_solve') nrn_tree_solve.restype = None _dptr = _double_ptr def _unregister_reaction(r): global _all_reactions for i, r2 in enumerate(_all_reactions): if r2() == r: del _all_reactions[i] break def _register_reaction(r): # TODO: should we search to make sure that (a weakref to) r hasn't already been added? global _all_reactions _all_reactions.append(weakref.ref(r))
import neuron from neuron import h, nrn, hoc, nrn_dll_sym from . import region, constants from . import rxdsection import numpy import weakref from .rxdException import RxDException import warnings import ctypes from collections.abc import Callable # function to change extracellular diffusion set_diffusion = nrn_dll_sym("set_diffusion") set_diffusion.argtypes = [ ctypes.c_int, ctypes.c_int, numpy.ctypeslib.ndpointer(ctypes.c_double), ctypes.c_int, ] set_diffusion.restype = ctypes.c_int # data storage _volumes = numpy.array([]) _surface_area = numpy.array([]) _diffs = numpy.array([]) _states = numpy.array([]) _node_fluxes = { "index": [], "type": [], "source": [],
_linmodadd_c = None _diffusion_matrix = None _curr_scales = None _curr_ptrs = None _curr_indices = None _all_reactions = [] _zero_volume_indices = [] _nonzero_volume_indices = [] _double_ptr = ctypes.POINTER(ctypes.c_double) _int_ptr = ctypes.POINTER(_ctypes_c_int) nrn_tree_solve = neuron.nrn_dll_sym('nrn_tree_solve') nrn_tree_solve.restype = None _dptr = _double_ptr _dimensions = collections.defaultdict(lambda: 1) _default_dx = 0.25 _default_method = 'deterministic' def set_solve_type(domain=None, dimension=None, dx=None, nsubseg=None, method=None): """Specify the numerical discretization and solver options. domain -- a section or Python iterable of sections""" setting_default = False if domain is None: domain = h.allsec()
import neuron from neuron import h, nrn, hoc, nrn_dll_sym from . import region, constants from . import rxdsection import numpy import weakref from .rxdException import RxDException import warnings import ctypes import collections #function to change extracellular diffusion set_diffusion = nrn_dll_sym('set_diffusion') set_diffusion.argtypes = [ctypes.c_int, ctypes.c_int, numpy.ctypeslib.ndpointer(ctypes.c_double), ctypes.c_int] set_diffusion.restype = ctypes.c_int # data storage _volumes = numpy.array([]) _surface_area = numpy.array([]) _diffs = numpy.array([]) _states = numpy.array([]) _node_fluxes = {'index': [], 'type': [], 'source': [], 'scale': [], 'region': []} _has_node_fluxes = False _point_indices = {} # node data types _concentration_node = 0 _molecule_node = 1
import uuid import threading import ctypes import json import weakref import time import neuron from neuron import h _update_thread = None _gui_widgets = [] _shape_plot_list = [] _active_container = None _structure_change_count = neuron.nrn_dll_sym("structure_change_cnt", ctypes.c_int) _diam_change_count = neuron.nrn_dll_sym("diam_change_cnt", ctypes.c_int) _last_diam_change_count = _diam_change_count.value _last_structure_change_count = _structure_change_count.value update_interval = 0.1 # seconds _has_setup = False def _ensure_update_thread(): global _update_thread if _update_thread is None: _update_thread = threading.Thread(target=_do_updates) _update_thread.daemon = True _update_thread.start() def _do_updates():