-
Notifications
You must be signed in to change notification settings - Fork 107
/
test_dissipation.py
287 lines (250 loc) · 10.2 KB
/
test_dissipation.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
__doc__ = """ Test Dissipation module for in Elastica implementation"""
# System imports
from elastica.dissipation import (
DamperBase,
AnalyticalLinearDamper,
LaplaceDissipationFilter,
)
from elastica.utils import Tolerance
import numpy as np
from numpy.testing import assert_allclose
import pytest
from tests.test_rod.mock_rod import MockTestRod, MockTestRingRod
def test_damper_base():
test_rod = MockTestRod()
test_rod.velocity_collection = np.ones(3) * 5.0
test_rod.omega_collection = np.ones(3) * 11.0
class TestDamper(DamperBase):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def dampen_rates(self, rod, time):
rod.velocity_collection *= time
rod.omega_collection *= time
test_damper = TestDamper(_system=test_rod)
test_damper.dampen_rates(test_rod, 2)
assert_allclose(test_rod.velocity_collection, 10.0, atol=Tolerance.atol())
assert_allclose(test_rod.omega_collection, 22.0, atol=Tolerance.atol())
def test_damper_base_properties_access():
test_rod = MockTestRod()
class TestDamper(DamperBase):
def __init__(self, **kwargs):
super().__init__(**kwargs)
# Able to access properties in constraint class
assert self._system == test_rod
def dampen_rates(self, rod, time):
assert self._system == test_rod
test_damper = TestDamper(_system=test_rod)
test_damper.dampen_rates(test_rod, 2)
def test_analytical_linear_damper():
test_rod = MockTestRod()
test_rod.mass[:] = 1.0
test_dilatation = 2.0 * np.ones((3, test_rod.n_elems))
test_inv_mass_second_moment_of_inertia = 3.0 * np.ones((3, 3, test_rod.n_elems))
test_rod.dilatation = test_dilatation.copy()
test_rod.inv_mass_second_moment_of_inertia = (
test_inv_mass_second_moment_of_inertia.copy()
)
damping_constant = 0.25
dt = 0.5
exponential_damper = AnalyticalLinearDamper(
_system=test_rod, damping_constant=damping_constant, time_step=dt
)
# check common prefactors
# e ^ (-damp_coeff * dt)
ref_translational_damping_coefficient = np.exp(-0.25 * 0.5)
# e ^ (-damp_coeff * dt * elemental_mass * inv_mass_second_moment_of_inertia)
ref_rotational_damping_coefficient = np.exp(-0.25 * 0.5 * 1.0 * 3.0) * np.ones(
(3, test_rod.n_elems)
)
# end corrections
ref_rotational_damping_coefficient[:, 0] = np.exp(-0.25 * 0.5 * 1.5 * 3.0)
ref_rotational_damping_coefficient[:, -1] = np.exp(-0.25 * 0.5 * 1.5 * 3.0)
assert_allclose(
exponential_damper.translational_damping_coefficient,
ref_translational_damping_coefficient,
atol=Tolerance.atol(),
)
assert_allclose(
exponential_damper.rotational_damping_coefficient,
ref_rotational_damping_coefficient,
atol=Tolerance.atol(),
)
pre_damping_velocity_collection = np.random.rand(3, test_rod.n_elems + 1)
test_rod.velocity_collection = (
pre_damping_velocity_collection.copy()
) # We need copy of the list not a reference to this array
pre_damping_omega_collection = np.random.rand(3, test_rod.n_elems)
test_rod.omega_collection = (
pre_damping_omega_collection.copy()
) # We need copy of the list not a reference to this array
exponential_damper.dampen_rates(test_rod, time=0)
post_damping_velocity_collection = (
pre_damping_velocity_collection * ref_translational_damping_coefficient
)
# multiplying_factor = ref_rot_coeff ^ dilation
post_damping_omega_collection = (
pre_damping_omega_collection * ref_rotational_damping_coefficient ** 2.0
)
assert_allclose(
post_damping_velocity_collection,
test_rod.velocity_collection,
atol=Tolerance.atol(),
)
assert_allclose(
post_damping_omega_collection, test_rod.omega_collection, atol=Tolerance.atol()
)
@pytest.mark.parametrize("filter_order", [-1, 0, 3.2])
def test_laplace_dissipation_filter_init_invalid_filter_order(filter_order):
test_rod = MockTestRod()
with pytest.raises(ValueError) as exc_info:
_ = LaplaceDissipationFilter(
_system=test_rod,
filter_order=filter_order,
)
assert (
exc_info.value.args[0]
== "Invalid filter order! Filter order must be a positive integer."
)
@pytest.mark.parametrize("filter_order", [2, 3, 4])
def test_laplace_dissipation_filter_init(filter_order):
test_rod = MockTestRod()
filter_damper = LaplaceDissipationFilter(
_system=test_rod,
filter_order=filter_order,
)
assert filter_damper.filter_order == filter_order
assert_allclose(
filter_damper.velocity_filter_term, np.zeros((3, test_rod.n_elems + 1))
)
assert_allclose(filter_damper.omega_filter_term, np.zeros((3, test_rod.n_elems)))
@pytest.mark.parametrize("filter_order", [2, 3, 4])
def test_laplace_dissipation_filter_for_constant_field(filter_order):
test_rod = MockTestRod()
filter_damper = LaplaceDissipationFilter(
_system=test_rod,
filter_order=filter_order,
)
test_rod.velocity_collection[...] = 2.0
test_rod.omega_collection[...] = 3.0
filter_damper.dampen_rates(rod=test_rod, time=0)
# filter should keep a spatially invariant field unaffected
post_damping_velocity_collection = 2.0 * np.ones_like(test_rod.velocity_collection)
post_damping_omega_collection = 3.0 * np.ones_like(test_rod.omega_collection)
assert_allclose(
post_damping_velocity_collection,
test_rod.velocity_collection,
atol=Tolerance.atol(),
)
assert_allclose(
post_damping_omega_collection, test_rod.omega_collection, atol=Tolerance.atol()
)
def test_laplace_dissipation_filter_for_flip_flop_field():
filter_order = 1
test_rod = MockTestRod()
filter_damper = LaplaceDissipationFilter(
_system=test_rod,
filter_order=filter_order,
)
test_rod.velocity_collection[...] = 0.0
test_rod.velocity_collection[..., 1::2] = 2.0
test_rod.omega_collection[...] = 0.0
test_rod.omega_collection[..., 1::2] = 3.0
pre_damping_velocity_collection = test_rod.velocity_collection.copy()
pre_damping_omega_collection = test_rod.omega_collection.copy()
filter_damper.dampen_rates(rod=test_rod, time=0)
post_damping_velocity_collection = np.zeros_like(test_rod.velocity_collection)
post_damping_omega_collection = np.zeros_like(test_rod.omega_collection)
# filter should remove the flip-flop mode th give the average constant mode
post_damping_velocity_collection[..., 1:-1] = 2.0 / 2
post_damping_omega_collection[..., 1:-1] = 3.0 / 2
# end values remain untouched
post_damping_velocity_collection[
..., 0 :: test_rod.n_elems
] = pre_damping_velocity_collection[..., 0 :: test_rod.n_elems]
post_damping_omega_collection[
..., 0 :: test_rod.n_elems - 1
] = pre_damping_omega_collection[..., 0 :: test_rod.n_elems - 1]
assert_allclose(
post_damping_velocity_collection,
test_rod.velocity_collection,
atol=Tolerance.atol(),
)
assert_allclose(
post_damping_omega_collection, test_rod.omega_collection, atol=Tolerance.atol()
)
@pytest.mark.parametrize("filter_order", [-1, 0, 3.2])
def test_laplace_dissipation_filter_init_invalid_filter_order_for_ring_rod(
filter_order,
):
test_rod = MockTestRingRod()
with pytest.raises(ValueError) as exc_info:
_ = LaplaceDissipationFilter(
_system=test_rod,
filter_order=filter_order,
)
assert (
exc_info.value.args[0]
== "Invalid filter order! Filter order must be a positive integer."
)
@pytest.mark.parametrize("filter_order", [2, 3, 4])
def test_laplace_dissipation_filter_init_for_ring_rod(filter_order):
test_rod = MockTestRingRod()
filter_damper = LaplaceDissipationFilter(
_system=test_rod,
filter_order=filter_order,
)
assert filter_damper.filter_order == filter_order
assert_allclose(
filter_damper.velocity_filter_term[:, 1:-1], np.zeros((3, test_rod.n_elems))
)
assert_allclose(
filter_damper.omega_filter_term[:, 1:-1], np.zeros((3, test_rod.n_elems))
)
@pytest.mark.parametrize("filter_order", [2, 3, 4])
def test_laplace_dissipation_filter_for_constant_field_for_ring_rod(filter_order):
test_rod = MockTestRingRod()
filter_damper = LaplaceDissipationFilter(
_system=test_rod,
filter_order=filter_order,
)
test_rod.velocity_collection[...] = 2.0
test_rod.omega_collection[...] = 3.0
filter_damper.dampen_rates(rod=test_rod, time=0)
# filter should keep a spatially invariant field unaffected
post_damping_velocity_collection = 2.0 * np.ones_like(test_rod.velocity_collection)
post_damping_omega_collection = 3.0 * np.ones_like(test_rod.omega_collection)
assert_allclose(
post_damping_velocity_collection,
test_rod.velocity_collection,
atol=Tolerance.atol(),
)
assert_allclose(
post_damping_omega_collection, test_rod.omega_collection, atol=Tolerance.atol()
)
def test_laplace_dissipation_filter_for_flip_flop_field_for_ring_rod():
filter_order = 1
test_rod = MockTestRingRod()
filter_damper = LaplaceDissipationFilter(
_system=test_rod,
filter_order=filter_order,
)
test_rod.velocity_collection[...] = 0.0
test_rod.velocity_collection[..., 1::2] = 2.0
test_rod.omega_collection[...] = 0.0
test_rod.omega_collection[..., 1::2] = 3.0
pre_damping_velocity_collection = test_rod.velocity_collection.copy()
pre_damping_omega_collection = test_rod.omega_collection.copy()
filter_damper.dampen_rates(rod=test_rod, time=0)
post_damping_velocity_collection = np.zeros_like(test_rod.velocity_collection)
post_damping_omega_collection = np.zeros_like(test_rod.omega_collection)
# filter should remove the flip-flop mode th give the average constant mode
post_damping_velocity_collection[:] = 2.0 / 2
post_damping_omega_collection[:] = 3.0 / 2
assert_allclose(
post_damping_velocity_collection,
test_rod.velocity_collection,
atol=Tolerance.atol(),
)
assert_allclose(
post_damping_omega_collection, test_rod.omega_collection, atol=Tolerance.atol()
)