Chi-Tech
mgd_02_main_exec.cc
Go to the documentation of this file.
2#include "tools/tools.h"
3
4#include "chi_runtime.h"
5#include "chi_log.h"
6
7#include <iomanip>
8
9//#include "mesh/MeshHandler/chi_meshhandler.h"
10//#include "math/SpatialDiscretization/FiniteElement/PiecewiseLinear/pwlc.h"
11
12//========================================================== Execute
14{
15 Chi::log.Log() << "\nExecuting CFEM Multigroup Diffusion solver";
16
17 //============================================= Create Krylov Solver
18 // setup KSP once for all
21 A_.front(), //Matrix
22 TextName(), //Solver name
23 KSPCG, //Solver type
24 PCGAMG, //Preconditioner type
25 basic_options_("residual_tolerance").FloatValue(), //Relative residual tolerance
26 basic_options_("max_inner_iters").IntegerValue() //Max # of inner iterations
27 );
28
29 KSPSetApplicationContext(petsc_solver_.ksp, (void*)&my_app_context_);
30 KSPMonitorCancel(petsc_solver_.ksp);
32 nullptr, nullptr);
33
34 int64_t iverbose = basic_options_("verbose_level").IntegerValue();
35 my_app_context_.verbose = iverbose > 1 ? PETSC_TRUE : PETSC_FALSE;
36// if (my_app_context.verbose == PETSC_TRUE)
37// cout << "--context TRUE" << endl;
38// if (my_app_context.verbose == PETSC_FALSE)
39// cout << "--context FALSE" << endl;
40// std::cout << "STOP" << std::endl; std::cin.get();
41
42 // shortcuts
43 // unsigned int lfg = mg_diffusion::Solver::last_fast_group;
44 // unsigned int ng = mg_diffusion::Solver::num_groups;
45
46 //============================================= Solve fast groups:
47 for (unsigned int g=0; g < last_fast_group_; ++g)
48 {
51 }
52
53 //============================================= Solve thermal groups:
54 unsigned int thermal_iteration = 0;
55 // max # of thermal iterations
56 int64_t max_thermal_iters = basic_options_("max_thermal_iters").IntegerValue();
57 // max thermal error between two successive iterates
58 double thermal_tol = basic_options_("thermal_flux_tolerance").FloatValue();
59 // computed error
60 double thermal_error_all;
61 double thermal_error_g;
62
63 do
64 {
65 thermal_error_all = 0.0;
66 for (unsigned int g=last_fast_group_; g < num_groups_; ++g)
67 {
68 // conpute rhs src
70 // copy solution
71 VecCopy(x_[g], x_old_[g]);
72 // solve group g for new solution
74 // compute L2 norm of thermal error for current g (requires one more copy)
75 VecCopy(x_[g], thermal_dphi_);
76 VecAXPY(thermal_dphi_, -1.0, x_old_[g]);
77 VecNorm(thermal_dphi_, NORM_2, &thermal_error_g);
78 thermal_error_all = std::max(thermal_error_all,thermal_error_g);
79 }
80 // perform two-grid
81 if (do_two_grid_)
82 {
86 }
87
88 if (iverbose > 0)
89 Chi::log.Log() << " --thermal iteration = " << std::setw(5) << std::right << thermal_iteration
90 << ", Error=" << std::setw(11) << std::right << std::scientific << std::setprecision(7)
91 << thermal_error_all << std::endl;
92
93 ++thermal_iteration;
94 }
95 while ( (thermal_error_all > thermal_tol) &&
96 (thermal_iteration < max_thermal_iters) );
97
98 if (iverbose > 0)
99 {
100 if (thermal_error_all < thermal_tol)
101 std::cout << "\nThermal iterations converged for fixed-source problem" << std::endl;
102 else
103 std::cout << "\nThermal iterations NOT converged for fixed-source problem" << std::endl;
104 }
105
107 Chi::log.Log() << "Done solving multi-group diffusion";
108
109}
static chi::ChiLog & log
Definition: chi_runtime.h:81
LogStream Log(LOG_LVL level=LOG_0)
Definition: chi_log.cc:35
std::string TextName() const
Definition: chi_solver.cc:116
BasicOptions basic_options_
Definition: chi_solver.h:57
void Assemble_RHS_TwoGrid(int64_t iverbose)
KSPAppContext my_app_context_
void Execute() override
std::vector< Vec > x_
std::vector< Vec > x_old_
std::vector< Mat > A_
void Assemble_RHS(unsigned int g, int64_t iverbose)
chi_math::PETScUtils::PETScSolverSetup petsc_solver_
void SolveOneGroupProblem(unsigned int g, int64_t iverbose)
PETScSolverSetup CreateCommonKrylovSolverSetup(Mat ref_matrix, const std::string &in_solver_name="KSPSolver", const std::string &in_solver_type=KSPGMRES, const std::string &in_preconditioner_type=PCNONE, double in_relative_residual_tolerance=1.0e-6, int64_t in_maximum_iterations=100)
PetscErrorCode MGKSPMonitor(KSP ksp, PetscInt n, PetscReal rnorm, void *)
Definition: tools.cc:11