Chi-Tech
nl_keigen_acc_residual_func.cc
Go to the documentation of this file.
1#include <petscsnes.h>
2
4
5#include "chi_log.h"
6
7namespace lbs::acceleration
8{
9
10PetscErrorCode
11 NLKEigenAccResidualFunction(SNES snes, Vec phi, Vec r, void* ctx)
12{
13 NLKEigenDiffContext* nl_context_ptr;
14 SNESGetApplicationContext(snes, &nl_context_ptr);
15
16 auto& diff_solver = nl_context_ptr->diff_solver_;
17
18 auto& lbs_solver = nl_context_ptr->lbs_solver_;
19 auto& groupsets = lbs_solver.Groupsets();
20 auto active_set_source_function = lbs_solver.GetActiveSetSourceFunction();
21 auto& front_gs = groupsets.front();
22
23 auto& q_moments_local = lbs_solver.QMomentsLocal();
24 auto& phi_old_local = lbs_solver.PhiOldLocal();
25 auto phi_temp = phi_old_local;
26
27 const auto& phi_l = nl_context_ptr->phi_l_;
28 const auto& phi_lph_i = nl_context_ptr->phi_lph_i_;
29 const auto& phi_lph_ip1 = nl_context_ptr->phi_lph_ip1_;
30 const auto& k_l = nl_context_ptr->k_l;
31 const auto& Sf = nl_context_ptr->Sf_;
32
33 //============================================= Lambdas
34 auto SetLBSFissionSource = [&active_set_source_function,&front_gs]
35 (const VecDbl& input, VecDbl& output)
36 {
37 chi_math::Set(output, 0.0);
38 active_set_source_function(front_gs, output,
39 input,
42 };
43
44 auto SetLBSScatterSource = [&active_set_source_function,&front_gs]
45 (const VecDbl& input, VecDbl& output, bool suppress_wgs)
46 {
47 chi_math::Set(output, 0.0);
48 active_set_source_function(front_gs, output,
49 input,
52 (suppress_wgs ?
55 };
56
57 auto SetPhi0FissionSource = [&front_gs,&lbs_solver,&phi_temp,
58 &SetLBSFissionSource,&q_moments_local]
59 (const VecDbl& input)
60 {
61 chi_math::Set(phi_temp, 0.0);
62 lbs_solver.GSProjectBackPhi0(front_gs, /*in*/input,
63 /*out*/phi_temp);
64
65 SetLBSFissionSource(/*input*/phi_temp, /*output*/q_moments_local);
66
67 auto output = lbs_solver.WGSCopyOnlyPhi0(front_gs, q_moments_local);
68 return output;
69 };
70
71 auto SetPhi0ScatterSource = [&front_gs,&lbs_solver,&phi_temp,
72 &SetLBSScatterSource,&q_moments_local]
73 (const VecDbl& input, bool suppress_wgs)
74 {
75 chi_math::Set(phi_temp, 0.0);
76 lbs_solver.GSProjectBackPhi0(front_gs, /*in*/input,
77 /*out*/phi_temp);
78
79 SetLBSScatterSource(/*input*/phi_temp, /*output*/q_moments_local,
80 suppress_wgs);
81
82 auto output = lbs_solver.WGSCopyOnlyPhi0(front_gs, q_moments_local);
83 return output;
84 };
85
86 auto Phi0FissionProdL2Norm = [&front_gs,&lbs_solver,&phi_temp]
87 (const VecDbl& input)
88 {
89 chi_math::Set(phi_temp, 0.0);
90 lbs_solver.GSProjectBackPhi0(front_gs,
91 /*in*/input,
92 /*out*/phi_temp);
93
94 return lbs_solver.ComputeFissionProduction(phi_temp);
95 };
96
97 //============================================= Business end
98 using namespace chi_math;
99
100 auto delta_phi = nl_context_ptr->PhiVecToSTLVec(phi);
101 auto epsilon = delta_phi;
102
103 auto Ss_res = SetPhi0ScatterSource(phi_lph_ip1-phi_lph_i, /*suppress_wgs=*/false);
104 auto Sscat = SetPhi0ScatterSource(delta_phi, /*suppress_wgs=*/true);
105
106 auto Sfaux = SetPhi0FissionSource(delta_phi + phi_lph_ip1);
107 double lambda = Phi0FissionProdL2Norm(delta_phi + phi_lph_ip1);
108 Scale(Sfaux, 1.0 / lambda);
109
110 diff_solver.Assemble_b(Sscat + Sfaux + Ss_res - Sf);
111 diff_solver.Solve(epsilon, /*use_initial_guess=*/true);
112
113 nl_context_ptr->STLVecToPhiVec(epsilon - delta_phi, r);
114
115// double production_old = Phi0FissionProdL2Norm(epsilon_k + phi_lph);
116// double production_new = Phi0FissionProdL2Norm(epsilon_kp1 + phi_lph);
117
118// nl_context_ptr->kresid_func_context_.k_eff =
119// production_new / production_old * mu_k;
120
121// chi::log.Log() << Phi0FissionProdL2Norm(delta_phi) << " " << lambda;
122
123 nl_context_ptr->kresid_func_context_.k_eff = lambda;
124
125 return 0;
126}
127
128}//namespace lbs::acceleration
std::vector< LBSGroupset > & Groupsets()
void Scale(VecDbl &x, const double &val)
void Set(VecDbl &x, const double &val)
PetscErrorCode NLKEigenAccResidualFunction(SNES snes, Vec phi, Vec r, void *ctx)
@ APPLY_AGS_FISSION_SOURCES
Definition: lbs_structs.h:94
@ NO_FLAGS_SET
Definition: lbs_structs.h:89
@ APPLY_WGS_FISSION_SOURCES
Definition: lbs_structs.h:93
@ SUPPRESS_WG_SCATTER
Definition: lbs_structs.h:95
@ APPLY_WGS_SCATTER_SOURCES
Definition: lbs_structs.h:91
@ APPLY_AGS_SCATTER_SOURCES
Definition: lbs_structs.h:92
std::vector< double > VecDbl
Definition: lbs_structs.h:18
struct _p_Vec * Vec
void STLVecToPhiVec(const VecDbl &input, Vec phi) const