To implement the matrix-vector product (mvp) function for heat diffusion using Moving Least Squares (MLS) shape functions in a Material Point Method (MPM) simulation to be solved using a Conjugate Gradient (CG) solver, you can follow these steps:
Define Grid Fields:
Create grid fields to store intermediate values, such as grid_v (grid representation of the vector v) and grid_mv (result of applying the heat diffusion operator to grid_v).
Initialize Grid Fields:
Zero out the grid_v and grid_mv fields at the beginning of the mvp function.
Particle-to-Grid (P2G) Transfer:
For each particle, compute its position relative to the grid and calculate the MLS shape function weights.
Accumulate the weighted contributions of v[p] onto the grid nodes grid_v.
Grid Computation (Compute Laplacian):
Compute the Laplacian of grid_v at each grid node to represent the application of the heat diffusion operator.
Use finite difference approximations for the Laplacian.
Grid-to-Particle (G2P) Transfer:
For each particle, interpolate the result from grid_mv back to the particles using the same MLS shape function weights.
Accumulate the weighted contributions onto mv[p].
Use Quadratic B-Spline Shape Functions:
Implement quadratic B-spline functions for the MLS shape functions, which are standard in MPM simulations.
Compute weights and their gradients as needed for accurate interpolation and differentiation.
Here's the complete implementation of the mvp function:
python
import taichi as ti
ti.init()
# Define dimensions and grid parameters
dim = 2 # Dimension (2D or 3D)
dx = 1.0 # Grid spacing
inv_dx = 1.0 / dx
grid_size = 128 # Number of grid nodes in each dimension
grid_s = ti.Vector.field(dim, dtype=ti.i32, shape=())
grid_s[None] = ti.Vector([grid_size] * dim)
# Define e unit vectors for finite differences
e = [ti.Vector.unit(dim, i) for i in range(dim)]
# Particle data
num_particles = 1000
x = ti.Vector.field(dim, dtype=ti.f32, shape=num_particles) # Positions
v = ti.field(dtype=ti.f32, shape=num_particles) # Input vector
mv = ti.field(dtype=ti.f32, shape=num_particles) # Output vector
# Grid fields
grid_v = ti.field(dtype=ti.f32, shape=(grid_size,) * dim)
grid_mv = ti.field(dtype=ti.f32, shape=(grid_size,) * dim)
@ti.kernel
def mvp(v: ti.template(), mv: ti.template()):
# Zero grid_v and grid_mv
for I in ti.grouped(grid_v):
grid_v[I] = 0.0
grid_mv[I] = 0.0
# Particle-to-Grid (P2G)
for p in x:
Xp = x[p] * inv_dx
base = Xp.cast(int) - 1 # Adjust base index for quadratic B-spline
fx = Xp - base.cast(float)
# Quadratic B-spline weights
w = [ [0.0, 0.0, 0.0] for _ in range(dim) ]
for d in ti.static(range(dim)):
fx_d = fx[d]
w[d][0] = 0.5 * (1.5 - fx_d)**2
w[d][1] = 0.75 - (fx_d - 1.0)**2
w[d][2] = 0.5 * (fx_d - 0.5)**2
# Accumulate onto grid_v
for offset in ti.grouped(ti.ndrange(*((3,) * dim))):
weight = 1.0
for d in ti.static(range(dim)):
weight *= w[d][offset[d]]
node = base + offset
grid_v[node] += weight * v[p]
# Grid computation: compute Laplacian of grid_v
for I in ti.grouped(grid_v):
laplacian = 0.0
for d in ti.static(range(dim)):
ip = I + e[d]
im = I - e[d]
if 0 <= ip[d] < grid_size and 0 <= im[d] < grid_size:
laplacian += (grid_v[ip] - 2 * grid_v[I] + grid_v[im]) * inv_dx**2
grid_mv[I] = laplacian
# Grid-to-Particle (G2P)
for p in x:
mv[p] = 0.0 # Reset mv[p] before accumulation
for p in x:
Xp = x[p] * inv_dx
base = Xp.cast(int) - 1
fx = Xp - base.cast(float)
# Quadratic B-spline weights
w = [ [0.0, 0.0, 0.0] for _ in range(dim) ]
for d in ti.static(range(dim)):
fx_d = fx[d]
w[d][0] = 0.5 * (1.5 - fx_d)**2
w[d][1] = 0.75 - (fx_d - 1.0)**2
w[d][2] = 0.5 * (fx_d - 0.5)**2
# Interpolate grid_mv back to particles
for offset in ti.grouped(ti.ndrange(*((3,) * dim))):
weight = 1.0
for d in ti.static(range(dim)):
weight *= w[d][offset[d]]
node = base + offset
mv[p] += weight * grid_mv[node]
Explanation:
Particle-to-Grid Transfer:
We iterate over each particle and compute its relative position fx within the grid cell.
The quadratic B-spline weights w are computed for each dimension.
We then accumulate the weighted v[p] onto the surrounding grid nodes grid_v[node].
Grid Computation:
The Laplacian operator is applied to grid_v to compute grid_mv.
We use central differences to approximate the Laplacian, taking care of boundary conditions.
Grid-to-Particle Transfer:
We reset mv[p] to zero before the accumulation.
For each particle, we compute the same weights w and interpolate grid_mv back to mv[p].
Notes:
Quadratic B-Spline Weights:
The quadratic B-spline functions ensure smooth interpolation and are commonly used in MPM simulations.
The weights are computed based on the particle's relative position within the grid.
Boundary Conditions:
In the grid computation, we check if the neighboring nodes ip and im are within the grid bounds to avoid accessing out-of-bounds memory.
Dimensionality:
The implementation is dimension-agnostic; you can set dim = 2 for 2D or dim = 3 for 3D simulations.
Usage in CG Solver:
The mvp function computes the matrix-vector product mv = K * v without explicitly forming the matrix K.
This function can be used within a CG solver loop to solve the heat diffusion equation in the MPM simulation.
Initialization of Fields:
Before running the mvp function, you need to initialize the particle positions x[p], the input vector v[p], and set up the grid fields appropriately based on your simulation domain and parameters.