The issue you're encountering stems from how you're using grid values and particle values in your matrix-vector product (MVP) implementation. Specifically, you're using the grid variable v[base + offset] where you should be using interpolated particle values v_p.
Main Problems:
-
Using Grid Values Instead of Particle Values:
- In your computation of
gradc_scp[p], you're using v[base + offset], which is the value of v at the grid node. However, for accurate computation of quantities at the particle p, you should be using v_p, the value of v interpolated at the particle position.
- Similarly, in the assembly of
mv[base + offset], you're using v[base + offset] directly. This leads to incorrect assembly of the mass and stiffness matrices because you're mixing grid and particle variables improperly.
-
Incorrect Assembly of Mass Matrix Terms:
- In the mass matrix term
mv[base + offset] += p_vol * F[p].determinant() * weight * (1/dt) * v[base + offset], including v[base + offset] directly during assembly is not appropriate. The multiplication by v should occur after the assembly of the mass matrix, not during it.
How to Fix It:
-
Interpolate v_p at Particle Positions:
-
Before computing gradc_scp[p], interpolate v_p at the particle positions from the grid values. This ensures that you're using the correct value of v at each particle.
-
Here's how you can interpolate v_p:
@ti.kernel
def interpolate_v_to_particles(v: ti.template()):
for p in x:
v_p[p] = 0.0
Xp = x[p] / dx
base = (Xp - 0.5).cast(int)
fx = Xp - base
# Weight functions
w = [0.5 * (1.5 - fx)**2, 0.75 - (fx - 1)**2, 0.5 * (fx - 0.5)**2]
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
v_p[p] += weight * v[grid_idx]
-
Then, use v_p[p] in your computation of gradc_scp[p]:
gradc_scp[p] += p_vol * F[p].determinant() * grad_c * l0**2 * parabolic_M * 4 * v_p[p]
-
Assemble Mass and Stiffness Matrices Correctly:
-
In the assembly of mv, you should not multiply by v[base + offset] during the assembly. Instead, assemble the mass matrix coefficients separately, and perform the multiplication with v afterward.
-
Modify the mass matrix assembly as follows:
# Assemble mass matrix term without multiplying by v[grid]
mv[base + offset] += p_vol * F[p].determinant() * weight * (1/dt)
-
After assembling mv, perform the multiplication with v:
@ti.kernel
def apply_mass_matrix(mv: ti.template(), v: ti.template()):
for I in ti.grouped(mv):
mv[I] *= v[I]
-
Correct the Stiffness Matrix Term:
-
Ensure that in your stiffness matrix term, you're correctly computing the contributions:
mv[base + offset] += grad_c.dot(gradc_scp[p])
- Note that
gradc_scp[p] is now correctly computed using v_p.
-
Ensure Consistent Use of Variables:
- Make sure that all variables are consistently used as either particle variables or grid variables, and that they are correctly interpolated when necessary.
Revised mvp Function:
@ti.kernel
def mvp(v: ti.template(), mv: ti.template()):
# Interpolate v to particles
for p in x:
v_p[p] = 0.0
Xp = x[p] / dx
base = (Xp - 0.5).cast(int)
fx = Xp - base
# Weight functions
w = [0.5 * (1.5 - fx)**2, 0.75 - (fx - 1)**2, 0.5 * (fx - 0.5)**2]
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
v_p[p] += weight * v[grid_idx]
# Compute gradc_scp[p] using v_p[p]
for p in x:
gradc_scp[p] = ti.Vector([0.0, 0.0])
Xp = x[p] / dx
base = (Xp - 0.5).cast(int)
fx = Xp - base
# Weight functions and their derivatives
w = [0.5 * (1.5 - fx)**2, 0.75 - (fx - 1)**2, 0.5 * (fx - 0.5)**2]
dw = [fx - 1.5, -2 * (fx - 1), fx - 0.5]
for i, j in ti.static(ti.ndrange(3, 3)):
offset = ti.Vector([i, j])
grid_idx = base + offset
grad_c = ti.Vector([dw[i].x * w[j].y, w[i].x * dw[j].y])
gradc_scp[p] += p_vol * F[p].determinant() * grad_c * l0**2 * parabolic_M * 4 * v_p[p]
# Assemble mv without multiplying by v[grid] during assembly
for p in x:
Xp = x[p] / dx
base = (Xp - 0.5).cast(int)
fx = Xp - base
# Weight functions and their derivatives
w = [0.5 * (1.5 - fx)**2, 0.75 - (fx - 1)**2, 0.5 * (fx - 0.5)**2]
dw = [fx - 1.5, -2 * (fx - 1), fx - 0.5]
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
grad_c = ti.Vector([dw[i].x * w[j].y, w[i].x * dw[j].y])
# Mass matrix term
mv[grid_idx] += p_vol * F[p].determinant() * weight * (1/dt)
# Stiffness matrix term
mv[grid_idx] += grad_c.dot(gradc_scp[p])
# Multiply mv by v after assembly
for I in ti.grouped(mv):
mv[I] *= v[I]
Explanation:
- Interpolate
v_p[p]: This ensures that the value of v at each particle is accurately represented based on the surrounding grid nodes.
- Assemble Matrices Correctly: By separating the assembly of the mass matrix and stiffness matrix from the multiplication by
v, you ensure that the global matrices are correctly formed.
- Consistent Variable Usage: By using
v_p[p] consistently for particle values and avoiding direct use of v[base + offset] during assembly, you prevent the mixing of grid and particle variables.
Conclusion:
By making these changes, your simulation should evolve correctly according to the heat equation. The key is to ensure that you're properly interpolating values between particles and grid nodes and assembling your matrices without prematurely multiplying by v.