Repository navigation
Expand file tree
/
Copy pathelasticity.jl
More file actions
245 lines (214 loc) · 11.2 KB
/
Copy pathelasticity.jl
File metadata and controls
245 lines (214 loc) · 11.2 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
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
# ===========================================================================
# PhaseFieldFracture — a readable, pure-Julia phase-field fracture solver
#
# Author: Yang Bai — Materials Mechanics Laboratory (MMLab)
# Contact: yangbai90@outlook.com
# Copyright: © 2026 Materials Mechanics Laboratory (MMLab). All rights reserved.
# ===========================================================================
# ===========================================================================
# Linear elasticity (plane strain) with phase-field degradation
# ===========================================================================
#
# The degraded solid stores the elastic energy
#
# ψ(ε, φ) = g(φ) ψ⁺(ε) + ψ⁻(ε) , g(φ) = (1-φ)² + k ,
#
# where ψ⁺ / ψ⁻ are the tensile / compressive parts of the strain energy
# (see strainsplit.jl). Only the tensile part is degraded by the damage, so
# a broken point loses tensile stiffness but keeps compressive stiffness.
#
# Two constitutive choices are offered through `Material.split`:
#
# :spectral — Miehe's principal-strain split (the physically correct model;
# the default). ψ⁺, σ⁺, C⁺ come from `spectral_stress_tangent`.
# :none — NO split: the whole energy drives and the whole stiffness is
# degraded, ψ⁺ = ½ εᵀ D ε, σ = g D ε, C = g D. This is the
# classic isotropic model; kept so students can switch the split
# on and off and see the difference (e.g. damage in compression).
#
# Because the split makes the stress a NONLINEAR function of the strain, the
# displacement sub-problem is solved by Newton iteration (`solve_displacement`)
# using the consistent tangent C = ∂σ/∂ε assembled in `assemble_mechanics`.
# (For :none the stress is linear, so Newton converges in a single step.)
"""
Material properties. Built from `(E, ν)` by the keyword constructor below,
which also precomputes the Lamé constants λ, μ and the plane-strain matrix D.
Fields:
E, ν Young's modulus and Poisson's ratio
Gc critical energy release rate (fracture toughness)
ℓ phase-field length scale (crack-band half-width)
k small residual stiffness so that g(1) = k > 0 keeps Kᵤ regular
λ, μ Lamé constants: λ = Eν/[(1+ν)(1−2ν)], μ = E/[2(1+ν)]
D 3×3 plane-strain elasticity matrix (engineering Voigt)
split :spectral (Miehe tension/compression split) or :none (no split)
"""
struct Material
E::Float64 # Young's modulus
ν::Float64 # Poisson's ratio
Gc::Float64 # critical energy release rate (fracture toughness)
ℓ::Float64 # phase-field length scale
k::Float64 # residual stiffness, keeps K_u regular when φ → 1
λ::Float64 # Lamé's first parameter
μ::Float64 # shear modulus (Lamé's second parameter)
D::Matrix{Float64} # 3×3 plane-strain elasticity matrix
split::Symbol # :spectral (Miehe split) or :none (no split)
end
"""
Material(; E, ν, Gc, ℓ, k = 1e-8, split = :spectral)
Construct a plane-strain material. Computes the Lamé constants λ, μ and the
elasticity matrix D from `(E, ν)`. `split` selects the constitutive model:
`:spectral` (Miehe tension/compression split, the default) or `:none`.
"""
function Material(; E, ν, Gc, ℓ, k = 1e-8, split = :spectral)
0 ≤ ν < 0.5 || error("plane strain requires 0 ≤ ν < 0.5")
split in (:spectral, :none) || error("split must be :spectral or :none")
# Lamé constants (plane strain).
λ = E * ν / ((1 + ν) * (1 - 2ν))
μ = E / (2 * (1 + ν))
# Plane-strain elasticity matrix D such that σ = D ε for engineering
# strain ε = [εₓₓ, ε_yy, γₓy]; equivalently D[1,1]=λ+2μ, D[1,2]=λ, D[3,3]=μ.
D = E / ((1 + ν) * (1 - 2ν)) .* [1-ν ν 0
ν 1-ν 0
0 0 (1-2ν)/2]
return Material(E, ν, Gc, ℓ, k, λ, μ, D, split)
end
"Degradation function g(φ) = (1-φ)² + k."
degrade(m::Material, φ) = (1 - φ)^2 + m.k
"Total elastic strain-energy density ψ = ½ εᵀ D ε (used by the `:none` model)."
strain_energy(m::Material, ε) = 0.5 * dot(ε, m.D * ε)
# ---------------------------------------------------------------------------
# Constitutive response at one integration point
# ---------------------------------------------------------------------------
"""
driving_energy(mat, ε) -> ψ⁺
Crack driving energy density at engineering strain `ε`. For the `:spectral`
model this is the tensile part ψ⁺(ε); for `:none` it is the whole energy
½εᵀDε. Its running maximum in time is the history field H (see phasefield.jl).
"""
function driving_energy(mat::Material, ε)
mat.split === :none && return strain_energy(mat, ε)
return driving_energy_spectral(mat.λ, mat.μ, ε)
end
"""
constitutive(mat, ε, g) -> (σ, C)
Degraded stress `σ = g σ⁺ + σ⁻` and its consistent tangent `C = g C⁺ + C⁻`
(both in engineering convention) at strain `ε` with degradation `g = g(φ)`.
For `:none` there is no split: σ = g D ε and C = g D (the whole stiffness is
degraded). For `:spectral`, only the tensile part is degraded.
"""
function constitutive(mat::Material, ε, g)
if mat.split === :none
σ = g .* (mat.D * ε)
return σ, g .* mat.D
end
σ_pos, σ_neg, C_pos, C_neg = spectral_stress_tangent(mat.λ, mat.μ, ε)
return g .* σ_pos .+ σ_neg, g .* C_pos .+ C_neg
end
# ---------------------------------------------------------------------------
# Assembly of the mechanical tangent and internal force
# ---------------------------------------------------------------------------
"""
assemble_mechanics(mesh, mat, φ, u) -> (K, f_int)
Assemble, at the current displacement `u` and damage `φ`, the global
tangent stiffness K = Σₑ ∫_Ωₑ Bᵀ C(ε, φ) B dΩ , (for Newton)
internal force f_int = Σₑ ∫_Ωₑ Bᵀ σ(ε, φ) dΩ , (the residual)
where at each Gauss point ε = B uₑ, φ is interpolated as φ(ξ,η) = N·φₑ, and
(σ, C) come from `constitutive`. With the tension/compression split both σ
and C depend on ε, so K and f_int must be recomputed as `u` changes — this is
why the displacement solve is a Newton loop rather than a single linear solve.
At equilibrium f_int = 0 on the free dofs; its sum over constrained dofs is the
reaction force (see `reaction_top`).
"""
function assemble_mechanics(mesh::Mesh, mat::Material, φ, u)
ndof = 2 * nnodes(mesh) # total displacement dofs
ncell = ncells(mesh)
# COO triplets (I[k], J[k], V[k]) as before; each cell adds an 8×8 = 64 block.
I = Vector{Int}(undef, 64 * ncell) # row indices
J = Vector{Int}(undef, 64 * ncell) # column indices
V = Vector{Float64}(undef, 64 * ncell) # tangent values
f_int = zeros(ndof) # global internal force vector
p = 0 # running write position into I/J/V
for e in 1:ncell
nodes = @view mesh.cells[e, :] # the 4 node indices of this cell
X = mesh.coords[nodes, :] # their coordinates, a 4×2 matrix
φe = @view φ[nodes] # nodal damage on this cell
dofs = cell_dofs(nodes) # the 8 global dofs of this cell
ue = u[dofs] # nodal displacements on this cell
Kₑ = zeros(8, 8) # element tangent
fₑ = zeros(8) # element internal force
for (ξ, η) in GAUSS # numerical integration over the cell
N, ∇N, detJ = element(X, ξ, η)
B = strain_matrix(∇N) # so that strain ε = B·uₑ
ε = B * ue # engineering strain at this Gauss point
g = degrade(mat, dot(N, φe)) # degradation g(φ), φ interpolated here
σ, C = constitutive(mat, ε, g) # degraded stress and tangent
Kₑ .+= (B' * C * B) .* detJ # ∫ Bᵀ C B dΩ (weight = 1)
fₑ .+= (B' * σ) .* detJ # ∫ Bᵀ σ dΩ
end
for a in 1:8 # scatter the 8×8 block and 8-vector globally
f_int[dofs[a]] += fₑ[a]
for b in 1:8
p += 1
I[p] = dofs[a]; J[p] = dofs[b]; V[p] = Kₑ[a, b]
end
end
end
return sparse(I, J, V, ndof, ndof), f_int # assemble & sum duplicate entries
end
# ---------------------------------------------------------------------------
# Boundary conditions and the (nonlinear) displacement solve
# ---------------------------------------------------------------------------
"Displacement BCs: bottom edge clamped (uₓ=u_y=0); top edge given u_y, uₓ free."
function displacement_bcs(mesh::Mesh, top_uy)
bc = Dict{Int,Float64}()
for n in mesh.bottom
bc[xdof(n)] = 0.0
bc[ydof(n)] = 0.0
end
for n in mesh.top
bc[ydof(n)] = top_uy
end
return bc
end
"""
solve_displacement(mesh, mat, φ, top_uy, u0; max_newton, tol) -> (u, f_int)
Solve the mechanical equilibrium ∫ Bᵀ σ(ε(u), φ) dΩ = 0 for the displacement
`u`, at fixed damage `φ` and prescribed top displacement `top_uy`, by
**Newton–Raphson** starting from the guess `u0`:
r(u) = f_int(u) (internal force = residual, no ext. load)
solve K(u) Δu = −r(u) with Δu = 0 on the constrained dofs
u ← u + Δu until ‖r_free‖ ≤ tol·‖r_free⁰‖ .
`K` and `f_int` come from `assemble_mechanics`. Because the potential
g ψ⁺ + ψ⁻ is convex in ε, Newton with the consistent tangent converges
robustly (and in one step when `split = :none`, where σ is linear in ε).
Returns the converged displacement `u` and the internal force `f_int` at that
`u` (the latter is used to read off the reaction force).
"""
function solve_displacement(mesh::Mesh, mat::Material, φ, top_uy, u0;
max_newton::Int = 25, tol::Float64 = 1e-9)
ndof = 2 * nnodes(mesh)
bc = displacement_bcs(mesh, top_uy)
u = copy(u0) # warm start from the previous iterate
for (d, v) in bc # enforce the prescribed values exactly
u[d] = v
end
isfixed = falses(ndof) # mask of constrained (Dirichlet) dofs
for d in keys(bc)
isfixed[d] = true
end
free = findall(.!isfixed) # free (unknown) dofs
zero_bc = Dict(d => 0.0 for d in keys(bc)) # homogeneous BC for the increment Δu
K = spzeros(ndof, ndof); f_int = zeros(ndof)
r0 = 0.0
for it in 1:max_newton
K, f_int = assemble_mechanics(mesh, mat, φ, u) # tangent + residual at u
rnorm = norm(@view f_int[free]) # residual on the free dofs
it == 1 && (r0 = rnorm) # reference for the rel. test
if rnorm ≤ tol * r0 + 1e-14 # converged (abs. floor for r0≈0)
break
end
Δu = solve_dirichlet(K, -f_int, zero_bc) # K Δu = −r, Δu = 0 on fixed dofs
u .+= Δu
end
return u, f_int
end