Chi-Tech
lbsDO_balance.cc
Go to the documentation of this file.
2
3#include "chi_runtime.h"
4#include "chi_log.h"
6
8
9#include <iomanip>
10
11//###################################################################
12/**Zeroes all the outflow data-structures required to compute
13 * balance.*/
15{
16 for (auto& cell_transport_view : cell_transport_views_)
17 for (auto& group : groupset.groups_)
18 cell_transport_view.ZeroOutflow(group.id_);
19}
20
21//###################################################################
22/**Compute balance.*/
24{
26 Chi::log.Log() << "\n********** Computing balance\n";
27
28 //======================================== Get material source
29 // This is done using the SetSource routine
30 // because it allows a lot of flexibility.
31 auto mat_src = phi_old_local_;
32 mat_src.assign(mat_src.size(),0.0);
33 for (auto& groupset : groupsets_)
34 {
35 q_moments_local_.assign(q_moments_local_.size(), 0.0);
36 active_set_source_function_(groupset, q_moments_local_,
37 PhiOldLocal(),
40 LBSSolver::GSScopedCopyPrimarySTLvectors(groupset,q_moments_local_,mat_src);
41 }
42
43 //======================================== Compute absorption, material-source
44 // and in-flow
45 double local_out_flow = 0.0;
46 double local_in_flow = 0.0;
47 double local_absorption = 0.0;
48 double local_production = 0.0;
49 for (const auto& cell : grid_ptr_->local_cells)
50 {
51 const auto& cell_mapping = discretization_->GetCellMapping(cell);
52 const auto& transport_view = cell_transport_views_[cell.local_id_];
53 const auto& fe_intgrl_values = unit_cell_matrices_[cell.local_id_];
54 const size_t num_nodes = transport_view.NumNodes();
55 const auto& IntV_shapeI = fe_intgrl_values.Vi_vectors;
56 const auto& IntS_shapeI = fe_intgrl_values.face_Si_vectors;
57
58 //====================================== Inflow
59 // This is essentially an integration over
60 // all faces, all angles, and all groups.
61 // Only the cosines that are negative are
62 // added to the integral.
63 for (int f=0; f<cell.faces_.size(); ++f)
64 {
65 const auto& face = cell.faces_[f];
66
67 for (const auto& groupset : groupsets_)
68 {
69 for (int n = 0; n < groupset.quadrature_->omegas_.size(); ++n)
70 {
71 const auto &omega = groupset.quadrature_->omegas_[n];
72 const double wt = groupset.quadrature_->weights_[n];
73 const double mu = omega.Dot(face.normal_);
74
75 if (mu < 0.0 and (not face.has_neighbor_)) //mu<0 and bndry
76 {
77 const auto &bndry = sweep_boundaries_[face.neighbor_id_];
78 for (int fi = 0; fi < face.vertex_ids_.size(); ++fi)
79 {
80 const int i = cell_mapping.MapFaceNode(f, fi);
81 const auto &IntFi_shapeI = IntS_shapeI[f][i];
82
83 for (const auto& group : groupset.groups_)
84 {
85 const int g = group.id_;
86 const double psi = *bndry->HeterogeneousPsiIncoming(
87 cell.local_id_, f, fi, n, g, 0);
88 local_in_flow -= mu * wt * psi * IntFi_shapeI;
89 }//for g
90 }//for fi
91 }//if bndry
92 }//for n
93 }//for groupset
94 }//for f
95
96
97 //====================================== Outflow
98 //The group-wise outflow was determined
99 //during a solve so here we just
100 //consolidate it.
101 for (int g=0; g < num_groups_; ++g)
102 local_out_flow += transport_view.GetOutflow(g);
103
104 //====================================== Absorption and Src
105 //Isotropic flux based absorption and source
106 const auto& xs = transport_view.XS();
107 const auto& sigma_a = xs.SigmaAbsorption();
108 for (int i=0; i<num_nodes; ++i)
109 for (int g=0; g < num_groups_; ++g)
110 {
111 size_t imap = transport_view.MapDOF(i,0,g);
112 double phi_0g = phi_old_local_[imap];
113 double q_0g = mat_src[imap];
114
115 local_absorption += sigma_a[g] * phi_0g * IntV_shapeI[i];
116 local_production += q_0g * IntV_shapeI[i];
117 }//for g
118 }//for cell
119
120 //======================================== Consolidate local balances
121 double local_balance = local_production + local_in_flow
122 - local_absorption - local_out_flow;
123 double local_gain = local_production + local_in_flow;
124
125 std::vector<double> local_balance_table = {local_absorption,
126 local_production,
127 local_in_flow,
128 local_out_flow,
129 local_balance,
130 local_gain};
131 size_t table_size = local_balance_table.size();
132
133 std::vector<double> globl_balance_table(table_size,0.0);
134
135 MPI_Allreduce(local_balance_table.data(), //sendbuf
136 globl_balance_table.data(), //recvbuf
137 table_size,MPI_DOUBLE, //count + datatype
138 MPI_SUM, //operation
139 Chi::mpi.comm); //communicator
140
141 double globl_absorption = globl_balance_table.at(0);
142 double globl_production = globl_balance_table.at(1);
143 double globl_in_flow = globl_balance_table.at(2);
144 double globl_out_flow = globl_balance_table.at(3);
145 double globl_balance = globl_balance_table.at(4);
146 double globl_gain = globl_balance_table.at(5);
147
148 Chi::log.Log() << "Balance table:\n"
149 << std::setprecision(5) << std::scientific
150 << " Absorption rate = " << globl_absorption << "\n"
151 << " Production rate = " << globl_production << "\n"
152 << " In-flow rate = " << globl_in_flow << "\n"
153 << " Out-flow rate = " << globl_out_flow << "\n"
154 << " Net Gain (In-flow + sources) = " << globl_gain << "\n"
155 << " Net Balance = " << globl_balance << "\n"
156 << " (Net Balance)/(Net Gain) = " << globl_balance/globl_gain << "\n";
157
158 Chi::log.Log() << "\n********** Done computing balance\n";
159
161}
static chi::ChiLog & log
Definition: chi_runtime.h:81
static chi::MPI_Info & mpi
Definition: chi_runtime.h:78
LogStream Log(LOG_LVL level=LOG_0)
Definition: chi_log.cc:35
const MPI_Comm & comm
MPI communicator.
Definition: mpi_info.h:28
void Barrier() const
Definition: mpi_info.cc:38
void ZeroOutflowBalanceVars(LBSGroupset &groupset)
std::vector< LBSGroup > groups_
Definition: lbs_groupset.h:41
std::vector< lbs::CellLBSView > cell_transport_views_
Definition: lbs_solver.h:83
virtual void GSScopedCopyPrimarySTLvectors(LBSGroupset &groupset, const std::vector< double > &x_src, std::vector< double > &y)
@ 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