Certainly! Below is a Taichi implementation of the Material Point Method (MPM) with phase-field fracture, utilizing a Conjugate Gradient (CG) solver. This code provides a basic framework that you can expand upon for more complex simulations.
Please note: Due to the complexity of MPM simulations with phase-field fracture models, this implementation is a simplified version intended for educational purposes. For production-level code or detailed simulations, further optimization and refinement would be necessary.
python
import taichi as ti
import numpy as np
ti.init(arch=ti.gpu) # Use GPU for acceleration if available
# Simulation parameters
dim = 2
n_particles = 10000
n_grid = 128
dx = 1 / n_grid
dt = 1e-4
p_vol = (dx * 0.5) ** 2
p_rho = 1
p_mass = p_vol * p_rho
E = 1e4 # Young's modulus
nu = 0.2 # Poisson's ratio
mu = E / (2 * (1 + nu)) # Shear modulus
la = E * nu / ((1 + nu) * (1 - 2 * nu)) # Lambda for elasticity
# Phase-field fracture parameters
Gc = 1e-2 # Fracture energy
l0 = dx * 2 # Regularization parameter for phase-field
# Taichi fields
particle_x = ti.Vector.field(dim, dtype=ti.f32, shape=n_particles)
particle_v = ti.Vector.field(dim, dtype=ti.f32, shape=n_particles)
particle_C = ti.Matrix.field(dim, dim, dtype=ti.f32, shape=n_particles) # Affine velocity field
particle_F = ti.Matrix.field(dim, dim, dtype=ti.f32, shape=n_particles) # Deformation gradient
particle_Jp = ti.field(dtype=ti.f32, shape=n_particles) # Plastic deformation determinant
particle_phi = ti.field(dtype=ti.f32, shape=n_particles) # Phase-field variable
particle_phi_new = ti.field(dtype=ti.f32, shape=n_particles) # Updated phase-field variable
grid_v = ti.Vector.field(dim, dtype=ti.f32, shape=(n_grid, n_grid))
grid_m = ti.field(dtype=ti.f32, shape=(n_grid, n_grid))
grid_phi = ti.field(dtype=ti.f32, shape=(n_grid, n_grid))
# Conjugate Gradient solver fields
cg_max_iterations = 50
cg_tolerance = 1e-4
b = ti.field(dtype=ti.f32, shape=n_particles)
x = ti.field(dtype=ti.f32, shape=n_particles)
r = ti.field(dtype=ti.f32, shape=n_particles)
p = ti.field(dtype=ti.f32, shape=n_particles)
Ap = ti.field(dtype=ti.f32, shape=n_particles)
@ti.kernel
def initialize():
for i in range(n_particles):
particle_x[i] = [ti.random() * 0.4 + 0.3, ti.random() * 0.4 + 0.3] # Initialize positions
particle_v[i] = [0, 0]
particle_F[i] = ti.Matrix.identity(ti.f32, dim)
particle_Jp[i] = 1
particle_phi[i] = 1.0 # Initially, no damage
@ti.kernel
def particle_to_grid():
for p in range(n_particles):
Xp = particle_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]
# Compute stress and apply to grid
F = particle_F[p]
J = F.determinant()
phi = ti.max(0.0, particle_phi[p]) # Ensure phase-field variable is non-negative
# Degraded elastic moduli
mu_ = (1 - phi) ** 2 * mu
la_ = (1 - phi) ** 2 * la
# Compute stress tensor sigma
stress = (2 * mu_ * (F - F.transpose().inverse()) +
la_ * (J - 1) * ti.Matrix.identity(ti.f32, dim))
stress = (-dt * p_vol * stress) @ F.transpose()
for i, j in ti.static(ti.ndrange(3, 3)):
offset = ti.Vector([i, j])
dpos = (offset - fx) * dx
weight = w[i][0] * w[j][1]
grid_v[base + offset] += weight * (p_mass * particle_v[p] + stress @ dpos)
grid_m[base + offset] += weight * p_mass
@ti.kernel
def grid_operations():
for i, j in grid_m:
if grid_m[i, j] > 0:
grid_v[i, j] = grid_v[i, j] / grid_m[i, j]
grid_v[i, j][1] -= dt * 9.8 # Gravity
# Boundary conditions
if i < 3 or i > n_grid - 3:
grid_v[i, j][0] = 0
if j < 3 or j > n_grid - 3:
grid_v[i, j][1] = 0
@ti.kernel
def grid_to_particle():
for p in range(n_particles):
Xp = particle_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(ti.f32, dim)
new_C = ti.Matrix.zero(ti.f32, dim, dim)
for i, j in ti.static(ti.ndrange(3, 3)):
offset = ti.Vector([i, j])
dpos = (offset - fx) * dx
weight = w[i][0] * w[j][1]
g_v = grid_v[base + offset]
new_v += weight * g_v
new_C += 4 * weight * ti.outer_product(g_v, dpos) / dx ** 2
particle_v[p] = new_v
particle_x[p] += dt * new_v
F = (ti.Matrix.identity(ti.f32, dim) + dt * new_C) @ particle_F[p]
particle_F[p] = F
@ti.kernel
def update_phase_field():
# Update phase-field variable using a CG solver
# Here we set up the system A x = b
# For simplicity, we assume A is diagonally dominant and approximate
# In practice, you need to assemble A properly
for i in range(n_particles):
# Compute local fracture energy release rate
# This is a simplified version; in practice, you need to compute the divergence of the stress
b[i] = (Gc / l0) * (1 - particle_phi[i]) - l0 * particle_phi[i]
x[i] = particle_phi[i] # Initial guess
cg_solve()
for i in range(n_particles):
particle_phi_new[i] = x[i]
@ti.kernel
def copy_phase_field():
for i in range(n_particles):
particle_phi[i] = particle_phi_new[i]
def cg_solve():
# Conjugate Gradient Solver
@ti.kernel
def compute_Ap():
for i in range(n_particles):
Ap[i] = (Gc / l0 + l0) * p[i] # Simplified A matrix multiplication
@ti.kernel
def reduce(r_dot_r: ti.f32):
for i in r:
ti.atomic_add(r_dot_r, r[i] * r[i])
@ti.kernel
def update_x_r(alpha: ti.f32):
for i in x:
x[i] += alpha * p[i]
r[i] -= alpha * Ap[i]
@ti.kernel
def update_p(beta: ti.f32):
for i in p:
p[i] = r[i] + beta * p[i]
r_dot_r = ti.field(dtype=ti.f32, shape=())
r_dot_r[None] = 0.0
# Initialize
compute_Ap()
for i in range(n_particles):
r[i] = b[i] - Ap[i]
p[i] = r[i]
# CG Iterations
for iter in range(cg_max_iterations):
r_dot_r[None] = 0.0
compute_Ap()
reduce_kernel = reduce(r_dot_r)
r_dot_r_prev = r_dot_r[None]
Ap_p = sum(Ap[i] * p[i] for i in range(n_particles))
alpha = r_dot_r_prev / Ap_p
update_x_r(alpha)
compute_Ap()
r_dot_r[None] = 0.0
reduce_kernel = reduce(r_dot_r)
r_dot_r_curr = r_dot_r[None]
if r_dot_r_curr < cg_tolerance:
break
beta = r_dot_r_curr / r_dot_r_prev
update_p(beta)
def substep():
grid_v.fill(0)
grid_m.fill(0)
particle_to_grid()
grid_operations()
grid_to_particle()
update_phase_field()
copy_phase_field()
initialize()
gui = ti.GUI("MPM with Phase-Field Fracture", res=(512, 512))
for frame in range(1000):
for s in range(10):
substep()
positions = particle_x.to_numpy()
phi = particle_phi.to_numpy()
colors = np.zeros((n_particles, 3))
colors[:, 0] = 1 - phi # Color particles based on phase-field variable
colors[:, 2] = phi
gui.circles(positions, radius=1.5, color=colors)
gui.show()
Explanation:
Initialization: Particles are initialized within a region, and their deformation gradient F, affine velocity field C, and phase-field variable phi are set.
Particle to Grid (P2G): Particles transfer their mass and momentum to the grid nodes. The stress tensor is computed considering the degradation due to the phase-field variable.
Grid Operations: Grid velocities are updated with gravity and boundary conditions are applied.
Grid to Particle (G2P): Particles update their velocities and positions based on grid information.
Phase-Field Update: The phase-field variable phi is updated using a simplified Conjugate Gradient solver. This step simulates the fracture process by degrading material properties.
Conjugate Gradient Solver: A basic CG solver is implemented to solve the linear system for the phase-field update.
Visualization: Particles are visualized with colors representing the phase-field variable (phi), indicating intact (blue) and fractured (red) regions.
Important Notes:
Simplifications: The implementation uses a simplified phase-field update and CG solver for demonstration purposes. A full implementation would require assembling the stiffness matrix and handling boundary conditions appropriately.
Performance: For large-scale simulations or more accurate results, consider optimizing the code and using more sophisticated numerical methods.
Phase-Field Models: Phase-field fracture models are complex and involve solving partial differential equations. Ensure that the chosen parameters (Gc, l0) are appropriate for your material and simulation scale.
Constitutive Models: The stress tensor computation is based on a simple neo-Hookean model with degradation due to damage. Depending on the material, you may need a different constitutive model.
Feel free to adjust the parameters, improve the CG solver, or incorporate more complex physics as needed for your specific application.