DFT-FE 1.1.0-pre
Density Functional Theory With Finite-Elements
Loading...
Searching...
No Matches
kerkerSolverProblemWrapper.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 kerkerSolverProblemWrapper_H_
20#define kerkerSolverProblemWrapper_H_
21#include <variant>
22#include <memory>
23#include <headers.h>
24#include <kerkerSolverProblem.h>
25#ifdef DFTFE_WITH_DEVICE
27#endif
28namespace dftfe
29{
30 using kerkerSolverProblemObject = std::variant<
31#define kerkerSolverProblemWrapperTemplates(T1) \
32 std::shared_ptr<kerkerSolverProblem<T1>>,
33#define kerkerSolverProblemWrapperTemplatesL(T1) \
34 std::shared_ptr<kerkerSolverProblem<T1>>
35#include "kerkerSolverProblemWrapper.def"
36#undef kerkerSolverProblemWrapperTemplates
37#undef kerkerSolverProblemWrapperTemplatesL
38 >;
39 template <class... Args>
41 createKerkerSolverProblemObject(dftfe::Int feOrder, Args &&...args)
42 {
43 switch (feOrder)
44 {
45#define kerkerSolverProblemWrapperTemplates(T1) \
46 case T1: \
47 return kerkerSolverProblemObject( \
48 std::make_shared<kerkerSolverProblem<T1>>(std::forward<Args>(args)...));
49#define kerkerSolverProblemWrapperTemplatesL(T1) \
50 case T1: \
51 return kerkerSolverProblemObject( \
52 std::make_shared<kerkerSolverProblem<T1>>(std::forward<Args>(args)...));
53#include "kerkerSolverProblemWrapper.def"
54#undef kerkerSolverProblemWrapperTemplates
55#undef kerkerSolverProblemWrapperTemplatesL
56 default:
57 throw std::logic_error{
58 "createKerkerSolverProblemObject dispatch failed"};
59 }
60 }
61
62
64 {
65 public:
66 /// Constructor
68 const MPI_Comm &mpi_comm_parent,
69 const MPI_Comm &mpi_comm_domain)
72 mpi_comm_parent,
73 mpi_comm_domain))
74 {}
75
78 {
79 return std::visit(
80 [](auto &t) -> distributedCPUVec<double> & { return t->getX(); },
82 }
83
84 void
86 {
87 std::visit([&](auto &t) { t->vmult(Ax, x); },
89 }
90
91 void
93 {
94 std::visit([&](auto &t) { t->computeRhs(rhs); },
96 }
97
98 void
100 const distributedCPUVec<double> &src,
101 const double omega) const
102 {
103 std::visit([&](
104 auto const &t) { t->precondition_Jacobi(dst, src, omega); },
106 }
107
108 void
110 {
111 std::visit([](auto &t) { t->distributeX(); },
113 }
114
115 void
116 subscribe(std::atomic<bool> *const validity,
117 const std::string &identifier = "") const
118 {}
119
120 void
121 unsubscribe(std::atomic<bool> *const validity,
122 const std::string &identifier = "") const
123 {}
124
125 bool
126 operator!=(double val) const
127 {
128 return true;
129 }
130
131 template <typename... Args>
132 void
133 reinit(Args &&...args)
134 {
135 std::visit([&](auto &t) { t->reinit(std::forward<Args>(args)...); },
137 }
138
139 template <typename... Args>
140 void
141 init(Args &&...args)
142 {
143 std::visit([&](auto &t) { t->init(std::forward<Args>(args)...); },
145 }
146
147 private:
149 };
150
151#ifdef DFTFE_WITH_DEVICE
152 using kerkerSolverProblemDeviceObject = std::variant<
153# define kerkerSolverProblemWrapperTemplates(T1) \
154 std::shared_ptr<kerkerSolverProblemDevice<T1>>,
155# define kerkerSolverProblemWrapperTemplatesL(T1) \
156 std::shared_ptr<kerkerSolverProblemDevice<T1>>
157# include "kerkerSolverProblemWrapper.def"
158# undef kerkerSolverProblemWrapperTemplates
159# undef kerkerSolverProblemWrapperTemplatesL
160 >;
161
162
163 template <class... Args>
164 inline kerkerSolverProblemDeviceObject
165 createKerkerSolverProblemDeviceObject(dftfe::Int feOrder, Args &&...args)
166 {
167 switch (feOrder)
168 {
169# define kerkerSolverProblemWrapperTemplates(T1) \
170 case T1: \
171 return kerkerSolverProblemDeviceObject{ \
172 std::make_shared<kerkerSolverProblemDevice<T1>>( \
173 std::forward<Args>(args)...)};
174# define kerkerSolverProblemWrapperTemplatesL(T1) \
175 case T1: \
176 return kerkerSolverProblemDeviceObject{ \
177 std::make_shared<kerkerSolverProblemDevice<T1>>( \
178 std::forward<Args>(args)...)};
179# include "kerkerSolverProblemWrapper.def"
180# undef kerkerSolverProblemWrapperTemplates
181# undef kerkerSolverProblemWrapperTemplatesL
182 default:
183 throw std::logic_error{
184 "createKerkerSolverProblemDeviceObject dispatch failed"};
185 }
186 }
187
188
189 class kerkerSolverProblemDeviceWrapperClass : public linearSolverProblemDevice
190 {
191 public:
192 /// Constructor
193 kerkerSolverProblemDeviceWrapperClass(const dftfe::Int feOrder,
194 const MPI_Comm &mpi_comm_parent,
195 const MPI_Comm &mpi_comm_domain)
196 : d_kerkerSolverProblemObject(
197 createKerkerSolverProblemDeviceObject(feOrder,
198 mpi_comm_parent,
199 mpi_comm_domain))
200 {}
201
202 distributedDeviceVec<double> &
203 getX()
204 {
205 return std::visit(
206 [](auto &t) -> distributedDeviceVec<double> & { return t->getX(); },
207 d_kerkerSolverProblemObject);
208 }
209
210 distributedDeviceVec<double> &
211 getPreconditioner()
212 {
213 return std::visit(
214 [](auto &t) -> distributedDeviceVec<double> & {
215 return t->getPreconditioner();
216 },
217 d_kerkerSolverProblemObject);
218 }
219
220 void
221 computeAX(distributedDeviceVec<double> &dst,
222 distributedDeviceVec<double> &src)
223 {
224 std::visit([&](auto &t) { t->computeAX(dst, src); },
225 d_kerkerSolverProblemObject);
226 }
227
228
229 void
230 computeRhs(distributedCPUVec<double> &rhs)
231 {
232 std::visit([&](auto &t) { t->computeRhs(rhs); },
233 d_kerkerSolverProblemObject);
234 }
235
236 void
237 setX()
238 {
239 std::visit([](auto &t) { t->setX(); }, d_kerkerSolverProblemObject);
240 }
241
242 void
243 distributeX()
244 {
245 std::visit([](auto &t) { t->distributeX(); },
246 d_kerkerSolverProblemObject);
247 }
248
249 void
250 copyXfromDeviceToHost()
251 {
252 std::visit([](auto &t) { t->copyXfromDeviceToHost(); },
253 d_kerkerSolverProblemObject);
254 }
255
256 template <typename... Args>
257 void
258 reinit(Args &&...args)
259 {
260 std::visit([&](auto &t) { t->reinit(std::forward<Args>(args)...); },
261 d_kerkerSolverProblemObject);
262 }
263
264 template <typename... Args>
265 void
266 init(Args &&...args)
267 {
268 std::visit([&](auto &t) { t->init(std::forward<Args>(args)...); },
269 d_kerkerSolverProblemObject);
270 }
271
272 private:
273 kerkerSolverProblemDeviceObject d_kerkerSolverProblemObject;
274 };
275
276#endif
277
278} // namespace dftfe
279
280#endif
kerkerSolverProblemObject d_kerkerSolverProblemObject
Definition kerkerSolverProblemWrapper.h:148
void reinit(Args &&...args)
Definition kerkerSolverProblemWrapper.h:133
distributedCPUVec< double > & getX()
get the reference to x field
Definition kerkerSolverProblemWrapper.h:77
void subscribe(std::atomic< bool > *const validity, const std::string &identifier="") const
Definition kerkerSolverProblemWrapper.h:116
void vmult(distributedCPUVec< double > &Ax, distributedCPUVec< double > &x)
Compute A matrix multipled by x.
Definition kerkerSolverProblemWrapper.h:85
bool operator!=(double val) const
function needed by dealii to mimic SparseMatrix
Definition kerkerSolverProblemWrapper.h:126
kerkerSolverProblemWrapperClass(const dftfe::Int feOrder, const MPI_Comm &mpi_comm_parent, const MPI_Comm &mpi_comm_domain)
Constructor.
Definition kerkerSolverProblemWrapper.h:67
void precondition_Jacobi(distributedCPUVec< double > &dst, const distributedCPUVec< double > &src, const double omega) const
Jacobi preconditioning function.
Definition kerkerSolverProblemWrapper.h:99
void unsubscribe(std::atomic< bool > *const validity, const std::string &identifier="") const
Definition kerkerSolverProblemWrapper.h:121
void init(Args &&...args)
Definition kerkerSolverProblemWrapper.h:141
void computeRhs(distributedCPUVec< double > &rhs)
Compute right hand side vector for the problem Ax = rhs.
Definition kerkerSolverProblemWrapper.h:92
void distributeX()
distribute x to the constrained nodes.
Definition kerkerSolverProblemWrapper.h:109
Definition pseudoPotentialToDftfeConverter.cc:34
kerkerSolverProblemObject createKerkerSolverProblemObject(dftfe::Int feOrder, Args &&...args)
Definition kerkerSolverProblemWrapper.h:57
std::variant< #define kerkerSolverProblemWrapperTemplates(T1) \ #define kerkerSolverProblemWrapperTemplatesL(T1) \ # 1 "/github/workspace/include/kerkerSolverProblemWrapper.def" 1 kerkerSolverProblemWrapperTemplates(1) kerkerSolverProblemWrapperTemplates(2) kerkerSolverProblemWrapperTemplates(3) kerkerSolverProblemWrapperTemplates(4) kerkerSolverProblemWrapperTemplates(5) kerkerSolverProblemWrapperTemplates(6) kerkerSolverProblemWrapperTemplates(7) kerkerSolverProblemWrapperTemplates(8) kerkerSolverProblemWrapperTemplates(9) kerkerSolverProblemWrapperTemplates(10) kerkerSolverProblemWrapperTemplates(11) kerkerSolverProblemWrapperTemplates(12) kerkerSolverProblemWrapperTemplates(13) kerkerSolverProblemWrapperTemplates(14) kerkerSolverProblemWrapperTemplates(15) kerkerSolverProblemWrapperTemplatesL(16)# 35 "/github/workspace/include/kerkerSolverProblemWrapper.h" 2 > kerkerSolverProblemObject
Definition kerkerSolverProblemWrapper.h:30
dealii::LinearAlgebra::distributed::Vector< elem_type, dealii::MemorySpace::Host > distributedCPUVec
Definition headers.h:92
std::int32_t Int
Definition TypeConfig.h:11