def getModel(system): # check dependancy set_provided = set([equ[0] for equ in system]) set_needed = set(reduce(lambda x, y: x+y, [equ[1] for equ in system])) # dependancy checking is not needed. # if not set_needed <= set_provided: # print 'Needed value is not provided.' # return None # else: # print 'Dependancy test: pass.' # check valid rendered = [] for equ in system: e = Equation(dict(equ[2]), equ[3].replace(':', '_')) var = [ele.replace(':', '_') for ele in [equ[0]]+equ[1]+['t'] ] eval_dict = dict(zip(var, [0.]*len(var))) eval_dict['__builtins__'] = None print '='*20 try: testv = eval(e.render(), eval_dict, safe_dict) print 'Test pass:'******'\tValue(all 0):', testv rendered.append(e.render()) except Exception, exp: print 'Test error:', e.render() print '\t', exp return None, None
def getModel(system, dependancy_check=True): """ :system: the equation system. Get the ODE model to simulate from system. """ # check dependancy set_provided = set([equ[0] for equ in system]) set_needed = set(reduce(lambda x, y: x+y, [equ[1] for equ in system], [])) # dependancy checking is not needed. if dependancy_check: # the needed set is included in the provided set if not set_needed <= set_provided: auto = set_needed - set_provided for ele in list(auto): system.append([ele, [], [], '0']) # debug code # print 'needed:', set_needed # print 'provided:', set_provided # print 'Needed value is not provided.' # return None, 'Needed value is not provided.' # else: # print 'Dependancy test: pass.' # check valid rendered = [] for equ in system: # e is the Equation object e = Equation(dict(equ[2]), equ[3].replace(':', '_')) # all the variables var = [ele.replace(':', '_') for ele in [equ[0]]+equ[1]+['t'] ] # the look-up table of variables eval_dict = dict(zip(var, [0.]*len(var))) eval_dict['__builtins__'] = None try: # try to evaluate the formula testv = eval(e.render(), eval_dict, safe_dict) # print 'Test pass:'******'\tValue(all 0):', testv rendered.append(e.render()) except Exception, exp: print 'Test error:', equ msg = traceback.format_exc() #print '\t', exp.message print msg return None, msg, None