9.2
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-2026 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 "alge/cs_param_sles.h"
33#include "alge/cs_sles.h"
34#include "base/cs_math.h"
35#include "base/cs_param_types.h"
36#include "cdo/cs_cdo_blas.h"
37#include "cdo/cs_sdm.h"
38
39/*============================================================================
40 * Macro definitions
41 *============================================================================*/
42
43/*============================================================================
44 * Type definitions
45 *============================================================================*/
46
47/* \enum cs_iter_algo_type_bit_t
48 * \brief Bit values for the definition of the type of iterative algorithms
49 *
50 * \var CS_ITER_ALGO_DEFAULT
51 * Iterative algorithms like linear solvers for saddle-point system, fixed
52 * point...
53 *
54 * \var CS_ITER_ALGO_ANDERSON
55 * Iterative algorithms relying on the Anderson acceleration
56 *
57 * \var CS_ITER_ALGO_TWO_LEVEL
58 * Iterative algorithms using an inner/outer iterative loops
59 */
60
61typedef enum {
62
63 CS_ITER_ALGO_DEFAULT = 1 << 0, /* = 1 */
64 CS_ITER_ALGO_ANDERSON = 1 << 1, /* = 2 */
65 CS_ITER_ALGO_TWO_LEVEL = 1 << 2, /* = 4 */
66
68
70
71/* Structure used as context for the iterative algorithm considered by
72 default */
73
74typedef struct {
75
113 double tol;
114
115 double prev_res;
116 double res;
117 double res0;
118
122
124
125/* Structures used when an Anderson acceleration is considered */
126/* ----------------------------------------------------------- */
127
134typedef struct {
135
154 double max_cond;
155 double beta;
156
158
165typedef struct {
166
176
214 double tol;
215
216 double prev_res;
217 double res;
218 double res0;
219
223
256 cs_lnum_t n_elts; /*<! Size of an array for one direction */
257 int n_dir;
258
263
265 cs_sdm_t *R;
266
268
270
271/* ++++++++++++++ */
272/* Main structure */
273/* ++++++++++++++ */
274
285typedef struct {
286
309
310 void *context;
311
313
314/*============================================================================
315 * Public function prototypes
316 *============================================================================*/
317
318/*----------------------------------------------------------------------------*/
319/*
320 * \brief Create and initialize by default a new cs_iter_algo_t structure
321 *
322 * \param[in] type type of iterative algorithm
323 *
324 * \return a pointer to the new allocated structure
325 */
326/*----------------------------------------------------------------------------*/
327
330
331/*----------------------------------------------------------------------------*/
332/*
333 * \brief Create a new cs_iter_algo_t structure with the given settings
334 *
335 * \param[in] type type of iterative algorithm
336 * \param[in] verbosity level of information to print
337 * \param[in] cvg_param set of parameters driving the convergence of the
338 * iterative algorithm
339 *
340 * \return a pointer to the new allocated structure
341 */
342/*----------------------------------------------------------------------------*/
343
346 int verbosity,
347 cs_param_convergence_t cvg_param);
348
349/*----------------------------------------------------------------------------*/
350/*
351 * \brief Free a cs_iter_algo_t structure
352 *
353 * \param[in, out] p_algo double pointer on the structure to free
354 */
355/*----------------------------------------------------------------------------*/
356
357void
359
360/*----------------------------------------------------------------------------*/
361/*
362 * \brief Reset a cs_iter_algo_t structure
363 *
364 * \param[in, out] algo pointer to a cs_iter_algo_t
365 */
366/*----------------------------------------------------------------------------*/
367
368void
370
371/*----------------------------------------------------------------------------*/
372/*
373 * \brief Free the members (arrays and matrix) associated to the context
374 * structure of an Anderson acceleration
375 *
376 * \param[in, out] c pointer to an Anderson context structure
377 */
378/*----------------------------------------------------------------------------*/
379
380void
382
383/*----------------------------------------------------------------------------*/
384/*
385 * \brief Define the verbosity of the given iterative algorithm
386 *
387 * \param[in, out] algo pointer to the structure to update
388 * \param[in] verbosity level of information to print
389 */
390/*----------------------------------------------------------------------------*/
391
392void
394 int verbosity);
395
396/*----------------------------------------------------------------------------*/
397/*
398 * \brief Define the criteria related to the convergence of the given iterative
399 * algorithm
400 *
401 * \param[in, out] algo pointer to the structure to update
402 * \param[in] cvg_param set of parameters driving the convergence of the
403 * iterative algorithm
404 */
405/*----------------------------------------------------------------------------*/
406
407void
409 cs_param_convergence_t cvg_param);
410
411/*----------------------------------------------------------------------------*/
412/*
413 * \brief Set the final tolerance used to check the convergence of the algorithm
414 * This tolerance should take into account rtol and atol for instance
415 *
416 * \param[in, out] algo pointer to the structure to update
417 * \param[in] tol tolerance to apply
418 */
419/*----------------------------------------------------------------------------*/
420
421void
423 double tol);
424
425/*----------------------------------------------------------------------------*/
426/*
427 * \brief Set the initial residual used to detect a divergence
428 *
429 * \param[in, out] algo pointer to the structure to update
430 * \param[in] value value of the initial residual
431 */
432/*----------------------------------------------------------------------------*/
433
434void
436 double value);
437
438/*----------------------------------------------------------------------------*/
439/*
440 * \brief Set the normalization to apply when checking the convergence of the
441 * algorithm.
442 *
443 * \param[in, out] algo pointer to the structure to update
444 * \param[in] value normalization to apply
445 */
446/*----------------------------------------------------------------------------*/
447
448void
450 double value);
451
452/*----------------------------------------------------------------------------*/
453/*
454 * \brief Set the convergence status of the given structure
455 *
456 * \param[in, out] algo pointer to the structure to update
457 * \param[in] cvg_status status to set
458 */
459/*----------------------------------------------------------------------------*/
460
461void
463 cs_sles_convergence_state_t cvg_status);
464
465/*----------------------------------------------------------------------------*/
466/*
467 * \brief Create a new cs_iter_algo_aac_t structure for Anderson acceleration
468 *
469 * \param[in, out] algo pointer to the structure to update
470 * \param[in] aac_param set of parameters for the Anderson acceleration
471 * \param[in] n_elts number of elements by direction
472 */
473/*----------------------------------------------------------------------------*/
474
475void
477 cs_iter_algo_param_aac_t aac_param,
478 cs_lnum_t n_elts);
479
480/*----------------------------------------------------------------------------*/
481/*
482 * \brief Retrieve the current number of iterations done
483 *
484 * \param[in, out] algo pointer to the structure to examine
485 *
486 * \return the number of iterations done
487 */
488/*----------------------------------------------------------------------------*/
489
490int
492
493/*----------------------------------------------------------------------------*/
494/*
495 * \brief Retrieve the cumulated number of inner iterations done
496 *
497 * \param[in, out] algo pointer to the structure to examine
498 *
499 * \return the number of iterations done
500 */
501/*----------------------------------------------------------------------------*/
502
503int
505
506/*----------------------------------------------------------------------------*/
507/*
508 * \brief Retrieve the last computed residual
509 *
510 * \param[in, out] algo pointer to the structure to examine
511 *
512 * \return the number of iterations done
513 */
514/*----------------------------------------------------------------------------*/
515
516double
518
519/*----------------------------------------------------------------------------*/
520/*
521 * \brief Retrieve the last convergence state
522 *
523 * \param[in, out] algo pointer to the structure to examine
524 *
525 * \return the convergence status
526 */
527/*----------------------------------------------------------------------------*/
528
531
532/*----------------------------------------------------------------------------*/
533/*
534 * \brief Get the normalization to apply when computing the tolerance threshold
535 *
536 * \param[in] algo pointer to a cs_iter_algo_t structure
537 */
538/*----------------------------------------------------------------------------*/
539
540double
542
543/*----------------------------------------------------------------------------*/
544/*
545 * \brief Retrieve the set of parameters for an Anderson algorithm
546 *
547 * \param[in, out] algo pointer to a cs_iter_algo_t structure
548 *
549 * \return a cs_iter_algo_param_aac_t structure
550 */
551/*----------------------------------------------------------------------------*/
552
555
556/*----------------------------------------------------------------------------*/
557/*
558 * \brief Apply one more iteration of the Anderson acceleration
559 *
560 * \param[in, out] algo pointer to a cs_iter_algo_t structure
561 * \param[in, out] cur_iterate current iterate
562 * \param[in] pre_iterate previous iterate
563 * \param[in] dotprod function to compute a dot product
564 * \param[in] sqnorm function to compute a square norm
565 */
566/*----------------------------------------------------------------------------*/
567
568void
570 cs_real_t *cur_iterate,
571 const cs_real_t *pre_iterate,
572 cs_cdo_blas_dotprod_t *dotprod,
574
575/*----------------------------------------------------------------------------*/
576/*
577 * \brief Update the counting of inner iterations in two-level algorithms
578 *
579 * \param[in, out] algo pointer to the structure to update
580 * \param[in] n_last_inner_iter last number of inner loop iterations
581 */
582/*----------------------------------------------------------------------------*/
583
584void
586 int n_last_inner_iter);
587
588/*----------------------------------------------------------------------------*/
589/*
590 * \brief Update the value of the residual and its associated members.
591 *
592 * \param[in, out] algo pointer to a cs_iter_algo_t structure
593 * \param[in] res new value of the residual
594 */
595/*----------------------------------------------------------------------------*/
596
597void
599 double res);
600
601/*----------------------------------------------------------------------------*/
602/*
603 * \brief Update the convergence status and the number of iterations. The
604 * tolerance threshold is given so that it has to be computed before
605 * calling this function.
606 *
607 * \param[in, out] algo pointer to a cs_iter_algo_t structure
608 * \param[in] tol tolerance threshold to apply
609 *
610 * \return the convergence state
611 */
612/*----------------------------------------------------------------------------*/
613
616 double tol);
617
618/*----------------------------------------------------------------------------*/
619/*
620 * \brief Update the convergence status and the number of iterations. The
621 * tolerance threshold is automatically computed by a default formula
622 * relying on the relative tolerance scaled by the normalization factor
623 * and the absolute tolerance.
624 *
625 * \param[in, out] algo pointer to a cs_iter_algo_t structure
626 *
627 * \return the convergence state
628 */
629/*----------------------------------------------------------------------------*/
630
633
634/*----------------------------------------------------------------------------*/
635/*
636 * \brief Log the convergence of the iterative algorithm
637 *
638 * \param[in] algo pointer to the iterative algorithm structure
639 * \param[in] label label to specify the log
640 */
641/*----------------------------------------------------------------------------*/
642
643void
645 const char *label);
646
647/*----------------------------------------------------------------------------*/
648/*
649 * \brief Check if something wrong happens during the iterative process after
650 * one new iteration
651 *
652 * \param[in] func_name name of the calling function
653 * \param[in] eq_name name of the equation being solved
654 * \param[in] algo_name name of the iterative algo. used
655 * \param[in] algo pointer to the iterative algo. structure
656 */
657/*----------------------------------------------------------------------------*/
658
659void
660cs_iter_algo_check_warning(const char *func_name,
661 const char *eq_name,
662 const char *algo_name,
664
665/*----------------------------------------------------------------------------*/
666
667#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:73
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:59
double cs_real_t
Floating-point value.
Definition: cs_defs.h:332
int cs_lnum_t
local mesh entity id
Definition: cs_defs.h:325
unsigned short int cs_flag_t
Definition: cs_defs.h:334
void cs_iter_algo_reset(cs_iter_algo_t *algo)
Reset a cs_iter_algo_t structure.
Definition: cs_iter_algo.cpp:540
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:1513
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:641
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:1415
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:572
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:966
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:672
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:902
cs_iter_algo_type_bit_t
Definition: cs_iter_algo.h:61
@ CS_ITER_ALGO_DEFAULT
Definition: cs_iter_algo.h:63
@ CS_ITER_ALGO_ANDERSON
Definition: cs_iter_algo.h:64
@ CS_ITER_ALGO_TWO_LEVEL
Definition: cs_iter_algo.h:65
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:400
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:1390
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:479
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:735
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:799
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:597
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:1229
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:1266
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:704
void cs_iter_algo_free(cs_iter_algo_t **p_algo)
Free a cs_iter_algo_t structure.
Definition: cs_iter_algo.cpp:503
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:993
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:933
double cs_iter_algo_get_residual(const cs_iter_algo_t *algo)
Retrieve the last computed residual.
Definition: cs_iter_algo.cpp:869
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:767
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:1314
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:618
cs_flag_t cs_iter_algo_type_t
Definition: cs_iter_algo.h:69
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:832
Structure and routines handling the SLES ((Sparse Linear Equation Solver) settings stored inside a cs...
cs_sles_convergence_state_t
Definition: cs_sles.h:57
char * label
Definition: keywords.h:58
char * algo
Definition: field_names.h:106
Context structure for the algorithm called Anderson acceleration.
Definition: cs_iter_algo.h:165
int n_inner_iter
Definition: cs_iter_algo.h:221
cs_real_t * gold
Definition: cs_iter_algo.h:261
double prev_res
Definition: cs_iter_algo.h:216
double tol
Definition: cs_iter_algo.h:214
double res0
Definition: cs_iter_algo.h:218
cs_real_t * dg
Definition: cs_iter_algo.h:262
cs_iter_algo_param_aac_t param
Definition: cs_iter_algo.h:175
cs_real_t * fold
Definition: cs_iter_algo.h:259
cs_real_t * Q
Definition: cs_iter_algo.h:264
cs_lnum_t n_elts
Definition: cs_iter_algo.h:256
cs_sles_convergence_state_t cvg_status
Definition: cs_iter_algo.h:212
cs_real_t * gamma
Definition: cs_iter_algo.h:267
int n_dir
Definition: cs_iter_algo.h:257
double res
Definition: cs_iter_algo.h:217
cs_real_t * df
Definition: cs_iter_algo.h:260
int last_inner_iter
Definition: cs_iter_algo.h:222
cs_sdm_t * R
Definition: cs_iter_algo.h:265
double normalization
Definition: cs_iter_algo.h:213
int n_algo_iter
Definition: cs_iter_algo.h:220
Definition: cs_iter_algo.h:74
int n_inner_iter
Definition: cs_iter_algo.h:120
double prev_res
Definition: cs_iter_algo.h:115
double tol
Definition: cs_iter_algo.h:113
double res0
Definition: cs_iter_algo.h:117
cs_sles_convergence_state_t cvg_status
Definition: cs_iter_algo.h:111
double res
Definition: cs_iter_algo.h:116
int last_inner_iter
Definition: cs_iter_algo.h:121
double normalization
Definition: cs_iter_algo.h:112
int n_algo_iter
Definition: cs_iter_algo.h:119
Structure storing all the parameters to drive the algorithm called Anderson acceleration.
Definition: cs_iter_algo.h:134
int n_max_dir
Definition: cs_iter_algo.h:152
double max_cond
Definition: cs_iter_algo.h:154
int starting_iter
Definition: cs_iter_algo.h:153
double beta
Definition: cs_iter_algo.h:155
Structure to handle the convergence of an iterative algorithm.
Definition: cs_iter_algo.h:285
cs_param_convergence_t cvg_param
Definition: cs_iter_algo.h:306
bool header_done
Definition: cs_iter_algo.h:308
void * context
Definition: cs_iter_algo.h:310
cs_iter_algo_type_t type
Definition: cs_iter_algo.h:305
int verbosity
Definition: cs_iter_algo.h:307
Set of parameters to check the convergence (or the divergence) of an iterative process (tolerances or...
Definition: cs_param_types.h:603