<s> """<STR_LIT>""" <EOL> import unittest <EOL> from openmdao . main . api import Assembly , Component , set_as_top <EOL> from openmdao . main . datatypes . api import Array , Float <EOL> from openmdao . lib . drivers . distributioncasedriver import DistributionCaseDriver <EOL> from openmdao . lib . drivers . distributioncasedriver import FiniteDifferenceGenerator <EOL> class SimpleComponent ( Component ) : <EOL> """<STR_LIT>""" <EOL> x = Float ( <NUM_LIT:1.> , iotype = '<STR_LIT>' ) <EOL> y = Float ( <NUM_LIT:0.> , iotype = '<STR_LIT>' ) <EOL> def execute ( self ) : <EOL> """<STR_LIT>""" <EOL> self . y = <NUM_LIT> * self . x <EOL> def rosen_suzuki ( x0 , x1 , x2 , x3 ) : <EOL> """<STR_LIT>""" <EOL> return x0 ** <NUM_LIT:2> - <NUM_LIT> * x0 + x1 ** <NUM_LIT:2> - <NUM_LIT> * x1 + <NUM_LIT> * x2 ** <NUM_LIT:2> - <NUM_LIT> * x2 + x3 ** <NUM_LIT:2> + <NUM_LIT> * x3 + <NUM_LIT:50> <EOL> class RosenSuzukiComponent ( Component ) : <EOL> """<STR_LIT>""" <EOL> x0 = Float ( <NUM_LIT:1.> , iotype = '<STR_LIT>' ) <EOL> x1 = Float ( <NUM_LIT:1.> , iotype = '<STR_LIT>' ) <EOL> x2 = Float ( <NUM_LIT:1.> , iotype = '<STR_LIT>' ) <EOL> x3 = Float ( <NUM_LIT:1.> , iotype = '<STR_LIT>' , low = - <NUM_LIT> , high = <NUM_LIT> ) <EOL> rosen_suzuki = Float ( <NUM_LIT:0.> , iotype = '<STR_LIT>' ) <EOL> def __init__ ( self ) : <EOL> super ( RosenSuzukiComponent , self ) . __init__ ( ) <EOL> def execute ( self ) : <EOL> """<STR_LIT>""" <EOL> self . rosen_suzuki = rosen_suzuki ( self . x0 , self . x1 , self . x2 , self . x3 ) <EOL> class MyModel ( Assembly ) : <EOL> """<STR_LIT>""" <EOL> def configure ( self ) : <EOL> self . add ( '<STR_LIT>' , DistributionCaseDriver ( ) ) <EOL> self . add ( '<STR_LIT>' , RosenSuzukiComponent ( ) ) <EOL> self . driver . workflow . add ( '<STR_LIT>' ) <EOL> self . driver . distribution_generator = FiniteDifferenceGenerator ( self . driver ) <EOL> self . driver . add_response ( '<STR_LIT>' ) <EOL> self . driver . add_parameter ( "<STR_LIT>" , low = - <NUM_LIT> , high = <NUM_LIT> , fd_step = <NUM_LIT:0.1> ) <EOL> self . driver . add_parameter ( "<STR_LIT>" , low = - <NUM_LIT> , high = <NUM_LIT> , fd_step = <NUM_LIT> ) <EOL> self . driver . add_parameter ( "<STR_LIT>" , low = - <NUM_LIT> , high = <NUM_LIT> , fd_step = <NUM_LIT> ) <EOL> self . driver . add_parameter ( "<STR_LIT>" , low = - <NUM_LIT> , high = <NUM_LIT> , fd_step = <NUM_LIT> ) <EOL> class TestCase ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> self . model = set_as_top ( MyModel ( ) ) <EOL> def tearDown ( self ) : <EOL> pass <EOL> def test_super_simple_forward ( self ) : <EOL> model = Assembly ( ) <EOL> model . add ( '<STR_LIT>' , DistributionCaseDriver ( ) ) <EOL> model . add ( '<STR_LIT>' , SimpleComponent ( ) ) <EOL> model . driver . workflow . add ( '<STR_LIT>' ) <EOL> model . driver . distribution_generator = FiniteDifferenceGenerator ( model . driver ) <EOL> model . driver . add_response ( '<STR_LIT>' ) <EOL> model . driver . add_parameter ( "<STR_LIT>" , low = - <NUM_LIT> , high = <NUM_LIT> , fd_step = <NUM_LIT:0.1> ) <EOL> model . driver . distribution_generator . form = "<STR_LIT>" <EOL> model . driver . distribution_generator . order = <NUM_LIT:2> <EOL> model . run ( ) <EOL> x = model . driver . case_inputs . driven . x <EOL> y = model . driver . case_outputs . driven . y <EOL> self . assertAlmostEqual ( x [ <NUM_LIT:0> ] , <NUM_LIT:1.0> , places = <NUM_LIT:6> ) <EOL> self . assertAlmostEqual ( y [ <NUM_LIT:0> ] , <NUM_LIT> , places = <NUM_LIT:6> ) <EOL> self . assertAlmostEqual ( x [ <NUM_LIT:1> ] , <NUM_LIT> , places = <NUM_LIT:6> ) <EOL> self . assertAlmostEqual ( y [ <NUM_LIT:1> ] , <NUM_LIT> , places = <NUM_LIT:6> ) <EOL> self . assertAlmostEqual ( x [ <NUM_LIT:2> ] , <NUM_LIT> , places = <NUM_LIT:6> ) <EOL> self . assertAlmostEqual ( y [ <NUM_LIT:2> ] , <NUM_LIT> , places = <NUM_LIT:6> ) <EOL> def test_super_simple_backward ( self ) : <EOL> model = Assembly ( ) <EOL> model . add ( '<STR_LIT>' , DistributionCaseDriver ( ) ) <EOL> model . add ( '<STR_LIT>' , SimpleComponent ( ) ) <EOL> model . driver . workflow . add ( '<STR_LIT>' ) <EOL> model . driver . distribution_generator = FiniteDifferenceGenerator ( model . driver ) <EOL> model . driver . add_response ( '<STR_LIT>' ) <EOL> model . driver . add_parameter ( "<STR_LIT>" , low = - <NUM_LIT> , high = <NUM_LIT> , fd_step = <NUM_LIT:0.1> ) <EOL> model . driver . distribution_generator . form = "<STR_LIT>" <EOL> model . driver . distribution_generator . order = <NUM_LIT:2> <EOL> model . run ( ) <EOL> x = model . driver . case_inputs . driven . x <EOL> y = model . driver . case_outputs . driven . y <EOL> self . assertAlmostEqual ( x [ <NUM_LIT:0> ] , <NUM_LIT:1.0> , places = <NUM_LIT:6> ) <EOL> self . assertAlmostEqual ( y [ <NUM_LIT:0> ] , <NUM_LIT> , places = <NUM_LIT:6> ) <EOL> self . assertAlmostEqual ( x [ <NUM_LIT:1> ] , <NUM_LIT> , places = <NUM_LIT:6> ) <EOL> self . assertAlmostEqual ( y [ <NUM_LIT:1> ] , <NUM_LIT> , places = <NUM_LIT:6> ) <EOL> self . assertAlmostEqual ( x [ <NUM_LIT:2> ] , <NUM_LIT> , places = <NUM_LIT:6> ) <EOL> self . assertAlmostEqual ( y [ <NUM_LIT:2> ] , <NUM_LIT> , places = <NUM_LIT:6> ) <EOL> def test_super_simple_central ( self ) : <EOL> model = Assembly ( ) <EOL> model . add ( '<STR_LIT>' , DistributionCaseDriver ( ) ) <EOL> model . add ( '<STR_LIT>' , SimpleComponent ( ) ) <EOL> model . driver . workflow . add ( '<STR_LIT>' ) <EOL> model . driver . distribution_generator = FiniteDifferenceGenerator ( model . driver ) <EOL> model . driver . add_response ( '<STR_LIT>' ) <EOL> model . driver . add_parameter ( "<STR_LIT>" , low = - <NUM_LIT> , high = <NUM_LIT> , fd_step = <NUM_LIT:0.1> ) <EOL> model . driver . distribution_generator . form = "<STR_LIT>" <EOL> model . driver . distribution_generator . order = <NUM_LIT:2> <EOL> model . run ( ) <EOL> x = model . driver . case_inputs . driven . x <EOL> y = model . driver . case_outputs . driven . y <EOL> self . assertAlmostEqual ( x [ <NUM_LIT:0> ] , <NUM_LIT:1.0> , places = <NUM_LIT:6> ) <EOL> self . assertAlmostEqual ( y [ <NUM_LIT:0> ] , <NUM_LIT> , places = <NUM_LIT:6> ) <EOL> self . assertAlmostEqual ( x [ <NUM_LIT:1> ] , <NUM_LIT> , places = <NUM_LIT:6> ) <EOL> self . assertAlmostEqual ( y [ <NUM_LIT:1> ] , <NUM_LIT> , places = <NUM_LIT:6> ) <EOL> self . assertAlmostEqual ( x [ <NUM_LIT:2> ] , <NUM_LIT> , places = <NUM_LIT:6> ) <EOL> self . assertAlmostEqual ( y [ <NUM_LIT:2> ] , <NUM_LIT> , places = <NUM_LIT:6> ) <EOL> def test_basics ( self ) : <EOL> self . model . driver . distribution_generator = FiniteDifferenceGenerator ( self . model . driver ) <EOL> self . order = <NUM_LIT:1> <EOL> self . model . driver . distribution_generator . form = "<STR_LIT>" <EOL> self . model . driver . distribution_generator . order = self . order <EOL> self . model . run ( ) <EOL> self . verify_results ( ) <EOL> self . model . driven . x0 = self . model . driven . x1 = self . model . driven . x2 = self . model . driven . x3 = <NUM_LIT:1.0> <EOL> self . model . driver . distribution_generator = FiniteDifferenceGenerator ( self . model . driver ) <EOL> self . order = <NUM_LIT:2> <EOL> self . model . driver . distribution_generator . form = "<STR_LIT>" <EOL> self . model . driver . distribution_generator . order = self . order <EOL> self . model . run ( ) <EOL> self . verify_results ( ) <EOL> self . model . driven . x0 = self . model . driven . x1 = self . model . driven . x2 = self . model . driven . x3 = <NUM_LIT:1.0> <EOL> self . model . driver . distribution_generator = FiniteDifferenceGenerator ( self . model . driver ) <EOL> self . order = <NUM_LIT:2> <EOL> self . model . driver . distribution_generator . form = "<STR_LIT>" <EOL> self . model . driver . distribution_generator . order = self . order <EOL> self . model . run ( ) <EOL> self . verify_results ( ) <EOL> self . model . driven . x0 = self . model . driven . x1 = self . model . driven . x2 = self . model . driven . x3 = <NUM_LIT:1.0> <EOL> self . model . driver . distribution_generator = FiniteDifferenceGenerator ( self . model . driver ) <EOL> self . order = <NUM_LIT:3> <EOL> self . model . driver . distribution_generator . form = "<STR_LIT>" <EOL> self . model . driver . distribution_generator . order = self . order <EOL> self . model . run ( ) <EOL> self . verify_results ( ) <EOL> def verify_results ( self ) : <EOL> num_params = self . model . driver . total_parameters ( ) <EOL> results = self . model . driver . case_outputs . driven . rosen_suzuki <EOL> if self . model . driver . distribution_generator . form != "<STR_LIT>" : <EOL> self . assertEqual ( len ( results ) , <NUM_LIT:1> + num_params * self . order ) <EOL> else : <EOL> if self . model . driver . distribution_generator . order % <NUM_LIT:2> == <NUM_LIT:1> : <EOL> self . assertEqual ( len ( results ) , num_params * ( self . order + <NUM_LIT:1> ) ) <EOL> else : <EOL> self . assertEqual ( len ( results ) , <NUM_LIT:1> + num_params * self . order ) <EOL> inputs = self . model . driver . case_inputs . driven <EOL> for i , result in enumerate ( results ) : <EOL> x0 = inputs . x0 [ i ] <EOL> x1 = inputs . x1 [ i ] <EOL> x2 = inputs . x2 [ i ] <EOL> x3 = inputs . x3 [ i ] <EOL> self . assertEqual ( result , rosen_suzuki ( x0 , x1 , x2 , x3 ) ) <EOL> def test_invalid_input ( self ) : <EOL> model = Assembly ( ) <EOL> model . add ( '<STR_LIT>' , DistributionCaseDriver ( ) ) <EOL> model . add ( '<STR_LIT>' , SimpleComponent ( ) ) <EOL> model . driver . workflow . add ( '<STR_LIT>' ) <EOL> model . driver . distribution_generator = FiniteDifferenceGenerator ( model . driver ) <EOL> try : <EOL> model . driver . add_parameter ( "<STR_LIT>" , low = - <NUM_LIT> , high = <NUM_LIT> , fd_step = <NUM_LIT:0.1> ) <EOL> except AttributeError as err : <EOL> self . assertEqual ( str ( err ) , "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> else : <EOL> self . fail ( '<STR_LIT>' ) <EOL> def test_invalid_case_outputs ( self ) : <EOL> model = Assembly ( ) <EOL> model . add ( '<STR_LIT>' , DistributionCaseDriver ( ) ) <EOL> model . add ( '<STR_LIT>' , SimpleComponent ( ) ) <EOL> model . driver . workflow . add ( '<STR_LIT>' ) <EOL> model . driver . distribution_generator = FiniteDifferenceGenerator ( model . driver ) <EOL> try : <EOL> model . driver . add_response ( '<STR_LIT>' ) <EOL> except ValueError as err : <EOL> self . assertEqual ( str ( err ) , "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> else : <EOL> self . fail ( '<STR_LIT>' ) <EOL> def test_invalid_form ( self ) : <EOL> model = Assembly ( ) <EOL> model . add ( '<STR_LIT>' , DistributionCaseDriver ( ) ) <EOL> model . add ( '<STR_LIT>' , SimpleComponent ( ) ) <EOL> model . driver . workflow . add ( '<STR_LIT>' ) <EOL> model . driver . distribution_generator = FiniteDifferenceGenerator ( model . driver ) <EOL> model . driver . add_response ( '<STR_LIT>' ) <EOL> model . driver . add_parameter ( "<STR_LIT>" , low = - <NUM_LIT> , high = <NUM_LIT> , fd_step = <NUM_LIT:0.1> ) <EOL> try : <EOL> model . driver . distribution_generator . form = "<STR_LIT>" <EOL> except ValueError , err : <EOL> msg = "<STR_LIT>" "<STR_LIT>" <EOL> self . assertEqual ( str ( err ) , msg ) <EOL> else : <EOL> self . fail ( '<STR_LIT>' ) <EOL> class ArrayRosenSuzuki ( Component ) : <EOL> """<STR_LIT>""" <EOL> x = Array ( [ <NUM_LIT:1.> , <NUM_LIT:1.> , <NUM_LIT:1.> , <NUM_LIT:1.> ] , iotype = '<STR_LIT>' ) <EOL> rosen_suzuki = Float ( <NUM_LIT:0.> , iotype = '<STR_LIT>' ) <EOL> def execute ( self ) : <EOL> """<STR_LIT>""" <EOL> self . rosen_suzuki = rosen_suzuki ( self . x [ <NUM_LIT:0> ] , self . x [ <NUM_LIT:1> ] , self . x [ <NUM_LIT:2> ] , self . x [ <NUM_LIT:3> ] ) <EOL> class ArrayModel ( Assembly ) : <EOL> """<STR_LIT>""" <EOL> def configure ( self ) : <EOL> driver = self . add ( '<STR_LIT>' , DistributionCaseDriver ( ) ) <EOL> self . add ( '<STR_LIT>' , ArrayRosenSuzuki ( ) ) <EOL> driver . workflow . add ( '<STR_LIT>' ) <EOL> driver . distribution_generator = FiniteDifferenceGenerator ( driver ) <EOL> driver . add_response ( '<STR_LIT>' ) <EOL> driver . add_parameter ( '<STR_LIT>' , low = - <NUM_LIT> , high = <NUM_LIT> , <EOL> fd_step = [ <NUM_LIT:0.1> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) <EOL> class ArrayTest ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_forward ( self ) : <EOL> model = set_as_top ( ArrayModel ( ) ) <EOL> driver = model . driver <EOL> driver . distribution_generator . form = "<STR_LIT>" <EOL> driver . distribution_generator . order = <NUM_LIT:1> <EOL> model . run ( ) <EOL> num_params = driver . total_parameters ( ) <EOL> results = driver . case_outputs . driven . rosen_suzuki <EOL> self . assertEqual ( len ( results ) , <NUM_LIT:1> + num_params ) <EOL> for i , result in enumerate ( results ) : <EOL> x = driver . case_inputs . driven . x [ i ] <EOL> self . assertEqual ( result , rosen_suzuki ( x [ <NUM_LIT:0> ] , x [ <NUM_LIT:1> ] , x [ <NUM_LIT:2> ] , x [ <NUM_LIT:3> ] ) ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> unittest . main ( ) </s>
