forked from finsberg/pulse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_material.py
245 lines (183 loc) · 6.78 KB
/
test_material.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
import itertools
import dolfin
import numpy as np
import pytest
try:
from dolfin_adjoint import (
Constant,
DirichletBC,
Expression,
UnitCubeMesh,
interpolate,
project,
)
except ImportError:
from dolfin import (
project,
DirichletBC,
Constant,
UnitCubeMesh,
interpolate,
Expression,
)
from pulse import kinematics
from pulse.geometry import Geometry, MarkerFunctions, Microstructure
from pulse.material import HolzapfelOgden, NeoHookean, material_models
from pulse.mechanicsproblem import BoundaryConditions, MechanicsProblem, NeumannBC
from pulse.utils import mpi_comm_world
class Free(dolfin.SubDomain):
def inside(self, x, on_boundary):
return x[0] > (1.0 - dolfin.DOLFIN_EPS) and on_boundary
class Fixed(dolfin.SubDomain):
def inside(self, x, on_boundary):
return x[0] < dolfin.DOLFIN_EPS and on_boundary
fixed = Fixed()
fixed_marker = 1
free = Free()
free_marker = 2
@pytest.fixture
def unitcube_geometry():
N = 3
mesh = UnitCubeMesh(mpi_comm_world(), N, N, N)
ffun = dolfin.MeshFunction("size_t", mesh, 2)
ffun.set_all(0)
fixed.mark(ffun, fixed_marker)
free.mark(ffun, free_marker)
marker_functions = MarkerFunctions(ffun=ffun)
# Fibers
V_f = dolfin.VectorFunctionSpace(mesh, "CG", 1)
f0 = interpolate(Expression(("1.0", "0.0", "0.0"), degree=1), V_f)
s0 = interpolate(Expression(("0.0", "1.0", "0.0"), degree=1), V_f)
n0 = interpolate(Expression(("0.0", "0.0", "1.0"), degree=1), V_f)
microstructure = Microstructure(f0=f0, s0=s0, n0=n0)
geometry = Geometry(
mesh=mesh,
marker_functions=marker_functions,
microstructure=microstructure,
)
return geometry
cases = itertools.product(
material_models,
("active_strain", "active_stress"),
(True, False),
)
@pytest.mark.parametrize("Material, active_model, isochoric", cases)
def test_material(unitcube_geometry, Material, active_model, isochoric):
compressible_model = "incompressible"
if active_model == "active_stress":
active_value = 20.0
activation = Constant(1.0)
T_ref = active_value
def dirichlet_bc(W):
V = W if W.sub(0).num_sub_spaces() == 0 else W.sub(0)
return DirichletBC(V, Constant((0.0, 0.0, 0.0)), fixed)
else:
activation = Constant(0.0)
active_value = 0.0
T_ref = 1.0
def dirichlet_bc(W):
V = W if W.sub(0).num_sub_spaces() == 0 else W.sub(0)
return DirichletBC(V.sub(0), Constant(0.0), fixed, "pointwise")
neumann_bc = NeumannBC(traction=Constant(-active_value), marker=free_marker)
bcs = BoundaryConditions(dirichlet=(dirichlet_bc,), neumann=(neumann_bc,))
matparams = Material.default_parameters()
material = Material(
activation=activation,
parameters=matparams,
T_ref=T_ref,
isochoric=isochoric,
compressible_model=compressible_model,
active_model=active_model,
)
assert material.isochoric == isochoric
problem = MechanicsProblem(unitcube_geometry, material, bcs)
problem.solve()
u, p = problem.state.split(deepcopy=True)
print(material.name)
if active_model == "active_strain":
tol = 1e-4
if not isochoric:
if material.name in [
"guccione",
"linear_elastic",
"saint_venant_kirchhoff",
]:
assert all(abs(p.vector().get_local()) < tol)
elif material.name == "holzapfel_ogden":
assert all(abs(p.vector().get_local() - material.parameters["a"]) < tol)
elif material.name == "neo_hookean":
assert all(
abs(p.vector().get_local() - material.parameters["mu"]) < tol,
)
else:
raise TypeError(f"Unkown material {material.name}")
else:
assert all(abs(p.vector().get_local()) < tol)
else:
F = kinematics.DeformationGradient(u)
T = material.CauchyStress(F, p)
V_dg = dolfin.FunctionSpace(unitcube_geometry.mesh, "DG", 1)
# Fiber on current geometry
f = F * unitcube_geometry.f0
# Fiber stress
Tf = dolfin.inner(T * f / f**2, f)
Tf_dg = project(Tf, V_dg)
tol = 1e-10
assert all(abs(Tf_dg.vector().get_local() - active_value) < tol)
assert all(abs(u.vector().get_local()) < tol)
if not isochoric:
if material.name in [
"guccione",
"linear_elastic",
"saint_venant_kirchhoff",
]:
assert all(abs(p.vector().get_local()) < tol)
elif material.name == "holzapfel_ogden":
assert all(abs(p.vector().get_local() - material.parameters["a"]) < tol)
elif material.name == "neo_hookean":
assert all(
abs(p.vector().get_local() - material.parameters["mu"]) < tol,
)
else:
raise TypeError(f"Unkown material {material.name}")
else:
assert all(abs(p.vector().get_local()) < tol)
@pytest.mark.parametrize("active_model", ("active_strain", "active_stress"))
def test_active_contraction_yield_displacement(unitcube_geometry, active_model):
activation = Constant(0.001)
def dirichlet_bc(W):
V = W if W.sub(0).num_sub_spaces() == 0 else W.sub(0)
return DirichletBC(V, Constant((0.0, 0.0, 0.0)), fixed)
bcs = BoundaryConditions(dirichlet=(dirichlet_bc,))
matparams = HolzapfelOgden.default_parameters()
material = HolzapfelOgden(
activation=activation,
parameters=matparams,
active_model=active_model,
)
problem = MechanicsProblem(unitcube_geometry, material, bcs)
problem.solve()
u, p = problem.state.split(deepcopy=True)
assert np.linalg.norm(u.vector().get_local()) > 0
def test_pass_active_model_as_object(unitcube_geometry):
activation = Constant(0.001)
def dirichlet_bc(W):
V = W if W.sub(0).num_sub_spaces() == 0 else W.sub(0)
return DirichletBC(V, Constant((0.0, 0.0, 0.0)), fixed)
bcs = BoundaryConditions(dirichlet=(dirichlet_bc,))
matparams = NeoHookean.default_parameters()
material = NeoHookean(
parameters=matparams,
active_model="active_strain",
activation=activation,
)
problem = MechanicsProblem(unitcube_geometry, material, bcs)
problem.solve()
u, p = problem.state.split(deepcopy=True)
assert np.linalg.norm(u.vector().get_local()) > 0
if __name__ == "__main__":
active_model = "active_stress"
dev_iso_split = True
geo = unitcube_geometry()
for m in material_models:
test_material(geo, m, active_model, dev_iso_split)