Chi-Tech
pi_keigen_scdsa_03_utils.cc
Go to the documentation of this file.
1#include "pi_keigen_scdsa.h"
2
3#include "chi_runtime.h"
4#include "chi_log.h"
5#include "utils/chi_timer.h"
6
9
10namespace lbs
11{
12
13// ##################################################################
14/**Copies only the scalar moments from an lbs primary flux moments
15 * vector.*/
16std::vector<double>
18 const std::vector<double>& phi_in)
19{
20 typedef const int64_t cint64;
21
22 const auto& lbs_sdm = lbs_solver_.SpatialDiscretization();
23 const auto& diff_sdm = diffusion_solver_->SpatialDiscretization();
24 const auto& diff_uk_man = diffusion_solver_->UnknownStructure();
25 const auto& phi_uk_man = lbs_solver_.UnknownManager();
26
27 const int gsi = groupset.groups_.front().id_;
28 const size_t gss = groupset.groups_.size();
29
30 const size_t diff_num_local_dofs =
31 requires_ghosts_ ? diff_sdm.GetNumLocalAndGhostDOFs(diff_uk_man)
32 : diff_sdm.GetNumLocalDOFs(diff_uk_man);
33
34 std::vector<double> phi_data;
37 phi_in, lbs_sdm, diff_sdm, phi_uk_man, lbs_pwld_ghost_info_);
38 else
39 phi_data = phi_in;
40
41 VecDbl output_phi_local(diff_num_local_dofs, 0.0);
42
43 for (const auto& cell : lbs_solver_.Grid().local_cells)
44 {
45 const auto& cell_mapping = lbs_sdm.GetCellMapping(cell);
46 const size_t num_nodes = cell_mapping.NumNodes();
47
48 for (size_t i = 0; i < num_nodes; i++)
49 {
50 cint64 diff_phi_map = diff_sdm.MapDOFLocal(cell, i, diff_uk_man, 0, 0);
51 cint64 lbs_phi_map = lbs_sdm.MapDOFLocal(cell, i, phi_uk_man, 0, gsi);
52
53 double* output_mapped = &output_phi_local[diff_phi_map];
54 const double* phi_in_mapped = &phi_data[lbs_phi_map];
55
56 for (size_t g = 0; g < gss; g++)
57 {
58 output_mapped[g] = phi_in_mapped[g];
59 } // for g
60 } // for node
61 } // for cell
62
63 return output_phi_local;
64}
65
66// ##################################################################
67/**Copies back only the scalar moments to a lbs primary flux vector.*/
69 const LBSGroupset& groupset,
70 const std::vector<double>& input,
71 std::vector<double>& output)
72{
73 typedef const int64_t cint64;
74
75 const auto& lbs_sdm = lbs_solver_.SpatialDiscretization();
76 const auto& diff_sdm = diffusion_solver_->SpatialDiscretization();
77 const auto& diff_uk_man = diffusion_solver_->UnknownStructure();
78 const auto& phi_uk_man = lbs_solver_.UnknownManager();
79
80 const int gsi = groupset.groups_.front().id_;
81 const size_t gss = groupset.groups_.size();
82
83 const size_t diff_num_local_dofs =
84 requires_ghosts_ ? diff_sdm.GetNumLocalAndGhostDOFs(diff_uk_man)
85 : diff_sdm.GetNumLocalDOFs(diff_uk_man);
86
87 ChiLogicalErrorIf(input.size() != diff_num_local_dofs,
88 "Vector size mismatch");
89
90 for (const auto& cell : lbs_solver_.Grid().local_cells)
91 {
92 const auto& cell_mapping = lbs_sdm.GetCellMapping(cell);
93 const size_t num_nodes = cell_mapping.NumNodes();
94
95 for (size_t i = 0; i < num_nodes; i++)
96 {
97 cint64 diff_phi_map = diff_sdm.MapDOFLocal(cell, i, diff_uk_man, 0, 0);
98 cint64 lbs_phi_map = lbs_sdm.MapDOFLocal(cell, i, phi_uk_man, 0, gsi);
99
100 const double* input_mapped = &input[diff_phi_map];
101 double* output_mapped = &output[lbs_phi_map];
102
103 for (int g = 0; g < gss; g++)
104 output_mapped[g] = input_mapped[g];
105 } // for dof
106 } // for cell
107}
108
109// ##################################################################
110/**Creates a ghost communicator and all associated information.*/
114 const chi_math::UnknownManager& uk_man)
115{
116 Chi::log.Log() << "Making PWLD ghost communicator";
117
118 const size_t num_local_dofs = sdm.GetNumLocalDOFs(uk_man);
119 const size_t num_globl_dofs = sdm.GetNumGlobalDOFs(uk_man);
120
121 Chi::log.Log() << "Number of global dofs" << num_globl_dofs;
122
123 const size_t num_unknowns = uk_man.unknowns_.size();
124
125 // Build a list of global ids
126 std::set<int64_t> global_dof_ids_set;
127
128 const auto& grid = lbs_solver_.Grid();
129 const auto ghost_cell_ids = grid.cells.GetGhostGlobalIDs();
130 for (const auto global_id : ghost_cell_ids)
131 {
132 const auto& cell = grid.cells[global_id];
133 const auto& cell_mapping = sdm.GetCellMapping(cell);
134 const size_t num_nodes = cell_mapping.NumNodes();
135
136 for (size_t i = 0; i < num_nodes; ++i)
137 {
138 for (size_t u = 0; u < num_unknowns; ++u)
139 {
140 const size_t num_comps = uk_man.unknowns_[u].num_components_;
141 for (size_t c = 0; c < num_comps; ++c)
142 {
143 const int64_t dof_map = sdm.MapDOF(cell, i, uk_man, u, c);
144 global_dof_ids_set.insert(dof_map);
145 } // for component
146 } // for unknown
147 } // for node i
148 } // for ghost cell
149
150 // Convert the list to a vector
151 std::vector<int64_t> global_indices(global_dof_ids_set.begin(),
152 global_dof_ids_set.end());
153
154 // Create the vector ghost communicator
155 auto vgc = std::make_shared<chi_math::VectorGhostCommunicator>(
156 num_local_dofs, num_globl_dofs, global_indices, Chi::mpi.comm);
157
158 // Create the map
159 std::map<int64_t, int64_t> ghost_global_id_2_local_map;
160 {
161 int64_t k = 0;
162 for (const auto ghost_id : global_indices)
163 {
164 ghost_global_id_2_local_map[ghost_id] =
165 static_cast<int64_t>(num_local_dofs + k++);
166 }
167 }
168
169 Chi::log.Log() << "Done making PWLD ghost communicator";
170 return {vgc, ghost_global_id_2_local_map};
171}
172
173} // namespace lbs
#define ChiLogicalErrorIf(condition, message)
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
size_t NumNodes() const
Definition: CellMapping.cc:34
const CellMapping & GetCellMapping(const chi_mesh::Cell &cell) const
size_t GetNumGlobalDOFs(const UnknownManager &unknown_manager) const
virtual int64_t MapDOF(const chi_mesh::Cell &cell, unsigned int node, const UnknownManager &unknown_manager, unsigned int unknown_id, unsigned int component) const =0
size_t GetNumLocalDOFs(const UnknownManager &unknown_manager) const
std::vector< Unknown > unknowns_
std::vector< uint64_t > GetGhostGlobalIDs() const
LocalCellHandler local_cells
GlobalCellHandler cells
std::vector< LBSGroup > groups_
Definition: lbs_groupset.h:41
const chi_math::SpatialDiscretization & SpatialDiscretization() const
const chi_math::UnknownManager & UnknownManager() const
const chi_mesh::MeshContinuum & Grid() const
static std::vector< double > NodallyAveragedPWLDVector(const std::vector< double > &input, const chi_math::SpatialDiscretization &pwld_sdm, const chi_math::SpatialDiscretization &pwlc_sdm, const chi_math::UnknownManager &uk_man, const XXPowerIterationKEigenSCDSA::GhostInfo &ghost_info)
std::vector< double > CopyOnlyPhi0(const LBSGroupset &groupset, const std::vector< double > &phi_in)
DiffusionSolverPtr diffusion_solver_
void ProjectBackPhi0(const LBSGroupset &groupset, const std::vector< double > &input, std::vector< double > &output)
GhostInfo MakePWLDVecGhostCommInfo(const chi_math::SpatialDiscretization &sdm, const chi_math::UnknownManager &uk_man)
std::vector< double > VecDbl
Definition: lbs_structs.h:18