Skip to content

Commit 7b798cb

Browse files
authored
Merge pull request #191 from ndem0/version2
Version2
2 parents fd54c9b + 5ff9dea commit 7b798cb

File tree

6 files changed

+13358
-38
lines changed

6 files changed

+13358
-38
lines changed

pygem/khandler.py

Lines changed: 91 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Derived module from filehandler.py to handle LS-DYNA keyword (.k) files.
33
"""
4+
import re
45
import numpy as np
56
import pygem.filehandler as fh
67

@@ -30,27 +31,39 @@ def parse(self, filename):
3031
coordinates of the points of the mesh.
3132
:rtype: numpy.ndarray
3233
"""
34+
3335
self._check_filename_type(filename)
3436
self._check_extension(filename)
3537
self.infile = filename
36-
index = -9
38+
3739
mesh_points = []
40+
node_indicator = False
41+
3842
with open(self.infile, 'r') as input_file:
3943
for num, line in enumerate(input_file):
44+
45+
expression = re.compile(r'(.+?)(?:,|$)')
46+
expression = expression.findall(line)
47+
48+
if line.startswith("$"):
49+
continue
50+
4051
if line.startswith('*NODE'):
41-
index = num
42-
if num == index + 1:
43-
if line.startswith('$'):
44-
index = num
45-
elif line.startswith('*'):
46-
index = -9
47-
else:
48-
l = []
49-
l.append(float(line[8:24]))
50-
l.append(float(line[24:40]))
51-
l.append(float(line[40:56]))
52-
mesh_points.append(l)
53-
index = num
52+
node_indicator = True
53+
continue
54+
55+
if line.startswith('*ELEMENT'):
56+
break
57+
58+
if not node_indicator:
59+
pass
60+
else:
61+
if len(expression) == 1:
62+
expression = re.findall(r'\S+', expression[0])
63+
l = [float(expression[1]), float(expression[2]),
64+
float(expression[3])]
65+
mesh_points.append(l)
66+
5467
mesh_points = np.array(mesh_points)
5568
return mesh_points
5669

@@ -68,22 +81,71 @@ def write(self, mesh_points, filename):
6881
self._check_extension(filename)
6982
self._check_infile_instantiation()
7083
self.outfile = filename
71-
index = -9
84+
7285
i = 0
86+
node_indicator = False
87+
7388
with open(self.outfile, 'w') as output_file:
7489
with open(self.infile, 'r') as input_file:
75-
for num, line in enumerate(input_file):
90+
for _, line in enumerate(input_file):
91+
get_num = re.findall(r'[-+]?[0-9]*\.?[0-9]+', line)
92+
93+
# Write header files
94+
if line.startswith('$'):
95+
output_file.write(line)
96+
continue
97+
98+
# Change the node indicator if you find the elements section
99+
if line.startswith('*ELEMENT'):
100+
node_indicator = False
101+
102+
# Change the nodes indicator if you find the nodes section
76103
if line.startswith('*NODE'):
77-
index = num
78-
if num == index + 1:
79-
if line.startswith('$'):
80-
index = num
81-
elif line.startswith('*'):
82-
index = -9
83-
else:
84-
for j in range(0, 3):
85-
line = line[:8 + 16 * (j)] + '{:16.10f}'.format(
86-
mesh_points[i][j]) + line[8 + 16 * (j + 1):]
87-
i += 1
88-
index = num
89-
output_file.write(line)
104+
node_indicator = True
105+
output_file.write(line)
106+
continue
107+
108+
# If in the nodes section append the mesh points otherwise copy the data from parsed file
109+
if not node_indicator:
110+
output_file.write(line)
111+
else:
112+
113+
# Split lines to find decimeter
114+
split_line = line.split(" ")
115+
116+
# Format the data into correct format
117+
data = [int(get_num[0]),
118+
'{:.10f}'.format(float(mesh_points[i][0])),
119+
'{:.10f}'.format(float(mesh_points[i][1])),
120+
'{:.10f}'.format(float(mesh_points[i][2]))]
121+
122+
comma_seperator = False
123+
pointer = 0
124+
125+
for index, value in enumerate(split_line):
126+
127+
if value:
128+
129+
if value[len(value) - 1] == ",":
130+
comma_seperator = True
131+
new_str = value.replace(value[:-1], str(data[pointer]))
132+
split_line[index] = new_str
133+
134+
else:
135+
new_str = value.replace(value, str(data[pointer]))
136+
split_line[index] = new_str
137+
if float(data[pointer]) < 0 and not comma_seperator:
138+
del split_line[index - 1]
139+
140+
pointer += 1
141+
else:
142+
pass
143+
144+
original_str = ""
145+
for j in split_line:
146+
original_str += j + " "
147+
original_str = original_str[:-1]
148+
149+
output_file.write(original_str + "\n")
150+
151+
i += 1

test.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@
66
test_defaults = [
77
'tests/test_freeform.py',
88
'tests/test_idwparams.py',
9-
'tests/test_affine.py',
109
'tests/test_idw.py',
1110
'tests/test_khandler.py',
1211
'tests/test_mdpahandler.py',
1312
'tests/test_openfhandler.py',
14-
'tests/test_package.py',
15-
'tests/test_radial.py',
1613
'tests/test_rbfparams.py',
1714
'tests/test_stlhandler.py',
1815
'tests/test_unvhandler.py',
@@ -27,15 +24,15 @@
2724
default_argv = ['--tests'] + test_defaults
2825
cad_argv = ['--tests'] + test_cad
2926

27+
return_value = 0 # Success
28+
3029
try:
3130
import pygem.cad
32-
nose.run(argv=cad_argv)
31+
return_value = 1 if nose.run(argv=cad_argv) is False else 0
3332
except:
3433
print('module OCC not found, skip tests for CAD')
3534

36-
nose.run(argv=default_argv)
37-
38-
39-
40-
35+
return_value = 1 if nose.run(argv=default_argv) is False else 0
4136

37+
import sys
38+
sys.exit(int(return_value))

0 commit comments

Comments
 (0)