-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlv_ellipsoid_fixed_x.py
More file actions
201 lines (167 loc) · 6.21 KB
/
lv_ellipsoid_fixed_x.py
File metadata and controls
201 lines (167 loc) · 6.21 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# # Left Ventricular Ellipsoid with Custom Boundary Conditions
#
# This demo builds upon the [Left Ventricular Ellipsoid Simulation](lv_ellipsoid.py).
#
# While the previous example utilized the convenience parameter `base_bc=pulse.BaseBC.fixed`
# to fully clamp the base, this example demonstrates how to:
# 1. Apply **Robin Boundary Conditions** (springs) to the epicardium and base to mimic
# surrounding tissue support.
# 2. Manually define **Dirichlet Boundary Conditions** to constrain specific degrees of freedom.
#
# The geometry generation, constitutive modeling, and active stress implementation remain
# identical to the base example.
#
# ---
# ## Imports
from pathlib import Path
from mpi4py import MPI
import dolfinx
from dolfinx import log
import cardiac_geometries
import cardiac_geometries.geometry
import pulse
# ## Geometry and Materials
#
# We generate the same truncated ellipsoid geometry and define the material model
# (Holzapfel-Ogden with Active Stress) as in the [base example](lv_ellipsoid.ipynb).
# 1. Generate Mesh
outdir = Path("lv_ellipsoid_custom_bcs")
outdir.mkdir(parents=True, exist_ok=True)
geodir = outdir / "geometry"
if not geodir.exists():
cardiac_geometries.mesh.lv_ellipsoid(
outdir=geodir,
create_fibers=True,
fiber_space="P_2",
)
# 2. Load Geometry
geo = cardiac_geometries.geometry.Geometry.from_folder(
comm=MPI.COMM_WORLD,
folder=geodir,
)
geometry = pulse.Geometry.from_cardiac_geometries(geo, metadata={"quadrature_degree": 4})
# 3. Define Constitutive Model
material_params = pulse.HolzapfelOgden.transversely_isotropic_parameters()
material = pulse.HolzapfelOgden(f0=geo.f0, s0=geo.s0, **material_params)
Ta = pulse.Variable(dolfinx.fem.Constant(geometry.mesh, dolfinx.default_scalar_type(0.0)), "kPa")
active_model = pulse.ActiveStress(geo.f0, activation=Ta, eta=0.3)
comp_model = pulse.Incompressible()
model = pulse.CardiacModel(
material=material,
active=active_model,
compressibility=comp_model,
)
# ## Custom Boundary Conditions
#
# Instead of using the preset `base_bc` parameter, we will explicitly define our boundary conditions.
#
# ### 1. Neumann BC (Cavity Pressure)
# Standard pressure load on the endocardium.
traction = pulse.Variable(dolfinx.fem.Constant(geometry.mesh, dolfinx.default_scalar_type(0.0)), "kPa")
neumann = pulse.NeumannBC(traction=traction, marker=geometry.markers["ENDO"][0])
# ### 2. Robin BCs (Elastic Support)
# We apply a spring-like penalty on the Epicardium and the Base to represent the
# resistance provided by the pericardium and surrounding tissue.
#
# $$
# \mathbf{P}\mathbf{N} + k \mathbf{u} = 0
# $$
robin_epi = pulse.RobinBC(
value=pulse.Variable(
dolfinx.fem.Constant(geometry.mesh, dolfinx.default_scalar_type(1e3)),
"Pa / m",
),
marker=geometry.markers["EPI"][0],
)
robin_base = pulse.RobinBC(
value=pulse.Variable(
dolfinx.fem.Constant(geometry.mesh, dolfinx.default_scalar_type(1e3)),
"Pa / m",
),
marker=geometry.markers["BASE"][0],
)
# ### 3. Manual Dirichlet BC
# We manually define a Dirichlet condition. In this specific case, we constrain the
# displacement in the x-direction ($u_x = 0$) on the Base. Ideally, the Base plane is
# perpendicular to the X-axis in this geometry, so this prevents the base from moving
# longitudinally while allowing expansion/sliding in the Y-Z plane (resisted only by the Robin spring).
def dirichlet_bc(V: dolfinx.fem.FunctionSpace):
# Find facets for the BASE marker
facets = geo.ffun.find(geo.markers["BASE"][0])
# Locate degrees of freedom for the x-component (sub(0)) on these facets
dofs = dolfinx.fem.locate_dofs_topological(V.sub(0), 2, facets)
# Return the Dirichlet BC object
return [dolfinx.fem.dirichletbc(0.0, dofs, V.sub(0))]
# We collect all conditions into the `BoundaryConditions` container.
bcs = pulse.BoundaryConditions(
neumann=(neumann,),
dirichlet=(dirichlet_bc,),
robin=(robin_epi, robin_base),
)
# ## Solving the Problem
#
# We initialize the `StaticProblem`.
# **Note**: We do *not* pass `parameters={"base_bc": ...}` here, as we are fully controlling
# the boundaries via the `bcs` argument.
problem = pulse.StaticProblem(
model=model,
geometry=geometry,
bcs=bcs,
)
log.set_log_level(log.LogLevel.INFO)
# ### Phase 1: Passive Inflation
vtx = dolfinx.io.VTXWriter(geometry.mesh.comm, outdir / "lv_displacement.bp", [problem.u], engine="BP4")
vtx.write(0.0)
pressures = [1.0] # kPa
for i, plv in enumerate(pressures, start=1):
print(f"Solving for pressure: {plv} kPa")
traction.assign(plv)
problem.solve()
vtx.write(float(i))
# #### Visualization
try:
import pyvista
except ImportError:
print("Pyvista is not installed")
else:
V = dolfinx.fem.functionspace(geometry.mesh, ("Lagrange", 1, (geometry.mesh.geometry.dim,)))
uh = dolfinx.fem.Function(V)
uh.interpolate(problem.u)
p = pyvista.Plotter()
topology, cell_types, geometry_data = dolfinx.plot.vtk_mesh(V)
grid = pyvista.UnstructuredGrid(topology, cell_types, geometry_data)
grid["u"] = uh.x.array.reshape((geometry_data.shape[0], 3))
p.add_mesh(grid, style="wireframe", color="k", opacity=0.3, label="Reference")
warped = grid.warp_by_vector("u", factor=1.0)
p.add_mesh(warped, show_edges=True, color="firebrick", label="Inflated")
p.add_legend()
p.show_axes()
if not pyvista.OFF_SCREEN:
p.show()
else:
p.screenshot(outdir / "lv_ellipsoid_pressure.png")
# ### Phase 2: Active Contraction
active_tensions = [3.0] # kPa
for i, ta in enumerate(active_tensions, start=len(pressures) + 1):
print(f"Solving for active tension: {ta} kPa")
Ta.assign(ta)
problem.solve()
vtx.write(float(i))
vtx.close()
try:
import pyvista
except ImportError:
pass
else:
uh.interpolate(problem.u)
grid["u"] = uh.x.array.reshape((geometry_data.shape[0], 3))
p = pyvista.Plotter()
p.add_mesh(grid, style="wireframe", color="k", opacity=0.3, label="Reference")
warped = grid.warp_by_vector("u", factor=1.0)
p.add_mesh(warped, show_edges=True, color="red", label="Contracted")
p.add_legend()
p.show_axes()
if not pyvista.OFF_SCREEN:
p.show()
else:
p.screenshot(outdir / "lv_ellipsoid_active.png")