I have an algorithm (second derivative) to solve poisson equaition. Why the commented algorithm don't work? I have tried very fine grid and small dt and still not work, so there must be something wrong with it
The commented algorithm seems to be a discretization of the Poisson equation using a second-order finite difference method, but there appears to be an inconsistency in the coefficients applied to the terms involving the grid values. Let's compare the working and commented algorithms to understand what might be wrong.
The working algorithm implements a standard central difference approximation for the second derivatives in both x and y directions:
This scheme is based on a standard 5-point finite difference stencil, which is commonly used for discretizing the Laplacian operator in a two-dimensional domain. The scheme assumes that temp_p represents the previous time or iteration value of the solution variable p. The term b[i][j] seems to represent the source term in the Poisson equation.
This algorithm seems to be attempting a higher-order approximation by including more points beyond the immediate neighbors. However, there are several potential issues:
The coefficients applied to temp_p[i + 2][j], temp_p[i - 2][j], temp_p[i][j + 2], and temp_p[i][j - 2] are not consistent with standard finite difference schemes for the second derivative. Typically, for a higher-order approximation like an 9-point stencil, you would expect coefficients that satisfy the symmetry and weighting of the points around the center. The coefficients used here do not match any standard higher-order finite difference scheme that I am aware of.
The coefficient for the source term -12.0 * b[i][j] * (dx * dx * dy * dy) seems arbitrary and does not match the discretization used for the derivatives. The correct coefficient should be consistent with the discretization of the Laplacian operator.
The denominator 30.0 * (dx * dx + dy * dy) does not correspond to any standard discretization normalization factor for the Poisson equation.
In conclusion, the commented algorithm does not work because the coefficients used for the finite difference approximations are incorrect. To fix it, you would need to correct the coefficients to match a known higher-order finite difference scheme or revert to the simpler and correct 5-point stencil used in the working algorithm. If you intended to use a higher-order scheme, you would need to carefully derive the coefficients to ensure they accurately approximate the second derivatives.