Sure, to satisfy your request, we can implement our own Conjugate Gradient (CG) solver and use it to solve the phase-field fracture equation in the program. Below is the modified code with a self-implemented CG solver.
import taichi as ti
import numpy as np
from taichi.linalg import LinearOperator # We still need LinearOperator for our own operator
ti.init(arch=ti.cpu, debug=False)
# 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))
kappa = 2 * mu # Bulk modulus approximation for plane stress condition
Gc = 1.0 # Critical energy release rate
tau = 1.0 # Characteristic time
# Fields
x = ti.Vector.field(2, dtype=float, shape=n_particles)
v = ti.Vector.field(2, dtype=float, shape=n_particles)
C = ti.Matrix.field(2, 2, dtype=float, shape=n_particles)
F = ti.Matrix.field(2, 2, dtype=float, shape=n_particles)
phi = ti.field(dtype=float, shape=n_particles) # Phase field variable
color = ti.Vector.field(3, dtype=float, shape=n_particles)
grid_v = ti.Vector.field(2, dtype=float, shape=(n_grid, n_grid))
grid_m = ti.field(dtype=float, shape=(n_grid, n_grid))
# Phase field grids
grid_phi = ti.field(dtype=float, shape=(n_grid, n_grid))
grid_phi_new = ti.field(dtype=float, shape=(n_grid, n_grid))
grid_phi_m = ti.field(dtype=float, shape=(n_grid, n_grid))
grid_psi = ti.field(dtype=float, shape=(n_grid, n_grid))
grid_psi_m = ti.field(dtype=float, shape=(n_grid, n_grid))
@ti.func
def degradation(phi):
return (1 - phi)**2 # Quadratic degradation
@ti.func
def compute_strain_energy(Fp, phi_p):
J = Fp.determinant()
JaF = 1 / ti.sqrt(J) * Fp
psi_dev = mu / 2 * ((JaF.transpose() @ JaF).trace() - 2)
psi_vol = kappa / 2 * ((J**2 - 1) / 2 - ti.log(J))
g_phi = degradation(phi_p)
psi = g_phi * (psi_dev + psi_vol)
return psi
@ti.func
def compute_stress(Fp, phi_p):
B = Fp @ Fp.transpose()
devB = B - ti.Matrix.identity(float, 2) * (B.trace() / 2)
J = Fp.determinant()
tau_dev = mu / J * devB
prime = kappa / 2.0 * (J - 1.0 / J)
tau_vol = J * prime * ti.Matrix.identity(float, 2)
g_phi = degradation(phi_p)
stress = g_phi * (tau_dev + tau_vol)
return stress
@ti.kernel
def reset_grid():
for I in ti.grouped(grid_m):
grid_v[I] = [0, 0]
grid_m[I] = 0
grid_phi[I] = 0
grid_phi_m[I] = 0
grid_psi[I] = 0
grid_psi_m[I] = 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], ti.f32)
J = F[p].determinant()
F[p] = ti.Matrix.identity(float, 2) * ti.sqrt(J)
stress = compute_stress(F[p], phi[p])
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 p2g_phi():
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]
phi_p = phi[p]
for i, j in ti.static(ti.ndrange(3, 3)):
offset = ti.Vector([i, j])
grid_idx = base + offset
weight = w[i].x * w[j].y
grid_phi[grid_idx] += weight * phi_p
grid_phi_m[grid_idx] += weight
@ti.kernel
def normalize_grid_phi():
for I in ti.grouped(grid_phi):
if grid_phi_m[I] > 0:
grid_phi[I] /= grid_phi_m[I]
else:
grid_phi[I] = 1.0
@ti.kernel
def p2g_psi():
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]
psi_p = compute_strain_energy(F[p], phi[p])
for i, j in ti.static(ti.ndrange(3, 3)):
offset = ti.Vector([i, j])
grid_idx = base + offset
weight = w[i].x * w[j].y
grid_psi[grid_idx] += weight * psi_p
grid_psi_m[grid_idx] += weight
@ti.kernel
def normalize_grid_psi():
for I in ti.grouped(grid_psi):
if grid_psi_m[I] > 0:
grid_psi[I] /= grid_psi_m[I]
else:
grid_psi[I] = 0.0
@ti.kernel
def grid_op():
for I in ti.grouped(grid_m):
if grid_m[I] > 0:
grid_v[I] /= grid_m[I]
grid_v[I].y -= dt * gravity
if I[0] < bound and grid_v[I].x < 0:
grid_v[I].x = 0
if I[0] > n_grid - bound and grid_v[I].x > 0:
grid_v[I].x = 0
if I[1] < bound and grid_v[I].y < 0:
grid_v[I].y = 0
if I[1] > n_grid - bound and grid_v[I].y > 0:
grid_v[I].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, 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
@ti.kernel
def g2p_phi():
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]
phi_new = 0.0
for i, j in ti.static(ti.ndrange(3, 3)):
offset = ti.Vector([i, j])
grid_idx = base + offset
weight = w[i].x * w[j].y
phi_new += weight * grid_phi[grid_idx]
phi[p] = phi_new
@ti.kernel
def update_particle_color():
for p in x:
phi_p = phi[p]
color[p] = ti.Vector([phi_p, 0.0, 1.0 - phi_p]) # Blue to red transition
@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]])
phi[i] = 1.0 # Intact material
# Initialize the particles
init()
n = n_grid
# Fields for CG solver
x_cg = ti.field(dtype=ti.f32, shape=(n, n))
b_cg = ti.field(dtype=ti.f32, shape=(n, n))
r_cg = ti.field(dtype=ti.f32, shape=(n, n))
p_cg = ti.field(dtype=ti.f32, shape=(n, n))
Ap_cg = ti.field(dtype=ti.f32, shape=(n, n))
sum_cg = ti.field(dtype=ti.f32, shape=())
# Phase field operator
class PhaseFieldOperator:
def __init__(self, n):
self.n = n # Grid dimension
@ti.kernel
def matvec(self, y: ti.template(), x: ti.template()):
for i, j in ti.ndrange(self.n, self.n):
laplacian = (
-4 * x[i, j]
+ x[min(i + 1, self.n - 1), j]
+ x[max(i - 1, 0), j]
+ x[i, min(j + 1, self.n - 1)]
+ x[i, max(j - 1, 0)]
) / dx**2
psi = grid_psi[i, j]
g_derivative = -2 * (1 - x[i, j])
derivative = g_derivative * psi
y[i, j] = x[i, j] - dt / tau * (Gc * laplacian - derivative)
A = PhaseFieldOperator(n)
@ti.kernel
def initialize_b():
for I in ti.grouped(grid_phi):
b_cg[I] = grid_phi[I]
@ti.kernel
def initialize_x():
for I in ti.grouped(grid_phi):
x_cg[I] = grid_phi[I]
@ti.kernel
def copy_x_to_grid_phi():
for I in ti.grouped(grid_phi):
grid_phi[I] = x_cg[I]
@ti.kernel
def dot_product(a: ti.template(), b: ti.template()):
sum_cg[None] = 0.0
for I in ti.grouped(a):
sum_cg[None] += a[I] * b[I]
@ti.kernel
def update_x(x: ti.template(), alpha: ti.f32, p: ti.template()):
for I in ti.grouped(x):
x[I] += alpha * p[I]
@ti.kernel
def update_r(r: ti.template(), alpha: ti.f32, Ap: ti.template()):
for I in ti.grouped(r):
r[I] -= alpha * Ap[I]
@ti.kernel
def update_p(p: ti.template(), r: ti.template(), beta: ti.f32):
for I in ti.grouped(p):
p[I] = r[I] + beta * p[I]
def cg_solve(A, x, b, tol=1e-6, max_iter=1000):
A.matvec(Ap_cg, x)
@ti.kernel
def init_r_p():
for I in ti.grouped(b):
r_cg[I] = b[I] - Ap_cg[I]
p_cg[I] = r_cg[I]
init_r_p()
dot_product(r_cg, r_cg)
rsold = sum_cg[None]
for iter in range(max_iter):
A.matvec(Ap_cg, p_cg)
dot_product(p_cg, Ap_cg)
pAp = sum_cg[None]
if pAp == 0:
print("CG: Breakdown due to pAp == 0")
break
alpha = rsold / pAp
update_x(x, alpha, p_cg)
update_r(r_cg, alpha, Ap_cg)
dot_product(r_cg, r_cg)
rsnew = sum_cg[None]
if rsnew < tol**2:
print(f'CG converged at iteration {iter}')
break
beta = rsnew / rsold
update_p(p_cg, r_cg, beta)
rsold = rsnew
def solve_phase_field():
# Transfer phi and psi to grid
p2g_phi()
normalize_grid_phi()
p2g_psi()
normalize_grid_psi()
# Initialize b_cg and x_cg
initialize_b()
initialize_x()
# Perform the CG solve
cg_solve(A, x_cg, b_cg, tol=1e-6, max_iter=1000)
# Copy the result back to grid_phi
copy_x_to_grid_phi()
# Main GUI loop
gui = ti.GUI("MPM88 with Phase Field Fracture", res=(512, 512))
while gui.running:
for s in range(2):
reset_grid()
p2g()
grid_op()
g2p()
# Phase field update
solve_phase_field()
g2p_phi()
# Update color based on phi
update_particle_color()
gui.clear(0xFFFFFF)
gui.circles(x.to_numpy(), radius=1.5, color=color.to_numpy())
gui.show()
In this code:
- We've replaced the usage of
make_cg with a self-implemented CG solver cg_solve.
- Implemented CG functions
dot_product, update_x, update_r, and update_p as Taichi kernels.
- Adjusted the
PhaseFieldOperator to accept Taichi fields and modified the matvec method accordingly.
- Defined missing parameters
Gc and tau.
- Used Taichi fields throughout the CG solver to avoid unnecessary data transfers between CPU and GPU.
Feel free to adjust the parameters and further refine the code to fit your specific requirements or improve performance.