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 <unsigned int FEOrder, unsigned int 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 unsigned int matrixFreeVectorComponent,
72 const unsigned int matrixFreeQuadratureComponentRhsDensity,
73 const unsigned int matrixFreeQuadratureComponentAX,
74 const std::map<dealii::types::global_dof_index, double> &atoms,
75 const std::map<dealii::CellId, std::vector<double>> &smearedChargeValues,
76 const unsigned int 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 unsigned int 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 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<int, dftfe::utils::MemorySpace::DEVICE> d_map;
243
244 // Pointers to shape function value, gradient, jacobian and map for
245 // matrixfree
246 double *d_shapeFunctionPtr;
247 double *d_jacobianFactorPtr;
248 int * d_mapPtr;
249
250
251 // constraints
252 dftUtils::constraintMatrixInfo<dftfe::utils::MemorySpace::DEVICE>
253 d_constraintsTotalPotentialInfo,
254 d_inhomogenousConstraintsTotalPotentialInfo;
255
256 /// pointer to dealii dealii::AffineConstraints<double> object
257 const dealii::AffineConstraints<double> *d_constraintMatrixPtr;
258
259 /// matrix free index required to access the DofHandler and
260 /// dealii::AffineConstraints<double> objects corresponding to the problem
261 unsigned int d_matrixFreeVectorComponent;
262
263 /// matrix free quadrature index
264 unsigned int d_matrixFreeQuadratureComponentRhsDensity;
265
266 /// matrix free quadrature index
267 unsigned int d_matrixFreeQuadratureComponentAX;
268
269 /// pointer to electron density cell quadrature data
270 const dftfe::utils::MemoryStorage<double, dftfe::utils::MemorySpace::HOST>
271 *d_rhoValuesPtr;
272
273 /// pointer to smeared charge cell quadrature data
274 const std::map<dealii::CellId, std::vector<double>>
275 *d_smearedChargeValuesPtr;
276
277 ///
278 unsigned int d_smearedChargeQuadratureId;
279
280 /// pointer to map between global dof index in current processor and the
281 /// atomic charge on that dof
282 const std::map<dealii::types::global_dof_index, double> *d_atomsPtr;
283
284 /// shape function gradient integral storage
285 std::vector<double> d_cellShapeFunctionGradientIntegralFlattened;
286
287 /// storage for mean value constraint vector
288 distributedCPUVec<double> d_meanValueConstraintVec;
289
290 /// storage for mean value constraint device vector
291 distributedDeviceVec<double> d_meanValueConstraintDeviceVec;
292
293 /// boolean flag to query if mean value constraint datastructures are
294 /// precomputed
295 bool d_isMeanValueConstraintComputed;
296
297 ///
298 bool d_isGradSmearedChargeRhs;
299
300 ///
301 bool d_isStoreSmearedChargeRhs;
302
303 ///
304 bool d_isReuseSmearedChargeRhs;
305
306 ///
307 unsigned int d_smearedChargeGradientComponentId;
308
309 /// mean value constraints: mean value constrained node
310 dealii::types::global_dof_index d_meanValueConstraintNodeId;
311
312 /// mean value constrained node local id
313 dealii::types::global_dof_index d_meanValueConstraintNodeIdLocal;
314
315 /// mean value constraints: constrained proc id containing the mean value
316 /// constrained node
317 unsigned int d_meanValueConstraintProcId;
318
319 /// duplicate constraints object with flattened maps for faster access
320 dftUtils::constraintMatrixInfo<dftfe::utils::MemorySpace::HOST>
321 d_constraintsInfo;
322 std::shared_ptr<
323 dftfe::basis::
324 FEBasisOperations<double, double, dftfe::utils::MemorySpace::HOST>>
325 d_basisOperationsPtr;
326 ///
327 std::shared_ptr<
328 dftfe::linearAlgebra::BLASWrapper<dftfe::utils::MemorySpace::DEVICE>>
329 d_BLASWrapperPtr;
330 bool d_isFastConstraintsInitialized;
331 bool d_isHomogenousConstraintsInitialized;
332
333 const MPI_Comm mpi_communicator;
334 const unsigned int n_mpi_processes;
335 const unsigned int this_mpi_process;
336 dealii::ConditionalOStream pcout;
337 };
338
339} // namespace dftfe
340# endif // poissonSolverProblemDevice_H_
341#endif
Definition pseudoPotentialToDftfeConverter.cc:34