<s> from __future__ import print_function <EOL> from openmdao . api import Component , Group , Problem , Newton , ScipyGMRES <EOL> class Line ( Component ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> super ( Line , self ) . __init__ ( ) <EOL> self . add_param ( '<STR_LIT:x>' , <NUM_LIT:1.0> ) <EOL> self . add_output ( '<STR_LIT:y>' , <NUM_LIT:0.0> ) <EOL> self . slope = - <NUM_LIT> <EOL> self . intercept = <NUM_LIT> <EOL> def solve_nonlinear ( self , params , unknowns , resids ) : <EOL> """<STR_LIT>""" <EOL> x = params [ '<STR_LIT:x>' ] <EOL> m = self . slope <EOL> b = self . intercept <EOL> unknowns [ '<STR_LIT:y>' ] = m * x + b <EOL> def linearize ( self , params , unknowns , resids ) : <EOL> """<STR_LIT>""" <EOL> m = self . slope <EOL> J = { } <EOL> J [ '<STR_LIT:y>' , '<STR_LIT:x>' ] = m <EOL> return J <EOL> class Parabola ( Component ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> super ( Parabola , self ) . __init__ ( ) <EOL> self . add_param ( '<STR_LIT:x>' , <NUM_LIT:1.0> ) <EOL> self . add_output ( '<STR_LIT:y>' , <NUM_LIT:0.0> ) <EOL> self . a = <NUM_LIT> <EOL> self . b = <NUM_LIT:0.0> <EOL> self . c = - <NUM_LIT> <EOL> def solve_nonlinear ( self , params , unknowns , resids ) : <EOL> """<STR_LIT>""" <EOL> x = params [ '<STR_LIT:x>' ] <EOL> a = self . a <EOL> b = self . b <EOL> c = self . c <EOL> unknowns [ '<STR_LIT:y>' ] = a * x ** <NUM_LIT:2> + b * x + c <EOL> def linearize ( self , params , unknowns , resids ) : <EOL> """<STR_LIT>""" <EOL> x = params [ '<STR_LIT:x>' ] <EOL> a = self . a <EOL> b = self . b <EOL> J = { } <EOL> J [ '<STR_LIT:y>' , '<STR_LIT:x>' ] = <NUM_LIT> * a * x + b <EOL> return J <EOL> class Balance ( Component ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> super ( Balance , self ) . __init__ ( ) <EOL> self . add_param ( '<STR_LIT>' , <NUM_LIT:0.0> ) <EOL> self . add_param ( '<STR_LIT>' , <NUM_LIT:0.0> ) <EOL> self . add_state ( '<STR_LIT:x>' , <NUM_LIT> ) <EOL> def solve_nonlinear ( self , params , unknowns , resids ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def apply_nonlinear ( self , params , unknowns , resids ) : <EOL> """<STR_LIT>""" <EOL> y1 = params [ '<STR_LIT>' ] <EOL> y2 = params [ '<STR_LIT>' ] <EOL> resids [ '<STR_LIT:x>' ] = y1 - y2 <EOL> def linearize ( self , params , unknowns , resids ) : <EOL> """<STR_LIT>""" <EOL> J = { } <EOL> J [ '<STR_LIT:x>' , '<STR_LIT>' ] = <NUM_LIT:1.0> <EOL> J [ '<STR_LIT:x>' , '<STR_LIT>' ] = - <NUM_LIT:1.0> <EOL> return J <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> top = Problem ( ) <EOL> root = top . root = Group ( ) <EOL> root . add ( '<STR_LIT>' , Line ( ) ) <EOL> root . add ( '<STR_LIT>' , Parabola ( ) ) <EOL> root . add ( '<STR_LIT>' , Balance ( ) ) <EOL> root . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> root . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> root . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> root . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> root . nl_solver = Newton ( ) <EOL> root . ln_solver = ScipyGMRES ( ) <EOL> top . setup ( ) <EOL> top [ '<STR_LIT>' ] = <NUM_LIT> <EOL> root . list_states ( ) <EOL> top . run ( ) <EOL> print ( '<STR_LIT>' % ( top [ '<STR_LIT>' ] , top [ '<STR_LIT>' ] , top [ '<STR_LIT>' ] ) ) <EOL> top [ '<STR_LIT>' ] = - <NUM_LIT> <EOL> root . list_states ( ) <EOL> top . run ( ) <EOL> print ( '<STR_LIT>' % ( top [ '<STR_LIT>' ] , top [ '<STR_LIT>' ] , top [ '<STR_LIT>' ] ) ) </s>
