<s> """<STR_LIT>""" <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> from fnmatch import fnmatch <EOL> import re <EOL> import sys <EOL> import traceback <EOL> from itertools import chain <EOL> from numpy import ndarray <EOL> import networkx as nx <EOL> from networkx . algorithms . components import strongly_connected_components <EOL> from networkx . algorithms . dag import is_directed_acyclic_graph <EOL> from openmdao . main . mpiwrap import MPI <EOL> from openmdao . main . exceptions import NoFlatError <EOL> from openmdao . main . interfaces import implements , IAssembly , IDriver , IComponent , IContainer , ICaseRecorder , IHasParameters <EOL> from openmdao . main . mp_support import has_interface <EOL> from openmdao . main . container import _copydict <EOL> from openmdao . main . component import Component , Container <EOL> from openmdao . main . variable import Variable <EOL> from openmdao . main . vartree import VariableTree <EOL> from openmdao . main . datatypes . api import List , Slot , Bool , VarTree <EOL> from openmdao . main . driver import Driver <EOL> from openmdao . main . rbac import rbac <EOL> from openmdao . main . mp_support import is_instance <EOL> from openmdao . main . printexpr import eliminate_expr_ws <EOL> from openmdao . main . expreval import ExprEvaluator , ConnectedExprEvaluator <EOL> from openmdao . main . pseudocomp import PseudoComponent , UnitConversionPComp <EOL> from openmdao . main . array_helpers import is_differentiable_var , get_val_and_index , get_flattened_index , get_var_shape , flattened_size <EOL> from openmdao . main . depgraph import DependencyGraph , all_comps , list_driver_connections , simple_node_iter , is_boundary_node <EOL> from openmdao . main . systems import SimpleSystem , SerialSystem , ParamSystem , _create_simple_sys , OpaqueSystem <EOL> from openmdao . main . mpiwrap import to_idx_array <EOL> from openmdao . util . graph import list_deriv_vars , base_var , fix_single_tuple <EOL> from openmdao . util . log import logger <EOL> from openmdao . util . debug import strict_chk_config <EOL> from openmdao . util . typegroups import real_types <EOL> from openmdao . units import PhysicalQuantity <EOL> from openmdao . util . graphplot import _clean_graph <EOL> from networkx . readwrite import json_graph <EOL> import json <EOL> _iodict = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT:input>' } <EOL> _missing = object ( ) <EOL> def set_as_top ( cont , first_only = False ) : <EOL> """<STR_LIT>""" <EOL> cont . cpath_updated ( ) <EOL> return cont <EOL> class PassthroughTrait ( Variable ) : <EOL> """<STR_LIT>""" <EOL> def validate ( self , obj , name , value ) : <EOL> """<STR_LIT>""" <EOL> if self . validation_trait : <EOL> return self . validation_trait . validate ( obj , name , value ) <EOL> return value <EOL> class PassthroughProperty ( Variable ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , target_trait , ** metadata ) : <EOL> self . _trait = target_trait <EOL> self . _vals = { } <EOL> super ( PassthroughProperty , self ) . __init__ ( ** metadata ) <EOL> def get ( self , obj , name ) : <EOL> v = self . _vals . get ( obj , _missing ) <EOL> if v is not _missing : <EOL> return v . get ( name , self . _trait . default_value ) <EOL> else : <EOL> return self . _trait . default_value <EOL> def set ( self , obj , name , value ) : <EOL> if obj not in self . _vals : <EOL> self . _vals [ obj ] = { } <EOL> old = self . get ( obj , name ) <EOL> if value != old : <EOL> self . _vals [ obj ] [ name ] = self . _trait . validate ( obj , name , value ) <EOL> obj . trait_property_changed ( name , old , value ) <EOL> class RecordingOptions ( VariableTree ) : <EOL> """<STR_LIT>""" <EOL> save_problem_formulation = Bool ( True , desc = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> includes = List ( [ '<STR_LIT:*>' ] , desc = '<STR_LIT>' ) <EOL> excludes = List ( [ ] , desc = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> class Assembly ( Component ) : <EOL> """<STR_LIT>""" <EOL> implements ( IAssembly ) <EOL> driver = Slot ( IDriver , allow_none = True , <EOL> desc = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> recorders = List ( Slot ( ICaseRecorder , required = False ) , <EOL> desc = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> recording_options = VarTree ( RecordingOptions ( ) , iotype = '<STR_LIT>' , <EOL> framework_var = True , deriv_ignore = True , <EOL> desc = '<STR_LIT>' ) <EOL> def __init__ ( self ) : <EOL> super ( Assembly , self ) . __init__ ( ) <EOL> self . _pseudo_count = <NUM_LIT:0> <EOL> self . _pre_driver = None <EOL> self . _derivs_required = False <EOL> self . _unexecuted = [ ] <EOL> self . _var_meta = { } <EOL> self . _connections = [ ] <EOL> self . _depgraph = None <EOL> self . _reduced_graph = None <EOL> self . add ( '<STR_LIT>' , Driver ( ) ) <EOL> self . missing_deriv_policy = '<STR_LIT>' <EOL> self . add ( '<STR_LIT>' , RecordingOptions ( ) ) <EOL> @ property <EOL> def _top_driver ( self ) : <EOL> if self . _pre_driver : <EOL> return self . _pre_driver <EOL> return self . driver <EOL> @ rbac ( ( '<STR_LIT>' , '<STR_LIT:user>' ) ) <EOL> def set_itername ( self , itername , seqno = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> super ( Assembly , self ) . set_itername ( itername ) <EOL> self . _top_driver . set_itername ( itername ) <EOL> if seqno : <EOL> self . _top_driver . workflow . set_initial_count ( seqno ) <EOL> def find_referring_connections ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return [ ( u . text , v . text ) for u , v in self . _connections <EOL> if u . refers_to ( name ) or v . refers_to ( name ) ] <EOL> def find_in_workflows ( self , name ) : <EOL> """<STR_LIT>""" <EOL> wflows = [ ] <EOL> for item in self . list_containers ( ) : <EOL> if item != name : <EOL> obj = self . get ( item ) <EOL> if isinstance ( obj , Driver ) and name in obj . workflow . _explicit_names : <EOL> wflows . append ( ( obj . workflow , obj . workflow . _explicit_names . index ( name ) ) ) <EOL> return wflows <EOL> def rename ( self , oldname , newname ) : <EOL> """<STR_LIT>""" <EOL> self . config_changed ( ) <EOL> self . _check_rename ( oldname , newname ) <EOL> for comp in self . get_comps ( ) : <EOL> comp . name_changed ( oldname , newname ) <EOL> for u , v in self . _connections : <EOL> u . name_changed ( oldname , newname ) <EOL> v . name_changed ( oldname , newname ) <EOL> obj = self . get ( oldname ) <EOL> obj . name = newname <EOL> self . add ( newname , obj ) <EOL> self . remove_trait ( oldname ) <EOL> def replace ( self , target_name , newobj ) : <EOL> """<STR_LIT>""" <EOL> tobj = getattr ( self , target_name ) <EOL> if not tobj : <EOL> self . add ( target_name , newobj ) <EOL> return <EOL> refs = { } <EOL> if has_interface ( tobj , IComponent ) : <EOL> for cname in self . list_containers ( ) : <EOL> obj = getattr ( self , cname ) <EOL> if obj is not tobj and has_interface ( obj , IDriver ) : <EOL> refs [ cname ] = obj . get_references ( target_name ) <EOL> if hasattr ( newobj , '<STR_LIT>' ) : <EOL> try : <EOL> newobj . mimic ( tobj ) <EOL> except Exception : <EOL> self . reraise_exception ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( target_name , type ( tobj ) . __name__ , <EOL> type ( newobj ) . __name__ ) , sys . exc_info ( ) ) <EOL> conns = self . find_referring_connections ( target_name ) <EOL> wflows = self . find_in_workflows ( target_name ) <EOL> if self . _call_cpath_updated is False and isinstance ( obj , Container ) : <EOL> newobj . parent = self <EOL> newobj . name = target_name <EOL> newobj . cpath_updated ( ) <EOL> req_vars = set ( [ u . split ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ <NUM_LIT:1> ] . split ( '<STR_LIT:[>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> for u , v in conns if u . startswith ( target_name + '<STR_LIT:.>' ) ] ) <EOL> req_vars . update ( [ v . split ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ <NUM_LIT:1> ] . split ( '<STR_LIT:[>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> for u , v in conns if v . startswith ( target_name + '<STR_LIT:.>' ) ] ) <EOL> missing = [ v for v in req_vars if not newobj . contains ( v ) ] <EOL> if missing : <EOL> self . _logger . warning ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % missing ) <EOL> for u , v in conns : <EOL> self . disconnect ( u , v ) <EOL> if has_interface ( newobj , IComponent ) : <EOL> self . disconnect ( newobj . name ) <EOL> self . add ( target_name , newobj ) <EOL> for u , v in conns : <EOL> try : <EOL> self . get ( u ) <EOL> self . get ( v ) <EOL> self . connect ( u , v ) <EOL> except Exception as err : <EOL> self . _logger . warning ( "<STR_LIT>" , <EOL> u , v , err ) <EOL> for dname , _refs in refs . items ( ) : <EOL> drv = getattr ( self , dname ) <EOL> drv . remove_references ( target_name ) <EOL> drv . restore_references ( _refs ) <EOL> if has_interface ( newobj , IComponent ) : <EOL> for wflow , idx in wflows : <EOL> wflow . add ( target_name , idx ) <EOL> def remove ( self , name ) : <EOL> """<STR_LIT>""" <EOL> obj = getattr ( self , name ) <EOL> if has_interface ( obj , IComponent ) or isinstance ( obj , PseudoComponent ) : <EOL> for cname in self . list_containers ( ) : <EOL> cobj = getattr ( self , cname ) <EOL> if isinstance ( cobj , Driver ) and cobj is not obj : <EOL> cobj . remove_references ( name ) <EOL> self . disconnect ( name ) <EOL> elif name in self . list_inputs ( ) or name in self . list_outputs ( ) : <EOL> self . disconnect ( name ) <EOL> return super ( Assembly , self ) . remove ( name ) <EOL> def create_passthrough ( self , pathname , alias = None ) : <EOL> """<STR_LIT>""" <EOL> parts = pathname . split ( '<STR_LIT:.>' ) <EOL> if alias : <EOL> newname = alias <EOL> else : <EOL> newname = parts [ - <NUM_LIT:1> ] <EOL> if newname in self . __dict__ : <EOL> self . raise_exception ( "<STR_LIT>" % newname , KeyError ) <EOL> if len ( parts ) < <NUM_LIT:2> : <EOL> self . raise_exception ( '<STR_LIT>' <EOL> '<STR_LIT>' , NameError ) <EOL> comp = self <EOL> for part in parts [ : - <NUM_LIT:1> ] : <EOL> try : <EOL> comp = getattr ( comp , part ) <EOL> except AttributeError : <EOL> trait = None <EOL> break <EOL> else : <EOL> trait = comp . get_trait ( parts [ - <NUM_LIT:1> ] ) <EOL> iotype = comp . get_iotype ( parts [ - <NUM_LIT:1> ] ) <EOL> if trait : <EOL> ttype = trait . trait_type <EOL> if ttype is None : <EOL> ttype = trait <EOL> else : <EOL> if not self . contains ( pathname ) : <EOL> self . raise_exception ( "<STR_LIT>" % <EOL> pathname , KeyError ) <EOL> iotype = self . get_metadata ( pathname , '<STR_LIT>' ) <EOL> if trait is not None and not trait . validate : <EOL> trait = None <EOL> metadata = self . get_metadata ( pathname ) <EOL> metadata [ '<STR_LIT:target>' ] = pathname <EOL> metadata [ '<STR_LIT>' ] = trait . trait_type . default_value <EOL> if "<STR_LIT>" in metadata : <EOL> if metadata [ '<STR_LIT>' ] . get is None : <EOL> newtrait = PassthroughTrait ( ** metadata ) <EOL> else : <EOL> newtrait = PassthroughProperty ( metadata [ '<STR_LIT>' ] , <EOL> ** metadata ) <EOL> elif trait and ttype . get : <EOL> newtrait = PassthroughProperty ( ttype , ** metadata ) <EOL> else : <EOL> newtrait = PassthroughTrait ( validation_trait = trait , ** metadata ) <EOL> self . add_trait ( newname , newtrait ) <EOL> val = self . get ( pathname ) <EOL> ttype = trait . trait_type <EOL> if ttype . copy : <EOL> if isinstance ( val , Container ) : <EOL> val_copy = val . copy ( ) <EOL> val_copy . parent = self <EOL> val = val_copy <EOL> val . name = newname <EOL> else : <EOL> val = _copydict [ ttype . copy ] ( val ) <EOL> setattr ( self , newname , val ) <EOL> try : <EOL> if iotype == '<STR_LIT>' : <EOL> self . connect ( newname , pathname ) <EOL> else : <EOL> self . connect ( pathname , newname ) <EOL> except RuntimeError : <EOL> info = sys . exc_info ( ) <EOL> self . remove ( newname ) <EOL> raise info [ <NUM_LIT:0> ] , info [ <NUM_LIT:1> ] , info [ <NUM_LIT:2> ] <EOL> return newtrait <EOL> def _find_unexecuted_comps ( self ) : <EOL> self . _unexecuted = [ ] <EOL> self . _pre_driver = None <EOL> cgraph = self . _depgraph . component_graph ( ) <EOL> wfcomps = set ( [ c . name for c in self . driver . iteration_set ( ) ] ) <EOL> wfcomps . add ( '<STR_LIT>' ) <EOL> diff = set ( cgraph . nodes ( ) ) - wfcomps <EOL> self . _pre_driver = None <EOL> if diff : <EOL> out_edges = nx . edge_boundary ( cgraph , diff ) <EOL> pre = [ u for u , v in out_edges ] <EOL> self . _unexecuted = list ( diff - set ( pre ) ) <EOL> if pre : <EOL> self . _pre_driver = Driver ( ) <EOL> self . _pre_driver . parent = self <EOL> pre . append ( '<STR_LIT>' ) <EOL> self . _pre_driver . workflow . add ( pre ) <EOL> self . _pre_driver . name = '<STR_LIT>' <EOL> self . _pre_driver . _iter_set = set ( pre ) <EOL> self . _pre_driver . _full_iter_set = self . driver . _full_iter_set . union ( pre ) <EOL> self . _depgraph . add_node ( '<STR_LIT>' , comp = True , driver = True ) <EOL> @ rbac ( ( '<STR_LIT>' , '<STR_LIT:user>' ) ) <EOL> def collect_metadata ( self ) : <EOL> self . _get_all_var_metadata ( self . _reduced_graph ) <EOL> for comp in self . get_comps ( ) : <EOL> if has_interface ( comp , IAssembly ) : <EOL> comp . collect_metadata ( ) <EOL> @ rbac ( ( '<STR_LIT>' , '<STR_LIT:user>' ) ) <EOL> def check_config ( self , strict = False ) : <EOL> """<STR_LIT>""" <EOL> super ( Assembly , self ) . check_config ( strict = strict ) <EOL> try : <EOL> self . _check_input_collisions ( self . _depgraph ) <EOL> self . _check_unset_req_vars ( ) <EOL> self . _check_unexecuted_comps ( strict ) <EOL> except : <EOL> info = sys . exc_info ( ) <EOL> self . _new_config = True <EOL> raise info [ <NUM_LIT:0> ] , info [ <NUM_LIT:1> ] , info [ <NUM_LIT:2> ] <EOL> def _check_input_collisions ( self , graph ) : <EOL> dests = set ( [ v for u , v in graph . list_connections ( ) if <EOL> '<STR_LIT>' not in graph [ u ] [ v ] and <EOL> '<STR_LIT>' not in graph [ u ] [ v ] ] ) <EOL> allbases = set ( [ base_var ( graph , v ) for v in dests ] ) <EOL> unconnected_bases = allbases - dests <EOL> connected_bases = allbases - unconnected_bases <EOL> collisions = [ ] <EOL> for drv in chain ( [ self . _top_driver ] , <EOL> self . _top_driver . subdrivers ( recurse = True ) ) : <EOL> if has_interface ( drv , IHasParameters ) : <EOL> for target in drv . list_param_targets ( ) : <EOL> tbase = base_var ( graph , target ) <EOL> if target == tbase : <EOL> if target in allbases : <EOL> collisions . append ( "<STR_LIT>" <EOL> % ( target , drv . get_pathname ( ) ) ) <EOL> else : <EOL> if target in dests or tbase in connected_bases : <EOL> collisions . append ( "<STR_LIT>" <EOL> % ( target , drv . get_pathname ( ) ) ) <EOL> if collisions : <EOL> self . raise_exception ( "<STR_LIT>" <EOL> "<STR_LIT>" % '<STR_LIT:U+002C>' . join ( collisions ) , <EOL> RuntimeError ) <EOL> def _check_unexecuted_comps ( self , strict ) : <EOL> if strict_chk_config ( strict ) : <EOL> errfunct = self . raise_exception <EOL> else : <EOL> errfunct = self . _logger . warning <EOL> if self . _pre_driver : <EOL> msg = "<STR_LIT>" "<STR_LIT>" <EOL> if not strict_chk_config ( strict ) : <EOL> msg += "<STR_LIT>" "<STR_LIT>" <EOL> pre = self . _pre_driver . _iter_set - set ( [ '<STR_LIT>' ] ) <EOL> msg += "<STR_LIT>" % list ( sorted ( pre ) ) <EOL> errfunct ( msg ) <EOL> if self . _unexecuted : <EOL> errfunct ( "<STR_LIT>" <EOL> "<STR_LIT>" % list ( self . _unexecuted ) ) <EOL> def _check_unset_req_vars ( self ) : <EOL> """<STR_LIT>""" <EOL> graph = self . _depgraph <EOL> for name in chain ( all_comps ( graph ) , <EOL> graph . get_boundary_inputs ( ) , <EOL> graph . get_boundary_outputs ( ) ) : <EOL> obj = getattr ( self , name ) <EOL> if has_interface ( obj , IContainer ) : <EOL> for vname in obj . get_req_default ( self . trait ( name ) . required ) : <EOL> base = base_var ( graph , vname ) <EOL> indeg = graph . in_degree ( base ) <EOL> io = graph . node [ base ] [ '<STR_LIT>' ] <EOL> if ( io == '<STR_LIT>' and indeg < <NUM_LIT:1> ) or ( io == '<STR_LIT:state>' and indeg < <NUM_LIT:2> ) : <EOL> self . raise_exception ( "<STR_LIT>" <EOL> "<STR_LIT>" % vname , RuntimeError ) <EOL> def _check_connect ( self , src , dest ) : <EOL> """<STR_LIT>""" <EOL> destexpr = ConnectedExprEvaluator ( dest , self , is_dest = True ) <EOL> srcexpr = ConnectedExprEvaluator ( src , self , <EOL> getter = '<STR_LIT>' ) <EOL> srccomps = srcexpr . get_referenced_compnames ( ) <EOL> destcomps = list ( destexpr . get_referenced_compnames ( ) ) <EOL> if destcomps and destcomps [ <NUM_LIT:0> ] in srccomps : <EOL> raise RuntimeError ( "<STR_LIT>" <EOL> % ( src , dest ) ) <EOL> return srcexpr , destexpr <EOL> @ rbac ( ( '<STR_LIT>' , '<STR_LIT:user>' ) ) <EOL> def connect ( self , src , dest ) : <EOL> """<STR_LIT>""" <EOL> src = eliminate_expr_ws ( src ) <EOL> if isinstance ( dest , basestring ) : <EOL> dest = ( dest , ) <EOL> for dst in dest : <EOL> dst = eliminate_expr_ws ( dst ) <EOL> self . _connect ( src , dst ) <EOL> def _connect ( self , src , dest ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> try : <EOL> srcexpr , destexpr = self . _check_connect ( src , dest ) <EOL> self . _connections . append ( ( srcexpr , destexpr ) ) <EOL> destval = destexpr . evaluate ( ) <EOL> if isinstance ( destval , ndarray ) and destval . size == <NUM_LIT:0> : <EOL> destexpr . set ( srcexpr . evaluate ( ) , self ) <EOL> self . _check_dest ( srcexpr , destexpr ) <EOL> except AttributeError as err : <EOL> exc_type , value , traceback = sys . exc_info ( ) <EOL> invalid_vars = srcexpr . get_unresolved ( ) + destexpr . get_unresolved ( ) <EOL> parts = invalid_vars [ <NUM_LIT:0> ] . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> parent = repr ( self . name ) if self . name else '<STR_LIT>' <EOL> vname = repr ( parts [ <NUM_LIT:0> ] ) <EOL> if len ( parts ) > <NUM_LIT:1> : <EOL> parent = repr ( parts [ <NUM_LIT:0> ] ) <EOL> vname = repr ( parts [ <NUM_LIT:1> ] ) <EOL> msg = "<STR_LIT>" <EOL> msg = msg . format ( parent = parent , vname = vname ) <EOL> raise exc_type , exc_type ( msg ) , traceback <EOL> except : <EOL> info = sys . exc_info ( ) <EOL> self . reraise_exception ( "<STR_LIT>" % <EOL> ( src , dest ) , info ) <EOL> self . config_changed ( ) <EOL> def _check_dest ( self , srcexpr , destexpr ) : <EOL> """<STR_LIT>""" <EOL> srcvars = srcexpr . get_referenced_varpaths ( copy = False ) <EOL> destvar = destexpr . get_referenced_varpaths ( ) . pop ( ) <EOL> destcompname , destcomp , destvarname = _split_varpath ( self , destvar ) <EOL> desttrait = None <EOL> srccomp = None <EOL> if not isinstance ( destcomp , PseudoComponent ) and not destvar . startswith ( '<STR_LIT>' ) and not len ( srcvars ) > <NUM_LIT:1> : <EOL> for srcvar in srcvars : <EOL> if not srcvar . startswith ( '<STR_LIT>' ) : <EOL> srccompname , srccomp , srcvarname = _split_varpath ( self , srcvar ) <EOL> if not isinstance ( srccomp , PseudoComponent ) : <EOL> src_io = '<STR_LIT>' if srccomp is self else '<STR_LIT>' <EOL> srccomp . get_dyn_trait ( srcvarname , src_io ) <EOL> if desttrait is None : <EOL> dest_io = '<STR_LIT>' if destcomp is self else '<STR_LIT>' <EOL> desttrait = destcomp . get_dyn_trait ( destvarname , dest_io ) <EOL> if not isinstance ( srccomp , PseudoComponent ) and desttrait is not None : <EOL> if destvar == destexpr . text : <EOL> ttype = desttrait . trait_type <EOL> if not ttype : <EOL> ttype = desttrait <EOL> srcval = srcexpr . evaluate ( ) <EOL> if ttype . validate : <EOL> ttype . validate ( destcomp , destvarname , srcval ) <EOL> else : <EOL> pass <EOL> @ rbac ( ( '<STR_LIT>' , '<STR_LIT:user>' ) ) <EOL> def disconnect ( self , varpath , varpath2 = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if varpath2 is None : <EOL> to_remove = [ ( u , v ) for u , v in self . _connections <EOL> if u . refers_to ( varpath ) or v . refers_to ( varpath ) <EOL> or u . text == varpath . strip ( ) <EOL> or v . text == varpath . strip ( ) ] <EOL> else : <EOL> to_remove = [ ( u , v ) for u , v in self . _connections <EOL> if u . text == varpath . strip ( ) <EOL> and v . text == varpath2 . strip ( ) ] <EOL> self . _connections = [ tup for tup in self . _connections <EOL> if tup not in to_remove ] <EOL> finally : <EOL> self . config_changed ( update_parent = False ) <EOL> def config_changed ( self , update_parent = True ) : <EOL> """<STR_LIT>""" <EOL> super ( Assembly , self ) . config_changed ( update_parent ) <EOL> for name in self . list_containers ( ) : <EOL> cont = getattr ( self , name ) <EOL> if isinstance ( cont , Driver ) : <EOL> cont . config_changed ( update_parent = False ) <EOL> self . _pre_driver = None <EOL> self . _system = None <EOL> def _set_failed ( self , path , value ) : <EOL> parts = path . split ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> if len ( parts ) > <NUM_LIT:1> : <EOL> obj = getattr ( self , parts [ <NUM_LIT:0> ] ) <EOL> if isinstance ( obj , PseudoComponent ) : <EOL> obj . set ( parts [ <NUM_LIT:1> ] , value ) <EOL> def execute ( self ) : <EOL> """<STR_LIT>""" <EOL> for system in self . _system . local_subsystems ( ) : <EOL> system . pre_run ( ) <EOL> self . _system . run ( self . itername , case_uuid = self . _case_uuid ) <EOL> dconns = [ c [ <NUM_LIT:1> ] for c in self . list_connections ( ) if c [ <NUM_LIT:0> ] . split ( '<STR_LIT:[>' ) [ <NUM_LIT:0> ] . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] in self . list_inputs ( ) and c [ <NUM_LIT:1> ] . split ( '<STR_LIT:[>' ) [ <NUM_LIT:0> ] . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] in self . list_outputs ( ) ] <EOL> self . _system . vec [ '<STR_LIT:u>' ] . set_to_scope ( self , vnames = dconns ) <EOL> def configure_recording ( self , recording_options = None ) : <EOL> """<STR_LIT>""" <EOL> if self . parent is None : <EOL> if self . recorders : <EOL> recording_options = self . recording_options <EOL> for recorder in self . recorders : <EOL> recorder . startup ( ) <EOL> else : <EOL> recording_options = None <EOL> if recording_options : <EOL> includes = recording_options . includes <EOL> excludes = recording_options . excludes <EOL> save_problem_formulation = recording_options . save_problem_formulation <EOL> else : <EOL> includes = excludes = save_problem_formulation = None <EOL> inputs = set ( ) <EOL> constants = { } <EOL> for name in self . list_containers ( ) : <EOL> obj = getattr ( self , name ) <EOL> if has_interface ( obj , IDriver , IAssembly ) : <EOL> inps , consts = obj . configure_recording ( recording_options ) <EOL> inputs . update ( inps ) <EOL> constants . update ( consts ) <EOL> if not save_problem_formulation and not includes : <EOL> return ( inputs , constants ) <EOL> top = self <EOL> while top . parent : <EOL> top = top . parent <EOL> prefix_drop = len ( top . name ) + <NUM_LIT:1> if top . name else <NUM_LIT:0> <EOL> objs = [ self ] <EOL> objs . extend ( getattr ( self , name ) for name in self . list_containers ( ) ) <EOL> for obj in objs : <EOL> if has_interface ( obj , IComponent ) : <EOL> prefix = obj . get_pathname ( ) [ prefix_drop : ] <EOL> if prefix : <EOL> prefix += '<STR_LIT:.>' <EOL> in_names = obj . list_inputs ( ) <EOL> if obj . parent is not None : <EOL> conn = obj . parent . connected_inputs ( obj . name ) <EOL> for name in conn : <EOL> obj_name , _ , in_name = name . partition ( '<STR_LIT:.>' ) <EOL> if in_name in in_names : <EOL> in_names . remove ( in_name ) <EOL> for name in in_names : <EOL> path = prefix + name <EOL> if path in inputs : <EOL> continue <EOL> record_constant = False <EOL> for pattern in includes : <EOL> if fnmatch ( path , pattern ) : <EOL> record_constant = True <EOL> if record_constant : <EOL> for pattern in excludes : <EOL> if fnmatch ( path , pattern ) : <EOL> record_constant = False <EOL> if record_constant : <EOL> val = getattr ( obj , name ) <EOL> if isinstance ( val , VariableTree ) : <EOL> for path , val in _expand_tree ( path , val ) : <EOL> constants [ path ] = val <EOL> else : <EOL> constants [ path ] = val <EOL> if self . parent is None : <EOL> for recorder in self . recorders : <EOL> recorder . record_constants ( constants ) <EOL> return ( inputs , constants ) <EOL> def record_configuration ( self ) : <EOL> """<STR_LIT>""" <EOL> top = self <EOL> while top . parent : <EOL> top = top . parent <EOL> top . _setup ( ) <EOL> self . configure_recording ( ) <EOL> for recorder in self . recorders : <EOL> recorder . close ( ) <EOL> @ rbac ( ( '<STR_LIT>' , '<STR_LIT:user>' ) ) <EOL> def connected_inputs ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return self . _depgraph . list_inputs ( name , connected = True ) <EOL> def stop ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _top_driver . stop ( ) <EOL> @ rbac ( ( '<STR_LIT>' , '<STR_LIT:user>' ) ) <EOL> def child_config_changed ( self , child , adding = True , removing = True ) : <EOL> """<STR_LIT>""" <EOL> self . config_changed ( ) <EOL> def list_connections ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ ( u . text , v . text ) for u , v in self . _connections ] <EOL> def check_gradient ( self , name = None , inputs = None , outputs = None , <EOL> stream = sys . stdout , mode = '<STR_LIT>' , <EOL> fd_form = '<STR_LIT>' , fd_step = <NUM_LIT> , <EOL> fd_step_type = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> driver = self . driver <EOL> obj = None <EOL> if inputs : <EOL> inputs = list ( inputs ) <EOL> if outputs : <EOL> outputs = list ( outputs ) <EOL> if inputs and outputs : <EOL> if name : <EOL> logger . warning ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , name ) <EOL> elif not name : <EOL> name = '<STR_LIT>' <EOL> if name : <EOL> obj = getattr ( self , name , None ) <EOL> if obj is None : <EOL> self . raise_exception ( "<STR_LIT>" % name ) <EOL> if has_interface ( obj , IDriver ) : <EOL> driver = obj <EOL> if not inputs : <EOL> if has_interface ( obj , IDriver ) : <EOL> pass <EOL> elif has_interface ( obj , IAssembly ) : <EOL> inputs = [ '<STR_LIT:.>' . join ( ( obj . name , inp ) ) <EOL> for inp in obj . list_inputs ( ) <EOL> if is_differentiable_var ( inp , obj ) ] <EOL> inputs = sorted ( inputs ) <EOL> elif has_interface ( obj , IComponent ) : <EOL> inputs = [ '<STR_LIT:.>' . join ( ( obj . name , inp ) ) <EOL> for inp in list_deriv_vars ( obj ) [ <NUM_LIT:0> ] ] <EOL> inputs = sorted ( inputs ) <EOL> else : <EOL> self . raise_exception ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if not outputs : <EOL> if has_interface ( obj , IDriver ) : <EOL> pass <EOL> elif has_interface ( obj , IAssembly ) : <EOL> outputs = [ '<STR_LIT:.>' . join ( ( obj . name , out ) ) <EOL> for out in obj . list_outputs ( ) <EOL> if is_differentiable_var ( out , obj ) ] <EOL> outputs = sorted ( outputs ) <EOL> elif has_interface ( obj , IComponent ) : <EOL> outputs = [ '<STR_LIT:.>' . join ( ( obj . name , outp ) ) <EOL> for outp in list_deriv_vars ( obj ) [ <NUM_LIT:1> ] ] <EOL> outputs = sorted ( outputs ) <EOL> else : <EOL> self . raise_exception ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if not has_interface ( obj , IDriver ) and ( not inputs or not outputs ) : <EOL> msg = '<STR_LIT>' % obj . name <EOL> self . raise_exception ( msg ) <EOL> base_fd_form = driver . gradient_options . fd_form <EOL> base_fd_step = driver . gradient_options . fd_step <EOL> base_fd_step_type = driver . gradient_options . fd_step_type <EOL> driver . gradient_options . fd_form = fd_form <EOL> driver . gradient_options . fd_step = fd_step <EOL> driver . gradient_options . fd_step_type = fd_step_type <EOL> try : <EOL> result = driver . check_gradient ( inputs = inputs , <EOL> outputs = outputs , <EOL> stream = stream , <EOL> mode = mode ) <EOL> finally : <EOL> driver . gradient_options . fd_form = base_fd_form <EOL> driver . gradient_options . fd_step = base_fd_step <EOL> driver . gradient_options . fd_step_type = base_fd_step_type <EOL> return result <EOL> @ rbac ( ( '<STR_LIT>' , '<STR_LIT:user>' ) ) <EOL> def new_pseudo_name ( self ) : <EOL> name = "<STR_LIT>" % self . _pseudo_count <EOL> self . _pseudo_count += <NUM_LIT:1> <EOL> return name <EOL> def get_graph ( self , components_only = False , format = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> if components_only : <EOL> graph = self . _depgraph . component_graph ( ) <EOL> else : <EOL> graph = _clean_graph ( self . _depgraph ) <EOL> if format . lower ( ) == '<STR_LIT>' : <EOL> graph_data = json_graph . node_link_data ( graph ) <EOL> return json . dumps ( graph_data ) <EOL> elif format . lower ( ) == '<STR_LIT>' : <EOL> agraph = nx . to_agraph ( graph ) <EOL> return agraph . draw ( format = '<STR_LIT>' , prog = '<STR_LIT>' ) <EOL> else : <EOL> self . raise_exception ( "<STR_LIT>" % format ) <EOL> def _repr_svg_ ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> import pygraphviz <EOL> return self . get_graph ( components_only = True , format = '<STR_LIT>' ) <EOL> except ImportError : <EOL> return '<STR_LIT>' <EOL> def list_components ( self ) : <EOL> '''<STR_LIT>''' <EOL> return [ name for name in self . list_containers ( ) <EOL> if isinstance ( self . get ( name ) , Component ) ] <EOL> def get_comps ( self ) : <EOL> """<STR_LIT>""" <EOL> return set ( [ c for _ , c in self . items ( ) <EOL> if has_interface ( c , IComponent ) ] ) <EOL> @ rbac ( ( '<STR_LIT>' , '<STR_LIT:user>' ) ) <EOL> def setup_systems ( self ) : <EOL> rgraph = self . _reduced_graph <EOL> for node , data in rgraph . nodes_iter ( data = True ) : <EOL> if '<STR_LIT>' in data : <EOL> data [ '<STR_LIT>' ] = _create_simple_sys ( self , rgraph , node ) <EOL> self . _top_driver . setup_systems ( ) <EOL> rgraph = rgraph . subgraph ( rgraph . nodes_iter ( ) ) <EOL> rgraph . collapse_subdrivers ( [ ] , [ self . _top_driver ] ) <EOL> drvname = self . _top_driver . name <EOL> if len ( rgraph ) > <NUM_LIT:1> : <EOL> self . _system = SerialSystem ( self , rgraph , rgraph . component_graph ( ) , <EOL> self . name + '<STR_LIT>' ) <EOL> for strong in strongly_connected_components ( rgraph ) : <EOL> if drvname in strong : <EOL> if len ( strong ) > <NUM_LIT:1> : <EOL> for p in rgraph . predecessors ( drvname ) : <EOL> if p in strong : <EOL> rgraph . remove_edge ( p , drvname ) <EOL> if is_directed_acyclic_graph ( rgraph ) : <EOL> break <EOL> break <EOL> self . _system . set_ordering ( nx . topological_sort ( rgraph ) , { } ) <EOL> else : <EOL> self . _system = rgraph . node [ self . _top_driver . name ] [ '<STR_LIT>' ] <EOL> for name in self . _unexecuted : <EOL> comp = getattr ( self , name ) <EOL> if has_interface ( comp , IDriver ) or has_interface ( comp , IAssembly ) : <EOL> comp . setup_systems ( ) <EOL> @ rbac ( ( '<STR_LIT>' , '<STR_LIT:user>' ) ) <EOL> def get_req_cpus ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _top_driver . get_req_cpus ( ) <EOL> def setup_communicators ( self , comm ) : <EOL> self . mpi . comm = comm <EOL> self . _system . setup_communicators ( comm ) <EOL> def setup_variables ( self ) : <EOL> self . _system . setup_variables ( ) <EOL> def setup_sizes ( self ) : <EOL> """<STR_LIT>""" <EOL> sys_stack = [ self . _system ] <EOL> loc_comps = [ ] <EOL> while sys_stack : <EOL> system = sys_stack . pop ( ) <EOL> loc_comps . extend ( [ s . name for s in system . simple_subsystems ( ) <EOL> if s . _comp is not None ] ) <EOL> sys_stack . extend ( system . local_subsystems ( ) ) <EOL> if isinstance ( system , OpaqueSystem ) : <EOL> sys_stack . append ( system . _inner_system ) <EOL> loc_comps = set ( loc_comps ) <EOL> loc_comps . add ( None ) <EOL> for node , data in self . _reduced_graph . nodes_iter ( data = True ) : <EOL> if '<STR_LIT>' not in data : <EOL> src = node [ <NUM_LIT:0> ] <EOL> scomp = src . split ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] if '<STR_LIT:.>' in src else None <EOL> dist_src = False <EOL> ssys = None <EOL> if scomp and scomp in self . _reduced_graph : <EOL> ssys = self . _reduced_graph . node [ scomp ] [ '<STR_LIT>' ] <EOL> sval , idx = get_val_and_index ( self , src ) <EOL> if isinstance ( sval , ndarray ) : <EOL> dests = node [ <NUM_LIT:1> ] <EOL> for dest in dests : <EOL> dcomp = dest . split ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] if '<STR_LIT:.>' in dest else None <EOL> if dcomp in loc_comps : <EOL> if dcomp : <EOL> dsys = self . _reduced_graph . node [ dcomp ] [ '<STR_LIT>' ] <EOL> if dsys . is_distrib_var ( node ) : <EOL> continue <EOL> elif ssys and ssys . is_distrib_var ( node ) : <EOL> continue <EOL> dval , didx = get_val_and_index ( self , dest ) <EOL> if isinstance ( dval , ndarray ) : <EOL> if sval . shape != dval . shape : <EOL> self . set ( dest , sval ) <EOL> self . _system . setup_sizes ( ) <EOL> def setup_vectors ( self , arrays = None ) : <EOL> """<STR_LIT>""" <EOL> self . _system . setup_vectors ( None ) <EOL> def setup_scatters ( self ) : <EOL> self . _system . setup_scatters ( ) <EOL> def _needs_pseudo ( self , srcexpr , destexpr ) : <EOL> """<STR_LIT>""" <EOL> srcrefs = list ( srcexpr . refs ( ) ) <EOL> if srcrefs and srcrefs [ <NUM_LIT:0> ] != srcexpr . text : <EOL> return '<STR_LIT>' <EOL> destmeta = destexpr . get_metadata ( '<STR_LIT>' ) <EOL> srcmeta = srcexpr . get_metadata ( '<STR_LIT>' ) <EOL> if srcmeta : <EOL> srcunit = srcmeta [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> if srcunit : <EOL> srcunit = PhysicalQuantity ( <NUM_LIT:1.> , srcunit ) . unit <EOL> else : <EOL> srcunit = None <EOL> if destmeta : <EOL> destunit = destmeta [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> if destunit : <EOL> destunit = PhysicalQuantity ( <NUM_LIT:1.> , destunit ) . unit <EOL> else : <EOL> destunit = None <EOL> if destunit and srcunit : <EOL> if destunit . powers != srcunit . powers or destunit . factor != srcunit . factor or destunit . offset != srcunit . offset : <EOL> return '<STR_LIT>' <EOL> return None <EOL> def setup_depgraph ( self , dgraph = None ) : <EOL> self . _depgraph = DependencyGraph ( ) <EOL> for name in self . list_inputs ( ) : <EOL> self . _depgraph . add_boundary_var ( self , name , <EOL> iotype = '<STR_LIT>' ) <EOL> for name in self . list_outputs ( ) : <EOL> self . _depgraph . add_boundary_var ( self , name , <EOL> iotype = '<STR_LIT>' ) <EOL> for cname in self . list_containers ( ) : <EOL> obj = getattr ( self , cname ) <EOL> if cname not in self . _depgraph and has_interface ( obj , IComponent ) : <EOL> self . _depgraph . add_component ( cname , obj ) <EOL> for srcexpr , destexpr in self . _connections : <EOL> src = srcexpr . text <EOL> dest = destexpr . text <EOL> try : <EOL> pcomp_type = self . _needs_pseudo ( srcexpr , destexpr ) <EOL> if pcomp_type : <EOL> if pcomp_type == '<STR_LIT>' : <EOL> pseudocomp = UnitConversionPComp ( self , srcexpr , destexpr , <EOL> pseudo_type = pcomp_type ) <EOL> else : <EOL> pseudocomp = PseudoComponent ( self , srcexpr , destexpr , <EOL> pseudo_type = pcomp_type ) <EOL> pseudocomp . activate ( self ) <EOL> else : <EOL> pseudocomp = None <EOL> self . _depgraph . check_connect ( src , dest ) <EOL> dcomps = destexpr . get_referenced_compnames ( ) <EOL> scomps = srcexpr . get_referenced_compnames ( ) <EOL> for dname in dcomps : <EOL> if dname in scomps : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> for cname in chain ( dcomps , scomps ) : <EOL> comp = getattr ( self , cname ) <EOL> if has_interface ( comp , IComponent ) : <EOL> comp . config_changed ( update_parent = False ) <EOL> for vname in chain ( srcexpr . get_referenced_varpaths ( copy = False ) , <EOL> destexpr . get_referenced_varpaths ( copy = False ) ) : <EOL> if not self . contains ( vname ) : <EOL> raise AttributeError ( "<STR_LIT>" % vname ) <EOL> self . _depgraph . connect ( self , src , dest ) <EOL> except : <EOL> self . reraise_exception ( "<STR_LIT>" % <EOL> ( src , dest ) , sys . exc_info ( ) ) <EOL> for cname in self . list_containers ( ) : <EOL> obj = getattr ( self , cname ) <EOL> if hasattr ( obj , '<STR_LIT>' ) : <EOL> for param in obj . get_parameters ( ) . values ( ) : <EOL> self . _depgraph . add_param ( obj . name , tuple ( param . targets ) ) <EOL> if hasattr ( obj , '<STR_LIT>' ) : <EOL> for constraint in obj . get_constraints ( ) . values ( ) : <EOL> constraint . activate ( obj ) <EOL> if hasattr ( obj , '<STR_LIT>' ) : <EOL> for constraint in obj . get_2sided_constraints ( ) . values ( ) : <EOL> constraint . activate ( obj ) <EOL> if hasattr ( obj , '<STR_LIT>' ) : <EOL> for objective in obj . get_objectives ( ) . values ( ) : <EOL> objective . activate ( obj ) <EOL> if hasattr ( obj , '<STR_LIT>' ) : <EOL> for res in obj . get_responses ( ) . values ( ) : <EOL> res . activate ( obj ) <EOL> for comp in self . get_comps ( ) : <EOL> if has_interface ( comp , IDriver ) or has_interface ( comp , IAssembly ) : <EOL> comp . setup_depgraph ( self . _depgraph ) <EOL> def setup_reduced_graph ( self , inputs = None , outputs = None , drvname = None ) : <EOL> """<STR_LIT>""" <EOL> dgraph = self . _depgraph . subgraph ( self . _depgraph . nodes_iter ( ) ) <EOL> keep = set ( [ n for n , d in dgraph . nodes_iter ( data = True ) <EOL> if d . get ( '<STR_LIT>' ) == '<STR_LIT:state>' ] ) <EOL> if self . parent is not None : <EOL> self . _derivs_required = self . parent . _derivs_required <EOL> else : <EOL> self . _derivs_required = False <EOL> if not ( inputs is None and outputs is None ) : <EOL> self . _derivs_required = True <EOL> dsrcs , ddests = self . _top_driver . get_expr_var_depends ( recurse = True ) <EOL> keep . add ( self . _top_driver . name ) <EOL> keep . update ( [ c . name for c in self . _top_driver . iteration_set ( ) ] ) <EOL> for u , v in dgraph . list_connections ( ) : <EOL> if is_boundary_node ( dgraph , u ) : <EOL> keep . add ( u ) <EOL> if is_boundary_node ( dgraph , v ) : <EOL> keep . add ( v ) <EOL> if inputs is None : <EOL> inputs = list ( ddests ) <EOL> else : <EOL> inputs = [ fix_single_tuple ( i ) for i in inputs ] <EOL> ins = [ ] <EOL> for inp in inputs : <EOL> if isinstance ( inp , basestring ) : <EOL> keep . add ( inp ) <EOL> ins . append ( inp ) <EOL> else : <EOL> keep . update ( inp ) <EOL> ins . append ( inp [ <NUM_LIT:0> ] ) <EOL> inputs = ins <EOL> if outputs is None : <EOL> outputs = dsrcs <EOL> dgraph = dgraph . _explode_vartrees ( self ) <EOL> skip = False <EOL> for inp in inputs : <EOL> if not hasattr ( self , inp . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] . split ( '<STR_LIT:[>' ) [ <NUM_LIT:0> ] ) : <EOL> skip = True <EOL> break <EOL> if inp not in dgraph : <EOL> base = base_var ( dgraph , inp ) <EOL> for n in chain ( dgraph . successors ( base ) , dgraph . predecessors ( base ) ) : <EOL> if base_var ( dgraph , n ) != base : <EOL> keep . add ( base ) <EOL> dgraph . add_connected_subvar ( inp ) <EOL> for out in outputs : <EOL> if not hasattr ( self , out . split ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] . split ( '<STR_LIT:[>' ) [ <NUM_LIT:0> ] ) : <EOL> skip = True <EOL> break <EOL> if out not in dgraph : <EOL> base = base_var ( dgraph , out ) <EOL> for n in chain ( dgraph . successors ( base ) , dgraph . predecessors ( base ) ) : <EOL> if base_var ( dgraph , n ) != base : <EOL> keep . add ( base ) <EOL> dgraph . add_connected_subvar ( out ) <EOL> if not skip : <EOL> dgraph = dgraph . relevant_subgraph ( inputs , outputs , keep ) <EOL> dgraph . _remove_vartrees ( self ) <EOL> keep . update ( inputs ) <EOL> keep . update ( outputs ) <EOL> dgraph . _fix_state_connections ( self ) <EOL> dgraph . _connect_subvars_to_comps ( ) <EOL> collapsed_graph = dgraph . collapse_connections ( ) <EOL> collapsed_graph . fix_duplicate_dests ( ) <EOL> collapsed_graph . _consolidate_srcs ( ) <EOL> collapsed_graph . vars2tuples ( dgraph ) <EOL> self . name2collapsed = collapsed_graph . map_collapsed_nodes ( ) <EOL> for node , data in collapsed_graph . nodes_iter ( data = True ) : <EOL> if '<STR_LIT>' in data and collapsed_graph . degree ( node ) > <NUM_LIT:0> : <EOL> if data . get ( '<STR_LIT>' ) == '<STR_LIT>' and collapsed_graph . in_degree ( node ) == <NUM_LIT:0> : <EOL> collapsed_graph . add_node ( node [ <NUM_LIT:0> ] . split ( '<STR_LIT:[>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] , comp = '<STR_LIT>' ) <EOL> collapsed_graph . add_edge ( node [ <NUM_LIT:0> ] . split ( '<STR_LIT:[>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] , node ) <EOL> elif data . get ( '<STR_LIT>' ) == '<STR_LIT>' : <EOL> collapsed_graph . add_node ( node [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] . split ( '<STR_LIT:[>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] , comp = '<STR_LIT>' ) <EOL> collapsed_graph . add_edge ( node , node [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] . split ( '<STR_LIT:[>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] ) <EOL> coll_keep = set ( [ self . name2collapsed . get ( k , k ) for k in keep ] ) <EOL> collapsed_graph . prune ( coll_keep ) <EOL> collapsed_graph . fix_dangling_vars ( self ) <EOL> if drvname : <EOL> for node in collapsed_graph : <EOL> if collapsed_graph . in_degree ( node ) > <NUM_LIT:1> and isinstance ( node , tuple ) : <EOL> for pred in collapsed_graph . predecessors ( node ) : <EOL> if pred != drvname : <EOL> collapsed_graph . remove_edge ( pred , node ) <EOL> if collapsed_graph . in_degree ( node ) == <NUM_LIT:1> : <EOL> break <EOL> self . _reduced_graph = collapsed_graph <EOL> for comp in self . get_comps ( ) : <EOL> if has_interface ( comp , IDriver ) and comp . requires_derivs ( ) : <EOL> self . _derivs_required = True <EOL> if has_interface ( comp , IAssembly ) : <EOL> comp . setup_reduced_graph ( inputs = _get_scoped_inputs ( comp , dgraph , inputs ) , <EOL> outputs = _get_scoped_outputs ( comp , dgraph , outputs ) ) <EOL> def compute_ordering ( self , graph ) : <EOL> """<STR_LIT>""" <EOL> compgraph = self . _depgraph . component_graph ( ) <EOL> self . _top_driver . compute_ordering ( compgraph ) <EOL> for comp in self . get_comps ( ) : <EOL> if has_interface ( comp , IAssembly ) : <EOL> comp . compute_ordering ( None ) <EOL> def compute_itersets ( self , graph ) : <EOL> """<STR_LIT>""" <EOL> compgraph = self . _depgraph . component_graph ( ) <EOL> self . driver . compute_itersets ( compgraph ) <EOL> self . _find_unexecuted_comps ( ) <EOL> for comp in self . get_comps ( ) : <EOL> if has_interface ( comp , IAssembly ) : <EOL> comp . compute_itersets ( None ) <EOL> def _get_var_info ( self , node ) : <EOL> """<STR_LIT>""" <EOL> info = { '<STR_LIT:size>' : <NUM_LIT:0> } <EOL> base = None <EOL> noflat = False <EOL> if isinstance ( node , tuple ) : <EOL> name = node [ <NUM_LIT:0> ] <EOL> for n in simple_node_iter ( node ) : <EOL> if self . get_metadata ( n . split ( '<STR_LIT:[>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] , '<STR_LIT>' ) : <EOL> noflat = True <EOL> break <EOL> else : <EOL> name = node <EOL> parts = name . split ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> if len ( parts ) > <NUM_LIT:1> : <EOL> cname , vname = parts <EOL> child = getattr ( self , cname ) <EOL> else : <EOL> cname , vname = '<STR_LIT>' , name <EOL> child = self <EOL> try : <EOL> val , idx = get_val_and_index ( child , vname ) <EOL> if isinstance ( val , real_types ) : <EOL> info [ '<STR_LIT>' ] = True <EOL> if hasattr ( val , '<STR_LIT>' ) : <EOL> info [ '<STR_LIT>' ] = val . shape <EOL> if '<STR_LIT:[>' in vname : <EOL> base = vname . split ( '<STR_LIT:[>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> flat_idx = to_idx_array ( get_flattened_index ( idx , <EOL> get_var_shape ( base , child ) , <EOL> cvt_to_slice = False ) ) <EOL> else : <EOL> base = None <EOL> flat_idx = None <EOL> info [ '<STR_LIT:size>' ] = flattened_size ( vname , val , scope = child ) <EOL> if flat_idx is not None : <EOL> info [ '<STR_LIT>' ] = flat_idx <EOL> except NoFlatError : <EOL> info [ '<STR_LIT>' ] = True <EOL> if base is not None : <EOL> if cname : <EOL> bname = '<STR_LIT:.>' . join ( ( cname , base ) ) <EOL> else : <EOL> bname = base <EOL> info [ '<STR_LIT>' ] = bname <EOL> meta = child . get_metadata ( vname ) <EOL> for mname in [ '<STR_LIT>' ] : <EOL> if meta . get ( mname ) is not None : <EOL> info [ mname ] = meta <EOL> if noflat : <EOL> info [ '<STR_LIT>' ] = True <EOL> return info <EOL> def _get_all_var_metadata ( self , graph ) : <EOL> """<STR_LIT>""" <EOL> varmeta = self . _var_meta <EOL> for node , data in graph . nodes_iter ( data = True ) : <EOL> if node not in varmeta and '<STR_LIT>' not in data : <EOL> meta = self . _get_var_info ( node ) <EOL> self . _update_varmeta ( node , meta ) <EOL> for node , data in graph . nodes_iter ( data = True ) : <EOL> if '<STR_LIT>' in data and '<STR_LIT:.>' not in node : <EOL> try : <EOL> comp = getattr ( self , node ) <EOL> ins , outs = comp . list_deriv_vars ( ) <EOL> except AttributeError : <EOL> continue <EOL> for name in chain ( ins , outs ) : <EOL> name = '<STR_LIT:.>' . join ( ( node , name ) ) <EOL> if name not in varmeta : <EOL> varmeta [ name ] = self . _get_var_info ( name ) <EOL> def _update_varmeta ( self , node , meta ) : <EOL> self . _var_meta [ node ] = meta <EOL> for name in simple_node_iter ( node ) : <EOL> self . _var_meta [ name ] = meta <EOL> def get_var_print_ranks ( self ) : <EOL> """<STR_LIT>""" <EOL> self . print_ranks = print_ranks = { } <EOL> if MPI is None or not self . _system . is_active ( ) : <EOL> return <EOL> ourvars = self . get_all_local_vars ( ) <EOL> allvars = self . mpi . comm . allgather ( ourvars ) <EOL> for rank in range ( self . mpi . comm . size - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ) : <EOL> for v in allvars [ rank ] : <EOL> if isinstance ( v , tuple ) : <EOL> print_ranks . setdefault ( v , [ ] ) . append ( rank ) <EOL> else : <EOL> print_ranks [ v ] = ( rank , ) <EOL> cnames = [ self . name2collapsed . get ( n , n ) for n in print_ranks ] <EOL> for cname in cnames : <EOL> if isinstance ( cname , tuple ) : <EOL> src = cname [ <NUM_LIT:0> ] <EOL> for dest in cname [ <NUM_LIT:1> ] : <EOL> print_ranks [ dest ] = print_ranks [ src ] <EOL> def get_all_local_vars ( self ) : <EOL> """<STR_LIT>""" <EOL> all_locs = set ( ) <EOL> all_locs . update ( self . list_inputs ( ) ) <EOL> all_locs . update ( self . list_outputs ( ) ) <EOL> for s in [ self . _system ] + list ( self . _system . local_subsystems ( recurse = True ) ) : <EOL> if isinstance ( s , SimpleSystem ) : <EOL> try : <EOL> obj = self . get ( s . name ) <EOL> except : <EOL> continue <EOL> if has_interface ( obj , IAssembly ) : <EOL> all_locs . update ( [ '<STR_LIT:.>' . join ( [ obj . name , n ] ) <EOL> for n in obj . get_all_local_vars ( ) ] ) <EOL> elif has_interface ( obj , IComponent ) : <EOL> lvars = [ '<STR_LIT:.>' . join ( [ obj . name , n ] ) <EOL> for n in obj . list_inputs ( ) + <EOL> obj . list_outputs ( ) ] <EOL> for i , v in enumerate ( lvars ) : <EOL> if v in s . distrib_idxs : <EOL> lvars [ i ] = ( v , ) <EOL> all_locs . update ( lvars ) <EOL> elif isinstance ( s , ParamSystem ) : <EOL> all_locs . add ( s . name ) <EOL> return all_locs <EOL> def setup_init ( self ) : <EOL> """<STR_LIT>""" <EOL> self . cpath_updated ( ) <EOL> self . _var_meta = { } <EOL> self . _pre_driver = None <EOL> self . _unexecuted = [ ] <EOL> self . _provideJ_bounds = None <EOL> self . _system = None <EOL> self . _derivs_required = False <EOL> for name in self . __dict__ . keys ( ) : <EOL> if name . startswith ( '<STR_LIT>' ) : <EOL> obj = getattr ( self , name ) <EOL> if isinstance ( obj , PseudoComponent ) : <EOL> self . remove ( name ) <EOL> for comp in self . get_comps ( ) : <EOL> comp . setup_init ( ) <EOL> def init_var_sizes ( self ) : <EOL> self . _top_driver . init_var_sizes ( ) <EOL> def post_setup ( self ) : <EOL> for comp in self . get_comps ( ) : <EOL> comp . post_setup ( ) <EOL> if self . _system . is_active ( ) : <EOL> self . _system . vec [ '<STR_LIT:u>' ] . set_from_scope ( self ) <EOL> def _setup ( self , inputs = None , outputs = None , drvname = None ) : <EOL> """<STR_LIT>""" <EOL> if self . parent : <EOL> return <EOL> if MPI : <EOL> MPI . COMM_WORLD . Set_errhandler ( MPI . ERRORS_ARE_FATAL ) <EOL> comm = MPI . COMM_WORLD <EOL> else : <EOL> comm = None <EOL> try : <EOL> self . setup_init ( ) <EOL> self . setup_depgraph ( ) <EOL> self . compute_itersets ( None ) <EOL> self . compute_ordering ( None ) <EOL> self . init_var_sizes ( ) <EOL> self . setup_reduced_graph ( inputs = inputs , outputs = outputs , <EOL> drvname = drvname ) <EOL> self . setup_systems ( ) <EOL> self . check_config ( ) <EOL> self . setup_communicators ( comm ) <EOL> self . collect_metadata ( ) <EOL> self . get_var_print_ranks ( ) <EOL> self . setup_variables ( ) <EOL> self . setup_sizes ( ) <EOL> self . setup_vectors ( ) <EOL> self . setup_scatters ( ) <EOL> except Exception as err : <EOL> exc = sys . exc_info ( ) <EOL> sys . stdout . flush ( ) <EOL> sys . stderr . flush ( ) <EOL> raise exc [ <NUM_LIT:0> ] , exc [ <NUM_LIT:1> ] , exc [ <NUM_LIT:2> ] <EOL> self . post_setup ( ) <EOL> def dump_iteration_tree ( obj , f = sys . stdout , full = True , tabsize = <NUM_LIT:4> , derivs = False ) : <EOL> """<STR_LIT>""" <EOL> def _dump_iteration_tree ( obj , f , tablevel ) : <EOL> tab = '<STR_LIT:U+0020>' * tablevel <EOL> if is_instance ( obj , Driver ) : <EOL> f . write ( "<STR_LIT>" % ( tab , obj . name ) ) <EOL> if derivs : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> names = set ( obj . workflow . _explicit_names ) <EOL> for cname in obj . _ordering : <EOL> comp = getattr ( obj . parent , cname ) <EOL> if not full and comp . name not in names : <EOL> continue <EOL> if is_instance ( comp , Driver ) or is_instance ( comp , Assembly ) : <EOL> _dump_iteration_tree ( comp , f , tablevel + tabsize ) <EOL> elif is_instance ( comp , PseudoComponent ) : <EOL> f . write ( "<STR_LIT>" % <EOL> ( '<STR_LIT:U+0020>' * ( tablevel + tabsize ) , comp . name , comp . _orig_expr ) ) <EOL> else : <EOL> f . write ( "<STR_LIT>" % ( '<STR_LIT:U+0020>' * ( tablevel + tabsize ) , comp . name ) ) <EOL> elif is_instance ( obj , Assembly ) : <EOL> f . write ( "<STR_LIT>" % ( tab , obj . name ) ) <EOL> _dump_iteration_tree ( obj . driver , f , tablevel + tabsize ) <EOL> _dump_iteration_tree ( obj , f , <NUM_LIT:0> ) <EOL> def _get_scoped_inputs ( comp , g , explicit_ins ) : <EOL> """<STR_LIT>""" <EOL> cnamedot = comp . name + '<STR_LIT:.>' <EOL> inputs = set ( ) <EOL> if explicit_ins is None : <EOL> explicit_ins = ( ) <EOL> for u , v in g . list_connections ( ) : <EOL> if v . startswith ( cnamedot ) : <EOL> inputs . add ( v ) <EOL> inputs . update ( [ n for n in explicit_ins if n . startswith ( cnamedot ) ] ) <EOL> if not inputs : <EOL> return None <EOL> return [ n . split ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ <NUM_LIT:1> ] for n in inputs ] <EOL> def _get_scoped_outputs ( comp , g , explicit_outs ) : <EOL> """<STR_LIT>""" <EOL> cnamedot = comp . name + '<STR_LIT:.>' <EOL> outputs = set ( ) <EOL> if explicit_outs is None : <EOL> explicit_outs = ( ) <EOL> for u , v in g . list_connections ( ) : <EOL> if u . startswith ( cnamedot ) : <EOL> outputs . add ( u ) <EOL> outputs . update ( [ n for n in explicit_outs if n . startswith ( cnamedot ) ] ) <EOL> if not outputs : <EOL> return None <EOL> return [ n . split ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ <NUM_LIT:1> ] for n in outputs ] <EOL> def _split_varpath ( cont , path ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> compname , varname = path . split ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> except ValueError : <EOL> return ( None , cont , path ) <EOL> t = cont . get_trait ( compname ) <EOL> if t and t . iotype : <EOL> return ( None , cont , path ) <EOL> return ( compname , getattr ( cont , compname ) , varname ) <EOL> def _expand_tree ( path , tree ) : <EOL> """<STR_LIT>""" <EOL> path += '<STR_LIT:.>' <EOL> result = [ ] <EOL> for name , val in tree . _items ( set ( ) ) : <EOL> if isinstance ( val , VariableTree ) : <EOL> result . extend ( _expand_tree ( path + name , val ) ) <EOL> else : <EOL> result . append ( ( path + name , val ) ) <EOL> return result </s>
