DFT-FE 1.1.0-pre
Density Functional Theory With Finite-Elements
Loading...
Searching...
No Matches
poissonSolverProblemDevice.h
Go to the documentation of this file.
1// ---------------------------------------------------------------------
2//
3// Copyright (c) 2017-2025 The Regents of the University of Michigan and DFT-FE
4// authors.
5//
6// This file is part of the DFT-FE code.
7//
8// The DFT-FE code is free software; you can use it, redistribute
9// it, and/or modify it under the terms of the GNU Lesser General
10// Public License as published by the Free Software Foundation; either
11// version 2.1 of the License, or (at your option) any later version.
12// The full text of the license can be found in the file LICENSE at
13// the top level of the DFT-FE distribution.
14//
15// ---------------------------------------------------------------------
16//
17
18#if defined(DFTFE_WITH_DEVICE)
19# ifndef poissonSolverProblemDevice_H_
20# define poissonSolverProblemDevice_H_
21
23# include <constraintMatrixInfo.h>
24# include <constants.h>
25# include <dftUtils.h>
26# include <headers.h>
27# include "FEBasisOperations.h"
28# include "BLASWrapper.h"
29
30namespace dftfe
31{
32 /**
33 * @brief poisson solver problem device class template. template parameter FEOrderElectro
34 * is the finite element polynomial order. FEOrder template parameter is used
35 * in conjunction with FEOrderElectro to determine the order of the Gauss
36 * quadrature rule. The class should not be used with FLOATING NUCLEAR
37 * CHARGES = false or POINT WISE DIRICHLET CONSTRAINT = true
38 *
39 * @author Gourab Panigrahi
40 */
41 template <dftfe::uInt FEOrder, dftfe::uInt FEOrderElectro>
42 class poissonSolverProblemDevice : public linearSolverProblemDevice
43 {
44 public:
45 /// Constructor
46 poissonSolverProblemDevice(const MPI_Comm &mpi_comm);
47
48 /**
49 * @brief clears all datamembers and reset to original state.
50 *
51 *
52 */
53 void
54 clear();
55
56 /**
57 * @brief reinitialize data structures for total electrostatic potential solve.
58 *
59 * For Hartree electrostatic potential solve give an empty map to the atoms
60 * parameter.
61 *
62 */
63 void
64 reinit(
65 const std::shared_ptr<
66 dftfe::basis::
67 FEBasisOperations<double, double, dftfe::utils::MemorySpace::HOST>>
68 &basisOperationsPtr,
69 distributedCPUVec<double> &x,
70 const dealii::AffineConstraints<double> &constraintMatrix,
71 const dftfe::uInt matrixFreeVectorComponent,
72 const dftfe::uInt matrixFreeQuadratureComponentRhsDensity,
73 const dftfe::uInt matrixFreeQuadratureComponentAX,
74 const std::map<dealii::types::global_dof_index, double> &atoms,
75 const std::map<dealii::CellId, std::vector<double>> &smearedChargeValues,
76 const dftfe::uInt smearedChargeQuadratureId,
77 const dftfe::utils::MemoryStorage<double, dftfe::utils::MemorySpace::HOST>
78 &rhoValues,
79 const std::shared_ptr<
80 dftfe::linearAlgebra::BLASWrapper<dftfe::utils::MemorySpace::DEVICE>>
81 BLASWrapperPtr,
82 const bool isComputeDiagonalA = true,
83 const bool isComputeMeanValueConstraints = false,
84 const bool smearedNuclearCharges = false,
85 const bool isRhoValues = true,
86 const bool isGradSmearedChargeRhs = false,
87 const dftfe::uInt smearedChargeGradientComponentId = 0,
88 const bool storeSmearedChargeRhs = false,
89 const bool reuseSmearedChargeRhs = false,
90 const bool reinitializeFastConstraints = false);
91
92 /**
93 * @brief Compute A matrix multipled by x.
94 *
95 */
96 void
97 computeAX(distributedDeviceVec<double> &Ax,
98 distributedDeviceVec<double> &x);
99
100 /**
101 * @brief Compute right hand side vector for the problem Ax = rhs.
102 *
103 * @param rhs vector for the right hand side values
104 */
105 void
106 computeRhs(distributedCPUVec<double> &rhs);
107
108 /**
109 * @brief get the reference to x field
110 *
111 * @return reference to x field. Assumes x field data structure is already initialized
112 */
113 distributedDeviceVec<double> &
114 getX();
115
116 /**
117 * @brief get the reference to Preconditioner
118 *
119 * @return reference to Preconditioner
120 */
121 distributedDeviceVec<double> &
122 getPreconditioner();
123
124 /**
125 * @brief Copies x from Device to Host
126 *
127 */
128 void
129 copyXfromDeviceToHost();
130
131 /**
132 * @brief distribute x to the constrained nodes.
133 *
134 */
135 void
136 distributeX();
137
138 /// function needed by dealii to mimic SparseMatrix for Jacobi
139 /// preconditioning
140 void
141 subscribe(std::atomic<bool> *const validity,
142 const std::string &identifier = "") const {};
143
144 /// function needed by dealii to mimic SparseMatrix for Jacobi
145 /// preconditioning
146 void
147 unsubscribe(std::atomic<bool> *const validity,
148 const std::string &identifier = "") const {};
149
150 /// function needed by dealii to mimic SparseMatrix
151 bool
152 operator!=(double val) const
153 {
154 return true;
155 };
156
157 void
158 setX();
159
160
161 private:
162 /**
163 * @brief Sets up the matrixfree shapefunction, gradient, jacobian and map for matrixfree computeAX
164 *
165 */
166 void
167 setupMatrixFree();
168
169 /**
170 * @brief Sets up the constraints matrix
171 *
172 */
173 void
174 setupConstraints();
175
176 /**
177 * @brief Compute the diagonal of A.
178 *
179 */
180 void
181 computeDiagonalA();
182
183 /**
184 * @brief Compute mean value constraint which is required in case of fully periodic
185 * boundary conditions.
186 *
187 */
188 void
189 computeMeanValueConstraint();
190
191 /**
192 * @brief Mean value constraint distibute
193 *
194 */
195 void
196 meanValueConstraintDistribute(distributedDeviceVec<double> &vec) const;
197
198 /**
199 * @brief Mean value constraint distibute slave to master
200 *
201 */
202 void
203 meanValueConstraintDistributeSlaveToMaster(
204 distributedDeviceVec<double> &vec) const;
205
206 void
207 meanValueConstraintDistributeSlaveToMaster(
208 distributedCPUVec<double> &vec) const;
209
210 /**
211 * @brief Mean value constraint set zero
212 *
213 */
214 void
215 meanValueConstraintSetZero(distributedDeviceVec<double> &vec) const;
216
217 void
218 meanValueConstraintSetZero(distributedCPUVec<double> &vec) const;
219
220 /// storage for diagonal of the A matrix
221 distributedCPUVec<double> d_diagonalA;
222 distributedDeviceVec<double> d_diagonalAdevice;
223
224 /// storage for smeared charge rhs in case of total potential solve (doesn't
225 /// change every scf)
226 distributedCPUVec<double> d_rhsSmearedCharge;
227
228 /// pointer to dealii MatrixFree object
229 const dealii::MatrixFree<3, double> *d_matrixFreeDataPtr;
230
231 /// pointer to the x vector being solved for
232 distributedCPUVec<double> *d_xPtr;
233 distributedDeviceVec<double> d_xDevice;
234
235 // number of cells local to each mpi task, number of degrees of freedom
236 // locally owned and total degrees of freedom including ghost
237 dftfe::Int d_nLocalCells, d_xLocalDof, d_xLen;
238
239 // shape function value, gradient, jacobian and map for matrixfree
240 dftfe::utils::MemoryStorage<double, dftfe::utils::MemorySpace::DEVICE>
241 d_shapeFunction, d_jacobianFactor;
242 dftfe::utils::MemoryStorage<dftfe::Int, dftfe::utils::MemorySpace::DEVICE>
243 d_map;
244
245 // Pointers to shape function value, gradient, jacobian and map for
246 // matrixfree
247 double *d_shapeFunctionPtr;
248 double *d_jacobianFactorPtr;
249 dftfe::Int *d_mapPtr;
250
251
252 // constraints
253 dftUtils::constraintMatrixInfo<dftfe::utils::MemorySpace::DEVICE>
254 d_constraintsTotalPotentialInfo,
255 d_inhomogenousConstraintsTotalPotentialInfo;
256
257 /// pointer to dealii dealii::AffineConstraints<double> object
258 const dealii::AffineConstraints<double> *d_constraintMatrixPtr;
259
260 /// matrix free index required to access the DofHandler and
261 /// dealii::AffineConstraints<double> objects corresponding to the problem
262 dftfe::uInt d_matrixFreeVectorComponent;
263
264 /// matrix free quadrature index
265 dftfe::uInt d_matrixFreeQuadratureComponentRhsDensity;
266
267 /// matrix free quadrature index
268 dftfe::uInt d_matrixFreeQuadratureComponentAX;
269
270 /// pointer to electron density cell quadrature data
271 const dftfe::utils::MemoryStorage<double, dftfe::utils::MemorySpace::HOST>
272 *d_rhoValuesPtr;
273
274 /// pointer to smeared charge cell quadrature data
275 const std::map<dealii::CellId, std::vector<double>>
276 *d_smearedChargeValuesPtr;
277
278 ///
279 dftfe::uInt d_smearedChargeQuadratureId;
280
281 /// pointer to map between global dof index in current processor and the
282 /// atomic charge on that dof
283 const std::map<dealii::types::global_dof_index, double> *d_atomsPtr;
284
285 /// shape function gradient integral storage
286 std::vector<double> d_cellShapeFunctionGradientIntegralFlattened;
287
288 /// storage for mean value constraint vector
289 distributedCPUVec<double> d_meanValueConstraintVec;
290
291 /// storage for mean value constraint device vector
292 distributedDeviceVec<double> d_meanValueConstraintDeviceVec;
293
294 /// boolean flag to query if mean value constraint datastructures are
295 /// precomputed
296 bool d_isMeanValueConstraintComputed;
297
298 ///
299 bool d_isGradSmearedChargeRhs;
300
301 ///
302 bool d_isStoreSmearedChargeRhs;
303
304 ///
305 bool d_isReuseSmearedChargeRhs;
306
307 ///
308 dftfe::uInt d_smearedChargeGradientComponentId;
309
310 /// mean value constraints: mean value constrained node
311 dealii::types::global_dof_index d_meanValueConstraintNodeId;
312
313 /// mean value constrained node local id
314 dealii::types::global_dof_index d_meanValueConstraintNodeIdLocal;
315
316 /// mean value constraints: constrained proc id containing the mean value
317 /// constrained node
318 dftfe::uInt d_meanValueConstraintProcId;
319
320 /// duplicate constraints object with flattened maps for faster access
321 dftUtils::constraintMatrixInfo<dftfe::utils::MemorySpace::HOST>
322 d_constraintsInfo;
323 std::shared_ptr<
324 dftfe::basis::
325 FEBasisOperations<double, double, dftfe::utils::MemorySpace::HOST>>
326 d_basisOperationsPtr;
327 ///
328 std::shared_ptr<
329 dftfe::linearAlgebra::BLASWrapper<dftfe::utils::MemorySpace::DEVICE>>
330 d_BLASWrapperPtr;
331 bool d_isFastConstraintsInitialized;
332 bool d_isHomogenousConstraintsInitialized;
333
334 const MPI_Comm mpi_communicator;
335 const dftfe::uInt n_mpi_processes;
336 const dftfe::uInt this_mpi_process;
337 dealii::ConditionalOStream pcout;
338 };
339
340} // namespace dftfe
341# endif // poissonSolverProblemDevice_H_
342#endif
Definition pseudoPotentialToDftfeConverter.cc:34
std::uint32_t uInt
Definition TypeConfig.h:10
std::int32_t Int
Definition TypeConfig.h:11