def make_jac_pydstool(ode): jac, new_fnspecs = dst.prepJacobian(ode.funcspec._initargs['varspecs'], ['X', 'Y', 'A', 'B'], ode.funcspec._initargs['fnspecs']) scope = dst.copy(ode.pars) scope.update(new_fnspecs) jac_fn = dst.expr2fun(jac, ensure_args=['t'], **scope) return jac_fn
def make_measure(fn_name, fn_spec, **defs): """Dynamically create a python function for use with calculation context. """ all_defs = defs.copy() q = dst.QuantSpec('_dummy_', fn_spec, treatMultiRefs=False) import PyDSTool.parseUtils as pu mapping = pu.symbolMapClass() assumed_modules = [] tokens = q.parser.tokenized for sym in q.freeSymbols: # Hack, for now: if first (therefore, assumed all) # occurrences of symbol are in quotes, then don't convert. # Better solution would be to make parser create "x" as a single # symbol, at least with a detect quote option first_ix = tokens.index(sym) if first_ix == 0 or (first_ix > 0 and tokens[first_ix-1] not in ['"', "'"]): if pu.isHierarchicalName(sym): parts = sym.split('.') if parts[0] == 'sim': mapping[sym] = 'con.'+sym ## elif parts[0] == 'bombardier': ## # special case as this factory function is defined in that ## # module so that reference will fail at runtime: remove ## # 'bombardier' prefix ## rest_sym = '.'.join(parts[1:]) ## mapping[sym] = rest_sym ## scope = globals() ## # locals override ## scope.update(locals()) ## if parts[1] in scope: ## all_defs[parts[1]] = scope[parts[1]] ## else: ## raise ValueError("Cannot resolve scope of symbol '%s'"%sym) else: # assume module reference assumed_modules.append(parts[0]) # record here to ensure inclusion in dyn_dummy mapping[sym] = 'self.'+sym else: mapping[sym] = 'con.workspace.'+sym elif first_ix > 0 and tokens[first_ix-1] in ['"', "'"]: # put this symbol in the mapping values to ensure not included # as an argument to the function mapping[sym] = sym q.mapNames(mapping) import types for module_name in assumed_modules: global_scope = globals() # test if module name in scope if module_name in global_scope: _mod = global_scope[module_name] if isinstance(_mod, types.ModuleType): all_defs[module_name] = _mod # dyn_dummy contains dummy mappings but declares symbols to leave # evaluating until runtime dyn_dummy = dict(zip(mapping.values(), ['']*len(mapping))) funq = dst.expr2fun(q, ensure_args=['con'], ensure_dynamic=dyn_dummy, for_funcspec=False, fn_name=fn_name, **all_defs) # decorate output funq.attr_name = fn_name return funq
# find dixed points of the model fp_coord = pp.find_fixedpoints(dmModel, n=4, eps=1e-8) # plot the null-clines nulls_x, nulls_y = pp.find_nullclines(dmModel, 's1', 's2', n=3, \ eps=1e-8, max_step=0.01, fps=fp_coord) plot(nulls_x[:,0], nulls_x[:,1], 'b') plot(nulls_y[:,0], nulls_y[:,1], 'g') # compute the jacobian matrix jac, new_fnspecs = dst.prepJacobian(dmModel.funcspec._initargs['varspecs'], ['s1','s2'],dmModel.funcspec._initargs['fnspecs']) scope = dst.copy(dmModel.pars) scope.update(new_fnspecs) jac_fn = dst.expr2fun(jac, ensure_args=['t'],**scope) # add fixed points to the phase portrait for i in range(0,len(fp_coord)): fp = pp.fixedpoint_2D(dmModel, dst.Point(fp_coord[i]), jac = jac_fn, eps=1e-8) pp.plot_PP_fps(fp) # compute and plot projectories traj = dmModel.compute('trajectory1') pts = traj.sample() plot(pts['s1'], pts['s2'], 'r-o') xlabel('s_1') ylabel('s_2') title('Phase plane I1=0.035 nA, I2=0.035 nA')