add variable option to compute bond/angle/dihedral local

This commit is contained in:
Steve Plimpton
2018-09-04 14:48:44 -06:00
parent 5edff5d970
commit bcecc0389e
9 changed files with 601 additions and 157 deletions

View File

@ -21,6 +21,8 @@
#include "domain.h"
#include "force.h"
#include "angle.h"
#include "input.h"
#include "variable.h"
#include "math_const.h"
#include "memory.h"
#include "error.h"
@ -30,11 +32,13 @@ using namespace MathConst;
#define DELTA 10000
enum{THETA,ENG,VARIABLE};
/* ---------------------------------------------------------------------- */
ComputeAngleLocal::ComputeAngleLocal(LAMMPS *lmp, int narg, char **arg) :
Compute(lmp, narg, arg),
vlocal(NULL), alocal(NULL)
bstyle(NULL), vstr(NULL), vvar(NULL), tstr(NULL), vlocal(NULL), alocal(NULL)
{
if (narg < 4) error->all(FLERR,"Illegal compute angle/local command");
@ -42,19 +46,82 @@ ComputeAngleLocal::ComputeAngleLocal(LAMMPS *lmp, int narg, char **arg) :
error->all(FLERR,"Compute angle/local used when angles are not allowed");
local_flag = 1;
// style args
nvalues = narg - 3;
bstyle = new int[nvalues];
vstr = new char*[nvalues];
vvar = new int[nvalues];
nvalues = 0;
tflag = 0;
nvar = 0;
int iarg;
for (iarg = 3; iarg < narg; iarg++) {
if (strcmp(arg[iarg],"theta") == 0) {
bstyle[nvalues++] = THETA;
tflag = 1;
} else if (strcmp(arg[iarg],"eng") == 0) {
bstyle[nvalues++] = ENG;
} else if (strncmp(arg[iarg],"v_",2) == 0) {
bstyle[nvalues++] = VARIABLE;
int n = strlen(arg[iarg]);
vstr[nvar] = new char[n];
strcpy(vstr[nvar],&arg[iarg][2]);
nvar++;
} else break;
}
// optional args
setflag = 0;
tstr = NULL;
while (iarg < narg) {
if (strcmp(arg[iarg],"set") == 0) {
setflag = 1;
if (iarg+3 > narg) error->all(FLERR,"Illegal compute angle/local command");
if (strcmp(arg[iarg+1],"theta") == 0) {
delete [] tstr;
int n = strlen(arg[iarg+2]) + 1;
tstr = new char[n];
strcpy(tstr,arg[iarg+2]);
tflag = 1;
} else error->all(FLERR,"Illegal compute angle/local command");
iarg += 3;
} else error->all(FLERR,"Illegal compute angle/local command");
}
// error check
if (nvar) {
if (!setflag)
error->all(FLERR,"Compute angle/local variable requires a set variable");
for (int i = 0; i < nvar; i++) {
vvar[i] = input->variable->find(vstr[i]);
if (vvar[i] < 0)
error->all(FLERR,"Variable name for copute angle/local does not exist");
if (!input->variable->equalstyle(vvar[i]))
error->all(FLERR,"Variable for compute angle/local is invalid style");
}
if (tstr) {
tvar = input->variable->find(tstr);
if (tvar < 0)
error->all(FLERR,"Variable name for compute angle/local does not exist");
if (!input->variable->internalstyle(tvar))
error->all(FLERR,"Variable for compute angle/local is invalid style");
}
} else if (setflag)
error->all(FLERR,"Compute angle/local set with no variable");
// initialize output
if (nvalues == 1) size_local_cols = 0;
else size_local_cols = nvalues;
tflag = eflag = -1;
nvalues = 0;
for (int iarg = 3; iarg < narg; iarg++) {
if (strcmp(arg[iarg],"theta") == 0) tflag = nvalues++;
else if (strcmp(arg[iarg],"eng") == 0) eflag = nvalues++;
else error->all(FLERR,"Invalid keyword in compute angle/local command");
}
nmax = 0;
vlocal = NULL;
alocal = NULL;
@ -64,6 +131,13 @@ ComputeAngleLocal::ComputeAngleLocal(LAMMPS *lmp, int narg, char **arg) :
ComputeAngleLocal::~ComputeAngleLocal()
{
delete [] bstyle;
for (int i = 0; i < nvar; i++) delete [] vstr[i];
delete [] vstr;
delete [] vvar;
delete [] tstr;
memory->destroy(vlocal);
memory->destroy(alocal);
}
@ -75,6 +149,20 @@ void ComputeAngleLocal::init()
if (force->angle == NULL)
error->all(FLERR,"No angle style is defined for compute angle/local");
if (nvar) {
for (int i = 0; i < nvar; i++) {
vvar[i] = input->variable->find(vstr[i]);
if (vvar[i] < 0)
error->all(FLERR,"Variable name for compute angle/local does not exist");
}
if (tstr) {
tvar = input->variable->find(tstr);
if (tvar < 0)
error->all(FLERR,"Variable name for compute angle/local does not exist");
}
}
// do initial memory allocation so that memory_usage() is correct
ncount = compute_angles(0);
@ -109,11 +197,11 @@ void ComputeAngleLocal::compute_local()
int ComputeAngleLocal::compute_angles(int flag)
{
int i,m,n,na,atom1,atom2,atom3,imol,iatom,atype;
int i,m,n,na,atom1,atom2,atom3,imol,iatom,atype,ivar;
tagint tagprev;
double delx1,dely1,delz1,delx2,dely2,delz2;
double rsq1,rsq2,r1,r2,c;
double *tbuf,*ebuf;
double rsq1,rsq2,r1,r2,c,theta;
double *ptr;
double **x = atom->x;
tagint *tag = atom->tag;
@ -131,17 +219,7 @@ int ComputeAngleLocal::compute_angles(int flag)
int nlocal = atom->nlocal;
int molecular = atom->molecular;
if (flag) {
if (nvalues == 1) {
if (tflag >= 0) tbuf = vlocal;
if (eflag >= 0) ebuf = vlocal;
} else {
if (tflag >= 0 && alocal) tbuf = &alocal[0][tflag];
else tbuf = NULL;
if (eflag >= 0 && alocal) ebuf = &alocal[0][eflag];
else ebuf = NULL;
}
}
// loop over all atoms and their angles
Angle *angle = force->angle;
@ -175,39 +253,62 @@ int ComputeAngleLocal::compute_angles(int flag)
if (atom3 < 0 || !(mask[atom3] & groupbit)) continue;
if (atype == 0) continue;
if (flag) {
if (tflag >= 0) {
delx1 = x[atom1][0] - x[atom2][0];
dely1 = x[atom1][1] - x[atom2][1];
delz1 = x[atom1][2] - x[atom2][2];
domain->minimum_image(delx1,dely1,delz1);
if (!flag) {
m++;
continue;
}
rsq1 = delx1*delx1 + dely1*dely1 + delz1*delz1;
r1 = sqrt(rsq1);
// theta needed by one or more outputs
delx2 = x[atom3][0] - x[atom2][0];
dely2 = x[atom3][1] - x[atom2][1];
delz2 = x[atom3][2] - x[atom2][2];
domain->minimum_image(delx2,dely2,delz2);
if (tflag) {
delx1 = x[atom1][0] - x[atom2][0];
dely1 = x[atom1][1] - x[atom2][1];
delz1 = x[atom1][2] - x[atom2][2];
domain->minimum_image(delx1,dely1,delz1);
rsq2 = delx2*delx2 + dely2*dely2 + delz2*delz2;
r2 = sqrt(rsq2);
rsq1 = delx1*delx1 + dely1*dely1 + delz1*delz1;
r1 = sqrt(rsq1);
delx2 = x[atom3][0] - x[atom2][0];
dely2 = x[atom3][1] - x[atom2][1];
delz2 = x[atom3][2] - x[atom2][2];
domain->minimum_image(delx2,dely2,delz2);
// c = cosine of angle
rsq2 = delx2*delx2 + dely2*dely2 + delz2*delz2;
r2 = sqrt(rsq2);
c = delx1*delx2 + dely1*dely2 + delz1*delz2;
c /= r1*r2;
if (c > 1.0) c = 1.0;
if (c < -1.0) c = -1.0;
tbuf[n] = 180.0*acos(c)/MY_PI;
// c = cosine of angle
// theta = angle in radians
c = delx1*delx2 + dely1*dely2 + delz1*delz2;
c /= r1*r2;
if (c > 1.0) c = 1.0;
if (c < -1.0) c = -1.0;
theta = acos(c);
}
if (nvalues == 1) ptr = &vlocal[m];
else ptr = alocal[m];
if (nvar) {
ivar = 0;
if (tstr) input->variable->internal_set(tvar,theta);
}
for (n = 0; n < nvalues; n++) {
switch (bstyle[n]) {
case THETA:
ptr[n] = 180.0*theta/MY_PI;
break;
case ENG:
if (atype > 0) ptr[n] = angle->single(atype,atom1,atom2,atom3);
else ptr[n] = 0.0;
break;
case VARIABLE:
ptr[n] = input->variable->compute_equal(vvar[ivar]);
ivar++;
break;
}
if (eflag >= 0) {
if (atype > 0)
ebuf[n] = angle->single(atype,atom1,atom2,atom3);
else ebuf[n] = 0.0;
}
n += nvalues;
}
m++;