@ti.kernel
def mvp(v: ti.template(), mv: ti.template()):
for p in x:
gradc_scp[p] = 0
# matrix-vector product
for p in x:
Xp = x[p] / dx
base = (Xp - 0.5).cast(int)
fx = Xp - base
code
# Weight functions
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] # Derivative of shape functions
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])
gradc_scp[p] += p_vol * F[p].determinant() *grad_c*l0**2*parabolic_M*4
# matrix-vector product
for p in x:
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]
dw = [fx - 1.5, -2 * (fx - 1), fx - 0.5] # Derivative of shape functions
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])
Fp = (4 * l0 * parabolic_M * psi_max[p] * 0.999 / G
parabolic_M + 1/dt)
# Original MVP term
mv[base + offset] += p_vol * F[p].determinant() * weight * (1/dt)* v[base + offset]
mv[base + offset] += gradc_scp[p].dot(grad_c)*v[base + offset]
Values don't diffuse as expected for heat equation, fix that
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
It looks like you're trying to implement the heat equation using the Material Point Method (MPM) in Taichi, but the diffusion isn't behaving as expected. Let's examine your code and see how we can fix it to correctly simulate the heat diffusion.
Understanding the Issue:
In the heat equation, the key term responsible for diffusion is the Laplacian operator (∇²u). When discretized using MPM or finite element methods, this term involves the gradient of shape functions and their interactions.
In your code, the computation of the matrix-vector product (MVP) seems to have some issues in how the diffusion term is assembled. Specifically, the way gradc_scp[p] and grad_c are used in accumulating mv may not correctly represent the Laplacian operator.
Key Points to Address:
Calculation of Gradient Terms:
Ensure that the gradient of the shape functions (grad_c) and any related terms are computed correctly.
The accumulation of these terms should correctly represent the diffusion effect in the discrete equations.
Assembly of the Diffusion Term:
The diffusion term in the heat equation corresponds to a stiffness matrix in finite element methods.
The matrix entries involve the inner product of the gradients of shape functions.
Ensure that the assembly of this term into mv accurately reflects this.
Sign Convention:
The diffusion term involves the negative Laplacian (i.e., -∇²u), which affects the sign in the equations.
Verify that the signs in your code correctly represent the physics of diffusion.
Proposed Corrections:
Let's go through your code and make the necessary adjustments.
First Loop: Initialize gradc_scp
python
for p in x:
gradc_scp[p] = ti.Vector.zero(ti.f32, 2) # Ensure it's a zero vector
Second Loop: Compute gradc_scp[p]
This loop seems intended to compute some form of particle-based gradient. However, the way it's calculated might not be aligning with the diffusion term.
Correction:
Instead of accumulating gradc_scp[p] directly, consider computing the term that represents the interaction between particles and grid nodes for the diffusion term.
The diffusion term requires computing contributions involving grad(N_i) • grad(N_j), where N_i and N_j are shape functions.
Modify the second loop to compute the stiffness-like contributions:
python
for p in x:
Xp = x[p] / dx
base = (Xp - 0.5).cast(int)
fx = Xp - base
# Weight functions and their gradients
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] # Derivatives of shape functions
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_N_i = ti.Vector([dw[i].x * w[j].y, w[i].x * dw[j].y]) # Gradient of N_i
# Accumulate the stiffness matrix contributions
gradc_scp[p] += grad_N_i
Note that we're computing and storing the gradient of the shape functions at the particle locations.
Third Loop: Assemble the Matrix-Vector Product
In this loop, we'll assemble the diffusion term into mv.
Correction:
Use the gradients computed in the second loop correctly.
Ensure that the accumulation into mv represents the diffusion effect.
Pay attention to the sign of the diffusion term and any scaling factors.
Modify the third loop as follows:
python
for p in x:
Xp = x[p] / dx
base = (Xp - 0.5).cast(int)
fx = Xp - base
# Weight functions and their gradients
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] # Derivatives of shape functions
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_N_j = ti.Vector([dw[i].x * w[j].y, w[i].x * dw[j].y]) # Gradient of N_j
# Mass term (if needed)
mass_contribution = p_vol * F[p].determinant() * weight * (1 / dt) * v[grid_idx]
mv[grid_idx] += mass_contribution
# Diffusion term
stiffness_contribution = p_vol * F[p].determinant() * l0**2 * parabolic_M * grad_N_j.dot(gradc_scp[p]) * v[grid_idx]
mv[grid_idx] -= stiffness_contribution # Note the negative sign for diffusion
Explanation:
Mass Contribution: Represents the time derivative term in the heat equation.
Stiffness Contribution: Represents the diffusion term, assembled using the gradients of the shape functions.
Negative Sign: The diffusion term involves the negative Laplacian, so we subtract the stiffness contribution.
Scaling Factors: Ensure that constants like l0, parabolic_M, and any other physical parameters are correctly applied.
Additional Suggestions:
Check Physical Units: Make sure that all terms have consistent units to prevent scaling issues.
Boundary Conditions: Ensure that appropriate boundary conditions are applied, as they can significantly affect diffusion behavior.
Normalization: If necessary, normalize the shape functions or gradients to improve numerical stability.
Time Step (dt): Verify that the time step size is appropriate for the diffusion process.
Example Correction:
Here's the corrected mvp function with the above changes:
python
@ti.kernel
def mvp(v: ti.template(), mv: ti.template()):
for p in x:
gradc_scp[p] = ti.Vector.zero(ti.f32, 2) # Initialize gradient
for p in x:
Xp = x[p] / dx
base = (Xp - 0.5).cast(int)
fx = Xp - base
# Weight functions and their gradients
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_N_i = ti.Vector([dw[i].x * w[j].y, w[i].x * dw[j].y])
gradc_scp[p] += grad_N_i # Store gradient for particle p
for p in x:
Xp = x[p] / dx
base = (Xp - 0.5).cast(int)
fx = Xp - base
# Weight functions and their gradients
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_N_j = ti.Vector([dw[i].x * w[j].y, w[i].x * dw[j].y])
# Mass term
mv[grid_idx] += p_vol * F[p].determinant() * weight * (1 / dt) * v[grid_idx]
# Diffusion term
mv[grid_idx] -= p_vol * F[p].determinant() * l0**2 * parabolic_M * grad_N_j.dot(gradc_scp[p]) * v[grid_idx]
Notes:
The diffusion term now correctly represents the interaction between the gradients of the shape functions and includes the appropriate negative sign.
The mass term remains the same, ensuring temporal accuracy.
This should produce the expected diffusion behavior in your simulation.
Testing the Correction:
Visualize Results: After implementing the correction, visualize the field to check if diffusion occurs as expected.
Compare with Analytical Solution: If possible, compare your numerical results with an analytical solution of the heat equation for a similar setup.
Adjust Parameters: If diffusion is too slow or too fast, adjust parameters like dt, parabolic_M, or l0 accordingly.
Summary:
By correctly assembling the diffusion term in your matrix-vector product and ensuring that the sign convention and scaling factors are appropriate, your simulation should now exhibit the expected diffusion behavior consistent with the heat equation.
Let me know if you have any questions or need further assistance!