11"""
22Derived module from filehandler.py to handle LS-DYNA keyword (.k) files.
33"""
4+ import re
45import numpy as np
56import 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
0 commit comments