Skip to content

Commit 2362a2f

Browse files
author
Han Wang
committed
add energy calculation in fix/dplr
1 parent 14091c3 commit 2362a2f

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

source/lmp/fix_dplr.cpp

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ is_key (const string& input)
3434

3535

3636
FixDPLR::FixDPLR(LAMMPS *lmp, int narg, char **arg)
37-
:Fix(lmp, narg, arg), efield(3, 0.0)
37+
:Fix(lmp, narg, arg),
38+
efield(3, 0.0),
39+
efield_fsum(4, 0.0),
40+
efield_fsum_all(4, 0.0),
41+
efield_force_flag(0)
3842
{
3943
virial_flag = 1;
4044

@@ -112,6 +116,7 @@ FixDPLR::FixDPLR(LAMMPS *lmp, int narg, char **arg)
112116
int FixDPLR::setmask()
113117
{
114118
int mask = 0;
119+
mask |= THERMO_ENERGY;
115120
mask |= POST_INTEGRATE;
116121
mask |= PRE_FORCE;
117122
mask |= POST_FORCE;
@@ -383,11 +388,17 @@ void FixDPLR::post_force(int vflag)
383388
imageint *image = atom->image;
384389
double unwrap[3];
385390
double v[6];
391+
efield_fsum[0] = efield_fsum[1] = efield_fsum[2] = efield_fsum[3] = 0.0;
392+
efield_force_flag = 0;
386393
for (int ii = 0; ii < nlocal; ++ii){
387394
for (int dd = 0; dd < 3; ++dd){
388395
dfele[ii*3+dd] += q[ii] * efield[dd];
389396
}
390397
domain->unmap(x[ii],image[ii],unwrap);
398+
efield_fsum[0] -= efield[0]*unwrap[0]+efield[1]*unwrap[1]+efield[2]*unwrap[2];
399+
efield_fsum[1] += efield[0];
400+
efield_fsum[2] += efield[1];
401+
efield_fsum[3] += efield[2];
391402
if (evflag) {
392403
v[0] = q[ii] * efield[0] *unwrap[0];
393404
v[1] = q[ii] * efield[1] *unwrap[1];
@@ -495,5 +506,28 @@ void FixDPLR::unpack_reverse_comm(int n, int *list, double *buf)
495506
}
496507
}
497508

509+
/* ----------------------------------------------------------------------
510+
return energy added by fix
511+
------------------------------------------------------------------------- */
498512

513+
double FixDPLR::compute_scalar(void)
514+
{
515+
if (efield_force_flag == 0) {
516+
MPI_Allreduce(&efield_fsum[0],&efield_fsum_all[0],4,MPI_DOUBLE,MPI_SUM,world);
517+
efield_force_flag = 1;
518+
}
519+
return efield_fsum_all[0];
520+
}
521+
522+
/* ----------------------------------------------------------------------
523+
return total extra force due to fix
524+
------------------------------------------------------------------------- */
499525

526+
double FixDPLR::compute_vector(int n)
527+
{
528+
if (efield_force_flag == 0) {
529+
MPI_Allreduce(&efield_fsum[0],&efield_fsum_all[0],4,MPI_DOUBLE,MPI_SUM,world);
530+
efield_force_flag = 1;
531+
}
532+
return efield_fsum_all[n+1];
533+
}

source/lmp/fix_dplr.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ namespace LAMMPS_NS {
3232
void post_force(int);
3333
int pack_reverse_comm(int, int, double *);
3434
void unpack_reverse_comm(int, int *, double *);
35+
double compute_scalar(void);
36+
double compute_vector(int);
3537
private:
3638
PairNNP * pair_nnp;
3739
DeepTensor dpt;
@@ -46,6 +48,8 @@ namespace LAMMPS_NS {
4648
vector<FLOAT_PREC> dipole_recd;
4749
vector<double> dfcorr_buff;
4850
vector<double> efield;
51+
vector<double> efield_fsum, efield_fsum_all;
52+
int efield_force_flag;
4953
void get_valid_pairs(vector<pair<int,int> >& pairs);
5054
};
5155
}

0 commit comments

Comments
 (0)