SPH approximation of the gradient of a quadratic function:
In this example we approximate the gradient of a quadratic polynomial using the SPH discretization with the cubic spline kernel [KBST19, KBST22]. The left plot shows the particle sampling pattern while the right plot shows the quadratic function, the exact gradient, the SPH gradient approximation, and the error. To compute the SPH approximation the function values are sampled along the red line in the left plot. That means that for each point on the line the neighbors are determined and the SPH difference formula to approximate the gradient is applied [KBST19]:
$$\langle \nabla f(x,y) \rangle = \sum_j \frac{m_j}{\rho_j} (f(x_j,y_j)  f(x,y)) \nabla W_{ij}$$
Quadratic_function:
$$f(x,y) = x^2 + \frac14 y^2  1$$
Gradient:
$$\nabla f(x,y) = 2x + \frac12 y$$
Kernel gradient correction
The SPH gradient approximation of a fluid quantity is errorprone if the particle neighborhood is only partially filled. To consider this in the computation and to make it firstorder consistent, the kernel gradient can be corrected by a matrix $\mathbf{L}_i$:
$$\nabla \tilde {W}_{ij} = L_i \nabla W_{ij},$$
where
$$\mathbf{L}_i = \left ( \sum_j \frac{m_j}{\rho_j} \nabla W_{ij} \otimes (\mathbf{x}_j  \mathbf{x}_i) \right )^{1}.
$$
Note that $\otimes$ denotes the dyadic product of two vectors.
References
 [KBST19] Dan Koschier, Jan Bender, Barbara Solenthaler, Matthias Teschner. Smoothed Particle Hydrodynamics for PhysicallyBased Simulation of Fluids and Solids. Eurographics Tutorial, 2019
 [KBST22] Dan Koschier, Jan Bender, Barbara Solenthaler, Matthias Teschner. A Survey on SPH Methods in Computer Graphics. Computer Graphics Forum, 2022
