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