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 <unsigned int FEOrder, unsigned int 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<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<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 unsigned int offset,
128 const unsigned int matrixFreeQuadratureIdAX,
129 const dealii::AffineConstraints<double> &hangingPeriodicConstraintMatrix,
130 const std::vector<std::vector<double>> & imagePositions,
131 const std::vector<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<int>> & bQuadAtomIdsAllAtoms,
136 std::map<dealii::CellId, std::vector<int>> & bQuadAtomIdsAllAtomsImages,
137 std::map<dealii::CellId, std::vector<unsigned int>>
138 &bCellNonTrivialAtomIds,
139 std::vector<std::map<dealii::CellId, std::vector<unsigned int>>>
140 &bCellNonTrivialAtomIdsBins,
141 std::map<dealii::CellId, std::vector<unsigned int>>
142 &bCellNonTrivialAtomImageIds,
143 std::vector<std::map<dealii::CellId, std::vector<unsigned int>>>
144 & bCellNonTrivialAtomImageIdsBins,
145 const std::vector<double> &smearingWidths,
146 std::vector<double> & smearedChargeScaling,
147 const unsigned int smearedChargeQuadratureId,
148 const bool useSmearedCharges = false,
149 const bool isVselfPerturbationSolve = false);
150
151# ifdef DFTFE_WITH_DEVICE
152 /**
153 * @brief Solve nuclear electrostatic self-potential of atoms in each bin one-by-one
154 *
155 * @param[in] matrix_free_data MatrixFree object
156 * @param[in] offset MatrixFree object starting offset for vself bins solve
157 * @param[out] phiExt sum of the self-potential fields of all atoms and
158 * image atoms
159 * @param[in] phiExtConstraintMatrix constraintMatrix corresponding to
160 * phiExt
161 * @param[in] imagePositions image atoms positions data
162 * @param[in] imageIds image atoms Ids data
163 * @param[in] imageCharges image atoms charge values data *
164 * @param[out] localVselfs peak self-potential values of atoms in the local
165 * processor
166 */
167 void
168 solveVselfInBinsDevice(
169 const std::shared_ptr<
171 FEBasisOperations<double, double, dftfe::utils::MemorySpace::HOST>>
172 & basisOperationsPtr,
173 const unsigned int mfBaseDofHandlerIndex,
174 const unsigned int matrixFreeQuadratureIdAX,
175 const unsigned int offset,
176 const dftfe::utils::MemoryStorage<double,
178 &cellGradNIGradNJIntergralDevice,
179 const std::shared_ptr<
181 & BLASWrapperPtr,
182 const dealii::AffineConstraints<double> &hangingPeriodicConstraintMatrix,
183 const std::vector<std::vector<double>> & imagePositions,
184 const std::vector<int> & imageIds,
185 const std::vector<double> & imageCharges,
186 std::vector<std::vector<double>> & localVselfs,
187 std::map<dealii::CellId, std::vector<double>> &bQuadValuesAllAtoms,
188 std::map<dealii::CellId, std::vector<int>> & bQuadAtomIdsAllAtoms,
189 std::map<dealii::CellId, std::vector<int>> & bQuadAtomIdsAllAtomsImages,
190 std::map<dealii::CellId, std::vector<unsigned int>>
191 &bCellNonTrivialAtomIds,
192 std::vector<std::map<dealii::CellId, std::vector<unsigned int>>>
193 &bCellNonTrivialAtomIdsBins,
194 std::map<dealii::CellId, std::vector<unsigned int>>
195 &bCellNonTrivialAtomImageIds,
196 std::vector<std::map<dealii::CellId, std::vector<unsigned int>>>
197 & bCellNonTrivialAtomImageIdsBins,
198 const std::vector<double> &smearingWidths,
199 std::vector<double> & smearedChargeScaling,
200 const unsigned int smearedChargeQuadratureId,
201 const bool useSmearedCharges = false,
202 const bool isVselfPerturbationSolve = false);
203
204
205# endif
206
207 /**
208 * @brief Solve nuclear electrostatic self-potential of atoms in each bin in a perturbed domain (used for cell stress calculation)
209 *
210 */
211 void
213 const std::shared_ptr<
215 FEBasisOperations<double, double, dftfe::utils::MemorySpace::HOST>>
216 & basisOperationsPtr,
217 const unsigned int mfBaseDofHandlerIndex,
218 const unsigned int matrixFreeQuadratureIdAX,
219 const unsigned int offset,
220# ifdef DFTFE_WITH_DEVICE
221 const dftfe::utils::MemoryStorage<double,
223 &cellGradNIGradNJIntergralDevice,
224 const std::shared_ptr<
226 &BLASWrapperPtr,
227# endif
228 const dealii::AffineConstraints<double> &hangingPeriodicConstraintMatrix,
229 const std::vector<std::vector<double>> & imagePositions,
230 const std::vector<int> & imageIds,
231 const std::vector<double> & imageCharges,
232 const std::vector<double> & smearingWidths,
233 const unsigned int smearedChargeQuadratureId,
234 const bool useSmearedCharges = false);
235
236 /// get const reference map of binIds and atomIds
237 const std::map<int, std::set<int>> &
239
240 /// get const reference map of binIds and atomIds
241 const std::map<int, std::set<int>> &
243
244 /// get const reference to map of global dof index and vself solve boundary
245 /// flag in each bin
246 const std::vector<std::map<dealii::types::global_dof_index, int>> &
248
249 /// get const reference to map of global dof index and vself solve boundary
250 /// flag in each bin
251 const std::vector<std::map<dealii::types::global_dof_index, int>> &
253
254 /// get const reference to map of global dof index and vself field initial
255 /// value in each bin
256 const std::vector<std::map<dealii::types::global_dof_index, int>> &
258
259 /// get const reference to map of global dof index and vself field initial
260 /// value in each bin
261 const std::vector<
262 std::map<dealii::types::global_dof_index, dealii::Point<3>>> &
264
265 /// get const reference to solved vself fields
266 const std::vector<distributedCPUVec<double>> &
268
269 /// get const reference to del{vself}/del{R_i} fields
270 const std::vector<distributedCPUVec<double>> &
272
273 /// perturbation of vself solution field to be used to evaluate the
274 /// Gateaux derivative of vself field with respect to affine strain
275 /// components using central finite difference
276 const std::vector<distributedCPUVec<double>> &
278
279 /// get const reference to d_atomIdBinIdMapLocalAllImages
280 const std::map<unsigned int, unsigned int> &
282
283 /// get stored adaptive ball radius
284 double
286
287
288 private:
289 /**
290 * @brief locate underlying fem nodes for atoms in bins.
291 *
292 */
293 void
294 locateAtomsInBins(const dealii::DoFHandler<3> &dofHandler);
295
296 /**
297 * @brief sanity check for Dirichlet boundary conditions on the vself balls in each bin
298 *
299 */
300 void
302 const dealii::DoFHandler<3> & dofHandler,
303 const dealii::AffineConstraints<double> &onlyHangingNodeConstraints);
304
305 /// storage for input atomLocations argument in createAtomBins function
306 std::vector<std::vector<double>> d_atomLocations;
307
308 /// storage for optimized constraints handling object
311
312 /// vector of constraint matrices for vself bins
313 std::vector<dealii::AffineConstraints<double>> d_vselfBinConstraintMatrices;
314
315 /// map of binIds and atomIds
316 std::map<int, std::set<int>> d_bins;
317
318 /// map of binIds and atomIds and imageIds
319 std::map<int, std::set<int>> d_binsImages;
320
321 /// map of global dof index and vself solve boundary flag (chargeId or
322 // imageId+numberGlobalCharges) in each bin
323 std::vector<std::map<dealii::types::global_dof_index, int>> d_boundaryFlag;
324
325 /// map of global dof index and vself solve boundary flag (only chargeId)in
326 /// each bin
327 std::vector<std::map<dealii::types::global_dof_index, int>>
329
330 /// map of global dof index to location of closest charge
331 std::vector<std::map<dealii::types::global_dof_index, dealii::Point<3>>>
333
334 /// map of global dof index and vself field initial value in each bin
335 std::vector<std::map<dealii::types::global_dof_index, double>>
337
338 /// map of global dof index and vself field initial value in each bin
339 std::vector<std::map<dealii::types::global_dof_index, int>>
341
342 /// Internal data: stores the map of atom Id (only in the local processor)
343 /// to the vself bin Id. Populated in solve vself in Bins
344 std::map<unsigned int, unsigned int> d_atomIdBinIdMapLocalAllImages;
345
346 /// solved vself solution field for each bin
347 std::vector<distributedCPUVec<double>> d_vselfFieldBins;
348
349 /// solved del{vself}/del{R_i} solution field for each bin
350 std::vector<distributedCPUVec<double>> d_vselfFieldDerRBins;
351
352 /// perturbation of vself solution field to be used to evaluate the
353 /// Gateaux derivative of vself field with respect to affine strain
354 /// components using central finite difference
355 std::vector<distributedCPUVec<double>> d_vselfFieldPerturbedBins;
356
357 // std::vector<double> d_inhomoIdsColoredVecFlattened;
358
359 /// Map of locally relevant global dof index and the atomic charge in each
360 /// bin
361 std::vector<std::map<dealii::types::global_dof_index, double>> d_atomsInBin;
362
363 /// Vself ball radius. This is stored after the first call to createAtomBins
364 /// and reused for subsequent calls
366
368
369 const MPI_Comm d_mpiCommParent;
370 const MPI_Comm mpi_communicator;
371 const MPI_Comm d_mpiInterCommKpts;
372 const unsigned int n_mpi_processes;
373 const unsigned int this_mpi_process;
374 dealii::ConditionalOStream pcout;
375 };
376
377} // namespace dftfe
378#endif // vselfBinsManager_H_
Namespace which declares the input parameters and the functions to parse them from the input paramete...
Definition dftParameters.h:35
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:371
std::vector< std::map< dealii::types::global_dof_index, int > > d_boundaryFlagOnlyChargeId
Definition vselfBinsManager.h:328
std::vector< dealii::AffineConstraints< double > > d_vselfBinConstraintMatrices
vector of constraint matrices for vself bins
Definition vselfBinsManager.h:313
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< 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...
std::vector< std::map< dealii::types::global_dof_index, double > > d_atomsInBin
Definition vselfBinsManager.h:361
const std::vector< std::map< dealii::types::global_dof_index, int > > & getBoundaryFlagsBinsOnlyChargeId() const
const std::vector< std::map< dealii::types::global_dof_index, int > > & getBoundaryFlagsBins() const
std::map< int, std::set< int > > d_binsImages
map of binIds and atomIds and imageIds
Definition vselfBinsManager.h:319
double getStoredAdaptiveBallRadius() const
get stored adaptive ball radius
void solveVselfInBins(const std::shared_ptr< dftfe::basis::FEBasisOperations< double, double, dftfe::utils::MemorySpace::HOST > > &basisOperationsPtr, const unsigned int offset, const unsigned int matrixFreeQuadratureIdAX, const dealii::AffineConstraints< double > &hangingPeriodicConstraintMatrix, const std::vector< std::vector< double > > &imagePositions, const std::vector< 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< int > > &bQuadAtomIdsAllAtoms, std::map< dealii::CellId, std::vector< int > > &bQuadAtomIdsAllAtomsImages, std::map< dealii::CellId, std::vector< unsigned int > > &bCellNonTrivialAtomIds, std::vector< std::map< dealii::CellId, std::vector< unsigned int > > > &bCellNonTrivialAtomIdsBins, std::map< dealii::CellId, std::vector< unsigned int > > &bCellNonTrivialAtomImageIds, std::vector< std::map< dealii::CellId, std::vector< unsigned int > > > &bCellNonTrivialAtomImageIdsBins, const std::vector< double > &smearingWidths, std::vector< double > &smearedChargeScaling, const unsigned int smearedChargeQuadratureId, const bool useSmearedCharges=false, const bool isVselfPerturbationSolve=false)
Solve nuclear electrostatic self-potential of atoms in each bin one-by-one.
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:336
std::vector< distributedCPUVec< double > > d_vselfFieldPerturbedBins
Definition vselfBinsManager.h:355
dftUtils::constraintMatrixInfo< dftfe::utils::MemorySpace::HOST > d_constraintsOnlyHangingInfo
storage for optimized constraints handling object
Definition vselfBinsManager.h:310
std::map< int, std::set< int > > d_bins
map of binIds and atomIds
Definition vselfBinsManager.h:316
const std::vector< distributedCPUVec< double > > & getVselfFieldDerRBins() const
get const reference to del{vself}/del{R_i} fields
const std::map< int, std::set< int > > & getAtomImageIdsBins() const
get const reference map of binIds and atomIds
void locateAtomsInBins(const dealii::DoFHandler< 3 > &dofHandler)
locate underlying fem nodes for atoms in bins.
std::vector< std::map< dealii::types::global_dof_index, int > > d_boundaryFlag
map of global dof index and vself solve boundary flag (chargeId or
Definition vselfBinsManager.h:323
dealii::ConditionalOStream pcout
Definition vselfBinsManager.h:374
std::vector< std::map< dealii::types::global_dof_index, int > > d_closestAtomBin
map of global dof index and vself field initial value in each bin
Definition vselfBinsManager.h:340
const MPI_Comm d_mpiCommParent
Definition vselfBinsManager.h:369
const std::vector< distributedCPUVec< double > > & getPerturbedVselfFieldBins() const
const unsigned int n_mpi_processes
Definition vselfBinsManager.h:372
const dftParameters & d_dftParams
Definition vselfBinsManager.h:367
std::vector< distributedCPUVec< double > > d_vselfFieldDerRBins
solved del{vself}/del{R_i} solution field for each bin
Definition vselfBinsManager.h:350
double d_storedAdaptiveBallRadius
Definition vselfBinsManager.h:365
const std::vector< std::map< dealii::types::global_dof_index, int > > & getClosestAtomIdsBins() const
void solveVselfInBinsPerturbedDomain(const std::shared_ptr< dftfe::basis::FEBasisOperations< double, double, dftfe::utils::MemorySpace::HOST > > &basisOperationsPtr, const unsigned int mfBaseDofHandlerIndex, const unsigned int matrixFreeQuadratureIdAX, const unsigned int offset, const dealii::AffineConstraints< double > &hangingPeriodicConstraintMatrix, const std::vector< std::vector< double > > &imagePositions, const std::vector< int > &imageIds, const std::vector< double > &imageCharges, const std::vector< double > &smearingWidths, const unsigned int smearedChargeQuadratureId, const bool useSmearedCharges=false)
Solve nuclear electrostatic self-potential of atoms in each bin in a perturbed domain (used for cell ...
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
const std::map< int, std::set< int > > & getAtomIdsBins() const
get const reference map of binIds and atomIds
const std::map< unsigned int, unsigned int > & getAtomIdBinIdMapLocalAllImages() const
get const reference to d_atomIdBinIdMapLocalAllImages
std::vector< distributedCPUVec< double > > d_vselfFieldBins
solved vself solution field for each bin
Definition vselfBinsManager.h:347
std::vector< std::vector< double > > d_atomLocations
storage for input atomLocations argument in createAtomBins function
Definition vselfBinsManager.h:306
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< 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::map< unsigned int, unsigned int > d_atomIdBinIdMapLocalAllImages
Definition vselfBinsManager.h:344
const MPI_Comm mpi_communicator
Definition vselfBinsManager.h:370
const std::vector< std::map< dealii::types::global_dof_index, dealii::Point< 3 > > > & getClosestAtomLocationsBins() const
const unsigned int this_mpi_process
Definition vselfBinsManager.h:373
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:332
Definition FEBasisOperations.h:30
@ DEVICE
Definition MemorySpaceType.h:36
Definition pseudoPotentialToDftfeConverter.cc:34