DFT-FE 1.1.0-pre
Density Functional Theory With Finite-Elements
Loading...
Searching...
No Matches
vselfBinsManager.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#include <headers.h>
20#include "dftParameters.h"
21#include "FEBasisOperations.h"
22
23#ifndef vselfBinsManager_H_
24# define vselfBinsManager_H_
25
26namespace dftfe
27{
28 /**
29 * @brief Categorizes atoms into bins for efficient solution of nuclear electrostatic self-potential.
30 * template parameter FEOrderElectro is the finite element polynomial order.
31 *
32 * @author Sambit Das, Phani Motamarri
33 */
34 template <dftfe::uInt FEOrder, dftfe::uInt FEOrderElectro>
36 {
37 public:
38 /**
39 * @brief Constructor
40 *
41 * @param mpi_comm_parent parent mpi communicator
42 * @param mpi_comm_domain domain decomposition mpi communicator
43 */
44 vselfBinsManager(const MPI_Comm &mpi_comm_parent,
45 const MPI_Comm &mpi_comm_domain,
46 const MPI_Comm &mpi_intercomm_kpts,
47 const dftParameters &dftParams);
48
49
50 /**
51 * @brief Categorize atoms into bins based on self-potential ball radius
52 * around each atom such that no two atoms in each bin has overlapping
53 * balls.
54 *
55 * @param[out] constraintsVector constraintsVector to which the vself bins
56 * solve constraint matrices will be pushed back
57 * @param[in] dofHandler DofHandler object
58 * @param[in] constraintMatrix dealii::AffineConstraints<double> which was
59 * used for the total electrostatics solve
60 * @param[in] atomLocations global atom locations and charge values data
61 * @param[in] imagePositions image atoms positions data
62 * @param[in] imageIds image atoms Ids data
63 * @param[in] imageCharges image atoms charge values data
64 * @param[in] radiusAtomBall self-potential ball radius
65 */
66 void
68 std::vector<const dealii::AffineConstraints<double> *> &constraintsVector,
69 const dealii::AffineConstraints<double> &onlyHangingNodeConstraints,
70 const dealii::DoFHandler<3> &dofHandler,
71 const dealii::AffineConstraints<double> &constraintMatrix,
72 const std::vector<std::vector<double>> &atomLocations,
73 const std::vector<std::vector<double>> &imagePositions,
74 const std::vector<dftfe::Int> &imageIds,
75 const std::vector<double> &imageCharges,
76 const double radiusAtomBall);
77
78 /**
79 * @brief Categorize atoms into bins based on self-potential ball radius
80 * around each atom such that no two atoms in each bin has overlapping
81 * balls.
82 *
83 * @param[out] constraintsVector constraintsVector to which the vself bins
84 * solve constraint matrices will be pushed back
85 * @param[in] dofHandler DofHandler object
86 * @param[in] constraintMatrix dealii::AffineConstraints<double> which was
87 * used for the total electrostatics solve
88 * @param[in] atomLocations global atom locations and charge values data
89 * @param[in] imagePositions image atoms positions data
90 * @param[in] imageIds image atoms Ids data
91 * @param[in] imageCharges image atoms charge values data
92 */
93 void
95 std::vector<const dealii::AffineConstraints<double> *> &constraintsVector,
96 const dealii::AffineConstraints<double> &onlyHangingNodeConstraints,
97 const dealii::DoFHandler<3> &dofHandler,
98 const dealii::AffineConstraints<double> &constraintMatrix,
99 const std::vector<std::vector<double>> &atomLocations,
100 const std::vector<std::vector<double>> &imagePositions,
101 const std::vector<dftfe::Int> &imageIds,
102 const std::vector<double> &imageCharges,
103 const bool vselfPerturbationUpdateForStress = false);
104
105
106 /**
107 * @brief Solve nuclear electrostatic self-potential of atoms in each bin one-by-one
108 *
109 * @param[in] matrix_free_data MatrixFree object
110 * @param[in] offset MatrixFree object starting offset for vself bins solve
111 * @param[out] phiExt sum of the self-potential fields of all atoms and
112 * image atoms
113 * @param[in] phiExtConstraintMatrix constraintMatrix corresponding to
114 * phiExt
115 * @param[in] imagePositions image atoms positions data
116 * @param[in] imageIds image atoms Ids data
117 * @param[in] imageCharges image atoms charge values data *
118 * @param[out] localVselfs peak self-potential values of atoms in the local
119 * processor
120 */
121 void
123 const std::shared_ptr<
125 FEBasisOperations<double, double, dftfe::utils::MemorySpace::HOST>>
126 &basisOperationsPtr,
127 const dftfe::uInt offset,
128 const dftfe::uInt matrixFreeQuadratureIdAX,
129 const dealii::AffineConstraints<double> &hangingPeriodicConstraintMatrix,
130 const std::vector<std::vector<double>> &imagePositions,
131 const std::vector<dftfe::Int> &imageIds,
132 const std::vector<double> &imageCharges,
133 std::vector<std::vector<double>> &localVselfs,
134 std::map<dealii::CellId, std::vector<double>> &bQuadValuesAllAtoms,
135 std::map<dealii::CellId, std::vector<dftfe::Int>> &bQuadAtomIdsAllAtoms,
136 std::map<dealii::CellId, std::vector<dftfe::Int>>
137 &bQuadAtomIdsAllAtomsImages,
138 std::map<dealii::CellId, std::vector<dftfe::uInt>>
139 &bCellNonTrivialAtomIds,
140 std::vector<std::map<dealii::CellId, std::vector<dftfe::uInt>>>
141 &bCellNonTrivialAtomIdsBins,
142 std::map<dealii::CellId, std::vector<dftfe::uInt>>
143 &bCellNonTrivialAtomImageIds,
144 std::vector<std::map<dealii::CellId, std::vector<dftfe::uInt>>>
145 &bCellNonTrivialAtomImageIdsBins,
146 const std::vector<double> &smearingWidths,
147 std::vector<double> &smearedChargeScaling,
148 const dftfe::uInt smearedChargeQuadratureId,
149 const bool useSmearedCharges = false,
150 const bool isVselfPerturbationSolve = false);
151
152# ifdef DFTFE_WITH_DEVICE
153 /**
154 * @brief Solve nuclear electrostatic self-potential of atoms in each bin one-by-one
155 *
156 * @param[in] matrix_free_data MatrixFree object
157 * @param[in] offset MatrixFree object starting offset for vself bins solve
158 * @param[out] phiExt sum of the self-potential fields of all atoms and
159 * image atoms
160 * @param[in] phiExtConstraintMatrix constraintMatrix corresponding to
161 * phiExt
162 * @param[in] imagePositions image atoms positions data
163 * @param[in] imageIds image atoms Ids data
164 * @param[in] imageCharges image atoms charge values data *
165 * @param[out] localVselfs peak self-potential values of atoms in the local
166 * processor
167 */
168 void
169 solveVselfInBinsDevice(
170 const std::shared_ptr<
172 FEBasisOperations<double, double, dftfe::utils::MemorySpace::HOST>>
173 &basisOperationsPtr,
174 const dftfe::uInt mfBaseDofHandlerIndex,
175 const dftfe::uInt matrixFreeQuadratureIdAX,
176 const dftfe::uInt offset,
177 const dftfe::utils::MemoryStorage<double,
179 &cellGradNIGradNJIntergralDevice,
180 const std::shared_ptr<
182 &BLASWrapperPtr,
183 const dealii::AffineConstraints<double> &hangingPeriodicConstraintMatrix,
184 const std::vector<std::vector<double>> &imagePositions,
185 const std::vector<dftfe::Int> &imageIds,
186 const std::vector<double> &imageCharges,
187 std::vector<std::vector<double>> &localVselfs,
188 std::map<dealii::CellId, std::vector<double>> &bQuadValuesAllAtoms,
189 std::map<dealii::CellId, std::vector<dftfe::Int>> &bQuadAtomIdsAllAtoms,
190 std::map<dealii::CellId, std::vector<dftfe::Int>>
191 &bQuadAtomIdsAllAtomsImages,
192 std::map<dealii::CellId, std::vector<dftfe::uInt>>
193 &bCellNonTrivialAtomIds,
194 std::vector<std::map<dealii::CellId, std::vector<dftfe::uInt>>>
195 &bCellNonTrivialAtomIdsBins,
196 std::map<dealii::CellId, std::vector<dftfe::uInt>>
197 &bCellNonTrivialAtomImageIds,
198 std::vector<std::map<dealii::CellId, std::vector<dftfe::uInt>>>
199 &bCellNonTrivialAtomImageIdsBins,
200 const std::vector<double> &smearingWidths,
201 std::vector<double> &smearedChargeScaling,
202 const dftfe::uInt smearedChargeQuadratureId,
203 const bool useSmearedCharges = false,
204 const bool isVselfPerturbationSolve = false);
205
206
207# endif
208
209 /**
210 * @brief Solve nuclear electrostatic self-potential of atoms in each bin in a perturbed domain (used for cell stress calculation)
211 *
212 */
213 void
215 const std::shared_ptr<
217 FEBasisOperations<double, double, dftfe::utils::MemorySpace::HOST>>
218 &basisOperationsPtr,
219 const dftfe::uInt mfBaseDofHandlerIndex,
220 const dftfe::uInt matrixFreeQuadratureIdAX,
221 const dftfe::uInt offset,
222# ifdef DFTFE_WITH_DEVICE
223 const dftfe::utils::MemoryStorage<double,
225 &cellGradNIGradNJIntergralDevice,
226 const std::shared_ptr<
228 &BLASWrapperPtr,
229# endif
230 const dealii::AffineConstraints<double> &hangingPeriodicConstraintMatrix,
231 const std::vector<std::vector<double>> &imagePositions,
232 const std::vector<dftfe::Int> &imageIds,
233 const std::vector<double> &imageCharges,
234 const std::vector<double> &smearingWidths,
235 const dftfe::uInt smearedChargeQuadratureId,
236 const bool useSmearedCharges = false);
237
238 /// get const reference map of binIds and atomIds
239 const std::map<dftfe::Int, std::set<dftfe::Int>> &
241
242 /// get const reference map of binIds and atomIds
243 const std::map<dftfe::Int, std::set<dftfe::Int>> &
245
246 /// get const reference to map of global dof index and vself solve boundary
247 /// flag in each bin
248 const std::vector<std::map<dealii::types::global_dof_index, dftfe::Int>> &
250
251 /// get const reference to map of global dof index and vself solve boundary
252 /// flag in each bin
253 const std::vector<std::map<dealii::types::global_dof_index, dftfe::Int>> &
255
256 /// get const reference to map of global dof index and vself field initial
257 /// value in each bin
258 const std::vector<std::map<dealii::types::global_dof_index, dftfe::Int>> &
260
261 /// get const reference to map of global dof index and vself field initial
262 /// value in each bin
263 const std::vector<
264 std::map<dealii::types::global_dof_index, dealii::Point<3>>> &
266
267 /// get const reference to solved vself fields
268 const std::vector<distributedCPUVec<double>> &
270
271 /// get const reference to del{vself}/del{R_i} fields
272 const std::vector<distributedCPUVec<double>> &
274
275 /// perturbation of vself solution field to be used to evaluate the
276 /// Gateaux derivative of vself field with respect to affine strain
277 /// components using central finite difference
278 const std::vector<distributedCPUVec<double>> &
280
281 /// get const reference to d_atomIdBinIdMapLocalAllImages
282 const std::map<dftfe::uInt, dftfe::uInt> &
284
285 /// get stored adaptive ball radius
286 double
288
289
290 private:
291 /**
292 * @brief locate underlying fem nodes for atoms in bins.
293 *
294 */
295 void
296 locateAtomsInBins(const dealii::DoFHandler<3> &dofHandler);
297
298 /**
299 * @brief sanity check for Dirichlet boundary conditions on the vself balls in each bin
300 *
301 */
302 void
304 const dealii::DoFHandler<3> &dofHandler,
305 const dealii::AffineConstraints<double> &onlyHangingNodeConstraints);
306
307 /// storage for input atomLocations argument in createAtomBins function
308 std::vector<std::vector<double>> d_atomLocations;
309
310 /// storage for optimized constraints handling object
313
314 /// vector of constraint matrices for vself bins
315 std::vector<dealii::AffineConstraints<double>> d_vselfBinConstraintMatrices;
316
317 /// map of binIds and atomIds
318 std::map<dftfe::Int, std::set<dftfe::Int>> d_bins;
319
320 /// map of binIds and atomIds and imageIds
321 std::map<dftfe::Int, std::set<dftfe::Int>> d_binsImages;
322
323 /// map of global dof index and vself solve boundary flag (chargeId or
324 // imageId+numberGlobalCharges) in each bin
325 std::vector<std::map<dealii::types::global_dof_index, dftfe::Int>>
327
328 /// map of global dof index and vself solve boundary flag (only chargeId)in
329 /// each bin
330 std::vector<std::map<dealii::types::global_dof_index, dftfe::Int>>
332
333 /// map of global dof index to location of closest charge
334 std::vector<std::map<dealii::types::global_dof_index, dealii::Point<3>>>
336
337 /// map of global dof index and vself field initial value in each bin
338 std::vector<std::map<dealii::types::global_dof_index, double>>
340
341 /// map of global dof index and vself field initial value in each bin
342 std::vector<std::map<dealii::types::global_dof_index, dftfe::Int>>
344
345 /// Internal data: stores the map of atom Id (only in the local processor)
346 /// to the vself bin Id. Populated in solve vself in Bins
347 std::map<dftfe::uInt, dftfe::uInt> d_atomIdBinIdMapLocalAllImages;
348
349 /// solved vself solution field for each bin
350 std::vector<distributedCPUVec<double>> d_vselfFieldBins;
351
352 /// solved del{vself}/del{R_i} solution field for each bin
353 std::vector<distributedCPUVec<double>> d_vselfFieldDerRBins;
354
355 /// perturbation of vself solution field to be used to evaluate the
356 /// Gateaux derivative of vself field with respect to affine strain
357 /// components using central finite difference
358 std::vector<distributedCPUVec<double>> d_vselfFieldPerturbedBins;
359
360 // std::vector<double> d_inhomoIdsColoredVecFlattened;
361
362 /// Map of locally relevant global dof index and the atomic charge in each
363 /// bin
364 std::vector<std::map<dealii::types::global_dof_index, double>> d_atomsInBin;
365
366 /// Vself ball radius. This is stored after the first call to createAtomBins
367 /// and reused for subsequent calls
369
371
372 const MPI_Comm d_mpiCommParent;
373 const MPI_Comm mpi_communicator;
374 const MPI_Comm d_mpiInterCommKpts;
377 dealii::ConditionalOStream pcout;
378 };
379
380} // namespace dftfe
381#endif // vselfBinsManager_H_
Namespace which declares the input parameters and the functions to parse them from the input paramete...
Definition dftParameters.h:36
Overloads dealii's distribute and distribute_local_to_global functions associated with constraints cl...
Definition constraintMatrixInfo.h:43
Definition BLASWrapper.h:35
Definition MemoryStorage.h:33
const MPI_Comm d_mpiInterCommKpts
Definition vselfBinsManager.h:374
std::vector< dealii::AffineConstraints< double > > d_vselfBinConstraintMatrices
vector of constraint matrices for vself bins
Definition vselfBinsManager.h:315
std::vector< std::map< dealii::types::global_dof_index, double > > d_atomsInBin
Definition vselfBinsManager.h:364
void solveVselfInBins(const std::shared_ptr< dftfe::basis::FEBasisOperations< double, double, dftfe::utils::MemorySpace::HOST > > &basisOperationsPtr, const dftfe::uInt offset, const dftfe::uInt matrixFreeQuadratureIdAX, const dealii::AffineConstraints< double > &hangingPeriodicConstraintMatrix, const std::vector< std::vector< double > > &imagePositions, const std::vector< dftfe::Int > &imageIds, const std::vector< double > &imageCharges, std::vector< std::vector< double > > &localVselfs, std::map< dealii::CellId, std::vector< double > > &bQuadValuesAllAtoms, std::map< dealii::CellId, std::vector< dftfe::Int > > &bQuadAtomIdsAllAtoms, std::map< dealii::CellId, std::vector< dftfe::Int > > &bQuadAtomIdsAllAtomsImages, std::map< dealii::CellId, std::vector< dftfe::uInt > > &bCellNonTrivialAtomIds, std::vector< std::map< dealii::CellId, std::vector< dftfe::uInt > > > &bCellNonTrivialAtomIdsBins, std::map< dealii::CellId, std::vector< dftfe::uInt > > &bCellNonTrivialAtomImageIds, std::vector< std::map< dealii::CellId, std::vector< dftfe::uInt > > > &bCellNonTrivialAtomImageIdsBins, const std::vector< double > &smearingWidths, std::vector< double > &smearedChargeScaling, const dftfe::uInt smearedChargeQuadratureId, const bool useSmearedCharges=false, const bool isVselfPerturbationSolve=false)
Solve nuclear electrostatic self-potential of atoms in each bin one-by-one.
void solveVselfInBinsPerturbedDomain(const std::shared_ptr< dftfe::basis::FEBasisOperations< double, double, dftfe::utils::MemorySpace::HOST > > &basisOperationsPtr, const dftfe::uInt mfBaseDofHandlerIndex, const dftfe::uInt matrixFreeQuadratureIdAX, const dftfe::uInt offset, const dealii::AffineConstraints< double > &hangingPeriodicConstraintMatrix, const std::vector< std::vector< double > > &imagePositions, const std::vector< dftfe::Int > &imageIds, const std::vector< double > &imageCharges, const std::vector< double > &smearingWidths, const dftfe::uInt smearedChargeQuadratureId, const bool useSmearedCharges=false)
Solve nuclear electrostatic self-potential of atoms in each bin in a perturbed domain (used for cell ...
const dftfe::uInt this_mpi_process
Definition vselfBinsManager.h:376
std::map< dftfe::uInt, dftfe::uInt > d_atomIdBinIdMapLocalAllImages
Definition vselfBinsManager.h:347
const std::vector< std::map< dealii::types::global_dof_index, dftfe::Int > > & getBoundaryFlagsBinsOnlyChargeId() const
void createAtomBins(std::vector< const dealii::AffineConstraints< double > * > &constraintsVector, const dealii::AffineConstraints< double > &onlyHangingNodeConstraints, const dealii::DoFHandler< 3 > &dofHandler, const dealii::AffineConstraints< double > &constraintMatrix, const std::vector< std::vector< double > > &atomLocations, const std::vector< std::vector< double > > &imagePositions, const std::vector< dftfe::Int > &imageIds, const std::vector< double > &imageCharges, const double radiusAtomBall)
Categorize atoms into bins based on self-potential ball radius around each atom such that no two atom...
const std::vector< std::map< dealii::types::global_dof_index, dftfe::Int > > & getBoundaryFlagsBins() const
double getStoredAdaptiveBallRadius() const
get stored adaptive ball radius
std::vector< std::map< dealii::types::global_dof_index, double > > d_vselfBinField
map of global dof index and vself field initial value in each bin
Definition vselfBinsManager.h:339
std::vector< distributedCPUVec< double > > d_vselfFieldPerturbedBins
Definition vselfBinsManager.h:358
dftUtils::constraintMatrixInfo< dftfe::utils::MemorySpace::HOST > d_constraintsOnlyHangingInfo
storage for optimized constraints handling object
Definition vselfBinsManager.h:312
std::vector< std::map< dealii::types::global_dof_index, dftfe::Int > > d_closestAtomBin
map of global dof index and vself field initial value in each bin
Definition vselfBinsManager.h:343
const std::vector< distributedCPUVec< double > > & getVselfFieldDerRBins() const
get const reference to del{vself}/del{R_i} fields
void locateAtomsInBins(const dealii::DoFHandler< 3 > &dofHandler)
locate underlying fem nodes for atoms in bins.
std::vector< std::map< dealii::types::global_dof_index, dftfe::Int > > d_boundaryFlagOnlyChargeId
Definition vselfBinsManager.h:331
dealii::ConditionalOStream pcout
Definition vselfBinsManager.h:377
const dftfe::uInt n_mpi_processes
Definition vselfBinsManager.h:375
const MPI_Comm d_mpiCommParent
Definition vselfBinsManager.h:372
const std::map< dftfe::Int, std::set< dftfe::Int > > & getAtomImageIdsBins() const
get const reference map of binIds and atomIds
const std::vector< distributedCPUVec< double > > & getPerturbedVselfFieldBins() const
const std::map< dftfe::uInt, dftfe::uInt > & getAtomIdBinIdMapLocalAllImages() const
get const reference to d_atomIdBinIdMapLocalAllImages
const dftParameters & d_dftParams
Definition vselfBinsManager.h:370
const std::map< dftfe::Int, std::set< dftfe::Int > > & getAtomIdsBins() const
get const reference map of binIds and atomIds
std::vector< distributedCPUVec< double > > d_vselfFieldDerRBins
solved del{vself}/del{R_i} solution field for each bin
Definition vselfBinsManager.h:353
std::map< dftfe::Int, std::set< dftfe::Int > > d_binsImages
map of binIds and atomIds and imageIds
Definition vselfBinsManager.h:321
double d_storedAdaptiveBallRadius
Definition vselfBinsManager.h:368
vselfBinsManager(const MPI_Comm &mpi_comm_parent, const MPI_Comm &mpi_comm_domain, const MPI_Comm &mpi_intercomm_kpts, const dftParameters &dftParams)
Constructor.
const std::vector< distributedCPUVec< double > > & getVselfFieldBins() const
get const reference to solved vself fields
void createAtomBinsSanityCheck(const dealii::DoFHandler< 3 > &dofHandler, const dealii::AffineConstraints< double > &onlyHangingNodeConstraints)
sanity check for Dirichlet boundary conditions on the vself balls in each bin
std::vector< distributedCPUVec< double > > d_vselfFieldBins
solved vself solution field for each bin
Definition vselfBinsManager.h:350
std::vector< std::vector< double > > d_atomLocations
storage for input atomLocations argument in createAtomBins function
Definition vselfBinsManager.h:308
const MPI_Comm mpi_communicator
Definition vselfBinsManager.h:373
const std::vector< std::map< dealii::types::global_dof_index, dealii::Point< 3 > > > & getClosestAtomLocationsBins() const
const std::vector< std::map< dealii::types::global_dof_index, dftfe::Int > > & getClosestAtomIdsBins() const
std::vector< std::map< dealii::types::global_dof_index, dealii::Point< 3 > > > d_dofClosestChargeLocationMap
map of global dof index to location of closest charge
Definition vselfBinsManager.h:335
void updateBinsBc(std::vector< const dealii::AffineConstraints< double > * > &constraintsVector, const dealii::AffineConstraints< double > &onlyHangingNodeConstraints, const dealii::DoFHandler< 3 > &dofHandler, const dealii::AffineConstraints< double > &constraintMatrix, const std::vector< std::vector< double > > &atomLocations, const std::vector< std::vector< double > > &imagePositions, const std::vector< dftfe::Int > &imageIds, const std::vector< double > &imageCharges, const bool vselfPerturbationUpdateForStress=false)
Categorize atoms into bins based on self-potential ball radius around each atom such that no two atom...
std::vector< std::map< dealii::types::global_dof_index, dftfe::Int > > d_boundaryFlag
map of global dof index and vself solve boundary flag (chargeId or
Definition vselfBinsManager.h:326
std::map< dftfe::Int, std::set< dftfe::Int > > d_bins
map of binIds and atomIds
Definition vselfBinsManager.h:318
Definition FEBasisOperations.h:30
@ DEVICE
Definition MemorySpaceType.h:36
Definition pseudoPotentialToDftfeConverter.cc:34
std::uint32_t uInt
Definition TypeConfig.h:10