<s> """<STR_LIT>""" <EOL> import numpy as np <EOL> from openmdao . api import Component , Group , IndepVarComp , ExecComp , Newton , ScipyGMRES <EOL> class SellarDis1 ( Component ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> super ( SellarDis1 , self ) . __init__ ( ) <EOL> self . add_param ( '<STR_LIT:z>' , val = np . zeros ( <NUM_LIT:2> ) ) <EOL> self . add_param ( '<STR_LIT:x>' , val = <NUM_LIT:0.> ) <EOL> self . add_param ( '<STR_LIT>' , val = <NUM_LIT:0.> ) <EOL> self . add_output ( '<STR_LIT>' , val = <NUM_LIT:1.0> ) <EOL> def solve_nonlinear ( self , params , unknowns , resids ) : <EOL> """<STR_LIT>""" <EOL> z1 = params [ '<STR_LIT:z>' ] [ <NUM_LIT:0> ] <EOL> z2 = params [ '<STR_LIT:z>' ] [ <NUM_LIT:1> ] <EOL> x1 = params [ '<STR_LIT:x>' ] <EOL> y2 = params [ '<STR_LIT>' ] <EOL> unknowns [ '<STR_LIT>' ] = z1 ** <NUM_LIT:2> + z2 + x1 - <NUM_LIT> * y2 <EOL> def linearize ( self , params , unknowns , resids ) : <EOL> """<STR_LIT>""" <EOL> J = { } <EOL> J [ '<STR_LIT>' , '<STR_LIT>' ] = - <NUM_LIT> <EOL> J [ '<STR_LIT>' , '<STR_LIT:z>' ] = np . array ( [ [ <NUM_LIT:2> * params [ '<STR_LIT:z>' ] [ <NUM_LIT:0> ] , <NUM_LIT:1.0> ] ] ) <EOL> J [ '<STR_LIT>' , '<STR_LIT:x>' ] = <NUM_LIT:1.0> <EOL> return J <EOL> class SellarDis2 ( Component ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> super ( SellarDis2 , self ) . __init__ ( ) <EOL> self . add_param ( '<STR_LIT:z>' , val = np . zeros ( <NUM_LIT:2> ) ) <EOL> self . add_param ( '<STR_LIT>' , val = <NUM_LIT:0.> ) <EOL> self . add_output ( '<STR_LIT>' , val = <NUM_LIT:1.0> ) <EOL> def solve_nonlinear ( self , params , unknowns , resids ) : <EOL> """<STR_LIT>""" <EOL> z1 = params [ '<STR_LIT:z>' ] [ <NUM_LIT:0> ] <EOL> z2 = params [ '<STR_LIT:z>' ] [ <NUM_LIT:1> ] <EOL> y1 = params [ '<STR_LIT>' ] <EOL> y1 = abs ( y1 ) <EOL> unknowns [ '<STR_LIT>' ] = y1 ** <NUM_LIT> + z1 + z2 <EOL> def linearize ( self , params , unknowns , resids ) : <EOL> """<STR_LIT>""" <EOL> J = { } <EOL> J [ '<STR_LIT>' , '<STR_LIT>' ] = <NUM_LIT> * params [ '<STR_LIT>' ] ** - <NUM_LIT> <EOL> J [ '<STR_LIT>' , '<STR_LIT:z>' ] = np . array ( [ [ <NUM_LIT:1.0> , <NUM_LIT:1.0> ] ] ) <EOL> return J <EOL> class StateConnection ( Component ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> super ( StateConnection , self ) . __init__ ( ) <EOL> self . add_param ( '<STR_LIT>' , <NUM_LIT:1.0> ) <EOL> self . add_state ( '<STR_LIT>' , val = <NUM_LIT:1.0> ) <EOL> def apply_nonlinear ( self , params , unknowns , resids ) : <EOL> """<STR_LIT>""" <EOL> y2_actual = params [ '<STR_LIT>' ] <EOL> y2_command = unknowns [ '<STR_LIT>' ] <EOL> resids [ '<STR_LIT>' ] = y2_actual - y2_command <EOL> def solve_nonlinear ( self , params , unknowns , resids ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def linearize ( self , params , unknowns , resids ) : <EOL> """<STR_LIT>""" <EOL> J = { } <EOL> J [ ( '<STR_LIT>' , '<STR_LIT>' ) ] = - <NUM_LIT:1.0> <EOL> J [ ( '<STR_LIT>' , '<STR_LIT>' ) ] = <NUM_LIT:1.0> <EOL> return J <EOL> class SellarStateConnection ( Group ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> super ( SellarStateConnection , self ) . __init__ ( ) <EOL> self . add ( '<STR_LIT>' , IndepVarComp ( '<STR_LIT:x>' , <NUM_LIT:1.0> ) , promotes = [ '<STR_LIT:x>' ] ) <EOL> self . add ( '<STR_LIT>' , IndepVarComp ( '<STR_LIT:z>' , np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) , promotes = [ '<STR_LIT:z>' ] ) <EOL> self . add ( '<STR_LIT>' , StateConnection ( ) ) <EOL> self . add ( '<STR_LIT>' , SellarDis1 ( ) , promotes = [ '<STR_LIT:x>' , '<STR_LIT:z>' , '<STR_LIT>' ] ) <EOL> self . add ( '<STR_LIT>' , SellarDis2 ( ) , promotes = [ '<STR_LIT:z>' , '<STR_LIT>' ] ) <EOL> self . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . add ( '<STR_LIT>' , ExecComp ( '<STR_LIT>' , <EOL> z = np . array ( [ <NUM_LIT:0.0> , <NUM_LIT:0.0> ] ) ) , <EOL> promotes = [ '<STR_LIT:x>' , '<STR_LIT:z>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . add ( '<STR_LIT>' , ExecComp ( '<STR_LIT>' ) , promotes = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . add ( '<STR_LIT>' , ExecComp ( '<STR_LIT>' ) , promotes = [ '<STR_LIT>' ] ) <EOL> self . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . nl_solver = Newton ( ) <EOL> self . ln_solver = ScipyGMRES ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> from openmdao . core . problem import Problem <EOL> from openmdao . drivers . scipy_optimizer import ScipyOptimizer <EOL> top = Problem ( ) <EOL> top . root = SellarStateConnection ( ) <EOL> top . driver = ScipyOptimizer ( ) <EOL> top . driver . options [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> top . driver . options [ '<STR_LIT>' ] = <NUM_LIT> <EOL> top . driver . add_desvar ( '<STR_LIT:z>' , lower = np . array ( [ - <NUM_LIT> , <NUM_LIT:0.0> ] ) , <EOL> upper = np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> top . driver . add_desvar ( '<STR_LIT:x>' , lower = <NUM_LIT:0.0> , upper = <NUM_LIT> ) <EOL> top . driver . add_objective ( '<STR_LIT>' ) <EOL> top . driver . add_constraint ( '<STR_LIT>' , upper = <NUM_LIT:0.0> ) <EOL> top . driver . add_constraint ( '<STR_LIT>' , upper = <NUM_LIT:0.0> ) <EOL> top . setup ( ) <EOL> top . run ( ) <EOL> print ( "<STR_LIT:\n>" ) <EOL> print ( "<STR_LIT>" % ( top [ '<STR_LIT:z>' ] [ <NUM_LIT:0> ] , <EOL> top [ '<STR_LIT:z>' ] [ <NUM_LIT:1> ] , <EOL> top [ '<STR_LIT:x>' ] ) ) <EOL> print ( "<STR_LIT>" % ( top [ '<STR_LIT>' ] , top [ '<STR_LIT>' ] ) ) <EOL> print ( "<STR_LIT>" , top [ '<STR_LIT>' ] ) </s>
