Skip to content

Commit 9ea5e0d

Browse files
committed
added support for Kratos mdpa files
1 parent fb9d157 commit 9ea5e0d

File tree

5 files changed

+2178
-0
lines changed

5 files changed

+2178
-0
lines changed

pygem/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@
2020
from .stephandler import StepHandler
2121
from .igeshandler import IgesHandler
2222
from .khandler import KHandler
23+
from .mdpahandler import MdpaHandler
2324
from .params import *

pygem/mdpahandler.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
"""
2+
Derived module from khandler.py to handle Kratos mesh (.mdpa) files.
3+
"""
4+
import numpy as np
5+
import pygem.filehandler as fh
6+
7+
8+
class MdpaHandler(fh.FileHandler):
9+
"""
10+
Kratos mesh file handler class
11+
12+
:cvar string infile: name of the input file to be processed.
13+
:cvar string outfile: name of the output file where to write in.
14+
:cvar list extensions: extensions of the input/output files. It is equal
15+
to '.k'.
16+
"""
17+
18+
def __init__(self):
19+
super(MdpaHandler, self).__init__()
20+
self.extensions = ['.mdpa']
21+
22+
def parse(self, filename):
23+
"""
24+
Method to parse the file `filename`. It returns a matrix with all the
25+
coordinates. It reads only the section after "Begin Nodes" of the
26+
mdpa files.
27+
28+
:param string filename: name of the input file.
29+
30+
:return: mesh_points: it is a `n_points`-by-3 matrix containing the
31+
coordinates of the points of the mesh.
32+
:rtype: numpy.ndarray
33+
"""
34+
self._check_filename_type(filename)
35+
self._check_extension(filename)
36+
self.infile = filename
37+
index = -9
38+
mesh_points = []
39+
with open(self.infile, 'r') as input_file:
40+
for num, line in enumerate(input_file):
41+
if line.startswith('Begin Nodes'):
42+
index = num
43+
if num == index + 1:
44+
if line.startswith('End Nodes'):
45+
break
46+
else:
47+
li = []
48+
for t in line.split()[1:]:
49+
try:
50+
li.append(float(t))
51+
except ValueError:
52+
pass
53+
mesh_points.append(li)
54+
index = num
55+
mesh_points = np.array(mesh_points)
56+
return mesh_points
57+
58+
def write(self, mesh_points, filename):
59+
"""
60+
Writes a .mdpa file, called filename, copying all the lines from
61+
self.filename but the coordinates. mesh_points is a matrix that
62+
contains the new coordinates to write in the .mdpa file.
63+
64+
:param numpy.ndarray mesh_points: it is a `n_points`-by-3 matrix
65+
containing the coordinates of the points of the mesh
66+
:param string filename: name of the output file.
67+
"""
68+
self._check_filename_type(filename)
69+
self._check_extension(filename)
70+
self._check_infile_instantiation()
71+
self.outfile = filename
72+
index = -9
73+
i = 0
74+
with open(self.outfile, 'w') as output_file:
75+
with open(self.infile, 'r') as input_file:
76+
for num, line in enumerate(input_file):
77+
if line.startswith('Begin Nodes'):
78+
index = num
79+
if num == index + 1:
80+
if line.startswith('End Nodes'):
81+
index = -9
82+
else:
83+
for j in range(0, 3):
84+
line = (line[:5 + 15 * (j)] +
85+
'{:15.10f}'.format(mesh_points[i][j]) +
86+
line[5 + 15 * (j + 1):])
87+
i += 1
88+
index = num
89+
output_file.write(line)

0 commit comments

Comments
 (0)