forked from SAP-archive/fedem-solvers
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_Microbatch.py
More file actions
108 lines (86 loc) · 2.86 KB
/
test_Microbatch.py
File metadata and controls
108 lines (86 loc) · 2.86 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
# 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 `test_extfunc.py`
but now using the window dts operator as the simulation driver.
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 math import sin, cos
from pandas import DataFrame, concat, read_csv
from fedempy.dts_operators import window
from test_utils import compare_lists
def F1(x):
"""
First input function.
"""
return 1.0e7 * sin(x)
def F2(x):
"""
Second input function.
"""
return 5.0e3 * (cos(2.5 * x) - 1.0)
class DTScontext:
"""
Dummy DTS container class.
"""
def __init__(self):
self._window = None
self._state = None
@property
def window(self):
return self._window
@property
def state(self):
return self._state
@state.setter
def state(self, value):
self._state = value
# Define the state container
dts = DTScontext()
# Start the fedem solver in the $TEST_DIR/Cantilever-extFunc folder.
wrkdir = environ["TEST_DIR"] + "/Cantilever-extFunc"
print("\n#### Running dynamics solver in", wrkdir)
# Read reference values into a DataFrame
refdata = read_csv(wrkdir + "/TipPosition.asc", sep="\t", skiprows=6)
# Create the input data
nstep0 = 100
nsteps = nstep0 + nstep0
inputs = { "F1" : [], "F2" : [] }
tindex = []
for i in range(1,1+nsteps):
t = refdata.iloc[i, 0]
tindex.append(t)
inputs["F1"].append(F1(t))
inputs["F2"].append(F2(t))
df = DataFrame(inputs,index=tindex)
# Set up keyword dictionary for the simulation driver
slvopt = { "fco": "Setup.fco", "fsifile": "Model.fsi" }
kwargs = { "lib_dir": wrkdir, "solver_options": slvopt }
kwargs.update({ "output_ids": [3, 4, 5], "use_state": True })
# Run the simulation over the first nstep0 steps
outputs1 = window.run(df.iloc[:nstep0, :].copy(), dts, **kwargs)
print(f"\nHere are the outputs for the first {nstep0} steps:\n", outputs1)
# Run the simulation over next nstep0 steps
outputs2 = window.run(df.iloc[nstep0:, :], dts, **kwargs)
print(f"\nHere are the outputs for the next {nstep0} steps:\n", outputs2)
# Concatenate the two output frames
outputs = concat([outputs1, outputs2])
outputs.insert(0, "Time", refdata.iloc[1:1+nsteps, 0].values)
print(f"\nHere are all {nsteps} steps:\n", outputs)
# Check against reference values
ctol = 1.0e-8
ierr = 0
for i in range(nsteps):
t = refdata.iloc[i+1, 0]
ierr += compare_lists(t, outputs.iloc[i, :].values, refdata.iloc[i+1, :].values, ctol)
if ierr > 0:
print(f" *** Detected {ierr} discrepancies, test not passed")
exit(ierr)
else:
print(f" * All response values match, checked {nsteps} steps")