DFT-FE 1.1.0-pre
Density Functional Theory With Finite-Elements
Loading...
Searching...
No Matches
pseudoUtils.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// @author Sambit Das
18//
19
20#ifndef pseudoUtils_H_
21#define pseudoUtils_H_
22
23#include <headers.h>
24#include <mpi.h>
25#include <boost/math/distributions/normal.hpp>
26#include <boost/math/special_functions/spherical_harmonic.hpp>
27#include <boost/random/normal_distribution.hpp>
28
29
30namespace dftfe
31{
32 namespace pseudoUtils
33 {
34 // some inline functions
35 inline void
36 exchangeLocalList(const std::vector<dftfe::uInt> &masterNodeIdList,
37 std::vector<dftfe::uInt> &globalMasterNodeIdList,
38 dftfe::uInt numMeshPartitions,
39 const MPI_Comm &mpi_communicator)
40 {
41 int numberMasterNodesOnLocalProc = masterNodeIdList.size();
42
43 int *masterNodeIdListSizes = new int[numMeshPartitions];
44
45 MPI_Allgather(&numberMasterNodesOnLocalProc,
46 1,
48 &numberMasterNodesOnLocalProc),
49 masterNodeIdListSizes,
50 1,
51 dftfe::dataTypes::mpi_type_id(masterNodeIdListSizes),
52 mpi_communicator);
53
54 dftfe::Int newMasterNodeIdListSize =
55 std::accumulate(&(masterNodeIdListSizes[0]),
56 &(masterNodeIdListSizes[numMeshPartitions]),
57 0);
58
59 globalMasterNodeIdList.resize(newMasterNodeIdListSize);
60
61 int *mpiOffsets = new int[numMeshPartitions];
62
63 mpiOffsets[0] = 0;
64
65 for (dftfe::Int i = 1; i < numMeshPartitions; ++i)
66 mpiOffsets[i] = masterNodeIdListSizes[i - 1] + mpiOffsets[i - 1];
67
68 MPI_Allgatherv(&(masterNodeIdList[0]),
69 numberMasterNodesOnLocalProc,
70 dftfe::dataTypes::mpi_type_id(masterNodeIdList.data()),
71 &(globalMasterNodeIdList[0]),
72 &(masterNodeIdListSizes[0]),
73 &(mpiOffsets[0]),
75 globalMasterNodeIdList.data()),
76 mpi_communicator);
77
78
79 delete[] masterNodeIdListSizes;
80 delete[] mpiOffsets;
81
82 return;
83 }
84
85
86 inline void
87 exchangeNumberingMap(std::map<dftfe::Int, dftfe::Int> &localMap,
88 dftfe::uInt numMeshPartitions,
89 const MPI_Comm &mpi_communicator)
90
91 {
92 std::map<dftfe::Int, dftfe::Int>::iterator iter;
93
94 std::vector<dftfe::Int> localSpreadVec;
95
96 iter = localMap.begin();
97 while (iter != localMap.end())
98 {
99 localSpreadVec.push_back(iter->first);
100 localSpreadVec.push_back(iter->second);
101
102 ++iter;
103 }
104 int localSpreadVecSize = localSpreadVec.size();
105
106 int *spreadVecSizes = new int[numMeshPartitions];
107
108 MPI_Allgather(&localSpreadVecSize,
109 1,
110 dftfe::dataTypes::mpi_type_id(&localSpreadVecSize),
111 spreadVecSizes,
112 1,
113 dftfe::dataTypes::mpi_type_id(spreadVecSizes),
114 mpi_communicator);
115
116 dftfe::Int globalSpreadVecSize =
117 std::accumulate(&(spreadVecSizes[0]),
118 &(spreadVecSizes[numMeshPartitions]),
119 0);
120
121 std::vector<dftfe::Int> globalSpreadVec(globalSpreadVecSize);
122
123 int *mpiOffsets = new int[numMeshPartitions];
124
125 mpiOffsets[0] = 0;
126
127 for (dftfe::Int i = 1; i < numMeshPartitions; ++i)
128 mpiOffsets[i] = spreadVecSizes[i - 1] + mpiOffsets[i - 1];
129
130 MPI_Allgatherv(&(localSpreadVec[0]),
131 localSpreadVecSize,
132 dftfe::dataTypes::mpi_type_id(localSpreadVec.data()),
133 &(globalSpreadVec[0]),
134 &(spreadVecSizes[0]),
135 &(mpiOffsets[0]),
136 dftfe::dataTypes::mpi_type_id(globalSpreadVec.data()),
137 mpi_communicator);
138
139 for (dftfe::Int i = 0; i < globalSpreadVecSize; i = i + 2)
140 localMap[globalSpreadVec[i]] = globalSpreadVec[i + 1];
141
142
143 delete[] spreadVecSizes;
144 delete[] mpiOffsets;
145
146 return;
147 }
148
149 inline void
150 getRadialFunctionVal(const double radialCoordinate,
151 double &splineVal,
152 const alglib::spline1dinterpolant *spline)
153 {
154 splineVal = alglib::spline1dcalc(*spline, radialCoordinate);
155 return;
156 }
157
158 inline void
159 getSphericalHarmonicVal(const double theta,
160 const double phi,
161 const dftfe::Int l,
162 const dftfe::Int m,
163 double &sphericalHarmonicVal)
164 {
165 if (m < 0)
166 sphericalHarmonicVal =
167 std::sqrt(2.0) * boost::math::spherical_harmonic_i(l, -m, theta, phi);
168
169 else if (m == 0)
170 sphericalHarmonicVal =
171 boost::math::spherical_harmonic_r(l, m, theta, phi);
172
173 else if (m > 0)
174 sphericalHarmonicVal =
175 std::sqrt(2.0) * boost::math::spherical_harmonic_r(l, m, theta, phi);
176
177 return;
178 }
179
180 inline void
182 double &r,
183 double &theta,
184 double &phi)
185 {
186 double tolerance = 1e-12;
187 r = std::sqrt(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]);
188
189 if (std::fabs(r - 0.0) <= tolerance)
190 {
191 theta = 0.0;
192 phi = 0.0;
193 }
194 else
195 {
196 theta = std::acos(x[2] / r);
197 //
198 // check if theta = 0 or PI (i.e, whether the point is on the Z-axis)
199 // If yes, assign phi = 0.0.
200 // NOTE: In case theta = 0 or PI, phi is undetermined. The actual
201 // value of phi doesn't matter in computing the enriched function
202 // value or its gradient. We assign phi = 0.0 here just as a dummy
203 // value
204 //
205 if (fabs(theta - 0.0) >= tolerance && fabs(theta - M_PI) >= tolerance)
206 phi = std::atan2(x[1], x[0]);
207 else
208 phi = 0.0;
209 }
210 }
211 } // namespace pseudoUtils
212} // namespace dftfe
213#endif
MPI_Datatype mpi_type_id(const int *)
Definition dftfeDataTypes.h:49
wrapper to convert pseudopotential file from upf to dftfe format and returns the nonlinear core corre...
Definition pseudoPotentialToDftfeConverter.cc:36
void getRadialFunctionVal(const double radialCoordinate, double &splineVal, const alglib::spline1dinterpolant *spline)
Definition pseudoUtils.h:150
void exchangeNumberingMap(std::map< dftfe::Int, dftfe::Int > &localMap, dftfe::uInt numMeshPartitions, const MPI_Comm &mpi_communicator)
Definition pseudoUtils.h:87
void convertCartesianToSpherical(double *x, double &r, double &theta, double &phi)
Definition pseudoUtils.h:181
void getSphericalHarmonicVal(const double theta, const double phi, const dftfe::Int l, const dftfe::Int m, double &sphericalHarmonicVal)
Definition pseudoUtils.h:159
void exchangeLocalList(const std::vector< dftfe::uInt > &masterNodeIdList, std::vector< dftfe::uInt > &globalMasterNodeIdList, dftfe::uInt numMeshPartitions, const MPI_Comm &mpi_communicator)
Definition pseudoUtils.h:36
Definition pseudoPotentialToDftfeConverter.cc:34
@ e
Definition ExcSSDFunctionalBaseClass.h:59
std::uint32_t uInt
Definition TypeConfig.h:10
std::int32_t Int
Definition TypeConfig.h:11