'''
Neuron simulator export for:

Components:
    null (Type: include)
    na_s (Type: ionChannelHH:  conductance=1.0E-11 (SI conductance))
    null (Type: annotation)
    kdr (Type: ionChannelHH:  conductance=1.0E-11 (SI conductance))
    null (Type: annotation)
    k (Type: ionChannelHH:  conductance=1.0E-11 (SI conductance))
    null (Type: annotation)
    cal (Type: ionChannelHH:  conductance=1.0E-11 (SI conductance))
    leak (Type: ionChannelPassive:  conductance=1.0E-11 (SI conductance))
    null (Type: annotation)
    cah (Type: ionChannelHH:  conductance=1.0E-11 (SI conductance))
    kca (Type: ionChannelHH:  conductance=1.0E-11 (SI conductance))
    null (Type: annotation)
    h (Type: ionChannelHH:  conductance=1.0E-11 (SI conductance))
    ca_conc (Type: fixedFactorConcentrationModel:  restingConc=0.0 (SI concentration) decayConstant=0.013333333333333 (SI time) rho=300000.0 (SI rho_factor))
    null (Type: include)
    na_a (Type: ionChannelHH:  conductance=1.0E-11 (SI conductance))
    null (Type: include)
    null (Type: include)
    null (Type: include)
    null (Type: include)
    null (Type: include)
    null (Type: include)
    null (Type: include)
    null (Type: include)
    null (Type: include)
    null (Type: include)
    cell (Type: cell)
    null (Type: include)
    iclamp0 (Type: pulseGenerator:  delay=0.5 (SI time) duration=0.2 (SI time) amplitude=-3.0E-10 (SI current))
    net (Type: networkWithTemperature:  temperature=300.15 (SI temperature))
    sim1 (Type: Simulation:  length=2.0 (SI time) step=2.5E-5 (SI time))


    This NEURON file has been generated by org.neuroml.export (see https://github.com/NeuroML/org.neuroml.export)
         org.neuroml.export  v1.4.6
         org.neuroml.model   v1.4.6
         jLEMS               v0.9.8.6

'''

import neuron

import time
h = neuron.h
h.load_file("nrngui.hoc")

h("objref p")
h("p = new PythonObject()")

class NeuronSimulation():

    def __init__(self, tstop, dt):

        print("\n    Starting simulation in NEURON generated from NeuroML2 model...\n")

        # Adding simulation Component(id=sim1 type=Simulation) of network/component: net (Type: networkWithTemperature:  temperature=300.15 (SI temperature))

        # Temperature used for network: 300.15 K
        h.celsius = 300.15 - 273.15

        print("Population pop contains 1 instance(s) of component: cell of type: cell")

        print("Setting the default initial concentrations for ca (used in cell) to 3.7152 mM (internal), 3.0 mM (external)")
        h("cai0_ca_ion = 3.7152")
        h("cao0_ca_ion = 3.0")

        h.load_file("cell.hoc")
        a_pop = []
        h("{ n_pop = 1 }")
        h("objectvar a_pop[n_pop]")
        for i in range(int(h.n_pop)):
            h("a_pop[%i] = new cell()"%i)
            h("access a_pop[%i].soma"%i)

        h("{ a_pop[0].position(0, 0, 0) }")

        h("proc initialiseV_pop() { for i = 0, n_pop-1 { a_pop[i].set_initial_v() } }")
        h("objref fih_pop")
        h('{fih_pop = new FInitializeHandler(0, "initialiseV_pop()")}')

        h("proc initialiseIons_pop() { for i = 0, n_pop-1 { a_pop[i].set_initial_ion_properties() } }")
        h("objref fih_ion_pop")
        h('{fih_ion_pop = new FInitializeHandler(1, "initialiseIons_pop()")}')

        # Adding input: Component(id=0 type=input)

        h("objref Clamps_0")
        h("a_pop[0].soma { Clamps_0 = new iclamp0(0.500000) } ")

        trec = h.Vector()
        trec.record(h._ref_t)

        h.tstop = tstop

        h.dt = dt

        h.steps_per_ms = 1/h.dt

        # Display: self.display_d1
        self.display_d1 = h.Graph(0)
        self.display_d1.size(0,h.tstop,-80.0,50.0)
        self.display_d1.view(0, -80.0, h.tstop, 130.0, 80, 330, 330, 250)
        h.graphList[0].append(self.display_d1)
        # Line, plotting: pop/0/cell/0/v
        self.display_d1.addexpr("a_pop[0].soma.v(0.5)", "a_pop[0].soma.v(0.5)", 1, 1, 0.8, 0.9, 2)
        # Line, plotting: pop/0/cell/1/v
        self.display_d1.addexpr("a_pop[0].dend.v(0.5)", "a_pop[0].dend.v(0.5)", 2, 1, 0.8, 0.9, 2)
        # Line, plotting: pop/0/cell/2/v
        self.display_d1.addexpr("a_pop[0].axon.v(0.5)", "a_pop[0].axon.v(0.5)", 3, 1, 0.8, 0.9, 2)



        # File to save: time
        # Column: time
        h(' objectvar v_time ')
        h(' { v_time = new Vector() } ')
        h(' { v_time.record(&t) } ')
        h.v_time.resize((h.tstop * h.steps_per_ms) + 1)

        # File to save: outf
        # Column: pop/0/cell/0/v
        h(' objectvar v_v_outf ')
        h(' { v_v_outf = new Vector() } ')
        h(' { v_v_outf.record(&a_pop[0].soma.v(0.5)) } ')
        h.v_v_outf.resize((h.tstop * h.steps_per_ms) + 1)
        # Column: pop/0/cell/1/v
        h(' objectvar v_v_outf_ ')
        h(' { v_v_outf_ = new Vector() } ')
        h(' { v_v_outf_.record(&a_pop[0].dend.v(0.5)) } ')
        h.v_v_outf_.resize((h.tstop * h.steps_per_ms) + 1)
        # Column: pop/0/cell/2/v
        h(' objectvar v_v_outf__ ')
        h(' { v_v_outf__ = new Vector() } ')
        h(' { v_v_outf__.record(&a_pop[0].axon.v(0.5)) } ')
        h.v_v_outf__.resize((h.tstop * h.steps_per_ms) + 1)
        # Column: pop/0/cell/1/caConc
        h(' objectvar v_v_outf___ ')
        h(' { v_v_outf___ = new Vector() } ')
        h(' { v_v_outf___.record(&a_pop[0].dend.cai(0.5)) } ')
        h.v_v_outf___.resize((h.tstop * h.steps_per_ms) + 1)

        self.initialized = False

        self.sim_end = -1 # will be overwritten

        h.nrncontrolmenu()


    def run(self):

        self.initialized = True
        sim_start = time.time()
        print("Running a simulation of %sms (dt = %sms)" % (h.tstop, h.dt))

        h.run()

        self.sim_end = time.time()
        sim_time = self.sim_end - sim_start
        print("Finished NEURON simulation in %f seconds (%f mins)..."%(sim_time, sim_time/60.0))

        self.save_results()


    def advance(self):

        if not self.initialized:
            h.finitialize()
            self.initialized = True

        h.fadvance()


    def save_results(self):

        print("Saving results at t=%s..."%h.t)

        if self.sim_end < 0: self.sim_end = time.time()

        self.display_d1.exec_menu("View = plot")

        # File to save: time
        py_v_time = [ t/1000 for t in h.v_time.to_python() ]  # Convert to Python list for speed...

        f_time_f2 = open('time.dat', 'w')
        for i in range(int(h.tstop * h.steps_per_ms) + 1):
            f_time_f2.write('%f'% py_v_time[i])  # Save in SI units...+ '\n')
        f_time_f2.close()
        print("Saved data to: time.dat")

        # File to save: outf
        py_v_v_outf = [ float(x  / 1000.0) for x in h.v_v_outf.to_python() ]  # Convert to Python list for speed, variable has dim: voltage
        py_v_v_outf_ = [ float(x  / 1000.0) for x in h.v_v_outf_.to_python() ]  # Convert to Python list for speed, variable has dim: voltage
        py_v_v_outf__ = [ float(x  / 1000.0) for x in h.v_v_outf__.to_python() ]  # Convert to Python list for speed, variable has dim: voltage
        py_v_v_outf___ = [ float(x ) for x in h.v_v_outf___.to_python() ]  # Convert to Python list for speed, variable has dim: concentration

        f_outf_f2 = open('single_olive_iclamp.dat', 'w')
        for i in range(int(h.tstop * h.steps_per_ms) + 1):
            f_outf_f2.write('%e\t'% py_v_time[i]  + '%e\t'%(py_v_v_outf[i])  + '%e\t'%(py_v_v_outf_[i])  + '%e\t'%(py_v_v_outf__[i])  + '%e\t'%(py_v_v_outf___[i]) + '\n')
        f_outf_f2.close()
        print("Saved data to: single_olive_iclamp.dat")

        save_end = time.time()
        save_time = save_end - self.sim_end
        print("Finished saving results in %f seconds"%(save_time))

        print("Done")

if __name__ == '__main__':

    ns = NeuronSimulation(tstop=2000 , dt=0.025)

    ns.run()


8aafe7643a706d204881457253883cbffa5e9ef9
<mask token>


class NeuronSimulation:

    def __init__(self, tstop, dt):
        print(
            '\n    Starting simulation in NEURON generated from NeuroML2 model...\n'
            )
        h.celsius = 300.15 - 273.15
        print(
            'Population pop contains 1 instance(s) of component: cell of type: cell'
            )
        print(
            'Setting the default initial concentrations for ca (used in cell) to 3.7152 mM (internal), 3.0 mM (external)'
            )
        h('cai0_ca_ion = 3.7152')
        h('cao0_ca_ion = 3.0')
        h.load_file('cell.hoc')
        a_pop = []
        h('{ n_pop = 1 }')
        h('objectvar a_pop[n_pop]')
        for i in range(int(h.n_pop)):
            h('a_pop[%i] = new cell()' % i)
            h('access a_pop[%i].soma' % i)
        h('{ a_pop[0].position(0, 0, 0) }')
        h('proc initialiseV_pop() { for i = 0, n_pop-1 { a_pop[i].set_initial_v() } }'
            )
        h('objref fih_pop')
        h('{fih_pop = new FInitializeHandler(0, "initialiseV_pop()")}')
        h('proc initialiseIons_pop() { for i = 0, n_pop-1 { a_pop[i].set_initial_ion_properties() } }'
            )
        h('objref fih_ion_pop')
        h('{fih_ion_pop = new FInitializeHandler(1, "initialiseIons_pop()")}')
        h('objref Clamps_0')
        h('a_pop[0].soma { Clamps_0 = new iclamp0(0.500000) } ')
        trec = h.Vector()
        trec.record(h._ref_t)
        h.tstop = tstop
        h.dt = dt
        h.steps_per_ms = 1 / h.dt
        self.display_d1 = h.Graph(0)
        self.display_d1.size(0, h.tstop, -80.0, 50.0)
        self.display_d1.view(0, -80.0, h.tstop, 130.0, 80, 330, 330, 250)
        h.graphList[0].append(self.display_d1)
        self.display_d1.addexpr('a_pop[0].soma.v(0.5)',
            'a_pop[0].soma.v(0.5)', 1, 1, 0.8, 0.9, 2)
        self.display_d1.addexpr('a_pop[0].dend.v(0.5)',
            'a_pop[0].dend.v(0.5)', 2, 1, 0.8, 0.9, 2)
        self.display_d1.addexpr('a_pop[0].axon.v(0.5)',
            'a_pop[0].axon.v(0.5)', 3, 1, 0.8, 0.9, 2)
        h(' objectvar v_time ')
        h(' { v_time = new Vector() } ')
        h(' { v_time.record(&t) } ')
        h.v_time.resize(h.tstop * h.steps_per_ms + 1)
        h(' objectvar v_v_outf ')
        h(' { v_v_outf = new Vector() } ')
        h(' { v_v_outf.record(&a_pop[0].soma.v(0.5)) } ')
        h.v_v_outf.resize(h.tstop * h.steps_per_ms + 1)
        h(' objectvar v_v_outf_ ')
        h(' { v_v_outf_ = new Vector() } ')
        h(' { v_v_outf_.record(&a_pop[0].dend.v(0.5)) } ')
        h.v_v_outf_.resize(h.tstop * h.steps_per_ms + 1)
        h(' objectvar v_v_outf__ ')
        h(' { v_v_outf__ = new Vector() } ')
        h(' { v_v_outf__.record(&a_pop[0].axon.v(0.5)) } ')
        h.v_v_outf__.resize(h.tstop * h.steps_per_ms + 1)
        h(' objectvar v_v_outf___ ')
        h(' { v_v_outf___ = new Vector() } ')
        h(' { v_v_outf___.record(&a_pop[0].dend.cai(0.5)) } ')
        h.v_v_outf___.resize(h.tstop * h.steps_per_ms + 1)
        self.initialized = False
        self.sim_end = -1
        h.nrncontrolmenu()

    def run(self):
        self.initialized = True
        sim_start = time.time()
        print('Running a simulation of %sms (dt = %sms)' % (h.tstop, h.dt))
        h.run()
        self.sim_end = time.time()
        sim_time = self.sim_end - sim_start
        print('Finished NEURON simulation in %f seconds (%f mins)...' % (
            sim_time, sim_time / 60.0))
        self.save_results()

    def advance(self):
        if not self.initialized:
            h.finitialize()
            self.initialized = True
        h.fadvance()

    def save_results(self):
        print('Saving results at t=%s...' % h.t)
        if self.sim_end < 0:
            self.sim_end = time.time()
        self.display_d1.exec_menu('View = plot')
        py_v_time = [(t / 1000) for t in h.v_time.to_python()]
        f_time_f2 = open('time.dat', 'w')
        for i in range(int(h.tstop * h.steps_per_ms) + 1):
            f_time_f2.write('%f' % py_v_time[i])
        f_time_f2.close()
        print('Saved data to: time.dat')
        py_v_v_outf = [float(x / 1000.0) for x in h.v_v_outf.to_python()]
        py_v_v_outf_ = [float(x / 1000.0) for x in h.v_v_outf_.to_python()]
        py_v_v_outf__ = [float(x / 1000.0) for x in h.v_v_outf__.to_python()]
        py_v_v_outf___ = [float(x) for x in h.v_v_outf___.to_python()]
        f_outf_f2 = open('single_olive_iclamp.dat', 'w')
        for i in range(int(h.tstop * h.steps_per_ms) + 1):
            f_outf_f2.write('%e\t' % py_v_time[i] + '%e\t' % py_v_v_outf[i] +
                '%e\t' % py_v_v_outf_[i] + '%e\t' % py_v_v_outf__[i] + 
                '%e\t' % py_v_v_outf___[i] + '\n')
        f_outf_f2.close()
        print('Saved data to: single_olive_iclamp.dat')
        save_end = time.time()
        save_time = save_end - self.sim_end
        print('Finished saving results in %f seconds' % save_time)
        print('Done')


<mask token>

<mask token>
h.load_file('nrngui.hoc')
h('objref p')
h('p = new PythonObject()')


class NeuronSimulation:

    def __init__(self, tstop, dt):
        print(
            '\n    Starting simulation in NEURON generated from NeuroML2 model...\n'
            )
        h.celsius = 300.15 - 273.15
        print(
            'Population pop contains 1 instance(s) of component: cell of type: cell'
            )
        print(
            'Setting the default initial concentrations for ca (used in cell) to 3.7152 mM (internal), 3.0 mM (external)'
            )
        h('cai0_ca_ion = 3.7152')
        h('cao0_ca_ion = 3.0')
        h.load_file('cell.hoc')
        a_pop = []
        h('{ n_pop = 1 }')
        h('objectvar a_pop[n_pop]')
        for i in range(int(h.n_pop)):
            h('a_pop[%i] = new cell()' % i)
            h('access a_pop[%i].soma' % i)
        h('{ a_pop[0].position(0, 0, 0) }')
        h('proc initialiseV_pop() { for i = 0, n_pop-1 { a_pop[i].set_initial_v() } }'
            )
        h('objref fih_pop')
        h('{fih_pop = new FInitializeHandler(0, "initialiseV_pop()")}')
        h('proc initialiseIons_pop() { for i = 0, n_pop-1 { a_pop[i].set_initial_ion_properties() } }'
            )
        h('objref fih_ion_pop')
        h('{fih_ion_pop = new FInitializeHandler(1, "initialiseIons_pop()")}')
        h('objref Clamps_0')
        h('a_pop[0].soma { Clamps_0 = new iclamp0(0.500000) } ')
        trec = h.Vector()
        trec.record(h._ref_t)
        h.tstop = tstop
        h.dt = dt
        h.steps_per_ms = 1 / h.dt
        self.display_d1 = h.Graph(0)
        self.display_d1.size(0, h.tstop, -80.0, 50.0)
        self.display_d1.view(0, -80.0, h.tstop, 130.0, 80, 330, 330, 250)
        h.graphList[0].append(self.display_d1)
        self.display_d1.addexpr('a_pop[0].soma.v(0.5)',
            'a_pop[0].soma.v(0.5)', 1, 1, 0.8, 0.9, 2)
        self.display_d1.addexpr('a_pop[0].dend.v(0.5)',
            'a_pop[0].dend.v(0.5)', 2, 1, 0.8, 0.9, 2)
        self.display_d1.addexpr('a_pop[0].axon.v(0.5)',
            'a_pop[0].axon.v(0.5)', 3, 1, 0.8, 0.9, 2)
        h(' objectvar v_time ')
        h(' { v_time = new Vector() } ')
        h(' { v_time.record(&t) } ')
        h.v_time.resize(h.tstop * h.steps_per_ms + 1)
        h(' objectvar v_v_outf ')
        h(' { v_v_outf = new Vector() } ')
        h(' { v_v_outf.record(&a_pop[0].soma.v(0.5)) } ')
        h.v_v_outf.resize(h.tstop * h.steps_per_ms + 1)
        h(' objectvar v_v_outf_ ')
        h(' { v_v_outf_ = new Vector() } ')
        h(' { v_v_outf_.record(&a_pop[0].dend.v(0.5)) } ')
        h.v_v_outf_.resize(h.tstop * h.steps_per_ms + 1)
        h(' objectvar v_v_outf__ ')
        h(' { v_v_outf__ = new Vector() } ')
        h(' { v_v_outf__.record(&a_pop[0].axon.v(0.5)) } ')
        h.v_v_outf__.resize(h.tstop * h.steps_per_ms + 1)
        h(' objectvar v_v_outf___ ')
        h(' { v_v_outf___ = new Vector() } ')
        h(' { v_v_outf___.record(&a_pop[0].dend.cai(0.5)) } ')
        h.v_v_outf___.resize(h.tstop * h.steps_per_ms + 1)
        self.initialized = False
        self.sim_end = -1
        h.nrncontrolmenu()

    def run(self):
        self.initialized = True
        sim_start = time.time()
        print('Running a simulation of %sms (dt = %sms)' % (h.tstop, h.dt))
        h.run()
        self.sim_end = time.time()
        sim_time = self.sim_end - sim_start
        print('Finished NEURON simulation in %f seconds (%f mins)...' % (
            sim_time, sim_time / 60.0))
        self.save_results()

    def advance(self):
        if not self.initialized:
            h.finitialize()
            self.initialized = True
        h.fadvance()

    def save_results(self):
        print('Saving results at t=%s...' % h.t)
        if self.sim_end < 0:
            self.sim_end = time.time()
        self.display_d1.exec_menu('View = plot')
        py_v_time = [(t / 1000) for t in h.v_time.to_python()]
        f_time_f2 = open('time.dat', 'w')
        for i in range(int(h.tstop * h.steps_per_ms) + 1):
            f_time_f2.write('%f' % py_v_time[i])
        f_time_f2.close()
        print('Saved data to: time.dat')
        py_v_v_outf = [float(x / 1000.0) for x in h.v_v_outf.to_python()]
        py_v_v_outf_ = [float(x / 1000.0) for x in h.v_v_outf_.to_python()]
        py_v_v_outf__ = [float(x / 1000.0) for x in h.v_v_outf__.to_python()]
        py_v_v_outf___ = [float(x) for x in h.v_v_outf___.to_python()]
        f_outf_f2 = open('single_olive_iclamp.dat', 'w')
        for i in range(int(h.tstop * h.steps_per_ms) + 1):
            f_outf_f2.write('%e\t' % py_v_time[i] + '%e\t' % py_v_v_outf[i] +
                '%e\t' % py_v_v_outf_[i] + '%e\t' % py_v_v_outf__[i] + 
                '%e\t' % py_v_v_outf___[i] + '\n')
        f_outf_f2.close()
        print('Saved data to: single_olive_iclamp.dat')
        save_end = time.time()
        save_time = save_end - self.sim_end
        print('Finished saving results in %f seconds' % save_time)
        print('Done')


if __name__ == '__main__':
    ns = NeuronSimulation(tstop=2000, dt=0.025)
    ns.run()

<mask token>
h = neuron.h
h.load_file('nrngui.hoc')
h('objref p')
h('p = new PythonObject()')


class NeuronSimulation:

    def __init__(self, tstop, dt):
        print(
            '\n    Starting simulation in NEURON generated from NeuroML2 model...\n'
            )
        h.celsius = 300.15 - 273.15
        print(
            'Population pop contains 1 instance(s) of component: cell of type: cell'
            )
        print(
            'Setting the default initial concentrations for ca (used in cell) to 3.7152 mM (internal), 3.0 mM (external)'
            )
        h('cai0_ca_ion = 3.7152')
        h('cao0_ca_ion = 3.0')
        h.load_file('cell.hoc')
        a_pop = []
        h('{ n_pop = 1 }')
        h('objectvar a_pop[n_pop]')
        for i in range(int(h.n_pop)):
            h('a_pop[%i] = new cell()' % i)
            h('access a_pop[%i].soma' % i)
        h('{ a_pop[0].position(0, 0, 0) }')
        h('proc initialiseV_pop() { for i = 0, n_pop-1 { a_pop[i].set_initial_v() } }'
            )
        h('objref fih_pop')
        h('{fih_pop = new FInitializeHandler(0, "initialiseV_pop()")}')
        h('proc initialiseIons_pop() { for i = 0, n_pop-1 { a_pop[i].set_initial_ion_properties() } }'
            )
        h('objref fih_ion_pop')
        h('{fih_ion_pop = new FInitializeHandler(1, "initialiseIons_pop()")}')
        h('objref Clamps_0')
        h('a_pop[0].soma { Clamps_0 = new iclamp0(0.500000) } ')
        trec = h.Vector()
        trec.record(h._ref_t)
        h.tstop = tstop
        h.dt = dt
        h.steps_per_ms = 1 / h.dt
        self.display_d1 = h.Graph(0)
        self.display_d1.size(0, h.tstop, -80.0, 50.0)
        self.display_d1.view(0, -80.0, h.tstop, 130.0, 80, 330, 330, 250)
        h.graphList[0].append(self.display_d1)
        self.display_d1.addexpr('a_pop[0].soma.v(0.5)',
            'a_pop[0].soma.v(0.5)', 1, 1, 0.8, 0.9, 2)
        self.display_d1.addexpr('a_pop[0].dend.v(0.5)',
            'a_pop[0].dend.v(0.5)', 2, 1, 0.8, 0.9, 2)
        self.display_d1.addexpr('a_pop[0].axon.v(0.5)',
            'a_pop[0].axon.v(0.5)', 3, 1, 0.8, 0.9, 2)
        h(' objectvar v_time ')
        h(' { v_time = new Vector() } ')
        h(' { v_time.record(&t) } ')
        h.v_time.resize(h.tstop * h.steps_per_ms + 1)
        h(' objectvar v_v_outf ')
        h(' { v_v_outf = new Vector() } ')
        h(' { v_v_outf.record(&a_pop[0].soma.v(0.5)) } ')
        h.v_v_outf.resize(h.tstop * h.steps_per_ms + 1)
        h(' objectvar v_v_outf_ ')
        h(' { v_v_outf_ = new Vector() } ')
        h(' { v_v_outf_.record(&a_pop[0].dend.v(0.5)) } ')
        h.v_v_outf_.resize(h.tstop * h.steps_per_ms + 1)
        h(' objectvar v_v_outf__ ')
        h(' { v_v_outf__ = new Vector() } ')
        h(' { v_v_outf__.record(&a_pop[0].axon.v(0.5)) } ')
        h.v_v_outf__.resize(h.tstop * h.steps_per_ms + 1)
        h(' objectvar v_v_outf___ ')
        h(' { v_v_outf___ = new Vector() } ')
        h(' { v_v_outf___.record(&a_pop[0].dend.cai(0.5)) } ')
        h.v_v_outf___.resize(h.tstop * h.steps_per_ms + 1)
        self.initialized = False
        self.sim_end = -1
        h.nrncontrolmenu()

    def run(self):
        self.initialized = True
        sim_start = time.time()
        print('Running a simulation of %sms (dt = %sms)' % (h.tstop, h.dt))
        h.run()
        self.sim_end = time.time()
        sim_time = self.sim_end - sim_start
        print('Finished NEURON simulation in %f seconds (%f mins)...' % (
            sim_time, sim_time / 60.0))
        self.save_results()

    def advance(self):
        if not self.initialized:
            h.finitialize()
            self.initialized = True
        h.fadvance()

    def save_results(self):
        print('Saving results at t=%s...' % h.t)
        if self.sim_end < 0:
            self.sim_end = time.time()
        self.display_d1.exec_menu('View = plot')
        py_v_time = [(t / 1000) for t in h.v_time.to_python()]
        f_time_f2 = open('time.dat', 'w')
        for i in range(int(h.tstop * h.steps_per_ms) + 1):
            f_time_f2.write('%f' % py_v_time[i])
        f_time_f2.close()
        print('Saved data to: time.dat')
        py_v_v_outf = [float(x / 1000.0) for x in h.v_v_outf.to_python()]
        py_v_v_outf_ = [float(x / 1000.0) for x in h.v_v_outf_.to_python()]
        py_v_v_outf__ = [float(x / 1000.0) for x in h.v_v_outf__.to_python()]
        py_v_v_outf___ = [float(x) for x in h.v_v_outf___.to_python()]
        f_outf_f2 = open('single_olive_iclamp.dat', 'w')
        for i in range(int(h.tstop * h.steps_per_ms) + 1):
            f_outf_f2.write('%e\t' % py_v_time[i] + '%e\t' % py_v_v_outf[i] +
                '%e\t' % py_v_v_outf_[i] + '%e\t' % py_v_v_outf__[i] + 
                '%e\t' % py_v_v_outf___[i] + '\n')
        f_outf_f2.close()
        print('Saved data to: single_olive_iclamp.dat')
        save_end = time.time()
        save_time = save_end - self.sim_end
        print('Finished saving results in %f seconds' % save_time)
        print('Done')


if __name__ == '__main__':
    ns = NeuronSimulation(tstop=2000, dt=0.025)
    ns.run()

<mask token>
import neuron
import time
h = neuron.h
h.load_file('nrngui.hoc')
h('objref p')
h('p = new PythonObject()')


class NeuronSimulation:

    def __init__(self, tstop, dt):
        print(
            '\n    Starting simulation in NEURON generated from NeuroML2 model...\n'
            )
        h.celsius = 300.15 - 273.15
        print(
            'Population pop contains 1 instance(s) of component: cell of type: cell'
            )
        print(
            'Setting the default initial concentrations for ca (used in cell) to 3.7152 mM (internal), 3.0 mM (external)'
            )
        h('cai0_ca_ion = 3.7152')
        h('cao0_ca_ion = 3.0')
        h.load_file('cell.hoc')
        a_pop = []
        h('{ n_pop = 1 }')
        h('objectvar a_pop[n_pop]')
        for i in range(int(h.n_pop)):
            h('a_pop[%i] = new cell()' % i)
            h('access a_pop[%i].soma' % i)
        h('{ a_pop[0].position(0, 0, 0) }')
        h('proc initialiseV_pop() { for i = 0, n_pop-1 { a_pop[i].set_initial_v() } }'
            )
        h('objref fih_pop')
        h('{fih_pop = new FInitializeHandler(0, "initialiseV_pop()")}')
        h('proc initialiseIons_pop() { for i = 0, n_pop-1 { a_pop[i].set_initial_ion_properties() } }'
            )
        h('objref fih_ion_pop')
        h('{fih_ion_pop = new FInitializeHandler(1, "initialiseIons_pop()")}')
        h('objref Clamps_0')
        h('a_pop[0].soma { Clamps_0 = new iclamp0(0.500000) } ')
        trec = h.Vector()
        trec.record(h._ref_t)
        h.tstop = tstop
        h.dt = dt
        h.steps_per_ms = 1 / h.dt
        self.display_d1 = h.Graph(0)
        self.display_d1.size(0, h.tstop, -80.0, 50.0)
        self.display_d1.view(0, -80.0, h.tstop, 130.0, 80, 330, 330, 250)
        h.graphList[0].append(self.display_d1)
        self.display_d1.addexpr('a_pop[0].soma.v(0.5)',
            'a_pop[0].soma.v(0.5)', 1, 1, 0.8, 0.9, 2)
        self.display_d1.addexpr('a_pop[0].dend.v(0.5)',
            'a_pop[0].dend.v(0.5)', 2, 1, 0.8, 0.9, 2)
        self.display_d1.addexpr('a_pop[0].axon.v(0.5)',
            'a_pop[0].axon.v(0.5)', 3, 1, 0.8, 0.9, 2)
        h(' objectvar v_time ')
        h(' { v_time = new Vector() } ')
        h(' { v_time.record(&t) } ')
        h.v_time.resize(h.tstop * h.steps_per_ms + 1)
        h(' objectvar v_v_outf ')
        h(' { v_v_outf = new Vector() } ')
        h(' { v_v_outf.record(&a_pop[0].soma.v(0.5)) } ')
        h.v_v_outf.resize(h.tstop * h.steps_per_ms + 1)
        h(' objectvar v_v_outf_ ')
        h(' { v_v_outf_ = new Vector() } ')
        h(' { v_v_outf_.record(&a_pop[0].dend.v(0.5)) } ')
        h.v_v_outf_.resize(h.tstop * h.steps_per_ms + 1)
        h(' objectvar v_v_outf__ ')
        h(' { v_v_outf__ = new Vector() } ')
        h(' { v_v_outf__.record(&a_pop[0].axon.v(0.5)) } ')
        h.v_v_outf__.resize(h.tstop * h.steps_per_ms + 1)
        h(' objectvar v_v_outf___ ')
        h(' { v_v_outf___ = new Vector() } ')
        h(' { v_v_outf___.record(&a_pop[0].dend.cai(0.5)) } ')
        h.v_v_outf___.resize(h.tstop * h.steps_per_ms + 1)
        self.initialized = False
        self.sim_end = -1
        h.nrncontrolmenu()

    def run(self):
        self.initialized = True
        sim_start = time.time()
        print('Running a simulation of %sms (dt = %sms)' % (h.tstop, h.dt))
        h.run()
        self.sim_end = time.time()
        sim_time = self.sim_end - sim_start
        print('Finished NEURON simulation in %f seconds (%f mins)...' % (
            sim_time, sim_time / 60.0))
        self.save_results()

    def advance(self):
        if not self.initialized:
            h.finitialize()
            self.initialized = True
        h.fadvance()

    def save_results(self):
        print('Saving results at t=%s...' % h.t)
        if self.sim_end < 0:
            self.sim_end = time.time()
        self.display_d1.exec_menu('View = plot')
        py_v_time = [(t / 1000) for t in h.v_time.to_python()]
        f_time_f2 = open('time.dat', 'w')
        for i in range(int(h.tstop * h.steps_per_ms) + 1):
            f_time_f2.write('%f' % py_v_time[i])
        f_time_f2.close()
        print('Saved data to: time.dat')
        py_v_v_outf = [float(x / 1000.0) for x in h.v_v_outf.to_python()]
        py_v_v_outf_ = [float(x / 1000.0) for x in h.v_v_outf_.to_python()]
        py_v_v_outf__ = [float(x / 1000.0) for x in h.v_v_outf__.to_python()]
        py_v_v_outf___ = [float(x) for x in h.v_v_outf___.to_python()]
        f_outf_f2 = open('single_olive_iclamp.dat', 'w')
        for i in range(int(h.tstop * h.steps_per_ms) + 1):
            f_outf_f2.write('%e\t' % py_v_time[i] + '%e\t' % py_v_v_outf[i] +
                '%e\t' % py_v_v_outf_[i] + '%e\t' % py_v_v_outf__[i] + 
                '%e\t' % py_v_v_outf___[i] + '\n')
        f_outf_f2.close()
        print('Saved data to: single_olive_iclamp.dat')
        save_end = time.time()
        save_time = save_end - self.sim_end
        print('Finished saving results in %f seconds' % save_time)
        print('Done')


if __name__ == '__main__':
    ns = NeuronSimulation(tstop=2000, dt=0.025)
    ns.run()

'''
Neuron simulator export for:

Components:
    null (Type: include)
    na_s (Type: ionChannelHH:  conductance=1.0E-11 (SI conductance))
    null (Type: annotation)
    kdr (Type: ionChannelHH:  conductance=1.0E-11 (SI conductance))
    null (Type: annotation)
    k (Type: ionChannelHH:  conductance=1.0E-11 (SI conductance))
    null (Type: annotation)
    cal (Type: ionChannelHH:  conductance=1.0E-11 (SI conductance))
    leak (Type: ionChannelPassive:  conductance=1.0E-11 (SI conductance))
    null (Type: annotation)
    cah (Type: ionChannelHH:  conductance=1.0E-11 (SI conductance))
    kca (Type: ionChannelHH:  conductance=1.0E-11 (SI conductance))
    null (Type: annotation)
    h (Type: ionChannelHH:  conductance=1.0E-11 (SI conductance))
    ca_conc (Type: fixedFactorConcentrationModel:  restingConc=0.0 (SI concentration) decayConstant=0.013333333333333 (SI time) rho=300000.0 (SI rho_factor))
    null (Type: include)
    na_a (Type: ionChannelHH:  conductance=1.0E-11 (SI conductance))
    null (Type: include)
    null (Type: include)
    null (Type: include)
    null (Type: include)
    null (Type: include)
    null (Type: include)
    null (Type: include)
    null (Type: include)
    null (Type: include)
    null (Type: include)
    cell (Type: cell)
    null (Type: include)
    iclamp0 (Type: pulseGenerator:  delay=0.5 (SI time) duration=0.2 (SI time) amplitude=-3.0E-10 (SI current))
    net (Type: networkWithTemperature:  temperature=300.15 (SI temperature))
    sim1 (Type: Simulation:  length=2.0 (SI time) step=2.5E-5 (SI time))


    This NEURON file has been generated by org.neuroml.export (see https://github.com/NeuroML/org.neuroml.export)
         org.neuroml.export  v1.4.6
         org.neuroml.model   v1.4.6
         jLEMS               v0.9.8.6

'''

import neuron

import time
h = neuron.h
h.load_file("nrngui.hoc")

h("objref p")
h("p = new PythonObject()")

class NeuronSimulation():

    def __init__(self, tstop, dt):

        print("\n    Starting simulation in NEURON generated from NeuroML2 model...\n")

        # Adding simulation Component(id=sim1 type=Simulation) of network/component: net (Type: networkWithTemperature:  temperature=300.15 (SI temperature))

        # Temperature used for network: 300.15 K
        h.celsius = 300.15 - 273.15

        print("Population pop contains 1 instance(s) of component: cell of type: cell")

        print("Setting the default initial concentrations for ca (used in cell) to 3.7152 mM (internal), 3.0 mM (external)")
        h("cai0_ca_ion = 3.7152")
        h("cao0_ca_ion = 3.0")

        h.load_file("cell.hoc")
        a_pop = []
        h("{ n_pop = 1 }")
        h("objectvar a_pop[n_pop]")
        for i in range(int(h.n_pop)):
            h("a_pop[%i] = new cell()"%i)
            h("access a_pop[%i].soma"%i)

        h("{ a_pop[0].position(0, 0, 0) }")

        h("proc initialiseV_pop() { for i = 0, n_pop-1 { a_pop[i].set_initial_v() } }")
        h("objref fih_pop")
        h('{fih_pop = new FInitializeHandler(0, "initialiseV_pop()")}')

        h("proc initialiseIons_pop() { for i = 0, n_pop-1 { a_pop[i].set_initial_ion_properties() } }")
        h("objref fih_ion_pop")
        h('{fih_ion_pop = new FInitializeHandler(1, "initialiseIons_pop()")}')

        # Adding input: Component(id=0 type=input)

        h("objref Clamps_0")
        h("a_pop[0].soma { Clamps_0 = new iclamp0(0.500000) } ")

        trec = h.Vector()
        trec.record(h._ref_t)

        h.tstop = tstop

        h.dt = dt

        h.steps_per_ms = 1/h.dt

        # Display: self.display_d1
        self.display_d1 = h.Graph(0)
        self.display_d1.size(0,h.tstop,-80.0,50.0)
        self.display_d1.view(0, -80.0, h.tstop, 130.0, 80, 330, 330, 250)
        h.graphList[0].append(self.display_d1)
        # Line, plotting: pop/0/cell/0/v
        self.display_d1.addexpr("a_pop[0].soma.v(0.5)", "a_pop[0].soma.v(0.5)", 1, 1, 0.8, 0.9, 2)
        # Line, plotting: pop/0/cell/1/v
        self.display_d1.addexpr("a_pop[0].dend.v(0.5)", "a_pop[0].dend.v(0.5)", 2, 1, 0.8, 0.9, 2)
        # Line, plotting: pop/0/cell/2/v
        self.display_d1.addexpr("a_pop[0].axon.v(0.5)", "a_pop[0].axon.v(0.5)", 3, 1, 0.8, 0.9, 2)



        # File to save: time
        # Column: time
        h(' objectvar v_time ')
        h(' { v_time = new Vector() } ')
        h(' { v_time.record(&t) } ')
        h.v_time.resize((h.tstop * h.steps_per_ms) + 1)

        # File to save: outf
        # Column: pop/0/cell/0/v
        h(' objectvar v_v_outf ')
        h(' { v_v_outf = new Vector() } ')
        h(' { v_v_outf.record(&a_pop[0].soma.v(0.5)) } ')
        h.v_v_outf.resize((h.tstop * h.steps_per_ms) + 1)
        # Column: pop/0/cell/1/v
        h(' objectvar v_v_outf_ ')
        h(' { v_v_outf_ = new Vector() } ')
        h(' { v_v_outf_.record(&a_pop[0].dend.v(0.5)) } ')
        h.v_v_outf_.resize((h.tstop * h.steps_per_ms) + 1)
        # Column: pop/0/cell/2/v
        h(' objectvar v_v_outf__ ')
        h(' { v_v_outf__ = new Vector() } ')
        h(' { v_v_outf__.record(&a_pop[0].axon.v(0.5)) } ')
        h.v_v_outf__.resize((h.tstop * h.steps_per_ms) + 1)
        # Column: pop/0/cell/1/caConc
        h(' objectvar v_v_outf___ ')
        h(' { v_v_outf___ = new Vector() } ')
        h(' { v_v_outf___.record(&a_pop[0].dend.cai(0.5)) } ')
        h.v_v_outf___.resize((h.tstop * h.steps_per_ms) + 1)

        self.initialized = False

        self.sim_end = -1 # will be overwritten

        h.nrncontrolmenu()


    def run(self):

        self.initialized = True
        sim_start = time.time()
        print("Running a simulation of %sms (dt = %sms)" % (h.tstop, h.dt))

        h.run()

        self.sim_end = time.time()
        sim_time = self.sim_end - sim_start
        print("Finished NEURON simulation in %f seconds (%f mins)..."%(sim_time, sim_time/60.0))

        self.save_results()


    def advance(self):

        if not self.initialized:
            h.finitialize()
            self.initialized = True

        h.fadvance()


    def save_results(self):

        print("Saving results at t=%s..."%h.t)

        if self.sim_end < 0: self.sim_end = time.time()

        self.display_d1.exec_menu("View = plot")

        # File to save: time
        py_v_time = [ t/1000 for t in h.v_time.to_python() ]  # Convert to Python list for speed...

        f_time_f2 = open('time.dat', 'w')
        for i in range(int(h.tstop * h.steps_per_ms) + 1):
            f_time_f2.write('%f'% py_v_time[i])  # Save in SI units...+ '\n')
        f_time_f2.close()
        print("Saved data to: time.dat")

        # File to save: outf
        py_v_v_outf = [ float(x  / 1000.0) for x in h.v_v_outf.to_python() ]  # Convert to Python list for speed, variable has dim: voltage
        py_v_v_outf_ = [ float(x  / 1000.0) for x in h.v_v_outf_.to_python() ]  # Convert to Python list for speed, variable has dim: voltage
        py_v_v_outf__ = [ float(x  / 1000.0) for x in h.v_v_outf__.to_python() ]  # Convert to Python list for speed, variable has dim: voltage
        py_v_v_outf___ = [ float(x ) for x in h.v_v_outf___.to_python() ]  # Convert to Python list for speed, variable has dim: concentration

        f_outf_f2 = open('single_olive_iclamp.dat', 'w')
        for i in range(int(h.tstop * h.steps_per_ms) + 1):
            f_outf_f2.write('%e\t'% py_v_time[i]  + '%e\t'%(py_v_v_outf[i])  + '%e\t'%(py_v_v_outf_[i])  + '%e\t'%(py_v_v_outf__[i])  + '%e\t'%(py_v_v_outf___[i]) + '\n')
        f_outf_f2.close()
        print("Saved data to: single_olive_iclamp.dat")

        save_end = time.time()
        save_time = save_end - self.sim_end
        print("Finished saving results in %f seconds"%(save_time))

        print("Done")

if __name__ == '__main__':

    ns = NeuronSimulation(tstop=2000 , dt=0.025)

    ns.run()

