Chi-Tech
nl_keigen_ags_solver.cc
Go to the documentation of this file.
2
4
6
8
9#include "chi_runtime.h"
10#include "chi_log.h"
11
12#include <petscsnes.h>
13
14#include <iomanip>
15
16#define SNESTypes Mat,Vec,SNES
17#define CheckContext(x) \
18if (not x) \
19throw std::runtime_error(std::string(__PRETTY_FUNCTION__) + \
20": context casting failure")
21#define GetNLKAGSContextPtr(x) \
22 std::dynamic_pointer_cast<NLKEigenAGSContext<Vec,SNES>>(x); \
23 CheckContext(x)
24
25namespace lbs
26{
27
28template<>
30{
31 auto nl_context_ptr = GetNLKAGSContextPtr(context_ptr_);
32
33 auto& lbs_solver = nl_context_ptr->lbs_solver_;
34 for (auto& groupset : lbs_solver.Groupsets())
35 nl_context_ptr->groupset_ids.push_back(groupset.id_);
36}
37
38template<>
40{
41 auto nl_context_ptr = GetNLKAGSContextPtr(context_ptr_);
42
43 auto& lbs_solver = nl_context_ptr->lbs_solver_;
44 if (lbs_solver.Options().verbose_outer_iterations)
45 SNESMonitorSet(nl_solver_, &lbs::KEigenSNESMonitor,
46 &nl_context_ptr->kresid_func_context_, nullptr);
47
48 if (lbs_solver.Options().verbose_inner_iterations)
49 {
50 KSP ksp;
51 SNESGetKSP(nl_solver_, &ksp);
52 KSPMonitorSet(ksp, &lbs::KEigenKSPMonitor,
53 &nl_context_ptr->kresid_func_context_, nullptr);
54 }
55}
56
57template<>
59{
60 auto nl_context_ptr = GetNLKAGSContextPtr(context_ptr_);
61
62 auto& lbs_solver = nl_context_ptr->lbs_solver_;
63 auto sizes = lbs_solver.GetNumPhiIterativeUnknowns();
64
65 num_local_dofs_ = static_cast<int64_t>(sizes.first);
66 num_globl_dofs_ = static_cast<int64_t>(sizes.second);
67}
68
69template<>
71{
72 //============================================= Create the vectors
73 x_ = chi_math::PETScUtils::CreateVector(num_local_dofs_,
74 num_globl_dofs_);
75 VecDuplicate(x_, &r_);
76}
77
78template<>
80{
81 auto nl_context_ptr = GetNLKAGSContextPtr(context_ptr_);
82
83 SNESSetFunction(nl_solver_, r_, NLKEigenResidualFunction,
84 &nl_context_ptr->kresid_func_context_);
85}
86
87template<>
89{
90 MatCreateSNESMF(nl_solver_, &J_);
91 SNESSetJacobian(nl_solver_, J_, J_, MatMFFDComputeJacobian, nullptr);
92}
93
94template<>
96{
97 auto nl_context_ptr = GetNLKAGSContextPtr(context_ptr_);
98
99 auto& lbs_solver = nl_context_ptr->lbs_solver_;
100 const auto& groupset_ids = nl_context_ptr->groupset_ids;
101
102 lbs_solver.SetMultiGSPETScVecFromPrimarySTLvector(
103 groupset_ids, x_, PhiSTLOption::PHI_OLD);
104}
105
106template<>
108{
109 auto nl_context_ptr = GetNLKAGSContextPtr(context_ptr_);
110
111 auto& lbs_solver = nl_context_ptr->lbs_solver_;
112
113 //============================================= Unpack solution
114 const auto& groups = lbs_solver.Groups();
115 lbs_solver.SetPrimarySTLvectorFromGroupScopedPETScVec(
116 groups.front().id_, groups.back().id_, x_, lbs_solver.PhiOldLocal());
117
118 //============================================= Compute final k_eff
119 double k_eff = lbs_solver.ComputeFissionProduction(lbs_solver.PhiOldLocal());
120
121 PetscInt number_of_func_evals;
122 SNESGetNumberFunctionEvals(nl_solver_, &number_of_func_evals);
123
124 //================================================== Print summary
125 Chi::log.Log() << "\n"
126 << " Final k-eigenvalue : "
127 << std::fixed << std::setw(10) << std::setprecision(7)
128 << k_eff
129 << " (num_TrOps:" << number_of_func_evals << ")"
130 << "\n";
131}
132
133}//namespace lbs
static chi::ChiLog & log
Definition: chi_runtime.h:81
LogStream Log(LOG_LVL level=LOG_0)
Definition: chi_log.cc:35
void PostSolveCallback() override
void SetFunction() override
void PreSetupCallback() override
void SetJacobian() override
void SetMonitor() override
void SetInitialGuess() override
void SetSystemSize() override
Vec CreateVector(int64_t local_size, int64_t global_size)
PetscErrorCode NLKEigenResidualFunction(SNES snes, Vec phi, Vec r, void *ctx)
PetscErrorCode KEigenSNESMonitor(SNES, PetscInt iter, PetscReal rnorm, void *ctx)
PetscErrorCode KEigenKSPMonitor(KSP ksp, PetscInt iter, PetscReal rnorm, void *ctx)
#define GetNLKAGSContextPtr(x)