DFT-FE 1.1.0-pre
Density Functional Theory With Finite-Elements
Loading...
Searching...
No Matches
dftUtils.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
19#ifndef dftUtils_H_
20#define dftUtils_H_
21
22#include <headers.h>
23#include <mpi.h>
24
25namespace dftfe
26{
27 /**
28 * @brief Contains repeatedly used functions in the KSDFT calculations
29 *
30 * @author Sambit Das, Krishnendu Ghosh, Phani Motamarri
31 */
32
33 namespace dftUtils
34 {
35 extern "C"
36 {
37 //
38 // lapack Ax=b
39 //
40 void
41 dgesv_(int * N,
42 int * NRHS,
43 double *A,
44 int * LDA,
45 int * IPIV,
46 double *B,
47 int * LDB,
48 int * INFO);
49 }
50
51 inline double
52 smearedCharge(double r, double rc)
53 {
54 double val;
55 if (r > rc)
56 {
57 val = 0.0;
58 }
59 else
60 {
61 val = -21.0 * pow(r - rc, 3.0) *
62 (6.0 * r * r + 3.0 * r * rc + rc * rc) /
63 (5.0 * M_PI * pow(rc, 8.0));
64 }
65 return val;
66 }
67
68 inline double
69 smearedChargeDr(double r, double rc)
70 {
71 double val;
72 if (r > rc)
73 {
74 val = 0.0;
75 }
76 else
77 {
78 val =
79 (-63.0 * pow(r - rc, 2.0) * (6.0 * r * r + 3.0 * r * rc + rc * rc) -
80 63.0 * pow(r - rc, 3.0) * (4.0 * r + rc)) /
81 (5.0 * M_PI * pow(rc, 8.0));
82 }
83 return val;
84 }
85
86 inline double
87 smearedPot(double r, double rc)
88 {
89 double val;
90 if (r > rc)
91 {
92 val = 1.0 / r;
93 }
94 else
95 {
96 val = (9.0 * pow(r, 7.0) - 30.0 * pow(r, 6.0) * rc +
97 28.0 * pow(r, 5.0) * pow(rc, 2.0) -
98 14.0 * pow(r, 2.0) * pow(rc, 5) + 12.0 * pow(rc, 7)) /
99 (5.0 * pow(rc, 8.0));
100 }
101 return val;
102 }
103
104 // derivative w.r.t r
105 inline double
106 smearedPotDr(double r, double rc)
107 {
108 double val;
109 if (r > rc)
110 {
111 val = -1.0 / pow(r, 2.0);
112 }
113 else
114 {
115 val = (63.0 * pow(r, 6.0) - 180.0 * pow(r, 5.0) * rc +
116 140.0 * pow(r, 4.0) * pow(rc, 2.0) -
117 28.0 * pow(r, 1.0) * pow(rc, 5)) /
118 (5.0 * pow(rc, 8.0));
119 }
120 return val;
121 }
122
123 inline std::vector<double>
125 const std::vector<double> &latticeVectorsFlattened,
126 const std::vector<double> &coordWithRespectToCellCorner)
127 {
128 std::vector<double> latticeVectorsDup = latticeVectorsFlattened;
129 std::vector<double> coordDup = coordWithRespectToCellCorner;
130 //
131 // to get the fractionalCoords, solve a linear
132 // system of equations
133 //
134 int N = 3;
135 int NRHS = 1;
136 int LDA = 3;
137 int IPIV[3];
138 int info;
139
140 dgesv_(&N,
141 &NRHS,
142 &latticeVectorsDup[0],
143 &LDA,
144 &IPIV[0],
145 &coordDup[0],
146 &LDA,
147 &info);
148 AssertThrow(info == 0,
149 dealii::ExcMessage(
150 "LU solve in finding fractional coordinates failed."));
151 return coordDup;
152 }
153
154 /** @brief Calculates value of composite generator
155 *
156 */
157 double
159 const double r,
160 const double a0,
161 const double power);
162
163 /** @brief Create bounding box around a sphere.
164 *
165 * @param sphere center
166 * @param sphere radius
167 * @return bounding box
168 */
169 dealii::BoundingBox<3>
170 createBoundingBoxForSphere(const dealii::Point<3> &center,
171 const double sphereRadius);
172
173 /** @brief Calculates partial occupancy of the atomic orbital using
174 * Fermi-Dirac smearing.
175 *
176 * @param eigenValue
177 * @param fermiEnergy
178 * @param kb Boltzmann constant
179 * @param T smearing temperature
180 * @return double The partial occupancy of the orbital
181 */
182 double
183 getPartialOccupancy(const double eigenValue,
184 const double fermiEnergy,
185 const double kb,
186 const double T);
187
188 /** @brief Calculates the derivative of the partial occupancy of the atomic orbital
189 * with respect to (x=eigenvalue-fermiEnergy) using Fermi-Dirac smearing.
190 *
191 * @param eigenValue
192 * @param fermiEnergy
193 * @param kb Boltzmann constant
194 * @param T smearing temperature
195 * @return double The partial occupancy derivative of the orbital
196 */
197 double
198 getPartialOccupancyDer(const double eigenValue,
199 const double fermiEnergy,
200 const double kb,
201 const double T);
202
203 /** @brief Calculates cross product of two vectors
204 *
205 * @param a first vector
206 * @param b second vector
207 * @param crossProductVector cross product of a and b
208 * @return void
209 */
210 void
211 cross_product(const std::vector<double> &a,
212 const std::vector<double> &b,
213 std::vector<double> & crossProductVector);
214
215
216 /** @brief Applies an affine transformation to the domain bounding vectors
217 *
218 * @param d_domainBoundingVectors the bounding vectors of the domain given as a 2d array
219 * @param deformationGradient
220 * @return void.
221 */
222 void
224 std::vector<std::vector<double>> & domainBoundingVectors,
225 const dealii::Tensor<2, 3, double> &deformationGradient);
226
227 /** @brief Writes to vtu file only from the lowest pool id
228 *
229 * @param dataOut DataOut class object
230 * @param mpiCommParent parent mpi communicator
231 * @param mpiCommDomain mpi communicator of domain decomposition inside each pool
232 * @param interpoolcomm mpi communicator across k point pools
233 * @param interBandGroupComm mpi communicator across band groups
234 * @param fileName
235 */
236 void
237 writeDataVTUParallelLowestPoolId(const dealii::DoFHandler<3> &dofHandler,
238 const dealii::DataOut<3> & dataOut,
239 const MPI_Comm & mpiCommParent,
240 const MPI_Comm & mpiCommDomain,
241 const MPI_Comm & interpoolcomm,
242 const MPI_Comm & interBandGroupComm,
243 const std::string &folderName,
244 const std::string &fileName);
245
246 /** @brief Create index vector which is used for band parallelization
247 *
248 * @[in]param interBandGroupComm mpi communicator across band groups
249 * @[in]param numBands
250 * @[out]param bandGroupLowHighPlusOneIndices
251 */
252 void
254 const MPI_Comm & interBandGroupComm,
255 const unsigned int numBands,
256 std::vector<unsigned int> &bandGroupLowHighPlusOneIndices);
257
258 void
260 const MPI_Comm & interKptPoolComm,
261 const int numberIndices,
262 std::vector<int> &kptGroupLowHighPlusOneIndices);
263
264
265 /** @brief Wrapper to print current memory usage (prints only the maximum across mpiComm)
266 * using PetscMemoryGetCurrentUsage
267 *
268 * @[in]param mpiComm mpi communicator across which the memory printing
269 * will be synchronized
270 * @[in]param message message to be printed alongwith the memory usage
271 */
272 void
273 printCurrentMemoryUsage(const MPI_Comm &mpiComm, const std::string message);
274
275 /**
276 * A class to split the given communicator into a number of pools
277 */
278 class Pool
279 {
280 public:
281 Pool(const MPI_Comm & mpi_communicator,
282 const unsigned int n_pools,
283 const int verbosity);
284
285 /**
286 * @brief get the communicator across the processor groups
287 */
288 MPI_Comm &
290
291 /**
292 * @brief get the communicator associated with processor group
293 */
294 MPI_Comm &
296
297 private:
300 };
301
302 /// Exception handler for not implemented functionality
304 ExcNotImplementedYet,
305 "This functionality is not implemented yet or not needed to be implemented.");
306
307 /// Exception handler for DFT-FE internal error
308 DeclExceptionMsg(ExcInternalError, "DFT-FE internal error.");
309 } // namespace dftUtils
310
311} // namespace dftfe
312#endif
MPI_Comm & get_intrapool_comm()
get the communicator associated with processor group
MPI_Comm & get_interpool_comm()
get the communicator across the processor groups
MPI_Comm interpoolcomm
Definition dftUtils.h:298
Pool(const MPI_Comm &mpi_communicator, const unsigned int n_pools, const int verbosity)
MPI_Comm intrapoolcomm
Definition dftUtils.h:299
Contains repeatedly used functions in the KSDFT calculations.
Definition CompositeData.h:29
double smearedPot(double r, double rc)
Definition dftUtils.h:87
void transformDomainBoundingVectors(std::vector< std::vector< double > > &domainBoundingVectors, const dealii::Tensor< 2, 3, double > &deformationGradient)
Applies an affine transformation to the domain bounding vectors.
double smearedPotDr(double r, double rc)
Definition dftUtils.h:106
void dgesv_(int *N, int *NRHS, double *A, int *LDA, int *IPIV, double *B, int *LDB, int *INFO)
double getPartialOccupancy(const double eigenValue, const double fermiEnergy, const double kb, const double T)
Calculates partial occupancy of the atomic orbital using Fermi-Dirac smearing.
double getCompositeGeneratorVal(const double rc, const double r, const double a0, const double power)
Calculates value of composite generator.
double smearedChargeDr(double r, double rc)
Definition dftUtils.h:69
void createBandParallelizationIndices(const MPI_Comm &interBandGroupComm, const unsigned int numBands, std::vector< unsigned int > &bandGroupLowHighPlusOneIndices)
Create index vector which is used for band parallelization.
dealii::BoundingBox< 3 > createBoundingBoxForSphere(const dealii::Point< 3 > &center, const double sphereRadius)
Create bounding box around a sphere.
void createKpointParallelizationIndices(const MPI_Comm &interKptPoolComm, const int numberIndices, std::vector< int > &kptGroupLowHighPlusOneIndices)
void printCurrentMemoryUsage(const MPI_Comm &mpiComm, const std::string message)
Wrapper to print current memory usage (prints only the maximum across mpiComm) using PetscMemoryGetCu...
void writeDataVTUParallelLowestPoolId(const dealii::DoFHandler< 3 > &dofHandler, const dealii::DataOut< 3 > &dataOut, const MPI_Comm &mpiCommParent, const MPI_Comm &mpiCommDomain, const MPI_Comm &interpoolcomm, const MPI_Comm &interBandGroupComm, const std::string &folderName, const std::string &fileName)
Writes to vtu file only from the lowest pool id.
double smearedCharge(double r, double rc)
Definition dftUtils.h:52
double getPartialOccupancyDer(const double eigenValue, const double fermiEnergy, const double kb, const double T)
Calculates the derivative of the partial occupancy of the atomic orbital with respect to (x=eigenvalu...
void cross_product(const std::vector< double > &a, const std::vector< double > &b, std::vector< double > &crossProductVector)
Calculates cross product of two vectors.
DeclExceptionMsg(ExcNotImplementedYet, "This functionality is not implemented yet or not needed to be implemented.")
Exception handler for not implemented functionality.
std::vector< double > getFractionalCoordinates(const std::vector< double > &latticeVectorsFlattened, const std::vector< double > &coordWithRespectToCellCorner)
Definition dftUtils.h:124
Definition pseudoPotentialToDftfeConverter.cc:34
@ LDA
Definition ExcSSDFunctionalBaseClass.h:29