FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
abacus-develop/source/source_esolver/esolver_nep.cpp at develop · deepmodeling/abacus-develop · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
deepmodeling
/
abacus-develop
Public
forked from
abacusmodeling/abacus-develop
Notifications
You must be signed in to change notification settings
Fork
247
Star
291
Code
Issues
251
Pull requests
12
Discussions
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
abacus-develop
/
source
/
source_esolver
/
esolver_nep.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
391 lines (356 loc) · 14.9 KB
Breadcrumbs
abacus-develop
/
source
/
source_esolver
/
esolver_nep.cpp
Copy path
File metadata and controls
391 lines (356 loc) · 14.9 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
*
* @file esolver_nep.cpp
#include "source_io/module_parameter/parameter.h"
* @brief Implementation of ESolver_NEP class for neuroevolution potential (NEP).
*
* This file contains the implementation of the ESolver_NEP class, which is used for solving the energy and forces in a
* NEP simulation.
* NEP is a method for training deep neural networks to accurately predict the potential energy surface of a
* molecular system.
*
* For more information about NEP, see the following reference:
* 1. https://gpumd.org/potentials/nep.html
* 2. https://doi.org/10.1002/mgea.70028
*
* @author MoseyQAQ
* @date 2025-10-10
*/
#
include
"
esolver_nep.h
"
#
include
"
source_base/parallel_common.h
"
#
include
"
source_base/timer.h
"
#
include
"
source_cell/mdcell.h
"
#
include
"
source_cell/module_neighlist/neighbor_search.h
"
#
include
"
source_cell/cif_io.h
"
#
include
"
source_io/module_output/output_log.h
"
#
include
"
source_io/module_parameter/parameter.h
"
#
include
<
algorithm
>
#
include
<
array
>
#
include
<
numeric
>
#
include
<
unordered_map
>
using
namespace
ModuleESolver
;
void
ESolver_NEP::before_all_runners
(BaseCell& basecell,
const
Input_para& inp)
{
this
->
inp_
= &inp;
nep_potential =
0.0
;
nep_virial.
create
(
3
,
3
);
if
(basecell.
kind
() == BaseCell::Kind::mdcell)
{
MDCell& mdcell =
static_cast
<MDCell&>(basecell);
#
ifdef
__NEP
const
double
cutoff =
std::max
(nep.
paramb
.
rc_radial_max
, nep.
paramb
.
rc_angular_max
)
* ModuleBase::
ANGSTROM_AU
;
mdcell.
initialize_neighbors
(cutoff);
initialize_type_map_
(mdcell.
type_labels
());
#
else
ModuleBase::WARNING_QUIT
(
"
ESolver_NEP
"
,
"
Please recompile with -D__NEP
"
);
#
endif
return
;
}
UnitCell& ucell =
static_cast
<UnitCell&>(basecell);
nep_force.
create
(ucell.
nat
,
3
);
atype.
resize
(ucell.
nat
);
_e.
resize
(ucell.
nat
);
_f.
resize
(
3
* ucell.
nat
);
_v.
resize
(
9
* ucell.
nat
);
ModuleIO::CifParser::write
(
PARAM
.
globalv
.
global_out_dir
+
"
STRU.cif
"
,
ucell,
"
# Generated by ABACUS ModuleIO::CifParser
"
,
"
data_?
"
);
#
ifdef
__NEP
//
/ determine the type map from STRU to NEP model
type_map
(ucell);
#
endif
}
void
ESolver_NEP::runner
(BaseCell& basecell,
const
int
istep)
{
ModuleBase::TITLE
(
"
ESolver_NEP
"
,
"
runner
"
);
ModuleBase::timer::start
(
"
ESolver_NEP
"
,
"
runner
"
);
if
(basecell.
kind
() == BaseCell::Kind::mdcell)
{
#
ifndef
__NEP
ModuleBase::WARNING_QUIT
(
"
ESolver_NEP
"
,
"
Please recompile with -D__NEP
"
);
#
else
static_cast
<
void
>(istep);
MDCell& mdcell =
static_cast
<MDCell&>(basecell);
if
(!mdcell.
has_neighbor_search
())
{
mdcell.
prepare_neighbors
();
}
const
int
nowned_atoms = mdcell.
nowned_atoms
();
const
int
nghost = mdcell.
nghost
();
const
int
natom = nowned_atoms + nghost;
if
(natom ==
0
)
{
ModuleBase::WARNING_QUIT
(
"
ESolver_NEP
"
,
"
MDCell contains no atoms.
"
);
}
const
std::vector<LocalAtom>& owned_atoms = mdcell.
owned_atoms
();
const
std::vector<LocalAtom>& ghost_atoms = mdcell.
ghost_atoms
();
std::vector<
int
>
local_type
(
static_cast
<std::
size_t
>(natom),
0
);
std::vector<std::array<
double
,
3
> >
position
(
static_cast
<std::
size_t
>(natom));
std::vector<std::array<
double
,
3
> >
force
(
static_cast
<std::
size_t
>(natom));
std::vector<
double
*>
position_ptrs
(
static_cast
<std::
size_t
>(natom),
NULL
);
std::vector<
double
*>
force_ptrs
(
static_cast
<std::
size_t
>(natom),
NULL
);
for
(
int
iat =
0
; iat < natom; ++iat)
{
const
LocalAtom& atom = iat < nowned_atoms ? owned_atoms[
static_cast
<std::
size_t
>(iat)]
: ghost_atoms[
static_cast
<std::
size_t
>(iat - nowned_atoms)];
if
(atom.
type
<
0
||
static_cast
<std::
size_t
>(atom.
type
) >= md_type_to_nep_type_.
size
())
{
ModuleBase::WARNING_QUIT
(
"
ESolver_NEP
"
,
"
MDCell atom type is outside the NEP type map.
"
);
}
local_type[
static_cast
<std::
size_t
>(iat)] = atom.
type
;
position[
static_cast
<std::
size_t
>(iat)][
0
] = atom.
cart
.
x
* mdcell.
lat0
() * ModuleBase::
BOHR_TO_A
;
position[
static_cast
<std::
size_t
>(iat)][
1
] = atom.
cart
.
y
* mdcell.
lat0
() * ModuleBase::
BOHR_TO_A
;
position[
static_cast
<std::
size_t
>(iat)][
2
] = atom.
cart
.
z
* mdcell.
lat0
() * ModuleBase::
BOHR_TO_A
;
force[
static_cast
<std::
size_t
>(iat)].
fill
(
0.0
);
position_ptrs[
static_cast
<std::
size_t
>(iat)] = position[
static_cast
<std::
size_t
>(iat)].
data
();
force_ptrs[
static_cast
<std::
size_t
>(iat)] = force[
static_cast
<std::
size_t
>(iat)].
data
();
}
const
NeighborList& neighbor_list = mdcell.
neighbor_search
().
get_neighbor_list
();
std::vector<
int
>
ilist
(
static_cast
<std::
size_t
>(nowned_atoms),
0
);
std::vector<
int
>
numneigh
(
static_cast
<std::
size_t
>(natom),
0
);
std::vector<
int
*>
firstneigh
(
static_cast
<std::
size_t
>(natom),
NULL
);
for
(
int
iat =
0
; iat < nowned_atoms; ++iat)
{
ilist[
static_cast
<std::
size_t
>(iat)] = iat;
numneigh[
static_cast
<std::
size_t
>(iat)] = neighbor_list.
get_numneigh
(iat);
firstneigh[
static_cast
<std::
size_t
>(iat)] =
const_cast
<
int
*>(neighbor_list.
get_firstneigh
(iat));
}
double
local_energy =
0.0
;
double
local_virial[
6
] = {
0.0
,
0.0
,
0.0
,
0.0
,
0.0
,
0.0
};
ModuleBase::timer::start
(
"
ESolver_NEP
"
,
"
compute
"
);
nep.
compute_for_lammps
(nowned_atoms,
nowned_atoms,
nowned_atoms >
0
? ilist.
data
() :
NULL
,
numneigh.
data
(),
firstneigh.
data
(),
local_type.
data
(),
md_type_to_nep_type_.
data
(),
position_ptrs.
data
(),
local_energy,
local_virial,
NULL
,
force_ptrs.
data
(),
NULL
);
ModuleBase::timer::end
(
"
ESolver_NEP
"
,
"
compute
"
);
std::vector<LocalAtom>& mutable_owned_atoms = mdcell.
mutable_owned_atoms
();
std::vector<LocalAtom>& mutable_ghost_atoms = mdcell.
mutable_ghost_atoms
();
for
(
int
iat =
0
; iat < nowned_atoms; ++iat)
{
mutable_owned_atoms[
static_cast
<std::
size_t
>(iat)].
force
.
set
(force[
static_cast
<std::
size_t
>(iat)][
0
],
force[
static_cast
<std::
size_t
>(iat)][
1
],
force[
static_cast
<std::
size_t
>(iat)][
2
]);
}
for
(
int
iat =
0
; iat < nghost; ++iat)
{
mutable_ghost_atoms[
static_cast
<std::
size_t
>(iat)].
force
.
set
(force[
static_cast
<std::
size_t
>(nowned_atoms + iat)][
0
],
force[
static_cast
<std::
size_t
>(nowned_atoms + iat)][
1
],
force[
static_cast
<std::
size_t
>(nowned_atoms + iat)][
2
]);
}
mdcell.
accumulate_ghost_forces
();
#
ifdef
__MPI
MPI_Allreduce
(
MPI_IN_PLACE
, &local_energy,
1
,
MPI_DOUBLE
,
MPI_SUM
, mdcell.
communicator
());
MPI_Allreduce
(
MPI_IN_PLACE
, local_virial,
6
,
MPI_DOUBLE
,
MPI_SUM
, mdcell.
communicator
());
#
endif
const
double
fact_e =
1.0
/ ModuleBase::Ry_to_eV;
const
double
fact_f =
1.0
/ (ModuleBase::Ry_to_eV * ModuleBase::
ANGSTROM_AU
);
const
double
fact_v =
1.0
/ (mdcell.
omega
() * ModuleBase::Ry_to_eV);
nep_potential = local_energy * fact_e;
for
(
int
iat =
0
; iat < nowned_atoms; ++iat)
{
LocalAtom& atom = mutable_owned_atoms[
static_cast
<std::
size_t
>(iat)];
atom.
force
*= fact_f;
}
nep_virial
(
0
,
0
) = local_virial[
0
] * fact_v;
nep_virial
(
1
,
1
) = local_virial[
1
] * fact_v;
nep_virial
(
2
,
2
) = local_virial[
2
] * fact_v;
nep_virial
(
0
,
1
) =
nep_virial
(
1
,
0
) = local_virial[
3
] * fact_v;
nep_virial
(
0
,
2
) =
nep_virial
(
2
,
0
) = local_virial[
4
] * fact_v;
nep_virial
(
1
,
2
) =
nep_virial
(
2
,
1
) = local_virial[
5
] * fact_v;
#
endif
ModuleBase::timer::end
(
"
ESolver_NEP
"
,
"
runner
"
);
return
;
}
UnitCell& ucell =
static_cast
<UnitCell&>(basecell);
//
note that NEP are column major, thus a transpose is needed
//
cell
std::vector<
double
>
cell
(
9
,
0.0
);
cell[
0
] = ucell.
latvec
.
e11
* ucell.
lat0_angstrom
;
cell[
1
] = ucell.
latvec
.
e21
* ucell.
lat0_angstrom
;
cell[
2
] = ucell.
latvec
.
e31
* ucell.
lat0_angstrom
;
cell[
3
] = ucell.
latvec
.
e12
* ucell.
lat0_angstrom
;
cell[
4
] = ucell.
latvec
.
e22
* ucell.
lat0_angstrom
;
cell[
5
] = ucell.
latvec
.
e32
* ucell.
lat0_angstrom
;
cell[
6
] = ucell.
latvec
.
e13
* ucell.
lat0_angstrom
;
cell[
7
] = ucell.
latvec
.
e23
* ucell.
lat0_angstrom
;
cell[
8
] = ucell.
latvec
.
e33
* ucell.
lat0_angstrom
;
//
coord
std::vector<
double
>
coord
(
3
* ucell.
nat
,
0.0
);
int
iat =
0
;
const
int
nat = ucell.
nat
;
for
(
int
it =
0
; it < ucell.
ntype
; ++it)
{
for
(
int
ia =
0
; ia < ucell.
atoms
[it].
na
; ++ia)
{
coord[iat] = ucell.
atoms
[it].
tau
[ia].
x
* ucell.
lat0_angstrom
;
coord[iat + nat] = ucell.
atoms
[it].
tau
[ia].
y
* ucell.
lat0_angstrom
;
coord[iat +
2
* nat] = ucell.
atoms
[it].
tau
[ia].
z
* ucell.
lat0_angstrom
;
iat++;
}
}
assert
(ucell.
nat
== iat);
#
ifdef
__NEP
nep_potential =
0.0
;
nep_force.
zero_out
();
nep_virial.
zero_out
();
nep.
compute
(atype, cell, coord, _e, _f, _v);
//
unit conversion
const
double
fact_e =
1.0
/ ModuleBase::Ry_to_eV;
const
double
fact_f =
1.0
/ (ModuleBase::Ry_to_eV * ModuleBase::
ANGSTROM_AU
);
const
double
fact_v =
1.0
/ (ucell.
omega
* ModuleBase::Ry_to_eV);
//
potential energy
nep_potential = fact_e *
std::accumulate
(_e.
begin
(), _e.
end
(),
0.0
);
GlobalV::ofs_running <<
"
#TOTAL ENERGY#
"
<<
std::setprecision
(
11
) << nep_potential * ModuleBase::Ry_to_eV <<
"
eV
"
<< std::endl;
//
forces
for
(
int
i =
0
; i < nat; ++i)
{
nep_force
(i,
0
) = _f[i] * fact_f;
nep_force
(i,
1
) = _f[i + nat] * fact_f;
nep_force
(i,
2
) = _f[i +
2
* nat] * fact_f;
}
//
virial
std::vector<
double
>
v_sum
(
9
,
0.0
);
for
(
int
j =
0
; j <
9
; ++j)
{
for
(
int
i =
0
; i < nat; ++i)
{
int
index = j * nat + i;
v_sum[j] += _v[index];
}
}
//
virial -> stress
for
(
int
i =
0
; i <
3
; ++i)
{
for
(
int
j =
0
; j <
3
; ++j)
{
nep_virial
(i, j) = v_sum[
3
* i + j] * fact_v;
}
}
#
else
ModuleBase::WARNING_QUIT
(
"
ESolver_NEP
"
,
"
Please recompile with -D__NEP
"
);
#
endif
ModuleBase::timer::end
(
"
ESolver_NEP
"
,
"
runner
"
);
}
double
ESolver_NEP::cal_energy
()
{
return
nep_potential;
}
void
ESolver_NEP::cal_force
(BaseCell& basecell, ModuleBase::matrix& force)
{
if
(basecell.
kind
() == BaseCell::Kind::mdcell)
{
const
MDCell& mdcell =
static_cast
<
const
MDCell&>(basecell);
force.
create
(mdcell.
nowned_atoms
(),
3
);
for
(
int
iat =
0
; iat < mdcell.
nowned_atoms
(); ++iat)
{
const
LocalAtom& atom = mdcell.
owned_atoms
()[
static_cast
<std::
size_t
>(iat)];
force
(iat,
0
) = atom.
force
.
x
;
force
(iat,
1
) = atom.
force
.
y
;
force
(iat,
2
) = atom.
force
.
z
;
}
return
;
}
force = nep_force;
UnitCell& ucell =
static_cast
<UnitCell&>(basecell);
ModuleIO::print_force
(GlobalV::ofs_running, ucell,
"
TOTAL-FORCE (eV/Angstrom)
"
, force,
false
);
}
void
ESolver_NEP::cal_stress
(BaseCell& basecell, ModuleBase::matrix& stress)
{
stress = nep_virial;
if
(basecell.
kind
() == BaseCell::Kind::unitcell)
{
ModuleIO::print_stress
(
"
TOTAL-STRESS
"
, stress,
true
,
false
, GlobalV::ofs_running);
}
else
{
#
ifdef
__MPI
if
(
static_cast
<MDCell&>(basecell).
mpi_rank
() ==
0
)
#
endif
{
ModuleIO::print_stress
(
"
TOTAL-STRESS
"
, stress,
true
,
false
, GlobalV::ofs_running);
}
}
//
external stress
double
unit_transform = ModuleBase::
RYDBERG_SI
/
pow
(ModuleBase::
BOHR_RADIUS_SI
,
3
) *
1.0e-8
;
double
external_stress[
3
] = {
this
->
inp_
->
press1
,
this
->
inp_
->
press2
,
this
->
inp_
->
press3
};
for
(
int
i =
0
; i <
3
; i++)
{
stress
(i, i) -= external_stress[i] / unit_transform;
}
}
void
ESolver_NEP::after_all_runners
(BaseCell& basecell)
{
static_cast
<
void
>(basecell);
GlobalV::ofs_running <<
"
\n
--------------------------------------------
"
<< std::endl;
GlobalV::ofs_running <<
std::setprecision
(
16
);
GlobalV::ofs_running <<
"
!FINAL_ETOT_IS
"
<< nep_potential * ModuleBase::Ry_to_eV <<
"
eV
"
<< std::endl;
GlobalV::ofs_running <<
"
--------------------------------------------
\n\n
"
<< std::endl;
}
#
ifdef
__NEP
void
ESolver_NEP::type_map
(
const
UnitCell& ucell)
{
std::vector<std::string>
type_labels
(
static_cast
<std::
size_t
>(ucell.
ntype
));
for
(
int
it =
0
; it < ucell.
ntype
; ++it)
{
type_labels[
static_cast
<std::
size_t
>(it)] = ucell.
atoms
[it].
label
;
}
initialize_type_map_
(type_labels);
int
iat =
0
;
for
(
int
it =
0
; it < ucell.
ntype
; ++it)
{
for
(
int
ia =
0
; ia < ucell.
atoms
[it].
na
; ++ia)
{
atype[
static_cast
<std::
size_t
>(iat++)] = md_type_to_nep_type_[
static_cast
<std::
size_t
>(it)];
}
}
assert
(ucell.
nat
== iat);
}
void
ESolver_NEP::initialize_type_map_
(
const
std::vector<std::string>& type_labels)
{
//
parse the element list from NEP model file
std::unordered_map<std::string,
int
> label;
std::string temp;
for
(
int
i =
0
; i < nep.
element_list
.
size
(); ++i)
{
label[nep.
element_list
[i]] = i;
//
> label: map from element string to index int.
}
std::cout <<
"
\n
Element list of model file
"
<< nep_file <<
"
"
<< std::endl;
std::cout <<
"
----------------------------------------------------------------
"
;
int
count =
0
;
for
(
auto
it = label.
begin
(); it != label.
end
(); ++it)
{
if
(count %
5
==
0
)
{
std::cout << std::endl;
std::cout <<
"
"
;
}
count++;
temp = it->
first
+
"
:
"
+
std::to_string
(it->
second
);
std::cout << std::left <<
std::setw
(
10
) << temp;
}
std::cout <<
"
\n
-----------------------------------------------------------------
"
<< std::endl;
//
parse the atype based on the element list
md_type_to_nep_type_.
resize
(type_labels.
size
());
for
(std::
size_t
it =
0
; it < type_labels.
size
(); ++it)
{
const
std::unordered_map<std::string,
int
>::const_iterator found = label.
find
(type_labels[it]);
if
(found == label.
end
())
{
ModuleBase::WARNING_QUIT
(
"
ESolver_NEP
"
,
"
The label
"
+ type_labels[it] +
"
is not found in the type map.
"
);
}
md_type_to_nep_type_[it] = found->
second
;
}
}
#
endif
Back
|
FazBrowse Home
|
New Git URL