## *TEST* ## 'awk' PROGRAM NAME: feinp_generic2stl_awkprogTESTcbeam_cbar ## ## in $FEDIR/scripts ## where FEDIR=/apps/nns_com/fea ## ## *** NEEDS TO BE MADE SIMILAR TO 'feinp_generic2stl_awkprog'. ## ######################################################################## ## PURPOSE: FOR TESTING OF REFORMATTING OF 'GENERIC' CBEAM/CBAR RECORDS ## into an STL file. ## ## ** This awk-program READs a 'GENERIC' FE input file and ## ** EXTRACTs data from 'generic' GRID and 'generic' ELEMENT records ## ** (CBEAM & CBAR only -- for test) into an STL output file. ## ## Each 'generic' ELEMENT record is expected to be a single ## 'free-format' record [fields are space(s)-separated], ## generally of the form ## elname eid pid g1 g2 g3 ... ## where elname is an element name like CBEAM ## eid = element-id (an integer) ## pid = property id, usually (an integer) ## gi's are ids of grid/node points (integers). ######################################################################### ## CALLED BY: TEST SCRIPT 'feinp_nas2stl_nogui_TESTawkprogs' ## in $FEDIR/scripts. ## ## *** To prepare for incorporation into ## 'feinp_generic2stl_awkprog' ## which is called in 'feinp_nas2stl_bygui' ## ## which is a "NASTRAN-to-STL" file creation utility. ## ## *** COMPARE THIS AWK-PROG WITH 'feinp_generic2stl_awkprog'. ## ## The 'generic' ELEMENT-info file is created in (and is used as input ## in) 'feinp_nas2stl_bygui'. See that script for more info. ## Or see 'feinp_nas2stl_nogui_TESTawkprogs'. ## ######################################################################## ## INPUT: GENERIC-FE input filename ## OUTPUT: stdout ######################################################################## ## TEST FILES: Test NASTRAN files with various elements are ## in directory /apps/nns_com/fea/demos_nas2stl. ## Some of these NASTRAN input files are from ## /apps/msc/msc70/msc70/nast/demo ## and /apps/msc/msc70/msc70/nast/tpl. ## Others may be from NNS NASTRAN users. ######################################################################## ## SUPPORTED 'GENERIC' FE RECORD TYPES BELOW: ## - GRID & CBEAM/CBAR ######################################################################## ## MAINTENANCE HISTORY: ## Written by: B.Montandon O06 16Aug2000 'nasinp2stl_TESTcbeam_cbar' ## test script+awkprog was created ## based on 'nasinp2stl_awkprog' ## and 'nasinp2stl_bygui' ## in $FEDIR/scripts ## Updated by: B.Montandon O06 13Sep2000 Extracted awkprog from ## 'nasinp2stl_TESTcbeam_cbar' ## into this awkprog file. ######################################################################## ######################################################################### ## awk-prog to TRANSLATE a 'GENERIC' CBEAM/CBAR file to an STL file. ######################################################################### ## THIS LOGIC FOR LOOKING UP GRID INFO ## ASSUMES *ALL* THE generic 'GRID' RECORDS ARE BEFORE ## THE 'element-connectivity' RECORDS --- and are sorted by gridid. ######################################################################### ## SAMPLE NASTRAN INPUT FOR CONVERSION TO A 'GENERIC' FE FILE FOR TESTING: # FILEIN="/apps/nns_com/fea/demos_nas2stl/cbeams_1_free.dat" # FILEIN="/apps/nns_com/fea/demos_nas2stl/cbars_1_free.dat # FILEIN="/apps/nns_com/fea/demos_nas2stl/cbars_20_rbe2_1_fixed_demo_d10306d.dat # FILEIN="/apps/nns_com/fea/demos_nas2stl/cbeam_bar_tube_rod_1_free.dat ## SAMPLE INPUT FOR TESTING: ## (a simple one-beam *generic,free-format* input) ## (NOTE: Contains beam orientation data which is used to orient ## the STL rectangle [2 triangles] that represent the beam.) ## GRID 114 dummy 11.0 1.0 0.0 ## GRID 115 dummy 12.0 1.0 0.0 ## $ ## CBEAM 37 37 114 115 0.0 1.0 0.0 BEGIN { printf ("SOLID - FROM NASTRAN INPUT %-65s\n",FILE_IN); DEBUG=DE_BUG; i=0; jbeambar=0; ## i is a GRID rec counter; ## jbeambar is an beam-bar element rec counter. } { ############################################################################## ## *GRID*-RECORD PROCESSING SECTION: ############################################################################## ## LOOK FOR *GRID* IN THE FIRST FIELD. INCREMENT THE GRID-REC COUNT, i. ############################################################################## FLD1CAPS=toupper($1) if ( FLD1CAPS=="GRID" ) {i++ ## EXIT, WITH MSG TO OUTFILE, IF A GRID REC HAS LESS THAN 6 FIELDS. if (NF<6) { print "*ERROR" print "*ERROR: There must be at least 6 fields in GRID recs:" print "*ERROR: String GRID, two integers, and 3 x,y,z coordinates." exit } #################################################### ## STORE THE GRID-ID in the g[.] array. (pre-sorted) #################################################### g[i]=$2 ################################################################ ## STORE THE 4th,5th,6th FIELDS IN THE gx[.],gy[.],gz[.] arrays. ################################################################ gx[i]=$4; gy[i]=$5; gz[i]=$6 ## *FOR TESTING* -- show the just-captured GRID data & input rec: if (DEBUG==1) { print "#DEBUG" print "#DEBUG - GRID READ: i="i" g[i]="g[i] print "#DEBUG - gx[i]="gx[i]" gy[i]="gy[i]" gz[i]="gz[i] print "#DEBUG - REC-IN:" print "#DEBUG - $0" } next } ####################################################################### ## END OF the *GRID*-RECORD PROCESSING SECTION ####################################################################### ####################################################################### ## *CBEAM/CBAR*-RECORD PROCESSING SECTION: ####################################################################### ## LOOK FOR *CBEAM* or *CBAR* IN THE FIRST FIELD. ## INCREMENT THE ELEMENT-REC COUNT, jbeambar. ####################################################################### # FLD1CAPS=toupper($1) ## DONE ABOVE. if ( FLD1CAPS=="CBEAM" || FLD1CAPS=="cbeam" || \ FLD1CAPS=="CBAR" || FLD1CAPS=="cbar") {jbeambar++ ################################################################ ## STORE THE *CBEAM/CBAR* ELEMENT-ID IN THE *e* VAR. ## STORE THE 4th, 5th FIELDS (two grid ids) IN THE ## g1, g2 VARS. ## STORE THE 6th, 7th, 8th FIELDS (orientation vec) IN THE ## orx, ory, orz VARS. ################################################################ e=$2; g1=$4; g2=$5; orx=$6; ory=$7; orz=$8 ## *FOR TESTING* -- show the just-captured *CBEAM/CBAR* data & input rec: ## (Option: Do not direct awk stdout into a file; ## allow stdout to go to screen.) if (DEBUG==1) { print "#DEBUG" print "#DEBUG - ELEMENT READ: j=CBEAM/CBAR-count="jbeambar" e=elid="e print "#DEBUG - g1=G1="g1" g2=G2="g2 print "#DEBUG - orx="orx" ory="ory" orz="orz print "#DEBUG - REC-IN:" print "#DEBUG - "$0 } ################################################################### ## binary-SEARCH THE g[.] GRID-ID array FOR A MATCH, g[k], WITH g1. ## FOR A MATCH, SET x1,y1,z1 to gx[k],gy[k],gz[k]. ################################################################### k=binarySearch(g, i, g1) if ( k==-1 ) { print "*ERROR: NO GRID COORDS FOUND for GRID-id "g1" of CBEAM/BAR-id "e"." next } else { x1=gx[k] ; y1=gy[k] ; z1=gz[k] ## *FOR TESTING* -- show the looked-up *CBEAM/BAR* grid-data: if (DEBUG==1) { print "#DEBUG" print "#DEBUG - BINARY SEARCH of grid array g[k] FOUND:" print "#DEBUG - g1="g1" at k="k" g[k]="g[k] print "#DEBUG - with xyz coordinates" print "#DEBUG - x1="x1" y1="y1" z1="z1 } } ################################################################### ## binary-SEARCH THE g[.] GRID-ID array FOR A MATCH, g[k], WITH g2. ## FOR A MATCH, SET x2,y2,z2 to gx[k],gy[k],gz[k]. ################################################################### k=binarySearch(g, i, g2) if ( k==-1 ) { print "*ERROR: NO GRID COORDS FOUND for GRID-id "g2" of CBEAM/BAR-id "e"." next } else { x2=gx[k] ; y2=gy[k] ; z2=gz[k] ## *FOR TESTING* -- show the looked-up *CBEAM/BAR* grid-data: if (DEBUG==1) { print "#DEBUG" print "#DEBUG - BINARY SEARCH of grid array g[k] FOUND:" print "#DEBUG - g2="g2" at k="k" g[k]="g[k] print "#DEBUG - with xyz coordinates" print "#DEBUG - x2="x2" y2="y2" z2="z2 } } ################################################################ ## FIND THE LENGTH OF THE BEAM. ## AND SET A *radius* VALUE to 1/20th OF THE BEAM LENGTH. ################################################################ len=sqrt( (x2-x1)^2 + (y2-y1)^2 + (z2-z1)^2 ) rad=len/20.0 ################################################################ ## CALCULATE A NORMAL TO THE BEAM AND THE ORIENTATION VECTOR ## (using pts 1,2,1+or) of THE CURRENT *CBEAM* ELEMENT. ## It is in the z-direction of the beam local coordinate system. ################################################################ ax=x2-x1; ay=y2-y1; az=z2-z1 # bx=(x1+orx)-x1; by=(y1+ory)-y1; bz=(z1+orz)-z1 bx=orx; by=ory; bz=orz cx=ay*bz-az*by cy=az*bx-ax*bz cz=ax*by-ay*bx ################################################################ ## NORMALIZE THE COMPONENTS OF THE NORMAL TO THE BEAM WEB ## which is the z-direction of the beam local coordinate system. ################################################################ # normzlen=sqrt( (cx)^2 + (cy)^2 + (cz)^2 ) # normzx=cx/normzlen # normzy=cy/normzlen # normzz=cz/normzlen ################################################################ ## CALCULATE A NORMAL TO THE BEAM AND ITS Z-coordinate AXIS ## (using pts 1,2,1+c) of THE CURRENT *CBEAM* ELEMENT. ## It is in the y-direction of the beam local coordinate system. ################################################################ ax=x2-x1; ay=y2-y1; az=z2-z1 bx=(x1+cx)-x1; by=(y1+cy)-y1; bz=(z1+cz)-z1 cx=ay*bz-az*by cy=az*bx-ax*bz cz=ax*by-ay*bx ################################################################### ## NORMALIZE THE COMPONENTS OF THE y-NORMAL TO THE BEAM, ## which is in PLANE OF THE WEB OF THE BEAM. ################################################################### normylen=sqrt( (cx)^2 + (cy)^2 + (cz)^2 ) normyx=cx/normylen normyy=cy/normylen normyz=cz/normylen ################################################################ ## MAKE A LONG THIN RECTANGLE (ACTUALLY TWO LONG THIN TRIANGLES) ## TO ROUGHLY EMULATE THE BEAM. ## (Make points x1b,y1b,z1b & x2b,y2b,z2b --- slightly offset ## from x1,y1,z1 & x2,y2,z2 respectively, in the direction ## of the orientation vector.) ################################################################ x1b=x1+(rad*normyx) y1b=y1+(rad*normyy) z1b=z1+(rad*normyz) x2b=x2+(rad*normyx) y2b=y2+(rad*normyy) z2b=z2+(rad*normyz) ################################################################ ## CALCULATE THE FACET NORMAL OF TRIANGLE#1 (using pts 1,2,1b) ## of THE CURRENT *CBEAM* ELEMENT. ################################################################ ax=x2-x1; ay=y2-y1; az=z2-z1 bx=x1b-x1; by=y1b-y1; bz=z1b-z1 cx=ay*bz-az*by cy=az*bx-ax*bz cz=ax*by-ay*bx ################################################################ ## OUTPUT THE SET OF STL RECS CORRESPONDING TO TRIANGLE#1 ## of THE CURRENT *CBEAM* ELEMENT. ################################################################ print "facet normal "cx" "cy" "cz print " outer loop" print " vertex "x1" "y1" "z1 print " vertex "x2" "y2" "z2 print " vertex "x1b" "y1b" "z1b print " endloop" print "endfacet" ################################################################ ## CALCULATE THE FACET NORMAL OF TRIANGLE#2 (using pts 2,2b,1b) ## of THE CURRENT *CBEAM* ELEMENT. ################################################################ ax=x2b-x2; ay=y2b-y2; az=z2b-z2 bx=x1b-x2; by=y1b-y2; bz=z1b-z2 cx=ay*bz-az*by cy=az*bx-ax*bz cz=ax*by-ay*bx ################################################################ ## OUTPUT THE SET OF STL RECS CORRESPONDING TO TRIANGLE#2 ## of THE CURRENT *CBEAM* ELEMENT. ################################################################ print "facet normal "cx" "cy" "cz print " outer loop" print " vertex "x2" "y2" "z2 print " vertex "x2b" "y2b" "z2b print " vertex "x1b" "y1b" "z1b print " endloop" print "endfacet" next } ####################################################################### ## END OF if ( FLD1CAPS=="CBEAM" || ... || FLD1CAPS=="CBAR" || ... ) ## i.e. END OF the *CBEAM/BAR*-RECORD PROCESSING SECTION ####################################################################### } END { j=jbeambar if ( j==0 ) { print "*ERROR: No CBEAM or CBAR elements were found in the input file." } printf ("ENDSOLID - 2 x %12d triangles generated from %12d CBEAM+CBAR elements.\n",jbeambar,jbeambar) } ## START OF function *binarySearch* ######################################################################## ## NOTE: *function* statement is not recognized within ## awk BEGIN, *body=main*, and END sections. ######################################################################## ## ## DESCRIPTION OF A BINARY SEARCH ALGORITHM ## ## from http://www.ics.hawaii.edu/~yongsi/ics665/principle.html ## ## We have a sorted array A and a key K. Our task is to determine whether ## the key K is present in this array A. If we do find A[i] = K, ## return the array index i. Otherwise, return i = -1. ## ## The search Binary search works in the following way: ## ## 1. Compare K with the middle value (M) in remaiding part of array A. ## 2. If K = M, we are done. ## 3. If K < M, we continue our search to the lower end of array A and ## entirely ignore the other end. ## 4. If K > M, we continue, but confine ourselves to the higher end. ## ######################################################################## ## EXAMPLE: A = {1 3 4 6 8 9 11}. K = 4. ## ## M = 6 ## Compare K with 6. It is smaller. Continue search with A = 1 3 4. ## M = 3 ## Compare K with 3. It is bigger. Continue search with A = 3 4. (4 only?) ## M = 4 ## Compare K with 4. It is equal. We are done, we found K in A. ######################################################################## ## ## The algorithm (C-like) for Binary Search is listed below: ######################################################################## function binarySearch(ARRAY,ARRAY_SIZE,KEY, low,high,middle) { low=1 high=ARRAY_SIZE while ( low <= high ) { middle = int( (low+high)/2 ) ## FOR TESTING: # print "low= "low" high="high" middle="middle if ( KEY == ARRAY[ middle ] ) { return middle } else if ( KEY < ARRAY[ middle ] ) { high = middle - 1 } else { low = middle + 1 } } return -1 } ## END OF awk program ' ######################################################################### ## ADD SOME HOW/WHEN-GENERATED INFO TO THE END OF THE STL FILE. ######################################################################### ## echo "\ ## ENDSOLID - created via $FEDIR/scripts/feinp_generic2stl_awkprogTESTcbeam_cbar ## ENDSOLID - `date '+%Y %b %d %a %T%p %Z'` on host $THISHOST ## ENDSOLID - Generated via 'generic,free-format' FE file $GENERIC_FE_FILE ## ENDSOLID - which was built from sorting the nodes file $NODESFILE ## ENDSOLID - and attaching the elements file $ELSFILE ## ENDSOLID - OUTPUT IS IN $OUTFILE ## " >> $OUTFILE