Certainly! Below is a Taichi implementation of the Material Point Method (MPM) with phase-field fracture using a Conjugate Gradient (CG) solver. This implementation includes the essential components:
- Initialization: Setting up particles and grid.
- Phase-field fracture: Solving the phase-field evolution equation to simulate fracture.
- MPM Simulation Loop: Includes Particle-to-Grid (P2G), Grid Operations (including solving for the phase-field variable), and Grid-to-Particle (G2P) steps.
- CG Solver: Used to solve the discretized phase-field equation on the grid.
import taichi as ti
import numpy as np
ti.init(arch=ti.cpu) # Use 'ti.gpu' if you have a GPU
# Simulation parameters
dim = 2 # 2D simulation
n_particles = 10000
n_grid = 128
dx = 1.0 / n_grid
inv_dx = float(n_grid)
dt = 1e-4 # Time step
substeps = 10 # Number of substeps per frame
total_frames = 200
p_vol = (dx * 0.5) ** 2 # Particle volume
p_rho = 1.0 # Particle density
p_mass = p_vol * p_rho
gravity = 9.8 # Gravity
E = 1e4 # Young's modulus
nu = 0.3 # Poisson's ratio
mu_0 = E / (2 * (1 + nu)) # Lame parameters
lambda_0 = E * nu / ((1 + nu) * (1 - 2 * nu))
# Phase-field parameters
Gc = 1.0 # Critical energy release rate
l0 = 0.01 # Length scale parameter
# Particle fields
particle_pos = ti.Vector.field(dim, dtype=ti.f32, shape=n_particles)
particle_vel = ti.Vector.field(dim, dtype=ti.f32, shape=n_particles)
particle_F = ti.Matrix.field(dim, dim, dtype=ti.f32, shape=n_particles)
particle_Jp = ti.field(dtype=ti.f32, shape=n_particles)
particle_phase = ti.field(dtype=ti.f32, shape=n_particles) # Damage variable (phase-field)
# Grid fields
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_phase = ti.field(dtype=ti.f32, shape=(n_grid, n_grid)) # Grid-based phase-field variable
# Initialize particles
@ti.kernel
def initialize():
for i in range(n_particles):
particle_pos[i] = [ti.random() * 0.4 + 0.3, ti.random() * 0.2 + 0.2]
particle_vel[i] = [0, 0]
particle_F[i] = ti.Matrix.identity(ti.f32, dim)
particle_Jp[i] = 1.0
particle_phase[i] = 1.0 # Initially, no damage
# Particle to Grid (P2G)
@ti.kernel
def p2g():
for p in particle_pos:
Xp = particle_pos[p] * inv_dx
base = ti.floor(Xp - 0.5).cast(int)
fx = Xp - base
w = [0.5 * (1.5 - fx)**2,
0.75 - (fx - 1)**2,
0.5 * (fx - 0.5)**2]
deformation_gradient = particle_F[p]
volume = p_vol * particle_Jp[p]
# Calculate stress
stress = (-dt * 4 * inv_dx * inv_dx) * (
mu_0 * (deformation_gradient - deformation_gradient.transpose())
+ lambda_0 * ti.Matrix.identity(ti.f32, dim) * ti.log(particle_Jp[p])
)
# Apply damage (phase-field variable)
stress *= particle_phase[p]**2
affine = stress + p_mass * particle_vel[p].outer_product([0, 0])
for i in ti.static(range(3)):
for j in ti.static(range(3)):
offset = ti.Vector([i, j])
dpos = (offset.cast(ti.f32) - fx) * dx
weight = w[i][0] * w[j][1]
grid_v[base + offset] += weight * (p_mass * particle_vel[p] + affine @ dpos)
grid_m[base + offset] += weight * p_mass
grid_phase[base + offset] += weight * particle_phase[p]
# Grid operations 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][1] -= dt * gravity # Apply gravity
# Boundary conditions
if i < 3 and grid_v[i, j][0] < 0:
grid_v[i, j][0] = 0
if i > n_grid - 3 and grid_v[i, j][0] > 0:
grid_v[i, j][0] = 0
if j < 3 and grid_v[i, j][1] < 0:
grid_v[i, j][1] = 0
if j > n_grid - 3 and grid_v[i, j][1] > 0:
grid_v[i, j][1] = 0
# Build the linear system for the phase-field equation
n_nodes = n_grid * n_grid
A_builder = ti.linalg.SparseMatrixBuilder(n_nodes, n_nodes, max_num_triplets=5 * n_nodes)
b = ti.field(dtype=ti.f32, shape=n_nodes)
x = ti.field(dtype=ti.f32, shape=n_nodes)
@ti.kernel
def build_A_and_b():
for i, j in ti.ndrange(n_grid, n_grid):
k = i * n_grid + j
b[k] = grid_phase[i, j] # Right-hand side (c_old)
center = 1.0 + dt * 4 * Gc * l0 / dx**2
A_builder[k, k] += center
coeff = -dt * Gc * l0 / dx**2
if i > 0:
left_k = (i - 1) * n_grid + j
A_builder[k, left_k] += coeff
if i < n_grid - 1:
right_k = (i + 1) * n_grid + j
A_builder[k, right_k] += coeff
if j > 0:
down_k = i * n_grid + (j - 1)
A_builder[k, down_k] += coeff
if j < n_grid - 1:
up_k = i * n_grid + (j + 1)
A_builder[k, up_k] += coeff
# Update phase-field variable after solving
@ti.kernel
def update_phase_field():
for i, j in ti.ndrange(n_grid, n_grid):
k = i * n_grid + j
grid_phase[i, j] = x[k]
# Solve phase-field equation using CG solver
def solve_phase_field():
A_builder.clear()
build_A_and_b()
A_csr = A_builder.build()
solver = ti.linalg.SparseSolver(solver_type="CG", tol=1e-6)
solver.analyze_pattern(A_csr)
solver.factorize(A_csr)
solver.solve(b, x)
update_phase_field()
# Grid to Particle (G2P)
@ti.kernel
def g2p():
for p in particle_pos:
Xp = particle_pos[p] * inv_dx
base = ti.floor(Xp - 0.5).cast(int)
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_phase = 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
new_v += weight * grid_v[grid_idx]
new_phase += weight * grid_phase[grid_idx]
particle_vel[p] = new_v
particle_pos[p] += dt * particle_vel[p]
particle_phase[p] = new_phase
# Update deformation gradient and other properties as needed
# Main simulation loop
initialize()
for frame in range(total_frames):
for step in range(substeps):
grid_v.fill(0)
grid_m.fill(0)
grid_phase.fill(0)
p2g()
grid_op()
solve_phase_field()
g2p()
# (Optional) Visualization code goes here
Explanation:
-
Initialization: Particles are initialized with positions, velocities, deformation gradients, and the phase-field variable (particle_phase), which represents damage (1.0 means intact).
-
P2G (Particle to Grid):
- Particle properties are transferred to the grid.
- The stress tensor is modified by multiplying it with the square of the phase-field variable to simulate damage reducing the material's strength.
-
Grid Operations:
- Grid velocities are updated, and gravity is applied.
- Boundary conditions are enforced to prevent particles from leaving the simulation domain.
-
Phase-Field Solver:
- The phase-field equation is discretized on the grid using finite differences.
- A sparse linear system is constructed and solved using the Conjugate Gradient (CG) solver provided by Taichi.
- The updated phase-field variable on the grid (
grid_phase) replaces the old values.
-
G2P (Grid to Particle):
- Grid properties are interpolated back to particles.
- The phase-field variable is updated on particles based on the grid values.
- Particle positions and velocities are updated for the next time step.
-
Simulation Loop:
- The simulation runs for a specified number of frames and substeps.
- At each step, the fields are reset, and the P2G, grid operations, phase-field solving, and G2P steps are executed.
Notes:
- Damage Evolution: In this simplified implementation, the phase-field evolution is governed by a diffusion-like equation. In practice, you would include terms related to the strain energy density to model crack initiation and propagation accurately.
- Numerical Stability: Care must be taken with the choice of time step
dt and grid resolution n_grid to ensure numerical stability, especially in simulations involving fractures.
- Visualization: For a complete simulation, you would include code to visualize the particles and the phase-field variable to observe the fracture behavior.
- Extensions: This is a basic framework. For more accurate and efficient simulations, you might consider more advanced techniques, such as adaptive remeshing, explicit time integration for certain terms, or improved constitutive models.
Feel free to adjust parameters, add visualization, and refine the model according to your specific requirements.