Chi-Tech
lbs_01d_init_spat_discr.cc
Go to the documentation of this file.
1#include "lbs_solver.h"
2
5
6#include "chi_runtime.h"
7#include "chi_log.h"
8
10
11#include <iomanip>
12
14{
15 using namespace chi_math::finite_element;
16 Chi::log.Log() << "Initializing spatial discretization.\n";
18
20}
21
23{
24 Chi::log.Log() << "Computing unit integrals.\n";
25 const auto& sdm = *discretization_;
26
27 //======================================== Define spatial weighting functions
28 struct SpatialWeightFunction //SWF
29 {
30 virtual double operator()(const chi_mesh::Vector3& pt) const
31 { return 1.0; }
32 virtual ~SpatialWeightFunction() = default;
33 };
34
35 struct SphericalSWF : public SpatialWeightFunction
36 {
37 double operator()(const chi_mesh::Vector3& pt) const override
38 { return pt[2]*pt[2]; }
39 };
40
41 struct CylindricalSWF : public SpatialWeightFunction
42 {
43 double operator()(const chi_mesh::Vector3& pt) const override
44 { return pt[0]; }
45 };
46
47 auto swf_ptr = std::make_shared<SpatialWeightFunction>();
48 if (options_.geometry_type == lbs::GeometryType::ONED_SPHERICAL)
49 swf_ptr = std::make_shared<SphericalSWF>();
50 if (options_.geometry_type == lbs::GeometryType::TWOD_CYLINDRICAL)
51 swf_ptr = std::make_shared<CylindricalSWF>();
52
53 auto ComputeCellUnitIntegrals = [&sdm](const chi_mesh::Cell& cell,
54 const SpatialWeightFunction& swf)
55 {
56 const auto& cell_mapping = sdm.GetCellMapping(cell);
57 const size_t cell_num_faces = cell.faces_.size();
58 const size_t cell_num_nodes = cell_mapping.NumNodes();
59 const auto vol_qp_data = cell_mapping.MakeVolumetricQuadraturePointData();
60
61 MatDbl IntV_gradshapeI_gradshapeJ(cell_num_nodes, VecDbl(cell_num_nodes));
62 MatVec3 IntV_shapeI_gradshapeJ(cell_num_nodes, VecVec3(cell_num_nodes));
63 MatDbl IntV_shapeI_shapeJ(cell_num_nodes, VecDbl(cell_num_nodes));
64 VecDbl IntV_shapeI(cell_num_nodes);
65
66 std::vector<MatDbl> IntS_shapeI_shapeJ(cell_num_faces);
67 std::vector<MatVec3> IntS_shapeI_gradshapeJ(cell_num_faces);
68 std::vector<VecDbl> IntS_shapeI(cell_num_faces);
69
70 //Volume integrals
71 for (unsigned int i = 0; i < cell_num_nodes; ++i)
72 {
73 for (unsigned int j = 0; j < cell_num_nodes; ++j)
74 {
75 for (const auto& qp : vol_qp_data.QuadraturePointIndices())
76 {
77 IntV_gradshapeI_gradshapeJ[i][j]
78 += swf(vol_qp_data.QPointXYZ(qp)) *
79 vol_qp_data.ShapeGrad(i, qp).Dot(vol_qp_data.ShapeGrad(j, qp)) *
80 vol_qp_data.JxW(qp); //K-matrix
81
82 IntV_shapeI_gradshapeJ[i][j]
83 += swf(vol_qp_data.QPointXYZ(qp)) *
84 vol_qp_data.ShapeValue(i, qp) *
85 vol_qp_data.ShapeGrad(j, qp) *
86 vol_qp_data.JxW(qp); //G-matrix
87
88 IntV_shapeI_shapeJ[i][j]
89 += swf(vol_qp_data.QPointXYZ(qp)) *
90 vol_qp_data.ShapeValue(i, qp) *
91 vol_qp_data.ShapeValue(j, qp) *
92 vol_qp_data.JxW(qp); //M-matrix
93 }// for qp
94 }// for j
95
96 for (const auto& qp : vol_qp_data.QuadraturePointIndices())
97 {
98 IntV_shapeI[i]
99 += swf(vol_qp_data.QPointXYZ(qp)) *
100 vol_qp_data.ShapeValue(i, qp) * vol_qp_data.JxW(qp);
101 }// for qp
102 }//for i
103
104 // surface integrals
105 for (size_t f = 0; f < cell_num_faces; ++f)
106 {
107 const auto faces_qp_data = cell_mapping.MakeSurfaceQuadraturePointData(f);
108 IntS_shapeI_shapeJ[f].resize(cell_num_nodes, VecDbl(cell_num_nodes));
109 IntS_shapeI[f].resize(cell_num_nodes);
110 IntS_shapeI_gradshapeJ[f].resize(cell_num_nodes, VecVec3(cell_num_nodes));
111
112 for (unsigned int i = 0; i < cell_num_nodes; ++i)
113 {
114 for (unsigned int j = 0; j < cell_num_nodes; ++j)
115 {
116 for (const auto& qp : faces_qp_data.QuadraturePointIndices())
117 {
118 IntS_shapeI_shapeJ[f][i][j]
119 += swf(faces_qp_data.QPointXYZ(qp)) *
120 faces_qp_data.ShapeValue(i, qp) *
121 faces_qp_data.ShapeValue(j, qp) *
122 faces_qp_data.JxW(qp);
123 IntS_shapeI_gradshapeJ[f][i][j]
124 += swf(faces_qp_data.QPointXYZ(qp)) *
125 faces_qp_data.ShapeValue(i, qp) *
126 faces_qp_data.ShapeGrad(j, qp) *
127 faces_qp_data.JxW(qp);
128 }// for qp
129 }//for j
130
131 for (const auto& qp : faces_qp_data.QuadraturePointIndices())
132 {
133 IntS_shapeI[f][i]
134 += swf(faces_qp_data.QPointXYZ(qp)) *
135 faces_qp_data.ShapeValue(i, qp) * faces_qp_data.JxW(qp);
136 }// for qp
137 }//for i
138 }//for f
139
140 return
141 UnitCellMatrices{IntV_gradshapeI_gradshapeJ, //K-matrix
142 IntV_shapeI_gradshapeJ, //G-matrix
143 IntV_shapeI_shapeJ, //M-matrix
144 IntV_shapeI, //Vi-vectors
145
146 IntS_shapeI_shapeJ, //face M-matrices
147 IntS_shapeI_gradshapeJ, //face G-matrices
148 IntS_shapeI}; //face Si-vectors
149 };
150
151 const size_t num_local_cells = grid_ptr_->local_cells.size();
152 unit_cell_matrices_.resize(num_local_cells);
153
154 for (const auto& cell : grid_ptr_->local_cells)
155 unit_cell_matrices_[cell.local_id_] = ComputeCellUnitIntegrals(cell, *swf_ptr);
156
157 const auto ghost_ids = grid_ptr_->cells.GetGhostGlobalIDs();
158 for (uint64_t ghost_id : ghost_ids)
159 unit_ghost_cell_matrices_[ghost_id] =
160 ComputeCellUnitIntegrals(grid_ptr_->cells[ghost_id],*swf_ptr);
161
162 //============================================= Assessing global unit cell
163 // matrix storage
164 std::array<size_t,2> num_local_ucms = {unit_cell_matrices_.size(),
165 unit_ghost_cell_matrices_.size()};
166 std::array<size_t,2> num_globl_ucms = {0,0};
167
168 MPI_Allreduce(num_local_ucms.data(), //sendbuf
169 num_globl_ucms.data(), //recvbuf
170 2, MPIU_SIZE_T, //count+datatype
171 MPI_SUM, //operation
172 Chi::mpi.comm); //comm
173
174
175
177 Chi::log.Log()
178 << "Ghost cell unit cell-matrix ratio: "
179 << (double)num_globl_ucms[1]*100/(double)num_globl_ucms[0]
180 << "%";
181 Chi::log.Log()
182 << "Cell matrices computed. Process memory = "
183 << std::setprecision(3)
185}
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
static double GetMemoryUsageInMB()
Get current memory usage in Megabytes.
const MPI_Comm & comm
MPI communicator.
Definition: mpi_info.h:28
void Barrier() const
Definition: mpi_info.cc:38
static std::shared_ptr< PieceWiseLinearDiscontinuous > New(const chi_mesh::MeshContinuum &grid, QuadratureOrder q_order=QuadratureOrder::SECOND, CoordinateSystemType cs_type=CoordinateSystemType::CARTESIAN)
std::vector< CellFace > faces_
Definition: cell.h:82
uint64_t local_id_
Definition: cell.h:76
chi_mesh::MeshContinuumPtr grid_ptr_
Definition: lbs_solver.h:75
std::shared_ptr< chi_math::SpatialDiscretization > discretization_
Definition: lbs_solver.h:74
virtual void InitializeSpatialDiscretization()
std::vector< VecDbl > MatDbl
Definition: lbs_structs.h:19
std::vector< chi_mesh::Vector3 > VecVec3
Definition: lbs_structs.h:20
std::vector< double > VecDbl
Definition: lbs_structs.h:18
std::vector< VecVec3 > MatVec3
Definition: lbs_structs.h:21