def test_set_results(self): folder = os.path.abspath(os.path.join(testPath, '..', 'models')) op2Filename = os.path.join(folder, 'solid_bending', 'solid_bending.op2') op2 = OP2() op2.set_results('stress') op2.read_op2(op2Filename) self.assertEqual(len(op2.solidStress), 1), len(op2.solidStress) self.assertEqual(len(op2.displacements), 0), len(op2.displacements) op2 = OP2() op2.set_results(['stress', 'displacements']) op2.read_op2(op2Filename) self.assertEqual(len(op2.solidStress), 1), len(op2.solidStress) self.assertEqual(len(op2.displacements), 1), len(op2.displacements)
def test_write_2(self): """tests basic op2 writing""" log = SimpleLogger(level='warning', encoding='utf-8') folder = os.path.join(MODEL_PATH, 'solid_bending') op2_filename = os.path.join(folder, 'solid_bending.op2') op2_filename_debug = os.path.join(folder, 'solid_bending.debug.out') op2_filename_out = os.path.join(folder, 'solid_bending_out.op2') op2_filename_debug_out = os.path.join(folder, 'solid_bending_out.debug.out') #debug_file = 'solid_bending.debug.out' #model = os.path.splitext(op2_filename)[0] #debug_file = model + '.debug.out' op2 = OP2(debug=True, log=log, debug_file=op2_filename_debug, mode=None) op2.read_op2(op2_filename) op2.write_op2(op2_filename_out) #, is_mag_phase=False) op2b = read_op2_geom(op2_filename_out, debug_file=op2_filename_debug_out, log=log) assert op2 == op2b os.remove(op2_filename_debug_out)
def read_files(self): i = 0 for fname, subcaseList in sorted(iteritems(self.cases)): #print("case[%s] = %s" % (key, self.cases[key])) subcaseList = list(set(subcaseList)) print("subcaseList[%s] = %s" % (fname, subcaseList)) op2 = OP2(debug=False) op2.set_subcases(subcaseList) op2.read_op2(fname) for subcaseID in subcaseList: print("subcaseID = %s" % subcaseID) print("i = %s" % i) self.displacementResults[i] = op2.displacements[subcaseID] #self.solidStrainResults[i] = op2.solidStrain #self.plateStrainResults[i] = op2.plateStrain #self.compositePlateStressResults[i] = op2.compositePlateStress #self.compositePlateStrainResults[i] = op2.compositePlateStrain self.clean_stress(i, op2, subcaseID) i += 1 del op2 # makes sure that unneeded data is not stored to save memory
def test_force(self): log = get_logger(level='warning') for folder, prefix, subcase in CASES: bdf = BDF(debug=False, log=log) op2 = OP2(debug=False, log=log) basepath = os.path.join(pkg_path, 'op2', 'test', 'examples', folder) bdf.read_bdf(os.path.join(basepath, prefix + '.bdf')) op2.read_op2(os.path.join(basepath, prefix + '.op2')) op2_new = data_in_material_coord(bdf, op2) for vecname in force_vectors: vector = getattr(op2_new, vecname).get(subcase) if vector is None: continue name = os.path.join( basepath, '{0}_subcase_{1:02d}.txt'.format(vecname, subcase)) if not os.path.isfile(name): raise AssertionError( 'Not found reference result {0}\n{1}'.format( name, print_bad_path(name))) ref_result = np.loadtxt(name) data = vector.data eids = get_eids_from_op2_vector(vector) check = eids != 0 if 'cquad8' in vecname: assert np.allclose(data[:, check][:, 0::5, :], ref_result[0::5], rtol=RTOL, atol=ATOL) else: assert np.allclose(data[:, check], ref_result, rtol=RTOL, atol=ATOL)
def test_op2_dmi_01(self): """tests DMI matrix style""" folder = os.path.abspath(os.path.join(PKG_PATH, '..', 'models')) bdf_filename = os.path.join(folder, 'matrix', 'matrix.dat') op2_filename = os.path.join(folder, 'matrix', 'mymatrix.op2') matrices = { 'A' : True, 'B' : False, 'ATB' : False, 'BTA' : False, 'MYDOF' : True, } model = read_bdf(bdf_filename, debug=False) dmi_a = model.dmis['A'] assert dmi_a.shape == (4, 2), 'shape=%s' % (dmi_a.shape) #print('dmi_a\n', dmi_a) a, rows_reversed, cols_reversed = dmi_a.get_matrix(is_sparse=False, apply_symmetry=False) #print('model.dmi.A =\n%s' % dmi_a) #print('model.dmi.A =\n%s' % str(a)) #return op2 = OP2(debug=False) op2.set_additional_matrices_to_read(matrices) try: op2.read_op2(op2_filename) raise RuntimeError('this is wrong...') except FatalError: # the OP2 doesn't have a trailing zero marker pass # M rows, Ncols A = np.array([ [1., 0.], [3., 6.], [5., 0.], [0., 8.], ], dtype='float32') B = A mydof = np.array([ -1.0, 1.0, 1.0, -1.0, 1.0, 2.0, -1.0, 1.0, 3.0, -1.0, 1.0, 4.0, -1.0, 1.0, 5.0, -1.0, 1.0, 6.0, -1.0, 2.0, 1.0, -1.0, 2.0, 2.0, -1.0, 2.0, 3.0, -1.0, 2.0, 4.0, -1.0, 2.0, 5.0, -1.0, 2.0, 6.0, ]) BTA = np.dot(B.T, A) ATB = np.dot(A.T, B) ATB_expected = np.array([ [35., 18.], [18., 100.] ], dtype='float32') #BTA_expected = ATB_expected expecteds = [A, ATB, B, BTA, mydof] matrix_names = sorted(matrices.keys()) for matrix_name, expected in zip(matrix_names, expecteds): assert matrix_name in op2.matrices, matrix_name actual = op2.matrices[matrix_name].data.todense() compare_dmi_matrix_from_bdf_to_op2(model, op2, expected, actual, matrix_name)
def readFiles(self): i = 0 for fname, subcaseList in sorted(self.cases.iteritems()): #print "case[%s] = %s" %(key,self.cases[key]) subcaseList = list(set(subcaseList)) print "subcaseList[%s] = %s" % (fname, subcaseList) op2 = OP2(fname, debug=False) op2.setSubcases(subcaseList) op2.readOP2() for subcaseID in subcaseList: print "subcaseID = ", subcaseID print "i = ", i self.displacementResults[i] = op2.displacements[subcaseID] #self.solidStrainResults[i] = op2.solidStrain #self.plateStrainResults[i] = op2.plateStrain #self.compositePlateStressResults[i] = op2.compositePlateStress #self.compositePlateStrainResults[i] = op2.compositePlateStrain self.cleanStress(i, op2, subcaseID) i += 1 del op2 # makes sure that unneeded data is not stored to save memory
def __init__(self, op2_filename='fem.op2'): log.info('---starting deflectionReader.init of %s---' % op2_filename) op2 = OP2() op2.set_results('displacements') op2.read_op2(infilename) self.deflections = op2.displacements.translations log.info('---finished deflectionReader.init of %s---' % infilename)
def loadOp2(self, isTesting=False): print("self.class = ", self.__class__.__name__) if isTesting == False: ## @todo implement in way that doesnt require a variable (e.g. check parent class) raise RuntimeError( "Don't call this method unless you're testing the F06Writer. It breaks the F06 and OP2 classes." ) from pyNastran.op2.op2 import OP2 self.op2Name = model + '.op2' op2 = OP2(self.op2Name) op2.readOP2() # oug self.eigenvectors = op2.eigenvectors self.displacements = op2.displacements self.temperatures = op2.temperatures # oes #CBEAM #CSHEAR #CELASi self.rodStress = op2.rodStress self.rodStrain = op2.rodStrain self.barStress = op2.barStress self.barStrain = op2.barStrain self.plateStress = op2.plateStress self.plateStrain = op2.plateStrain self.compositePlateStress = op2.compositePlateStress self.compositePlateStrain = op2.compositePlateStrain
def __init__(self, op2_filename='fem.op2', isubcase=1): log.info('---starting deflectionReader.init of %s---' % op2_filename) op2 = OP2() op2.set_results('displacements') op2.read_op2(op2_filename) self.deflections = op2.displacements[isubcase].data log.info('---finished deflectionReader.init of %s---' % op2_filename)
def test_pload4_cquad4(self): """tests a PLOAD4 with a CQUAD4""" bdf_filename = os.path.join(test_path, '..', 'models', 'pload4', 'cquad4.bdf') op2_filename = os.path.join(test_path, '..', 'models', 'pload4', 'cquad4.op2') op2 = OP2(debug=False, log=log) op2.read_op2(op2_filename) model_b = BDF(debug=False) model_b.read_bdf(bdf_filename) # p0 = (model_b.nodes[21].xyz + model_b.nodes[22].xyz + model_b.nodes[23].xyz) / 3. p0 = model_b.nodes[21].xyz eids = None nids = None for isubcase, subcase in sorted(iteritems(model_b.subcases)): if isubcase == 0: continue #if isubcase != 17: #continue loadcase_id = subcase.get_parameter('LOAD')[0] load = model_b.loads[loadcase_id] loadi = load[0] if loadi.type == 'PLOAD4': elem = loadi.eids_ref[0] area = 1.0 centroid = elem.Centroid() normal = elem.Normal() msg = '%s%s%s\n' % (elem.nodes[0], elem.nodes[1], elem.nodes[2]) assert array_equal(centroid, array( [0.5, 0.5, 0.])), 'centroid=%s\n%s' % (centroid, msg) assert array_equal(normal, array([0., 0., 1. ])), 'normal=%s\n%s' % (normal, msg) f, m = model_b.sum_forces_moments(p0, loadcase_id, include_grav=False) f2, m2 = model_b.sum_forces_moments_elements(p0, loadcase_id, eids, nids, include_grav=False) assert allclose(f, f2), 'f=%s f2=%s' % (f, f2) assert allclose(m, m2), 'm=%s m2=%s' % (m, m2) case = op2.spc_forces[isubcase] fm = -case.data[0, :, :].sum(axis=0) assert len(fm) == 6, fm if not allclose(f[0], fm[0]): print('%-2i Fx f=%s fexpected=%s' % (isubcase, f, fm)) if not allclose(f[1], fm[1]): print('%-2i Fy f=%s fexpected=%s' % (isubcase, f, fm)) if not allclose(f[2], fm[2]): print('%-2i Fz f=%s fexpected=%s' % (isubcase, f, fm))
def loadNastranResults(self, op2FileName, dirname, isNodal, isCentroidal): #self.gridResult.SetNumberOfComponents(self.nElements) self.TurnTextOn() self.scalarBar.VisibilityOn() self.scalarBar.Modified() op2 = OP2(op2FileName, debug=True) op2.readOP2() #print op2.printResults() #case = op2.displacements[1] #print "case = ",case #for nodeID,translation in sorted(case.translations.iteritems()): #print "nodeID=%s t=%s" %(nodeID,translation) #self.iSubcaseNameMap[self.iSubcase] = [Subtitle,Label] cases = {} subcaseIDs = op2.iSubcaseNameMap.keys() self.iSubcaseNameMap = op2.iSubcaseNameMap nElements = len(self.eidMap) #print "nElements = ",nElements nidsSet = False # set to False to disable nodeIDs eidsSet = True for subcaseID in subcaseIDs: if nidsSet: nids = zeros(self.nNodes, 'd') for (nid, nid2) in self.nidMap.iteritems(): nids[nid2] = nid cases[(subcaseID, 'Node_ID', 1, 'node', '%.0f')] = nids nidsSet = True if eidsSet: eids = zeros(nElements, 'd') for (eid, eid2) in self.eidMap.iteritems(): eids[eid2] = eid eKey = (subcaseID, 'isElementOn', 1, 'centroid', '%.0g') cases[(subcaseID, 'Element_ID', 1, 'centroid', '%.0f')] = eids cases[eKey] = zeros(nElements) # is the element supported eidsSet = True if False: if subcaseID in op2.displacements: # not correct? case = op2.displacements[subcaseID] key = (subcaseID, 'DisplacementX', 3, 'node', '%g') #cases[key] = case.translations if subcaseID in op2.temperatures: case = op2.temperatures[subcaseID] #print case temps = zeros(self.nNodes) key = (subcaseID, 'Temperature', 1, 'node', '%g') for (nid, T) in case.temperatures.iteritems(): #print T nid2 = self.nidMap[nid] temps[nid2] = T
def read_op2(self, op2path): if not os.path.isfile(op2path): print('ERROR - op2 "{0}" does not exist!'.format(op2path)) return op2 = OP2() print('Reading op2 file...') op2.read_op2(op2path) self.op2 = data_in_material_coord(self.bdf, op2, in_place=True) print('finished!')
def read_op2(op2_filename, isubcase=1): log.info('---starting deflectionReader.init of %s---' % op2_filename) op2 = OP2() op2.set_results('displacements') op2.read_op2(op2_filename) displacment_obj = op2.displacements[isubcase] log.info('---finished deflectionReader.init of %s---' % op2_filename) return displacment_obj.translations
def test_op2_dmi_02(self): """tests DMI matrix style""" bdf_filename = os.path.join(MODEL_PATH, 'matrix', 'matrix.dat') op2_filename = os.path.join(MODEL_PATH, 'matrix', 'mymatrix.op2') matrices = { 'A' : True, 'B' : False, 'ATB' : False, 'BTA' : False, 'MYDOF' : True, } model = read_bdf(bdf_filename, debug=False) dmi_a = model.dmis['A'] a, rows_reversed, cols_reversed = dmi_a.get_matrix(is_sparse=False, apply_symmetry=False) #print('model.dmi.A =\n%s' % dmi_a) #print('model.dmi.A =\n%s' % str(a)) #return op2 = OP2(debug=False) try: op2.read_op2(op2_filename, skip_undefined_matrices=True) raise RuntimeError('this is wrong...') except FatalError: # the OP2 doesn't have a trailing zero marker pass # M rows, Ncols A = np.array([ [1., 0.], [3., 6.], [5., 0.], [0., 8.], ], dtype='float32') B = A mydof = np.array([ -1.0, 1.0, 1.0, -1.0, 1.0, 2.0, -1.0, 1.0, 3.0, -1.0, 1.0, 4.0, -1.0, 1.0, 5.0, -1.0, 1.0, 6.0, -1.0, 2.0, 1.0, -1.0, 2.0, 2.0, -1.0, 2.0, 3.0, -1.0, 2.0, 4.0, -1.0, 2.0, 5.0, -1.0, 2.0, 6.0, ]) BTA = B.T @ A ATB = A.T @ B ATB_expected = np.array([ [35., 18.], [18., 100.] ], dtype='float32') #BTA_expected = ATB_expected expecteds = [A, ATB, B, BTA, mydof] matrix_names = sorted(matrices.keys()) for matrix_name, expected in zip(matrix_names, expecteds): assert matrix_name in op2.matrices, matrix_name actual = op2.matrices[matrix_name].data.toarray() compare_dmi_matrix_from_bdf_to_op2(model, op2, expected, actual, matrix_name)
def read_op2(op2_filename, isubcase=1, log=None): """loads the deflections from the op2 for the specified subcase""" log.info('---starting deflection_reader.init of %s---' % op2_filename) op2 = OP2(log=log) op2.set_subcases(subcases=[isubcase]) op2.set_results('displacements') op2.read_op2(op2_filename) displacment_obj = op2.displacements[isubcase] log.info('---finished deflection_reader.init of %s---' % op2_filename) return displacment_obj
def eigenvector(): """ Example 3: Eigenvector (transient) - Level 2 """ # This example will demonstate: # - calculate von mises stress and max shear for solid elements for a static case for an OP2 ####.. math:: \sqrt\left(T_x^2 + T_y^2 + T_z^2\right) # our model import pyNastran pkg_path = pyNastran.__path__[0] test_path = os.path.join(pkg_path, '..', 'models', 'solid_bending') op2_filename = os.path.join(test_path, 'solid_bending.op2') out_filename = os.path.join(test_path, 'solid_bending.out') # instantiate the model from pyNastran.op2.op2 import OP2 model = OP2() model.read_op2(op2_filename) print(model.get_op2_stats()) #### ## op2.ctetra_stress[1] ## type=RealSolidStressArray nelements=186 nnodes=930 ## nodes_per_element=5 (including centroid) ## eType, cid ## data: [1, nnodes, 10] where 10=[oxx, oyy, ozz, txy, tyz, txz, o1, o2, o3, von_mises] ## data.shape = (1, 930, 10) ## element types: CTETRA ## lsdvmns = [1] # we're analyzing a static problem, so itime=0 # we're also assuming subcase 1 itime = 0 isubcase = 1 # get the stress object (there is also cpenta_stress and chexa_stress as well as ctetra_strain/cpenta_strain/chexa_strain) stress = model.ctetra_stress[isubcase] # The stress/strain data can often be von_mises/max_shear (same for fiber_distance/curvature), so check! #### #data = [oxx, oyy, ozz, txy, tyz, txz, o1, o2, o3, von_mises] o1 = stress.data[itime, :, 6] o3 = stress.data[itime, :, 8] if stress.is_von_mises(): max_shear = (o1 - o3) / 2. von_mises = stress.data[itime, :, 9] else: from numpy import sqrt o2 = data[itime, :, 8] von_mises = sqrt(0.5 * ((o1 - o2)**2 + (o2 - o3)**2, (o3 - o1)**2)) max_shear = stress.data[itime, :, 9] for (eid, node), vm, ms in zip(stress.element_node, von_mises, max_shear): print(eid, 'CEN/4' if node == 0 else node, vm, ms)
def read_op2(op2_filename): log.info('---starting deflectionReader.init of %s---' % op2_filename) op2 = OP2(op2_filename) #terms = ['force','stress','stress_comp','strain','strain_comp','displacement','grid_point_forces'] op2.read_op2() subcase0 = op2.displacements.keys()[0] # get the 0th subcase displacment_obj = op2.displacements[subcase0] log.info('---finished deflectionReader.init of %s---' % op2_filename) return displacment_obj.translations
def load_op2_from_hdf5(hdf5_filename, combine=True, log=None): """loads an hdf5 file into an OP2 object""" assert os.path.exists(hdf5_filename), print_bad_path(hdf5_filename) model = OP2(log=None) model.op2_filename = hdf5_filename log.info('hdf5_op2_filename = %r' % hdf5_filename) debug = False with h5py.File(hdf5_filename, 'r') as h5_file: load_op2_from_hdf5_file(model, h5_file, log, debug=debug) model.combine_results(combine=combine) return model
def test_pload4_cquad4(self): """tests a PLOAD4 with a CQUAD4""" bdf_filename = os.path.join(test_path, '..', 'models', 'pload4', 'cquad4.bdf') op2_filename = os.path.join(test_path, '..', 'models', 'pload4', 'cquad4.op2') op2 = OP2(debug=False, log=log) op2.read_op2(op2_filename) model = BDF(debug=False) model.read_bdf(bdf_filename) # p0 = (model.nodes[21].xyz + model.nodes[22].xyz + model.nodes[23].xyz) / 3. p0 = model.nodes[21].xyz eids = None nids = None subcase_ids = [1, 2, 3, 4, 5, 6, 7, 8] for isubcase in subcase_ids: subcase = model.subcases[isubcase] loadcase_id = subcase.get_parameter('LOAD')[0] load = model.Load(loadcase_id) loadi = load[0] if loadi.type == 'PLOAD4': elem = loadi.eids_ref[0] #area = 1.0 centroid = elem.Centroid() normal = elem.Normal() # centroid = [0.5, 0.5, 0.] # normal = [0., 0., 1.] #print('centroid=%s normal=%s' % (centroid, normal)) msg = '%s%s%s\n' % (elem.nodes[0], elem.nodes[1], elem.nodes[2]) assert array_equal(centroid, array([0.5, 0.5, 0.])), 'centroid=%s\n%s' % (centroid, msg) assert array_equal(normal, array([0., 0., 1.])), 'normal=%s\n%s' % (normal, msg) f1, m1 = model.sum_forces_moments(p0, loadcase_id, include_grav=False) f2, m2 = model.sum_forces_moments_elements(p0, loadcase_id, eids, nids, include_grav=False) assert allclose(f1, f2), 'f1=%s f2=%s' % (f1, f2) assert allclose(m1, m2), 'm1=%s m2=%s' % (m1, m2) case = op2.spc_forces[isubcase] fm = -case.data[0, :, :].sum(axis=0) assert len(fm) == 6, fm force = fm[:3] if not allclose(f1[0], force[0]): model.log.error('subcase=%-2i Fx f=%s force_expected=%s' % ( isubcase, f1.tolist(), force.tolist())) if not allclose(f1[1], force[1]): model.log.error('subcase=%-2i Fy f=%s force_expected=%s' % ( isubcase, f1.tolist(), force.tolist())) if not allclose(f1[2], force[2]): model.log.error('subcase=%-2i Fz f=%s force_expected=%s' % ( isubcase, f1.tolist(), force.tolist())) save_load_deck(model, punch=False)
def main(bdf_name, run_first_nastran=True, debug=True): base = os.path.splitext(bdf_name)[0] print("len(sys.argv) = %s" % len(sys.argv)) #=========================== if run_first_nastran: # run nastran and verify the starting model is correct os.system('nastran scr=yes bat=no news=no old=no %s' % bdf_name) f06_name = base + '.f06' try: model2 = F06() model2.read_f06(f06_name) except FatalError as e: print(e) #return else: pass #=========================== # read/write the model in double precision out_bdf_8 = base + '_8.bdf' out_bdf_16 = base + '_16.bdf' out_bdf_16s = base + '_16s.bdf' model3 = BDF(debug=debug) model3.read_bdf(bdf_name) update_bdf(model3, post=-1) model3.write_bdf(out_bdf_8, size=8, is_double=False) model3.write_bdf(out_bdf_16s, size=16, is_double=False) model3.write_bdf(out_bdf_16, size=16, is_double=True) if debug: print("---wrote the bdf---") #=========================== # run nastran again os.system('nastran scr=yes bat=no news=no old=no %s' % out_bdf_8) os.system('nastran scr=yes bat=no news=no old=no %s' % out_bdf_16) #=========================== # verify it's correct if is_f06: out_f06_8 = base + '_8.f06' out_f06_16 = base + '_16.f06' model4 = F06(debug=False) model4.read_f06(out_f06_8) model5 = F06(debug=False) model5.read_f06(out_f06_16) out_op2_8 = base + '_8.op2' out_op2_16 = base + '_16.op2' model6 = OP2(debug=False) model6.read_op2(out_op2_16) print('\n\npassed!!')
def test_pload4_ctria3(self): """tests a PLOAD4 with a CTRIA3""" bdf_filename = os.path.join(test_path, '..', 'models', 'pload4', 'ctria3.bdf') op2_filename = os.path.join(test_path, '..', 'models', 'pload4', 'ctria3.op2') op2 = OP2(debug=False, log=log) op2.read_op2(op2_filename) model = BDF(debug=False) model.read_bdf(bdf_filename) # p0 = (model.nodes[21].xyz + model.nodes[22].xyz + model.nodes[23].xyz) / 3. p0 = model.nodes[21].xyz subcase_ids = [1, 2, 4, 5, 6] # no 3, 7, 8 for isubcase in subcase_ids: subcase = model.subcases[isubcase] loadcase_id = subcase.get_parameter('LOAD')[0] loads = model.get_reduced_loads(loadcase_id, consider_load_combinations=True)[0] load = loads[0] assert len(loads) == 1, 'subcase:\n%s\nloads=\n%s' % (subcase, loads) elem = load.eids_ref[0] centroid = elem.Centroid() normal = elem.Normal() msg = '%s%s%s\n' % ( elem.nodes[0], elem.nodes[1], elem.nodes[2]) assert array_equal(centroid, array([2/3., 1/3., 0.])), 'centroid=%s\n%s' % (centroid, msg) assert array_equal(normal, array([0., 0., 1.])), 'normal=%s\n%s' % (normal, msg) forces1, moments1 = model.sum_forces_moments(p0, loadcase_id, include_grav=False) eids = None nids = None forces2, moments2 = model.sum_forces_moments_elements( p0, loadcase_id, eids, nids, include_grav=False) assert allclose(forces1, forces2), 'forces1=%s forces2=%s' % (forces1, forces2) assert allclose(moments1, moments2), 'moments1=%s moments2=%s' % (moments1, moments2) case = op2.spc_forces[isubcase] fm = -case.data[0, :, :].sum(axis=0) assert len(fm) == 6, fm if not allclose(forces1[0], fm[0]): model.log.error('subcase=%-2i Fx f=%s fm_expected=%s' % ( isubcase, forces1.tolist(), fm.tolist())) if not allclose(forces1[1], fm[1]): model.log.error('subcase=%-2i Fy f=%s fm_expected=%s' % ( isubcase, forces1.tolist(), fm.tolist())) if not allclose(forces1[2], fm[2]): model.log.error('subcase=%-2i Fz f=%s fm_expected=%s' % ( isubcase, forces1.tolist(), fm.tolist())) save_load_deck(model, punch=False)
def setup(bdf_filename, op2_filename): fem = BDF(debug=False) fem.read_bdf(bdf_filename) eid_to_nid_map = {} for eid, element in iteritems(fem.elements): node_ids = element.node_ids eid_to_nid_map[eid] = node_ids del fem model = OP2() model.set_results(['solidStress', 'plateStress']) model.read_op2(op2_filename) return eid_to_nid_map, model
def read_write(): """ Example 1: Read Write - Level 2 """ # This example will demonstate: # - reading the OP2 # - getting some basic information # - writing the F06 # our model import pyNastran pkg_path = pyNastran.__path__[0] test_path = os.path.join(pkg_path, '..', 'models', 'solid_bending') op2_filename = os.path.join(test_path, 'solid_bending.op2') f06_filename = os.path.join(test_path, 'solid_bending_out.f06') # instantiate the model from pyNastran.op2.op2 import OP2 model = OP2() model.read_op2(op2_filename) print(model.get_op2_stats()) ## op2.displacements[1] ## type=RealDisplacementArray nnodes=72 ## data: [t1, t2, t3, r1, r2, r3] shape=[1, 72, 6] dtype=float32 ## gridTypes ## lsdvmns = [1] ## ## op2.spc_forces[1] ## type=RealSPCForcesArray nnodes=72 ## data: [t1, t2, t3, r1, r2, r3] shape=[1, 72, 6] dtype=float32 ## gridTypes ## lsdvmns = [1] ## ## op2.ctetra_stress[1] ## type=RealSolidStressArray nelements=186 nnodes=930 ## nodes_per_element=5 (including centroid) ## eType, cid ## data: [1, nnodes, 10] where 10=[oxx, oyy, ozz, txy, tyz, txz, o1, o2, o3, von_mises] ## data.shape = (1, 930, 10) ## element types: CTETRA ## lsdvmns = [1] model.write_f06(f06_filename) ## F06: ## RealDisplacementArray SUBCASE=1 ## RealSPCForcesArray SUBCASE=1 ## RealSolidStressArray SUBCASE=1 - CTETRA print(tail(f06_filename, 21))
def main(bdf_name, run_first_nastran=True, debug=True): base, ext = os.path.splitext(bdf_name) print "len(sys.argv) =", len(sys.argv) #=========================== if run_first_nastran: # run nastran and verify the starting model is correct os.system('nastran scr=yes bat=no news=no old=no %s' % bdf_name) f06_name = base + '.f06' try: model2 = F06(f06_name) model2.read_f06() except FatalError as e: print(e) #return else: pass #=========================== # read/write the model in double precision out_bdf_8 = base + '_8.bdf' out_bdf_16 = base + '_16.bdf' model3 = BDF() model3.read_bdf(bdf_name) update_bdf(model3) model3.write_bdf(out_bdf_8, size=8, precision='single') model3.write_bdf(out_bdf_16, size=16, precision='double') if debug: print "---wrote the bdf---" #=========================== # run nastran again os.system('nastran scr=yes bat=no news=no old=no %s' % out_bdf_8) os.system('nastran scr=yes bat=no news=no old=no %s' % out_bdf_16) #=========================== # verify it's correct out_f06_8 = base + '_8.f06' out_f06_16 = base + '_16.f06' out_op2_16 = base + '_16.op2' model4 = F06(out_f06_8, debug=False) model4.read_f06() model5 = F06(out_f06_16, debug=False) model5.read_f06() model6 = OP2(out_op2_16, debug=False) model6.read_op2() print('\n\npassed!!')
def get_eigenvalues(op2_filename, debug=False): model2 = OP2(debug=debug) model2.read_op2(op2_filename) cases = model2.eigenvectors.keys() isubcase = cases[0] eigenvector = model2.eigenvectors[isubcase] try: eigrs = eigenvector.eigrs #eigrs = d._eigrs except AttributeError: msg = '%s.object_attributes() = %s' % ( eigenvector.class_name, str(eigenvector.object_attributes(keys_to_skip='class_name'))) raise RuntimeError(msg) return eigrs
def solid_stress(): """ Example 4: Solid Stress (static) - Level 2 """ # This example will demonstate: # - calculating total deflection of the nodes for a dynamic case for an OP2 ####.. math:: \sqrt\left(T_x^2 + T_y^2 + T_z^2\right) # our model import pyNastran pkg_path = pyNastran.__path__[0] test_path = os.path.join(pkg_path, '..', 'models', 'plate_py') op2_filename = os.path.join(test_path, 'plate_py.op2') #out_filename = os.path.join(test_path, 'solid_bending.out') # instantiate the model from pyNastran.op2.op2 import OP2 model = OP2() model.read_op2(op2_filename) print(model.get_op2_stats()) #### ## op2.eigenvectors[1] ## type=RealEigenvectorArray ntimes=10 nnodes=231 ## data: [t1, t2, t3, r1, r2, r3] shape=[10, 231, 6] dtype=float32 ## gridTypes ## modes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] ## eigrs = [-0.00037413835525512695, -0.00022113323211669922, -0.0001882314682006836, -0.00010025501251220703, 0.0001621246337890625, 0.00 ## 07478296756744385, 1583362560.0, 2217974016.0, 10409966592.0, 11627085824.0] ## mode_cycles = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] isubcase = 1 eigenvector = model.eigenvectors[isubcase] # "time/mode/frequency are stored by id, so to get mode 5: modes = eigenvector._times # it may not be "time" so we don't use the name "time" from numpy import where imode5 = where(modes == 5)[0] txyz = eigenvector.data[imode5, :, :3] # calculate the total deflection of the vector from numpy.linalg import norm total_xyz = norm(txyz, axis=1) # get the eigenvalue print('eigr5 = %s' % eigenvector.eigrs[imode5])
def composite_plate_stress(): """ Example 6: Composite Plate Stress (static) - Level 2 """ # This example will demonstate: # - print the fiber distance and the max principal stress for a static case for an OP2 # our model import pyNastran pkg_path = pyNastran.__path__[0] test_path = os.path.join(pkg_path, '..', 'models', 'sol_101_elements') op2_filename = os.path.join(test_path, 'static_solid_shell_bar.op2') # instantiate the model from pyNastran.op2.op2 import OP2 model = OP2() model.read_op2(op2_filename) print(model.get_op2_stats()) ## op2.ctria3_composite_stress[1] ## type=RealCompositePlateStressArray nelements=4 ntotal=18 ## data: [1, ntotal, 9] where 9=[o11, o22, t12, t1z, t2z, angle, major, minor, max_shear] ## data.shape = (1, 18, 9) ## element types: CTRIA3 ## lsdvmns = [1] isubcase = 1 itime = 0 # this is a static case stress = model.ctria3_composite_stress[isubcase] # In the previous example, we had an option for a variable number of nodes for the CQUAD4s (1/5), but only nnodes=1 for the CTRIA3s. # In this example, we have 4 layers on one element and 5 on another, but they're all at the centroid. #### #[o11, o22, t12, t1z, t2z, angle, major, minor, ovm] eids = stress.element_layer[:, 0] layers = stress.element_layer[:, 1] maxp = stress.data[itime, :, 6] if stress.is_fiber_distance(): fiber_dist = stress.data[itime, :, 0] else: raise RuntimeError('found strain curvature; expected fiber distance') maxp = stress.data[itime, :, 5] for (eid, layer, maxpi) in zip(eids, layers, maxp): print(eid, 'CEN/4', layer, maxpi)
def isotropic_plate_stress(): """ Example 5: Isotropic Plate Stress (static) - Level 2 """ # This example will demonstate: # - print the fiber distance and the max principal stress for a static case for an OP2 # our model import pyNastran pkg_path = pyNastran.__path__[0] test_path = os.path.join(pkg_path, '..', 'models', 'sol_101_elements') op2_filename = os.path.join(test_path, 'static_solid_shell_bar.op2') # instantiate the model from pyNastran.op2.op2 import OP2 model = OP2() model.read_op2(op2_filename) print(model.get_op2_stats()) #### ## op2.cquad4_stress[1] ## type=RealPlateStressArray nelements=2 nnodes_per_element=5 nlayers=2 ntotal=20 ## data: [1, ntotal, 8] where 8=[fiber_distance, oxx, oyy, txy, angle, omax, omin, von_mises] ## data.shape=(1L, 20L, 8L) ## element types: CQUAD4 ## lsdvmns = [1] isubcase = 1 itime = 0 # this is a static case stress = model.cquad4_stress[isubcase] assert stress.nnodes == 5, 'this is a bilinear quad' # write the data #### #[fiber_dist, oxx, oyy, txy, angle, majorP, minorP, ovm] eids = stress.element_node[:, 0] nids = stress.element_node[:, 1] if stress.is_fiber_distance(): fiber_dist = stress.data[itime, :, 0] else: raise RuntimeError('found fiber curvature; expected fiber distance') maxp = stress.data[itime, :, 5] for (eid, nid, fdi, maxpi) in zip(eids, nids, fiber_dist, maxp): print(eid, 'CEN/4' if nid == 0 else nid, fdi, maxpi)
def run_model(bdf_name=None, op2_name=None, f06_name=None, op4_name=None, dynamic_vars=None, f06_has_weight=True, vectorized=False, encoding=None): # pragma: no cover outputs = [] if bdf_name: bdf = BDF(debug=False, log=None) if dynamic_vars is not None: bdf.set_dynamic_syntax(dynamic_vars) bdf.read_bdf(bdf_name, encoding=encoding) bdf.write_bdf(bdf_name + '.out', interspersed=False) bdf.write_bdf(bdf_name + '.out', interspersed=True) outputs.append(bdf) if op2_name: op2 = OP2(debug=False) op2.read_op2(op2_name) op2.write_f06(op2_name[:-4] + '.test_op2.out', quiet=True) outputs.append(op2) if f06_name: f06 = F06(debug=False, log=None) f06.read_f06(f06_name) f06.write_f06(f06_name[:-4] + '.test_f06.out', quiet=True) outputs.append(f06) if f06_has_weight: assert f06.grid_point_weight.reference_point is not None else: assert f06.grid_point_weight.reference_point is None if op4_name: op4 = OP4() op4.read_op4(op4_name, matrix_names=None, precision='default') outputs.append(op4) assert len(outputs) > 0 if len(outputs) == 1: return outputs[0] return outputs
def test_opt_1(self): """tests SOL 200""" log = get_logger(level='warning') bdf_filename = os.path.join(MODEL_PATH, 'sol200', 'model_200.bdf') unused_model = read_bdf(bdf_filename, xref=True, debug=False) op2_filename = os.path.join(MODEL_PATH, 'sol200', 'model_200.op2') #bdf, op2 = run_model(bdf_filename, op2_filename, #f06_has_weight=False, vectorized=True, #encoding='utf-8') op2 = OP2(log=log, debug=True, debug_file='temp.debug') op2.read_op2(op2_filename) unused_subcase_ids = op2.subcase_key.keys() #for subcase_id in subcase_ids: #assert isinstance(subcase_id, integer_types), subcase_id #for key, dresp in sorted(model.dresps.items()): #print(dresp) #dresp.calculate(op2, subcase_id) os.remove('temp.debug')