Chi-Tech
chi_diffusion_utils.cc
Go to the documentation of this file.
1#include "chi_diffusion.h"
2
3#include <iomanip>
4
5#include "chi_runtime.h"
6#include "chi_log.h"
7
8#include "chi_mpi.h"
9
10
11//###################################################################
12/**Customized monitor for PETSc Krylov sub-space solvers.*/
14 KSP ksp, PetscInt n, PetscReal rnorm, void *monitordestroy)
15{
16
17 Vec Rhs;
18 KSPGetRhs(ksp,&Rhs);
19 double rhs_norm;
20 VecNorm(Rhs,NORM_2,&rhs_norm);
21 if (rhs_norm < 1.0e-25)
22 rhs_norm = 1.0;
23
24 if (Chi::mpi.location_id == 0)
25 {
26 const auto ksp_name = "Diffusion";
27
28 std::stringstream buff;
29 buff
30 << ksp_name
31 << " iteration "
32 << std::setw(4) << n
33 << " - Residual "
34 << std::scientific << std::setprecision(7) << rnorm / rhs_norm
35 << std::endl;
36
37 Chi::log.Log() << buff.str();
38 }
39 return 0;
40}
41
42//###################################################################
43/**Customized convergence test.*/
45 KSP ksp, PetscInt n, PetscReal rnorm,
46 KSPConvergedReason* convergedReason, void*)
47{
48 //======================================================= Compute rhs norm
49 Vec Rhs;
50 KSPGetRhs(ksp,&Rhs);
51 double rhs_norm;
52 VecNorm(Rhs,NORM_2,&rhs_norm);
53 if (rhs_norm < 1.0e-25)
54 rhs_norm = 1.0;
55
56 //======================================================= Compute test criterion
57 double tol;
58 int64_t maxIts;
59 KSPGetTolerances(ksp,NULL,&tol,NULL,&maxIts);
60
61 double relative_residual = rnorm/rhs_norm;
62
63 Chi::log.Log() << "Iteration " << n << " Residual " << rnorm/rhs_norm;
64
65 if (relative_residual < tol)
66 *convergedReason = KSP_CONVERGED_RTOL;
67
68 return KSP_CONVERGED_ITERATING;
69}
static chi::ChiLog & log
Definition: chi_runtime.h:81
static chi::MPI_Info & mpi
Definition: chi_runtime.h:78
LogStream Log(LOG_LVL level=LOG_0)
Definition: chi_log.cc:35
PetscErrorCode DiffusionConvergenceTestNPT(KSP ksp, PetscInt n, PetscReal rnorm, KSPConvergedReason *convergedReason, void *monitordestroy)
PetscErrorCode KSPMonitorAChiTech(KSP ksp, PetscInt n, PetscReal rnorm, void *monitordestroy)
struct _p_Vec * Vec