The Laplace equation is given by
The equation of conservation of mass is also called the continuity equation and it is given by
If density does not change with time, we have
Now we can define a potential function
(warning: it is listed as 'poisson_*.f90' but it actually solves the Laplace equation)
The method of finite differences essentially consists of "inflating" infinitesimal differentials into differences. For example, consider the definition of the first derivative of a function
A finite difference approximation of this derivative would therefore be
Now let's return to our problem of the Laplace equation, and use
Using finite differences,
we set
This can be represented in stencil notation
As an example, for a
If we deconstruct this grid, putting rows one after another,
we can write the Poisson equation applied at the cell at the center
So, we get a ``dot product'' notation.
The cells of the grid represent only the inner points of the whole computational domain.
That is why we enumerated them as
So, this means that we can continue with the same pattern that we used for the cell at the center, and we put the boundary cells in the right-hand side. When we have all equations for all cells, we can solve the system of equations for all the unknowns. We can write the system in matrix notation.
Now, we stack all the equations together in a matrix. For our
The matrix (that we will call
where
The equation
For a given cell
The continuation of this problem is in the documentation of the Github repository. Although this is the basis for solving the Laplace equation, and effective implementation needs to do the following additional steps, which are more technical in nature:
We will apply this implementation to solve two problems:
In the case of a uniform free flow, it is useful to discuss here the analytical solution in order to compare later.
If the fluid flows from left to right,
this means from the definition of
We get linear flow lines, which follow the gradient of f. In particular, if we set the integration constant
Self-gravity can be expressed as
This can be introduced into an astrophysics problem in several situations:
The easiest procedure in this case is to directly compute the gravitational force with the enclosed mass, that is
where
In an N-body problem, the gravitational force between two particles of masses
The total force on a particle
If the particles are very close to each other, then the denominator can become very small and the forces increase a lot. Then the accelerations increase and the time steps in the simulation increase (the simulation freezes). Imagine we are modeling a system where close encounters are considered rare (for example, stars in the collision of two galaxies). Then, we can introduce a softening parameter
The softening parameter is arbitrary and it adds to the free degrees of a system (which is not necessarily what one wants when researching the solution of a problem).
In Cartesian coordinates, the Poisson equation becomes
if we only consider
using finite differences like in the Laplace equation, and letting
Again, this equation can be written as a matrix equation with given boundary conditions, for example
The problem is how to invert this matrix. In our simple example for the Laplace equation, we make the inversion directly. However, this is very costly, and faster methods are available, for example:
Study case: self-gravity module for PLUTO. Method paper