forked from finsberg/pulse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_iterate.py
183 lines (128 loc) · 4.97 KB
/
test_iterate.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
import pytest
import itertools
import numpy as np
from pulse.iterate import iterate
try:
import dolfin_adjoint
has_dolfin_adjoint = True
except ImportError:
has_dolfin_adjoint = False
import dolfin
has_dolfin_adjoint = False
if has_dolfin_adjoint:
# Run tests with and without annotation
annotates = (False, True)
else:
annotates = (False, False)
from utils import make_lv_mechanics_problem
cases = itertools.product((False, True), annotates)
@pytest.fixture
def problem():
problem = make_lv_mechanics_problem('R_0')
return problem
def test_iterate_pressure(problem):
target_pressure = 1.0
plv = [p.traction for p in problem.bcs.neumann if p.name == 'lv']
pressure = plv[0]
if has_dolfin_adjoint:
dolfin.parameters["adjoint"]["stop_annotating"] = False
dolfin_adjoint.adj_reset()
iterate(problem, pressure, target_pressure)
if has_dolfin_adjoint:
# Check the recording
assert dolfin_adjoint.replay_dolfin(tol=1e-12)
# Check that the pressure is correct
assert float(plv[0]) == target_pressure
# Check that the state is nonzero
assert dolfin.norm(problem.state.vector()) > 0
def test_iterate_gamma(problem):
target_gamma = 0.1
gamma = problem.material.activation
if has_dolfin_adjoint:
dolfin.parameters["adjoint"]["stop_annotating"] = False
dolfin_adjoint.adj_reset()
iterate(problem, gamma, target_gamma)
assert all(gamma.vector().get_local() == target_gamma)
assert dolfin.norm(problem.state.vector()) > 0
if has_dolfin_adjoint:
assert dolfin_adjoint.replay_dolfin(tol=1e-12)
def test_iterate_gamma_regional():
problem = make_lv_mechanics_problem('regional')
target_gamma = problem.material.activation.vector().get_local()
for i in range(len(target_gamma)):
target_gamma[i] = 0.1 - i*0.001
print(target_gamma)
gamma = problem.material.activation
if has_dolfin_adjoint:
dolfin.parameters["adjoint"]["stop_annotating"] = False
dolfin_adjoint.adj_reset()
iterate(problem, gamma, target_gamma)
print(gamma.vector().get_local())
assert np.all(gamma.vector().get_local() - target_gamma < 1e-12)
assert dolfin.norm(problem.state.vector()) > 0
if has_dolfin_adjoint:
assert dolfin_adjoint.replay_dolfin(tol=1e-12)
@pytest.mark.parametrize('continuation', [True, False])
def test_iterate_gamma_cg1(continuation):
problem = make_lv_mechanics_problem('CG_1')
V = problem.material.activation.function_space()
target_gamma \
= dolfin.interpolate(dolfin.Expression('0.1 * x[0]',
degree=1), V)
gamma = problem.material.activation
if has_dolfin_adjoint:
dolfin.parameters["adjoint"]["stop_annotating"] = False
dolfin_adjoint.adj_reset()
iterate(problem, gamma,
target_gamma,
continuation=continuation)
assert np.all(gamma.vector().get_local()
- target_gamma.vector().get_local() < 1e-12)
assert dolfin.norm(problem.state.vector()) > 0
if has_dolfin_adjoint:
assert dolfin_adjoint.replay_dolfin(tol=1e-12)
def test_iterate_gamma_pressure(problem):
target_pressure = 1.0
plv = [p.traction for p in problem.bcs.neumann if p.name == 'lv']
pressure = plv[0]
target_gamma = 0.1
gamma = problem.material.activation
if has_dolfin_adjoint:
dolfin.parameters["adjoint"]["stop_annotating"] = False
dolfin_adjoint.adj_reset()
iterate(problem, (pressure, gamma), (target_pressure, target_gamma))
assert all(gamma.vector().get_local() == target_gamma)
assert float(plv[0]) == target_pressure
assert dolfin.norm(problem.state.vector()) > 0
if has_dolfin_adjoint:
assert dolfin_adjoint.replay_dolfin(tol=1e-12)
def test_iterate_regional_gamma_pressure():
problem = make_lv_mechanics_problem('regional')
target_gamma = problem.material.activation.vector().get_local()
for i in range(len(target_gamma)):
target_gamma[i] = 0.1 - i*0.001
gamma = problem.material.activation
target_pressure = 1.0
plv = [p.traction for p in problem.bcs.neumann if p.name == 'lv']
pressure = plv[0]
with pytest.raises(ValueError):
iterate(problem, (pressure, gamma), (target_pressure, target_gamma))
if __name__ == "__main__":
prob = problem()
test_iterate_pressure(prob)
# test_iterate_gamma(prob)
# test_iterate_gamma_regional()
# test_iterate_gamma_cg1(True)
# test_iterate_gamma_pressure(prob)
# test_iterate_regional_gamma_pressure()
exit()
for c, a in cases:
print("Continuation = {}, annotate = {}".format(c, a))
prob = problem()
test_iterate_pressure(prob, continuation=c, annotate=a)
if has_dolfin_adjoint:
dolfin_adjoint.adj_reset()
prob = problem()
test_iterate_gamma(prob, continuation=c, annotate=a)
if has_dolfin_adjoint:
dolfin_adjoint.adj_reset()