pyNastran
0.5.0
pyNastran BDF Reader/Writer, OP2 Parser, and GUI
|
00001 ## GNU Lesser General Public License 00002 ## 00003 ## Program pyNastran - a python interface to NASTRAN files 00004 ## Copyright (C) 2011-2012 Steven Doyle, Al Danial 00005 ## 00006 ## Authors and copyright holders of pyNastran 00007 ## Steven Doyle <mesheb82@gmail.com> 00008 ## Al Danial <al.danial@gmail.com> 00009 ## 00010 ## This file is part of pyNastran. 00011 ## 00012 ## pyNastran is free software: you can redistribute it and/or modify 00013 ## it under the terms of the GNU Lesser General Public License as published by 00014 ## the Free Software Foundation, either version 3 of the License, or 00015 ## (at your option) any later version. 00016 ## 00017 ## pyNastran is distributed in the hope that it will be useful, 00018 ## but WITHOUT ANY WARRANTY; without even the implied warranty of 00019 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00020 ## GNU General Public License for more details. 00021 ## 00022 ## You should have received a copy of the GNU Lesser General Public License 00023 ## along with pyNastran. If not, see <http://www.gnu.org/licenses/>. 00024 ## 00025 import sys 00026 import copy 00027 from datetime import date 00028 00029 import pyNastran 00030 00031 def makeStamp(Title): 00032 #pageStamp = '1 MSC.NASTRAN JOB CREATED ON 10-DEC-07 AT 09:21:23 NOVEMBER 14, 2011 MSC.NASTRAN 6/17/05 PAGE ' 00033 #Title = 'MSC.NASTRAN JOB CREATED ON 10-DEC-07 AT 09:21:23' 00034 t = date.today() 00035 months = ['January','February','March','April','May','June','July','August','September','October','November','December'] 00036 today = '%-9s %s, %s' %(months[t.month-1],t.day,t.year) 00037 00038 releaseDate = '02/08/12'#pyNastran.__releaseDate__ 00039 releaseDate = '' 00040 build = 'pyNastran v%s %s' %(pyNastran.__version__,releaseDate) 00041 out = '1 %-67s %20s %-22s PAGE ' %(Title,today,build) 00042 return out 00043 00044 def makeF06Header(): 00045 n = '' 00046 lines1 = [ 00047 n+'/* ------------------------------------------------------------------- */\n', 00048 n+'/* PYNASTRAN */\n', 00049 n+'/* - NASTRAN FILE INTERFACE - */\n', 00050 n+'/* */\n', 00051 n+'/* A Python reader/editor/writer for the various */\n', 00052 n+'/* NASTRAN file formats. */\n', 00053 n+'/* Copyright (C) 2011-2012 Steven Doyle */\n', 00054 n+'/* */\n', 00055 n+'/* This program is free software; you can redistribute it and/or */\n', 00056 n+'/* modify it under the terms of the GNU Lesser General Public */\n', 00057 n+'/* License as published by the Free Software Foundation; */\n', 00058 n+'/* version 3 of the License. */\n', 00059 n+'/* */\n', 00060 n+'/* This program is distributed in the hope that it will be useful, */\n', 00061 n+'/* but WITHOUT ANY WARRANTY; without even the implied warranty of */\n', 00062 n+'/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */\n', 00063 n+'/* GNU Lesser General Public License for more details. */\n', 00064 n+'/* */\n', 00065 n+'/* You should have received a copy of the GNU Lesser General Public */\n', 00066 n+'/* License along with this program; if not, write to the */\n', 00067 n+'/* Free Software Foundation, Inc., */\n', 00068 n+'/* 675 Mass Ave, Cambridge, MA 02139, USA. */\n', 00069 n+'/* ------------------------------------------------------------------- */\n', 00070 '\n'] 00071 00072 n = 46*' ' 00073 lines2 = [ 00074 n+'* * * * * * * * * * * * * * * * * * * *\n', 00075 n+'* * * * * * * * * * * * * * * * * * * *\n', 00076 n+'* * * *\n', 00077 n+'* * pyNastran * *\n', 00078 n+'* * * *\n', 00079 n+'* * * *\n', 00080 n+'* * * *\n', 00081 n+'* * Version %8s * *\n' %(pyNastran.__version__), 00082 n+'* * * *\n', 00083 n+'* * * *\n', 00084 n+'* * %15s * *\n' %(pyNastran.__releaseDate2__), 00085 n+'* * * *\n', 00086 n+'* * Questions * *\n', 00087 n+'* * mesheb82@gmail.com * *\n', 00088 n+'* * * *\n', 00089 n+'* * * *\n', 00090 n+'* * * * * * * * * * * * * * * * * * * *\n', 00091 n+'* * * * * * * * * * * * * * * * * * * *\n\n\n'] 00092 return ''.join(lines1+lines2) 00093 00094 def makeEnd(): 00095 lines = [' \n' 00096 '1 * * * END OF JOB * * *\n' 00097 ' \n' 00098 ' \n'] 00099 return ''.join(lines) 00100 00101 class F06Writer(object): 00102 def __init__(self,model='tria3'): 00103 self.Title = '' 00104 self.setF06Name(model) 00105 00106 def setF06Name(self,model): 00107 self.model = model 00108 self.f06OutName = '%s.f06.out' %(self.model) 00109 00110 def loadOp2(self,isTesting=False): 00111 if isTesting==False: ## @todo implement in way that doesnt require a variable (e.g. check parent class) 00112 raise RuntimeError("Don't call this method unless you're testing the F06Writer. It breaks the F06 and OP2 classes.") 00113 from pyNastran.op2.op2 import OP2 00114 self.op2Name = model+'.op2' 00115 op2 = OP2(self.op2Name) 00116 op2.readOP2() 00117 00118 # oug 00119 self.eigenvectors = op2.eigenvectors 00120 self.displacements = op2.displacements 00121 self.temperatures = op2.temperatures 00122 00123 # oes 00124 #CBEAM 00125 #CSHEAR 00126 #CELASi 00127 self.rodStress = op2.rodStress 00128 self.rodStrain = op2.rodStrain 00129 self.barStress = op2.barStress 00130 self.barStrain = op2.barStrain 00131 self.plateStress = op2.plateStress 00132 self.plateStrain = op2.plateStrain 00133 self.compositePlateStress = op2.compositePlateStress 00134 self.compositePlateStrain = op2.compositePlateStrain 00135 00136 def makeF06Header(self): 00137 """If this class is inherited, the F06 Header may be overwritten""" 00138 return makeF06Header() 00139 00140 def makeStamp(self,Title): 00141 """If this class is inherited, the PAGE stamp may be overwritten""" 00142 return makeStamp(Title) 00143 00144 def writeF06(self,f06OutName,isMagPhase=False,makeFile=True,deleteObjects=True): 00145 """ 00146 Writes an F06 file based on the data we have stored in the object 00147 @param self 00148 the object pointer 00149 @param f06OutName 00150 the name of the F06 file to write 00151 @param isMagPhase 00152 should complex data be written using 00153 Magnitude/Phase instead of Real/Imaginary (default=False; Real/Imag) 00154 Real objects don't use this parameter. 00155 @param makeFile 00156 True -> makes a file, 00157 False -> makes a StringIO object for testing (default=True) 00158 """ 00159 if makeFile: 00160 f = open(f06OutName,'wb') 00161 else: 00162 import StringIO 00163 f = StringIO.StringIO() 00164 00165 #f.write(self.makeF06Header()) 00166 pageStamp = self.makeStamp(self.Title) 00167 #print "pageStamp = |%r|" %(pageStamp) 00168 #print "stamp = |%r|" %(stamp) 00169 00170 #isMagPhase = False 00171 pageNum = 1 00172 header = [' DEFAULT \n', 00173 '\n'] 00174 for iSubcase,result in sorted(self.eigenvalues.iteritems()): # goes first 00175 (subtitle,label) = self.iSubcaseNameMap[iSubcase] 00176 subtitle = subtitle.strip() 00177 header[0] = ' %s\n' %(subtitle) 00178 header[1] = '0 SUBCASE %i\n \n' %(iSubcase) 00179 print(result.__class__.__name__) 00180 (msg,pageNum) = result.writeF06(header,pageStamp,pageNum=pageNum,f=f,isMagPhase=isMagPhase) 00181 if deleteObjects: 00182 del result 00183 ### 00184 f.write(msg) 00185 pageNum +=1 00186 00187 for iSubcase,result in sorted(self.eigenvectors.iteritems()): # has a special header 00188 (subtitle,label) = self.iSubcaseNameMap[iSubcase] 00189 subtitle = subtitle.strip() 00190 header[0] = ' %s\n' %(subtitle) 00191 header[1] = '0 SUBCASE %i\n' %(iSubcase) 00192 print(result.__class__.__name__) 00193 (msg,pageNum) = result.writeF06(header,pageStamp,pageNum=pageNum,f=f,isMagPhase=isMagPhase) 00194 if deleteObjects: 00195 del result 00196 ### 00197 f.write(msg) 00198 pageNum +=1 00199 00200 # subcase name, subcase ID, transient word & value 00201 headerOld = [' DEFAULT \n', 00202 '\n',' \n'] 00203 header = copy.deepcopy(headerOld) 00204 resTypes = [ 00205 self.displacements,self.displacementsPSD,self.displacementsATO,self.displacementsRMS, 00206 self.scaledDisplacements, # ??? 00207 self.velocities,self.accelerations,self.eigenvectors, 00208 self.temperatures, 00209 self.loadVectors,self.thermalLoadVectors, 00210 self.forceVectors, 00211 00212 self.spcForces,self.mpcForces, 00213 00214 self.barForces,self.beamForces,self.springForces,self.damperForces, 00215 self.solidPressureForces, 00216 00217 self.rodStrain,self.nonlinearRodStress,self.barStrain,self.plateStrain,self.nonlinearPlateStrain,self.compositePlateStrain,self.solidStrain, 00218 self.beamStrain,self.ctriaxStrain,self.hyperelasticPlateStress, 00219 00220 self.rodStress,self.nonlinearRodStrain,self.barStress,self.plateStress,self.nonlinearPlateStress,self.compositePlateStress,self.solidStress, 00221 self.beamStress,self.ctriaxStress,self.hyperelasticPlateStrain, 00222 00223 00224 # beam, shear...not done 00225 #self.shearStrain,self.shearStress, 00226 00227 00228 self.gridPointStresses,self.gridPointVolumeStresses,#self.gridPointForces, 00229 ] 00230 00231 if 1: 00232 iSubcases = self.iSubcaseNameMap.keys() 00233 #print("self.iSubcaseNameMap = %s" %(self.iSubcaseNameMap)) 00234 for iSubcase in sorted(iSubcases): 00235 #print("***subcase = %s" %(iSubcase)) 00236 (subtitle,label) = self.iSubcaseNameMap[iSubcase] 00237 subtitle = subtitle.strip() 00238 label = label.strip() 00239 #print "label = ",label 00240 header[0] = ' %-127s\n' %(subtitle) 00241 header[1] = '0 %-72s SUBCASE %-15i\n' %(label,iSubcase) 00242 #header[1] = '0 %-72s SUBCASE %-15i\n' %('',iSubcase) 00243 for resType in resTypes: 00244 #print "resType = ",resType 00245 if iSubcase in resType: 00246 header = copy.deepcopy(headerOld) # fixes bug in case 00247 result = resType[iSubcase] 00248 try: 00249 print(result.__class__.__name__) 00250 (msg,pageNum) = result.writeF06(header,pageStamp,pageNum=pageNum,f=f,isMagPhase=False) 00251 except: 00252 #print "result name = %s" %(result.name()) 00253 raise 00254 if deleteObjects: 00255 del result 00256 ### 00257 f.write(msg) 00258 pageNum +=1 00259 ### 00260 ### 00261 ### 00262 if 0: 00263 for res in resTypes: 00264 for iSubcase,result in sorted(res.iteritems()): 00265 (msg,pageNum) = result.writeF06(header,pageStamp,pageNum=pageNum,f=f,isMagPhase=False) 00266 if deleteObjects: 00267 del result 00268 ### 00269 f.write(msg) 00270 pageNum +=1 00271 ### 00272 ### 00273 f.write(makeEnd()) 00274 if not makeFile: 00275 print(f.getvalue()) 00276 ### 00277 f.close() 00278 00279 if __name__=='__main__': 00280 #Title = 'MSC.NASTRAN JOB CREATED ON 10-DEC-07 AT 09:21:23' 00281 #stamp = makeStamp(Title) # doesnt have pageNum 00282 #print "|%s|" %(stamp+'22') 00283 00284 model = sys.argv[1] 00285 f06 = F06Writer(model) 00286 f06.loadOp2(isTesting=True) 00287 f06.writeF06() 00288 00289