Skip to content

Commit

Permalink
op2:
Browse files Browse the repository at this point in the history
 - recreating an OP2 object from an h5 file (only a few results are supported; obviously not well tested)

nastran gui:
 - loading said h5 file
  • Loading branch information
SteveDoyle2 committed Apr 20, 2018
1 parent 1b30d26 commit 4c8af08
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 6 deletions.
17 changes: 11 additions & 6 deletions pyNastran/converters/nastran/nastran_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
ForceTableResults, ElementalTableResults)

from pyNastran.op2.op2 import OP2
from pyNastran.op2.load_h5 import load_op2_from_h5
#from pyNastran.f06.f06_formatting import get_key0
from pyNastran.op2.op2_geom import OP2Geom

Expand Down Expand Up @@ -5608,18 +5609,19 @@ def _plot_applied_loads(self, model, cases, form0, icase, subcase_id,
print(sout)
return icase

def load_nastran_results(self, op2_filename):
def load_nastran_results(self, results_filename):
"""
Loads the Nastran results into the GUI
"""
self.scalarBar.VisibilityOn()
self.scalarBar.Modified()

if isinstance(op2_filename, string_types):
print("trying to read...%s" % op2_filename)
ext = os.path.splitext(op2_filename)[1].lower()
if isinstance(results_filename, string_types):
print("trying to read...%s" % results_filename)
ext = os.path.splitext(results_filename)[1].lower()

if ext == '.op2':
op2_filename = results_filename
model = OP2(log=self.log, debug=True)

if 0: # pragma: no cover
Expand Down Expand Up @@ -5677,9 +5679,12 @@ def load_nastran_results(self, op2_filename):
# print(model.get_op2_stats())

elif ext == '.nod':
self._load_patran_nod(op2_filename)
self._load_patran_nod(results_filename)
self.cycle_results_explicit() # start at nCase=0
return
elif ext == '.h5':
h5_filename = results_filename
model = load_op2_from_h5(h5_filename, log=self.log)
#elif ext == '.pch':
#raise NotImplementedError('*.pch is not implemented; filename=%r' % op2_filename)
#elif ext == '.f06':
Expand Down Expand Up @@ -5751,7 +5756,7 @@ def load_nastran_results(self, op2_filename):
# self.isubcase_name_map = {subcase_id : label for
# in iteritems(model.isubcase_name_map)}

form = self._fill_op2_output(op2_filename, cases, model, form, icase)
form = self._fill_op2_output(results_filename, cases, model, form, icase)
self._finish_results_io2(form, cases)

#name = 'spike'
Expand Down
140 changes: 140 additions & 0 deletions pyNastran/op2/load_h5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import os
import numpy as np
import h5py
from pyNastran.op2.op2 import OP2
from pyNastran.op2.tables.oug.oug_displacements import RealDisplacementArray, ComplexDisplacementArray
from pyNastran.op2.tables.oug.oug_velocities import RealVelocityArray, ComplexVelocityArray
from pyNastran.op2.tables.oug.oug_accelerations import RealAccelerationArray, ComplexAccelerationArray
from pyNastran.op2.tables.oug.oug_eigenvectors import RealEigenvectorArray, ComplexEigenvectorArray

from pyNastran.op2.tables.oqg_constraintForces.oqg_spc_forces import RealSPCForcesArray, ComplexSPCForcesArray
from pyNastran.op2.tables.oqg_constraintForces.oqg_mpc_forces import RealMPCForcesArray, ComplexMPCForcesArray
#from pyNastran.op2.tables.oqg_constraintForces.oqg_thermal_gradient_and_flux import RealTemperatureGradientAndFluxArray
from pyNastran.utils import print_bad_path

def cast(h5_result_attr):
if len(h5_result_attr.shape) == 0:
return np.array(h5_result_attr).tolist()
#raise NotImplementedError(h5_result_attr.dtype)
else:
return np.array(h5_result_attr)

TABLE_OBJ_MAP = {
'displacements' : (RealDisplacementArray, ComplexDisplacementArray),
'velocities' : (RealVelocityArray, ComplexVelocityArray),
'accelerations' : (RealAccelerationArray, ComplexAccelerationArray),
'eigenvectors' : (RealEigenvectorArray, ComplexEigenvectorArray),
'spc_forces' : (RealSPCForcesArray, ComplexSPCForcesArray),
'mpc_forces' : (RealMPCForcesArray, ComplexMPCForcesArray),
}
TABLE_OBJ_KEYS = list(TABLE_OBJ_MAP.keys())

def load_table(model, h5_result, real_obj, complex_obj, debug=False):
"""loads a RealEigenvectorArray/ComplexEigenvectorArray"""
is_real = cast(h5_result.get('is_real'))
is_complex = cast(h5_result.get('is_complex'))
nonlinear_factor = cast(h5_result.get('nonlinear_factor'))

data_names = cast(h5_result.get('data_names')).tolist()
sdata_names = [data_name + 's' for data_name in data_names]
data_code = {
'nonlinear_factor' : nonlinear_factor,
'sort_bits' : cast(h5_result.get('sort_bits')).tolist(),
'sort_method' : cast(h5_result.get('sort_method')),
'is_msc' : cast(h5_result.get('is_msc')),
'format_code' : cast(h5_result.get('format_code')),
'device_code' : cast(h5_result.get('device_code')),
'approach_code' : cast(h5_result.get('approach_code')),
'analysis_code' : cast(h5_result.get('analysis_code')),
'table_code' : cast(h5_result.get('table_code')),
'tCode' : cast(h5_result.get('tCode')),
'sort_code' : cast(h5_result.get('sort_code')),
'thermal' : cast(h5_result.get('thermal')),
'subtitle' : cast(h5_result.get('subtitle')),
'acoustic_flag' : cast(h5_result.get('acoustic_flag')),
'data_names' : data_names,
'name' : data_names[0],
}
is_sort1 = cast(h5_result.get('is_sort1'))
isubcase = cast(h5_result.get('isubcase'))
dt = nonlinear_factor

if is_real:
obj = real_obj(data_code, is_sort1, isubcase, dt,
f06_flag=False)
else:
raise NotImplementedError()
keys_to_skip = [
'class_name', 'headers', 'is_real', 'is_complex',
'is_sort1', 'is_sort2', 'table_name_str']
#time_keys = ['eigns', 'mode_cycles', 'modes']
for key in h5_result.keys():
if key in keys_to_skip:
continue
elif key in sdata_names:
if debug: # pragma: no cover
print(' *****key=%r' % key)
datai = cast(h5_result.get(key))
setattr(obj, key, datai)
setattr(obj, '_times', datai)
elif key not in data_code:
if debug: # pragma: no cover
print(' **key=%r' % key)
datai = cast(h5_result.get(key))
setattr(obj, key, datai)
return obj

def load_op2_from_h5(h5_filename, log=None):
assert os.path.exists(h5_filename), print_bad_path(h5_filename)
model = OP2(log=None)
model.op2_filename = h5_filename

h5_file = h5py.File(h5_filename, 'r')
debug = False
for key in h5_file.keys():
if key.startswith('Subcase'):
h5_subcase = h5_file.get(key)
print('subcase:')
for result_name in h5_subcase.keys():
if result_name in ['eigenvalues']:
print(' skipping %r...' % result_name)
elif result_name in TABLE_OBJ_KEYS:
if debug:
print(' %s:' % result_name)
real_obj, complex_obj = TABLE_OBJ_MAP[result_name]
h5_result = h5_subcase.get(result_name)
obj = load_table(model, h5_result, real_obj, complex_obj)

# isubcase, analysis_code, sort_method,
# count, ogs, superelement_adaptivity_index
opt_count = 0
ogs = 0
superelement_adaptivity_index = ''
# (1, 2, 1, 0, 0, u'')
key = (obj.isubcase, obj.analysis_code, obj.sort_method,
opt_count, ogs,
superelement_adaptivity_index)
model.eigenvectors[key] = obj
print(' loaded %r' % result_name)
else:
print('skipping %r...' % result_name)

#print(h5_subcase.keys())
elif key == 'info':
pass
elif key == 'matrices':
print('matrices:')
h5_matrix_group = h5_file.get(key)
for matrix_name in h5_matrix_group.keys():
print(' skipping %r...' % matrix_name)
h5_matrix = h5_matrix_group.get(matrix_name)
nkeys = len(h5_matrix.keys())
if not nkeys:
print(' %s is empty...skipping' % h5_matrix)
#for attr in h5_matrix.keys():
#print(' attr=%s' % attr)

else:
print('key = %r' % key)

return model

0 comments on commit 4c8af08

Please sign in to comment.