Chi-Tech
ags_linear_solver.cc
Go to the documentation of this file.
1#include "ags_linear_solver.h"
2
4
7
8#include <petscksp.h>
9
10#include "chi_runtime.h"
11#include "chi_log.h"
12
13#include <iomanip>
14
15#define sc_int64_t static_cast<int64_t>
16
17#define GetAGSContextPtr(x) \
18 std::dynamic_pointer_cast<AGSContext<Mat,Vec,KSP>>(x)
19namespace lbs
20{
21
22template<>
24{
25 auto ags_context_ptr = GetAGSContextPtr(context_ptr_);
26
27 const auto sizes = ags_context_ptr->SystemSize();
28
29 num_local_dofs_ = sizes.first;
30 num_globl_dofs_ = sizes.second;
31}
32
33template<>
35{
37 sc_int64_t(num_globl_dofs_));
38
39 VecSet(x_,0.0);
40 VecDuplicate(x_,&b_);
41
42 //============================================= Create the matrix-shell
43 MatCreateShell(PETSC_COMM_WORLD,sc_int64_t(num_local_dofs_),
44 sc_int64_t(num_local_dofs_),
45 sc_int64_t(num_globl_dofs_),
46 sc_int64_t(num_globl_dofs_),
47 &(*context_ptr_),&A_);
48
49 //============================================= Set the action-operator
50 MatShellSetOperation(A_, MATOP_MULT,
51 (void (*)()) chi_math::LinearSolverMatrixAction<Mat, Vec>);
52
53 //============================================= Set solver operators
54 KSPSetOperators(solver_, A_, A_);
55 KSPSetUp(solver_);
56}
57
58template<>
60{
61 auto ags_context_ptr = GetAGSContextPtr(context_ptr_);
62
63 ags_context_ptr->SetPreconditioner(solver_);
64}
65
66template<>
68{}
69
70template<>
72{
73
74}
75
76template<>
78{
79 auto ags_context_ptr = GetAGSContextPtr(context_ptr_);
80 auto& lbs_solver = ags_context_ptr->lbs_solver_;
81
82 const int gid_i = GroupSpanFirstID();
83 const int gid_f = GroupSpanLastID();
84 const auto& phi = lbs_solver.PhiOldLocal();
85
86 Vec x_old;
87 VecDuplicate(x_, &x_old);
88
89 //Save qmoms to be restored after each iteration.
90 //This is necessary for multiple ags iterations to function
91 //and for keigen-value problems
92 const auto saved_qmoms = lbs_solver.QMomentsLocal();
93
94 for (int iter = 0; iter < tolerance_options_.maximum_iterations; ++iter)
95 {
96
97 lbs_solver.SetGroupScopedPETScVecFromPrimarySTLvector(gid_i,gid_f,x_old,phi);
98
99 for (auto& solver : ags_context_ptr->sub_solvers_list_)
100 {
101 solver->Setup();
102 solver->Solve();
103 }
104
105 lbs_solver.SetGroupScopedPETScVecFromPrimarySTLvector(gid_i,gid_f,x_,phi);
106
107 VecAXPY(x_old, -1.0, x_);
108 PetscReal error_norm; VecNorm(x_old, NORM_2, &error_norm);
109 PetscReal sol_norm;VecNorm(x_, NORM_2, &sol_norm);
110
111
112 if (verbose_)
113 Chi::log.Log()
114 << "********** AGS solver iteration " << std::setw(3) << iter << " "
115 << " Relative change " << std::setw(10) << std::setprecision(4)
116 << error_norm/sol_norm;
117
118 lbs_solver.QMomentsLocal() = saved_qmoms; //Restore qmoms
119
120 if (error_norm < tolerance_options_.residual_absolute)
121 break;
122 }//for iteration
123
124
125 VecDestroy(&x_old);
126}
127
128template<>
130{
131 MatDestroy(&A_);
132}
133
134}//namespace lbs
#define sc_int64_t
#define GetAGSContextPtr(x)
static chi::ChiLog & log
Definition: chi_runtime.h:81
LogStream Log(LOG_LVL level=LOG_0)
Definition: chi_log.cc:35
void SetInitialGuess() override
void SetPreconditioner() override
virtual void SetSystemSize() override
void SetRHS() override
virtual void SetSystem() override
virtual ~AGSLinearSolver() override
void Solve() override
Vec CreateVector(int64_t local_size, int64_t global_size)
struct _p_Vec * Vec