7.1
general documentation
Stopping criterion based on L2 time residuals

Stopping criterion based on L2 time residuals

This is an example of cs_user_extra_operations allowing to properly stop a computation when the L2 time residuals (displayed in the run_solver.log file) of all solved variables have decreased below a value of 1e-3.

L2 time residuals of a variable at a given time step are a relative measure of the unsteady term of its transport equation:

\[ \sqrt{\int_\Omega \left| \der{\varia}{t} \right|^2 \dd \Omega / \int_\Omega \left| \varia \right|^2 \dd \Omega} \]

/* get total number of fields */
int n_fields = cs_field_n_fields();
/* get time step structure */
/* declare a C structure holding solving information values */
bool cved = true;
/* criterion is set here to 1e-3 */
cs_real_t epsres = 1e-3;
/* loop over all fields */
for (int f_id = 0; f_id < n_fields; f_id++) {
/* filter fields of type variable (i.e. the solved ones) */
if (f->type & CS_FIELD_VARIABLE) {
/* get solving info structure for current field */
cs_field_get_key_struct(f, cs_field_key_id("solving_info"), &sinfo);
/* has the value of the L2 time residual decreased below criterion ? */
cved = cved && (sinfo.l2residual <= epsres);
}
}
/* if current iteration is the second to last one, nothing to do */
cved = cved && (ts->nt_cur < ts->nt_max);
/* otherwise if converged */
if (cved) {
/* set maximum number of iteration to the next one */
ts->nt_max = ts->nt_cur+1;
/* Warning: L2 time residual is computed after extra operations */
bft_printf("Converged at %d\n",ts->nt_max);
}