/usr/share/code_saturne/user_examples/cs_user_parameters-linear_solvers.c is in code-saturne-data 4.2.0+repack-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 | /*============================================================================
* User subroutines for input of calculation parameters.
*============================================================================*/
/* Code_Saturne version 4.2.0 */
/*
This file is part of Code_Saturne, a general-purpose CFD tool.
Copyright (C) 1998-2015 EDF S.A.
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*----------------------------------------------------------------------------*/
#include "cs_defs.h"
/*----------------------------------------------------------------------------
* Standard C library headers
*----------------------------------------------------------------------------*/
#include <assert.h>
#include <math.h>
#include <string.h>
#include <stdio.h>
#if defined(HAVE_MPI)
#include <mpi.h>
#endif
#if defined(HAVE_PETSC)
#include <petscdraw.h>
#include <petscviewer.h>
#include <petscksp.h>
#endif
/*----------------------------------------------------------------------------
* PLE library headers
*----------------------------------------------------------------------------*/
#include <ple_coupling.h>
/*----------------------------------------------------------------------------
* Local headers
*----------------------------------------------------------------------------*/
#include "bft_mem.h"
#include "bft_error.h"
#include "bft_printf.h"
#include "fvm_writer.h"
#include "cs_base.h"
#include "cs_field.h"
#include "cs_field_pointer.h"
#include "cs_field_operator.h"
#include "cs_gui_util.h"
#include "cs_grid.h"
#include "cs_math.h"
#include "cs_mesh.h"
#include "cs_mesh_location.h"
#include "cs_mesh_quantities.h"
#include "cs_halo.h"
#include "cs_halo_perio.h"
#include "cs_log.h"
#include "cs_multigrid.h"
#include "cs_parameters.h"
#include "cs_physical_constants.h"
#include "cs_prototypes.h"
#include "cs_sles.h"
#include "cs_sles_it.h"
#include "cs_time_moment.h"
#include "cs_time_step.h"
#include "cs_turbomachinery.h"
#include "cs_selector.h"
#include "cs_post.h"
#if defined(HAVE_PETSC)
#include "cs_sles_petsc.h"
#endif
/*----------------------------------------------------------------------------
* Header for the current file
*----------------------------------------------------------------------------*/
#include "cs_prototypes.h"
/*----------------------------------------------------------------------------*/
BEGIN_C_DECLS
/*============================================================================
* User function definitions
*============================================================================*/
/*============================================================================
* User function definitions
*============================================================================*/
#if defined(HAVE_PETSC)
/*----------------------------------------------------------------------------
* User function example for setup options of a PETSc KSP solver.
*
* This function is called at the end of the setup stage for a KSP solver.
*
* Note: if the context pointer is non-NULL, it must point to valid data
* when the selection function is called so that value or structure should
* not be temporary (i.e. local);
*
* parameters:
* context <-> pointer to optional (untyped) value or structure
* ksp <-> pointer to PETSc KSP context
*----------------------------------------------------------------------------*/
/* Conjugate gradient with Jacobi preconditioning */
/*------------------------------------------------*/
/*! [sles_petsc_hook_1] */
static void
_petsc_p_setup_hook(const void *context,
KSP ksp)
{
PC pc;
KSPSetType(ksp, KSPCG); /* Preconditioned Conjugate Gradient */
KSPSetNormType(ksp, KSP_NORM_UNPRECONDITIONED); /* Try to have "true" norm */
KSPGetPC(ksp, &pc);
PCSetType(pc, PCJACOBI); /* Jacobi (diagonal) preconditioning */
}
/*! [sles_petsc_hook_1] */
/* Conjugate gradient with GAMG preconditioning */
/*----------------------------------------------*/
/*! [sles_petsc_hook_gamg] */
static void
_petsc_p_setup_hook_gamg(const void *context,
KSP ksp)
{
PC pc;
KSPSetType(ksp, KSPCG); /* Preconditioned Conjugate Gradient */
KSPGetPC(ksp, &pc);
PCSetType(pc, PCGAMG); /* GAMG (geometric-algebraic multigrid)
preconditioning */
}
/*! [sles_petsc_hook_gamg] */
/* Conjugate gradient with HYPRE BoomerAMG preconditioning */
/*---------------------------------------------------------*/
/*! [sles_petsc_hook_bamg] */
static void
_petsc_p_setup_hook_bamg(const void *context,
KSP ksp)
{
PC pc;
KSPSetType(ksp, KSPCG); /* Preconditioned Conjugate Gradient */
KSPGetPC(ksp, &pc);
PCSetType(pc, PCHYPRE); /* HYPRE BoomerAMG preconditioning */
}
/*! [sles_petsc_hook_bamg] */
/*----------------------------------------------------------------------------
* User function example for setup options of a PETSc KSP solver.
*
* This example outputs the matrix structure and values, based on several
* options.
*
* This function is called the end of the setup stage for a KSP solver.
*
* Note: if the context pointer is non-NULL, it must point to valid data
* when the selection function is called so that value or structure should
* not be temporary (i.e. local);
*
* parameters:
* context <-> pointer to optional (untyped) value or structure
* ksp <-> pointer to PETSc KSP context
*----------------------------------------------------------------------------*/
/*! [sles_petsc_hook_view] */
static void
_petsc_p_setup_hook_view(const void *context,
KSP ksp)
{
PC pc;
const char *p = getenv("CS_USER_PETSC_MAT_VIEW");
if (p != NULL) {
/* Get system and preconditioner matrixes */
Mat a, pa;
KSPGetOperators(ksp, &a, &pa);
/* Output matrix in several ways depending on
CS_USER_PETSC_MAT_VIEW environment variable */
if (strcmp(p, "DEFAULT") == 0)
MatView(a, PETSC_VIEWER_DEFAULT);
else if (strcmp(p, "DRAW_WORLD") == 0)
MatView(a, PETSC_VIEWER_DRAW_WORLD);
else if (strcmp(p, "DRAW") == 0) {
PetscViewer viewer;
PetscDraw draw;
PetscViewerDrawOpen(PETSC_COMM_WORLD, NULL, "PETSc View",
0, 0, 600, 600, &viewer);
PetscViewerDrawGetDraw(viewer, 0, &draw);
PetscViewerDrawSetPause(viewer, -1);
MatView(a, viewer);
PetscDrawPause(draw);
PetscViewerDestroy(&viewer);
}
}
}
/*! [sles_petsc_hook_view] */
#endif /* defined(HAVE_PETSC) */
/*----------------------------------------------------------------------------*/
/*!
* \brief Define linear solver options.
*
* This function is called at the setup stage, once user and most model-based
* fields are defined.
*
* Available native iterative linear solvers include conjugate gradient,
* Jacobi, BiCGStab, BiCGStab2, and GMRES. For symmetric linear systems,
* an algebraic multigrid solver is available (and recommended).
*
* External solvers may also be setup using this function, the cs_sles_t
* mechanism alowing such through user-define functions.
*/
/*----------------------------------------------------------------------------*/
void
cs_user_linear_solvers(void)
{
/* Available native iterative linear solvers are:
*
* CS_SLES_PCG (preconditionned conjugate gradient)
* CS_SLES_JACOBI (Jacobi)
* CS_SLES_BICGSTAB (Bi-conjugate gradient stabilized)
* CS_SLES_BICGSTAB2 (BiCGStab2)
* CS_SLES_GMRES (generalized minimal residual)
*
* The multigrid solver uses the conjugate gradient as a smoother
* and coarse solver by default, but this behavior may be modified. */
/* Example: use multigrid for wall distance computation */
/*------------------------------------------------------*/
/*! [sles_wall_dist] */
cs_multigrid_define(-1, "wall_distance");
/*! [sles_wall_dist] */
/* Example: use BiCGStab2 for user variable (named user_1) */
/*---------------------------------------------------------*/
/*! [sles_user_1] */
cs_field_t *cvar_user_1 = cs_field_by_name_try("user_1");
if (cvar_user_1 != NULL) {
cs_sles_it_define(cvar_user_1->id,
NULL, /* name passed is NULL if field_id > -1 */
CS_SLES_BICGSTAB2,
1, /* polynomial precond. degree (default 0) */
10000); /* n_max_iter */
}
/*! [sles_user_1] */
/* Example: increase verbosity parameters for pressure */
/*-----------------------------------------------------*/
BEGIN_EXAMPLE_SCOPE
/*! [sles_verbosity_1] */
cs_sles_t *sles_p = cs_sles_find_or_add(CS_F_(p)->id, NULL);
cs_sles_set_verbosity(sles_p, 4);
/*! [sles_verbosity_1] */
END_EXAMPLE_SCOPE
/* Example: change multigrid parameters for pressure */
/*---------------------------------------------------*/
BEGIN_EXAMPLE_SCOPE
/*! [sles_mgp_1] */
cs_multigrid_t *mg = cs_multigrid_define(CS_F_(p)->id, NULL);
cs_multigrid_set_coarsening_options(mg,
3, /* aggregation_limit (default 3) */
0, /* coarsening_type (default 0) */
10, /* n_max_levels (default 25) */
30, /* min_g_cells (default 30) */
0.95, /* P0P1 relaxation (default 0.95) */
20); /* postprocessing (default 0) */
cs_multigrid_set_solver_options
(mg,
CS_SLES_JACOBI, /* descent smoother type (default: CS_SLES_PCG) */
CS_SLES_JACOBI, /* ascent smoother type (default: CS_SLES_PCG) */
CS_SLES_PCG, /* coarse solver type (default: CS_SLES_PCG) */
50, /* n max cycles (default 100) */
5, /* n max iter for descent (default 2) */
5, /* n max iter for asscent (default 10) */
1000, /* n max iter coarse solver (default 10000) */
0, /* polynomial precond. degree descent (default 0) */
0, /* polynomial precond. degree ascent (default 0) */
1, /* polynomial precond. degree coarse (default 0) */
-1.0, /* precision multiplier descent (< 0 forces max iters) */
-1.0, /* precision multiplier ascent (< 0 forces max iters) */
0.1); /* requested precision multiplier coarse (default 1) */
/*! [sles_mgp_1] */
END_EXAMPLE_SCOPE
/* Set parallel grid merging options for all multigrid solvers */
/*-------------------------------------------------------------*/
/*! [sles_mg_parall] */
cs_grid_set_merge_options(4, /* # of ranks merged at a time */
300, /* mean # of cells under which we merge */
500, /* global # of cells under which we merge */
1); /* # of ranks under which we do not merge */
/*! [sles_mg_parall] */
/* Example: conjugate gradient preconditioned by multigrid for pressure */
/*----------------------------------------------------------------------*/
BEGIN_EXAMPLE_SCOPE
/*! [sles_mgp_2] */
cs_sles_it_t *c = cs_sles_it_define(CS_F_(p)->id,
NULL,
CS_SLES_PCG,
-1,
10000);
cs_sles_pc_t *pc = cs_multigrid_pc_create();
cs_multigrid_t *mg = cs_sles_pc_get_context(pc);
cs_sles_it_transfer_pc(c, &pc);
assert(strcmp(cs_sles_pc_get_type(cs_sles_it_get_pc(c)), "multigrid") == 0);
cs_multigrid_set_solver_options
(mg,
CS_SLES_P_GAUSS_SEIDEL, /* descent smoother (CS_SLES_P_GAUSS_SEIDEL) */
CS_SLES_P_GAUSS_SEIDEL, /* ascent smoother (CS_SLES_P_GAUSS_SEIDEL) */
CS_SLES_PCG, /* coarse solver (CS_SLES_P_GAUSS_SEIDEL) */
1, /* n max cycles (default 1) */
1, /* n max iter for descent (default 1) */
1, /* n max iter for asscent (default 1) */
500, /* n max iter coarse solver (default 1) */
0, /* polynomial precond. degree descent (default) */
0, /* polynomial precond. degree ascent (default) */
0, /* polynomial precond. degree coarse (default 0) */
-1.0, /* precision multiplier descent (< 0 forces max iters) */
-1.0, /* precision multiplier ascent (< 0 forces max iters) */
1.0); /* requested precision multiplier coarse (default 1) */
/*! [sles_mgp_2] */
END_EXAMPLE_SCOPE
/* Set a non-default linear solver for DOM radiation. */
/*----------------------------------------------------*/
/* The solver must be set for each direction; here, we assume
a quadrature with 32 directions is used */
BEGIN_EXAMPLE_SCOPE
/*! [sles_rad_dom_1] */
for (int i = 0; i < 32; i++) {
char name[16];
sprintf(name, "radiation_%03d", i+1);
cs_sles_it_define(-1,
name,
CS_SLES_JACOBI,
0, /* poly_degree */
1000); /* n_max_iter */
}
/*! [sles_rad_dom_1] */
END_EXAMPLE_SCOPE
/* Example: activate convergence plot for pressure */
/*-------------------------------------------------*/
BEGIN_EXAMPLE_SCOPE
/*! [sles_plot_1] */
const cs_field_t *f = CS_F_(p);
cs_sles_t *sles_p = cs_sles_find_or_add(f->id, NULL);
bool use_iteration = true; /* use iteration or wall clock time for axis */
if (strcmp(cs_sles_get_type(sles_p), "cs_sles_it_t") == 0) {
cs_sles_it_t *c = cs_sles_get_context(sles_p);
cs_sles_it_set_plot_options(c, f->name, use_iteration);
}
else if (strcmp(cs_sles_get_type(sles_p), "cs_multigrid_t") == 0) {
cs_multigrid_t *c = cs_sles_get_context(sles_p);
cs_multigrid_set_plot_options(c, f->name, use_iteration);
}
/*! [sles_plot_1] */
END_EXAMPLE_SCOPE
#if defined(HAVE_PETSC)
/* Setting global options for PETSc */
/*----------------------------------*/
BEGIN_EXAMPLE_SCOPE
/*! [sles_petsc_1] */
/* Initialization must be called before setting options;
it does not need to be called before calling
cs_sles_petsc_define(), as this is handled automatically. */
PETSC_COMM_WORLD = cs_glob_mpi_comm;
PetscInitializeNoArguments();
/* See the PETSc documentation for the options database */
PetscOptionsSetValue("-ksp_type", "cg");
PetscOptionsSetValue("-pc_type", "jacobi");
/*! [sles_petsc_1] */
END_EXAMPLE_SCOPE
/* Setting pressure solver with PETSc */
/*------------------------------------*/
BEGIN_EXAMPLE_SCOPE
/*! [sles_petsc_2] */
cs_sles_petsc_define(CS_F_(p)->id,
NULL,
MATSHELL,
_petsc_p_setup_hook,
NULL);
/*! [sles_petsc_2] */
END_EXAMPLE_SCOPE
/* Setting global options for PETSc with GAMG preconditioner */
/*-----------------------------------------------------------*/
BEGIN_EXAMPLE_SCOPE
/*! [sles_petsc_gamg_1] */
/* Initialization must be called before setting options;
it does not need to be called before calling
cs_sles_petsc_define(), as this is handled automatically. */
PETSC_COMM_WORLD = cs_glob_mpi_comm;
PetscInitializeNoArguments();
/* See the PETSc documentation for the options database */
PetscOptionsSetValue("-ksp_type", "cg");
PetscOptionsSetValue("-pc_type", "gamg");
PetscOptionsSetValue("-pc_gamg_agg_nsmooths", "1");
PetscOptionsSetValue("-mg_levels_ksp_type", "richardson");
PetscOptionsSetValue("-mg_levels_pc_type", "sor");
PetscOptionsSetValue("-mg_levels_ksp_max_it", "1");
PetscOptionsSetValue("-pc_gamg_threshold", "0.02");
PetscOptionsSetValue("-pc_gamg_reuse_interpolation", "TRUE");
PetscOptionsSetValue("-pc_gamg_square_graph", "4");
/*! [sles_petsc_gamg_1] */
END_EXAMPLE_SCOPE
/* Setting pressure solver with PETSc and GAMG preconditioner */
/*------------------------------------------------------------*/
BEGIN_EXAMPLE_SCOPE
/*! [sles_petsc_gamg_2] */
cs_sles_petsc_define(CS_F_(p)->id,
NULL,
MATMPIAIJ,
_petsc_p_setup_hook_gamg,
NULL);
/*! [sles_petsc_gamg_2] */
END_EXAMPLE_SCOPE
/* Setting global options for PETSc with HYPRE BoomerAMG preconditioner */
/*----------------------------------------------------------------------*/
BEGIN_EXAMPLE_SCOPE
/*! [sles_petsc_bamg_1] */
/* Initialization must be called before setting options;
it does not need to be called before calling
cs_sles_petsc_define(), as this is handled automatically. */
PETSC_COMM_WORLD = cs_glob_mpi_comm;
PetscInitializeNoArguments();
/* See the PETSc documentation for the options database */
PetscOptionsSetValue("-ksp_type", "cg");
PetscOptionsSetValue("-pc_type", "hypre");
PetscOptionsSetValue("-pc_hypre_type","boomeramg");
PetscOptionsSetValue("-pc_hypre_boomeramg_coarsen_type","HMIS");
PetscOptionsSetValue("-pc_hypre_boomeramg_interp_type","ext+i-cc");
PetscOptionsSetValue("-pc_hypre_boomeramg_agg_nl","2");
PetscOptionsSetValue("-pc_hypre_boomeramg_P_max","4");
PetscOptionsSetValue("-pc_hypre_boomeramg_strong_threshold","0.5");
PetscOptionsSetValue("-pc_hypre_boomeramg_no_CF","");
/*! [sles_petsc_bamg_1] */
END_EXAMPLE_SCOPE
/* Setting pressure solver with PETSc and BoomerAMG preconditioner */
/*-----------------------------------------------------------------*/
BEGIN_EXAMPLE_SCOPE
/*! [sles_petsc_bamg_2] */
cs_sles_petsc_define(CS_F_(p)->id,
NULL,
MATMPIAIJ,
_petsc_p_setup_hook_bamg,
NULL);
/*! [sles_petsc_bamg_2] */
END_EXAMPLE_SCOPE
#endif /* defined(HAVE_PETSC) */
}
/*----------------------------------------------------------------------------*/
END_C_DECLS
|