18#define sc_double static_cast<double>
19#define sc_int64_t static_cast<int64_t>
21#define GetGSContextPtr(x) \
22 std::dynamic_pointer_cast<WGSContext<Mat, Vec, KSP>>(x)
32 gs_context_ptr->PreSetupCallback();
45 const auto sizes = gs_context_ptr->SystemSize();
47 num_local_dofs_ = sizes.first;
48 num_globl_dofs_ = sizes.second;
54 if (IsSystemSet())
return;
60 VecDuplicate(x_, &b_);
63 MatCreateShell(PETSC_COMM_WORLD,
73 A_, MATOP_MULT, (
void (*)())chi_math::LinearSolverMatrixAction<Mat, Vec>);
76 KSPSetOperators(solver_, A_, A_);
83 if (IsSystemSet())
return;
86 gs_context_ptr->SetPreconditioner(solver_);
94 gs_context_ptr->PostSetupCallback();
102 gs_context_ptr->PreSolveCallback();
113 auto& groupset = gs_context_ptr->groupset_;
114 auto& lbs_solver = gs_context_ptr->lbs_solver_;
116 lbs_solver.SetGSPETScVecFromPrimarySTLvector(
119 double init_guess_norm = 0.0;
120 VecNorm(x_, NORM_2, &init_guess_norm);
122 if (init_guess_norm > 1.0e-10)
124 KSPSetInitialGuessNonzero(solver_, PETSC_TRUE);
125 if (gs_context_ptr->log_info_)
126 Chi::log.
Log() <<
"Using phi_old as initial guess.";
135 auto& groupset = gs_context_ptr->groupset_;
136 auto& lbs_solver = gs_context_ptr->lbs_solver_;
138 if (gs_context_ptr->log_info_)
142 saved_q_moments_local_ = lbs_solver.QMomentsLocal();
144 const bool single_richardson = iterative_method_ ==
"richardson" and
145 tolerance_options_.maximum_iterations == 1;
147 if (not single_richardson)
151 gs_context_ptr->set_source_function_(
152 groupset, lbs_solver.QMomentsLocal(), lbs_solver.PhiOldLocal(), scope);
156 gs_context_ptr->ApplyInverseTransportOperator(scope);
159 lbs_solver.SetGSPETScVecFromPrimarySTLvector(
163 VecNorm(b_, NORM_2, &context_ptr_->rhs_norm);
168 KSPGetPC(solver_, &pc);
170 VecDuplicate(b_, &temp_vec);
171 PCApply(pc, b_, temp_vec);
172 VecNorm(temp_vec, NORM_2, &context_ptr_->rhs_preconditioned_norm);
173 VecDestroy(&temp_vec);
181 const int scope = gs_context_ptr->rhs_src_scope_ |
182 gs_context_ptr->lhs_src_scope_;
183 gs_context_ptr->set_source_function_(
184 groupset, lbs_solver.QMomentsLocal(), lbs_solver.PhiOldLocal(), scope);
188 gs_context_ptr->ApplyInverseTransportOperator(scope);
191 lbs_solver.SetGSPETScVecFromPrimarySTLvector(
195 VecNorm(x_, NORM_2, &context_ptr_->rhs_norm);
200 KSPGetPC(solver_, &pc);
202 VecDuplicate(x_, &temp_vec);
203 PCApply(pc, x_, temp_vec);
204 VecNorm(temp_vec, NORM_2, &context_ptr_->rhs_preconditioned_norm);
205 VecDestroy(&temp_vec);
207 SetKSPSolveSuppressionFlag(
true);
216 if (not GetKSPSolveSuppressionFlag())
218 KSPConvergedReason reason;
219 KSPGetConvergedReason(solver_, &reason);
220 if (reason != KSP_CONVERGED_RTOL and reason != KSP_DIVERGED_ITS)
230 auto& groupset = gs_context_ptr->groupset_;
231 auto& lbs_solver = gs_context_ptr->lbs_solver_;
233 lbs_solver.SetPrimarySTLvectorFromGSPETScVec(
235 lbs_solver.SetPrimarySTLvectorFromGSPETScVec(
239 lbs_solver.QMomentsLocal() = saved_q_moments_local_;
242 gs_context_ptr->PostSolveCallback();
static chi::Timer program_timer
LogStream Log(LOG_LVL level=LOG_0)
std::string GetTimeString() const
void PreSolveCallback() override
void PostSolveCallback() override
void PreSetupCallback() override
virtual ~WGSLinearSolver() override
void SetConvergenceTest() override
virtual void SetSystemSize() override
void PostSetupCallback() override
void SetInitialGuess() override
virtual void SetSystem() override
void SetPreconditioner() override
Vec CreateVector(int64_t local_size, int64_t global_size)
std::string GetPETScConvergedReasonstring(KSPConvergedReason reason)
@ ZERO_INCOMING_DELAYED_PSI
PetscErrorCode GSConvergenceTest(KSP ksp, PetscInt n, PetscReal rnorm, KSPConvergedReason *convergedReason, void *)
#define GetGSContextPtr(x)