def save_load_deck(model, xref='standard', punch=True, run_remove_unused=True, run_convert=True, run_renumber=True, run_mirror=True, run_save_load=True, run_quality=True, write_saves=True, run_save_load_hdf5=True, run_mass_properties=True): """writes, re-reads, saves an obj, loads an obj, and returns the deck""" model.validate() model.pop_parse_errors() model.pop_xref_errors() bdf_file = StringIO() model.write_bdf(bdf_file, size=8, close=False) bdf_file.seek(0) model.write_bdf(bdf_file, size=16, close=False) bdf_file.seek(0) model.write_bdf(bdf_file, size=16, is_double=True, close=False) bdf_file.seek(0) if write_saves and model.save_file_structure: bdf_filenames = { 0: 'junk.bdf', } model.write_bdfs(bdf_filenames) os.remove('junk.bdf') if run_remove_unused: remove_unused(model) if run_convert: units_to = ['m', 'kg', 's'] units = ['ft', 'lbm', 's'] convert(model, units_to, units) model2 = BDF(log=model.log) #print(bdf_file.getvalue()) model2.read_bdf(bdf_file, punch=punch, xref=False) _cross_reference(model2, xref) model2.pop_parse_errors() model2.get_bdf_stats() model2.write_bdf('model2.bdf') nelements = len(model2.elements) + len(model2.masses) if run_mass_properties and len(model2.nodes) == 0 and nelements > 0: mass1, cg1, inertia1 = model2.mass_properties(reference_point=None, sym_axis=None) mass2, cg2, inertia2 = model2.mass_properties_nsm(reference_point=None, sym_axis=None) #if not quiet: #if model2.wtmass != 1.0: #print('weight = %s' % (mass1 / model2.wtmass)) #print('mass = %s' % mass1) #print('cg = %s' % cg1) #print('Ixx=%s, Iyy=%s, Izz=%s \nIxy=%s, Ixz=%s, Iyz=%s' % tuple(inertia1)) assert np.allclose(mass1, mass2), 'mass1=%s mass2=%s' % (mass1, mass2) assert np.allclose(cg1, cg2), 'mass=%s\ncg1=%s cg2=%s' % (mass1, cg1, cg2) assert np.allclose( inertia1, inertia2, atol=1e-5 ), 'mass=%s cg=%s\ninertia1=%s\ninertia2=%s\ndinertia=%s' % ( mass1, cg1, inertia1, inertia2, inertia1 - inertia2) mass3, cg3, inertia3 = mass_properties_breakdown(model2)[:3] assert np.allclose(mass1, mass3), 'mass1=%s mass3=%s' % (mass1, mass3) #assert np.allclose(cg1, cg3), 'mass=%s\ncg1=%s cg3=%s' % (mass1, cg1, cg3) if run_save_load: model2.save(obj_filename='model.obj', unxref=True) model3 = BDF(debug=False, log=model.log, mode='msc') model3.load(obj_filename='model.obj') os.remove('model.obj') else: model2.uncross_reference() model3 = model2 if run_save_load_hdf5 and IS_H5PY: model2.export_hdf5_filename('test.h5') model4 = BDF(log=model2.log) model4.load_hdf5_filename('test.h5') model4.validate() bdf_stream = StringIO() model4.write_bdf(bdf_stream, encoding=None, size=8, is_double=False, interspersed=False, enddata=None, write_header=True, close=True) for key, value in model2.card_count.items(): if key == 'ENDDATA': continue if key not in model4.card_count: msg = 'key=%r was not loaded to hdf5\nexpected=%s\nactual=%s' % ( key, model2.card_count, model4.card_count) #raise RuntimeError(msg) model.log.error(msg) cross_reference(model3, xref) if run_renumber: renumber('model2.bdf', model.log) if run_mirror: # we put embed this under renumber to prevent modifying an # existing model to prevent breaking tests # # shouldn't have any effect model2.bdf bdf_mirror('model2.bdf', plane='xz', log=model.log) os.remove('model2.bdf') if model.elements and run_quality: element_quality(model) return model3
def save_load_deck(model, xref='standard', punch=True, run_remove_unused=True, run_convert=True, run_renumber=True, run_mirror=True, run_save_load=True, run_quality=True, write_saves=True, run_save_load_hdf5=True, run_mass_properties=True, run_loads=True, run_test_bdf=True, run_op2_writer=True, run_op2_reader=True, op2_log_level: str = 'warning'): """writes, re-reads, saves an obj, loads an obj, and returns the deck""" if os.path.exists('junk.bdf'): os.remove('junk.bdf') model.set_error_storage(nparse_errors=0, stop_on_parsing_error=True, nxref_errors=0, stop_on_xref_error=True) model.validate() model.pop_parse_errors() model.pop_xref_errors() bdf_file = StringIO() model.write_bdf(bdf_file, size=8, close=False) bdf_file.seek(0) model.write_bdf(bdf_file, size=16, close=False) bdf_file.seek(0) model.write_bdf(bdf_file, size=16, is_double=True, close=False) bdf_file.seek(0) if write_saves and model.save_file_structure: bdf_filenames = { 0: 'junk.bdf', } model.write_bdfs(bdf_filenames) os.remove('junk.bdf') if run_convert: units_to = ['m', 'kg', 's'] units = ['ft', 'lbm', 's'] convert(model, units_to, units) model2 = BDF(log=model.log) #print(bdf_file.getvalue()) model2.read_bdf(bdf_file, punch=punch, xref=False) _cross_reference(model2, xref) model2.pop_parse_errors() model2.get_bdf_stats() model2.write_bdf('model2.bdf') if run_test_bdf: folder = '' log_error = get_logger(log=None, level='error', encoding='utf-8') test_bdf(folder, 'model2.bdf', stop_on_failure=True, punch=punch, quiet=True, log=log_error) os.remove('model2.test_bdf.bdf') nelements = len(model2.elements) + len(model2.masses) nnodes = len(model2.nodes) + len(model2.spoints) + len(model2.epoints) _run_mass_properties(model2, nnodes, nelements, run_mass_properties=run_mass_properties) _run_loads(model2, nelements, run_loads=run_loads) if run_save_load: model2.save(obj_filename='model.obj', unxref=True) model3 = BDF(debug=False, log=model.log, mode='msc') model3.load(obj_filename='model.obj') os.remove('model.obj') else: model2.uncross_reference() model3 = model2 _run_hdf5(model2, model.log, run_save_load_hdf5=run_save_load_hdf5) cross_reference(model3, xref) if run_renumber: renumber('model2.bdf', model.log) if run_mirror: # we put this under renumber to prevent modifying an # existing model to prevent breaking tests # # shouldn't have any effect model2.bdf model_mirrored = bdf_mirror('model2.bdf', plane='xz', log=model.log)[0] model_mirrored.write_bdf('mirrored2.bdf') read_bdf('mirrored2.bdf', log=model.log) os.remove('mirrored2.bdf') os.remove('model2.bdf') if model.elements and run_quality: element_quality(model) if run_op2_writer: op2_geom_model = attach_op2_results_to_bdf(model, op2_model=None) from pyNastran.op2.op2_geom import read_op2_geom table_name = 'OUGV1' node_gridtype = np.zeros((10, 2), dtype='int32') node_gridtype[:, 0] = np.arange(1, 11) data = np.zeros((1, 10, 6), dtype='float32') isubcase = 1 disp = RealDisplacementArray.add_static_case(table_name, node_gridtype, data, isubcase, is_sort1=True) op2_geom_model.displacements[isubcase] = disp op2_filename = 'spike.op2' bkp_log = op2_geom_model.log op2_geom_model.log = get_logger(log=None, level=op2_log_level, encoding='utf-8') op2_geom_model.write_op2(op2_filename, post=-1, endian=b'<', skips=None, nastran_format='nx') if run_op2_reader: unused_op2_geom = read_op2_geom(op2_filename, log=op2_geom_model.log, xref=False) else: frame = inspect.currentframe() call_frame = inspect.getouterframes(frame, 2) op2_geom_model.log.warning('skipping op2 reader for %s' % call_frame[1][3]) op2_geom_model.log = bkp_log os.remove(op2_filename) if run_remove_unused: remove_unused(model) return model3
def save_load_deck(model, xref='standard', punch=True, run_remove_unused=True, run_convert=True, run_renumber=True, run_mirror=True, run_save_load=True, run_quality=True, write_saves=True, run_save_load_hdf5=True): """writes, re-reads, saves an obj, loads an obj, and returns the deck""" model.validate() model.pop_parse_errors() model.pop_xref_errors() bdf_file = StringIO() model.write_bdf(bdf_file, size=8, close=False) bdf_file.seek(0) model.write_bdf(bdf_file, size=16, close=False) bdf_file.seek(0) model.write_bdf(bdf_file, size=16, is_double=True, close=False) bdf_file.seek(0) if write_saves and model.save_file_structure: bdf_filenames = { 0: 'junk.bdf', } model.write_bdfs(bdf_filenames) os.remove('junk.bdf') if run_remove_unused: remove_unused(model) if run_convert: units_to = ['m', 'kg', 's'] units = ['ft', 'lbm', 's'] convert(model, units_to, units) model2 = BDF(log=model.log) #print(bdf_file.getvalue()) model2.read_bdf(bdf_file, punch=punch, xref=False) _cross_reference(model2, xref) model2.pop_parse_errors() model2.get_bdf_stats() model2.write_bdf('model2.bdf') if run_save_load: model2.save(obj_filename='model.obj', unxref=True) model3 = BDF(debug=False, log=model.log, mode='msc') model3.load(obj_filename='model.obj') os.remove('model.obj') else: model2.uncross_reference() model3 = model2 if run_save_load_hdf5 and IS_H5PY: model2.export_to_hdf5_filename('test.h5') model4 = BDF(log=model2.log) model4.load_hdf5_filename('test.h5') model4.validate() bdf_stream = StringIO() model4.write_bdf(bdf_stream, encoding=None, size=8, is_double=False, interspersed=False, enddata=None, write_header=True, close=True) for key, value in model2.card_count.items(): if key == 'ENDDATA': continue if key not in model4.card_count: msg = 'key=%r was not loaded to hdf5\nexpected=%s\nactual=%s' % ( key, model2.card_count, model4.card_count) #raise RuntimeError(msg) model.log.error(msg) cross_reference(model3, xref) if run_renumber: renumber('model2.bdf', model.log) if run_mirror: # we put embed this under renumber to prevent modifying an # existing model to prevent breaking tests # # shouldn't have any effect model2.bdf bdf_mirror('model2.bdf', plane='xz', log=model.log) os.remove('model2.bdf') if model.elements and run_quality: element_quality(model) return model3