E, nu = 3e3, 0.0 # Young's modulus and Poisson's ratio
mu, la = E / (2 * (1 + nu)), E * nu / ((1 + nu) * (1 - 2 * nu))
x = ti.Vector.field(2, float, n_particles)
color = ti.Vector.field(3, float, n_particles)
v = ti.Vector.field(2, float, n_particles)
C = ti.Matrix.field(2, 2, float, n_particles)
F = ti.Matrix.field(2, 2, dtype=float, shape=n_particles) # deformation gradient
grid_v = ti.Vector.field(2, float, (n_grid, n_grid))
grid_m = ti.field(float, (n_grid, n_grid))
@ti.kernel
def reset_grid():
for i, j in grid_m:
grid_v[i, j] = [0, 0]
grid_m[i, j] = 0
@ti.kernel
def p2g():
for p in x:
Xp = x[p] / dx
base = int(Xp - 0.5)
fx = Xp - base
w = [0.5 * (1.5 - fx) ** 2, 0.75 - (fx - 1) ** 2, 0.5 * (fx - 0.5) ** 2]
F[p] = (ti.Matrix.identity(float, 2) + dt * C[p]) @ F[p]
U, sig, V = ti.svd(F[p])
J = F[p].determinant()
stress = 2 * mu * (F[p] - U @ V.transpose()) @ F[p].transpose(
) + ti.Matrix.identity(float, 2) * la * J * (J - 1)
stress = (-dt * p_vol * 4 * inv_dx * inv_dx) * stress
affine = stress + p_mass * C[p]
for i, j in ti.static(ti.ndrange(3, 3)):
offset = ti.Vector([i, j])
dpos = (offset - fx) * dx
grid_idx = base + offset
weight = w[i].x * w[j].y
grid_v[grid_idx] += weight * (p_mass * v[p] + affine @ dpos)
grid_m[grid_idx] += weight * p_mass
@ti.kernel
def grid_op():
for i, j in grid_m:
if grid_m[i, j] > 0:
grid_v[i, j] /= grid_m[i, j]
grid_v[i, j].y -= dt * gravity
if i < bound and grid_v[i, j].x < 0:
grid_v[i, j].x = 0
if i > n_grid - bound and grid_v[i, j].x > 0:
grid_v[i, j].x = 0
if j < bound and grid_v[i, j].y < 0:
grid_v[i, j].y = 0
if j > n_grid - bound and grid_v[i, j].y > 0:
grid_v[i, j].y = 0
@ti.kernel
def g2p():
for p in x:
Xp = x[p] / dx
base = int(Xp - 0.5)
fx = Xp - base
w = [0.5 * (1.5 - fx) ** 2, 0.75 - (fx - 1) ** 2, 0.5 * (fx - 0.5) ** 2]
new_v = ti.Vector.zero(float, 2)
new_C = ti.Matrix.zero(float, 2, 2)
for i, j in ti.static(ti.ndrange(3, 3)):
offset = ti.Vector([i, j])
dpos = (offset - fx) * dx
grid_idx = base + offset
grid_idx = ti.Vector([ti.math.clamp(grid_idx[0], 0, grid_v.shape[0] - 1),ti.math.clamp(grid_idx[1], 0, grid_v.shape[1] - 1)])
weight = w[i].x * w[j].y
g_v = grid_v[grid_idx]
new_v += weight * g_v
new_C += 4 * weight * g_v.outer_product(dpos) / dx**2
v[p] = new_v
x[p] += dt * v[p]
C[p] = new_C
@ti.kernel
def init():
for i in range(n_particles):
x[i] = [ti.random() * 0.5 + 0.25, ti.random() * 0.5 + 0.50]
v[i] = [0, -1]
F[i] = ti.Matrix([[1, 0], [0, 1]])
Initialize the particles
init()
Main GUI loop
gui = ti.ui.Window("MPM88", (1024, 1024))
canvas = gui.get_canvas()
while gui.running:
canvas.set_background_color((1, 1, 1))
for s in range(2):
reset_grid()
p2g()
grid_op()
g2p()
canvas.circles(x, radius=0.005, per_vertex_color=color)
gui.show()
Implement phase-field fracture, you need to implement your own CG, implement it fully
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Certainly! Implementing a phase-field fracture model within your MPM simulation involves introducing a damage variable, modifying the stress calculations, and solving a PDE for the phase-field variable using a Conjugate Gradient (CG) solver. Below, I've provided the code that integrates these components into your simulation, including a fully implemented CG solver. I've also included detailed explanations to help you understand each part of the implementation.
python
import taichi as ti
# Initialize Taichi
ti.init(arch=ti.cpu, debug=False)
# Simulation parameters
q = 0.5
n_particles = int(8192 * q**2)
n_grid = int(128 * q)
dx, inv_dx = 1 / n_grid, float(n_grid)
dt = 1e-4
# Material properties
p_rho = 1
p_vol = (dx * 0.5) ** 2
p_mass = p_vol * p_rho
gravity = 98
bound = 3
E, nu = 3e3, 0.0 # Young's modulus and Poisson's ratio
mu, la = E / (2 * (1 + nu)), E * nu / ((1 + nu) * (1 - 2 * nu))
# Phase-field fracture parameters
Gc = 1e-2 # Fracture energy
l0 = 1e-2 # Length scale parameter
# Fields
x = ti.Vector.field(2, float, n_particles) # Particle positions
v = ti.Vector.field(2, float, n_particles) # Particle velocities
C = ti.Matrix.field(2, 2, float, n_particles) # Affine velocity field
F = ti.Matrix.field(2, 2, float, n_particles) # Deformation gradient (F)
grid_v = ti.Vector.field(2, float, (n_grid, n_grid)) # Grid node velocities
grid_m = ti.field(float, (n_grid, n_grid)) # Grid node masses
# Phase-field variables on the grid
d = ti.field(dtype=ti.f32, shape=(n_grid, n_grid)) # Damage variable
H = ti.field(dtype=ti.f32, shape=(n_grid, n_grid)) # History field (strain energy density)
# Fields for CG solver
b = ti.field(dtype=ti.f32, shape=(n_grid, n_grid)) # Right-hand side of linear system
x_cg = ti.field(dtype=ti.f32, shape=(n_grid, n_grid)) # Solution vector
r = ti.field(dtype=ti.f32, shape=(n_grid, n_grid)) # Residual vector
p = ti.field(dtype=ti.f32, shape=(n_grid, n_grid)) # Search direction
Ap = ti.field(dtype=ti.f32, shape=(n_grid, n_grid)) # Matrix-vector product
# Utility functions
@ti.func
def clamp_grid_idx(grid_idx):
return ti.Vector([ti.min(ti.max(grid_idx[0], 0), n_grid - 1),
ti.min(ti.max(grid_idx[1], 0), n_grid - 1)])
@ti.func
def interpolate_d(base, fx):
w = [0.5 * (1.5 - fx)**2,
0.75 - (fx - 1.0)**2,
0.5 * (fx - 0.5)**2]
d_interp = 0.0
for i in ti.static(range(3)):
for j in ti.static(range(3)):
offset = ti.Vector([i, j])
weight = w[i][0] * w[j][1]
grid_idx = base + offset
grid_idx_clamped = clamp_grid_idx(grid_idx)
d_interp += weight * d[grid_idx_clamped]
return d_interp
# Kernel to reset grid variables
@ti.kernel
def reset_grid():
for i, j in grid_m:
grid_v[i, j] = [0, 0]
grid_m[i, j] = 0.0
# Particle-to-grid (P2G) transfer
@ti.kernel
def p2g():
for p in x:
Xp = x[p] * inv_dx
base = int(Xp - 0.5)
fx = Xp - base
w = [0.5 * (1.5 - fx)**2,
0.75 - (fx - 1.0)**2,
0.5 * (fx - 0.5)**2]
# Compute deformation gradient F
F[p] = (ti.Matrix.identity(float, 2) + dt * C[p]) @ F[p]
U, sig, V = ti.svd(F[p])
J = 1.0
for d_i in ti.static(range(2)):
J *= sig[d_i, d_i]
# Interpolate damage variable d to particle
d_particle = interpolate_d(base, fx)
damage = (1 - d_particle)**2
# Compute stress tensor with damage
stress = damage * (2 * mu * (F[p] - U @ V.transpose()) @ F[p].transpose() +
ti.Matrix.identity(float, 2) * la * J * (J - 1))
stress = (-dt * p_vol * 4 * inv_dx * inv_dx) * stress
affine = stress + p_mass * C[p]
# P2G scattering
for i in ti.static(range(3)):
for j in ti.static(range(3)):
offset = ti.Vector([i, j])
dpos = (offset.cast(float) - fx) * dx
weight = w[i][0] * w[j][1]
grid_idx = base + offset
grid_v[grid_idx] += weight * (p_mass * v[p] + affine @ dpos)
grid_m[grid_idx] += weight * p_mass
# Grid operations (update velocities, apply gravity and boundary conditions)
@ti.kernel
def grid_op():
for i, j in grid_m:
if grid_m[i, j] > 0:
grid_v[i, j] /= grid_m[i, j]
grid_v[i, j].y -= dt * gravity
# Boundary conditions
if i < bound and grid_v[i, j].x < 0:
grid_v[i, j].x = 0
if i > n_grid - bound and grid_v[i, j].x > 0:
grid_v[i, j].x = 0
if j < bound and grid_v[i, j].y < 0:
grid_v[i, j].y = 0
if j > n_grid - bound and grid_v[i, j].y > 0:
grid_v[i, j].y = 0
# Grid-to-particle (G2P) transfer
@ti.kernel
def g2p():
for p in x:
Xp = x[p] * inv_dx
base = int(Xp - 0.5)
fx = Xp - base
w = [0.5 * (1.5 - fx)**2,
0.75 - (fx - 1.0)**2,
0.5 * (fx - 0.5)**2]
new_v = ti.Vector.zero(float, 2)
new_C = ti.Matrix.zero(float, 2, 2)
for i in ti.static(range(3)):
for j in ti.static(range(3)):
offset = ti.Vector([i, j])
dpos = (offset.cast(float) - fx) * dx
weight = w[i][0] * w[j][1]
grid_idx = base + offset
grid_idx = clamp_grid_idx(grid_idx)
g_v = grid_v[grid_idx]
new_v += weight * g_v
new_C += 4 * weight * g_v.outer_product(dpos) * inv_dx
v[p] = new_v
x[p] += dt * v[p]
C[p] = new_C
# Initialize damage variable d and history field H
@ti.kernel
def init_phase_field():
for i, j in d:
d[i, j] = 0.0 # No damage initially
H[i, j] = 0.0 # Zero initial history field
# Update history field H (maximum strain energy density)
@ti.kernel
def update_history_field():
for p in x:
Xp = x[p] * inv_dx
base = int(Xp - 0.5)
fx = Xp - base
w = [0.5 * (1.5 - fx)**2,
0.75 - (fx - 1.0)**2,
0.5 * (fx - 0.5)**2]
# Compute strain energy density at particle
U, sig, V = ti.svd(F[p])
J = 1.0
for d_i in ti.static(range(2)):
J *= sig[d_i, d_i]
logJ = ti.log(J)
energy_density = 0.5 * mu * ((F[p] @ F[p].transpose()).trace() - 2) - mu * ti.log(J) + 0.5 * la * logJ * logJ
# Scatter maximum energy density to grid
for i in ti.static(range(3)):
for j in ti.static(range(3)):
offset = ti.Vector([i, j])
weight = w[i][0] * w[j][1]
grid_idx = base + offset
grid_idx = clamp_grid_idx(grid_idx)
ti.atomic_max(H[grid_idx], energy_density * weight)
# Assemble right-hand side vector b for the linear system
@ti.kernel
def assemble_b():
for i, j in b:
b[i, j] = 2 * (1 - d[i, j]) * H[i, j]
# Compute matrix-vector product Ap = A * p
@ti.kernel
def compute_Ap(p_in: ti.template(), Ap_out: ti.template()):
for i, j in Ap_out:
laplacian = -4.0 * p_in[i, j]
if i > 0:
laplacian += p_in[i - 1, j]
if i < n_grid - 1:
laplacian += p_in[i + 1, j]
if j > 0:
laplacian += p_in[i, j - 1]
if j < n_grid - 1:
laplacian += p_in[i, j + 1]
Ap_out[i, j] = Gc * (p_in[i, j] - l0**2 * laplacian) + 2 * H[i, j] * p_in[i, j]
# Conjugate Gradient solver
def CG_solver():
x_cg.copy_from(d) # Initialize x_cg with current damage variable
compute_Ap(x_cg, Ap)
subtract_in_place(r, Ap)
r.copy_from(b)
p.copy_from(r)
rs_old = dot_product(r, r)
tol = 1e-6
max_iter = 100
for _ in range(max_iter):
compute_Ap(p, Ap)
alpha = rs_old / dot_product(p, Ap)
update_x_cg(alpha)
update_r(alpha)
rs_new = dot_product(r, r)
if ti.sqrt(rs_new) < tol:
break
beta = rs_new / rs_old
update_p(beta)
rs_old = rs_new
d.copy_from(x_cg) # Update damage variable with solution
# Helper functions for CG solver
@ti.kernel
def subtract_in_place(a: ti.template(), b: ti.template()):
for i, j in a:
a[i, j] -= b[i, j]
def dot_product(a, b):
result = ti.field(dtype=ti.f32, shape=())
@ti.kernel
def compute_dot_product(result: ti.template()):
tmp = 0.0
for i, j in a:
tmp += a[i, j] * b[i, j]
result[None] = tmp
compute_dot_product(result)
return result[None]
@ti.kernel
def update_x_cg(alpha: ti.f32):
for i, j in x_cg:
x_cg[i, j] += alpha * p[i, j]
@ti.kernel
def update_r(alpha: ti.f32):
for i, j in r:
r[i, j] -= alpha * Ap[i, j]
@ti.kernel
def update_p(beta: ti.f32):
for i, j in p:
p[i, j] = r[i, j] + beta * p[i, j]
# Initialize particle positions and velocities
@ti.kernel
def init():
for i in range(n_particles):
x[i] = [ti.random() * 0.5 + 0.25, ti.random() * 0.5 + 0.25]
v[i] = [0, -1]
F[i] = ti.Matrix([[1, 0], [0, 1]])
C[i] = ti.Matrix.zero(float, 2, 2)
# Main simulation loop
init()
init_phase_field()
gui = ti.GUI("MPM Phase-field Fracture", res=(512, 512))
while gui.running:
for s in range(10):
reset_grid()
p2g()
grid_op()
update_history_field()
assemble_b()
CG_solver()
g2p()
gui.clear(0xFFFFFF)
gui.circles(x.to_numpy(), radius=1.5, color=0x3288bd)
# Visualize damage field
d_np = d.to_numpy()
gui.set_image(d_np)
gui.show()
Explanation:
Phase-field Fracture Variables:
d: The phase-field damage variable, where d = 0 represents undamaged material and d = 1 represents fully damaged material. It is defined on the grid.
H: The history field, which stores the maximum strain energy density experienced by the material at each point. It is used to drive the evolution of the damage variable d.
Integration into MPM Simulation:
Modifying Stress Calculations:
In the p2g() function, we interpolate the damage variable d from the grid to the particles using the interpolate_d() function.
The stress tensor is modified to include the damage effect:
This degradation of the material stiffness models the effect of fractures on the material's ability to withstand stress.
Updating History Field H:
The update_history_field() function computes the strain energy density at each particle and scatters the maximum value back to the grid.
This represents the material's memory of the highest energy state, which influences the progression of damage.
Solving the Phase-field PDE:
The phase-field PDE governing the damage evolution is discretized and results in a linear system of equations:
[ G_c (d - l_0^2 \nabla^2 d) = 2(1 - d)H ]
We assemble the right-hand side vector b in the assemble_b() function.
The CG_solver() function implements the Conjugate Gradient method to solve the linear system ( A d = b ), where ( A ) includes contributions from both the damage variable and the Laplacian operator.
Conjugate Gradient Solver Details:
Initialization:
x_cg is initialized with the current damage variable d.
The initial residual r is computed.
The search direction p is set to r.
Iteration:
In each iteration, we compute the matrix-vector product Ap = A * p.
The scalar alpha is computed to determine the step size.
We update the solution x_cg and the residual r.
The scalar beta is computed to adjust the search direction.
The process repeats until convergence criteria are met (tolerance or maximum iterations).
Update Damage Variable:
Important Notes:
Boundary Conditions:
The code assumes Neumann boundary conditions for the phase-field PDE. Adjustments may be needed depending on the desired behavior at the boundaries.
Numerical Stability:
The parameters Gc, l0, and the CG solver settings (tol and max_iter) may need tuning to ensure numerical stability and realistic fracture patterns.
A small time step dt and fine grid resolution n_grid help in capturing detailed fracture behavior but increase computational cost.
Material Model:
The energy density computation in update_history_field() uses a Neo-Hookean model. Depending on the material being simulated, you may need to adjust this model to reflect its mechanical properties accurately.
This implementation provides a foundation for simulating phase-field fracture within an MPM framework using a fully implemented Conjugate Gradient solver. You can further refine the model by incorporating more advanced material models, improving numerical methods, or optimizing the solver for performance.
References:
Miehe, C., Hofacker, M., & Welschinger, F. (2010). A phase field model for rate-independent crack propagation: Robust algorithmic implementation based on operator splits. Computer Methods in Applied Mechanics and Engineering, 199(45-48), 2765-2778.
Borden, M. J., Hughes, T. J., Landis, C. M., Anvari, A., & Lee, I. J. (2016). A phase-field formulation for fracture in ductile materials: Finite deformation balance law derivation, plastic degradation, and stress triaxiality effects. Computer Methods in Applied Mechanics and Engineering, 312, 130-166.
Feel free to adjust the parameters and refine the implementation to suit your specific simulation needs.