-
Notifications
You must be signed in to change notification settings - Fork 115
/
test_cyclic.py
336 lines (247 loc) · 11.4 KB
/
test_cyclic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import sys
import os
import numpy as np
import pytest
from pyvista.plotting import system_supports_plotting
from pyvista.plotting.renderer import CameraPosition
import pyansys
from pyansys.examples import rstfile
try:
__file__
except:
__file__ = '/home/alex/afrl/python/source/pyansys/tests/test_cyclic.py'
HAS_FFMPEG = True
try:
import imageio_ffmpeg
except:
HAS_FFMPEG = False
is_python2 = sys.version_info.major == 2
path = os.path.dirname(os.path.abspath(__file__))
testfiles_path = os.path.join(path, 'testfiles')
cyclic_testfiles_path = os.path.join(path, 'cyclic_reader')
# modal result z axis
try:
result_z = pyansys.download_sector_modal()
result_z.positive_cyclic_dir = True
except:
result_z = None
# static result x axis
@pytest.fixture(scope='module')
def result_x():
filename = os.path.join(testfiles_path, 'cyc12.rst')
return pyansys.read_binary(filename)
@pytest.fixture(scope='module')
def cyclic_v182_z():
# static result z axis
filename = os.path.join(cyclic_testfiles_path, 'cyclic_v182.rst')
return pyansys.read_binary(filename)
@pytest.fixture(scope='module')
def cyclic_v182_z_with_comp():
# cyclic modal with component
filename = os.path.join(cyclic_testfiles_path, 'cyclic_v182_w_comp.rst')
return pyansys.read_binary(filename)
def test_non_cyclic():
with pytest.raises(Exception):
pyansys.CyclicResult(rstfile)
@pytest.mark.skipif(not system_supports_plotting(), reason="Requires active X Server")
@pytest.mark.skipif(result_z is None, reason="Requires result file")
def test_plot_z_cyc():
cpos = result_z.plot(off_screen=True)
assert isinstance(cpos, CameraPosition)
@pytest.mark.skipif(not system_supports_plotting(), reason="Requires active X Server")
def test_plot_x_cyc(result_x):
cpos = result_x.plot(off_screen=True)
assert isinstance(cpos, CameraPosition)
@pytest.mark.skipif(not system_supports_plotting(), reason="Requires active X Server")
def test_plot_component_rotor(cyclic_v182_z_with_comp):
cyclic_v182_z_with_comp.plot_nodal_solution(0, full_rotor=False,
node_components='REFINE',
sel_type_all=False,
off_screen=True)
cyclic_v182_z_with_comp.plot_nodal_solution(0, full_rotor=True,
node_components='REFINE',
sel_type_all=False, off_screen=True)
# result_z.plot_nodal_stress(20, 'Sx', node_components='REFINE',
# sel_type_all=False, off_screen=True)
# result.plot_principal_nodal_stress(20, 'SEQV',
# node_components='REFINE',
# sel_type_all=False,
# off_screen=True)
def test_element_stress_v182_non_cyclic():
"""
Generated ansys results with:
ansys.Post1()
ansys.Set(1, 1)
ansys.Header('OFF', 'OFF', 'OFF', 'OFF', 'OFF', 'OFF')
ansys.Format('', 'E', 80, 20)
ansys.Page(1E9, '', -1)
msg = ansys.Presol('S').splitlines()
ansys_element_stress = []
for line in msg:
if len(line) == 201:
ansys_element_stress.append(line)
ansys_element_stress = np.genfromtxt(ansys_element_stress)
ansys_enode = ansys_element_stress[:, 0].astype(np.int)
ansys_element_stress = ansys_element_stress[:, 1:]
"""
ansys_result_file = os.path.join(cyclic_testfiles_path, 'cyclic_v182.rst')
result = pyansys.read_binary(ansys_result_file)
element_stress, elemnum, enode = result.element_stress(0, False, False)
assert np.allclose(np.sort(elemnum), elemnum), 'elemnum must be sorted'
element_stress = np.vstack(element_stress)
enode = np.hstack(enode)
# cyclic model will only output the master sector
from_ansys = np.load(os.path.join(cyclic_testfiles_path, 'v182_presol.npz'))
assert np.allclose(from_ansys['enode'], enode)
assert np.allclose(from_ansys['element_stress'], element_stress)
def test_nodal_stress_v182_non_cyclic():
"""
Generated with:
msg = ansys.Prnsol('s').splitlines()
array = np.genfromtxt(msg[9:])
ansys_nnum = array[:, 0].astype(np.int)
ansys_stress = array[:, 1:]
"""
ansys_result_file = os.path.join(cyclic_testfiles_path, 'cyclic_v182.rst')
result = pyansys.rst.ResultFile(ansys_result_file, ignore_cyclic=True)
nnum, stress = result.nodal_stress(0)
from_ansys = np.load(os.path.join(cyclic_testfiles_path, 'v182_prnsol_s.npz'))
assert np.allclose(from_ansys['nnum'][:nnum.size], nnum)
assert np.allclose(from_ansys['stress'][:nnum.size], stress)
def test_full_x_nodal_solution(result_x):
""" need to open gui to output full rotor results """
from_ansys = np.load(os.path.join(cyclic_testfiles_path,
'prnsol_d_cyclic_x_full_v182.npz'))
ansys_nnum = from_ansys['nnum']
ansys_disp = from_ansys['disp']
# self = pyansys.read_binary(cyclic_x_filename)
rnum = 0
phase = 0
full_rotor = True
nnum, disp = result_x.nodal_solution(rnum, phase, full_rotor=True,
as_complex=False,
in_nodal_coord_sys=False)
assert np.allclose(np.sort(nnum), nnum), 'nnum must be sorted'
mask = np.in1d(nnum, ansys_nnum)
n = mask.sum()
tmp = ansys_disp.reshape(disp.shape[0], n, 3)
assert np.allclose(nnum[mask], ansys_nnum[:n])
assert np.allclose(disp[:, mask], tmp)
def test_full_z_nodal_solution(cyclic_v182_z):
""" need to open gui to output full rotor results """
from_ansys = np.load(os.path.join(cyclic_testfiles_path,
'prnsol_d_cyclic_z_full_v182.npz'))
ansys_nnum = from_ansys['nnum']
ansys_disp = from_ansys['disp']
rnum = 0
phase = 0
nnum, disp = cyclic_v182_z.nodal_solution(rnum, phase,
full_rotor=True,
as_complex=False,
in_nodal_coord_sys=False)
mask = np.in1d(nnum, ansys_nnum)
n = mask.sum()
tmp = ansys_disp.reshape(disp.shape[0], n, 3)
assert np.allclose(disp[:, mask], tmp)
@pytest.mark.skipif(not system_supports_plotting(), reason="Requires active X Server")
def test_full_x_nodal_solution_plot(result_x):
result_x.plot_nodal_solution(0, off_screen=True)
def test_full_x_nodal_stress(result_x):
""" need to open gui to output full rotor results """
from_ansys = np.load(os.path.join(cyclic_testfiles_path,
'prnsol_s_cyclic_x_full_v182.npz'))
ansys_nnum = from_ansys['nnum']
ansys_stress = from_ansys['stress']
rnum = 0
phase = 0
nnum, stress = result_x.nodal_stress(rnum, phase, full_rotor=True)
mask = np.in1d(nnum, ansys_nnum)
n = mask.sum()
tmp = ansys_stress.reshape(stress.shape[0], n, 6)
np.abs(stress[:, mask] - tmp)
assert np.allclose(stress[:, mask], tmp)
assert np.allclose(nnum[mask], ansys_nnum[:n])
def test_mode_table(cyclic_v182_z, result_x):
assert isinstance(cyclic_v182_z.mode_table, np.ndarray)
assert isinstance(result_x.mode_table, np.ndarray)
@pytest.mark.skipif(result_z is None, reason="Requires result file")
def test_mode_table_result_z():
assert isinstance(result_z.mode_table, np.ndarray)
@pytest.mark.skipif(result_z is None, reason="Requires result file")
def test_harmonic_index_to_cumulative():
# harmonic_index_to_cumulative
assert result_z.harmonic_index_to_cumulative(0, 0) == 0
assert result_z.harmonic_index_to_cumulative(1, 0) == 6
assert result_z.harmonic_index_to_cumulative(-7, 2) == 47
with pytest.raises(Exception):
result_z.harmonic_index_to_cumulative(10, 0)
with pytest.raises(Exception):
result_z.harmonic_index_to_cumulative(0, 6)
def test_full_x_principal_nodal_stress(result_x):
""" need to open gui to output full rotor results """
from_ansys = np.load(os.path.join(cyclic_testfiles_path,
'prnsol_p_cyclic_x_full_v182.npz'))
ansys_nnum = from_ansys['nnum']
ansys_stress = from_ansys['stress']
rnum = 0
phase = 0
nnum, stress = result_x.principal_nodal_stress(rnum, phase, full_rotor=True)
mask = np.in1d(nnum, ansys_nnum)
n = mask.sum()
tmp = ansys_stress.reshape(stress.shape[0], n, 5)
assert np.allclose(nnum[mask], ansys_nnum[:n])
assert np.allclose(stress[:, mask], tmp, atol=4E-3) # too loose
@pytest.mark.skipif(not system_supports_plotting(), reason="Requires active X Server")
@pytest.mark.skipif(not HAS_FFMPEG, reason="requires imageio_ffmpeg")
@pytest.mark.skipif(result_z is None, reason="Requires result file")
def test_animate_nodal_solution(tmpdir):
temp_movie = str(tmpdir.mkdir("tmpdir").join('tmp.mp4'))
result_z.animate_nodal_solution(0, nangles=20, movie_filename=temp_movie,
off_screen=True, loop=False)
assert os.path.isfile(temp_movie)
@pytest.mark.skipif(is_python2, reason="Python 2.7 has a bug when loading displacements")
@pytest.mark.skipif(result_z is None, reason="Requires result file")
def test_cyclic_z_harmonic_displacement():
from_ansys = np.load(os.path.join(cyclic_testfiles_path,
'prnsol_u_cyclic_z_full_v182_set_4_2.npz'))
ansys_nnum = from_ansys['nnum']
ansys_disp = from_ansys['disp']
unod, count = np.unique(ansys_nnum, return_counts=True)
unod = np.setdiff1d(unod[count == result_z.n_sector], 32)
mask = np.in1d(ansys_nnum, unod)
ansys_nnum = ansys_nnum[mask]
ansys_disp = ansys_disp[mask]
nnum, disp = result_z.nodal_solution((4, 2), full_rotor=True)
mask = np.in1d(nnum, ansys_nnum)
tmp = ansys_disp.reshape(disp.shape[0], mask.sum(), 3)
assert np.allclose(disp[:, mask], tmp, atol=1E-5)
@pytest.mark.skipif(not system_supports_plotting(), reason="Requires active X Server")
def test_plot_nodal_stress(result_x):
result_x.plot_nodal_stress(0, 'z', off_screen=True)
result_x.plot_nodal_stress(0, 'z', off_screen=False)
@pytest.mark.skipif(not system_supports_plotting(), reason="Requires active X Server")
def test_plot_nodal_stress(result_x):
result_x.plot_nodal_stress(0, 'z', off_screen=True, full_rotor=False)
@pytest.mark.skipif(not system_supports_plotting(), reason="Requires active X Server")
def test_plot_principal_nodal_stress(result_x):
result_x.plot_principal_nodal_stress(0, 'eqv', off_screen=True)
# def test_full_z_nodal_stress():
# """ need to open gui to output full rotor results """
# from_ansys = np.load(os.path.join(cyclic_testfiles_path,
# 'prnsol_s_cyclic_z_full_v182_set_1_1.npz'))
# ansys_nnum = from_ansys['nnum']
# ansys_stress = from_ansys['stress']
# rnum = 0
# phase = 0
# unod, count = np.unique(ansys_nnum, return_counts=True)
# unod = np.setdiff1d(unod[count == result_z.n_sector], 32)
# mask = np.in1d(ansys_nnum, unod)
# ansys_nnum = ansys_nnum[mask]
# ansys_stress = ansys_stress[mask]
# nnum, stress = result_z.nodal_stress(rnum, phase, full_rotor=True)
# mask = np.in1d(nnum, ansys_nnum)
# n = mask.sum()
# tmp = ansys_stress.reshape(stress.shape[0], n, ansys_stress.shape[1])
# assert np.allclose(stress[:, mask], tmp, atol=1E-5)
# if __name__ == '__main__':
# test_element_stress_v182_non_cyclic()