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 #VTK_TRIANGLE = 5 00026 00027 from numpy import zeros 00028 00029 import vtk 00030 from vtk import vtkTriangle 00031 00032 from pyNastran.converters.cart3d.cart3d_reader import genericCart3DReader 00033 00034 class Cart3dIO(object): 00035 def __init__(self): 00036 pass 00037 00038 def removeOldGeometry(self,fileName): 00039 if fileName is None: 00040 self.grid = vtk.vtkUnstructuredGrid() 00041 self.gridResult = vtk.vtkFloatArray() 00042 #self.emptyResult = vtk.vtkFloatArray() 00043 #self.vectorResult = vtk.vtkFloatArray() 00044 self.grid2 = vtk.vtkUnstructuredGrid() 00045 self.scalarBar.VisibilityOff() 00046 skipReading = True 00047 else: 00048 self.TurnTextOff() 00049 self.grid.Reset() 00050 self.grid2.Reset() 00051 self.gridResult = vtk.vtkFloatArray() 00052 self.gridResult.Reset() 00053 self.gridResult.Modified() 00054 self.eidMap = {} 00055 self.nidMap = {} 00056 00057 self.resultCases = {} 00058 self.nCases = 0 00059 try: 00060 del self.caseKeys 00061 del self.iCase 00062 del self.iSubcaseNameMap 00063 except: 00064 print "cant delete geo" 00065 pass 00066 ### 00067 #print dir(self) 00068 skipReading = False 00069 self.scalarBar.VisibilityOff() 00070 self.scalarBar.Modified() 00071 return skipReading 00072 00073 00074 def loadCart3dGeometry(self,cart3dFileName,dirname,isNodal,isCentroidal): 00075 self.isNodal = isNodal 00076 self.isCentroidal = isCentroidal 00077 #key = self.caseKeys[self.iCase] 00078 #case = self.resultCases[key] 00079 00080 skipReading = self.removeOldGeometry(cart3dFileName) 00081 if skipReading: 00082 return 00083 00084 model = genericCart3DReader(cart3dFileName) 00085 self.modelType = model.modelType 00086 (nodes,elements,regions,loads) = model.readCart3d(cart3dFileName) 00087 self.nNodes = model.nPoints 00088 self.nElements = model.nElementsRead 00089 00090 #print "nNodes = ",self.nNodes 00091 print "nElements = ",self.nElements 00092 00093 self.grid.Allocate(self.nElements, 1000) 00094 self.gridResult.SetNumberOfComponents(self.nElements) 00095 self.grid2.Allocate(1, 1000) 00096 00097 points = vtk.vtkPoints() 00098 points.SetNumberOfPoints(self.nNodes) 00099 self.gridResult.Allocate(self.nNodes,1000) 00100 #vectorReselt.SetNumberOfComponents(3) 00101 self.nidMap = {} 00102 #elem.SetNumberOfPoints(nNodes) 00103 if 0: 00104 fraction = 1./self.nNodes # so you can color the nodes by ID 00105 for nid,node in sorted(nodes.iteritems()): 00106 points.InsertPoint(nid-1, *node) 00107 self.gridResult.InsertNextValue(nid*fraction) 00108 #print str(element) 00109 00110 #elem = vtk.vtkVertex() 00111 #elem.GetPointIds().SetId(0, i) 00112 #self.aQuadGrid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) 00113 #vectorResult.InsertTuple3(0, 0.0, 0.0, 1.0) 00114 00115 assert nodes is not None 00116 for nid,node in sorted(nodes.iteritems()): 00117 points.InsertPoint(nid-1, *node) 00118 00119 for eid,nodeIDs in sorted(elements.iteritems()): 00120 #print "ctria3" 00121 elem = vtkTriangle() 00122 elem.GetPointIds().SetId(0, nodeIDs[0]-1) 00123 elem.GetPointIds().SetId(1, nodeIDs[1]-1) 00124 elem.GetPointIds().SetId(2, nodeIDs[2]-1) 00125 self.grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds()) 00126 ### 00127 self.grid.SetPoints(points) 00128 #self.grid2.SetPoints(points2) 00129 #self.grid.GetPointData().SetScalars(self.gridResult) 00130 #print dir(self.grid) #.SetNumberOfComponents(0) 00131 #self.grid.GetCellData().SetNumberOfTuples(1); 00132 #self.grid.GetCellData().SetScalars(self.gridResult) 00133 self.grid.Modified() 00134 #self.grid2.Modified() 00135 self.grid.Update() 00136 #self.grid2.Update() 00137 print "updated grid" 00138 00139 00140 # loadCart3dResults - regions/loads 00141 self.TurnTextOn() 00142 self.scalarBar.VisibilityOn() 00143 self.scalarBar.Modified() 00144 00145 self.iSubcaseNameMap = {1:['Cart3d','']} 00146 cases = {} 00147 ID = 1 00148 00149 #print "nElements = ",nElements 00150 cases = self.fillCart3dCase(cases,ID,regions,loads) 00151 00152 self.resultCases = cases 00153 self.caseKeys = sorted(cases.keys()) 00154 #print "caseKeys = ",self.caseKeys 00155 #print "type(caseKeys) = ",type(self.caseKeys) 00156 self.iCase = -1 00157 self.nCases = len(self.resultCases)-1 # number of keys in dictionary 00158 self.cycleResults() # start at nCase=0 00159 00160 def fillCart3dCase(self,cases,ID,regions,loads): 00161 #print "regions**** = ",regions 00162 plotNodal = self.isNodal 00163 plotCentroidal = self.isCentroidal 00164 00165 nNodes = self.nNodes 00166 nElements = self.nElements 00167 Regions = zeros(nElements) 00168 Eids = zeros(nElements) 00169 00170 #u = zeros(nNodes) 00171 #v = zeros(nNodes) 00172 #w = zeros(nNodes) 00173 #Mach = zeros(nNodes) 00174 #rhoU = zeros(nNodes) 00175 #rhoV = zeros(nNodes) 00176 #rhoW = zeros(nNodes) 00177 00178 if plotCentroidal: 00179 for eid,regioni in sorted(regions.iteritems()): 00180 Eids[eid-1] = eid 00181 Regions[eid-1] = regioni 00182 ### 00183 del regions 00184 cases[(ID,'Region', 1,'centroid','%.0f')] = Regions 00185 cases[(ID,'Eids', 1,'centroid','%.0f')] = Eids 00186 00187 print "load.keys() = ",loads.keys() 00188 00189 if 'Cp' in loads and plotNodal: 00190 cp = loads['Cp'] 00191 Cp = zeros(nNodes) 00192 for nid,cpi in sorted(cp.iteritems()): 00193 Cp[nid-1] = cpi 00194 ### 00195 #del loads['Cp'] 00196 #del cp 00197 cases[(ID,'Cp', 1,'nodal','%.3f')] = Cp 00198 00199 if 'Mach' in loads and plotNodal: 00200 mach = loads['Mach'] 00201 Mach = zeros(nNodes) 00202 for nid,machi in sorted(mach.iteritems()): 00203 Mach[nid-1] = machi 00204 ### 00205 cases[(ID,'Mach', 1,'nodal','%.3f')] = Mach 00206 00207 if 'U' in loads and plotNodal: 00208 u = loads['U'] 00209 U = zeros(nNodes) 00210 for nid,ui in sorted(u.iteritems()): 00211 U[nid-1] = ui 00212 ### 00213 cases[(ID,'U', 1,'nodal','%.3f')] = U 00214 00215 if 'V' in loads and plotNodal: 00216 v = loads['V'] 00217 V = zeros(nNodes) 00218 for nid,vi in sorted(v.iteritems()): 00219 V[nid-1] = vi 00220 ### 00221 cases[(ID,'V', 1,'nodal','%.3f')] = V 00222 00223 if 'W' in loads and plotNodal: 00224 w = loads['W'] 00225 W = zeros(nNodes) 00226 for nid,wi in sorted(w.iteritems()): 00227 W[nid-1] = wi 00228 ### 00229 cases[(ID,'W', 1,'nodal','%.3f')] = W 00230 00231 #cases[(ID,'rhoU', 1,'nodal','%.3f')] = rhoU 00232 #cases[(ID,'rhoV', 1,'nodal','%.3f')] = rhoV 00233 #cases[(ID,'rhoW', 1,'nodal','%.3f')] = rhoW 00234 return cases 00235