My Project
programmer's 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);
}
f_id
void const int * f_id
Definition: cs_gui.h:292
cs_get_glob_time_step
cs_time_step_t * cs_get_glob_time_step(void)
Provide acces to cs_glob_time_step.
Definition: cs_time_step.c:401
cs_real_t
double cs_real_t
Floating-point value.
Definition: cs_defs.h:302
cs_field_t::type
int type
Definition: cs_field.h:129
cs_solving_info_t::l2residual
double l2residual
Definition: cs_parameters.h:105
cs_field_get_key_struct
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.c:3386
cs_time_step_t
time step descriptor
Definition: cs_time_step.h:51
ts
void cs_int_t cs_int_t cs_int_t cs_real_t * ts
Definition: cs_at_plugin.h:63
cs_field_by_id
cs_field_t * cs_field_by_id(int id)
Return a pointer to a field based on its id.
Definition: cs_field.c:2307
cs_field_key_id
int cs_field_key_id(const char *name)
Return an id associated with a given key name.
Definition: cs_field.c:2490
cs_solving_info_t
Definition: cs_parameters.h:100
CS_FIELD_VARIABLE
#define CS_FIELD_VARIABLE
Definition: cs_field.h:63
cs_field_t
Field descriptor.
Definition: cs_field.h:124
cs_field_n_fields
int cs_field_n_fields(void)
Return the number of defined fields.
Definition: cs_field.c:1527
bft_printf
int bft_printf(const char *const format,...)
Replacement for printf() with modifiable behavior.
Definition: bft_printf.c:140