I have developed a pseudospectral solver of the Navier-Stokes equations using FFTW. I tested my formulation of right hand sides (RHS) of the NS equations against standard trigonometric functions (sines, cosines and their combinations). For example, I set
density = sin 5x
x_velocity = 5cos 5y + 6sin7z
y_velocity = 4sin4y + cos x
z_velocity = 1
pressure = cos z
Supplying these values to the solver, it computed the RHS of the NS equations. I did the same by hand and compared the results with that obtained by the solver. Results were to good agreement. The maximum error between the exact answer and that computed by the solver was of the order of E-13
for a 128*128*128 grid.
Next I used a different function of the following form:
density = constant1+constant2*(tanh(x-constant3)-tanh(x-constant4))
x_velocity = 0
y_velocity = 0
z_velocity = 0
temperature = 1
pressure -> from ideal gas equation connecting density, temperature and pressure
The density was adjusted suitably based on the constants, to have a period of 2*pi
. On calculating the RHS of the x-momentum Navier Stokes based on these values given and comparing it with my answer (calculated by hand), I obtained a maximum error of the order of E-03
.
Further, using these values as initial values of the variables and moving forward in time by a Runge-Kutta 4 scheme, I get values of the density that seem to diverge very quickly. After about 30 time steps, I get NaNs.
Is there a specific reason why I notice a decrease in precision when non trigonometric periodic functions are used ?
Is 1. related to why my code seems to produce unstable results when marching forward in time ?
I wouldn't mind pasting the code here but it's pretty large.
I thought I would plot the initial density and its variation. But turns out I can't as I do not have enough reputation to do so.
The initial plot (@t = 0.0s) is a density plot that looks like a rectangular wave with the tanh functions used to smoothen the wave at the various corners.
At around t = 0.10s (the time step is 0.01s so, after 10 iterations), it develops spikes and becomes non-differentiable (still continuous).