Skip to content

Commit cf01463

Browse files
Feature elmerhandler (#186)
* Add test_elmerhandler * Add test_elmer_nodes * Add elmer_handler
1 parent 2ffbdf2 commit cf01463

File tree

5 files changed

+708
-1
lines changed

5 files changed

+708
-1
lines changed

pygem/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
PyGeM init
33
"""
44
# __all__ = [
5-
# 'affine', 'filehandler', 'freeform', 'radial', 'openfhandler',
5+
# 'affine', 'filehandler', 'freeform', 'radial', 'openfhandler', 'elmerhandler',
66
# 'stlhandler', 'unvhandler', 'vtkhandler', 'nurbshandler', 'stephandler',
77
# 'igeshandler', 'utils', 'gui', 'khandler', 'idw'
88
# ]
@@ -13,6 +13,7 @@
1313
from .idw import IDW
1414
from .filehandler import FileHandler
1515
from .openfhandler import OpenFoamHandler
16+
from .elmerhandler import ElmerHandler
1617
from .stlhandler import StlHandler
1718
from .unvhandler import UnvHandler
1819
from .vtkhandler import VtkHandler

pygem/elmerhandler.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
"""
2+
Derived module from filehandler.py to handle ElmerFEM files.
3+
"""
4+
import numpy as np
5+
import pygem.filehandler as fh
6+
7+
8+
class ElmerHandler(fh.FileHandler):
9+
"""
10+
Elmer 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
15+
is equal to ['.node'] since elmer files do not have extension.
16+
"""
17+
18+
def __init__(self):
19+
super(ElmerHandler, self).__init__()
20+
self.extensions = ['.nodes']
21+
22+
def parse(self, filename):
23+
"""
24+
Method to parse the `filename`. It returns a matrix with all
25+
the coordinates.
26+
27+
:param string filename: name of the input file.
28+
29+
:return: mesh_points: it is a `n_points`-by-3 matrix containing
30+
the coordinates of the points of the mesh
31+
:rtype: numpy.ndarray
32+
33+
.. todo::
34+
35+
- specify when it works
36+
"""
37+
self._check_filename_type(filename)
38+
self._check_extension(filename)
39+
40+
self.infile = filename
41+
42+
i = 0
43+
n_points = 0
44+
with open(self.infile, 'r') as input_file:
45+
for line in input_file:
46+
n_points += 1
47+
mesh_points = np.zeros(shape=(n_points, 3))
48+
49+
with open(self.infile, 'r') as input_file:
50+
51+
i = 0
52+
for line in input_file:
53+
numbers = line.split() #[n1 p x y z] -> [x y z]
54+
del numbers[0:2]
55+
56+
j = 0
57+
for number in numbers:
58+
59+
mesh_points[i][j] = float(number)
60+
j += 1
61+
i += 1
62+
63+
return mesh_points
64+
65+
def write(self, mesh_points, filename):
66+
"""
67+
Writes a elmer file, called filename, copying all the
68+
lines from self.filename but the coordinates. mesh_points
69+
is a matrix that contains the new coordinates to write in
70+
the elmer file.
71+
72+
:param numpy.ndarray mesh_points: it is a `n_points`-by-3
73+
matrix containing the coordinates of the points of the mesh.
74+
:param string filename: name of the output file.
75+
76+
.. todo:: DOCS
77+
"""
78+
self._check_filename_type(filename)
79+
self._check_extension(filename)
80+
self._check_infile_instantiation()
81+
self.outfile = filename
82+
83+
n_points = mesh_points.shape[0]
84+
i = 0
85+
with open(self.infile, 'r') as input_file, open(self.outfile,
86+
'w') as output_file:
87+
for line in input_file:
88+
numbers = line.split() #[n1 p x y z]
89+
90+
output_file.write(numbers[0] + ' ' +numbers[1] + ' ' \
91+
+ str(mesh_points[i][0]) + ' ' + str(mesh_points[i][1]) \
92+
+ ' ' + str(mesh_points[i][2]))
93+
i += 1
94+
95+
if i != n_points:
96+
output_file.write('\n')
97+
Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
1 -1 0 1 0
2+
2 -1 0 1.1 0
3+
3 -1 0 1.5 0
4+
4 -1 0 1.91666666667 0
5+
5 -1 0 2 0
6+
6 -1 1.05016851115 1 0
7+
7 -1 1.05016851115 1.1 0
8+
8 -1 1.05016851115 1.5 0
9+
9 -1 1.05016851115 1.91666666667 0
10+
10 -1 1.05016851115 2 0
11+
11 -1 1.65344439132 1 0
12+
12 -1 1.65344439132 1.1 0
13+
13 -1 1.65344439132 1.5 0
14+
14 -1 1.65344439132 1.91666666667 0
15+
15 -1 1.65344439132 2 0
16+
16 -1 2 0 0
17+
17 -1 2 0.125 0
18+
18 -1 2 0.5 0
19+
19 -1 2 0.916666666667 0
20+
20 -1 2 1 0
21+
21 -1 2 1.1 0
22+
22 -1 2 1.5 0
23+
23 -1 2 1.91666666667 0
24+
24 -1 2 2 0
25+
25 -1 2.45899592058 0 0
26+
26 -1 2.45899592058 0.125 0
27+
27 -1 2.45899592058 0.5 0
28+
28 -1 2.45899592058 0.916666666667 0
29+
29 -1 2.45899592058 1 0
30+
30 -1 2.45899592058 1.1 0
31+
31 -1 2.45899592058 1.5 0
32+
32 -1 2.45899592058 1.91666666667 0
33+
33 -1 2.45899592058 2 0
34+
34 -1 2.94650796191 0 0
35+
35 -1 2.94650796191 0.125 0
36+
36 -1 2.94650796191 0.5 0
37+
37 -1 2.94650796191 0.916666666667 0
38+
38 -1 2.94650796191 1 0
39+
39 -1 2.94650796191 1.1 0
40+
40 -1 2.94650796191 1.5 0
41+
41 -1 2.94650796191 1.91666666667 0
42+
42 -1 2.94650796191 2 0
43+
43 -1 3.46430775004 0 0
44+
44 -1 3.46430775004 0.125 0
45+
45 -1 3.46430775004 0.5 0
46+
46 -1 3.46430775004 0.916666666667 0
47+
47 -1 3.46430775004 1 0
48+
48 -1 3.46430775004 1.1 0
49+
49 -1 3.46430775004 1.5 0
50+
50 -1 3.46430775004 1.91666666667 0
51+
51 -1 3.46430775004 2 0
52+
52 -1 4.0142769772 0 0
53+
53 -1 4.0142769772 0.125 0
54+
54 -1 4.0142769772 0.5 0
55+
55 -1 4.0142769772 0.916666666667 0
56+
56 -1 4.0142769772 1 0
57+
57 -1 4.0142769772 1.1 0
58+
58 -1 4.0142769772 1.5 0
59+
59 -1 4.0142769772 1.91666666667 0
60+
60 -1 4.0142769772 2 0
61+
61 -1 4.59841423979 0 0
62+
62 -1 4.59841423979 0.125 0
63+
63 -1 4.59841423979 0.5 0
64+
64 -1 4.59841423979 0.916666666667 0
65+
65 -1 4.59841423979 1 0
66+
66 -1 4.59841423979 1.1 0
67+
67 -1 4.59841423979 1.5 0
68+
68 -1 4.59841423979 1.91666666667 0
69+
69 -1 4.59841423979 2 0
70+
70 -1 5.21884230142 0 0
71+
71 -1 5.21884230142 0.125 0
72+
72 -1 5.21884230142 0.5 0
73+
73 -1 5.21884230142 0.916666666667 0
74+
74 -1 5.21884230142 1 0
75+
75 -1 5.21884230142 1.1 0
76+
76 -1 5.21884230142 1.5 0
77+
77 -1 5.21884230142 1.91666666667 0
78+
78 -1 5.21884230142 2 0
79+
79 -1 5.87781580696 0 0
80+
80 -1 5.87781580696 0.125 0
81+
81 -1 5.87781580696 0.5 0
82+
82 -1 5.87781580696 0.916666666667 0
83+
83 -1 5.87781580696 1 0
84+
84 -1 5.87781580696 1.1 0
85+
85 -1 5.87781580696 1.5 0
86+
86 -1 5.87781580696 1.91666666667 0
87+
87 -1 5.87781580696 2 0
88+
88 -1 6.57772947602 0 0
89+
89 -1 6.57772947602 0.125 0
90+
90 -1 6.57772947602 0.5 0
91+
91 -1 6.57772947602 0.916666666667 0
92+
92 -1 6.57772947602 1 0
93+
93 -1 6.57772947602 1.1 0
94+
94 -1 6.57772947602 1.5 0
95+
95 -1 6.57772947602 1.91666666667 0
96+
96 -1 6.57772947602 2 0
97+
97 -1 7.32112680539 0 0
98+
98 -1 7.32112680539 0.125 0
99+
99 -1 7.32112680539 0.5 0
100+
100 -1 7.32112680539 0.916666666667 0
101+
101 -1 7.32112680539 1 0
102+
102 -1 7.32112680539 1.1 0
103+
103 -1 7.32112680539 1.5 0
104+
104 -1 7.32112680539 1.91666666667 0
105+
105 -1 7.32112680539 2 0
106+
106 -1 8.11070931212 0 0
107+
107 -1 8.11070931212 0.125 0
108+
108 -1 8.11070931212 0.5 0
109+
109 -1 8.11070931212 0.916666666667 0
110+
110 -1 8.11070931212 1 0
111+
111 -1 8.11070931212 1.1 0
112+
112 -1 8.11070931212 1.5 0
113+
113 -1 8.11070931212 1.91666666667 0
114+
114 -1 8.11070931212 2 0
115+
115 -1 8.9493463509 0 0
116+
116 -1 8.9493463509 0.125 0
117+
117 -1 8.9493463509 0.5 0
118+
118 -1 8.9493463509 0.916666666667 0
119+
119 -1 8.9493463509 1 0
120+
120 -1 8.9493463509 1.1 0
121+
121 -1 8.9493463509 1.5 0
122+
122 -1 8.9493463509 1.91666666667 0
123+
123 -1 8.9493463509 2 0
124+
124 -1 9.84008554131 0 0
125+
125 -1 9.84008554131 0.125 0
126+
126 -1 9.84008554131 0.5 0
127+
127 -1 9.84008554131 0.916666666667 0
128+
128 -1 9.84008554131 1 0
129+
129 -1 9.84008554131 1.1 0
130+
130 -1 9.84008554131 1.5 0
131+
131 -1 9.84008554131 1.91666666667 0
132+
132 -1 9.84008554131 2 0
133+
133 -1 10.7861638429 0 0
134+
134 -1 10.7861638429 0.125 0
135+
135 -1 10.7861638429 0.5 0
136+
136 -1 10.7861638429 0.916666666667 0
137+
137 -1 10.7861638429 1 0
138+
138 -1 10.7861638429 1.1 0
139+
139 -1 10.7861638429 1.5 0
140+
140 -1 10.7861638429 1.91666666667 0
141+
141 -1 10.7861638429 2 0
142+
142 -1 11.7910193185 0 0
143+
143 -1 11.7910193185 0.125 0
144+
144 -1 11.7910193185 0.5 0
145+
145 -1 11.7910193185 0.916666666667 0
146+
146 -1 11.7910193185 1 0
147+
147 -1 11.7910193185 1.1 0
148+
148 -1 11.7910193185 1.5 0
149+
149 -1 11.7910193185 1.91666666667 0
150+
150 -1 11.7910193185 2 0
151+
151 -1 12.858303628 0 0
152+
152 -1 12.858303628 0.125 0
153+
153 -1 12.858303628 0.5 0
154+
154 -1 12.858303628 0.916666666667 0
155+
155 -1 12.858303628 1 0
156+
156 -1 12.858303628 1.1 0
157+
157 -1 12.858303628 1.5 0
158+
158 -1 12.858303628 1.91666666667 0
159+
159 -1 12.858303628 2 0
160+
160 -1 13.9918952985 0 0
161+
161 -1 13.9918952985 0.125 0
162+
162 -1 13.9918952985 0.5 0
163+
163 -1 13.9918952985 0.916666666667 0
164+
164 -1 13.9918952985 1 0
165+
165 -1 13.9918952985 1.1 0
166+
166 -1 13.9918952985 1.5 0
167+
167 -1 13.9918952985 1.91666666667 0
168+
168 -1 13.9918952985 2 0
169+
169 -1 15.1959138192 0 0
170+
170 -1 15.1959138192 0.125 0
171+
171 -1 15.1959138192 0.5 0
172+
172 -1 15.1959138192 0.916666666667 0
173+
173 -1 15.1959138192 1 0
174+
174 -1 15.1959138192 1.1 0
175+
175 -1 15.1959138192 1.5 0
176+
176 -1 15.1959138192 1.91666666667 0
177+
177 -1 15.1959138192 2 0
178+
178 -1 16.4747346115 0 0
179+
179 -1 16.4747346115 0.125 0
180+
180 -1 16.4747346115 0.5 0
181+
181 -1 16.4747346115 0.916666666667 0
182+
182 -1 16.4747346115 1 0
183+
183 -1 16.4747346115 1.1 0
184+
184 -1 16.4747346115 1.5 0
185+
185 -1 16.4747346115 1.91666666667 0
186+
186 -1 16.4747346115 2 0
187+
187 -1 17.8330049293 0 0
188+
188 -1 17.8330049293 0.125 0
189+
189 -1 17.8330049293 0.5 0
190+
190 -1 17.8330049293 0.916666666667 0
191+
191 -1 17.8330049293 1 0
192+
192 -1 17.8330049293 1.1 0
193+
193 -1 17.8330049293 1.5 0
194+
194 -1 17.8330049293 1.91666666667 0
195+
195 -1 17.8330049293 2 0
196+
196 -1 19.2756607474 0 0
197+
197 -1 19.2756607474 0.125 0
198+
198 -1 19.2756607474 0.5 0
199+
199 -1 19.2756607474 0.916666666667 0
200+
200 -1 19.2756607474 1 0
201+
201 -1 19.2756607474 1.1 0
202+
202 -1 19.2756607474 1.5 0
203+
203 -1 19.2756607474 1.91666666667 0
204+
204 -1 19.2756607474 2 0
205+
205 -1 20.8079446987 0 0
206+
206 -1 20.8079446987 0.125 0
207+
207 -1 20.8079446987 0.5 0
208+
208 -1 20.8079446987 0.916666666667 0
209+
209 -1 20.8079446987 1 0
210+
210 -1 20.8079446987 1.1 0
211+
211 -1 20.8079446987 1.5 0
212+
212 -1 20.8079446987 1.91666666667 0
213+
213 -1 20.8079446987 2 0
214+
214 -1 22.435425126 0 0
215+
215 -1 22.435425126 0.125 0
216+
216 -1 22.435425126 0.5 0
217+
217 -1 22.435425126 0.916666666667 0
218+
218 -1 22.435425126 1 0
219+
219 -1 22.435425126 1.1 0
220+
220 -1 22.435425126 1.5 0
221+
221 -1 22.435425126 1.91666666667 0
222+
222 -1 22.435425126 2 0
223+
223 -1 24.1640163177 0 0
224+
224 -1 24.1640163177 0.125 0
225+
225 -1 24.1640163177 0.5 0
226+
226 -1 24.1640163177 0.916666666667 0
227+
227 -1 24.1640163177 1 0
228+
228 -1 24.1640163177 1.1 0
229+
229 -1 24.1640163177 1.5 0
230+
230 -1 24.1640163177 1.91666666667 0
231+
231 -1 24.1640163177 2 0
232+
232 -1 26 0 0
233+
233 -1 26 0.125 0
234+
234 -1 26 0.5 0
235+
235 -1 26 0.916666666667 0
236+
236 -1 26 1 0
237+
237 -1 26 1.1 0
238+
238 -1 26 1.5 0
239+
239 -1 26 1.91666666667 0
240+
240 -1 26 2 0

0 commit comments

Comments
 (0)