<s> import unittest <EOL> import ast <EOL> from openmdao . main . api import Assembly , Component , set_as_top <EOL> from openmdao . main . datatypes . api import Float , Array <EOL> from openmdao . main . pseudocomp import unit_xform <EOL> from openmdao . units . units import PhysicalQuantity <EOL> from openmdao . main . printexpr import print_node <EOL> from openmdao . util . testutil import assert_rel_error <EOL> class Simple ( Component ) : <EOL> a = Float ( iotype = '<STR_LIT>' , units = '<STR_LIT>' ) <EOL> b = Float ( iotype = '<STR_LIT>' , units = '<STR_LIT>' ) <EOL> c = Float ( iotype = '<STR_LIT>' , units = '<STR_LIT>' ) <EOL> d = Float ( iotype = '<STR_LIT>' , units = '<STR_LIT>' ) <EOL> dist = Float ( iotype = '<STR_LIT>' , units = '<STR_LIT>' ) <EOL> time = Float ( iotype = '<STR_LIT>' , units = '<STR_LIT:s>' ) <EOL> speed = Float ( iotype = '<STR_LIT>' , units = '<STR_LIT>' ) <EOL> arr = Array ( [ <NUM_LIT:1.> , <NUM_LIT> , <NUM_LIT> ] , iotype = '<STR_LIT>' , units = '<STR_LIT>' ) <EOL> def __init__ ( self ) : <EOL> super ( Simple , self ) . __init__ ( ) <EOL> self . a = <NUM_LIT:1> <EOL> self . b = <NUM_LIT:2> <EOL> self . c = <NUM_LIT:3> <EOL> self . d = - <NUM_LIT:1> <EOL> def execute ( self ) : <EOL> self . c = PhysicalQuantity ( self . a + self . b , '<STR_LIT>' ) . in_units_of ( '<STR_LIT>' ) . value <EOL> self . d = PhysicalQuantity ( self . a - self . b , '<STR_LIT>' ) . in_units_of ( '<STR_LIT>' ) . value <EOL> class SimpleNoUnits ( Component ) : <EOL> a = Float ( iotype = '<STR_LIT>' ) <EOL> b = Float ( iotype = '<STR_LIT>' ) <EOL> c = Float ( iotype = '<STR_LIT>' ) <EOL> d = Float ( iotype = '<STR_LIT>' ) <EOL> arr = Array ( [ <NUM_LIT:1.> , <NUM_LIT> , <NUM_LIT> ] , iotype = '<STR_LIT>' ) <EOL> def __init__ ( self ) : <EOL> super ( SimpleNoUnits , self ) . __init__ ( ) <EOL> self . a = <NUM_LIT:1> <EOL> self . b = <NUM_LIT:2> <EOL> self . c = <NUM_LIT:3> <EOL> self . d = - <NUM_LIT:1> <EOL> def execute ( self ) : <EOL> self . c = self . a + self . b <EOL> self . d = self . a - self . b <EOL> class MuComp ( Component ) : <EOL> mu = Float ( <NUM_LIT> , iotype = '<STR_LIT>' , units = '<STR_LIT>' ) <EOL> out = Float ( iotype = '<STR_LIT>' ) <EOL> def execute ( self ) : <EOL> self . out = self . mu <EOL> class MuAsm ( Assembly ) : <EOL> mu = Float ( <NUM_LIT> , iotype = '<STR_LIT>' , units = '<STR_LIT>' ) <EOL> def _simple_model ( units = True ) : <EOL> if units : <EOL> klass = Simple <EOL> else : <EOL> klass = SimpleNoUnits <EOL> top = set_as_top ( Assembly ( ) ) <EOL> top . add ( "<STR_LIT>" , klass ( ) ) <EOL> top . add ( "<STR_LIT>" , klass ( ) ) <EOL> top . driver . workflow . add ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> top . connect ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> return top <EOL> class PseudoCompTestCase ( unittest . TestCase ) : <EOL> def test_unnecessary_pcomp ( self ) : <EOL> top = set_as_top ( MuAsm ( ) ) <EOL> top . add ( '<STR_LIT>' , MuComp ( ) ) <EOL> top . _setup ( ) <EOL> nodes = set ( top . _depgraph . nodes ( ) ) <EOL> top . connect ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . assertEqual ( set ( ) , set ( top . _depgraph . nodes ( ) ) - nodes ) <EOL> def test_basic_nounits ( self ) : <EOL> top = _simple_model ( units = False ) <EOL> top . _setup ( ) <EOL> self . assertEqual ( set ( top . _depgraph . component_graph ( ) . nodes ( ) ) , <EOL> set ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) ) <EOL> self . assertEqual ( set ( top . _depgraph . list_connections ( ) ) , <EOL> set ( [ ( '<STR_LIT>' , '<STR_LIT>' ) ] ) ) <EOL> def test_basic_units ( self ) : <EOL> top = _simple_model ( ) <EOL> top . _setup ( ) <EOL> self . assertEqual ( set ( top . _depgraph . component_graph ( ) . nodes ( ) ) , <EOL> set ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) ) <EOL> self . assertEqual ( set ( top . _depgraph . list_connections ( ) ) , <EOL> set ( [ ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ] ) ) <EOL> self . assertEqual ( top . _pseudo_0 . _orig_expr , "<STR_LIT>" ) <EOL> top . comp1 . a = <NUM_LIT> <EOL> top . comp1 . b = <NUM_LIT> <EOL> top . run ( ) <EOL> self . assertAlmostEqual ( top . comp1 . c , <NUM_LIT> ) <EOL> self . assertAlmostEqual ( top . comp2 . a , <NUM_LIT> ) <EOL> def test_multi_src ( self ) : <EOL> top = _simple_model ( ) <EOL> top . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> top . comp1 . dist = <NUM_LIT> <EOL> top . comp1 . time = <NUM_LIT> <EOL> top . run ( ) <EOL> self . assertAlmostEqual ( top . comp2 . speed , <NUM_LIT> ) <EOL> self . assertTrue ( hasattr ( top , '<STR_LIT>' ) ) <EOL> self . assertTrue ( hasattr ( top , '<STR_LIT>' ) ) <EOL> self . assertEqual ( set ( top . list_connections ( ) ) , <EOL> set ( [ ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ] ) ) <EOL> self . assertEqual ( set ( top . _depgraph . component_graph ( ) . nodes ( ) ) , <EOL> set ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] ) ) <EOL> self . assertEqual ( set ( top . _depgraph . list_connections ( ) ) , <EOL> set ( [ ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ] ) ) <EOL> top . disconnect ( '<STR_LIT>' ) <EOL> top . _setup ( ) <EOL> self . assertEqual ( set ( top . _depgraph . list_connections ( ) ) , <EOL> set ( [ ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] ) ) <EOL> self . assertEqual ( set ( top . _depgraph . component_graph ( ) . nodes ( ) ) , <EOL> set ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) ) <EOL> self . assertTrue ( hasattr ( top , '<STR_LIT>' ) ) <EOL> self . assertEqual ( set ( top . list_connections ( ) ) , <EOL> set ( [ ( '<STR_LIT>' , '<STR_LIT>' ) ] ) ) <EOL> top . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> top . _setup ( ) <EOL> self . assertTrue ( hasattr ( top , '<STR_LIT>' ) ) <EOL> self . assertEqual ( set ( top . _depgraph . component_graph ( ) . nodes ( ) ) , <EOL> set ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) ) <EOL> self . assertEqual ( set ( top . _depgraph . list_connections ( ) ) , <EOL> set ( [ ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ] ) ) <EOL> self . assertEqual ( set ( top . list_connections ( ) ) , <EOL> set ( [ ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ] ) ) <EOL> top . disconnect ( '<STR_LIT>' ) <EOL> top . _setup ( ) <EOL> self . assertFalse ( hasattr ( top , '<STR_LIT>' ) ) <EOL> self . assertEqual ( set ( top . _depgraph . component_graph ( ) . nodes ( ) ) , <EOL> set ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) ) <EOL> self . assertEqual ( set ( top . _depgraph . list_connections ( ) ) , <EOL> set ( [ ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] ) ) <EOL> self . assertEqual ( set ( top . list_connections ( ) ) , <EOL> set ( [ ( '<STR_LIT>' , '<STR_LIT>' ) ] ) ) <EOL> top . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> top . _setup ( ) <EOL> self . assertTrue ( hasattr ( top , '<STR_LIT>' ) ) <EOL> self . assertEqual ( set ( top . _depgraph . component_graph ( ) . nodes ( ) ) , <EOL> set ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) ) <EOL> self . assertEqual ( set ( top . _depgraph . list_connections ( ) ) , <EOL> set ( [ ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ] ) ) <EOL> self . assertEqual ( set ( top . list_connections ( ) ) , <EOL> set ( [ ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ] ) ) <EOL> top . disconnect ( '<STR_LIT>' ) <EOL> top . _setup ( ) <EOL> self . assertFalse ( hasattr ( top , '<STR_LIT>' ) ) <EOL> self . assertEqual ( set ( top . _depgraph . component_graph ( ) . nodes ( ) ) , <EOL> set ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) ) <EOL> self . assertEqual ( set ( top . _depgraph . list_connections ( ) ) , <EOL> set ( [ ] ) ) <EOL> self . assertEqual ( set ( top . list_connections ( ) ) , <EOL> set ( ) ) <EOL> def test_multi_src_arr ( self ) : <EOL> top = _simple_model ( ) <EOL> top . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> top . comp1 . arr [ <NUM_LIT:1> ] = <NUM_LIT> <EOL> top . comp1 . time = <NUM_LIT> <EOL> top . run ( ) <EOL> self . assertAlmostEqual ( top . comp2 . speed , <NUM_LIT> ) <EOL> self . assertTrue ( hasattr ( top , '<STR_LIT>' ) ) <EOL> self . assertTrue ( hasattr ( top , '<STR_LIT>' ) ) <EOL> self . assertEqual ( set ( top . list_connections ( ) ) , <EOL> set ( [ ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ] ) ) <EOL> self . assertEqual ( set ( top . _depgraph . component_graph ( ) . nodes ( ) ) , <EOL> set ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] ) ) <EOL> self . assertEqual ( set ( top . _depgraph . list_connections ( ) ) , <EOL> set ( [ ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ] ) ) <EOL> top . disconnect ( '<STR_LIT>' ) <EOL> top . _setup ( ) <EOL> self . assertFalse ( hasattr ( top , '<STR_LIT>' ) ) <EOL> self . assertFalse ( hasattr ( top , '<STR_LIT>' ) ) <EOL> self . assertTrue ( hasattr ( top , '<STR_LIT>' ) ) <EOL> self . assertEqual ( set ( top . _depgraph . component_graph ( ) . nodes ( ) ) , <EOL> set ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) ) <EOL> self . assertEqual ( set ( top . _depgraph . list_connections ( ) ) , <EOL> set ( [ ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] ) ) <EOL> self . assertEqual ( set ( top . list_connections ( ) ) , <EOL> set ( [ ( '<STR_LIT>' , '<STR_LIT>' ) ] ) ) <EOL> def test_multi_src_boundary_var ( self ) : <EOL> top = _simple_model ( ) <EOL> top . add ( '<STR_LIT>' , Array ( [ <NUM_LIT:1.> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , iotype = '<STR_LIT>' , units = '<STR_LIT>' ) ) <EOL> top . add ( '<STR_LIT>' , Float ( <NUM_LIT:0.> , iotype = '<STR_LIT>' , units = '<STR_LIT>' ) ) <EOL> top . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> top . arr [ <NUM_LIT:1> ] = <NUM_LIT> <EOL> top . comp1 . time = <NUM_LIT> <EOL> top . run ( ) <EOL> self . assertAlmostEqual ( top . spd_out , <NUM_LIT> ) <EOL> class Comp1 ( Component ) : <EOL> a = Array ( [ <NUM_LIT:0> , <NUM_LIT:0> ] , iotype = "<STR_LIT>" ) <EOL> b = Float ( iotype = "<STR_LIT>" ) <EOL> def execute ( self ) : <EOL> self . b = ( self . a [ <NUM_LIT:0> ] + self . a [ <NUM_LIT:1> ] - <NUM_LIT:1> ) ** <NUM_LIT:2> <EOL> class SubAsmb ( Assembly ) : <EOL> x = Array ( [ <NUM_LIT:1.> , <NUM_LIT:1.> ] , iotype = "<STR_LIT>" ) <EOL> y = Float ( <NUM_LIT:2> , iotype = "<STR_LIT>" ) <EOL> z = Float ( iotype = "<STR_LIT>" ) <EOL> def configure ( self ) : <EOL> self . add ( '<STR_LIT>' , Comp1 ( ) ) <EOL> self . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . connect ( '<STR_LIT>' , '<STR_LIT:z>' ) <EOL> self . driver . workflow . add ( '<STR_LIT>' ) <EOL> class Test_Pseudo_Deriv ( unittest . TestCase ) : <EOL> def test_scaler_array_expression ( self ) : <EOL> model = Assembly ( ) <EOL> model . add ( '<STR_LIT>' , SubAsmb ( ) ) <EOL> model . driver . workflow . add ( '<STR_LIT>' ) <EOL> model . run ( ) <EOL> J = model . driver . calc_gradient ( inputs = [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> outputs = [ '<STR_LIT>' ] ) <EOL> assert_rel_error ( self , J [ <NUM_LIT:0> , <NUM_LIT:0> ] , <NUM_LIT> , <NUM_LIT> ) <EOL> assert_rel_error ( self , J [ <NUM_LIT:0> , <NUM_LIT:1> ] , <NUM_LIT> , <NUM_LIT> ) <EOL> assert_rel_error ( self , J [ <NUM_LIT:0> , <NUM_LIT:2> ] , <NUM_LIT> , <NUM_LIT> ) <EOL> class UnitXformerTestCase ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> pass <EOL> def tearDown ( self ) : <EOL> pass <EOL> def test_simple_conversion ( self ) : <EOL> node = ast . parse ( '<STR_LIT:a>' ) <EOL> cnv = unit_xform ( node , '<STR_LIT>' , '<STR_LIT>' ) <EOL> newexpr = print_node ( cnv ) <EOL> self . assertEqual ( newexpr , '<STR_LIT>' ) <EOL> def test_scaler_adder_conversion ( self ) : <EOL> node = ast . parse ( '<STR_LIT:a>' ) <EOL> cnv = unit_xform ( node , '<STR_LIT>' , '<STR_LIT>' ) <EOL> newexpr = print_node ( cnv ) <EOL> self . assertEqual ( newexpr , '<STR_LIT>' ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
