<s> import unittest <EOL> import numpy as np <EOL> from six import iteritems <EOL> from collections import OrderedDict <EOL> from openmdao . core . vec_wrapper import SrcVecWrapper , TgtVecWrapper <EOL> from openmdao . core . system import System , _SysData <EOL> from openmdao . core . problem import _ProbData <EOL> pbd = _ProbData ( ) <EOL> class TestVecWrapper ( unittest . TestCase ) : <EOL> def test_vecwrapper ( self ) : <EOL> unknowns_dict = OrderedDict ( ) <EOL> unknowns_dict [ '<STR_LIT>' ] = { '<STR_LIT>' : ( <NUM_LIT:3> , <NUM_LIT:2> ) , '<STR_LIT:size>' : <NUM_LIT:6> , '<STR_LIT>' : np . ones ( ( <NUM_LIT:3> , <NUM_LIT:2> ) ) } <EOL> unknowns_dict [ '<STR_LIT>' ] = { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT:size>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT> } <EOL> unknowns_dict [ '<STR_LIT>' ] = { '<STR_LIT:size>' : <NUM_LIT:0> , '<STR_LIT>' : "<STR_LIT:foo>" , '<STR_LIT>' : True } <EOL> unknowns_dict [ '<STR_LIT>' ] = { '<STR_LIT>' : ( <NUM_LIT:2> , <NUM_LIT:1> ) , '<STR_LIT:size>' : <NUM_LIT:2> , '<STR_LIT>' : np . zeros ( ( <NUM_LIT:2> , <NUM_LIT:1> ) ) , } <EOL> unknowns_dict [ '<STR_LIT>' ] = { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT:size>' : <NUM_LIT:1> , '<STR_LIT>' : - <NUM_LIT:1.0> , '<STR_LIT:state>' : True , } <EOL> sd = _SysData ( '<STR_LIT>' ) <EOL> for u , meta in unknowns_dict . items ( ) : <EOL> meta [ '<STR_LIT>' ] = u <EOL> meta [ '<STR_LIT>' ] = u <EOL> sd . to_prom_name [ u ] = u <EOL> u = SrcVecWrapper ( sd , pbd ) <EOL> u . setup ( unknowns_dict , store_byobjs = True ) <EOL> self . assertEqual ( u . vec . size , <NUM_LIT:10> ) <EOL> self . assertEqual ( len ( u ) , <NUM_LIT:5> ) <EOL> self . assertEqual ( list ( u . keys ( ) ) , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertTrue ( np . all ( u [ '<STR_LIT>' ] == np . ones ( ( <NUM_LIT:3> , <NUM_LIT:2> ) ) ) ) <EOL> self . assertEqual ( u [ '<STR_LIT>' ] , <NUM_LIT> ) <EOL> self . assertEqual ( u [ '<STR_LIT>' ] , '<STR_LIT:foo>' ) <EOL> self . assertTrue ( np . all ( u [ '<STR_LIT>' ] == np . zeros ( ( <NUM_LIT:2> , <NUM_LIT:1> ) ) ) ) <EOL> self . assertEqual ( u [ '<STR_LIT>' ] , - <NUM_LIT:1.0> ) <EOL> self . assertEqual ( [ t [ <NUM_LIT:0> ] for t in u . vec_val_iter ( ) ] , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> u [ '<STR_LIT>' ] = np . ones ( ( <NUM_LIT:3> , <NUM_LIT:2> ) ) * <NUM_LIT> <EOL> u [ '<STR_LIT>' ] = <NUM_LIT> <EOL> u [ '<STR_LIT>' ] = '<STR_LIT:bar>' <EOL> u [ '<STR_LIT>' ] = np . ones ( ( <NUM_LIT:2> , <NUM_LIT:1> ) ) * <NUM_LIT> <EOL> u [ '<STR_LIT>' ] = <NUM_LIT> <EOL> self . assertTrue ( np . all ( u [ '<STR_LIT>' ] == np . ones ( ( <NUM_LIT:3> , <NUM_LIT:2> ) ) * <NUM_LIT> ) ) <EOL> self . assertTrue ( np . all ( u [ '<STR_LIT>' ] == np . ones ( ( <NUM_LIT:2> , <NUM_LIT:1> ) ) * <NUM_LIT> ) ) <EOL> self . assertEqual ( u [ '<STR_LIT>' ] , <NUM_LIT> ) <EOL> self . assertEqual ( u [ '<STR_LIT>' ] , '<STR_LIT:bar>' ) <EOL> self . assertEqual ( u [ '<STR_LIT>' ] , <NUM_LIT> ) <EOL> try : <EOL> u [ '<STR_LIT>' ] = np . ones ( ( <NUM_LIT:3> , <NUM_LIT:3> ) ) <EOL> except Exception as err : <EOL> self . assertEqual ( str ( err ) , <EOL> "<STR_LIT>" ) <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> params = OrderedDict ( ) <EOL> params [ '<STR_LIT>' ] = { '<STR_LIT>' : ( <NUM_LIT:3> , <NUM_LIT:2> ) , '<STR_LIT:size>' : <NUM_LIT:6> , '<STR_LIT>' : np . ones ( ( <NUM_LIT:3> , <NUM_LIT:2> ) ) } <EOL> params [ '<STR_LIT>' ] = { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT:size>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT> } <EOL> params [ '<STR_LIT>' ] = { '<STR_LIT:size>' : <NUM_LIT:0> , '<STR_LIT>' : "<STR_LIT:foo>" } <EOL> params [ '<STR_LIT>' ] = { '<STR_LIT>' : ( <NUM_LIT:2> , <NUM_LIT:1> ) , '<STR_LIT:size>' : <NUM_LIT:6> , '<STR_LIT>' : np . zeros ( ( <NUM_LIT:2> , <NUM_LIT:1> ) ) } <EOL> for p , meta in params . items ( ) : <EOL> meta [ '<STR_LIT>' ] = p <EOL> meta [ '<STR_LIT>' ] = p <EOL> sd . to_prom_name [ u ] = u <EOL> connections = { } <EOL> for p in params : <EOL> connections [ p ] = ( p , None ) <EOL> s = _SysData ( '<STR_LIT>' ) <EOL> s . _unknowns_dict = u . _dat <EOL> p = TgtVecWrapper ( s , pbd ) <EOL> p . setup ( None , params , u , params . keys ( ) , <EOL> connections , store_byobjs = True ) <EOL> self . assertEqual ( p . vec . size , <NUM_LIT:9> ) <EOL> self . assertEqual ( len ( p ) , <NUM_LIT:4> ) <EOL> self . assertEqual ( list ( p . keys ( ) ) , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertTrue ( np . all ( p [ '<STR_LIT>' ] == np . zeros ( ( <NUM_LIT:3> , <NUM_LIT:2> ) ) ) ) <EOL> self . assertEqual ( p [ '<STR_LIT>' ] , <NUM_LIT:0.> ) <EOL> self . assertEqual ( p [ '<STR_LIT>' ] , '<STR_LIT:bar>' ) <EOL> self . assertTrue ( np . all ( p [ '<STR_LIT>' ] == np . zeros ( ( <NUM_LIT:2> , <NUM_LIT:1> ) ) ) ) <EOL> p [ '<STR_LIT>' ] = np . ones ( ( <NUM_LIT:3> , <NUM_LIT:2> ) ) * <NUM_LIT> <EOL> self . assertTrue ( np . all ( p [ '<STR_LIT>' ] == np . ones ( ( <NUM_LIT:3> , <NUM_LIT:2> ) ) * <NUM_LIT> ) ) <EOL> def test_view ( self ) : <EOL> unknowns_dict = OrderedDict ( ) <EOL> unknowns_dict [ '<STR_LIT>' ] = { '<STR_LIT>' : ( <NUM_LIT:3> , <NUM_LIT:2> ) , '<STR_LIT:size>' : <NUM_LIT:6> , '<STR_LIT>' : np . ones ( ( <NUM_LIT:3> , <NUM_LIT:2> ) ) } <EOL> unknowns_dict [ '<STR_LIT>' ] = { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT:size>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT> } <EOL> unknowns_dict [ '<STR_LIT>' ] = { '<STR_LIT:size>' : <NUM_LIT:0> , '<STR_LIT>' : "<STR_LIT:foo>" , '<STR_LIT>' : True } <EOL> unknowns_dict [ '<STR_LIT>' ] = { '<STR_LIT>' : ( <NUM_LIT:2> , <NUM_LIT:1> ) , '<STR_LIT>' : np . zeros ( ( <NUM_LIT:2> , <NUM_LIT:1> ) ) , '<STR_LIT:size>' : <NUM_LIT:2> , } <EOL> unknowns_dict [ '<STR_LIT>' ] = { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT:size>' : <NUM_LIT:1> , '<STR_LIT>' : - <NUM_LIT:1.0> , '<STR_LIT:state>' : True , } <EOL> sd = _SysData ( '<STR_LIT>' ) <EOL> for u , meta in unknowns_dict . items ( ) : <EOL> meta [ '<STR_LIT>' ] = u <EOL> meta [ '<STR_LIT>' ] = u <EOL> sd . to_prom_name [ u ] = u <EOL> u = SrcVecWrapper ( sd , pbd ) <EOL> u . setup ( unknowns_dict , store_byobjs = True ) <EOL> varmap = OrderedDict ( [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ] ) <EOL> s = System ( ) <EOL> s . _sysdata = _SysData ( '<STR_LIT>' ) <EOL> s . _probdata = pbd <EOL> uview = u . get_view ( s , None , varmap ) <EOL> self . assertEqual ( list ( uview . keys ( ) ) , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> uview [ '<STR_LIT>' ] = <NUM_LIT> <EOL> uview [ '<STR_LIT>' ] = '<STR_LIT:bar>' <EOL> self . assertEqual ( uview [ '<STR_LIT>' ] , <NUM_LIT> ) <EOL> self . assertEqual ( u [ '<STR_LIT>' ] , <NUM_LIT> ) <EOL> self . assertEqual ( uview [ '<STR_LIT>' ] , '<STR_LIT:bar>' ) <EOL> self . assertEqual ( u [ '<STR_LIT>' ] , '<STR_LIT:bar>' ) <EOL> uview2 = u . get_view ( s , None , { } ) <EOL> self . assertEqual ( list ( uview2 . keys ( ) ) , [ ] ) <EOL> def test_flat ( self ) : <EOL> unknowns_dict = OrderedDict ( ) <EOL> unknowns_dict [ '<STR_LIT>' ] = { '<STR_LIT>' : ( <NUM_LIT:3> , <NUM_LIT:2> ) , '<STR_LIT:size>' : <NUM_LIT:6> , '<STR_LIT>' : np . ones ( ( <NUM_LIT:3> , <NUM_LIT:2> ) ) } <EOL> unknowns_dict [ '<STR_LIT>' ] = { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT:size>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT> } <EOL> unknowns_dict [ '<STR_LIT>' ] = { '<STR_LIT:size>' : <NUM_LIT:0> , '<STR_LIT>' : "<STR_LIT:foo>" , '<STR_LIT>' : True } <EOL> unknowns_dict [ '<STR_LIT>' ] = { '<STR_LIT>' : ( <NUM_LIT:2> , <NUM_LIT:1> ) , '<STR_LIT:size>' : <NUM_LIT:2> , '<STR_LIT>' : np . zeros ( ( <NUM_LIT:2> , <NUM_LIT:1> ) ) , } <EOL> unknowns_dict [ '<STR_LIT>' ] = { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT:size>' : <NUM_LIT:1> , '<STR_LIT>' : - <NUM_LIT:1.0> , '<STR_LIT:state>' : True , } <EOL> sd = _SysData ( '<STR_LIT>' ) <EOL> for u , meta in unknowns_dict . items ( ) : <EOL> meta [ '<STR_LIT>' ] = u <EOL> meta [ '<STR_LIT>' ] = u <EOL> sd . to_prom_name [ u ] = u <EOL> u = SrcVecWrapper ( sd , pbd ) <EOL> u . setup ( unknowns_dict , store_byobjs = True ) <EOL> self . assertTrue ( ( np . array ( u . _dat [ '<STR_LIT>' ] . val ) == np . array ( [ <NUM_LIT:1.> , <NUM_LIT:1.> , <NUM_LIT:1.> , <NUM_LIT:1.> , <NUM_LIT:1.> , <NUM_LIT:1.> ] ) ) . all ( ) ) <EOL> self . assertTrue ( ( np . array ( u . _dat [ '<STR_LIT>' ] . val ) == np . array ( [ <NUM_LIT> ] ) ) . all ( ) ) <EOL> def test_norm ( self ) : <EOL> unknowns_dict = OrderedDict ( ) <EOL> unknowns_dict [ '<STR_LIT>' ] = { '<STR_LIT>' : ( <NUM_LIT:2> , <NUM_LIT:1> ) , '<STR_LIT:size>' : <NUM_LIT:2> , '<STR_LIT>' : np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) } <EOL> unknowns_dict [ '<STR_LIT>' ] = { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT:size>' : <NUM_LIT:1> , '<STR_LIT>' : - <NUM_LIT> } <EOL> sd = _SysData ( '<STR_LIT>' ) <EOL> for u , meta in unknowns_dict . items ( ) : <EOL> meta [ '<STR_LIT>' ] = u <EOL> meta [ '<STR_LIT>' ] = u <EOL> sd . to_prom_name [ u ] = u <EOL> u = SrcVecWrapper ( sd , pbd ) <EOL> u . setup ( unknowns_dict , store_byobjs = True ) <EOL> unorm = u . norm ( ) <EOL> self . assertAlmostEqual ( unorm , np . linalg . norm ( np . array ( [ <NUM_LIT> , <NUM_LIT> , - <NUM_LIT> ] ) ) ) <EOL> def test_bad_get_unknown ( self ) : <EOL> unknowns_dict = OrderedDict ( ) <EOL> unknowns_dict [ '<STR_LIT>' ] = { '<STR_LIT>' : ( <NUM_LIT:3> , <NUM_LIT:2> ) , '<STR_LIT:size>' : <NUM_LIT:6> , '<STR_LIT>' : np . ones ( ( <NUM_LIT:3> , <NUM_LIT:2> ) ) } <EOL> sd = _SysData ( '<STR_LIT>' ) <EOL> for u , meta in unknowns_dict . items ( ) : <EOL> meta [ '<STR_LIT>' ] = u <EOL> meta [ '<STR_LIT>' ] = u <EOL> sd . to_prom_name [ u ] = u <EOL> u = SrcVecWrapper ( sd , pbd ) <EOL> u . setup ( unknowns_dict , store_byobjs = True ) <EOL> try : <EOL> u [ '<STR_LIT>' ] <EOL> except KeyError as err : <EOL> self . assertEqual ( str ( err ) , "<STR_LIT>" ) <EOL> else : <EOL> self . fail ( '<STR_LIT>' ) <EOL> def test_bad_set_unknown ( self ) : <EOL> unknowns_dict = OrderedDict ( ) <EOL> unknowns_dict [ '<STR_LIT>' ] = { '<STR_LIT>' : ( <NUM_LIT:3> , <NUM_LIT:2> ) , '<STR_LIT:size>' : <NUM_LIT:6> , '<STR_LIT>' : np . ones ( ( <NUM_LIT:3> , <NUM_LIT:2> ) ) } <EOL> sd = _SysData ( '<STR_LIT>' ) <EOL> for u , meta in unknowns_dict . items ( ) : <EOL> meta [ '<STR_LIT>' ] = u <EOL> meta [ '<STR_LIT>' ] = u <EOL> sd . to_prom_name [ u ] = u <EOL> u = SrcVecWrapper ( sd , pbd ) <EOL> u . setup ( unknowns_dict , store_byobjs = True ) <EOL> try : <EOL> u [ '<STR_LIT>' ] = np . zeros ( ( <NUM_LIT:3> , <NUM_LIT:2> ) ) <EOL> except KeyError as err : <EOL> self . assertEqual ( str ( err ) , "<STR_LIT>" ) <EOL> else : <EOL> self . fail ( '<STR_LIT>' ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> unittest . main ( ) </s>
