To implement the mvp function for heat diffusion using MLS shape functions in the context of MPM (Material Point Method), we'll follow these main steps:
Transfer particle scalar values to grid nodes (P2G): Interpolate the particle values to the grid nodes using the shape functions.
Compute the Laplacian at grid nodes: Use finite differences to compute the Laplacian (which represents the diffusion operator in the heat equation) at each grid node.
Transfer the Laplacian back to particles (G2P): Interpolate the computed Laplacian from the grid nodes back to the particles using the same shape functions.
Here's the complete implementation:
python
import taichi as ti
# Assuming 1D for simplicity; extend to higher dimensions as needed.
ti.init()
dim = 1
n_particles = 100
n_grid = 128
dx = 1 / n_grid
inv_dx = 1 / dx
dt = 1e-4
x = ti.field(ti.f32, shape=n_particles) # particle positions
v = ti.field(ti.f32, shape=n_particles) # particle values (e.g., temperature)
mv = ti.field(ti.f32, shape=n_particles) # result of mvp (K*v)
grid_v = ti.field(ti.f32, shape=n_grid) # grid node values
grid_Laplacian_v = ti.field(ti.f32, shape=n_grid) # Laplacian at grid nodes
@ti.kernel
def mvp(v: ti.template(), mv: ti.template()):
# Zero grid values
for I in grid_v:
grid_v[I] = 0.0
grid_Laplacian_v[I] = 0.0
# Particle to Grid (P2G)
for p in x:
Xp = x[p] * inv_dx # particle position in grid units
base = int(Xp - 0.5)
fx = Xp - base
# Quadratic B-spline weights
w = [0.5 * (1.5 - fx)**2, 0.75 - (fx - 1)**2, 0.5 * (fx - 0.5)**2]
for i in ti.static(range(3)):
grid_idx = base + i
if 0 <= grid_idx < n_grid:
weight = w[i]
grid_v[grid_idx] += weight * v[p]
# Compute Laplacian on Grid
for I in range(1, n_grid - 1):
# Central difference approximation of Laplacian
grid_Laplacian_v[I] = (grid_v[I - 1] - 2 * grid_v[I] + grid_v[I + 1]) * inv_dx**2
# Boundary conditions (e.g., Neumann conditions where derivative is zero)
# Adjust as per the problem requirements
grid_Laplacian_v[0] = grid_Laplacian_v[1]
grid_Laplacian_v[n_grid - 1] = grid_Laplacian_v[n_grid - 2]
# Grid to Particle (G2P)
for p in x:
Xp = x[p] * inv_dx
base = int(Xp - 0.5)
fx = Xp - base
# Quadratic B-spline weights
w = [0.5 * (1.5 - fx)**2, 0.75 - (fx - 1)**2, 0.5 * (fx - 0.5)**2]
mv_p = 0.0
for i in ti.static(range(3)):
grid_idx = base + i
if 0 <= grid_idx < n_grid:
weight = w[i]
mv_p += weight * grid_Laplacian_v[grid_idx]
mv[p] = mv_p
Explanation:
P2G Step:
We zero out the grid fields to ensure no residual data affects the current computation.
For each particle, we calculate its position relative to the grid (Xp) and determine the base node (base).
The B-spline weights (w) are calculated based on the relative position fx.
We loop over the neighboring grid nodes and accumulate the weighted particle values to the grid nodes.
Laplacian Computation:
We use a central difference to approximate the Laplacian at each grid node.
For boundary nodes, we use simple strategies (e.g., copying values from adjacent nodes) to handle boundary conditions appropriately.
G2P Step:
For each particle, we calculate its position relative to the grid and determine the same base node and weights as before.
We interpolate the Laplacian values from the grid nodes back to the particle using the weights.
The result is stored in mv[p], representing (K * v)_p for each particle p.
This implementation effectively computes the action of the stiffness matrix K on the vector v without forming K explicitly, leveraging the MPM framework and MLS shape functions. It is suitable for use in a Conjugate Gradient solver for heat diffusion problems.