forked from deepmodeling/abacus-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathesolver.cpp
More file actions
342 lines (329 loc) · 10.1 KB
/
esolver.cpp
File metadata and controls
342 lines (329 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include "esolver.h"
#include "esolver_ks_pw.h"
#include "esolver_sdft_pw.h"
#include "source_base/module_device/device.h"
#include "source_io/module_parameter/parameter.h"
#ifdef __LCAO
#include "esolver_dm2rho.h"
#include "esolver_gets.h"
#include "esolver_ks_lcao.h"
#include "esolver_ks_lcao_tddft.h"
#include "esolver_ks_lcaopw.h"
#include "source_lcao/module_lr/esolver_lrtd_lcao.h"
extern "C"
{
#include "source_base/module_external/blacs_connector.h"
}
#endif
#include "esolver_dp.h"
#include "esolver_lj.h"
#include "esolver_of.h"
#include "source_io/module_parameter/md_parameter.h"
#include <stdexcept>
namespace ModuleESolver
{
std::string determine_type()
{
std::string esolver_type = "none";
if (PARAM.inp.basis_type == "pw")
{
if (PARAM.inp.esolver_type == "sdft")
{
esolver_type = "sdft_pw";
}
else if (PARAM.inp.esolver_type == "ofdft")
{
esolver_type = "ofdft";
}
else if (PARAM.inp.esolver_type == "ksdft")
{
esolver_type = "ksdft_pw";
}
}
else if (PARAM.inp.basis_type == "lcao_in_pw")
{
#ifdef __LCAO
if (PARAM.inp.esolver_type == "sdft")
{
esolver_type = "sdft_pw";
}
else if (PARAM.inp.esolver_type == "ksdft")
{
esolver_type = "ksdft_lip";
}
#else
ModuleBase::WARNING_QUIT("ESolver", "Calculation involving numerical orbitals must be compiled with __LCAO");
#endif
}
else if (PARAM.inp.basis_type == "lcao")
{
#ifdef __LCAO
if (PARAM.inp.esolver_type == "tddft")
{
esolver_type = "ksdft_lcao_tddft";
}
else if (PARAM.inp.esolver_type == "ksdft")
{
esolver_type = "ksdft_lcao";
}
else if (PARAM.inp.esolver_type == "ks-lr")
{
esolver_type = "ksdft_lr_lcao";
}
else if (PARAM.inp.esolver_type == "lr")
{
esolver_type = "lr_lcao";
}
#else
ModuleBase::WARNING_QUIT("ESolver", "Calculation involving numerical orbitals must be compiled with __LCAO");
#endif
}
if (PARAM.inp.esolver_type == "lj")
{
esolver_type = "lj_pot";
}
else if (PARAM.inp.esolver_type == "dp")
{
esolver_type = "dp_pot";
}
else if (esolver_type == "none")
{
ModuleBase::WARNING_QUIT("ESolver", "No such esolver_type combined with basis_type");
}
GlobalV::ofs_running << "\n #ENERGY SOLVER# " << esolver_type << std::endl;
auto device_info = PARAM.inp.device;
for (char& c: device_info)
{
if (std::islower(c))
{
c = std::toupper(c);
}
}
if (GlobalV::MY_RANK == 0)
{
std::cout << " RUNNING WITH DEVICE : " << device_info << " / "
<< base_device::information::get_device_info(PARAM.inp.device) << std::endl;
}
GlobalV::ofs_running << "\n RUNNING WITH DEVICE : " << device_info << " / "
<< base_device::information::get_device_info(PARAM.inp.device) << std::endl;
/***auto end_time = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::duration<double>>(end_time - start_time);
std::cout << "hipGetDeviceInfo took " << duration.count() << " seconds" << std::endl;***/
return esolver_type;
}
// Some API to operate E_Solver
ESolver* init_esolver(const Input_para& inp, UnitCell& ucell)
{
// determine type of esolver based on INPUT information
const std::string esolver_type = determine_type();
// initialize the corresponding Esolver child class
if (esolver_type == "ksdft_pw")
{
#if ((defined __CUDA) || (defined __ROCM))
if (PARAM.inp.device == "gpu")
{
if (PARAM.inp.precision == "single")
{
return new ESolver_KS_PW<std::complex<float>, base_device::DEVICE_GPU>();
}
else
{
return new ESolver_KS_PW<std::complex<double>, base_device::DEVICE_GPU>();
}
}
#endif
if (PARAM.inp.precision == "single")
{
return new ESolver_KS_PW<std::complex<float>, base_device::DEVICE_CPU>();
}
else
{
return new ESolver_KS_PW<std::complex<double>, base_device::DEVICE_CPU>();
}
}
else if (esolver_type == "sdft_pw")
{
#if ((defined __CUDA) || (defined __ROCM))
if (PARAM.inp.device == "gpu")
{
// if (PARAM.inp.precision == "single")
// {
// return new ESolver_SDFT_PW<std::complex<float>, base_device::DEVICE_GPU>();
// }
// else
// {
return new ESolver_SDFT_PW<std::complex<double>, base_device::DEVICE_GPU>();
// }
}
#endif
// if (PARAM.inp.precision == "single")
// {
// return new ESolver_SDFT_PW<std::complex<float>, base_device::DEVICE_CPU>();
// }
// else
// {
return new ESolver_SDFT_PW<std::complex<double>, base_device::DEVICE_CPU>();
// }
}
#ifdef __LCAO
else if (esolver_type == "ksdft_lip")
{
if (PARAM.inp.precision == "single")
{
return new ESolver_KS_LIP<std::complex<float>>();
}
else
{
return new ESolver_KS_LIP<std::complex<double>>();
}
}
else if (esolver_type == "ksdft_lcao")
{
if (PARAM.inp.calculation == "get_s")
{
if (PARAM.globalv.gamma_only_local)
{
ModuleBase::WARNING_QUIT("ESolver", "get_s is not implemented for gamma_only");
}
else
{
return new ESolver_GetS();
}
}
if (PARAM.globalv.gamma_only_local)
{
return new ESolver_KS_LCAO<double, double>();
}
else if (PARAM.inp.nspin < 4)
{
if (PARAM.inp.dm_to_rho)
{
return new ESolver_DM2rho<std::complex<double>, double>();
}
else
{
return new ESolver_KS_LCAO<std::complex<double>, double>();
}
}
else
{
if (PARAM.inp.dm_to_rho)
{
return new ESolver_DM2rho<std::complex<double>, std::complex<double>>();
}
else
{
return new ESolver_KS_LCAO<std::complex<double>, std::complex<double>>();
}
}
}
else if (esolver_type == "ksdft_lcao_tddft")
{
if (PARAM.inp.nspin < 4)
{
#if ((defined __CUDA) /* || (defined __ROCM) */)
if (PARAM.inp.device == "gpu")
{
return new ESolver_KS_LCAO_TDDFT<double, base_device::DEVICE_GPU>();
}
#endif
return new ESolver_KS_LCAO_TDDFT<double, base_device::DEVICE_CPU>();
}
else
{
#if ((defined __CUDA) /* || (defined __ROCM) */)
if (PARAM.inp.device == "gpu")
{
return new ESolver_KS_LCAO_TDDFT<std::complex<double>, base_device::DEVICE_GPU>();
}
#endif
return new ESolver_KS_LCAO_TDDFT<std::complex<double>, base_device::DEVICE_CPU>();
}
}
else if (esolver_type == "lr_lcao")
{
// use constructor rather than Init function to initialize reference (instead of pointers) to ucell
if (PARAM.globalv.gamma_only_local)
{
return new LR::ESolver_LR<double, double>(inp, ucell);
}
else
{
return new LR::ESolver_LR<std::complex<double>, double>(inp, ucell);
}
}
else if (esolver_type == "ksdft_lr_lcao")
{
// initialize the 1st ESolver_KS
ModuleESolver::ESolver* p_esolver = nullptr;
if (PARAM.globalv.gamma_only_local)
{
p_esolver = new ESolver_KS_LCAO<double, double>();
}
else if (PARAM.inp.nspin < 4)
{
p_esolver = new ESolver_KS_LCAO<std::complex<double>, double>();
}
else
{
p_esolver = new ESolver_KS_LCAO<std::complex<double>, std::complex<double>>();
}
p_esolver->before_all_runners(ucell, inp);
p_esolver->runner(ucell, 0); // scf-only
// force and stress is not needed currently,
// they will be supported after the analytical gradient
// of LR-TDDFT is implemented.
std::cout << " PREPARING FOR EXCITED STATES." << std::endl;
// initialize the 2nd ESolver_LR at the temporary pointer
ModuleESolver::ESolver* p_esolver_lr = nullptr;
if (PARAM.globalv.gamma_only_local)
{
p_esolver_lr = new LR::ESolver_LR<double, double>(
std::move(*dynamic_cast<ModuleESolver::ESolver_KS_LCAO<double, double>*>(p_esolver)),
inp,
ucell);
}
else
{
p_esolver_lr = new LR::ESolver_LR<std::complex<double>, double>(
std::move(*dynamic_cast<ModuleESolver::ESolver_KS_LCAO<std::complex<double>, double>*>(p_esolver)),
inp,
ucell);
}
// clean the 1st ESolver_KS and swap the pointer
ModuleESolver::clean_esolver(p_esolver, false); // do not call Cblacs_exit, remain it for the 2nd ESolver
return p_esolver_lr;
}
#endif
else if (esolver_type == "ofdft")
{
return new ESolver_OF();
}
else if (esolver_type == "lj_pot")
{
return new ESolver_LJ();
}
else if (esolver_type == "dp_pot")
{
return new ESolver_DP(PARAM.mdp.pot_file);
}
throw std::invalid_argument("esolver_type = " + std::string(esolver_type) + ". Wrong in " + std::string(__FILE__)
+ " line " + std::to_string(__LINE__));
}
void clean_esolver(ESolver*& pesolver, const bool lcao_cblacs_exit)
{
// Zhang Xiaoyang modified in 2024/7/6:
// Note: because of the init method of serial lcao hsolver
// it needs no release step for it, or this [delete] will cause Segmentation Fault
// Probably it will be modified later.
#ifdef __MPI
delete pesolver;
#ifdef __LCAO
if (lcao_cblacs_exit)
{
Cblacs_exit(1);
}
#endif
#endif
}
} // namespace ModuleESolver