Chi-Tech
poweriteration_keigen.cc
Go to the documentation of this file.
2
5
6#include "chi_runtime.h"
7#include "chi_log.h"
8#include "utils/chi_timer.h"
9
10#include <iomanip>
11
12namespace lbs
13{
14
16 double tolerance,
17 int max_iterations,
18 double& k_eff)
19{
20 const std::string fname = "lbs::PowerIterationKEigen";
21
22 for (auto& wgs_solver : lbs_solver.GetWGSSolvers())
23 {
24 auto context = wgs_solver->GetContext();
25 auto wgs_context =
26 std::dynamic_pointer_cast<lbs::WGSContext<Mat,Vec,KSP>>(context);
27
28 if (not wgs_context) throw std::logic_error(fname + ": Cast failed.");
29
30 wgs_context->lhs_src_scope_ = APPLY_WGS_SCATTER_SOURCES;
31 wgs_context->rhs_src_scope_ = APPLY_AGS_SCATTER_SOURCES |
33 }
34
35 auto& q_moments_local = lbs_solver.QMomentsLocal();
36 auto& phi_old_local = lbs_solver.PhiOldLocal();
37 auto& phi_new_local = lbs_solver.PhiNewLocal();
38 auto primary_ags_solver = lbs_solver.GetPrimaryAGSSolver();
39 auto& groupsets = lbs_solver.Groupsets();
40 auto active_set_source_function = lbs_solver.GetActiveSetSourceFunction();
41
42 auto& front_gs = groupsets.front();
43 auto& front_wgs_solver = lbs_solver.GetWGSSolvers()[front_gs.id_];
44 auto frons_wgs_context = std::dynamic_pointer_cast<lbs::WGSContext<Mat,Vec,KSP>>(
45 front_wgs_solver->GetContext());
46
47 double F_prev = 1.0;
48 k_eff = 1.0;
49 double k_eff_prev = 1.0;
50 double k_eff_change = 1.0;
51
52 //================================================== Start power iterations
53 primary_ags_solver->SetVerbosity(lbs_solver.Options().verbose_ags_iterations);
54 int nit = 0;
55 bool converged = false;
56 while (nit < max_iterations)
57 {
58 chi_math::Set(q_moments_local, 0.0);
59 for (auto& groupset : groupsets)
60 active_set_source_function(groupset,
61 q_moments_local,//output
62 phi_old_local,//input
65
66 chi_math::Scale(q_moments_local, 1.0/k_eff);
67
68 //============================ This solves the inners for transport
69 primary_ags_solver->Setup();
70 primary_ags_solver->Solve();
71
72 //======================================== Recompute k-eigenvalue
73 double F_new = lbs_solver.ComputeFissionProduction(phi_new_local);
74 k_eff = F_new / F_prev * k_eff;
75 double reactivity = (k_eff - 1.0) / k_eff;
76
77 //======================================== Check convergence, bookkeeping
78 k_eff_change = fabs(k_eff - k_eff_prev) / k_eff;
79 k_eff_prev = k_eff;
80 F_prev = F_new;
81 nit += 1;
82
83 if (k_eff_change < std::max(tolerance, 1.0e-12))
84 converged = true;
85
86 //======================================== Print iteration summary
87 if (lbs_solver.Options().verbose_outer_iterations)
88 {
89 std::stringstream k_iter_info;
90 k_iter_info
92 << " Iteration " << std::setw(5) << nit
93 << " k_eff " << std::setw(11) << std::setprecision(7) << k_eff
94 << " k_eff change " << std::setw(12) << k_eff_change
95 << " reactivity " << std::setw(10) << reactivity * 1e5;
96 if (converged) k_iter_info << " CONVERGED\n";
97
98 Chi::log.Log() << k_iter_info.str();
99 }
100
101 if (converged) break;
102 }//for k iterations
103
104 //================================================== Print summary
105 Chi::log.Log() << "\n";
106 Chi::log.Log()
107 << " Final k-eigenvalue : "
108 << std::setprecision(7) << k_eff;
109 Chi::log.Log()
110 << " Final change : "
111 << std::setprecision(6) << k_eff_change
112 << " (num_TrOps:" << frons_wgs_context->counter_applications_of_inv_op_ << ")"
113 << "\n";
114 Chi::log.Log() << "\n";
115}
116
117}//namespace lbs
static chi::Timer program_timer
Definition: chi_runtime.h:79
static chi::ChiLog & log
Definition: chi_runtime.h:81
LogStream Log(LOG_LVL level=LOG_0)
Definition: chi_log.cc:35
std::string GetTimeString() const
Definition: chi_timer.cc:38
AGSLinSolverPtr GetPrimaryAGSSolver()
std::vector< double > & PhiNewLocal()
std::vector< double > & QMomentsLocal()
std::vector< LBSGroupset > & Groupsets()
double ComputeFissionProduction(const std::vector< double > &phi)
std::vector< LinSolvePtr > & GetWGSSolvers()
SetSourceFunction GetActiveSetSourceFunction() const
lbs::Options & Options()
std::vector< double > & PhiOldLocal()
void Scale(VecDbl &x, const double &val)
void Set(VecDbl &x, const double &val)
void PowerIterationKEigen(LBSSolver &lbs_solver, double tolerance, int max_iterations, double &k_eff)
@ APPLY_AGS_FISSION_SOURCES
Definition: lbs_structs.h:94
@ APPLY_WGS_FISSION_SOURCES
Definition: lbs_structs.h:93
@ APPLY_FIXED_SOURCES
Definition: lbs_structs.h:90
@ APPLY_WGS_SCATTER_SOURCES
Definition: lbs_structs.h:91
@ APPLY_AGS_SCATTER_SOURCES
Definition: lbs_structs.h:92
bool verbose_ags_iterations
Definition: lbs_structs.h:144
bool verbose_outer_iterations
Definition: lbs_structs.h:145