<s> import struct <EOL> import string <EOL> import numpy as np <EOL> from stl import ASCII_FACET , BINARY_HEADER , BINARY_FACET <EOL> from ffd_axisymetric import Body , Shell <EOL> from openmdao . main . api import Component , VariableTree <EOL> from openmdao . lib . datatypes . api import VarTree , Array <EOL> from openmdao . lib . geometry . geom_data import GeomData <EOL> def _block_diag ( arrays ) : <EOL> """<STR_LIT>""" <EOL> result = None <EOL> for arr in arrays : <EOL> arr [ arr == - <NUM_LIT:0> ] = <NUM_LIT:0> <EOL> if result is None : <EOL> result = arr <EOL> else : <EOL> r_rows , r_cols = result . shape <EOL> a_rows , a_cols = arr . shape <EOL> result = np . vstack ( ( np . hstack ( ( result , np . zeros ( ( r_rows , a_cols ) ) ) ) , <EOL> np . hstack ( ( np . zeros ( ( a_rows , r_cols ) ) , arr ) ) ) ) <EOL> return result <EOL> class STLGroup ( Component ) : <EOL> def __init__ ( self , geom_parts ) : <EOL> super ( STLGroup , self ) . __init__ ( ) <EOL> self . _comps = [ ] <EOL> self . _i_comps = { } <EOL> self . _n_comps = <NUM_LIT:0> <EOL> for name , comp in geom_parts : <EOL> comp . name = name <EOL> self . _i_comps [ name ] = self . _n_comps <EOL> self . _comps . append ( comp ) <EOL> self . _n_comps += <NUM_LIT:1> <EOL> self . add ( name , VarTree ( VariableTree ( ) , iotype = "<STR_LIT>" , desc = "<STR_LIT>" % name ) ) <EOL> io = self . _build_io ( ) <EOL> for ( comp_name , var_name ) , meta in io : <EOL> comp = self . get ( comp_name ) <EOL> val = meta [ '<STR_LIT:value>' ] <EOL> del meta [ '<STR_LIT:value>' ] <EOL> comp . add ( var_name , Array ( val , ** meta ) ) <EOL> n_points = self . points . shape [ <NUM_LIT:0> ] <EOL> n_tria = self . triangles . shape [ <NUM_LIT:0> ] <EOL> self . add ( '<STR_LIT>' , VarTree ( GeomData ( n_points , n_tria ) , iotype = "<STR_LIT>" , desc = "<STR_LIT>" ) ) <EOL> self . geom_data . points = self . points <EOL> self . geom_data . facets = self . triangles <EOL> self . _needs_linerize = True <EOL> def _build_io ( self ) : <EOL> """<STR_LIT>""" <EOL> self . comp_param_count = { } <EOL> params = [ ] <EOL> for comp in self . _comps : <EOL> name = comp . name <EOL> if isinstance ( comp , Body ) : <EOL> val = comp . delta_C [ : , <NUM_LIT:0> ] <EOL> meta = { '<STR_LIT:value>' : val , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : val . shape , <EOL> '<STR_LIT>' : "<STR_LIT>" } <EOL> tup = ( ( name , '<STR_LIT:X>' ) , meta ) <EOL> params . append ( tup ) <EOL> n_X = val . shape [ <NUM_LIT:0> ] <EOL> val = comp . delta_C [ : , <NUM_LIT:1> ] <EOL> meta = { '<STR_LIT:value>' : val , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : val . shape , <EOL> '<STR_LIT>' : "<STR_LIT>" } <EOL> tup = ( ( name , '<STR_LIT:R>' ) , meta ) <EOL> params . append ( tup ) <EOL> n_R = val . shape [ <NUM_LIT:0> ] <EOL> self . comp_param_count [ comp ] = ( n_X , n_R ) <EOL> else : <EOL> val = comp . delta_Cc [ : , <NUM_LIT:0> ] <EOL> meta = { '<STR_LIT:value>' : val , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : val . shape , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> tup = ( ( name , '<STR_LIT:X>' ) , meta ) <EOL> params . append ( tup ) <EOL> n_X = val . shape [ <NUM_LIT:0> ] <EOL> val = comp . delta_Cc [ : , <NUM_LIT:1> ] <EOL> meta = { '<STR_LIT:value>' : val , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : val . shape , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> tup = ( ( name , '<STR_LIT:R>' ) , meta ) <EOL> params . append ( tup ) <EOL> n_R = val . shape [ <NUM_LIT:0> ] <EOL> val = comp . delta_Ct [ : , <NUM_LIT:1> ] <EOL> meta = { '<STR_LIT:value>' : val , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : val . shape , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> tup = ( ( name , '<STR_LIT>' ) , meta ) <EOL> params . append ( tup ) <EOL> n_T = val . shape [ <NUM_LIT:0> ] <EOL> self . comp_param_count [ comp ] = ( n_X , n_R , n_T ) <EOL> points = [ ] <EOL> triangles = [ ] <EOL> i_offset = <NUM_LIT:0> <EOL> n_controls = <NUM_LIT:0> <EOL> for comp in self . _comps : <EOL> n_controls += sum ( self . comp_param_count [ comp ] ) <EOL> if isinstance ( comp , Body ) : <EOL> points . extend ( comp . stl . points ) <EOL> size = len ( points ) <EOL> triangles . extend ( comp . stl . triangles + i_offset ) <EOL> i_offset = size <EOL> else : <EOL> points . extend ( comp . outer_stl . points ) <EOL> size = len ( points ) <EOL> triangles . extend ( comp . outer_stl . triangles + i_offset ) <EOL> i_offset = size <EOL> points . extend ( comp . inner_stl . points ) <EOL> size = len ( points ) <EOL> triangles . extend ( comp . inner_stl . triangles + i_offset ) <EOL> i_offset = size <EOL> self . points = np . array ( points ) <EOL> self . n_controls = n_controls <EOL> self . n_points = len ( points ) <EOL> self . triangles = np . array ( triangles ) <EOL> self . n_triangles = len ( triangles ) <EOL> return params <EOL> def deform ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> for name , delta_C in kwargs . iteritems ( ) : <EOL> i = self . _i_comps [ name ] <EOL> comp = self . _comps [ i ] <EOL> if isinstance ( comp , Body ) : <EOL> comp . deform ( delta_C ) <EOL> else : <EOL> comp . deform ( * delta_C ) <EOL> self . list_parameters ( ) <EOL> def _build_ascii_stl ( self , facets ) : <EOL> """<STR_LIT>""" <EOL> lines = [ '<STR_LIT>' , ] <EOL> for facet in facets : <EOL> lines . append ( ASCII_FACET . format ( face = facet ) ) <EOL> lines . append ( '<STR_LIT>' ) <EOL> return lines <EOL> def _build_binary_stl ( self , facets ) : <EOL> """<STR_LIT>""" <EOL> lines = [ struct . pack ( BINARY_HEADER , b'<STR_LIT>' , len ( facets ) ) , ] <EOL> for facet in facets : <EOL> facet = list ( facet ) <EOL> facet . append ( <NUM_LIT:0> ) <EOL> lines . append ( struct . pack ( BINARY_FACET , * facet ) ) <EOL> return lines <EOL> def writeSTL ( self , file_name , ascii = False ) : <EOL> """<STR_LIT>""" <EOL> facets = [ ] <EOL> for comp in self . _comps : <EOL> if isinstance ( comp , Body ) : <EOL> facets . extend ( comp . stl . get_facets ( ) ) <EOL> else : <EOL> facets . extend ( comp . outer_stl . get_facets ( ) ) <EOL> facets . extend ( comp . inner_stl . get_facets ( ) ) <EOL> f = open ( file_name , '<STR_LIT:w>' ) <EOL> if ascii : <EOL> lines = self . _build_ascii_stl ( facets ) <EOL> f . write ( "<STR_LIT:\n>" . join ( lines ) ) <EOL> else : <EOL> data = self . _build_binary_stl ( facets ) <EOL> f . write ( "<STR_LIT>" . join ( data ) ) <EOL> f . close ( ) <EOL> def writeFEPOINT ( self , stream ) : <EOL> """<STR_LIT>""" <EOL> self . provideJ ( ) <EOL> lines = [ '<STR_LIT>' , ] <EOL> var_line = '<STR_LIT>' <EOL> deriv_X_names = [ ] <EOL> deriv_R_names = [ ] <EOL> deriv_T_names = [ ] <EOL> deriv_tmpl = string . Template ( '<STR_LIT>' ) <EOL> for comp in self . _comps : <EOL> if isinstance ( comp , Body ) : <EOL> deriv_X_names . extend ( [ deriv_tmpl . substitute ( { '<STR_LIT:name>' : comp . name , '<STR_LIT:i>' : str ( i ) , '<STR_LIT:type>' : '<STR_LIT:X>' } ) for i in xrange ( <NUM_LIT:0> , comp . n_controls ) ] ) <EOL> deriv_R_names . extend ( [ deriv_tmpl . substitute ( { '<STR_LIT:name>' : comp . name , '<STR_LIT:i>' : str ( i ) , '<STR_LIT:type>' : '<STR_LIT:R>' } ) for i in xrange ( <NUM_LIT:0> , comp . n_controls ) ] ) <EOL> else : <EOL> deriv_X_names . extend ( [ deriv_tmpl . substitute ( { '<STR_LIT:name>' : comp . name , '<STR_LIT:i>' : str ( i ) , '<STR_LIT:type>' : '<STR_LIT:X>' } ) for i in xrange ( <NUM_LIT:0> , comp . n_c_controls ) ] ) <EOL> deriv_R_names . extend ( [ deriv_tmpl . substitute ( { '<STR_LIT:name>' : comp . name , '<STR_LIT:i>' : str ( i ) , '<STR_LIT:type>' : '<STR_LIT:R>' } ) for i in xrange ( <NUM_LIT:0> , comp . n_c_controls ) ] ) <EOL> deriv_T_names . extend ( [ deriv_tmpl . substitute ( { '<STR_LIT:name>' : comp . name , '<STR_LIT:i>' : str ( i ) , '<STR_LIT:type>' : '<STR_LIT:T>' } ) for i in xrange ( <NUM_LIT:0> , comp . n_t_controls ) ] ) <EOL> var_line += "<STR_LIT:U+0020>" . join ( deriv_X_names ) <EOL> var_line += "<STR_LIT:U+0020>" . join ( deriv_R_names ) <EOL> var_line += "<STR_LIT:U+0020>" . join ( deriv_T_names ) <EOL> lines . append ( var_line ) <EOL> lines . append ( '<STR_LIT>' % ( self . n_points , self . n_triangles ) ) <EOL> nx = <NUM_LIT:3> * self . dXqdC . shape [ <NUM_LIT:1> ] <EOL> nr = <NUM_LIT:3> * self . dYqdCr . shape [ <NUM_LIT:1> ] <EOL> nt = <NUM_LIT:3> * self . dYqdCt . shape [ <NUM_LIT:1> ] <EOL> j_cols = ( nx + nr + nt ) <EOL> for i , p in enumerate ( self . points ) : <EOL> line = "<STR_LIT>" % ( p [ <NUM_LIT:0> ] , p [ <NUM_LIT:1> ] , p [ <NUM_LIT:2> ] , i + <NUM_LIT:1> ) <EOL> deriv_values = np . zeros ( ( j_cols , ) ) <EOL> deriv_values [ : nx : <NUM_LIT:3> ] = self . dXqdC [ i ] <EOL> deriv_values [ nx + <NUM_LIT:1> : nx + nr : <NUM_LIT:3> ] = self . dYqdCr [ i ] <EOL> deriv_values [ nx + <NUM_LIT:2> : nx + nr : <NUM_LIT:3> ] = self . dZqdCr [ i ] <EOL> deriv_values [ nx + nr + <NUM_LIT:1> : : <NUM_LIT:3> ] = self . dYqdCt [ i ] <EOL> deriv_values [ nx + nr + <NUM_LIT:2> : : <NUM_LIT:3> ] = self . dZqdCt [ i ] <EOL> line += "<STR_LIT:U+0020>" . join ( np . char . mod ( '<STR_LIT>' , deriv_values ) ) <EOL> lines . append ( line ) <EOL> for tri in self . triangles : <EOL> line = "<STR_LIT>" % ( tri [ <NUM_LIT:0> ] + <NUM_LIT:1> , tri [ <NUM_LIT:1> ] + <NUM_LIT:1> , tri [ <NUM_LIT:2> ] + <NUM_LIT:1> , tri [ <NUM_LIT:2> ] + <NUM_LIT:1> ) <EOL> lines . append ( line ) <EOL> needs_close = False <EOL> if isinstance ( stream , basestring ) : <EOL> stream = open ( stream , '<STR_LIT:w>' ) <EOL> needs_close = True <EOL> print >> stream , "<STR_LIT:\n>" . join ( lines ) <EOL> if ( needs_close ) : <EOL> stream . close ( ) <EOL> def project_profile ( self ) : <EOL> self . provideJ ( ) <EOL> point_sets = [ ] <EOL> for comp in self . _comps : <EOL> if isinstance ( comp , Body ) : <EOL> p = comp . stl . points <EOL> indecies = np . logical_and ( abs ( p [ : , <NUM_LIT:2> ] ) < <NUM_LIT> , p [ : , <NUM_LIT:1> ] > <NUM_LIT:0> ) <EOL> points = p [ indecies ] <EOL> points = points [ points [ : , <NUM_LIT:0> ] . argsort ( ) ] <EOL> point_sets . append ( points ) <EOL> else : <EOL> p = comp . outer_stl . points <EOL> indecies = np . logical_and ( abs ( p [ : , <NUM_LIT:2> ] ) < <NUM_LIT> , p [ : , <NUM_LIT:1> ] > <NUM_LIT:0> ) <EOL> points = p [ indecies ] <EOL> points = points [ points [ : , <NUM_LIT:0> ] . argsort ( ) ] <EOL> point_sets . append ( points ) <EOL> p = comp . inner_stl . points <EOL> indecies = np . logical_and ( abs ( p [ : , <NUM_LIT:2> ] ) < <NUM_LIT> , p [ : , <NUM_LIT:1> ] > <NUM_LIT:0> ) <EOL> points = p [ indecies ] <EOL> points = points [ points [ : , <NUM_LIT:0> ] . argsort ( ) ] <EOL> point_sets . append ( points ) <EOL> return point_sets <EOL> def list_deriv_vars ( self ) : <EOL> outs = [ ] <EOL> ins = [ ] <EOL> for comp in self . _comps : <EOL> ins . append ( '<STR_LIT>' % comp . name ) <EOL> ins . append ( '<STR_LIT>' % comp . name ) <EOL> if isinstance ( comp , Shell ) : <EOL> ins . append ( '<STR_LIT>' % comp . name ) <EOL> return ins , outs <EOL> def provideJ ( self ) : <EOL> if not self . _needs_linerize : <EOL> return <EOL> self . _build_io ( ) <EOL> param_J_offset_map = { } <EOL> jx = [ ] <EOL> jyr = [ ] <EOL> jzr = [ ] <EOL> jyt = [ ] <EOL> jzt = [ ] <EOL> x_offset = <NUM_LIT:0> <EOL> yz_offset = <NUM_LIT:0> <EOL> t_offset = <NUM_LIT:0> <EOL> for comp in self . _comps : <EOL> if isinstance ( comp , Body ) : <EOL> jx . append ( comp . dXqdC ) <EOL> param_name = "<STR_LIT>" % comp . name <EOL> param_J_offset_map [ param_name ] = x_offset <EOL> nCx = self . comp_param_count [ comp ] [ <NUM_LIT:0> ] <EOL> x_offset += nCx <EOL> jyr . append ( comp . dYqdC ) <EOL> jzr . append ( comp . dZqdC ) <EOL> param_name = "<STR_LIT>" % comp . name <EOL> param_J_offset_map [ param_name ] = yz_offset <EOL> nCr = self . comp_param_count [ comp ] [ <NUM_LIT:1> ] <EOL> yz_offset += nCr <EOL> shape = comp . dXqdC . shape <EOL> param_name = "<STR_LIT>" % comp . name <EOL> jyt . append ( np . zeros ( ( shape [ <NUM_LIT:0> ] , <NUM_LIT:1> ) ) ) <EOL> jzt . append ( np . zeros ( ( shape [ <NUM_LIT:0> ] , <NUM_LIT:1> ) ) ) <EOL> param_J_offset_map [ param_name ] = t_offset <EOL> t_offset += <NUM_LIT:1> <EOL> else : <EOL> stackX = np . vstack ( ( comp . dXoqdCc , comp . dXiqdCc ) ) <EOL> jx . append ( stackX ) <EOL> param_name = "<STR_LIT>" % comp . name <EOL> param_J_offset_map [ param_name ] = x_offset <EOL> nCx = self . comp_param_count [ comp ] [ <NUM_LIT:0> ] <EOL> x_offset += nCx <EOL> stackY = np . vstack ( ( comp . dYoqdCc , comp . dYiqdCc ) ) <EOL> stackZ = np . vstack ( ( comp . dZoqdCc , comp . dZiqdCc ) ) <EOL> jyr . append ( stackY ) <EOL> jzr . append ( stackZ ) <EOL> param_name = "<STR_LIT>" % comp . name <EOL> param_J_offset_map [ param_name ] = yz_offset <EOL> nCr = self . comp_param_count [ comp ] [ <NUM_LIT:1> ] <EOL> yz_offset += nCr <EOL> stackY = np . vstack ( ( comp . dYoqdCt , comp . dYiqdCt ) ) <EOL> stackZ = np . vstack ( ( comp . dZoqdCt , comp . dZiqdCt ) ) <EOL> jyt . append ( stackY ) <EOL> jzt . append ( stackZ ) <EOL> param_name = "<STR_LIT>" % comp . name <EOL> param_J_offset_map [ param_name ] = t_offset <EOL> nCt = self . comp_param_count [ comp ] [ <NUM_LIT:2> ] <EOL> t_offset += nCt <EOL> self . dXqdC = _block_diag ( jx ) <EOL> self . dYqdCr = _block_diag ( jyr ) <EOL> self . dZqdCr = _block_diag ( jzr ) <EOL> self . dYqdCt = _block_diag ( jyt ) <EOL> self . dZqdCt = _block_diag ( jzt ) <EOL> self . param_J_map = { } <EOL> for comp in self . _comps : <EOL> param_name = "<STR_LIT>" % comp . name <EOL> offset = param_J_offset_map [ param_name ] <EOL> nCx = self . comp_param_count [ comp ] [ <NUM_LIT:0> ] <EOL> self . param_J_map [ param_name ] = ( self . dXqdC [ : , offset : offset + nCx ] , False , False ) <EOL> param_name = "<STR_LIT>" % comp . name <EOL> offset = param_J_offset_map [ param_name ] <EOL> nCr = self . comp_param_count [ comp ] [ <NUM_LIT:1> ] <EOL> self . param_J_map [ param_name ] = ( False , self . dYqdCr [ : , offset : offset + nCr ] , self . dZqdCr [ : , offset : offset + nCr ] ) <EOL> if isinstance ( comp , Shell ) : <EOL> param_name = "<STR_LIT>" % comp . name <EOL> offset = param_J_offset_map [ param_name ] <EOL> nCt = self . comp_param_count [ comp ] [ <NUM_LIT:2> ] <EOL> self . param_J_map [ param_name ] = ( False , self . dYqdCt [ : , offset : offset + nCt ] , self . dZqdCt [ : , offset : offset + nCt ] ) <EOL> for comp in self . _comps : <EOL> if isinstance ( comp , Body ) : <EOL> param_name = "<STR_LIT>" % comp . name <EOL> offset = param_J_offset_map [ param_name ] <EOL> self . dYqdCt = np . delete ( self . dYqdCt , offset , <NUM_LIT:1> ) <EOL> self . dZqdCt = np . delete ( self . dZqdCt , offset , <NUM_LIT:1> ) <EOL> self . _needs_linerize = False <EOL> def apply_deriv ( self , arg , result ) : <EOL> for name , value in arg . iteritems ( ) : <EOL> Jx , Jy , Jz = self . param_J_map [ name ] <EOL> if Jx is not False : <EOL> result [ '<STR_LIT>' ] [ : , <NUM_LIT:0> ] += Jx . dot ( value ) <EOL> if Jy is not False : <EOL> result [ '<STR_LIT>' ] [ : , <NUM_LIT:1> ] += Jy . dot ( value ) <EOL> result [ '<STR_LIT>' ] [ : , <NUM_LIT:2> ] += Jz . dot ( value ) <EOL> return result <EOL> def apply_derivT ( self , arg , result ) : <EOL> for name , value in result . iteritems ( ) : <EOL> Jx , Jy , Jz = self . param_J_map [ name ] <EOL> if Jx is not False : <EOL> result [ name ] += Jx . T . dot ( arg [ '<STR_LIT>' ] [ : , <NUM_LIT:0> ] ) <EOL> if Jy is not False : <EOL> result [ name ] += Jy . T . dot ( arg [ '<STR_LIT>' ] [ : , <NUM_LIT:1> ] ) <EOL> result [ name ] += Jz . T . dot ( arg [ '<STR_LIT>' ] [ : , <NUM_LIT:2> ] ) <EOL> return result <EOL> def execute ( self ) : <EOL> for comp in self . _comps : <EOL> if isinstance ( comp , Body ) : <EOL> delta_C_shape = comp . delta_C . shape <EOL> del_C = np . zeros ( delta_C_shape ) <EOL> del_C [ : , <NUM_LIT:0> ] = self . get ( '<STR_LIT>' % comp . name ) <EOL> del_C [ : , <NUM_LIT:1> ] = self . get ( '<STR_LIT>' % comp . name ) <EOL> comp . deform ( delta_C = del_C ) <EOL> else : <EOL> delta_Cc_shape = comp . delta_Cc . shape <EOL> del_Cc = np . zeros ( delta_Cc_shape ) <EOL> del_Cc [ : , <NUM_LIT:0> ] = self . get ( '<STR_LIT>' % comp . name ) <EOL> del_Cc [ : , <NUM_LIT:1> ] = self . get ( '<STR_LIT>' % comp . name ) <EOL> delta_Ct_shape = comp . delta_Ct . shape <EOL> del_Ct = np . zeros ( delta_Ct_shape ) <EOL> del_Ct [ : , <NUM_LIT:0> ] = self . get ( '<STR_LIT>' % comp . name ) <EOL> del_Ct [ : , <NUM_LIT:1> ] = self . get ( '<STR_LIT>' % comp . name ) <EOL> comp . deform ( delta_Cc = del_Cc , delta_Ct = del_Ct ) <EOL> self . _build_io ( ) <EOL> self . geom_data . points = self . points <EOL> self . geom_data . facets = self . triangles </s>
