<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> import os <EOL> import sys <EOL> import json <EOL> import warnings <EOL> import traceback <EOL> from collections import OrderedDict <EOL> from itertools import chain <EOL> from six import iteritems , itervalues <EOL> from six . moves import cStringIO <EOL> import networkx as nx <EOL> import numpy as np <EOL> from openmdao . core . system import System <EOL> from openmdao . core . group import Group <EOL> from openmdao . core . component import Component <EOL> from openmdao . core . parallel_group import ParallelGroup <EOL> from openmdao . core . parallel_fd_group import ParallelFDGroup <EOL> from openmdao . core . basic_impl import BasicImpl <EOL> from openmdao . core . _checks import check_connections , _both_names <EOL> from openmdao . core . driver import Driver <EOL> from openmdao . core . mpi_wrap import MPI , under_mpirun , debug <EOL> from openmdao . core . relevance import Relevance <EOL> from openmdao . components . indep_var_comp import IndepVarComp <EOL> from openmdao . solvers . scipy_gmres import ScipyGMRES <EOL> from openmdao . solvers . ln_direct import DirectSolver <EOL> from openmdao . solvers . ln_gauss_seidel import LinearGaussSeidel <EOL> from openmdao . units . units import get_conversion_tuple <EOL> from openmdao . util . string_util import get_common_ancestor , nearest_child , name_relative_to <EOL> from openmdao . util . graph import plain_bfs <EOL> from openmdao . util . options import OptionsDictionary <EOL> force_check = os . environ . get ( '<STR_LIT>' ) <EOL> trace = os . environ . get ( '<STR_LIT>' ) <EOL> class _ProbData ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . top_lin_gs = False <EOL> self . in_complex_step = False <EOL> class Problem ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , root = None , driver = None , impl = None , comm = None ) : <EOL> super ( Problem , self ) . __init__ ( ) <EOL> self . root = root <EOL> self . _probdata = _ProbData ( ) <EOL> if MPI : <EOL> from openmdao . core . petsc_impl import PetscImpl <EOL> if impl != PetscImpl : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if impl is None : <EOL> self . _impl = BasicImpl <EOL> else : <EOL> self . _impl = impl <EOL> self . comm = comm <EOL> if driver is None : <EOL> self . driver = Driver ( ) <EOL> else : <EOL> self . driver = driver <EOL> self . pathname = '<STR_LIT>' <EOL> def __getitem__ ( self , name ) : <EOL> """<STR_LIT>""" <EOL> if name in self . root . unknowns : <EOL> return self . root . unknowns [ name ] <EOL> elif name in self . root . params : <EOL> return self . root . params [ name ] <EOL> elif name in self . root . _sysdata . to_abs_pnames : <EOL> for p in self . root . _sysdata . to_abs_pnames [ name ] : <EOL> return self . _rec_get_param ( p ) <EOL> elif name in self . _dangling : <EOL> for p in self . _dangling [ name ] : <EOL> return self . _rec_get_param ( p ) <EOL> else : <EOL> raise KeyError ( "<STR_LIT>" % name ) <EOL> def _rec_get_param ( self , absname ) : <EOL> parts = absname . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> if len ( parts ) == <NUM_LIT:1> : <EOL> return self . root . params [ absname ] <EOL> else : <EOL> grp = self . root . _subsystem ( parts [ <NUM_LIT:0> ] ) <EOL> return grp . params [ parts [ <NUM_LIT:1> ] ] <EOL> def __setitem__ ( self , name , val ) : <EOL> """<STR_LIT>""" <EOL> if name in self . root . unknowns : <EOL> self . root . unknowns [ name ] = val <EOL> elif name in self . _dangling : <EOL> for p in self . _dangling [ name ] : <EOL> parts = p . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> if len ( parts ) == <NUM_LIT:1> : <EOL> self . root . params [ p ] = val <EOL> else : <EOL> grp = self . root . _subsystem ( parts [ <NUM_LIT:0> ] ) <EOL> grp . params [ parts [ <NUM_LIT:1> ] ] = val <EOL> else : <EOL> raise KeyError ( "<STR_LIT>" % name ) <EOL> def _setup_connections ( self , params_dict , unknowns_dict ) : <EOL> """<STR_LIT>""" <EOL> to_prom_name = self . _probdata . to_prom_name <EOL> connections = self . root . _get_explicit_connections ( ) <EOL> prom_noconns = self . _add_implicit_connections ( connections ) <EOL> input_graph = nx . DiGraph ( ) <EOL> self . _dangling = { } <EOL> to_abs_pnames = self . root . _sysdata . to_abs_pnames <EOL> usrcs = set ( ) <EOL> for tgt , srcs in iteritems ( connections ) : <EOL> for src , idxs in srcs : <EOL> input_graph . add_edge ( src , tgt , idxs = idxs ) <EOL> if src in unknowns_dict : <EOL> usrcs . add ( src ) <EOL> for prom , plist in iteritems ( to_abs_pnames ) : <EOL> input_graph . add_nodes_from ( plist ) <EOL> if prom in prom_noconns : <EOL> start = plist [ <NUM_LIT:0> ] <EOL> input_graph . add_edges_from ( ( ( start , p ) for p in plist [ <NUM_LIT:1> : ] ) , <EOL> idxs = None ) <EOL> newconns = { } <EOL> for src in usrcs : <EOL> newconns [ src ] = None <EOL> src_idxs = { src : None } <EOL> for s , t in nx . dfs_edges ( input_graph , src ) : <EOL> tidxs = input_graph [ s ] [ t ] [ '<STR_LIT>' ] <EOL> sidxs = src_idxs [ s ] <EOL> if tidxs is None : <EOL> tidxs = sidxs <EOL> elif sidxs is not None : <EOL> tidxs = np . array ( sidxs ) [ tidxs ] <EOL> src_idxs [ t ] = tidxs <EOL> if t in newconns : <EOL> newconns [ t ] . append ( ( src , tidxs ) ) <EOL> else : <EOL> newconns [ t ] = [ ( src , tidxs ) ] <EOL> self . _input_inputs = { } <EOL> for node in input_graph . nodes_iter ( ) : <EOL> if node not in newconns and len ( input_graph . pred [ node ] ) == <NUM_LIT:0> : <EOL> nosrc = [ node ] <EOL> for s , t in nx . dfs_edges ( input_graph , node ) : <EOL> if t in newconns : <EOL> src = newconns [ t ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> for n in nosrc : <EOL> newconns [ n ] = [ ( src , None ) ] <EOL> break <EOL> else : <EOL> nosrc . append ( t ) <EOL> else : <EOL> set_nosrc = set ( nosrc ) <EOL> for n in nosrc : <EOL> self . _dangling [ to_prom_name [ n ] ] = set_nosrc <EOL> self . _input_inputs [ n ] = nosrc <EOL> connections = OrderedDict ( ) <EOL> for tgt , srcs in sorted ( newconns . items ( ) ) : <EOL> if srcs is not None : <EOL> if len ( srcs ) > <NUM_LIT:1> : <EOL> src_names = ( n for n , idx in srcs ) <EOL> self . _setup_errors . append ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( tgt , sorted ( src_names ) ) ) <EOL> connections [ tgt ] = srcs [ <NUM_LIT:0> ] <EOL> return connections <EOL> def _check_input_diffs ( self , connections , params_dict , unknowns_dict ) : <EOL> """<STR_LIT>""" <EOL> for tgt , connected_inputs in iteritems ( self . _input_inputs ) : <EOL> tgt_idx = connected_inputs . index ( tgt ) <EOL> units = [ params_dict [ n ] . get ( '<STR_LIT>' ) for n in connected_inputs ] <EOL> vals = [ params_dict [ n ] [ '<STR_LIT>' ] for n in connected_inputs ] <EOL> diff_units = [ ] <EOL> for i , u in enumerate ( units ) : <EOL> if i != tgt_idx and u != units [ tgt_idx ] : <EOL> if units [ tgt_idx ] is None : <EOL> sname , s = connected_inputs [ i ] , u <EOL> tname , t = connected_inputs [ tgt_idx ] , units [ tgt_idx ] <EOL> else : <EOL> sname , s = connected_inputs [ tgt_idx ] , units [ tgt_idx ] <EOL> tname , t = connected_inputs [ i ] , u <EOL> diff_units . append ( ( connected_inputs [ i ] , u ) ) <EOL> if isinstance ( vals [ tgt_idx ] , np . ndarray ) : <EOL> diff_vals = [ ( connected_inputs [ i ] , v ) for i , v in <EOL> enumerate ( vals ) if not <EOL> ( isinstance ( v , np . ndarray ) and <EOL> v . shape == vals [ tgt_idx ] . shape and <EOL> ( v == vals [ tgt_idx ] ) . all ( ) ) ] <EOL> else : <EOL> vtype = type ( vals [ tgt_idx ] ) <EOL> diff_vals = [ ( connected_inputs [ i ] , v ) for i , v in <EOL> enumerate ( vals ) if vtype != type ( v ) or <EOL> v != vals [ tgt_idx ] ] <EOL> if diff_units : <EOL> filt = set ( [ u for n , u in diff_units ] ) <EOL> if None in filt : <EOL> filt . remove ( None ) <EOL> if filt : <EOL> proms = set ( [ params_dict [ item ] [ '<STR_LIT>' ] for item in connected_inputs ] ) <EOL> if len ( proms ) == <NUM_LIT:1> : <EOL> msg = "<STR_LIT>" + "<STR_LIT>" % proms . pop ( ) <EOL> else : <EOL> msg = "<STR_LIT>" + "<STR_LIT>" <EOL> msg += "<STR_LIT>" % sorted ( [ ( tgt , params_dict [ tgt ] . get ( '<STR_LIT>' ) ) ] + diff_units ) <EOL> correct_src = params_dict [ connected_inputs [ <NUM_LIT:0> ] ] [ '<STR_LIT>' ] <EOL> msg += "<STR_LIT>" % correct_src + "<STR_LIT>" <EOL> self . _setup_errors . append ( msg ) <EOL> if diff_vals : <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( sorted ( [ ( tgt , params_dict [ tgt ] [ '<STR_LIT>' ] ) ] + <EOL> diff_vals ) ) ) <EOL> self . _setup_errors . append ( msg ) <EOL> for promname , absnames in iteritems ( self . root . _sysdata . to_abs_pnames ) : <EOL> if len ( absnames ) > <NUM_LIT:1> : <EOL> step_sizes , step_types , forms = { } , { } , { } <EOL> for name in absnames : <EOL> meta = self . root . _params_dict [ name ] <EOL> ss = meta . get ( '<STR_LIT>' ) <EOL> if ss is not None : <EOL> step_sizes [ ss ] = name <EOL> st = meta . get ( '<STR_LIT>' ) <EOL> if st is not None : <EOL> step_types [ st ] = name <EOL> f = meta . get ( '<STR_LIT>' ) <EOL> if f is not None : <EOL> forms [ f ] = name <EOL> if len ( step_sizes ) > <NUM_LIT:1> : <EOL> self . _setup_errors . append ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( promname , <EOL> sorted ( [ ( v , k ) for k , v in step_sizes . items ( ) ] ) ) ) <EOL> if len ( step_types ) > <NUM_LIT:1> : <EOL> self . _setup_errors . append ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( promname , <EOL> sorted ( [ ( v , k ) for k , v in step_types . items ( ) ] ) ) ) <EOL> if len ( forms ) > <NUM_LIT:1> : <EOL> self . _setup_errors . append ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( promname , <EOL> sorted ( [ ( v , k ) for k , v in forms . items ( ) ] ) ) ) <EOL> def _get_ubc_vars ( self , connections ) : <EOL> """<STR_LIT>""" <EOL> full_order = { s . pathname : i for i , s in <EOL> enumerate ( self . root . subsystems ( recurse = True ) ) } <EOL> ubcs = [ ] <EOL> for tgt , srcs in iteritems ( connections ) : <EOL> tsys = tgt . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> ssys = srcs [ <NUM_LIT:0> ] . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> if full_order [ ssys ] > full_order [ tsys ] : <EOL> ubcs . append ( tgt ) <EOL> return ubcs <EOL> def setup ( self , check = True , out_stream = sys . stdout ) : <EOL> """<STR_LIT>""" <EOL> self . _setup_errors = [ ] <EOL> tree_changed = False <EOL> meta_changed = False <EOL> self . _probdata = _ProbData ( ) <EOL> if isinstance ( self . root . ln_solver , LinearGaussSeidel ) : <EOL> self . _probdata . top_lin_gs = True <EOL> self . driver . root = self . root <EOL> self . root . _init_sys_data ( self . pathname , self . _probdata ) <EOL> self . _setup_communicators ( ) <EOL> params_dict , unknowns_dict = self . root . _setup_variables ( ) <EOL> self . _probdata . params_dict = params_dict <EOL> self . _probdata . unknowns_dict = unknowns_dict <EOL> self . _probdata . to_prom_name = self . root . _sysdata . to_prom_name <EOL> connections = self . _setup_connections ( params_dict , unknowns_dict ) <EOL> self . _probdata . connections = connections <EOL> for tgt , ( src , idxs ) in iteritems ( connections ) : <EOL> tmeta = params_dict [ tgt ] <EOL> if '<STR_LIT>' not in tmeta or not tmeta [ '<STR_LIT>' ] : <EOL> if tmeta [ '<STR_LIT>' ] == ( ) : <EOL> smeta = unknowns_dict [ src ] <EOL> if idxs is not None : <EOL> size = len ( idxs ) <EOL> tmeta [ '<STR_LIT>' ] = ( size , ) <EOL> tmeta [ '<STR_LIT:size>' ] = size <EOL> tmeta [ '<STR_LIT>' ] = smeta [ '<STR_LIT>' ] [ np . array ( idxs ) ] <EOL> else : <EOL> tmeta [ '<STR_LIT>' ] = smeta [ '<STR_LIT>' ] <EOL> tmeta [ '<STR_LIT:size>' ] = smeta [ '<STR_LIT:size>' ] <EOL> tmeta [ '<STR_LIT>' ] = smeta [ '<STR_LIT>' ] <EOL> if idxs is not None : <EOL> if isinstance ( idxs , np . ndarray ) : <EOL> tmeta [ '<STR_LIT>' ] = idxs <EOL> else : <EOL> tmeta [ '<STR_LIT>' ] = np . array ( idxs , <EOL> dtype = self . _impl . idx_arr_type ) <EOL> if MPI : <EOL> for s in self . root . components ( recurse = True ) : <EOL> if hasattr ( s , '<STR_LIT>' ) or ( <EOL> hasattr ( s , '<STR_LIT>' ) and ( s . setup_distrib <EOL> is not Component . setup_distrib ) ) : <EOL> meta_changed = True <EOL> if tree_changed : <EOL> return self . setup ( check = check , out_stream = out_stream ) <EOL> elif meta_changed : <EOL> params_dict , unknowns_dict = self . root . _setup_variables ( compute_indices = True ) <EOL> self . _setup_errors . extend ( check_connections ( connections , params_dict , <EOL> unknowns_dict , <EOL> self . root . _sysdata . to_prom_name ) ) <EOL> self . _setup_units ( connections , params_dict , unknowns_dict ) <EOL> to_prom_name = self . root . _sysdata . to_prom_name <EOL> self . _probdata . to_prom_name = to_prom_name <EOL> for path , meta in iteritems ( params_dict ) : <EOL> meta [ '<STR_LIT>' ] = to_prom_name [ path ] <EOL> if path not in connections : <EOL> if '<STR_LIT>' not in meta or not meta [ '<STR_LIT>' ] : <EOL> if meta [ '<STR_LIT>' ] == ( ) : <EOL> self . _setup_errors . append ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( path ) ) <EOL> for path , meta in iteritems ( unknowns_dict ) : <EOL> meta [ '<STR_LIT>' ] = to_prom_name [ path ] <EOL> param_owners = _assign_parameters ( connections ) <EOL> pois = self . driver . desvars_of_interest ( ) <EOL> oois = self . driver . outputs_of_interest ( ) <EOL> self . _driver_vois = set ( ) <EOL> for tup in chain ( pois , oois ) : <EOL> self . _driver_vois . update ( tup ) <EOL> promoted_unknowns = self . root . _sysdata . to_abs_uname <EOL> parallel_p = False <EOL> for vnames in pois : <EOL> if len ( vnames ) > <NUM_LIT:1> : <EOL> parallel_p = True <EOL> for v in vnames : <EOL> if v not in promoted_unknowns : <EOL> raise NameError ( "<STR_LIT>" % v ) <EOL> parallel_u = False <EOL> for vnames in oois : <EOL> if len ( vnames ) > <NUM_LIT:1> : <EOL> parallel_u = True <EOL> for v in vnames : <EOL> if v not in promoted_unknowns : <EOL> raise NameError ( "<STR_LIT>" % v ) <EOL> mode = self . _check_for_parallel_derivs ( pois , oois , parallel_u , parallel_p ) <EOL> self . _probdata . relevance = Relevance ( self . root , params_dict , <EOL> unknowns_dict , connections , <EOL> pois , oois , mode ) <EOL> for s in self . root . subgroups ( recurse = True , include_self = True ) : <EOL> if not s . _order_set : <EOL> order = None <EOL> broken_edges = None <EOL> if self . comm . rank == <NUM_LIT:0> : <EOL> order , broken_edges = s . list_auto_order ( ) <EOL> if MPI : <EOL> if trace : <EOL> debug ( "<STR_LIT>" ) <EOL> order , broken_edges = self . comm . bcast ( ( order , broken_edges ) , root = <NUM_LIT:0> ) <EOL> if trace : <EOL> debug ( "<STR_LIT>" ) <EOL> s . set_order ( order ) <EOL> for edge in broken_edges : <EOL> cname = edge [ <NUM_LIT:1> ] <EOL> head_sys = self . root <EOL> for name in cname . split ( '<STR_LIT:.>' ) : <EOL> head_sys = getattr ( head_sys , name ) <EOL> head_sys . _run_apply = True <EOL> self . _check_input_diffs ( connections , params_dict , unknowns_dict ) <EOL> alloc_derivs = not self . root . fd_options [ '<STR_LIT>' ] <EOL> for sub in self . root . subgroups ( recurse = True , include_self = True ) : <EOL> alloc_derivs = alloc_derivs or sub . nl_solver . supports [ '<STR_LIT>' ] <EOL> self . root . _setup_vectors ( param_owners , impl = self . _impl , alloc_derivs = alloc_derivs ) <EOL> self . driver . _setup ( ) <EOL> self . _poi_indices , self . _qoi_indices = self . driver . _map_voi_indices ( ) <EOL> for sub in self . root . subgroups ( recurse = True , include_self = True ) : <EOL> sub . nl_solver . setup ( sub ) <EOL> sub . ln_solver . setup ( sub ) <EOL> self . _check_solvers ( ) <EOL> self . _start_recorders ( ) <EOL> if self . _setup_errors : <EOL> stream = cStringIO ( ) <EOL> stream . write ( "<STR_LIT>" ) <EOL> for err in self . _setup_errors : <EOL> stream . write ( "<STR_LIT>" % err ) <EOL> raise RuntimeError ( stream . getvalue ( ) ) <EOL> OptionsDictionary . locked = True <EOL> if check or force_check : <EOL> return self . check_setup ( out_stream ) <EOL> return { } <EOL> def cleanup ( self ) : <EOL> """<STR_LIT>""" <EOL> self . driver . cleanup ( ) <EOL> self . root . cleanup ( ) <EOL> def _check_solvers ( self ) : <EOL> """<STR_LIT>""" <EOL> iterated_states = set ( ) <EOL> group_states = [ ] <EOL> has_iter_solver = { } <EOL> for group in self . root . subgroups ( recurse = True , include_self = True ) : <EOL> try : <EOL> has_iter_solver [ group . pathname ] = ( group . ln_solver . options [ '<STR_LIT>' ] > <NUM_LIT:1> ) <EOL> except KeyError : <EOL> if isinstance ( group . ln_solver , DirectSolver ) : <EOL> has_iter_solver [ group . pathname ] = ( True ) <EOL> opt = group . fd_options <EOL> if opt [ '<STR_LIT>' ] == True and opt [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> if group . name != '<STR_LIT>' : <EOL> msg = "<STR_LIT>" <EOL> msg += "<STR_LIT>" <EOL> self . _setup_errors . append ( msg ) <EOL> for sub in self . root . subgroups ( recurse = True , include_self = True ) : <EOL> if hasattr ( sub . nl_solver , '<STR_LIT>' ) : <EOL> msg = "<STR_LIT>" <EOL> msg += "<STR_LIT>" <EOL> self . _setup_errors . append ( msg . format ( sub . name ) ) <EOL> parts = group . pathname . split ( '<STR_LIT:.>' ) <EOL> for i in range ( len ( parts ) ) : <EOL> if has_iter_solver [ '<STR_LIT:.>' . join ( parts [ : i ] ) ] : <EOL> is_iterated_somewhere = True <EOL> break <EOL> else : <EOL> is_iterated_somewhere = False <EOL> if is_iterated_somewhere : <EOL> continue <EOL> if isinstance ( group . ln_solver , LinearGaussSeidel ) and group . ln_solver . options [ '<STR_LIT>' ] == <NUM_LIT:1> : <EOL> graph = group . _get_sys_graph ( ) <EOL> strong = [ sorted ( s ) for s in nx . strongly_connected_components ( graph ) <EOL> if len ( s ) > <NUM_LIT:1> ] <EOL> if strong : <EOL> self . _setup_errors . append ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( group . pathname , strong ) ) <EOL> states = [ n for n , m in iteritems ( group . _unknowns_dict ) if m . get ( '<STR_LIT:state>' ) ] <EOL> if states : <EOL> group_states . append ( ( group , states ) ) <EOL> if isinstance ( group . ln_solver , DirectSolver ) or group . ln_solver . options [ '<STR_LIT>' ] > <NUM_LIT:1> : <EOL> iterated_states . update ( states ) <EOL> else : <EOL> for s in states : <EOL> if s not in iterated_states : <EOL> cname = s . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> comp = self . root <EOL> for name in cname . split ( '<STR_LIT:.>' ) : <EOL> comp = getattr ( comp , name ) <EOL> if not _needs_iteration ( comp ) : <EOL> iterated_states . add ( s ) <EOL> for group , states in group_states : <EOL> uniterated_states = [ s for s in states if s not in iterated_states ] <EOL> if uniterated_states : <EOL> self . _setup_errors . append ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( group . pathname , uniterated_states ) ) <EOL> def _check_dangling_params ( self , out_stream = sys . stdout ) : <EOL> """<STR_LIT>""" <EOL> to_prom_name = self . root . _sysdata . to_prom_name <EOL> dangling_params = sorted ( set ( [ <EOL> to_prom_name [ p ] for p , m in iteritems ( self . root . _params_dict ) <EOL> if p not in self . root . connections <EOL> ] ) ) <EOL> if dangling_params : <EOL> print ( "<STR_LIT>" , <EOL> file = out_stream ) <EOL> for d in dangling_params : <EOL> print ( d , file = out_stream ) <EOL> return dangling_params <EOL> def _check_mode ( self , out_stream = sys . stdout ) : <EOL> """<STR_LIT>""" <EOL> if self . _calculated_mode != self . root . _probdata . relevance . mode : <EOL> print ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( self . root . _probdata . relevance . mode , <EOL> self . _calculated_mode , <EOL> self . _p_length , <EOL> self . _u_length ) , <EOL> file = out_stream ) <EOL> return ( self . root . _probdata . relevance . mode , self . _calculated_mode ) <EOL> def _check_no_unknown_comps ( self , out_stream = sys . stdout ) : <EOL> """<STR_LIT>""" <EOL> nocomps = sorted ( [ c . pathname for c in self . root . components ( recurse = True , <EOL> local = True ) <EOL> if len ( c . unknowns ) == <NUM_LIT:0> ] ) <EOL> if nocomps : <EOL> print ( "<STR_LIT>" , file = out_stream ) <EOL> for n in nocomps : <EOL> print ( n , file = out_stream ) <EOL> return nocomps <EOL> def _check_no_recorders ( self , out_stream = sys . stdout ) : <EOL> """<STR_LIT>""" <EOL> recorders = [ ] <EOL> recorders . extend ( self . driver . recorders ) <EOL> for grp in self . root . subgroups ( recurse = True , local = True , <EOL> include_self = True ) : <EOL> recorders . extend ( grp . nl_solver . recorders ) <EOL> recorders . extend ( grp . ln_solver . recorders ) <EOL> if not recorders : <EOL> print ( "<STR_LIT>" , <EOL> file = out_stream ) <EOL> return recorders <EOL> def _check_no_connect_comps ( self , out_stream = sys . stdout ) : <EOL> """<STR_LIT>""" <EOL> conn_comps = set ( [ t . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> for t in self . root . connections ] ) <EOL> conn_comps . update ( [ s . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> for s , i in itervalues ( self . root . connections ) ] ) <EOL> noconn_comps = sorted ( [ c . pathname <EOL> for c in self . root . components ( recurse = True , local = True ) <EOL> if c . pathname not in conn_comps ] ) <EOL> if noconn_comps : <EOL> print ( "<STR_LIT>" , file = out_stream ) <EOL> for comp in noconn_comps : <EOL> print ( comp , file = out_stream ) <EOL> return noconn_comps <EOL> def _check_mpi ( self , out_stream = sys . stdout ) : <EOL> """<STR_LIT>""" <EOL> if under_mpirun ( ) : <EOL> parr = True <EOL> if self . comm . rank == <NUM_LIT:0> : <EOL> for grp in self . root . subgroups ( recurse = True , include_self = True ) : <EOL> if ( isinstance ( grp , ParallelGroup ) or <EOL> isinstance ( grp , ParallelFDGroup ) ) : <EOL> break <EOL> else : <EOL> parr = False <EOL> print ( "<STR_LIT>" , <EOL> file = out_stream ) <EOL> mincpu , maxcpu = self . root . get_req_procs ( ) <EOL> if maxcpu is not None and self . comm . size > maxcpu : <EOL> print ( "<STR_LIT>" % <EOL> ( self . comm . size , maxcpu ) ) <EOL> return ( self . comm . size , maxcpu , parr ) <EOL> else : <EOL> pargrps = [ ] <EOL> for grp in self . root . subgroups ( recurse = True , include_self = True ) : <EOL> if isinstance ( grp , ParallelGroup ) : <EOL> print ( "<STR_LIT>" % <EOL> grp . pathname , file = out_stream ) <EOL> pargrps . append ( grp . pathname ) <EOL> return sorted ( pargrps ) <EOL> def _check_graph ( self , out_stream = sys . stdout ) : <EOL> """<STR_LIT>""" <EOL> cycles = [ ] <EOL> ooo = [ ] <EOL> for grp in self . root . subgroups ( recurse = True , include_self = True ) : <EOL> graph = grp . _get_sys_graph ( ) <EOL> strong = [ s for s in nx . strongly_connected_components ( graph ) <EOL> if len ( s ) > <NUM_LIT:1> ] <EOL> if strong : <EOL> relstrong = [ ] <EOL> for slist in strong : <EOL> relstrong . append ( [ ] ) <EOL> for s in slist : <EOL> relstrong [ - <NUM_LIT:1> ] . append ( nearest_child ( grp . pathname , s ) ) <EOL> subs = [ s for s in grp . _subsystems ] <EOL> tups = sorted ( [ ( subs . index ( s ) , s ) for s in relstrong [ - <NUM_LIT:1> ] ] ) <EOL> relstrong [ - <NUM_LIT:1> ] = [ t [ <NUM_LIT:1> ] for t in tups ] <EOL> print ( "<STR_LIT>" % <EOL> ( grp . pathname , relstrong ) , file = out_stream ) <EOL> cycles . append ( relstrong ) <EOL> graph , _ = grp . _break_cycles ( grp . list_order ( ) , graph ) <EOL> visited = set ( ) <EOL> out_of_order = { } <EOL> for sub in itervalues ( grp . _subsystems ) : <EOL> visited . add ( sub . pathname ) <EOL> for u , v in nx . dfs_edges ( graph , sub . pathname ) : <EOL> if v in visited : <EOL> out_of_order . setdefault ( nearest_child ( grp . pathname , v ) , <EOL> set ( ) ) . add ( sub . pathname ) <EOL> if out_of_order : <EOL> for name in out_of_order : <EOL> out_of_order [ name ] = sorted ( [ <EOL> nearest_child ( grp . pathname , n ) for n in out_of_order [ name ] <EOL> ] ) <EOL> print ( "<STR_LIT>" % <EOL> grp . pathname , file = out_stream ) <EOL> for n , subs in iteritems ( out_of_order ) : <EOL> print ( "<STR_LIT>" % ( n , subs ) , file = out_stream ) <EOL> ooo . append ( ( grp . pathname , list ( iteritems ( out_of_order ) ) ) ) <EOL> print ( "<STR_LIT>" % grp . list_auto_order ( ) [ <NUM_LIT:0> ] , <EOL> file = out_stream ) <EOL> return ( cycles , sorted ( ooo ) ) <EOL> def _check_gmres_under_mpi ( self , out_stream = sys . stdout ) : <EOL> """<STR_LIT>""" <EOL> if under_mpirun ( ) : <EOL> has_parallel = False <EOL> for s in self . root . subgroups ( recurse = True , include_self = True ) : <EOL> if isinstance ( s , ParallelGroup ) : <EOL> has_parallel = True <EOL> break <EOL> if has_parallel and isinstance ( self . root . ln_solver , ScipyGMRES ) : <EOL> print ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , file = out_stream ) <EOL> def _check_ubcs ( self , out_stream = sys . stdout ) : <EOL> ubcs = self . _get_ubc_vars ( self . root . connections ) <EOL> if ubcs : <EOL> print ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ubcs , file = out_stream ) <EOL> return ubcs <EOL> def _check_unmarked_pbos ( self , out_stream = sys . stdout ) : <EOL> pbos = [ ] <EOL> for comp in self . root . components ( recurse = True , include_self = True ) : <EOL> if comp . _pbo_warns : <EOL> pbos . append ( ( comp . pathname , comp . _pbo_warns ) ) <EOL> if pbos : <EOL> print ( "<STR_LIT>" <EOL> "<STR_LIT>" , file = out_stream ) <EOL> for cname , pbo_warns in sorted ( pbos , key = lambda x : x [ <NUM_LIT:0> ] ) : <EOL> for vname , val in pbo_warns : <EOL> print ( "<STR_LIT>" % ( '<STR_LIT:.>' . join ( ( cname , vname ) ) , <EOL> type ( val ) . __name__ ) , file = out_stream ) <EOL> return pbos <EOL> def _check_relevant_pbos ( self , out_stream = sys . stdout ) : <EOL> """<STR_LIT>""" <EOL> if self . driver . __class__ is Driver or self . driver . supports [ '<STR_LIT>' ] is False or self . root . fd_options [ '<STR_LIT>' ] : <EOL> return [ ] <EOL> vec = self . root . unknowns <EOL> pbos = [ var for var in vec if vec . metadata ( var ) . get ( '<STR_LIT>' ) ] <EOL> rels = set ( ) <EOL> for key , rel in iteritems ( self . _probdata . relevance . relevant ) : <EOL> rels . update ( rel ) <EOL> rel_pbos = rels . intersection ( pbos ) <EOL> if rel_pbos : <EOL> rel_conns = [ ] <EOL> for src in rel_pbos : <EOL> for tgt , src_tuple in iteritems ( self . root . connections ) : <EOL> if src_tuple [ <NUM_LIT:0> ] == src and tgt in rels : <EOL> rel_conns . append ( ( src , tgt ) ) <EOL> if rel_conns : <EOL> print ( "<STR_LIT>" , <EOL> file = out_stream ) <EOL> for src , tgt in rel_conns : <EOL> val = vec [ src ] <EOL> print ( "<STR_LIT>" % ( src , tgt , type ( val ) . __name__ ) , <EOL> file = out_stream ) <EOL> else : <EOL> print ( "<STR_LIT>" <EOL> "<STR_LIT>" , sorted ( rel_pbos ) ) <EOL> print ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> file = out_stream ) <EOL> return list ( rel_pbos ) <EOL> def check_setup ( self , out_stream = sys . stdout ) : <EOL> """<STR_LIT>""" <EOL> print ( "<STR_LIT>" , file = out_stream ) <EOL> print ( "<STR_LIT>" , file = out_stream ) <EOL> results = { } <EOL> results [ '<STR_LIT>' ] = self . _check_no_recorders ( out_stream ) <EOL> results [ '<STR_LIT>' ] = self . _check_mpi ( out_stream ) <EOL> results [ '<STR_LIT>' ] = self . _check_dangling_params ( out_stream ) <EOL> results [ '<STR_LIT>' ] = self . _check_mode ( out_stream ) <EOL> results [ '<STR_LIT>' ] = self . _check_no_unknown_comps ( out_stream ) <EOL> results [ '<STR_LIT>' ] = self . _check_no_connect_comps ( out_stream ) <EOL> results [ '<STR_LIT>' ] , results [ '<STR_LIT>' ] = self . _check_graph ( out_stream ) <EOL> results [ '<STR_LIT>' ] = self . _check_ubcs ( out_stream ) <EOL> results [ '<STR_LIT>' ] = self . _check_gmres_under_mpi ( out_stream ) <EOL> results [ '<STR_LIT>' ] = self . _check_unmarked_pbos ( out_stream ) <EOL> results [ '<STR_LIT>' ] = self . _check_relevant_pbos ( out_stream ) <EOL> for s in self . root . subsystems ( recurse = True , local = True , include_self = True ) : <EOL> stream = cStringIO ( ) <EOL> s . check_setup ( out_stream = stream ) <EOL> content = stream . getvalue ( ) <EOL> if content : <EOL> print ( "<STR_LIT>" % ( s . pathname , content ) , file = out_stream ) <EOL> results [ "<STR_LIT>" % s . pathname ] = content <EOL> print ( "<STR_LIT>" , file = out_stream ) <EOL> print ( "<STR_LIT>" , file = out_stream ) <EOL> return results <EOL> def pre_run_check ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . root . fd_options . locked : <EOL> msg = "<STR_LIT>" <EOL> raise RuntimeError ( msg ) <EOL> def run ( self ) : <EOL> """<STR_LIT>""" <EOL> self . pre_run_check ( ) <EOL> if self . root . is_active ( ) : <EOL> self . driver . run ( self ) <EOL> if MPI : <EOL> if trace : debug ( "<STR_LIT>" ) <EOL> self . root . comm . barrier ( ) <EOL> if trace : debug ( "<STR_LIT>" ) <EOL> def run_once ( self ) : <EOL> """<STR_LIT>""" <EOL> self . pre_run_check ( ) <EOL> root = self . root <EOL> driver = self . driver <EOL> if root . is_active ( ) : <EOL> driver . run_once ( self ) <EOL> with root . _dircontext : <EOL> root . apply_nonlinear ( root . params , root . unknowns , root . resids , <EOL> metadata = driver . metadata ) <EOL> if MPI : <EOL> if trace : debug ( "<STR_LIT>" ) <EOL> root . comm . barrier ( ) <EOL> if trace : debug ( "<STR_LIT>" ) <EOL> def _mode ( self , mode , indep_list , unknown_list ) : <EOL> """<STR_LIT>""" <EOL> self . _p_length = <NUM_LIT:0> <EOL> self . _u_length = <NUM_LIT:0> <EOL> uset = set ( ) <EOL> for unames in unknown_list : <EOL> if isinstance ( unames , tuple ) : <EOL> uset . update ( unames ) <EOL> else : <EOL> uset . add ( unames ) <EOL> pset = set ( ) <EOL> for pnames in indep_list : <EOL> if isinstance ( pnames , tuple ) : <EOL> pset . update ( pnames ) <EOL> else : <EOL> pset . add ( pnames ) <EOL> to_prom_name = self . root . _sysdata . to_prom_name <EOL> for path , meta in chain ( iteritems ( self . root . _unknowns_dict ) , <EOL> iteritems ( self . root . _params_dict ) ) : <EOL> prom_name = to_prom_name [ path ] <EOL> if prom_name in uset : <EOL> self . _u_length += meta [ '<STR_LIT:size>' ] <EOL> uset . remove ( prom_name ) <EOL> if prom_name in pset : <EOL> self . _p_length += meta [ '<STR_LIT:size>' ] <EOL> pset . remove ( prom_name ) <EOL> if uset : <EOL> raise RuntimeError ( "<STR_LIT>" % list ( uset ) ) <EOL> if pset : <EOL> raise RuntimeError ( "<STR_LIT>" % list ( pset ) ) <EOL> if self . _p_length > self . _u_length : <EOL> self . _calculated_mode = '<STR_LIT>' <EOL> else : <EOL> self . _calculated_mode = '<STR_LIT>' <EOL> if mode == '<STR_LIT>' : <EOL> mode = self . root . ln_solver . options [ '<STR_LIT>' ] <EOL> if mode == '<STR_LIT>' : <EOL> mode = self . _calculated_mode <EOL> return mode <EOL> def calc_gradient ( self , indep_list , unknown_list , mode = '<STR_LIT>' , <EOL> return_format = '<STR_LIT>' , dv_scale = None , cn_scale = None , <EOL> sparsity = None ) : <EOL> """<STR_LIT>""" <EOL> if mode not in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> msg = "<STR_LIT>" <EOL> raise ValueError ( msg ) <EOL> if return_format not in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> msg = "<STR_LIT>" <EOL> raise ValueError ( msg ) <EOL> with self . root . _dircontext : <EOL> if mode == '<STR_LIT>' or self . root . fd_options [ '<STR_LIT>' ] : <EOL> return self . _calc_gradient_fd ( indep_list , unknown_list , <EOL> return_format , dv_scale = dv_scale , <EOL> cn_scale = cn_scale , sparsity = sparsity ) <EOL> else : <EOL> return self . _calc_gradient_ln_solver ( indep_list , unknown_list , <EOL> return_format , mode , <EOL> dv_scale = dv_scale , <EOL> cn_scale = cn_scale , <EOL> sparsity = sparsity ) <EOL> def _calc_gradient_fd ( self , indep_list , unknown_list , return_format , <EOL> dv_scale = None , cn_scale = None , sparsity = None ) : <EOL> """<STR_LIT>""" <EOL> root = self . root <EOL> unknowns = root . unknowns <EOL> params = root . params <EOL> to_prom_name = root . _sysdata . to_prom_name <EOL> to_abs_pnames = root . _sysdata . to_abs_pnames <EOL> to_abs_uname = root . _sysdata . to_abs_uname <EOL> if dv_scale is None : <EOL> dv_scale = { } <EOL> if cn_scale is None : <EOL> cn_scale = { } <EOL> abs_params = [ ] <EOL> fd_unknowns = [ var for var in unknown_list if var not in indep_list ] <EOL> pass_unknowns = [ var for var in unknown_list if var in indep_list ] <EOL> for name in indep_list : <EOL> if name in unknowns : <EOL> name = to_abs_uname [ name ] <EOL> for tgt , ( src , idxs ) in iteritems ( root . connections ) : <EOL> if name == src : <EOL> name = tgt <EOL> break <EOL> abs_params . append ( name ) <EOL> Jfd = root . fd_jacobian ( params , unknowns , root . resids , total_derivs = True , <EOL> fd_params = abs_params , fd_unknowns = fd_unknowns , <EOL> pass_unknowns = pass_unknowns , <EOL> poi_indices = self . _poi_indices , <EOL> qoi_indices = self . _qoi_indices ) <EOL> def get_fd_ikey ( ikey ) : <EOL> if isinstance ( ikey , tuple ) : <EOL> ikey = ikey [ <NUM_LIT:0> ] <EOL> fd_ikey = ikey <EOL> if fd_ikey not in params : <EOL> for tgt , ( src , idxs ) in iteritems ( root . connections ) : <EOL> if src == ikey : <EOL> fd_ikey = tgt <EOL> break <EOL> if fd_ikey not in params : <EOL> for key , meta in iteritems ( params ) : <EOL> if to_prom_name [ key ] == fd_ikey : <EOL> fd_ikey = meta [ '<STR_LIT>' ] <EOL> break <EOL> return fd_ikey <EOL> if return_format == '<STR_LIT>' : <EOL> J = OrderedDict ( ) <EOL> for okey in unknown_list : <EOL> J [ okey ] = OrderedDict ( ) <EOL> for j , ikey in enumerate ( indep_list ) : <EOL> if sparsity is not None : <EOL> if ikey not in sparsity [ okey ] : <EOL> continue <EOL> abs_ikey = abs_params [ j ] <EOL> fd_ikey = get_fd_ikey ( abs_ikey ) <EOL> if ( okey , fd_ikey ) not in Jfd : <EOL> fd_ikey = to_abs_pnames [ fd_ikey ] [ <NUM_LIT:0> ] <EOL> J [ okey ] [ ikey ] = Jfd [ ( okey , fd_ikey ) ] <EOL> if ikey in dv_scale : <EOL> J [ okey ] [ ikey ] *= dv_scale [ ikey ] <EOL> if okey in cn_scale : <EOL> J [ okey ] [ ikey ] *= cn_scale [ okey ] <EOL> else : <EOL> usize = <NUM_LIT:0> <EOL> psize = <NUM_LIT:0> <EOL> for u in unknown_list : <EOL> if u in self . _qoi_indices : <EOL> idx = self . _qoi_indices [ u ] <EOL> usize += len ( idx ) <EOL> else : <EOL> usize += self . root . unknowns . metadata ( u ) [ '<STR_LIT:size>' ] <EOL> for p in indep_list : <EOL> if p in self . _poi_indices : <EOL> idx = self . _poi_indices [ p ] <EOL> psize += len ( idx ) <EOL> else : <EOL> psize += self . root . unknowns . metadata ( p ) [ '<STR_LIT:size>' ] <EOL> J = np . zeros ( ( usize , psize ) ) <EOL> ui = <NUM_LIT:0> <EOL> for u in unknown_list : <EOL> pi = <NUM_LIT:0> <EOL> for j , p in enumerate ( indep_list ) : <EOL> abs_ikey = abs_params [ j ] <EOL> fd_ikey = get_fd_ikey ( abs_ikey ) <EOL> if ( u , fd_ikey ) not in Jfd : <EOL> fd_ikey = to_abs_pnames [ fd_ikey ] [ <NUM_LIT:0> ] <EOL> pd = Jfd [ u , fd_ikey ] <EOL> rows , cols = pd . shape <EOL> for row in range ( <NUM_LIT:0> , rows ) : <EOL> for col in range ( <NUM_LIT:0> , cols ) : <EOL> J [ ui + row ] [ pi + col ] = pd [ row ] [ col ] <EOL> if p in dv_scale : <EOL> J [ ui + row ] [ pi + col ] *= dv_scale [ p ] <EOL> if u in cn_scale : <EOL> J [ ui + row ] [ pi + col ] *= cn_scale [ u ] <EOL> pi += cols <EOL> ui += rows <EOL> return J <EOL> def _calc_gradient_ln_solver ( self , indep_list , unknown_list , return_format , mode , <EOL> dv_scale = None , cn_scale = None , sparsity = None ) : <EOL> """<STR_LIT>""" <EOL> root = self . root <EOL> relevance = root . _probdata . relevance <EOL> unknowns = root . unknowns <EOL> unknowns_dict = root . _unknowns_dict <EOL> to_abs_uname = root . _sysdata . to_abs_uname <EOL> comm = root . comm <EOL> iproc = comm . rank <EOL> nproc = comm . size <EOL> owned = root . _owning_ranks <EOL> if dv_scale is None : <EOL> dv_scale = { } <EOL> if cn_scale is None : <EOL> cn_scale = { } <EOL> mode = self . _mode ( mode , indep_list , unknown_list ) <EOL> fwd = mode == '<STR_LIT>' <EOL> root . clear_dparams ( ) <EOL> for names in root . _probdata . relevance . vars_of_interest ( mode ) : <EOL> for name in names : <EOL> if name in root . dumat : <EOL> root . dumat [ name ] . vec [ : ] = <NUM_LIT:0.0> <EOL> root . drmat [ name ] . vec [ : ] = <NUM_LIT:0.0> <EOL> root . dumat [ None ] . vec [ : ] = <NUM_LIT:0.0> <EOL> root . drmat [ None ] . vec [ : ] = <NUM_LIT:0.0> <EOL> root . _sys_linearize ( root . params , unknowns , root . resids ) <EOL> if return_format == '<STR_LIT>' : <EOL> J = OrderedDict ( ) <EOL> for okeys in unknown_list : <EOL> if isinstance ( okeys , str ) : <EOL> okeys = ( okeys , ) <EOL> for okey in okeys : <EOL> J [ okey ] = OrderedDict ( ) <EOL> for ikeys in indep_list : <EOL> if isinstance ( ikeys , str ) : <EOL> ikeys = ( ikeys , ) <EOL> for ikey in ikeys : <EOL> if sparsity is not None : <EOL> if ikey not in sparsity [ okey ] : <EOL> continue <EOL> J [ okey ] [ ikey ] = None <EOL> else : <EOL> usize = <NUM_LIT:0> <EOL> psize = <NUM_LIT:0> <EOL> Jslices = OrderedDict ( ) <EOL> for u in unknown_list : <EOL> start = usize <EOL> if u in self . _qoi_indices : <EOL> idx = self . _qoi_indices [ u ] <EOL> usize += len ( idx ) <EOL> else : <EOL> usize += self . root . unknowns . metadata ( u ) [ '<STR_LIT:size>' ] <EOL> Jslices [ u ] = slice ( start , usize ) <EOL> for p in indep_list : <EOL> start = psize <EOL> if p in self . _poi_indices : <EOL> idx = self . _poi_indices [ p ] <EOL> psize += len ( idx ) <EOL> else : <EOL> psize += unknowns . metadata ( p ) [ '<STR_LIT:size>' ] <EOL> Jslices [ p ] = slice ( start , psize ) <EOL> J = np . zeros ( ( usize , psize ) ) <EOL> if fwd : <EOL> input_list , output_list = indep_list , unknown_list <EOL> poi_indices , qoi_indices = self . _poi_indices , self . _qoi_indices <EOL> in_scale , un_scale = dv_scale , cn_scale <EOL> else : <EOL> input_list , output_list = unknown_list , indep_list <EOL> qoi_indices , poi_indices = self . _poi_indices , self . _qoi_indices <EOL> in_scale , un_scale = cn_scale , dv_scale <EOL> all_vois = self . root . _probdata . relevance . vars_of_interest ( mode ) <EOL> input_set = set ( ) <EOL> for inp in input_list : <EOL> if isinstance ( inp , str ) : <EOL> input_set . add ( inp ) <EOL> else : <EOL> input_set . update ( inp ) <EOL> voi_sets = [ ] <EOL> for voi_set in all_vois : <EOL> for voi in voi_set : <EOL> if voi in input_set : <EOL> voi_sets . append ( voi_set ) <EOL> break <EOL> flat_voi = [ item for sublist in all_vois for item in sublist ] <EOL> for items in input_list : <EOL> if isinstance ( items , str ) : <EOL> items = ( items , ) <EOL> for item in items : <EOL> if item not in flat_voi : <EOL> voi_sets . append ( ( item , ) ) <EOL> voi_srcs = { } <EOL> for params in voi_sets : <EOL> rhs = OrderedDict ( ) <EOL> voi_idxs = { } <EOL> old_size = None <EOL> for voi in params : <EOL> vkey = self . _get_voi_key ( voi , params ) <EOL> duvec = self . root . dumat [ vkey ] <EOL> rhs [ vkey ] = np . empty ( ( len ( duvec . vec ) , ) ) <EOL> voi_srcs [ vkey ] = voi <EOL> if voi in duvec : <EOL> in_idxs = duvec . _get_local_idxs ( voi , poi_indices ) <EOL> else : <EOL> in_idxs = [ ] <EOL> if len ( in_idxs ) == <NUM_LIT:0> : <EOL> if voi in poi_indices : <EOL> in_idxs = duvec . to_idx_array ( poi_indices [ voi ] ) <EOL> else : <EOL> in_idxs = np . arange ( <NUM_LIT:0> , unknowns_dict [ to_abs_uname [ voi ] ] [ '<STR_LIT:size>' ] , dtype = int ) <EOL> if old_size is None : <EOL> old_size = len ( in_idxs ) <EOL> elif old_size != len ( in_idxs ) : <EOL> raise RuntimeError ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( params , old_size , len ( in_idxs ) ) ) <EOL> voi_idxs [ vkey ] = in_idxs <EOL> for i in range ( len ( in_idxs ) ) : <EOL> for voi in params : <EOL> vkey = self . _get_voi_key ( voi , params ) <EOL> rhs [ vkey ] [ : ] = <NUM_LIT:0.0> <EOL> if self . root . _owning_ranks [ voi_srcs [ vkey ] ] == iproc : <EOL> rhs [ vkey ] [ voi_idxs [ vkey ] [ i ] ] = - <NUM_LIT:1.0> <EOL> dx_mat = root . ln_solver . solve ( rhs , root , mode ) <EOL> for param , dx in iteritems ( dx_mat ) : <EOL> vkey = self . _get_voi_key ( param , params ) <EOL> if param is None : <EOL> param = params [ <NUM_LIT:0> ] <EOL> for item in output_list : <EOL> if sparsity is not None : <EOL> if fwd and param not in sparsity [ item ] : <EOL> continue <EOL> elif not fwd and item not in sparsity [ param ] : <EOL> continue <EOL> if relevance . is_relevant ( vkey , item ) : <EOL> if fwd or owned [ item ] == iproc : <EOL> out_idxs = self . root . dumat [ vkey ] . _get_local_idxs ( item , <EOL> qoi_indices , <EOL> get_slice = True ) <EOL> dxval = dx [ out_idxs ] <EOL> if dxval . size == <NUM_LIT:0> : <EOL> dxval = None <EOL> else : <EOL> dxval = None <EOL> if nproc > <NUM_LIT:1> : <EOL> if trace : <EOL> debug ( "<STR_LIT>" % <EOL> ( dxval , owned [ item ] , param , item ) ) <EOL> dxval = comm . bcast ( dxval , root = owned [ item ] ) <EOL> if trace : <EOL> debug ( "<STR_LIT>" ) <EOL> else : <EOL> if item in qoi_indices : <EOL> zsize = len ( qoi_indices [ item ] ) <EOL> else : <EOL> zsize = unknowns . metadata ( item ) [ '<STR_LIT:size>' ] <EOL> dxval = np . zeros ( zsize ) <EOL> if dxval is not None : <EOL> nk = len ( dxval ) <EOL> if return_format == '<STR_LIT>' : <EOL> if fwd : <EOL> if J [ item ] [ param ] is None : <EOL> J [ item ] [ param ] = np . zeros ( ( nk , len ( in_idxs ) ) ) <EOL> J [ item ] [ param ] [ : , i ] = dxval <EOL> if param in in_scale : <EOL> J [ item ] [ param ] [ : , i ] *= in_scale [ param ] <EOL> if item in un_scale : <EOL> J [ item ] [ param ] [ : , i ] *= un_scale [ item ] <EOL> else : <EOL> if J [ param ] [ item ] is None : <EOL> J [ param ] [ item ] = np . zeros ( ( len ( in_idxs ) , nk ) ) <EOL> J [ param ] [ item ] [ i , : ] = dxval <EOL> if param in in_scale : <EOL> J [ param ] [ item ] [ i , : ] *= in_scale [ param ] <EOL> if item in un_scale : <EOL> J [ param ] [ item ] [ i , : ] *= un_scale [ item ] <EOL> else : <EOL> if fwd : <EOL> J [ Jslices [ item ] , Jslices [ param ] . start + i ] = dxval <EOL> if param in in_scale : <EOL> J [ Jslices [ item ] , Jslices [ param ] . start + i ] *= in_scale [ param ] <EOL> if item in un_scale : <EOL> J [ Jslices [ item ] , Jslices [ param ] . start + i ] *= un_scale [ item ] <EOL> else : <EOL> J [ Jslices [ param ] . start + i , Jslices [ item ] ] = dxval <EOL> if param in in_scale : <EOL> J [ Jslices [ param ] . start + i , Jslices [ item ] ] *= in_scale [ param ] <EOL> if item in un_scale : <EOL> J [ Jslices [ param ] . start + i , Jslices [ item ] ] *= un_scale [ item ] <EOL> root . clear_dparams ( ) <EOL> return J <EOL> def _get_voi_key ( self , voi , grp ) : <EOL> """<STR_LIT>""" <EOL> if ( voi in self . _driver_vois and <EOL> isinstance ( self . root . ln_solver , LinearGaussSeidel ) ) : <EOL> if ( len ( grp ) > <NUM_LIT:1> or <EOL> self . root . ln_solver . options [ '<STR_LIT>' ] ) : <EOL> return voi <EOL> return None <EOL> def check_partial_derivatives ( self , out_stream = sys . stdout , comps = None , <EOL> compact_print = False ) : <EOL> """<STR_LIT>""" <EOL> root = self . root <EOL> if self . driver . iter_count < <NUM_LIT:1> : <EOL> out_stream . write ( '<STR_LIT>' ) <EOL> self . run_once ( ) <EOL> root . _sys_linearize ( root . params , root . unknowns , root . resids ) <EOL> if out_stream is not None : <EOL> out_stream . write ( '<STR_LIT>' ) <EOL> data = { } <EOL> voi = None <EOL> allcomps = root . components ( recurse = True ) <EOL> if comps is None : <EOL> comps = allcomps <EOL> else : <EOL> allcompnames = set ( [ c . pathname for c in allcomps ] ) <EOL> requested = set ( comps ) <EOL> diff = requested . difference ( allcompnames ) <EOL> if diff : <EOL> sorted_diff = list ( diff ) <EOL> sorted_diff . sort ( ) <EOL> msg = "<STR_LIT>" <EOL> msg += str ( sorted_diff ) <EOL> raise RuntimeError ( msg ) <EOL> comps = [ root . _subsystem ( c_name ) for c_name in comps ] <EOL> for comp in comps : <EOL> cname = comp . pathname <EOL> opt = comp . fd_options <EOL> fwd_rev = True <EOL> if opt [ '<STR_LIT>' ] : <EOL> f_d_2 = True <EOL> fd_desc = opt [ '<STR_LIT>' ] <EOL> fd_desc2 = opt [ '<STR_LIT>' ] <EOL> else : <EOL> f_d_2 = False <EOL> fd_desc = None <EOL> fd_desc2 = None <EOL> if opt [ '<STR_LIT>' ] : <EOL> if not f_d_2 : <EOL> continue <EOL> fwd_rev = False <EOL> if isinstance ( comp , IndepVarComp ) : <EOL> continue <EOL> data [ cname ] = { } <EOL> jac_fwd = OrderedDict ( ) <EOL> jac_rev = OrderedDict ( ) <EOL> jac_fd = OrderedDict ( ) <EOL> jac_fd2 = OrderedDict ( ) <EOL> params = comp . params <EOL> unknowns = comp . unknowns <EOL> resids = comp . resids <EOL> dparams = comp . dpmat [ voi ] <EOL> dunknowns = comp . dumat [ voi ] <EOL> dresids = comp . drmat [ voi ] <EOL> states = comp . states <EOL> if len ( dparams ) == <NUM_LIT:0> : <EOL> continue <EOL> param_list = [ item for item in dparams if not dparams . metadata ( item ) . get ( '<STR_LIT>' ) ] <EOL> param_list . extend ( states ) <EOL> unkn_list = [ item for item in dunknowns if not dunknowns . metadata ( item ) . get ( '<STR_LIT>' ) ] <EOL> if out_stream is not None : <EOL> out_stream . write ( '<STR_LIT:->' * ( len ( cname ) + <NUM_LIT:15> ) + '<STR_LIT:\n>' ) <EOL> out_stream . write ( "<STR_LIT>" % cname ) <EOL> out_stream . write ( '<STR_LIT:->' * ( len ( cname ) + <NUM_LIT:15> ) + '<STR_LIT:\n>' ) <EOL> for p_name in param_list : <EOL> if not fwd_rev : <EOL> break <EOL> dinputs = dunknowns if p_name in states else dparams <EOL> p_size = np . size ( dinputs [ p_name ] ) <EOL> for u_name in unkn_list : <EOL> u_size = np . size ( dunknowns [ u_name ] ) <EOL> if comp . _jacobian_cache : <EOL> if ( u_name , p_name ) in comp . _jacobian_cache : <EOL> user = comp . _jacobian_cache [ ( u_name , p_name ) ] . shape <EOL> if len ( user ) < <NUM_LIT:2> : <EOL> user = ( user [ <NUM_LIT:0> ] , <NUM_LIT:1> ) <EOL> if user [ <NUM_LIT:0> ] != u_size or user [ <NUM_LIT:1> ] != p_size : <EOL> msg = "<STR_LIT>" + "<STR_LIT>" <EOL> msg = msg . format ( cname , u_name , p_name , ( u_size , p_size ) , user ) <EOL> raise ValueError ( msg ) <EOL> jac_fwd [ ( u_name , p_name ) ] = np . zeros ( ( u_size , p_size ) ) <EOL> jac_rev [ ( u_name , p_name ) ] = np . zeros ( ( u_size , p_size ) ) <EOL> if fwd_rev : <EOL> for u_name in unkn_list : <EOL> u_size = np . size ( dunknowns [ u_name ] ) <EOL> for idx in range ( u_size ) : <EOL> dresids . vec [ : ] = <NUM_LIT:0.0> <EOL> root . clear_dparams ( ) <EOL> dunknowns . vec [ : ] = <NUM_LIT:0.0> <EOL> dresids . _dat [ u_name ] . val [ idx ] = <NUM_LIT:1.0> <EOL> try : <EOL> comp . apply_linear ( params , unknowns , dparams , <EOL> dunknowns , dresids , '<STR_LIT>' ) <EOL> finally : <EOL> dparams . _apply_unit_derivatives ( ) <EOL> for p_name in param_list : <EOL> dinputs = dunknowns if p_name in states else dparams <EOL> jac_rev [ ( u_name , p_name ) ] [ idx , : ] = dinputs . _dat [ p_name ] . val <EOL> if fwd_rev : <EOL> for p_name in param_list : <EOL> dinputs = dunknowns if p_name in states else dparams <EOL> p_size = np . size ( dinputs [ p_name ] ) <EOL> for idx in range ( p_size ) : <EOL> dresids . vec [ : ] = <NUM_LIT:0.0> <EOL> root . clear_dparams ( ) <EOL> dunknowns . vec [ : ] = <NUM_LIT:0.0> <EOL> dinputs . _dat [ p_name ] . val [ idx ] = <NUM_LIT:1.0> <EOL> dparams . _apply_unit_derivatives ( ) <EOL> comp . apply_linear ( params , unknowns , dparams , <EOL> dunknowns , dresids , '<STR_LIT>' ) <EOL> for u_name , u_val in dresids . vec_val_iter ( ) : <EOL> jac_fwd [ ( u_name , p_name ) ] [ : , idx ] = u_val <EOL> dresids . vec [ : ] = <NUM_LIT:0.0> <EOL> root . clear_dparams ( ) <EOL> dunknowns . vec [ : ] = <NUM_LIT:0.0> <EOL> if opt [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> fd_func = comp . complex_step_jacobian <EOL> else : <EOL> fd_func = comp . fd_jacobian <EOL> jac_fd = fd_func ( params , unknowns , resids ) <EOL> if f_d_2 : <EOL> dresids . vec [ : ] = <NUM_LIT:0.0> <EOL> root . clear_dparams ( ) <EOL> dunknowns . vec [ : ] = <NUM_LIT:0.0> <EOL> if opt [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> fd_func = comp . complex_step_jacobian <EOL> else : <EOL> fd_func = comp . fd_jacobian <EOL> save_form = opt [ '<STR_LIT>' ] <EOL> OptionsDictionary . locked = False <EOL> opt [ '<STR_LIT>' ] = opt [ '<STR_LIT>' ] <EOL> jac_fd2 = fd_func ( params , unknowns , resids ) <EOL> opt [ '<STR_LIT>' ] = save_form <EOL> OptionsDictionary . locked = True <EOL> _assemble_deriv_data ( chain ( dparams , states ) , resids , data [ cname ] , <EOL> jac_fwd , jac_rev , jac_fd , out_stream , <EOL> c_name = cname , jac_fd2 = jac_fd2 , fd_desc = fd_desc , <EOL> fd_desc2 = fd_desc2 , compact_print = compact_print ) <EOL> return data <EOL> def check_total_derivatives ( self , out_stream = sys . stdout ) : <EOL> """<STR_LIT>""" <EOL> root = self . root <EOL> driver = self . driver <EOL> if driver . iter_count < <NUM_LIT:1> : <EOL> out_stream . write ( '<STR_LIT>' ) <EOL> self . run_once ( ) <EOL> if out_stream is not None : <EOL> out_stream . write ( '<STR_LIT>' ) <EOL> if len ( driver . _desvars ) > <NUM_LIT:0> : <EOL> param_srcs = list ( driver . _desvars . keys ( ) ) <EOL> to_abs_name = root . _sysdata . to_abs_uname <EOL> indep_list = [ p for p in param_srcs if not root . _unknowns_dict [ to_abs_name [ p ] ] . get ( '<STR_LIT>' ) ] <EOL> else : <EOL> abs_indep_list = root . _get_fd_params ( ) <EOL> param_srcs = [ root . connections [ p ] for p in abs_indep_list if not root . _params_dict [ p ] . get ( '<STR_LIT>' ) ] <EOL> to_prom_name = self . root . _sysdata . to_prom_name <EOL> indep_list = [ <EOL> to_prom_name [ p ] for p , idxs in param_srcs <EOL> ] <EOL> if len ( driver . _objs ) > <NUM_LIT:0> or len ( driver . _cons ) > <NUM_LIT:0> : <EOL> unknown_list = list ( driver . _objs . keys ( ) ) <EOL> unknown_list . extend ( list ( driver . _cons . keys ( ) ) ) <EOL> unknown_list = [ item for item in unknown_list if not root . unknowns . metadata ( item ) . get ( '<STR_LIT>' ) ] <EOL> else : <EOL> unknown_list = root . _get_fd_unknowns ( ) <EOL> unknown_list = [ item for item in unknown_list if not root . unknowns . metadata ( item ) . get ( '<STR_LIT>' ) ] <EOL> if root . ln_solver . options . get ( '<STR_LIT>' ) : <EOL> mode = self . _mode ( '<STR_LIT>' , indep_list , unknown_list ) <EOL> if mode == '<STR_LIT>' : <EOL> fwd , rev = True , False <EOL> Jrev = None <EOL> if out_stream is not None : <EOL> out_stream . write ( '<STR_LIT>' ) <EOL> out_stream . write ( '<STR_LIT>' ) <EOL> else : <EOL> fwd , rev = False , True <EOL> Jfor = None <EOL> if out_stream is not None : <EOL> out_stream . write ( '<STR_LIT>' ) <EOL> out_stream . write ( '<STR_LIT>' ) <EOL> else : <EOL> fwd = rev = True <EOL> if fwd : <EOL> Jfor = self . calc_gradient ( indep_list , unknown_list , mode = '<STR_LIT>' , <EOL> return_format = '<STR_LIT>' ) <EOL> Jfor = _jac_to_flat_dict ( Jfor ) <EOL> if rev : <EOL> Jrev = self . calc_gradient ( indep_list , unknown_list , mode = '<STR_LIT>' , <EOL> return_format = '<STR_LIT>' ) <EOL> Jrev = _jac_to_flat_dict ( Jrev ) <EOL> Jfd = self . calc_gradient ( indep_list , unknown_list , mode = '<STR_LIT>' , <EOL> return_format = '<STR_LIT>' ) <EOL> Jfd = _jac_to_flat_dict ( Jfd ) <EOL> data = { } <EOL> _assemble_deriv_data ( indep_list , unknown_list , data , <EOL> Jfor , Jrev , Jfd , out_stream ) <EOL> return data <EOL> def _start_recorders ( self ) : <EOL> """<STR_LIT>""" <EOL> self . driver . recorders . startup ( self . root ) <EOL> self . driver . recorders . record_metadata ( self . root ) <EOL> for group in self . root . subgroups ( recurse = True , include_self = True ) : <EOL> for solver in ( group . nl_solver , group . ln_solver ) : <EOL> solver . recorders . startup ( group ) <EOL> solver . recorders . record_metadata ( self . root ) <EOL> def _check_for_parallel_derivs ( self , params , unknowns , par_u , par_p ) : <EOL> """<STR_LIT>""" <EOL> mode = self . _mode ( '<STR_LIT>' , params , unknowns ) <EOL> if mode == '<STR_LIT>' : <EOL> has_parallel_derivs = par_p <EOL> else : <EOL> has_parallel_derivs = par_u <EOL> if ( isinstance ( self . root . ln_solver , LinearGaussSeidel ) and <EOL> self . root . ln_solver . options [ '<STR_LIT>' ] ) and has_parallel_derivs : <EOL> for sub in self . root . subgroups ( recurse = True ) : <EOL> sub_mode = sub . ln_solver . options [ '<STR_LIT>' ] <EOL> if isinstance ( sub . ln_solver , LinearGaussSeidel ) and sub_mode not in ( mode , '<STR_LIT>' ) : <EOL> msg = "<STR_LIT>" "<STR_LIT>" <EOL> msg = msg . format ( name = sub . name , submode = sub_mode , rootmode = mode ) <EOL> self . _setup_errors . append ( msg ) <EOL> return mode <EOL> def _json_system_tree ( self ) : <EOL> """<STR_LIT>""" <EOL> def _tree_dict ( system ) : <EOL> dct = OrderedDict ( ) <EOL> for s in system . subsystems ( recurse = True ) : <EOL> if isinstance ( s , Group ) : <EOL> dct [ s . name ] = _tree_dict ( s ) <EOL> else : <EOL> dct [ s . name ] = OrderedDict ( ) <EOL> for vname , meta in iteritems ( s . unknowns ) : <EOL> dct [ s . name ] [ vname ] = m = meta . copy ( ) <EOL> for mname in m : <EOL> if isinstance ( m [ mname ] , np . ndarray ) : <EOL> m [ mname ] = m [ mname ] . tolist ( ) <EOL> return dct <EOL> tree = OrderedDict ( ) <EOL> tree [ '<STR_LIT:root>' ] = _tree_dict ( self . root ) <EOL> return json . dumps ( tree ) <EOL> def _setup_communicators ( self ) : <EOL> if self . comm is None : <EOL> self . comm = self . _impl . world_comm ( ) <EOL> minproc , maxproc = self . driver . get_req_procs ( ) <EOL> if MPI : <EOL> if not ( maxproc is None or maxproc >= self . comm . size ) : <EOL> raise RuntimeError ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( self . comm . size , minproc , maxproc ) ) <EOL> elif self . comm . size < minproc : <EOL> if maxproc is None : <EOL> maxproc = '<STR_LIT>' <EOL> raise RuntimeError ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( self . comm . size , minproc , maxproc ) ) <EOL> self . driver . _setup_communicators ( self . comm , os . getcwd ( ) ) <EOL> def _setup_units ( self , connections , params_dict , unknowns_dict ) : <EOL> """<STR_LIT>""" <EOL> to_prom_name = self . root . _sysdata . to_prom_name <EOL> for target , ( source , idxs ) in iteritems ( connections ) : <EOL> tmeta = params_dict [ target ] <EOL> smeta = unknowns_dict [ source ] <EOL> if '<STR_LIT>' not in tmeta or '<STR_LIT>' not in smeta : <EOL> continue <EOL> src_unit = smeta [ '<STR_LIT>' ] <EOL> tgt_unit = tmeta [ '<STR_LIT>' ] <EOL> try : <EOL> scale , offset = get_conversion_tuple ( src_unit , tgt_unit ) <EOL> except TypeError as err : <EOL> if str ( err ) == "<STR_LIT>" : <EOL> msg = "<STR_LIT>" "<STR_LIT>" "<STR_LIT>" . format ( src_unit , <EOL> _both_names ( smeta , to_prom_name ) , <EOL> tgt_unit , <EOL> _both_names ( tmeta , to_prom_name ) ) <EOL> self . _setup_errors . append ( msg ) <EOL> continue <EOL> else : <EOL> raise <EOL> if scale != <NUM_LIT:1.0> or offset != <NUM_LIT:0.0> : <EOL> tmeta [ '<STR_LIT>' ] = ( scale , offset ) <EOL> def _add_implicit_connections ( self , connections ) : <EOL> """<STR_LIT>""" <EOL> dangling = set ( ) <EOL> abs_unames = self . root . _sysdata . to_abs_uname <EOL> for prom_name , pabs_list in iteritems ( self . root . _sysdata . to_abs_pnames ) : <EOL> if prom_name in abs_unames : <EOL> for pabs in pabs_list : <EOL> connections . setdefault ( pabs , [ ] ) . append ( ( abs_unames [ prom_name ] , None ) ) <EOL> else : <EOL> dangling . add ( prom_name ) <EOL> return dangling <EOL> def print_all_convergence ( self ) : <EOL> """<STR_LIT>""" <EOL> root = self . root <EOL> root . ln_solver . print_all_convergence ( ) <EOL> root . nl_solver . print_all_convergence ( ) <EOL> for grp in root . subgroups ( recurse = True ) : <EOL> grp . ln_solver . print_all_convergence ( ) <EOL> grp . nl_solver . print_all_convergence ( ) <EOL> def _assign_parameters ( connections ) : <EOL> """<STR_LIT>""" <EOL> param_owners = { } <EOL> for par , ( unk , idxs ) in iteritems ( connections ) : <EOL> param_owners . setdefault ( get_common_ancestor ( par , unk ) , set ( ) ) . add ( par ) <EOL> return param_owners <EOL> def _jac_to_flat_dict ( jac ) : <EOL> """<STR_LIT>""" <EOL> new_jac = OrderedDict ( ) <EOL> for key1 , val1 in iteritems ( jac ) : <EOL> for key2 , val2 in iteritems ( val1 ) : <EOL> new_jac [ ( key1 , key2 ) ] = val2 <EOL> return new_jac <EOL> def _pad_name ( name , pad_num = <NUM_LIT> , quotes = True ) : <EOL> """<STR_LIT>""" <EOL> l_name = len ( name ) <EOL> if l_name < pad_num : <EOL> pad = pad_num - l_name <EOL> if quotes : <EOL> pad_str = "<STR_LIT>" <EOL> else : <EOL> pad_str = "<STR_LIT>" <EOL> pad_name = pad_str . format ( name = name , sep = '<STR_LIT>' , pad = pad ) <EOL> return pad_name <EOL> else : <EOL> return '<STR_LIT>' . format ( name ) <EOL> def _assemble_deriv_data ( params , resids , cdata , jac_fwd , jac_rev , jac_fd , <EOL> out_stream , c_name = '<STR_LIT:root>' , jac_fd2 = None , fd_desc = None , <EOL> fd_desc2 = None , compact_print = False ) : <EOL> """<STR_LIT>""" <EOL> started = False <EOL> for p_name in params : <EOL> for u_name in resids : <EOL> key = ( u_name , p_name ) <EOL> if key not in jac_fd : <EOL> continue <EOL> ldata = cdata [ key ] = { } <EOL> Jsub_fd = jac_fd [ key ] <EOL> ldata [ '<STR_LIT>' ] = Jsub_fd <EOL> magfd = np . linalg . norm ( Jsub_fd ) <EOL> if jac_fwd : <EOL> Jsub_for = jac_fwd [ key ] <EOL> ldata [ '<STR_LIT>' ] = Jsub_for <EOL> magfor = np . linalg . norm ( Jsub_for ) <EOL> else : <EOL> magfor = None <EOL> if jac_rev : <EOL> Jsub_rev = jac_rev [ key ] <EOL> ldata [ '<STR_LIT>' ] = Jsub_rev <EOL> magrev = np . linalg . norm ( Jsub_rev ) <EOL> else : <EOL> magrev = None <EOL> if jac_fd2 : <EOL> Jsub_fd2 = jac_fd2 [ key ] <EOL> ldata [ '<STR_LIT>' ] = Jsub_fd2 <EOL> magfd2 = np . linalg . norm ( Jsub_fd2 ) <EOL> else : <EOL> magfd2 = None <EOL> ldata [ '<STR_LIT>' ] = ( magfor , magrev , magfd ) <EOL> if jac_fwd : <EOL> abs1 = np . linalg . norm ( Jsub_for - Jsub_fd ) <EOL> else : <EOL> abs1 = None <EOL> if jac_rev : <EOL> abs2 = np . linalg . norm ( Jsub_rev - Jsub_fd ) <EOL> else : <EOL> abs2 = None <EOL> if jac_fwd and jac_rev : <EOL> abs3 = np . linalg . norm ( Jsub_for - Jsub_rev ) <EOL> else : <EOL> abs3 = None <EOL> if jac_fd2 : <EOL> abs4 = np . linalg . norm ( Jsub_fd2 - Jsub_fd ) <EOL> else : <EOL> abs4 = None <EOL> ldata [ '<STR_LIT>' ] = ( abs1 , abs2 , abs3 ) <EOL> if magfd == <NUM_LIT:0.0> : <EOL> rel1 = rel2 = rel3 = rel4 = float ( '<STR_LIT>' ) <EOL> else : <EOL> if jac_fwd : <EOL> rel1 = np . linalg . norm ( Jsub_for - Jsub_fd ) / magfd <EOL> else : <EOL> rel1 = None <EOL> if jac_rev : <EOL> rel2 = np . linalg . norm ( Jsub_rev - Jsub_fd ) / magfd <EOL> else : <EOL> rel2 = None <EOL> if jac_fwd and jac_rev : <EOL> rel3 = np . linalg . norm ( Jsub_for - Jsub_rev ) / magfd <EOL> else : <EOL> rel3 = None <EOL> if jac_fd2 : <EOL> rel4 = np . linalg . norm ( Jsub_fd2 - Jsub_fd ) / magfd <EOL> else : <EOL> rel4 = None <EOL> ldata [ '<STR_LIT>' ] = ( rel1 , rel2 , rel3 ) <EOL> if out_stream is None : <EOL> continue <EOL> if compact_print : <EOL> if jac_fwd and jac_rev : <EOL> if not started : <EOL> tmp1 = "<STR_LIT>" <EOL> out_str = tmp1 . format ( _pad_name ( '<STR_LIT>' ) , _pad_name ( '<STR_LIT>' ) , <EOL> _pad_name ( '<STR_LIT>' , <NUM_LIT:10> , quotes = False ) , <EOL> _pad_name ( '<STR_LIT>' , <NUM_LIT:10> , quotes = False ) , <EOL> _pad_name ( '<STR_LIT>' , <NUM_LIT:10> , quotes = False ) , <EOL> _pad_name ( '<STR_LIT>' , <NUM_LIT:10> , quotes = False ) , <EOL> _pad_name ( '<STR_LIT>' , <NUM_LIT:10> , quotes = False ) , <EOL> _pad_name ( '<STR_LIT>' , <NUM_LIT:10> , quotes = False ) , <EOL> _pad_name ( '<STR_LIT>' , <NUM_LIT:10> , quotes = False ) <EOL> ) <EOL> out_stream . write ( out_str ) <EOL> out_stream . write ( '<STR_LIT:->' * len ( out_str ) + '<STR_LIT:\n>' ) <EOL> started = True <EOL> tmp1 = "<STR_LIT>" <EOL> out_stream . write ( tmp1 . format ( _pad_name ( u_name ) , _pad_name ( p_name ) , <EOL> magfor , magrev , magfd , abs1 , abs2 , <EOL> rel1 , rel2 ) ) <EOL> elif jac_fd and jac_fd2 : <EOL> if not started : <EOL> tmp1 = "<STR_LIT>" <EOL> out_str = tmp1 . format ( _pad_name ( '<STR_LIT>' ) , _pad_name ( '<STR_LIT>' ) , <EOL> _pad_name ( '<STR_LIT>' , <NUM_LIT> , quotes = False ) , <EOL> _pad_name ( '<STR_LIT>' , <NUM_LIT:12> , quotes = False ) , <EOL> _pad_name ( '<STR_LIT>' , <NUM_LIT:12> , quotes = False ) , <EOL> _pad_name ( '<STR_LIT>' , <NUM_LIT:12> , quotes = False ) <EOL> ) <EOL> out_stream . write ( out_str ) <EOL> out_stream . write ( '<STR_LIT:->' * len ( out_str ) + '<STR_LIT:\n>' ) <EOL> started = True <EOL> tmp1 = "<STR_LIT>" <EOL> out_stream . write ( tmp1 . format ( _pad_name ( u_name ) , _pad_name ( p_name ) , <EOL> magfd , magfd2 , abs4 , rel4 ) ) <EOL> else : <EOL> if started : <EOL> out_stream . write ( '<STR_LIT>' * <NUM_LIT:30> + '<STR_LIT:\n>' ) <EOL> else : <EOL> started = True <EOL> out_stream . write ( "<STR_LIT>" % ( c_name , u_name , p_name ) ) <EOL> if jac_fwd : <EOL> out_stream . write ( '<STR_LIT>' % magfor ) <EOL> if jac_rev : <EOL> out_stream . write ( '<STR_LIT>' % magrev ) <EOL> if not jac_fwd and not jac_rev : <EOL> out_stream . write ( '<STR_LIT>' ) <EOL> if jac_fd : <EOL> out_stream . write ( '<STR_LIT>' % magfd ) <EOL> if fd_desc : <EOL> out_stream . write ( '<STR_LIT>' % fd_desc ) <EOL> out_stream . write ( '<STR_LIT:\n>' ) <EOL> if jac_fd2 : <EOL> out_stream . write ( '<STR_LIT>' % magfd2 ) <EOL> if fd_desc2 : <EOL> out_stream . write ( '<STR_LIT>' % fd_desc2 ) <EOL> out_stream . write ( '<STR_LIT:\n>' ) <EOL> out_stream . write ( '<STR_LIT:\n>' ) <EOL> if jac_fwd : <EOL> out_stream . write ( '<STR_LIT>' % abs1 ) <EOL> if jac_rev : <EOL> out_stream . write ( '<STR_LIT>' % abs2 ) <EOL> if jac_fwd and jac_rev : <EOL> out_stream . write ( '<STR_LIT>' % abs3 ) <EOL> if jac_fd2 : <EOL> out_stream . write ( '<STR_LIT>' % abs4 ) <EOL> out_stream . write ( '<STR_LIT:\n>' ) <EOL> if jac_fwd : <EOL> out_stream . write ( '<STR_LIT>' % rel1 ) <EOL> if jac_rev : <EOL> out_stream . write ( '<STR_LIT>' % rel2 ) <EOL> if jac_fwd and jac_rev : <EOL> out_stream . write ( '<STR_LIT>' % rel3 ) <EOL> if jac_fd2 : <EOL> out_stream . write ( '<STR_LIT>' % rel4 ) <EOL> out_stream . write ( '<STR_LIT:\n>' ) <EOL> if jac_fwd : <EOL> out_stream . write ( '<STR_LIT>' ) <EOL> out_stream . write ( str ( Jsub_for ) ) <EOL> out_stream . write ( '<STR_LIT>' ) <EOL> if jac_rev : <EOL> out_stream . write ( '<STR_LIT>' ) <EOL> out_stream . write ( str ( Jsub_rev ) ) <EOL> out_stream . write ( '<STR_LIT>' ) <EOL> out_stream . write ( '<STR_LIT>' ) <EOL> out_stream . write ( str ( Jsub_fd ) ) <EOL> out_stream . write ( '<STR_LIT>' ) <EOL> if jac_fd2 : <EOL> out_stream . write ( '<STR_LIT>' ) <EOL> out_stream . write ( str ( Jsub_fd2 ) ) <EOL> out_stream . write ( '<STR_LIT>' ) <EOL> def _needs_iteration ( comp ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( comp , Component ) and comp . is_active ( ) and comp . states : <EOL> for klass in comp . __class__ . __mro__ : <EOL> if klass is Component : <EOL> break <EOL> if '<STR_LIT>' in klass . __dict__ : <EOL> return False <EOL> return True <EOL> return False <EOL> def _get_gmres_name ( ) : <EOL> if MPI : <EOL> return '<STR_LIT>' <EOL> else : <EOL> return '<STR_LIT>' </s>
