<s> import numpy as np <EOL> import unittest <EOL> from openmdao . main . api import Assembly , Component , Driver , set_as_top <EOL> from openmdao . main . datatypes . api import Float , Array <EOL> from openmdao . main . hasconstraints import HasConstraints , HasEqConstraints , HasIneqConstraints , Constraint , Has2SidedConstraints <EOL> from openmdao . main . interfaces import IHas2SidedConstraints , implements <EOL> from openmdao . main . pseudocomp import SimpleEQConPComp , SimpleEQ0PComp <EOL> from openmdao . main . test . simpledriver import SimpleDriver <EOL> from openmdao . test . execcomp import ExecComp <EOL> from openmdao . units . units import PhysicalQuantity <EOL> from openmdao . util . decorators import add_delegate <EOL> from openmdao . util . testutil import assert_rel_error <EOL> @ add_delegate ( HasConstraints ) <EOL> class MyDriver ( Driver ) : <EOL> pass <EOL> @ add_delegate ( HasEqConstraints ) <EOL> class MyEqDriver ( Driver ) : <EOL> pass <EOL> @ add_delegate ( HasIneqConstraints ) <EOL> class MyInEqDriver ( Driver ) : <EOL> pass <EOL> @ add_delegate ( HasConstraints , Has2SidedConstraints ) <EOL> class My2SDriver ( Driver ) : <EOL> implements ( IHas2SidedConstraints ) <EOL> class SimpleUnits ( 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> arr = Array ( [ <NUM_LIT:1.> , <NUM_LIT> , <NUM_LIT> ] , iotype = '<STR_LIT>' , units = '<STR_LIT>' ) <EOL> arr_out = Array ( [ <NUM_LIT:1.> , <NUM_LIT> , <NUM_LIT> ] , iotype = '<STR_LIT>' , units = '<STR_LIT>' ) <EOL> def __init__ ( self ) : <EOL> super ( SimpleUnits , 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 Simple ( 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> 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 = self . a + self . b <EOL> self . d = self . a - self . b <EOL> def list_deriv_vars ( self ) : <EOL> return ( '<STR_LIT:a>' , '<STR_LIT:b>' ) , ( '<STR_LIT:c>' , '<STR_LIT:d>' ) <EOL> def provideJ ( self ) : <EOL> der = <NUM_LIT:1.0> <EOL> return np . array ( [ [ der , der ] , [ der , der ] ] ) <EOL> class HasConstraintsTestCase ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . asm = set_as_top ( Assembly ( ) ) <EOL> self . asm . add ( '<STR_LIT>' , Simple ( ) ) <EOL> self . asm . add ( '<STR_LIT>' , Simple ( ) ) <EOL> self . asm . add ( '<STR_LIT>' , SimpleUnits ( ) ) <EOL> self . asm . add ( '<STR_LIT>' , SimpleUnits ( ) ) <EOL> def test_list_constraints ( self ) : <EOL> drv = self . asm . add ( '<STR_LIT>' , MyDriver ( ) ) <EOL> self . asm . run ( ) <EOL> drv . add_constraint ( '<STR_LIT>' ) <EOL> drv . add_constraint ( '<STR_LIT>' ) <EOL> self . assertEqual ( drv . list_constraints ( ) , <EOL> [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_list_eq_constraints ( self ) : <EOL> drv = self . asm . add ( '<STR_LIT>' , MyEqDriver ( ) ) <EOL> drv . add_constraint ( '<STR_LIT>' ) <EOL> drv . add_constraint ( '<STR_LIT>' ) <EOL> self . assertEqual ( drv . list_constraints ( ) , <EOL> [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_list_ineq_constraints ( self ) : <EOL> drv = self . asm . add ( '<STR_LIT>' , MyDriver ( ) ) <EOL> drv . add_constraint ( '<STR_LIT>' ) <EOL> drv . add_constraint ( '<STR_LIT>' ) <EOL> self . assertEqual ( drv . list_constraints ( ) , <EOL> [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def _check_ineq_add_constraint ( self , drv ) : <EOL> self . asm . add ( '<STR_LIT>' , drv ) <EOL> try : <EOL> drv . add_constraint ( '<STR_LIT>' ) <EOL> except Exception as err : <EOL> self . assertEqual ( str ( err ) , "<STR_LIT>" ) <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> self . assertEqual ( len ( drv . get_ineq_constraints ( ) ) , <NUM_LIT:0> ) <EOL> drv . add_constraint ( '<STR_LIT>' ) <EOL> try : <EOL> drv . add_constraint ( '<STR_LIT>' ) <EOL> except Exception as err : <EOL> self . assertEqual ( str ( err ) , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> self . assertEqual ( len ( drv . get_ineq_constraints ( ) ) , <NUM_LIT:1> ) <EOL> drv . remove_constraint ( '<STR_LIT>' ) <EOL> self . assertEqual ( len ( drv . get_ineq_constraints ( ) ) , <NUM_LIT:0> ) <EOL> try : <EOL> drv . remove_constraint ( '<STR_LIT>' ) <EOL> except Exception as err : <EOL> self . assertEqual ( str ( err ) , <EOL> "<STR_LIT>" ) <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> drv . add_constraint ( '<STR_LIT>' ) <EOL> self . assertEqual ( len ( drv . get_ineq_constraints ( ) ) , <NUM_LIT:1> ) <EOL> drv . add_constraint ( '<STR_LIT>' , name = '<STR_LIT>' ) <EOL> self . assertEqual ( len ( drv . get_ineq_constraints ( ) ) , <NUM_LIT:2> ) <EOL> try : <EOL> drv . add_constraint ( '<STR_LIT>' , name = '<STR_LIT>' ) <EOL> except Exception as err : <EOL> self . assertEqual ( str ( err ) , '<STR_LIT>' ) <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> self . assertEqual ( len ( drv . get_ineq_constraints ( ) ) , <NUM_LIT:2> ) <EOL> drv . remove_constraint ( '<STR_LIT>' ) <EOL> self . assertEqual ( len ( drv . get_ineq_constraints ( ) ) , <NUM_LIT:1> ) <EOL> drv . clear_constraints ( ) <EOL> self . assertEqual ( len ( drv . get_ineq_constraints ( ) ) , <NUM_LIT:0> ) <EOL> try : <EOL> drv . add_constraint ( '<STR_LIT>' ) <EOL> except ValueError as err : <EOL> self . assertEqual ( str ( err ) , <EOL> "<STR_LIT>" ) <EOL> else : <EOL> self . fail ( '<STR_LIT>' ) <EOL> def _check_eq_add_constraint ( self , drv ) : <EOL> self . asm . add ( '<STR_LIT>' , drv ) <EOL> self . assertEqual ( len ( drv . get_eq_constraints ( ) ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( len ( drv . get_eq_constraints ( ) ) , <NUM_LIT:0> ) <EOL> drv . add_constraint ( '<STR_LIT>' ) <EOL> self . assertEqual ( len ( drv . get_eq_constraints ( ) ) , <NUM_LIT:1> ) <EOL> try : <EOL> drv . add_constraint ( '<STR_LIT>' ) <EOL> except Exception as err : <EOL> self . assertEqual ( str ( err ) , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> drv . remove_constraint ( '<STR_LIT>' ) <EOL> self . assertEqual ( len ( drv . get_eq_constraints ( ) ) , <NUM_LIT:0> ) <EOL> try : <EOL> drv . remove_constraint ( '<STR_LIT>' ) <EOL> except Exception as err : <EOL> self . assertEqual ( str ( err ) , <EOL> "<STR_LIT>" ) <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> self . assertEqual ( len ( drv . get_eq_constraints ( ) ) , <NUM_LIT:0> ) <EOL> drv . add_constraint ( '<STR_LIT>' ) <EOL> self . assertEqual ( len ( drv . get_eq_constraints ( ) ) , <NUM_LIT:1> ) <EOL> drv . add_constraint ( '<STR_LIT>' , name = '<STR_LIT>' ) <EOL> self . assertEqual ( len ( drv . get_eq_constraints ( ) ) , <NUM_LIT:2> ) <EOL> try : <EOL> drv . add_constraint ( '<STR_LIT>' , name = '<STR_LIT>' ) <EOL> except Exception as err : <EOL> self . assertEqual ( str ( err ) , '<STR_LIT>' ) <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> drv . remove_constraint ( '<STR_LIT>' ) <EOL> self . assertEqual ( len ( drv . get_eq_constraints ( ) ) , <NUM_LIT:1> ) <EOL> drv . clear_constraints ( ) <EOL> self . assertEqual ( len ( drv . get_eq_constraints ( ) ) , <NUM_LIT:0> ) <EOL> try : <EOL> drv . add_constraint ( '<STR_LIT>' ) <EOL> except ValueError as err : <EOL> self . assertEqual ( str ( err ) , <EOL> "<STR_LIT>" ) <EOL> else : <EOL> self . fail ( '<STR_LIT>' ) <EOL> def _check_eq_eval_constraints ( self , drv ) : <EOL> self . asm . add ( '<STR_LIT>' , drv ) <EOL> vals = drv . eval_eq_constraints ( ) <EOL> self . assertEqual ( len ( vals ) , <NUM_LIT:0> ) <EOL> drv . add_constraint ( '<STR_LIT>' ) <EOL> self . asm . comp1 . a = <NUM_LIT:4> <EOL> self . asm . comp1 . b = <NUM_LIT:5> <EOL> self . asm . comp1 . c = <NUM_LIT:9> <EOL> self . asm . comp1 . d = - <NUM_LIT:1> <EOL> self . asm . run ( ) <EOL> vals = drv . eval_eq_constraints ( ) <EOL> self . assertEqual ( len ( vals ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( vals [ <NUM_LIT:0> ] , <NUM_LIT> ) <EOL> vals = drv . get_eq_constraints ( ) <EOL> self . assertEqual ( len ( vals ) , <NUM_LIT:1> ) <EOL> self . assertTrue ( isinstance ( vals [ '<STR_LIT>' ] , Constraint ) ) <EOL> def _check_ineq_eval_constraints ( self , drv ) : <EOL> self . asm . add ( '<STR_LIT>' , drv ) <EOL> vals = drv . eval_ineq_constraints ( ) <EOL> self . assertEqual ( len ( vals ) , <NUM_LIT:0> ) <EOL> drv . add_constraint ( '<STR_LIT>' ) <EOL> self . asm . comp1 . a = <NUM_LIT:4> <EOL> self . asm . comp1 . b = <NUM_LIT:5> <EOL> self . asm . comp1 . c = <NUM_LIT:9> <EOL> self . asm . comp1 . d = - <NUM_LIT:1> <EOL> self . asm . run ( ) <EOL> vals = drv . eval_ineq_constraints ( ) <EOL> self . assertEqual ( len ( vals ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( vals [ <NUM_LIT:0> ] , <NUM_LIT:1> ) <EOL> vals = drv . get_ineq_constraints ( ) <EOL> self . assertEqual ( len ( vals ) , <NUM_LIT:1> ) <EOL> self . assertTrue ( isinstance ( vals [ '<STR_LIT>' ] , Constraint ) ) <EOL> def test_constraint_scaler_adder ( self ) : <EOL> drv = self . asm . add ( '<STR_LIT>' , MyDriver ( ) ) <EOL> self . asm . comp1 . a = <NUM_LIT> <EOL> self . asm . comp1 . b = <NUM_LIT> <EOL> drv . add_constraint ( '<STR_LIT>' ) <EOL> self . asm . run ( ) <EOL> result = drv . eval_ineq_constraints ( ) <EOL> self . assertEqual ( result [ <NUM_LIT:0> ] , - <NUM_LIT> ) <EOL> drv . remove_constraint ( '<STR_LIT>' ) <EOL> result = drv . eval_ineq_constraints ( ) <EOL> self . assertEqual ( result , [ ] ) <EOL> def test_add_constraint_eq_eq ( self ) : <EOL> drv = MyDriver ( ) <EOL> self . asm . add ( '<STR_LIT>' , drv ) <EOL> try : <EOL> drv . add_constraint ( '<STR_LIT>' ) <EOL> except Exception as err : <EOL> self . assertEqual ( str ( err ) , "<STR_LIT>" ) <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> def test_add_constraint ( self ) : <EOL> drv = MyDriver ( ) <EOL> self . _check_eq_add_constraint ( drv ) <EOL> self . _check_ineq_add_constraint ( drv ) <EOL> def test_add_eq_constraint ( self ) : <EOL> self . _check_eq_add_constraint ( MyEqDriver ( ) ) <EOL> def test_add_ineq_constraint ( self ) : <EOL> self . _check_ineq_add_constraint ( MyInEqDriver ( ) ) <EOL> def test_implicit_constraint ( self ) : <EOL> drv = self . asm . add ( '<STR_LIT>' , MyEqDriver ( ) ) <EOL> try : <EOL> drv . add_constraint ( '<STR_LIT>' ) <EOL> except ValueError , err : <EOL> self . assertEqual ( str ( err ) , <EOL> "<STR_LIT>" ) <EOL> else : <EOL> self . fail ( '<STR_LIT>' ) <EOL> def test_eval_constraint ( self ) : <EOL> self . _check_eq_eval_constraints ( MyDriver ( ) ) <EOL> self . _check_ineq_eval_constraints ( MyDriver ( ) ) <EOL> def test_eval_eq_constraint ( self ) : <EOL> self . _check_eq_eval_constraints ( MyEqDriver ( ) ) <EOL> def test_eval_ineq_constraint ( self ) : <EOL> self . _check_ineq_eval_constraints ( MyInEqDriver ( ) ) <EOL> def test_pseudocomps ( self ) : <EOL> self . asm . add ( '<STR_LIT>' , MyDriver ( ) ) <EOL> self . asm . driver . workflow . add ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . asm . _setup ( ) <EOL> self . assertEqual ( self . asm . _depgraph . list_connections ( ) , <EOL> [ ] ) <EOL> self . asm . driver . add_constraint ( '<STR_LIT>' ) <EOL> self . asm . _setup ( ) <EOL> self . assertEqual ( self . asm . _pseudo_0 . _orig_expr , '<STR_LIT>' ) <EOL> self . assertEqual ( set ( self . asm . _depgraph . list_connections ( drivers = False ) ) , <EOL> set ( [ ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] ) ) <EOL> self . asm . driver . remove_constraint ( '<STR_LIT>' ) <EOL> self . asm . _setup ( ) <EOL> self . assertEqual ( self . asm . _depgraph . list_connections ( drivers = False ) , [ ] ) <EOL> self . asm . driver . add_constraint ( '<STR_LIT>' ) <EOL> self . asm . _setup ( ) <EOL> self . assertEqual ( set ( self . asm . _depgraph . list_connections ( drivers = False ) ) , <EOL> set ( [ ( '<STR_LIT>' , '<STR_LIT>' ) ] ) ) <EOL> self . asm . _setup ( ) <EOL> self . assertEqual ( self . asm . _pseudo_1 . _orig_expr , '<STR_LIT>' ) <EOL> self . asm . driver . add_constraint ( '<STR_LIT>' ) <EOL> self . asm . _setup ( ) <EOL> self . assertEqual ( self . asm . _pseudo_2 . _orig_expr , '<STR_LIT>' ) <EOL> self . asm . driver . add_constraint ( '<STR_LIT>' ) <EOL> self . asm . _setup ( ) <EOL> self . assertEqual ( self . asm . _pseudo_3 . _orig_expr , '<STR_LIT>' ) <EOL> self . asm . driver . add_constraint ( '<STR_LIT>' ) <EOL> self . asm . _setup ( ) <EOL> self . assertEqual ( self . asm . _pseudo_4 . _orig_expr , '<STR_LIT>' ) <EOL> self . asm . driver . clear_constraints ( ) <EOL> self . asm . comp1 . a = <NUM_LIT:2> <EOL> self . asm . comp1 . b = <NUM_LIT:1> <EOL> self . asm . comp2 . a = <NUM_LIT:4> <EOL> self . asm . comp2 . b = <NUM_LIT:2> <EOL> self . asm . driver . add_constraint ( '<STR_LIT>' ) <EOL> self . asm . driver . add_constraint ( '<STR_LIT>' ) <EOL> self . asm . driver . add_constraint ( '<STR_LIT>' ) <EOL> self . asm . run ( ) <EOL> self . assertEqual ( self . asm . _pseudo_5 . out0 , <NUM_LIT:1.0> ) <EOL> self . assertEqual ( self . asm . _pseudo_6 . out0 , - <NUM_LIT:1.0> ) <EOL> self . assertEqual ( self . asm . _pseudo_7 . out0 , <NUM_LIT> ) <EOL> def test_custom_pseudocomp_creation ( self ) : <EOL> self . asm . add ( '<STR_LIT>' , MyDriver ( ) ) <EOL> arg = { } <EOL> result = { } <EOL> self . asm . driver . add_constraint ( '<STR_LIT>' ) <EOL> self . asm . _setup ( ) <EOL> self . assertEqual ( self . asm . _pseudo_0 . __class__ , SimpleEQ0PComp ) <EOL> self . asm . run ( ) <EOL> arg [ '<STR_LIT>' ] = np . array ( [ <NUM_LIT> ] ) <EOL> result [ '<STR_LIT>' ] = np . array ( [ <NUM_LIT:0.0> ] ) <EOL> self . asm . _pseudo_0 . apply_deriv ( arg , result ) <EOL> self . assertEqual ( result [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , <NUM_LIT> ) <EOL> self . asm . driver . add_constraint ( '<STR_LIT>' ) <EOL> self . asm . _setup ( ) <EOL> self . assertEqual ( self . asm . _pseudo_1 . __class__ , SimpleEQ0PComp ) <EOL> self . asm . run ( ) <EOL> arg [ '<STR_LIT>' ] = np . array ( [ <NUM_LIT> ] ) <EOL> result [ '<STR_LIT>' ] = np . array ( [ <NUM_LIT:0.0> ] ) <EOL> self . asm . _pseudo_1 . apply_deriv ( arg , result ) <EOL> self . assertEqual ( result [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , <NUM_LIT> ) <EOL> self . asm . driver . add_constraint ( '<STR_LIT>' ) <EOL> self . asm . _setup ( ) <EOL> self . assertEqual ( self . asm . _pseudo_2 . __class__ , SimpleEQConPComp ) <EOL> self . asm . run ( ) <EOL> arg [ '<STR_LIT>' ] = np . array ( [ <NUM_LIT> ] ) <EOL> arg [ '<STR_LIT>' ] = np . array ( [ <NUM_LIT> ] ) <EOL> result [ '<STR_LIT>' ] = np . array ( [ <NUM_LIT:0.0> ] ) <EOL> self . asm . _pseudo_2 . apply_deriv ( arg , result ) <EOL> self . assertEqual ( result [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , <NUM_LIT> ) <EOL> self . asm . driver . clear_constraints ( ) <EOL> self . asm . driver . add_constraint ( '<STR_LIT>' ) <EOL> self . asm . _setup ( ) <EOL> self . assertEqual ( self . asm . _pseudo_3 . __class__ , SimpleEQConPComp ) <EOL> self . asm . run ( ) <EOL> arg [ '<STR_LIT>' ] = np . array ( [ <NUM_LIT> ] ) <EOL> arg [ '<STR_LIT>' ] = np . array ( [ <NUM_LIT> ] ) <EOL> result [ '<STR_LIT>' ] = np . array ( [ <NUM_LIT:0.0> ] ) <EOL> self . asm . _pseudo_3 . apply_deriv ( arg , result ) <EOL> self . assertEqual ( result [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , <NUM_LIT> ) <EOL> self . asm . driver . clear_constraints ( ) <EOL> self . asm . driver . add_constraint ( '<STR_LIT>' ) <EOL> self . asm . _setup ( ) <EOL> self . assertEqual ( self . asm . _pseudo_4 . __class__ , SimpleEQConPComp ) <EOL> self . asm . run ( ) <EOL> arg [ '<STR_LIT>' ] = np . array ( [ <NUM_LIT> ] ) <EOL> arg [ '<STR_LIT>' ] = np . array ( [ <NUM_LIT> ] ) <EOL> result [ '<STR_LIT>' ] = np . array ( [ <NUM_LIT:0.0> ] ) <EOL> self . asm . _pseudo_4 . apply_deriv ( arg , result ) <EOL> self . assertEqual ( result [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , <NUM_LIT> ) <EOL> def test_custom_jacobian ( self ) : <EOL> class AComp ( Component ) : <EOL> x = Array ( [ [ <NUM_LIT:1.0> , <NUM_LIT> ] , [ - <NUM_LIT> , <NUM_LIT> ] ] , iotype = '<STR_LIT>' ) <EOL> y = Array ( np . zeros ( ( <NUM_LIT:2> , <NUM_LIT:2> ) ) , iotype = '<STR_LIT>' ) <EOL> def __init__ ( self ) : <EOL> super ( AComp , self ) . __init__ ( ) <EOL> self . J = np . array ( [ [ <NUM_LIT> , - <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , - <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT:0.1> , <NUM_LIT> , <NUM_LIT> , - <NUM_LIT> ] ] ) <EOL> def execute ( self ) : <EOL> """<STR_LIT>""" <EOL> y = self . J . dot ( self . x . flatten ( ) ) <EOL> self . y = y . reshape ( ( <NUM_LIT:2> , <NUM_LIT:2> ) ) <EOL> def list_deriv_vars ( self ) : <EOL> """<STR_LIT>""" <EOL> input_keys = ( '<STR_LIT:x>' , ) <EOL> output_keys = ( '<STR_LIT:y>' , ) <EOL> return input_keys , output_keys <EOL> def provideJ ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . J <EOL> def fake_jac ( ) : <EOL> """<STR_LIT>""" <EOL> jacs = { } <EOL> jacs [ '<STR_LIT>' ] = np . array ( [ [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] ) <EOL> return jacs <EOL> top = set_as_top ( Assembly ( ) ) <EOL> top . add ( '<STR_LIT>' , SimpleDriver ( ) ) <EOL> top . add ( '<STR_LIT>' , AComp ( ) ) <EOL> top . driver . workflow . add ( '<STR_LIT>' ) <EOL> top . driver . add_parameter ( '<STR_LIT>' , low = <NUM_LIT:10> , high = <NUM_LIT:10> ) <EOL> top . driver . add_constraint ( '<STR_LIT>' , jacs = fake_jac ) <EOL> top . _setup ( ) <EOL> top . run ( ) <EOL> J = top . driver . calc_gradient ( mode = '<STR_LIT>' , return_format = '<STR_LIT>' ) <EOL> J = J [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> diff = np . abs ( J - top . comp . J ) <EOL> assert_rel_error ( self , diff . max ( ) , <NUM_LIT:0.0> , <NUM_LIT> ) <EOL> J = top . driver . calc_gradient ( mode = '<STR_LIT>' , return_format = '<STR_LIT>' ) <EOL> J = J [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> diff = np . abs ( J - fake_jac ( ) [ '<STR_LIT>' ] ) <EOL> assert_rel_error ( self , diff . max ( ) , <NUM_LIT:0.0> , <NUM_LIT> ) <EOL> top . driver . clear_constraints ( ) <EOL> top . _pseudo_count = <NUM_LIT:0> <EOL> top . driver . add_constraint ( '<STR_LIT>' , jacs = fake_jac ) <EOL> top . _setup ( ) <EOL> top . run ( ) <EOL> J = top . driver . calc_gradient ( mode = '<STR_LIT>' , return_format = '<STR_LIT>' ) <EOL> J = J [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> diff = np . abs ( J - top . comp . J ) <EOL> assert_rel_error ( self , diff . max ( ) , <NUM_LIT:0.0> , <NUM_LIT> ) <EOL> J = top . driver . calc_gradient ( mode = '<STR_LIT>' , return_format = '<STR_LIT>' ) <EOL> J = J [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> diff = np . abs ( J - fake_jac ( ) [ '<STR_LIT>' ] ) <EOL> assert_rel_error ( self , diff . max ( ) , <NUM_LIT:0.0> , <NUM_LIT> ) <EOL> top . driver . clear_constraints ( ) <EOL> top . _pseudo_count = <NUM_LIT:0> <EOL> top . driver . add_constraint ( '<STR_LIT>' , jacs = fake_jac ) <EOL> top . _setup ( ) <EOL> top . run ( ) <EOL> J = top . driver . calc_gradient ( mode = '<STR_LIT>' , return_format = '<STR_LIT>' ) <EOL> J = J [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> diff = np . abs ( J - top . comp . J ) <EOL> assert_rel_error ( self , diff . max ( ) , <NUM_LIT:0.0> , <NUM_LIT> ) <EOL> J = top . driver . calc_gradient ( mode = '<STR_LIT>' , return_format = '<STR_LIT>' ) <EOL> J = J [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> diff = np . abs ( J - fake_jac ( ) [ '<STR_LIT>' ] ) <EOL> assert_rel_error ( self , diff . max ( ) , <NUM_LIT:0.0> , <NUM_LIT> ) <EOL> top . driver . clear_constraints ( ) <EOL> top . _pseudo_count = <NUM_LIT:0> <EOL> top . driver . add_constraint ( '<STR_LIT>' , jacs = fake_jac ) <EOL> top . driver . gradient_options . lin_solver = '<STR_LIT>' <EOL> top . _setup ( ) <EOL> top . run ( ) <EOL> J = top . driver . calc_gradient ( mode = '<STR_LIT>' , return_format = '<STR_LIT>' ) <EOL> J = J [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> diff = np . abs ( J - top . comp . J ) <EOL> assert_rel_error ( self , diff . max ( ) , <NUM_LIT:0.0> , <NUM_LIT> ) <EOL> J = top . driver . calc_gradient ( mode = '<STR_LIT>' , return_format = '<STR_LIT>' ) <EOL> J = J [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> diff = np . abs ( J - fake_jac ( ) [ '<STR_LIT>' ] ) <EOL> assert_rel_error ( self , diff . max ( ) , <NUM_LIT:0.0> , <NUM_LIT> ) <EOL> def fake_jac2 ( ) : <EOL> """<STR_LIT>""" <EOL> jacs = { } <EOL> jacs [ '<STR_LIT>' ] = np . array ( [ [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] ) <EOL> return jacs <EOL> top . driver . clear_constraints ( ) <EOL> top . _pseudo_count = <NUM_LIT:0> <EOL> top . driver . add_constraint ( '<STR_LIT>' , jacs = fake_jac2 ) <EOL> top . _setup ( ) <EOL> top . run ( ) <EOL> J = top . driver . calc_gradient ( mode = '<STR_LIT>' , return_format = '<STR_LIT>' ) <EOL> J = J [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> diff = np . abs ( J - top . comp . J ) <EOL> assert_rel_error ( self , diff . max ( ) , <NUM_LIT:0.0> , <NUM_LIT> ) <EOL> J = top . driver . calc_gradient ( mode = '<STR_LIT>' , return_format = '<STR_LIT>' ) <EOL> J = J [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> J_abs = np . abs ( J ) <EOL> assert_rel_error ( self , J_abs . max ( ) , <NUM_LIT:0.0> , <NUM_LIT> ) <EOL> class Has2SidedConstraintsTestCase ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . asm = set_as_top ( Assembly ( ) ) <EOL> self . asm . add ( '<STR_LIT>' , Simple ( ) ) <EOL> self . asm . add ( '<STR_LIT>' , Simple ( ) ) <EOL> self . asm . add ( '<STR_LIT>' , SimpleUnits ( ) ) <EOL> self . asm . add ( '<STR_LIT>' , SimpleUnits ( ) ) <EOL> def test_unsupported ( self ) : <EOL> drv = self . asm . add ( '<STR_LIT>' , MyDriver ( ) ) <EOL> self . asm . run ( ) <EOL> try : <EOL> drv . add_constraint ( '<STR_LIT>' ) <EOL> except AttributeError as err : <EOL> self . assertEqual ( str ( err ) , "<STR_LIT>" ) <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> def test_get_2sided_constraints ( self ) : <EOL> drv = self . asm . add ( '<STR_LIT>' , My2SDriver ( ) ) <EOL> drv . add_constraint ( '<STR_LIT>' ) <EOL> drv . add_constraint ( '<STR_LIT>' ) <EOL> self . asm . run ( ) <EOL> cons = drv . get_2sided_constraints ( ) <EOL> self . assertTrue ( len ( cons ) == <NUM_LIT:2> ) <EOL> con1 = cons [ '<STR_LIT>' ] <EOL> self . assertEqual ( self . asm . comp1 . a , con1 . evaluate ( self . asm ) [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( con1 . low , - <NUM_LIT> ) <EOL> self . assertEqual ( con1 . high , <NUM_LIT> ) <EOL> con1 = cons [ '<STR_LIT>' ] <EOL> self . assertEqual ( self . asm . comp1 . c , con1 . evaluate ( self . asm ) [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( con1 . low , <NUM_LIT> ) <EOL> self . assertEqual ( con1 . high , <NUM_LIT> ) <EOL> cons = drv . get_constraints ( ) <EOL> self . assertTrue ( len ( cons ) == <NUM_LIT:0> ) <EOL> def test_list_constraints ( self ) : <EOL> drv = self . asm . add ( '<STR_LIT>' , My2SDriver ( ) ) <EOL> drv . add_constraint ( '<STR_LIT>' ) <EOL> drv . add_constraint ( '<STR_LIT>' ) <EOL> self . asm . run ( ) <EOL> cons = drv . list_constraints ( ) <EOL> self . assertTrue ( '<STR_LIT>' in cons ) <EOL> self . assertTrue ( '<STR_LIT>' in cons ) <EOL> def test_gradient ( self ) : <EOL> drv = self . asm . add ( '<STR_LIT>' , My2SDriver ( ) ) <EOL> drv . add_constraint ( '<STR_LIT>' ) <EOL> drv . add_constraint ( '<STR_LIT>' ) <EOL> drv . add_constraint ( '<STR_LIT>' ) <EOL> drv . add_constraint ( '<STR_LIT>' ) <EOL> self . asm . run ( ) <EOL> J = drv . calc_gradient ( inputs = [ '<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:1> ] [ <NUM_LIT:0> ] , <NUM_LIT:1.0> , <NUM_LIT> ) <EOL> assert_rel_error ( self , J [ <NUM_LIT:2> ] [ <NUM_LIT:0> ] , <NUM_LIT:1.0> , <NUM_LIT> ) <EOL> assert_rel_error ( self , J [ <NUM_LIT:3> ] [ <NUM_LIT:0> ] , <NUM_LIT> , <NUM_LIT> ) <EOL> def test_replace ( self ) : <EOL> drv = self . asm . add ( '<STR_LIT>' , My2SDriver ( ) ) <EOL> drv . add_constraint ( '<STR_LIT>' ) <EOL> drv . add_constraint ( '<STR_LIT>' ) <EOL> self . asm . run ( ) <EOL> self . asm . replace ( '<STR_LIT>' , My2SDriver ( ) ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> unittest . main ( ) </s>
