8.3
general documentation
cs_iter_algo.h
Go to the documentation of this file.
1#ifndef __CS_ITER_ALGO_H__
2#define __CS_ITER_ALGO_H__
3
4/*============================================================================
5 * Set of functions to manage high-level iterative algorithms
6 *============================================================================*/
7
8/*
9 This file is part of code_saturne, a general-purpose CFD tool.
10
11 Copyright (C) 1998-2024 EDF S.A.
12
13 This program is free software; you can redistribute it and/or modify it under
14 the terms of the GNU General Public License as published by the Free Software
15 Foundation; either version 2 of the License, or (at your option) any later
16 version.
17
18 This program is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21 details.
22
23 You should have received a copy of the GNU General Public License along with
24 this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
25 Street, Fifth Floor, Boston, MA 02110-1301, USA.
26*/
27
28/*----------------------------------------------------------------------------
29 * Local headers
30 *----------------------------------------------------------------------------*/
31
32#include "cs_cdo_blas.h"
33#include "cs_math.h"
34#include "cs_param_sles.h"
35#include "cs_param_types.h"
36#include "cs_sdm.h"
37#include "cs_sles.h"
38
39/*----------------------------------------------------------------------------*/
40
42
43/*============================================================================
44 * Macro definitions
45 *============================================================================*/
46
47/*============================================================================
48 * Type definitions
49 *============================================================================*/
50
51/* \enum cs_iter_algo_type_bit_t
52 * \brief Bit values for the definition of the type of iterative algorithms
53 *
54 * \var CS_ITER_ALGO_DEFAULT
55 * Iterative algorithms like linear solvers for saddle-point system, fixed
56 * point...
57 *
58 * \var CS_ITER_ALGO_ANDERSON
59 * Iterative algorithms relying on the Anderson acceleration
60 *
61 * \var CS_ITER_ALGO_TWO_LEVEL
62 * Iterative algorithms using an inner/outer iterative loops
63 */
64
65typedef enum {
66
67 CS_ITER_ALGO_DEFAULT = 1 << 0, /* = 1 */
68 CS_ITER_ALGO_ANDERSON = 1 << 1, /* = 2 */
69 CS_ITER_ALGO_TWO_LEVEL = 1 << 2, /* = 4 */
70
72
74
75/* Structure used as context for the iterative algorithm considered by
76 default */
77
78typedef struct {
79
117 double tol;
118
119 double prev_res;
120 double res;
121 double res0;
122
126
128
129/* Structures used when an Anderson acceleration is considered */
130/* ----------------------------------------------------------- */
131
138typedef struct {
139
161 double max_cond;
162 double beta;
164
166
173typedef struct {
174
181
219 double tol;
220
221 double prev_res;
222 double res;
223 double res0;
224
228
261 cs_lnum_t n_elts; /*<! Size of an array for one direction */
262 int n_dir;
263
268
270 cs_sdm_t *R;
271
273
275
276/* ++++++++++++++ */
277/* Main structure */
278/* ++++++++++++++ */
279
290typedef struct {
291
311
312 void *context;
313
315
316/*============================================================================
317 * Public function prototypes
318 *============================================================================*/
319
320/*----------------------------------------------------------------------------*/
328/*----------------------------------------------------------------------------*/
329
332
333/*----------------------------------------------------------------------------*/
344/*----------------------------------------------------------------------------*/
345
348 int verbosity,
349 cs_param_convergence_t cvg_param);
350
351/*----------------------------------------------------------------------------*/
357/*----------------------------------------------------------------------------*/
358
359void
361
362/*----------------------------------------------------------------------------*/
368/*----------------------------------------------------------------------------*/
369
370void
372
373/*----------------------------------------------------------------------------*/
380/*----------------------------------------------------------------------------*/
381
382void
384
385/*----------------------------------------------------------------------------*/
392/*----------------------------------------------------------------------------*/
393
394void
396 int verbosity);
397
398/*----------------------------------------------------------------------------*/
407/*----------------------------------------------------------------------------*/
408
409void
411 cs_param_convergence_t cvg_param);
412
413/*----------------------------------------------------------------------------*/
421/*----------------------------------------------------------------------------*/
422
423void
425 double tol);
426
427/*----------------------------------------------------------------------------*/
434/*----------------------------------------------------------------------------*/
435
436void
438 double value);
439
440/*----------------------------------------------------------------------------*/
448/*----------------------------------------------------------------------------*/
449
450void
452 double value);
453
454/*----------------------------------------------------------------------------*/
461/*----------------------------------------------------------------------------*/
462
463void
465 cs_sles_convergence_state_t cvg_status);
466
467/*----------------------------------------------------------------------------*/
475/*----------------------------------------------------------------------------*/
476
477void
479 cs_iter_algo_param_aac_t aac_param,
480 cs_lnum_t n_elts);
481
482/*----------------------------------------------------------------------------*/
490/*----------------------------------------------------------------------------*/
491
492int
494
495/*----------------------------------------------------------------------------*/
503/*----------------------------------------------------------------------------*/
504
505int
507
508/*----------------------------------------------------------------------------*/
516/*----------------------------------------------------------------------------*/
517
518double
520
521/*----------------------------------------------------------------------------*/
529/*----------------------------------------------------------------------------*/
530
533
534/*----------------------------------------------------------------------------*/
540/*----------------------------------------------------------------------------*/
541
542double
544
545/*----------------------------------------------------------------------------*/
553/*----------------------------------------------------------------------------*/
554
557
558/*----------------------------------------------------------------------------*/
568/*----------------------------------------------------------------------------*/
569
570void
572 cs_real_t *cur_iterate,
573 const cs_real_t *pre_iterate,
574 cs_cdo_blas_dotprod_t *dotprod,
576
577/*----------------------------------------------------------------------------*/
584/*----------------------------------------------------------------------------*/
585
586void
588 int n_last_inner_iter);
589
590/*----------------------------------------------------------------------------*/
597/*----------------------------------------------------------------------------*/
598
599void
601 double res);
602
603/*----------------------------------------------------------------------------*/
614/*----------------------------------------------------------------------------*/
615
618 double tol);
619
620/*----------------------------------------------------------------------------*/
631/*----------------------------------------------------------------------------*/
632
635
636/*----------------------------------------------------------------------------*/
643/*----------------------------------------------------------------------------*/
644
645void
647 const char *label);
648
649/*----------------------------------------------------------------------------*/
659/*----------------------------------------------------------------------------*/
660
661void
662cs_iter_algo_check_warning(const char *func_name,
663 const char *eq_name,
664 const char *algo_name,
666
667/*----------------------------------------------------------------------------*/
668
670
671#endif /* __CS_ITER_ALGO_H__ */
cs_real_t() cs_cdo_blas_square_norm_t(const cs_real_t *array)
Generic function pointer for computing a square norm. Parallel synchronization is performed inside th...
Definition: cs_cdo_blas.h:79
cs_real_t() cs_cdo_blas_dotprod_t(const cs_real_t *a, const cs_real_t *b)
Generic function pointer for computing a dot product. Parallel synchronization is performed inside th...
Definition: cs_cdo_blas.h:64
#define BEGIN_C_DECLS
Definition: cs_defs.h:542
double cs_real_t
Floating-point value.
Definition: cs_defs.h:342
#define END_C_DECLS
Definition: cs_defs.h:543
int cs_lnum_t
local mesh entity id
Definition: cs_defs.h:335
unsigned short int cs_flag_t
Definition: cs_defs.h:344
void cs_iter_algo_reset(cs_iter_algo_t *algo)
Reset a cs_iter_algo_t structure.
Definition: cs_iter_algo.cpp:547
void cs_iter_algo_check_warning(const char *func_name, const char *eq_name, const char *algo_name, cs_iter_algo_t *algo)
Check if something wrong happens during the iterative process after one new iteration.
Definition: cs_iter_algo.cpp:1491
void cs_iter_algo_set_tolerance(cs_iter_algo_t *algo, double tol)
Set the final tolerance used to check the convergence of the algorithm This tolerance should take int...
Definition: cs_iter_algo.cpp:646
void cs_iter_algo_log_cvg(cs_iter_algo_t *algo, const char *label)
Log the convergence of the iterative algorithm.
Definition: cs_iter_algo.cpp:1402
void cs_iter_algo_release_anderson_arrays(cs_iter_algo_aac_t *c)
Free the members (arrays and matrix) associated to the context structure of an Anderson acceleration.
Definition: cs_iter_algo.cpp:577
cs_iter_algo_param_aac_t cs_iter_algo_get_anderson_param(cs_iter_algo_t *algo)
Retrieve the set of parameters for an Anderson algorithm.
Definition: cs_iter_algo.cpp:972
void cs_iter_algo_set_initial_residual(cs_iter_algo_t *algo, double value)
Set the initial residual used to detect a divergence.
Definition: cs_iter_algo.cpp:677
cs_sles_convergence_state_t cs_iter_algo_get_cvg_status(const cs_iter_algo_t *algo)
Retrieve the last convergence state.
Definition: cs_iter_algo.cpp:908
cs_iter_algo_type_bit_t
Definition: cs_iter_algo.h:65
@ CS_ITER_ALGO_DEFAULT
Definition: cs_iter_algo.h:67
@ CS_ITER_ALGO_ANDERSON
Definition: cs_iter_algo.h:68
@ CS_ITER_ALGO_TWO_LEVEL
Definition: cs_iter_algo.h:69
cs_iter_algo_t * cs_iter_algo_create(cs_iter_algo_type_t type)
Create and initialize by default a new cs_iter_algo_t structure.
Definition: cs_iter_algo.cpp:404
cs_sles_convergence_state_t cs_iter_algo_update_cvg_tol_auto(cs_iter_algo_t *algo)
Update the convergence status and the number of iterations. The tolerance threshold is automatically ...
Definition: cs_iter_algo.cpp:1381
cs_iter_algo_t * cs_iter_algo_create_with_settings(cs_iter_algo_type_t type, int verbosity, cs_param_convergence_t cvg_param)
Create a new cs_iter_algo_t structure with the given settings.
Definition: cs_iter_algo.cpp:483
void cs_iter_algo_set_cvg_status(cs_iter_algo_t *algo, cs_sles_convergence_state_t cvg_status)
Set the convergence status of the given structure.
Definition: cs_iter_algo.cpp:740
int cs_iter_algo_get_n_iter(const cs_iter_algo_t *algo)
Retrieve the current number of iterations done.
Definition: cs_iter_algo.cpp:805
void cs_iter_algo_set_verbosity(cs_iter_algo_t *algo, int verbosity)
Define the verbosity of the given iterative algorithm.
Definition: cs_iter_algo.cpp:602
void cs_iter_algo_update_inner_iters(cs_iter_algo_t *algo, int n_last_inner_iter)
Update the counting of inner iterations in two-level algorithms.
Definition: cs_iter_algo.cpp:1220
void cs_iter_algo_update_residual(cs_iter_algo_t *algo, double res)
Update the value of the residual and its associated members.
Definition: cs_iter_algo.cpp:1257
void cs_iter_algo_set_normalization(cs_iter_algo_t *algo, double value)
Set the normalization to apply when checking the convergence of the algorithm.
Definition: cs_iter_algo.cpp:709
void cs_iter_algo_free(cs_iter_algo_t **p_algo)
Free a cs_iter_algo_t structure.
Definition: cs_iter_algo.cpp:507
void cs_iter_algo_update_anderson(cs_iter_algo_t *algo, cs_real_t *cur_iterate, const cs_real_t *pre_iterate, cs_cdo_blas_dotprod_t *dotprod, cs_cdo_blas_square_norm_t *sqnorm)
Apply one more iteration of the Anderson acceleration.
Definition: cs_iter_algo.cpp:999
double cs_iter_algo_get_normalization(const cs_iter_algo_t *algo)
Get the normalization to apply when computing the tolerance threshold.
Definition: cs_iter_algo.cpp:939
double cs_iter_algo_get_residual(const cs_iter_algo_t *algo)
Retrieve the last computed residual.
Definition: cs_iter_algo.cpp:875
void cs_iter_algo_set_anderson_param(cs_iter_algo_t *algo, cs_iter_algo_param_aac_t aac_param, cs_lnum_t n_elts)
Create a new cs_iter_algo_aac_t structure for Anderson acceleration.
Definition: cs_iter_algo.cpp:772
cs_sles_convergence_state_t cs_iter_algo_update_cvg_tol_given(cs_iter_algo_t *algo, double tol)
Update the convergence status and the number of iterations. The tolerance threshold is given so that ...
Definition: cs_iter_algo.cpp:1305
void cs_iter_algo_set_cvg_param(cs_iter_algo_t *algo, cs_param_convergence_t cvg_param)
Define the criteria related to the convergence of the given iterative algorithm.
Definition: cs_iter_algo.cpp:623
cs_flag_t cs_iter_algo_type_t
Definition: cs_iter_algo.h:73
int cs_iter_algo_get_n_inner_iter(const cs_iter_algo_t *algo)
Retrieve the cumulated number of inner iterations done.
Definition: cs_iter_algo.cpp:838
Structure and routines handling the SLES ((Sparse Linear Equation Solver) settings stored inside a cs...
cs_param_dotprod_type_t
Definition: cs_param_types.h:884
cs_sles_convergence_state_t
Definition: cs_sles.h:56
char * label
Definition: keywords.h:58
char * algo
Definition: field_names.h:100
Context structure for the algorithm called Anderson acceleration.
Definition: cs_iter_algo.h:173
int n_inner_iter
Definition: cs_iter_algo.h:226
cs_real_t * gold
Definition: cs_iter_algo.h:266
double prev_res
Definition: cs_iter_algo.h:221
double tol
Definition: cs_iter_algo.h:219
double res0
Definition: cs_iter_algo.h:223
cs_real_t * dg
Definition: cs_iter_algo.h:267
cs_iter_algo_param_aac_t param
Definition: cs_iter_algo.h:180
cs_real_t * fold
Definition: cs_iter_algo.h:264
cs_real_t * Q
Definition: cs_iter_algo.h:269
cs_lnum_t n_elts
Definition: cs_iter_algo.h:261
cs_sles_convergence_state_t cvg_status
Definition: cs_iter_algo.h:217
cs_real_t * gamma
Definition: cs_iter_algo.h:272
int n_dir
Definition: cs_iter_algo.h:262
double res
Definition: cs_iter_algo.h:222
cs_real_t * df
Definition: cs_iter_algo.h:265
int last_inner_iter
Definition: cs_iter_algo.h:227
cs_sdm_t * R
Definition: cs_iter_algo.h:270
double normalization
Definition: cs_iter_algo.h:218
int n_algo_iter
Definition: cs_iter_algo.h:225
Definition: cs_iter_algo.h:78
int n_inner_iter
Definition: cs_iter_algo.h:124
double prev_res
Definition: cs_iter_algo.h:119
double tol
Definition: cs_iter_algo.h:117
double res0
Definition: cs_iter_algo.h:121
cs_sles_convergence_state_t cvg_status
Definition: cs_iter_algo.h:115
double res
Definition: cs_iter_algo.h:120
int last_inner_iter
Definition: cs_iter_algo.h:125
double normalization
Definition: cs_iter_algo.h:116
int n_algo_iter
Definition: cs_iter_algo.h:123
Structure storing all the parameters to drive the algorithm called Anderson acceleration.
Definition: cs_iter_algo.h:138
cs_param_dotprod_type_t dp_type
Definition: cs_iter_algo.h:163
int n_max_dir
Definition: cs_iter_algo.h:159
double max_cond
Definition: cs_iter_algo.h:161
int starting_iter
Definition: cs_iter_algo.h:160
double beta
Definition: cs_iter_algo.h:162
Structure to handle the convergence of an iterative algorithm.
Definition: cs_iter_algo.h:290
cs_param_convergence_t cvg_param
Definition: cs_iter_algo.h:309
void * context
Definition: cs_iter_algo.h:312
cs_iter_algo_type_t type
Definition: cs_iter_algo.h:308
int verbosity
Definition: cs_iter_algo.h:310
Set of parameters to check the convergence (or the divergence) of an iterative process (tolerances or...
Definition: cs_param_types.h:558