Chi-Tech
nl_keigen_acc_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 <iomanip>
13
14#define CheckContext(x) \
15if (not x) \
16throw std::runtime_error(std::string(__PRETTY_FUNCTION__) + \
17": context casting failure")
18#define GetNLKDiffContextPtr(x) \
19 std::dynamic_pointer_cast<NLKEigenDiffContext>(x); \
20 CheckContext(x)
21
22namespace lbs::acceleration
23{
24
26{
27 auto nl_context_ptr = GetNLKDiffContextPtr(context_ptr_);
28
29 if (nl_context_ptr->verbosity_level_ >= 1)
30 SNESMonitorSet(nl_solver_, &lbs::KEigenSNESMonitor,
31 &nl_context_ptr->kresid_func_context_, nullptr);
32
33 if (nl_context_ptr->verbosity_level_ >= 2)
34 {
35 KSP ksp;
36 SNESGetKSP(nl_solver_, &ksp);
37 KSPMonitorSet(ksp, &lbs::KEigenKSPMonitor,
38 &nl_context_ptr->kresid_func_context_, nullptr);
39 }
40}
41
43{
44 auto nl_context_ptr = GetNLKDiffContextPtr(context_ptr_);
45
46 auto& diff_solver = nl_context_ptr->diff_solver_;
47 auto sizes = diff_solver.GetNumPhiIterativeUnknowns();
48
49 num_local_dofs_ = static_cast<int64_t>(sizes.first);
50 num_globl_dofs_ = static_cast<int64_t>(sizes.second);
51}
52
53
55{
56 //============================================= Create the vectors
59 VecDuplicate(x_, &r_);
60}
61
62
64{
65 auto nl_context_ptr = GetNLKDiffContextPtr(context_ptr_);
66
68 &nl_context_ptr->kresid_func_context_);
69}
70
72{
73 MatCreateSNESMF(nl_solver_, &J_);
74 SNESSetJacobian(nl_solver_, J_, J_, MatMFFDComputeJacobian, nullptr);
75}
76
78{
79 VecSet(x_, 0.0);
80}
81
83{
84 auto nl_context_ptr = GetNLKDiffContextPtr(context_ptr_);
85
86 auto& lbs_solver = nl_context_ptr->lbs_solver_;
87 auto& groupsets = lbs_solver.Groupsets();
88 auto& front_gs = groupsets.front();
89
90 auto& phi_old_local = lbs_solver.PhiOldLocal();
91 auto& phi_new_local = lbs_solver.PhiNewLocal();
92
93 auto delta_phi = nl_context_ptr->PhiVecToSTLVec(x_);
94 auto& phi_lph_ip1 = nl_context_ptr->phi_lph_ip1_;
95
96 using namespace chi_math;
97 auto phi_lp1_temp = phi_lph_ip1 + delta_phi;
98 lbs_solver.GSProjectBackPhi0(front_gs, phi_lp1_temp, phi_new_local);
99 lbs_solver.GSScopedCopyPrimarySTLvectors(front_gs, phi_new_local, phi_old_local);
100
101 //============================================= Compute final k_eff
102 double k_eff = nl_context_ptr->kresid_func_context_.k_eff;
103
104 const double production = lbs_solver.ComputeFissionProduction(phi_old_local);
105 lbs_solver.ScalePhiVector(PhiSTLOption::PHI_OLD, k_eff/production);
106
107
108 PetscInt number_of_func_evals;
109 SNESGetNumberFunctionEvals(nl_solver_, &number_of_func_evals);
110
111 //================================================== Print summary
112 if (nl_context_ptr->verbosity_level_ >= 1)
113 Chi::log.Log()
114 << " Final lambda-eigenvalue : "
115 << std::fixed << std::setw(10) << std::setprecision(7)
116 << k_eff
117 << " (num_DOps:" << number_of_func_evals << ")"
118 << "\n";
119}
120
121}//namespace lbs::acceleration
static chi::ChiLog & log
Definition: chi_runtime.h:81
LogStream Log(LOG_LVL level=LOG_0)
Definition: chi_log.cc:35
Vec CreateVector(int64_t local_size, int64_t global_size)
PetscErrorCode NLKEigenAccResidualFunction(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 GetNLKDiffContextPtr(x)