forked from SAP-archive/fedem-solvers
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_restart.py
More file actions
181 lines (155 loc) · 5.29 KB
/
test_restart.py
File metadata and controls
181 lines (155 loc) · 5.29 KB
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
# SPDX-FileCopyrightText: 2023 SAP SE
#
# SPDX-License-Identifier: Apache-2.0
#
# This file is part of FEDEM - https://openfedem.org
"""
This Python test basically does the same as one of the solver tests
located in the folder `../../../solverTests/AW-N80-80-Windturbine`.
It therefore reuses the input-files from that folder.
The test uses two environment variables:
FEDEM_SOLVER = Full path to the solver shared object library
TEST_DIR = Full path to parent folder of the solver tests in the build tree
"""
from os import environ
from fedempy.solver import FedemSolver
from test_utils import compare_lists, read_reference_data
# List of function IDs to extract results for
fId = [
155,
156,
153,
154,
135,
136,
139,
140,
143,
144,
147,
148,
149,
150,
145,
146,
141,
142,
137,
138,
]
n_out = len(fId)
ierr = 0
n_step = 100 # Time window size
# Start the fedem solver in the $TEST_DIR/AW-N80-80-Windturbine folder.
wrkdir = environ["TEST_DIR"] + "/AW-N80-80-Windturbine"
# From that folder, read solver options from the Setup.fco file,
# and read model data from the Model.fsi and Gages.fsi files.
# Run with -recovery=2 to perform gage recovery during time integration.
slv_opts = [
"-cwd=" + wrkdir,
"-fco=Setup.fco",
"-fsifile=Model.fsi",
"-fsi2file=Gages.fsi",
"-recovery=2",
]
solver = FedemSolver(environ["FEDEM_SOLVER"], slv_opts, True)
print("\n#### Running dynamics solver in", wrkdir)
print("Dimension of equation system:", solver.get_system_size())
print("State array size:", solver.state_size)
print("Gages array size:", solver.gauge_size)
# Read reference values to compare with from file
references = read_reference_data(wrkdir + "/Responses.asc")
print("Comparing with response variables in Responses.asc", len(references))
# Outer time window loop
step = 0
while ierr == 0:
# Caution: We here assume the time step size in the model is constant
t = solver.get_current_time()
dt = solver.get_next_time() - t
# Invoke the dynamics solver for this time window
outputs, do_continue = solver.solve_window(n_step, None, fId)
if not (do_continue and solver.ierr.value == 0):
break
# Save current state to core array
if not solver.save_state():
exit(-99)
if not solver.save_gauges():
exit(-99)
# Compare the responses
for i in range(n_step):
output = [float(outputs[i * n_out + j]) for j in range(n_out)]
output.insert(0, float(t + dt * (i + 1)))
ierr += compare_lists(output[0], output, references[step], 1.0e-4)
step += 1
if ierr > 0:
print(" *** Detected", ierr, "discrepancies, test not passed")
else:
print("All response values match, checked", n_step, "steps")
# Time window finished, terminate by closing down the result database, etc.
ierr += abs(solver.solver_done())
if ierr == 0:
print("Time window OK, solver closed")
else:
exit(ierr)
# Restart the simulation, reading the state and gages from core arrays
ierr = solver.solver_init(slv_opts, None, solver.state_data, solver.gauge_data)
# Simulation finished, terminate by closing down the result database, etc.
ierr += solver.solver_done()
if ierr == 0 and solver.ierr.value == 0:
print("Simulation OK, solver closed")
else:
exit(ierr + solver.ierr.value)
# Start the simulation again from the beginning
ierr = solver.solver_init(slv_opts)
if ierr < 0:
exit(ierr)
print("\n#### Running dynamics solver in", wrkdir)
t = solver.get_current_time()
dt = solver.get_next_time() - t
# Invoke the dynamics solver for this time window
outputs, do_continue = solver.solve_window(n_step, None, fId)
if solver.ierr.value != 0:
exit(solver.ierr.value)
# Save current state to core array
if not solver.save_state():
exit(-99)
# Compare the responses
ierr = 0
for i in range(n_step):
output = [float(outputs[i * n_out + j]) for j in range(n_out)]
output.insert(0, float(t + dt * (i + 1)))
ierr += compare_lists(output[0], output, references[i], 1.0e-4)
if ierr > 0:
print(" *** Detected", ierr, "discrepancies, test not passed")
exit(ierr)
else:
print("All response values of first time window match")
# Restart the simulation, reading the state from from core array
# Notice, here we don't do a solverDone first, so the model reside in core.
# Only the solution state is reloaded.
print("\n#### Continuing with second time window")
ierr = solver.restart_from_state(solver.state_data)
if ierr < 0:
exit(ierr)
# Invoke the dynamics solver for next time window
t = solver.get_current_time()
dt = solver.get_next_time() - t
outputs, do_continue = solver.solve_window(n_step, None, fId)
if solver.ierr.value != 0:
exit(solver.ierr.value)
# Compare the responses
ierr = 0
for i in range(n_step):
output = [float(outputs[i * n_out + j]) for j in range(n_out)]
output.insert(0, float(t + dt * (i + 1)))
ierr += compare_lists(output[0], output, references[n_step + i], 1.0e-4)
if ierr > 0:
print(" *** Detected", ierr, "discrepancies, test not passed")
else:
print("All response values of second time window match")
# Simulation finished, terminate by closing down the result database, etc.
ierr += abs(solver.solver_done())
if ierr == 0:
print("Simulation OK, solver closed")
else:
exit(ierr)