import taichi as ti from taichi.linalg import MatrixFreeCG, LinearOperator…
- User
import taichi as ti from taichi.linalg import MatrixFreeCG, LinearOperator ti.init(arch=ti.cpu,debug=False) 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
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)) 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 with CG you implemented, use Ginzburg-Landau PDE.
- Assistant
To implement phase-field fracture using the Ginzburg-Landau PDE in your MPM simulation, we'll introduce a phase-field variable
phithat represents the damage state of the material. The phase-field variable smoothly transitions between 0 (intact material) and 1 (fully fractured material). We'll couple this with your existing simulation by modifying the material's stiffness based on the local value ofphi.We'll use the Conjugate Gradient (CG) method to solve the Ginzburg-Landau PDE for the phase-field evolution.
Here's how we can proceed:
-
Introduce the Phase-Field Variable
phi: We'll definephion the grid to capture the damage evolution. -
Implement the Ginzburg-Landau PDE: We'll discretize the PDE and set up a linear system suitable for the CG solver.
-
Update Material Properties: We'll modify the stress computation to account for the degradation due to
phi. -
Integrate the CG Solver: We'll use Taichi's
MatrixFreeCGor implement our own CG solver to evolvephi.
Below is the modified code with explanations:
import taichi as ti from taichi.linalg import MatrixFreeCG, LinearOperator 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 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)) # Material properties for phase-field fracture Gc = 1.0 # Critical energy release rate ell = 0.02 # Regularization parameter (controls the width of the crack) beta = 1e-2 # Small parameter to avoid singularity # Fields x = ti.Vector.field(2, float, n_particles) # Positions v = ti.Vector.field(2, float, n_particles) # Velocities C = ti.Matrix.field(2, 2, float, n_particles) # APIC affine matrices F = ti.Matrix.field(2, 2, float, n_particles) # Deformation gradients # Grid fields grid_v = ti.Vector.field(2, float, (n_grid, n_grid)) grid_m = ti.field(float, (n_grid, n_grid)) # Phase-field variable (defined on the grid) phi = ti.field(float, (n_grid, n_grid)) # Damage variable phi_new = ti.field(float, (n_grid, n_grid)) # Updated damage variable b = ti.field(float, (n_grid, n_grid)) # Right-hand side for CG residual = ti.field(float, (n_grid, n_grid)) # Residual for CG # Initialize the grid @ti.kernel def reset_grid(): for i, j in grid_m: grid_v[i, j] = [0, 0] grid_m[i, j] = 0 # Particle to grid (P2G) @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] # Compute degradation function based on phi c = 1 - phi[base] # You may interpolate phi for smoother results # Degraded material properties mu_eff = c**2 * mu + beta la_eff = c**2 * la + beta 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_eff * (F[p] - U @ V.transpose()) @ F[p].transpose() \ + ti.Matrix.identity(float, 2) * la_eff * 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 # Grid operations @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) @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, n_grid - 1), ti.math.clamp(grid_idx[1], 0, n_grid - 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 # Initialize particles and phi @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]]) for i, j in phi: phi[i, j] = 0.0 # Initially, the material is intact # Build the linear operator for CG solver class PhiOperator(LinearOperator): def __init__(self, N): self.N = N @ti.kernel def apply(self, x: ti.template(), y: ti.template()): for i, j in phi: laplacian = (-4 * x[i, j] + x[i - 1, j] + x[i + 1, j] + x[i, j - 1] + x[i, j + 1]) / dx**2 y[i, j] = (-Gc * ell * laplacian + (Gc / ell + 2 * psi[i, j]) * x[i, j]) # Compute the elastic energy density psi psi = ti.field(float, (n_grid, n_grid)) @ti.kernel def compute_psi(): for p in x: Xp = x[p] / dx base = int(Xp) fx = Xp - base w = [1 - fx, fx] # Simple interpolation (you may use higher-order if needed) for i, j in ti.static(ti.ndrange(2, 2)): grid_idx = base + ti.Vector([i, j]) weight = w[i].x * w[j].y # Assuming small deformation, psi = 0.5 * stress * strain # For simplicity, we can approximate psi as some function of F epsilon = F[p] - ti.Matrix.identity(float, 2) psi[grid_idx] += weight * 0.5 * mu * (epsilon @ epsilon.transpose()).trace() # Solve for phi using CG def solve_phi(): N = n_grid * n_grid phi_op = PhiOperator(N) cg = MatrixFreeCG(solver=phi_op, tol=1e-4, max_iter=100) cg.solve(b, phi, initial_guess=phi) # Update the phase-field variable phi def update_phi(): # Assemble the right-hand side b compute_psi() @ti.kernel def assemble_b(): for i, j in b: b[i, j] = (Gc / ell) * phi[i, j] - 2 * psi[i, j] assemble_b() # Solve the linear system solve_phi() # Main simulation loop init() gui = ti.GUI("MPM with Phase-Field Fracture", res=(512, 512)) for frame in range(1000): for step in range(10): reset_grid() p2g() grid_op() g2p() update_phi() # Visualization gui.clear(0xFFFFFF) pos = x.to_numpy() gui.circles(pos, radius=1.5, color=0x0) gui.show()Explanation of the Modifications:
-
Phase-Field Variable
phi: We addedphias a grid field to represent the damage in the material. It's initialized to zero, indicating no damage initially. -
Damage-Degraded Material Properties:
- In
p2g(), we compute a degradation factorc = 1 - phi[base]. - We modify the shear modulus
mu_effand the bulk modulusla_effby multiplying them withc^2to degrade the material's stiffness in damaged regions.
- In
-
Elastic Energy Density
psi:- We compute
psiat each grid node, which represents the stored elastic energy. This is used in the Ginzburg-Landau PDE to drive the damage evolution.
- We compute
-
Ginzburg-Landau PDE:
- We discretize the PDE and set up a linear system
A * phi = b, whereAis implemented in thePhiOperatorclass. - The operator applies the Laplacian to
phiand includes terms involvingphiandpsi.
- We discretize the PDE and set up a linear system
-
Conjugate Gradient Solver:
- We use Taichi's
MatrixFreeCGto solve the linear system. ThePhiOperatorclass defines how to computeA * phi. - The
solve_phi()function sets up and runs the CG solver.
- We use Taichi's
-
Coupling with MPM Simulation:
- The updated
phiaffects the stress calculation through the degraded material properties. - This creates a feedback loop where deformation increases
psi, which drives the increase inphi, leading to material degradation.
- The updated
Notes:
-
Regularization Parameter
ell: Controls the width of the diffuse crack. Smallerellleads to sharper cracks but may require finer grids. -
Stabilization Term
beta: A small positive constant added to avoid numerical issues whenphiis near 1. -
Elastic Energy Density Approximation: For simplicity, we approximate
psibased on the deformation gradientF. In practice, you may need to computepsimore accurately. -
Boundary Conditions for
phi: The Laplacian operator requires proper boundary conditions. Here, we use zero Neumann boundary conditions implicitly by not updatingphibeyond the domain.
Visualization:
- The particles are displayed using the GUI. You can enhance the visualization by coloring particles based on the local
phivalue to see the fracture patterns.
Important Considerations:
-
Stability: The time step
dtmay need to be adjusted to ensure stability, especially when coupling with the phase-field evolution. -
Performance: Solving
phiwith CG each time step can be computationally intensive. You may optimize by using multi-grid methods or adaptive time stepping. -
Refinement: The code provided is a starting point. You may need to refine the computations, especially for
psi, and ensure that units and constants are correctly set for your specific material and simulation scale.
Feel free to adjust the parameters and enhance the model based on your needs.
-