Skip to content

Commit

Permalink
Fix 2412 reading for rods
Browse files Browse the repository at this point in the history
add testcase
  • Loading branch information
nico-arnold committed Sep 21, 2023
1 parent e410efe commit 0062c9f
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 50 deletions.
81 changes: 47 additions & 34 deletions pyuff/datasets/dataset_2412.py
Original file line number Diff line number Diff line change
@@ -1,61 +1,64 @@
import numpy as np
import itertools

from ..tools import _opt_fields, _parse_header_line, check_dict_for_none

def _write2412(fh, dset):
try:
elt_type_dict = {'triangle': 3, 'quad': 4}
fh.write('%6i\n%6i%74s\n' % (-1, 2412, ' '))

for elem in dset['all']:
fh.write('%10i%10i%10i%10i%10i%10i\n' % (
elem['element_num'],
elem['f_descriptor'],
elem['phys_table'],
elem['mat_table'],
elem['color'],
elem['num_nodes'],
))
if elem['f_descriptor'] == 11:
# rods have to be written in 3 lines
fh.write('%10i%10i%10i\n' % (
elem['beam_orientation'],
elem['beam_foreend_cross'],
elem['beam_aftend_cross']
for el_type in dset:
if type(el_type) is not int:
# Skip 'type', 'triangle' and 'quad' indices
continue
for elem in dset[el_type]:
fh.write('%10i%10i%10i%10i%10i%10i\n' % (
elem['element_nums'],
elem['fe_descriptor'],
elem['phys_table'],
elem['mat_table'],
elem['color'],
elem['num_nodes'],
))
for ii in elem['nodes_nums']:
fh.write('%10i' % ii)
fh.write('\n')
if elem['fe_descriptor'] == 11:
# Rods have to be written in 3 lines - ad additional line here
fh.write('%10i%10i%10i\n' % (
elem['beam_orientation'],
elem['beam_foreend_cross'],
elem['beam_aftend_cross']
))
for ii in elem['nodes_nums']:
fh.write('%10i' % ii)
fh.write('\n')
fh.write('%6i\n' % -1)

except:
raise Exception('Error writing data-set #2412')


def _extract2412(block_data):
"""Extract element data - data-set 2412."""
dset = {'type': 2412, 'all': None}
# Define dictionary of possible elements types
elt_type_dict = {'3': 'triangle', '4': 'quad'}
dset = {'type': 2412}
# Define dictionary of possible elements types for legacy interface
elt_type_dict = {41: 'triangle', 44: 'quad'}
# Elements that are seen as rods and read as 3 lines
rods_dict = {11, 21, 22, 23, 24}

# Read data
try:
split_data = block_data.splitlines()
split_data = [a.split() for a in split_data][2:]

# Extract Records
rec1 = np.array([])
rec2 = []
dataset = []
i = 0
while i < len(split_data):
dict_tmp = dict()
line = split_data[i]
dict_tmp['element_num'] = int(line[0])
dict_tmp['f_descriptor'] = int(line[1])
dict_tmp['element_nums'] = int(line[0])
dict_tmp['fe_descriptor'] = int(line[1])
dict_tmp['phys_table'] = int(line[2])
dict_tmp['mat_table'] = int(line[3])
dict_tmp['color'] = int(line[4])
dict_tmp['num_nodes'] = int(line[5])
if dict_tmp['f_descriptor'] == 11:
if dict_tmp['fe_descriptor'] in rods_dict:
# element is a rod and covers 3 lines
dict_tmp['beam_orientation'] = int(split_data[i+1][0])
dict_tmp['beam_foreend_cross'] = int(split_data[i + 1][1])
Expand All @@ -66,17 +69,27 @@ def _extract2412(block_data):
# element is no rod and covers 2 lines
dict_tmp['nodes_nums'] = [int(e) for e in split_data[i+1]]
i += 2
desc = dict_tmp['f_descriptor']
desc = dict_tmp['fe_descriptor']
if not desc in dset:
dset[desc] = []
dset[desc].append(dict_tmp)
dataset.append(dict_tmp)
dset['all'] = dataset
for num, name in elt_type_dict.items():
# if we have one of the keys that are enabled for the legacy interface, add everything for that here
if num in dset.keys():
dset[name] = {
'element_nums': [e['element_nums'] for e in dset[num]],
'fe_descriptor': [e['fe_descriptor'] for e in dset[num]],
'phys_table': [e['phys_table'] for e in dset[num]],
'mat_table': [e['mat_table'] for e in dset[num]],
'color': [e['color'] for e in dset[num]],
'num_nodes': [e['num_nodes'] for e in dset[num]],
'nodes_nums': [e['nodes_nums'] for e in dset[num]],
}

return dset

except:
raise Exception('Error reading data-set #2412')
return dset


def prepare_2412(
Expand Down
46 changes: 30 additions & 16 deletions tests/test_2412.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,37 @@
import pyuff

def test_read_2412():
uff_ascii = pyuff.UFF('./data/heat_engine_housing.uff')
a = uff_ascii.read_sets(3)
np.testing.assert_array_equal(a[111][-1]['nodes_nums'], np.array([1, 3, 9, 10]))
np.testing.assert_array_equal(a[91][-1]['nodes_nums'], np.array([2, 3, 8]))
# -- Load small test file
uff_ascii = pyuff.UFF('./data/mesh_Oros-modal_uff15_uff2412.uff')
a = uff_ascii.read_sets(2)

# -- Test legacy interface
np.testing.assert_array_equal(a['quad']['nodes_nums'][0], np.array([1, 25, 48, 24]))
np.testing.assert_array_equal(a['quad']['nodes_nums'][-1], np.array([50, 74, 73, 49]))
# -- Test new interface
np.testing.assert_array_equal(a[44][0]['nodes_nums'], np.array([1, 25, 48, 24]))
np.testing.assert_array_equal(a[44][-1]['nodes_nums'], np.array([50, 74, 73, 49]))

# -- Load bigger test file with rods
uff_ascii = pyuff.UFF('./data/simcenter_exported_result.uff')
a = uff_ascii.read_sets(2)

# -- Test new interface
assert all(['type' in a, 21 in a, 91 in a, 94 in a, len(a) == 4])
np.testing.assert_array_equal(a[21][0]['nodes_nums'], np.array([1894, 1443]))
np.testing.assert_array_equal(a[94][0]['nodes_nums'], np.array([1870, 1046, 1, 946]))
assert sorted(list(a[21][0].keys())) == ['beam_aftend_cross', 'beam_foreend_cross', 'beam_orientation', 'color', 'element_nums', 'fe_descriptor', 'mat_table', 'nodes_nums', 'num_nodes', 'phys_table']



def test_read_write_2412_mixed():
# Read dataset 2412 in test file
#uff_ascii = pyuff.UFF('./data/mesh_test_uff2412_mixed.uff')
uff_ascii = pyuff.UFF('./data/heat_engine_housing.uff')
a = uff_ascii.read_sets(3)
uff_ascii = pyuff.UFF('./data/mesh_test_uff2412_mixed.uff')
a = uff_ascii.read_sets(2)

# Test
np.testing.assert_array_equal(a[111][-1]['nodes_nums'], np.array([1, 3, 9, 10]))
np.testing.assert_array_equal(a[91][-1]['nodes_nums'], np.array([2, 3, 8]))
np.testing.assert_array_equal(a['triangle']['nodes_nums'][-1], np.array([3, 6, 11]))
np.testing.assert_array_equal(a['quad']['nodes_nums'][-1], np.array([3, 4, 5, 6]))

# Write dataset 2412
uff_write = pyuff.UFF('./data/tmp.uff')
Expand All @@ -27,9 +45,10 @@ def test_read_write_2412_mixed():
# Read dataset 2412 in written file
uff_ascii = pyuff.UFF('./data/tmp.uff')
b = uff_ascii.read_sets(0)

# Test
np.testing.assert_array_equal(a[111], b[111])
np.testing.assert_array_equal(a[91], b[91])
np.testing.assert_array_equal(a[41], b[41])
np.testing.assert_array_equal(a[44], b[44])

def test_prepare_2412():
dict_2412 = pyuff.prepare_2412(return_full_dict=True)
Expand All @@ -54,8 +73,3 @@ def test_prepare_2412():
raise Exception('Not correct keys')
if x2['type'] != 2412:
raise Exception('Not correct type')

if __name__ == '__main__':
# test_read_2412()
test_prepare_2412()
test_read_write_2412_mixed()

0 comments on commit 0062c9f

Please sign in to comment.