<s> """<STR_LIT>""" <EOL> import re , time <EOL> from openmdao . main . api import Component <EOL> from openmdao . main . datatypes . api import Float <EOL> from openmdao . main . expreval import ExprEvaluator , _expr_dict <EOL> from numpy import zeros <EOL> class ExecComp ( Component ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , exprs = ( ) , sleep = <NUM_LIT:0> , trace = False ) : <EOL> super ( ExecComp , self ) . __init__ ( ) <EOL> outs = set ( ) <EOL> allvars = set ( ) <EOL> self . exprs = exprs <EOL> self . codes = [ compile ( expr , '<STR_LIT>' , '<STR_LIT>' ) for expr in exprs ] <EOL> self . sleep = sleep <EOL> self . trace = trace <EOL> for expr in exprs : <EOL> lhs , rhs = expr . split ( '<STR_LIT:=>' ) <EOL> lhs = lhs . strip ( ) <EOL> lhs = lhs . split ( '<STR_LIT:U+002C>' ) <EOL> outs . update ( lhs ) <EOL> expreval = ExprEvaluator ( expr , scope = self ) <EOL> allvars . update ( expreval . get_referenced_varpaths ( copy = False ) ) <EOL> for var in allvars : <EOL> if '<STR_LIT:.>' not in var : <EOL> if var in outs : <EOL> iotype = '<STR_LIT>' <EOL> else : <EOL> iotype = '<STR_LIT>' <EOL> self . add ( var , Float ( <NUM_LIT:0.0> , iotype = iotype ) ) <EOL> def __getstate__ ( self ) : <EOL> """<STR_LIT>""" <EOL> state = super ( ExecComp , self ) . __getstate__ ( ) <EOL> state [ '<STR_LIT>' ] = None <EOL> return state <EOL> def __setstate__ ( self , state ) : <EOL> """<STR_LIT>""" <EOL> super ( ExecComp , self ) . __setstate__ ( state ) <EOL> self . codes = [ compile ( expr , '<STR_LIT>' , '<STR_LIT>' ) for expr in self . exprs ] <EOL> def execute ( self ) : <EOL> '''<STR_LIT>''' <EOL> global _expr_dict <EOL> if self . trace : <EOL> for var in self . list_inputs ( connected = True ) : <EOL> print "<STR_LIT>" % ( self . name , var , getattr ( self , var ) ) <EOL> for var in self . list_outputs ( connected = True ) : <EOL> print "<STR_LIT>" % ( self . name , var , getattr ( self , var ) ) <EOL> for expr in self . codes : <EOL> exec ( expr , _expr_dict , self . __dict__ ) <EOL> if self . sleep : <EOL> time . sleep ( self . sleep ) <EOL> if self . trace : <EOL> for var in self . list_inputs ( connected = True ) : <EOL> print "<STR_LIT>" % ( self . name , var , getattr ( self , var ) ) <EOL> for var in self . list_outputs ( connected = True ) : <EOL> print "<STR_LIT>" % ( self . name , var , getattr ( self , var ) ) <EOL> class ExecCompWithDerivatives ( Component ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , exprs = ( ) , derivatives = ( ) , sleep = <NUM_LIT:0> , dsleep = <NUM_LIT:0> ) : <EOL> super ( ExecCompWithDerivatives , self ) . __init__ ( ) <EOL> outs = set ( ) <EOL> allvars = set ( ) <EOL> self . exprs = exprs <EOL> self . codes = [ compile ( expr , '<STR_LIT>' , '<STR_LIT>' ) for expr in exprs ] <EOL> self . sleep = sleep <EOL> self . dsleep = dsleep <EOL> for expr in exprs : <EOL> lhs , rhs = expr . split ( '<STR_LIT:=>' ) <EOL> lhs = lhs . strip ( ) <EOL> lhs = lhs . split ( '<STR_LIT:U+002C>' ) <EOL> outs . update ( lhs ) <EOL> expreval = ExprEvaluator ( expr , scope = self ) <EOL> allvars . update ( expreval . get_referenced_varpaths ( copy = False ) ) <EOL> for var in allvars : <EOL> if '<STR_LIT:.>' not in var : <EOL> if var in outs : <EOL> iotype = '<STR_LIT>' <EOL> else : <EOL> iotype = '<STR_LIT>' <EOL> self . add ( var , Float ( <NUM_LIT:0.0> , iotype = iotype ) ) <EOL> self . deriv_exprs = derivatives <EOL> self . derivative_codes = [ compile ( expr , '<STR_LIT>' , '<STR_LIT>' ) for expr in derivatives ] <EOL> self . derivative_names = [ ] <EOL> regex = re . compile ( '<STR_LIT>' ) <EOL> for expr in derivatives : <EOL> expreval = ExprEvaluator ( expr , scope = self ) <EOL> exvars = expreval . get_referenced_varpaths ( copy = False ) <EOL> lhs , _ = expr . split ( '<STR_LIT:=>' ) <EOL> lhs = lhs . strip ( ) <EOL> allvars . add ( lhs ) <EOL> if len ( exvars - allvars ) > <NUM_LIT:0> : <EOL> self . raise_exception ( '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> ValueError ) <EOL> names = regex . findall ( lhs ) <EOL> num = names [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> wrt = names [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> self . derivative_names . append ( ( lhs , num , wrt ) ) <EOL> def __getstate__ ( self ) : <EOL> """<STR_LIT>""" <EOL> state = super ( ExecCompWithDerivatives , self ) . __getstate__ ( ) <EOL> state [ '<STR_LIT>' ] = None <EOL> state [ '<STR_LIT>' ] = None <EOL> return state <EOL> def __setstate__ ( self , state ) : <EOL> """<STR_LIT>""" <EOL> super ( ExecCompWithDerivatives , self ) . __setstate__ ( state ) <EOL> self . codes = [ compile ( expr , '<STR_LIT>' , '<STR_LIT>' ) for expr in self . exprs ] <EOL> self . derivative_codes = [ compile ( expr , '<STR_LIT>' , '<STR_LIT>' ) for expr in self . deriv_exprs ] <EOL> def execute ( self ) : <EOL> '''<STR_LIT>''' <EOL> global _expr_dict <EOL> for expr in self . codes : <EOL> exec ( expr , _expr_dict , self . __dict__ ) <EOL> if self . sleep : <EOL> time . sleep ( self . sleep ) <EOL> def provideJ ( self ) : <EOL> '''<STR_LIT>''' <EOL> global _expr_dict <EOL> for expr in self . derivative_codes : <EOL> exec ( expr , _expr_dict , self . __dict__ ) <EOL> inputs , outputs = self . list_deriv_vars ( ) <EOL> self . J = zeros ( ( len ( outputs ) , len ( inputs ) ) ) <EOL> for item in sorted ( self . derivative_names ) : <EOL> inp = inputs . index ( item [ <NUM_LIT:2> ] ) <EOL> outp = outputs . index ( item [ <NUM_LIT:1> ] ) <EOL> self . J [ outp , inp ] = getattr ( self , item [ <NUM_LIT:0> ] ) <EOL> if self . dsleep : <EOL> time . sleep ( self . dsleep ) <EOL> return self . J <EOL> def list_deriv_vars ( self ) : <EOL> """<STR_LIT>""" <EOL> input_keys = [ x [ <NUM_LIT:0> ] for x in self . items ( framework_var = None , <EOL> iotype = '<STR_LIT>' ) ] <EOL> output_keys = [ x [ <NUM_LIT:0> ] for x in self . items ( framework_var = None , <EOL> iotype = '<STR_LIT>' ) ] <EOL> return input_keys , output_keys </s>
