Skip to content

Adaptive timestep

Motivation

When engaging in numerical integration, the selection of the timestep relies on both the parameters of the problem and the chosen numerical scheme for discretised equations. An incorrect choice may lead to numerical instabilities, potentially resulting in a non-physical solution and program crashes. In computational fluid dynamics (CFD), a preventive measure against such issues is the use of the Courant-Friedrichs-Lewy (CFL) criterion. This criterion ensures that the numerical speed of propagation remains smaller than the one characterizing the physical problem, preventing information loss in each timestep.

One approach to maintaining numerical stability is to preset a timestep that is known a priori (although not always feasible) to be sufficiently small, consistently meeting the CFL criterion and encompassing all timescales during the simulation. However, this method presents two challenges. Firstly, a timestep analysis must be conducted for each new simulated case. Secondly, there is a risk of over-resolving the temporal aspect of the problem, leading to unnecessary computations and increased CPU time for the program. To address this, many CFD codes employ an adaptive timestep procedure, dynamically adjusting the timestep during runtime based on the smallest timescales that require resolution for stability.

Implementation

In this project, the use of adaptive timestepping is optional and determined in the case.txt file as a boolean variable. If it is used, a small timestep () overwrites that specified in the case.txt file. The criterion used in this project is:

where and are the maximum absolute velocity and absolute acceleration over all particles respectively, which are evaluated in the functions SphSolver::updatePosition(), SphSolver::boundaries() and SphSolver::velocityIntegration. The factors and are adjustable coefficients and may vary for different applications and fluids. Herein, in the default cases we make a choice of a strict criterion ( and , smallest reported values here) to ensure that the code can operate in a variety of applications. These parameters can also be changed in the case.txt file. The timestep is updated at the end of every time iteration by calling the following function:

void SphSolver::adaptiveTimestep(Fluid &data) {
  double h = data.getRadInfl();

  if (maxVelocity == 0.0 || maxAcceleration == 0.0) {
    // Set the timestep to the default value to avoid division by zero
    dt = (maxVelocity == 0.0 && maxAcceleration == 0.0) ? 1e-4
         : (maxVelocity == 0.0) ? coeffCfl2 * pow(h / maxAcceleration, 0.5)
                                : coeffCfl1 * h / maxVelocity;

    // Terminate the integration process because the particles will not move any
    // further
    termination_flag = (maxVelocity == 0.0 && maxAcceleration == 0.0);
  } else {
    // Update the timestep based on the CFL number
    dt = std::min(coeffCfl1 * h / maxVelocity,
                  coeffCfl2 * pow(h / maxAcceleration, 0.5));
  }

}

From the code snippet above, it can be seen that in the case where there is no velocity or acceleration of particles, the simulation will stop because the system will have definitely reached an equilibrium state and therefore no further calculations are needed. There is also the case where one of these quantities is zero and this means that a singularity can appear. To avoid such an incidence, when one of the velocity or acceleration is zero, only the criterion which makes use of the non-zero value is used.

Efficiency and further considerations

With the default factors and , the adaptive timestep approach required 15994 iterations to simulate a falling droplet of 800 particles and radius equal to 0.1, in opposition to the constant timestep approach which required 20000 iterations. A selection of and produced the result in only 8188 iterations.

adaptive_timstep_comparison
Effect of adaptive timestep.

Regarding the constant timestep, small overshoots of the energy plots are present. These are smoothed out in the adaptive timestep cases, despite the fact that the program required a smaller amount of iterations to be executed. Regarding the adaptive timestep cases, the results are similar for both simulations which means that for this case a more relaxed than the default criterion is applicable. Although the adaptive timestep introduces an extra layer of robustness (stability of cases with high number of particles in a very tight formation is not guaranteed) and potentially increased efficiency, different fluids and cases require an adjustment of the CFL coefficients in order to exploit the advantages of the approach and, therefore, the user should be careful in their selection.