8.3
general documentation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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);
}
int bft_printf(const char *const format,...)
Replacement for printf() with modifiable behavior.
Definition: bft_printf.cpp:140
double cs_real_t
Floating-point value.
Definition: cs_defs.h:342
int cs_field_n_fields(void)
Return the number of defined fields.
Definition: cs_field.cpp:1593
const void * cs_field_get_key_struct(const cs_field_t *f, const int key_id, void *s)
Return a structure for a given key associated with a field.
Definition: cs_field.cpp:3698
cs_field_t * cs_field_by_id(int id)
Return a pointer to a field based on its id.
Definition: cs_field.cpp:2465
int cs_field_key_id(const char *name)
Return an id associated with a given key name.
Definition: cs_field.cpp:2781
cs_time_step_t * cs_get_glob_time_step(void)
Provide read/write access to cs_glob_time_step.
Definition: cs_time_step.cpp:401
#define CS_FIELD_VARIABLE
Definition: cs_field.h:63
Field descriptor.
Definition: cs_field.h:131
int type
Definition: cs_field.h:136
Definition: cs_parameters.h:68
double l2residual
Definition: cs_parameters.h:74
time step descriptor
Definition: cs_time_step.h:64
int nt_cur
Definition: cs_time_step.h:74
int nt_max
Definition: cs_time_step.h:75