def build_simulation(component): print 'C++ Simulation' print 'Component', component ev_blks = utils.separate_integration_blocks(component) build_data = CPPBuildData(component, ev_blks) print ev_blks op_blks = '' func_names = [] for i, ev_blk in enumerate(ev_blks): blk, func_name = build_code_for_ev_blk(component=component, ev_blk=ev_blk, ev_blk_index=i, build_data=build_data) op_blks = op_blks + blk func_names.append(func_name) mytemplate = Template(solver_function) buf = StringIO() ctx = Context(buf, ev_blk_func_names=func_names, component=component) mytemplate.render_context(ctx) res = buf.getvalue() + ('\n' * 3) data_mgr_txt = DataMgrBuilder(ev_blks=ev_blks, cpp_builddata=build_data, component=component).get_text() op_file = 'out1.c' with open(op_file, 'w') as fout: fout.write(header1) fout.write(data_mgr_txt) fout.write(header2) fout.write(op_blks) fout.write(res) print 'File written to:', op_file print print print #mytemplate = Template("hello world!") #print mytemplate.render() assert False
def build_simulation(component): print "C++ Simulation" print "Component", component ev_blks = utils.separate_integration_blocks(component) build_data = CPPBuildData(component, ev_blks) print ev_blks op_blks = "" func_names = [] for i, ev_blk in enumerate(ev_blks): blk, func_name = build_code_for_ev_blk( component=component, ev_blk=ev_blk, ev_blk_index=i, build_data=build_data ) op_blks = op_blks + blk func_names.append(func_name) mytemplate = Template(solver_function) buf = StringIO() ctx = Context(buf, ev_blk_func_names=func_names, component=component) mytemplate.render_context(ctx) res = buf.getvalue() + ("\n" * 3) data_mgr_txt = DataMgrBuilder(ev_blks=ev_blks, cpp_builddata=build_data, component=component).get_text() op_file = "out1.c" with open(op_file, "w") as fout: fout.write(header1) fout.write(data_mgr_txt) fout.write(header2) fout.write(op_blks) fout.write(res) print "File written to:", op_file print print print # mytemplate = Template("hello world!") # print mytemplate.render() assert False
def simulate(component, times): print 'Python Simulation' print 'Component', component ev_blks = utils.separate_integration_blocks(component) print print print 'So, I am planning to solve:' for ev_blk in ev_blks: print '<As an event block>:' for analog_blk in ev_blk.analog_blks: print ' As a group:' for obj in analog_blk.objects: print ' -', repr(obj) print print 'then' print print print 'Simulating' tstop = 0.305 #tstop=0.010 dt = 0.01e-3 datastore = AvailableData(time_pts=np.arange(dt, tstop, dt), dt=dt) print 'Initialising empty event queues' for port in component.input_event_port_lut: print ' Port:', repr(port) datastore.events[port] = [] for port in component.output_event_port_lut: print ' Port:', repr(port) datastore.events[port] = [] print print 'Solving Event Blocks:' for ev_blk in ev_blks: solve_eventblock(component=component, evt_blk=ev_blk, datastore=datastore) import pylab as plt plt.figure() for d in datastore.traces.values(): print d.variable.symbol, np.min(d.data), np.max(d.data) sym = d.variable.symbol if sym == 'o1/nrn/nrn/V': plt.plot(datastore.time_pts, d.data, 'x-', label=d.variable.symbol) plt.figure() for d in datastore.traces.values(): print d.variable.symbol, np.min(d.data), np.max(d.data) sym = d.variable.symbol if sym == 'o2/nrn/syn_excit/i': plt.plot(datastore.time_pts, d.data, 'x-', label=d.variable.symbol) plt.legend() plt.figure() for d in datastore.traces.values(): print d.variable.symbol, np.min(d.data), np.max(d.data) sym = d.variable.symbol if sym == 'o2/nrn/syn_excit/A': plt.plot(datastore.time_pts, d.data, 'x-', label=d.variable.symbol) plt.legend() plt.figure() for d in datastore.traces.values(): print d.variable.symbol, np.min(d.data), np.max(d.data) plt.plot(datastore.time_pts, d.data, 'x', label=d.variable.symbol) pylab.legend() import itertools from collections import defaultdict si_base_units = defaultdict(list) plot_objs = list(itertools.chain(*[datastore.traces.values()])) for plt_obj in plot_objs: print plt_obj #terminal_obj = component.get_terminal_obj(plt_obj) dimension = plt_obj.variable.get_dimension() found = False for k, v in si_base_units.items(): if k.is_compatible(dimension): si_base_units[k].append(plt_obj.variable) found = True break if not found: si_base_units[dimension.with_no_powerten()].append( plt_obj.variable) print si_base_units print len(si_base_units) n_axes = len(si_base_units) f = pylab.figure() axes = [f.add_subplot(n_axes, 1, i + 1) for i in range(n_axes)] print 'datastore.traces', datastore.traces for ((unit, objs), ax) in zip(si_base_units.items(), axes): ax.set_ylabel(str(unit)) ax.margins(0.1) for o in sorted(objs): ax.plot(datastore.time_pts[1:], datastore.traces[o.symbol].data[1:], label=repr(o)) ax.legend() pylab.show()
def simulate(component, times): print "Python Simulation" print "Component", component ev_blks = utils.separate_integration_blocks(component) print print print "So, I am planning to solve:" for ev_blk in ev_blks: print "<As an event block>:" for analog_blk in ev_blk.analog_blks: print " As a group:" for obj in analog_blk.objects: print " -", repr(obj) print print "then" print print print "Simulating" tstop = 0.305 # tstop=0.010 dt = 0.01e-3 datastore = AvailableData(time_pts=np.arange(dt, tstop, dt), dt=dt) print "Initialising empty event queues" for port in component.input_event_port_lut: print " Port:", repr(port) datastore.events[port] = [] for port in component.output_event_port_lut: print " Port:", repr(port) datastore.events[port] = [] print print "Solving Event Blocks:" for ev_blk in ev_blks: solve_eventblock(component=component, evt_blk=ev_blk, datastore=datastore) import pylab as plt plt.figure() for d in datastore.traces.values(): print d.variable.symbol, np.min(d.data), np.max(d.data) sym = d.variable.symbol if sym == "o1/nrn/nrn/V": plt.plot(datastore.time_pts, d.data, "x-", label=d.variable.symbol) plt.figure() for d in datastore.traces.values(): print d.variable.symbol, np.min(d.data), np.max(d.data) sym = d.variable.symbol if sym == "o2/nrn/syn_excit/i": plt.plot(datastore.time_pts, d.data, "x-", label=d.variable.symbol) plt.legend() plt.figure() for d in datastore.traces.values(): print d.variable.symbol, np.min(d.data), np.max(d.data) sym = d.variable.symbol if sym == "o2/nrn/syn_excit/A": plt.plot(datastore.time_pts, d.data, "x-", label=d.variable.symbol) plt.legend() plt.figure() for d in datastore.traces.values(): print d.variable.symbol, np.min(d.data), np.max(d.data) plt.plot(datastore.time_pts, d.data, "x", label=d.variable.symbol) pylab.legend() import itertools from collections import defaultdict si_base_units = defaultdict(list) plot_objs = list(itertools.chain(*[datastore.traces.values()])) for plt_obj in plot_objs: print plt_obj # terminal_obj = component.get_terminal_obj(plt_obj) dimension = plt_obj.variable.get_dimension() found = False for k, v in si_base_units.items(): if k.is_compatible(dimension): si_base_units[k].append(plt_obj.variable) found = True break if not found: si_base_units[dimension.with_no_powerten()].append(plt_obj.variable) print si_base_units print len(si_base_units) n_axes = len(si_base_units) f = pylab.figure() axes = [f.add_subplot(n_axes, 1, i + 1) for i in range(n_axes)] print "datastore.traces", datastore.traces for ((unit, objs), ax) in zip(si_base_units.items(), axes): ax.set_ylabel(str(unit)) ax.margins(0.1) for o in sorted(objs): ax.plot(datastore.time_pts[1:], datastore.traces[o.symbol].data[1:], label=repr(o)) ax.legend() pylab.show()