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