/* ---------------------------------------------------------------------- LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator https://lammps.sandia.gov/, Sandia National Laboratories Steve Plimpton, sjplimp@sandia.gov Copyright (2003) Sandia Corporation. Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains certain rights in this software. This software is distributed under the GNU General Public License. See the README file in the top-level LAMMPS directory. ------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- Contributing author: Christina Payne (Vanderbilt U) Stan Moore (Sandia) for dipole terms ------------------------------------------------------------------------- */ #include "fix_efield_tip4p.h" // TIP4P #include // TIP4P #include #include "angle.h" // TIP4P #include "atom.h" #include "bond.h" // TIP4P #include "comm.h" #include "domain.h" #include "update.h" #include "force.h" #include "neighbor.h" // TIP4P #include "neigh_list.h" // TIP4P #include "modify.h" #include "respa.h" #include "input.h" #include "variable.h" #include "region.h" #include "memory.h" #include "error.h" using namespace LAMMPS_NS; using namespace FixConst; enum{NONE,CONSTANT,EQUAL,ATOM}; /* ---------------------------------------------------------------------- */ FixEfieldTIP4P::FixEfieldTIP4P(LAMMPS *lmp, int narg, char **arg) : // TIP4P Fix(lmp, narg, arg), xstr(nullptr), ystr(nullptr), zstr(nullptr), estr(nullptr), idregion(nullptr), efield(nullptr) { if (narg < 11) error->all(FLERR,"Illegal fix efield TIP4P command"); //TIP4P dynamic_group_allow = 1; vector_flag = 1; scalar_flag = 1; size_vector = 3; global_freq = 1; extvector = 1; extscalar = 1; respa_level_support = 1; ilevel_respa = 0; virial_flag = 1; qe2f = force->qe2f; xstr = ystr = zstr = nullptr; if (strstr(arg[3],"v_") == arg[3]) { int n = strlen(&arg[3][2]) + 1; xstr = new char[n]; strcpy(xstr,&arg[3][2]); } else { ex = qe2f * utils::numeric(FLERR,arg[3],false,lmp); xstyle = CONSTANT; } if (strstr(arg[4],"v_") == arg[4]) { int n = strlen(&arg[4][2]) + 1; ystr = new char[n]; strcpy(ystr,&arg[4][2]); } else { ey = qe2f * utils::numeric(FLERR,arg[4],false,lmp); ystyle = CONSTANT; } if (strstr(arg[5],"v_") == arg[5]) { int n = strlen(&arg[5][2]) + 1; zstr = new char[n]; strcpy(zstr,&arg[5][2]); } else { ez = qe2f * utils::numeric(FLERR,arg[5],false,lmp); zstyle = CONSTANT; } // optional args iregion = -1; idregion = nullptr; estr = nullptr; int iarg = 11; while (iarg < narg) { if (strcmp(arg[iarg],"region") == 0) { if (iarg+2 > narg) error->all(FLERR,"Illegal fix efield TIP4P command"); // TIP4P iregion = domain->find_region(arg[iarg+1]); if (iregion == -1) error->all(FLERR,"Region ID for fix efield TIP4P does not exist"); // TIP4P int n = strlen(arg[iarg+1]) + 1; idregion = new char[n]; strcpy(idregion,arg[iarg+1]); iarg += 2; } else if (strcmp(arg[iarg],"energy") == 0) { if (iarg+2 > narg) error->all(FLERR,"Illegal fix efield TIP4P command"); // TIP4P if (strstr(arg[iarg+1],"v_") == arg[iarg+1]) { int n = strlen(&arg[iarg+1][2]) + 1; estr = new char[n]; strcpy(estr,&arg[iarg+1][2]); } else error->all(FLERR,"Illegal fix efield TIP4P command"); // TIP4P iarg += 2; } else error->all(FLERR,"Illegal fix efield TIP4P command"); // TIP4P } typeO = utils::inumeric(FLERR,arg[6],false,lmp); //TIP4P typeH = utils::inumeric(FLERR,arg[7],false,lmp); //TIP4P typeB = utils::inumeric(FLERR,arg[8],false,lmp); //TIP4P typeA = utils::inumeric(FLERR,arg[9],false,lmp); //TIP4P qdist = utils::numeric(FLERR,arg[10],false,lmp); //TIP4P force_flag = 0; fsum[0] = fsum[1] = fsum[2] = fsum[3] = 0.0; maxatom = atom->nmax; memory->create(efield,maxatom,4,"efield:efield"); } /* ---------------------------------------------------------------------- */ FixEfieldTIP4P::~FixEfieldTIP4P() // TIP4P { delete [] xstr; delete [] ystr; delete [] zstr; delete [] estr; delete [] idregion; memory->destroy(efield); } /* ---------------------------------------------------------------------- */ int FixEfieldTIP4P::setmask() // TIP4P { int mask = 0; mask |= THERMO_ENERGY; mask |= POST_FORCE; mask |= POST_FORCE_RESPA; mask |= MIN_POST_FORCE; return mask; } /* ---------------------------------------------------------------------- */ void FixEfieldTIP4P::init() // TIP4P { qflag = muflag = 0; if (atom->q_flag) qflag = 1; if (atom->mu_flag && atom->torque_flag) muflag = 1; if (!qflag && !muflag) error->all(FLERR,"Fix efield TIP4P requires atom attribute q or mu"); // TIP4P // check variables if (xstr) { xvar = input->variable->find(xstr); if (xvar < 0) error->all(FLERR,"Variable name for fix efield TIP4P does not exist"); // TIP4P if (input->variable->equalstyle(xvar)) xstyle = EQUAL; else if (input->variable->atomstyle(xvar)) xstyle = ATOM; else error->all(FLERR,"Variable for fix efield TIP4P is invalid style"); // TIP4P } if (ystr) { yvar = input->variable->find(ystr); if (yvar < 0) error->all(FLERR,"Variable name for fix efield TIP4P does not exist"); // TIP4P if (input->variable->equalstyle(yvar)) ystyle = EQUAL; else if (input->variable->atomstyle(yvar)) ystyle = ATOM; else error->all(FLERR,"Variable for fix efield TIP4P is invalid style"); // TIP4P } if (zstr) { zvar = input->variable->find(zstr); if (zvar < 0) error->all(FLERR,"Variable name for fix efield TIP4P does not exist"); // TIP4P if (input->variable->equalstyle(zvar)) zstyle = EQUAL; else if (input->variable->atomstyle(zvar)) zstyle = ATOM; else error->all(FLERR,"Variable for fix efield TIP4P is invalid style"); // TIP4P } if (estr) { evar = input->variable->find(estr); if (evar < 0) error->all(FLERR,"Variable name for fix efield TIP4P does not exist"); // TIP4P if (input->variable->atomstyle(evar)) estyle = ATOM; else error->all(FLERR,"Variable for fix efield TIP4P is invalid style"); // TIP4P } else estyle = NONE; // set index and check validity of region if (iregion >= 0) { iregion = domain->find_region(idregion); if (iregion == -1) error->all(FLERR,"Region ID for fix aveforce does not exist"); } if (xstyle == ATOM || ystyle == ATOM || zstyle == ATOM) varflag = ATOM; else if (xstyle == EQUAL || ystyle == EQUAL || zstyle == EQUAL) varflag = EQUAL; else varflag = CONSTANT; if (muflag && varflag == ATOM) error->all(FLERR,"Fix efield TIP4P with dipoles cannot use atom-style variables"); // TIP4P if (muflag && update->whichflag == 2 && comm->me == 0) error->warning(FLERR, "The minimizer does not re-orient dipoles " "when using fix efield TIP4P"); // TIP4P if (varflag == CONSTANT && estyle != NONE) error->all(FLERR,"Cannot use variable energy with " "constant efield in fix efield TIP4P"); // TIP4P if ((varflag == EQUAL || varflag == ATOM) && update->whichflag == 2 && estyle == NONE) error->all(FLERR,"Must use variable energy with fix efield TIP4P"); // TIP4P if (strstr(update->integrate_style,"respa")) { ilevel_respa = ((Respa *) update->integrate)->nlevels-1; if (respa_level >= 0) ilevel_respa = MIN(respa_level,ilevel_respa); } // TIP4P if (atom->tag_enable == 0) error->all(FLERR,"Pair style lj/cut/tip4p/long requires atom IDs"); if (!force->newton_pair) error->all(FLERR, "Pair style lj/cut/tip4p/long requires newton pair on"); if (!atom->q_flag) error->all(FLERR, "Pair style lj/cut/tip4p/long requires atom attribute q"); if (force->bond == nullptr) error->all(FLERR,"Must use a bond style with TIP4P potential"); if (force->angle == nullptr) error->all(FLERR,"Must use an angle style with TIP4P potential"); // set alpha parameter // 根据qdist,theta,blen参数,计算alpha,进而用于计算虚拟电荷M点的位置。 double theta = force->angle->equilibrium_angle(typeA); double blen = force->bond->equilibrium_distance(typeB); alpha = qdist / (cos(0.5*theta) * blen); // TIP4P } /* ---------------------------------------------------------------------- */ void FixEfieldTIP4P::setup(int vflag) // TIP4P { if (strstr(update->integrate_style,"verlet")) post_force(vflag); else { ((Respa *) update->integrate)->copy_flevel_f(ilevel_respa); post_force_respa(vflag,ilevel_respa,0); ((Respa *) update->integrate)->copy_f_flevel(ilevel_respa); } } /* ---------------------------------------------------------------------- */ void FixEfieldTIP4P::min_setup(int vflag) // TIP4P { post_force(vflag); } /* ---------------------------------------------------------------------- apply F = qE ------------------------------------------------------------------------- */ void FixEfieldTIP4P::post_force(int vflag) // TIP4P { double **f = atom->f; double *q = atom->q; int *mask = atom->mask; imageint *image = atom->image; int nlocal = atom->nlocal; // energy and virial setup if (vflag) v_setup(vflag); else evflag = 0; // reallocate efield array if necessary if (varflag == ATOM && atom->nmax > maxatom) { maxatom = atom->nmax; memory->destroy(efield); memory->create(efield,maxatom,4,"efield:efield"); } // update region if necessary Region *region = nullptr; if (iregion >= 0) { region = domain->regions[iregion]; region->prematch(); } // fsum[0] = "potential energy" for added force // fsum[123] = extra force added to atoms fsum[0] = fsum[1] = fsum[2] = fsum[3] = 0.0; force_flag = 0; double **x = atom->x; double fx,fy,fz; double v[6]; // constant efield if (varflag == CONSTANT) { int iH1,iH2,itype; int *type = atom->type; tagint *tag = atom->tag; double unwrap[3]; double unwrapH1[3]; double unwrapH2[3]; double fO[3],fH[3]; // charge interactions // force = qE, potential energy = F dot x in unwrapped coords if (qflag) { for (int i = 0; i < nlocal; i++) if (mask[i] & groupbit) { if (region && !region->match(x[i][0],x[i][1],x[i][2])) continue; itype = type[i]; fx = q[i]*ex; fy = q[i]*ey; fz = q[i]*ez; /* ------------------------------TIP4P----------------------------------- */ if (itype != typeO) { f[i][0] += fx; f[i][1] += fy; f[i][2] += fz; domain->unmap(x[i],image[i],unwrap); fsum[0] -= fx*unwrap[0]+fy*unwrap[1]+fz*unwrap[2]; fsum[1] += fx; fsum[2] += fy; fsum[3] += fz; if (evflag) { v[0] = fx*unwrap[0]; v[1] = fy*unwrap[1]; v[2] = fz*unwrap[2]; v[3] = fx*unwrap[1]; v[4] = fx*unwrap[2]; v[5] = fy*unwrap[2]; v_tally(i, v); } } else { iH1 = atom->map(tag[i] + 1); iH2 = atom->map(tag[i] + 2); if (iH1 == -1 || iH2 == -1) error->one(FLERR,"TIP4P hydrogen is missing"); if (atom->type[iH1] != typeH || atom->type[iH2] != typeH) error->one(FLERR,"TIP4P hydrogen has incorrect atom type"); //iH1 = domain->closest_image(i,iH1); //iH2 = domain->closest_image(i,iH2); fO[0] = fx*(1 - alpha); fO[1] = fy*(1 - alpha); fO[2] = fz*(1 - alpha); fH[0] = 0.5 * alpha * fx; fH[1] = 0.5 * alpha * fy; fH[2] = 0.5 * alpha * fz; f[i][0] += fO[0]; f[i][1] += fO[1]; f[i][2] += fO[2]; f[iH1][0] += fH[0]; f[iH1][1] += fH[1]; f[iH1][2] += fH[2]; f[iH2][0] += fH[0]; f[iH2][1] += fH[1]; f[iH2][2] += fH[2]; domain->unmap(x[iH1],image[iH1],unwrapH1); domain->unmap(x[iH2],image[iH2],unwrapH2); fsum[0] -= fO[0]*unwrap[0]+fO[1]*unwrap[1]+fO[2]*unwrap[2]+fH[0]*unwrapH1[0]+fH[1]*unwrapH1[1]+fH[2]*unwrapH1[2]+fH[0]*unwrapH2[0]+fH[1]*unwrapH2[1]+fH[2]*unwrapH2[2]; fsum[1] += fx; fsum[2] += fy; fsum[3] += fz; if (evflag) { v[0] = fO[0]*unwrap[0]+fH[0]*unwrapH1[0]+fH[0]*unwrapH2[0]; v[1] = fO[1]*unwrap[1]+fH[1]*unwrapH1[1]+fH[1]*unwrapH2[1]; v[2] = fO[2]*unwrap[2]+fH[2]*unwrapH1[2]+fH[2]*unwrapH2[2]; v[3] = fO[0]*unwrap[1]+fH[0]*unwrapH1[1]+fH[0]*unwrapH2[1]; v[4] = fO[0]*unwrap[2]+fH[0]*unwrapH1[2]+fH[0]*unwrapH2[2]; v[5] = fO[1]*unwrap[2]+fH[1]*unwrapH1[2]+fH[1]*unwrapH2[2]; v_tally(i, v); } } } } /* ------------------------------TIP4P----------------------------------- */ // dipole interactions // no force, torque = mu cross E, potential energy = -mu dot E if (muflag) { double **mu = atom->mu; double **t = atom->torque; double tx,ty,tz; for (int i = 0; i < nlocal; i++) if (mask[i] & groupbit) { if (region && !region->match(x[i][0],x[i][1],x[i][2])) continue; tx = ez*mu[i][1] - ey*mu[i][2]; ty = ex*mu[i][2] - ez*mu[i][0]; tz = ey*mu[i][0] - ex*mu[i][1]; t[i][0] += tx; t[i][1] += ty; t[i][2] += tz; fsum[0] -= mu[i][0]*ex + mu[i][1]*ey + mu[i][2]*ez; } } // variable efield, wrap with clear/add // potential energy = evar if defined, else 0.0 } else { modify->clearstep_compute(); if (xstyle == EQUAL) ex = qe2f * input->variable->compute_equal(xvar); else if (xstyle == ATOM) input->variable->compute_atom(xvar,igroup,&efield[0][0],4,0); if (ystyle == EQUAL) ey = qe2f * input->variable->compute_equal(yvar); else if (ystyle == ATOM) input->variable->compute_atom(yvar,igroup,&efield[0][1],4,0); if (zstyle == EQUAL) ez = qe2f * input->variable->compute_equal(zvar); else if (zstyle == ATOM) input->variable->compute_atom(zvar,igroup,&efield[0][2],4,0); if (estyle == ATOM) input->variable->compute_atom(evar,igroup,&efield[0][3],4,0); modify->addstep_compute(update->ntimestep + 1); // charge interactions // force = qE if (qflag) { for (int i = 0; i < nlocal; i++) if (mask[i] & groupbit) { if (region && !region->match(x[i][0],x[i][1],x[i][2])) continue; if (xstyle == ATOM) fx = qe2f * q[i]*efield[i][0]; else fx = q[i]*ex; f[i][0] += fx; fsum[1] += fx; if (ystyle == ATOM) fy = qe2f * q[i]*efield[i][1]; else fy = q[i]*ey; f[i][1] += fy; fsum[2] += fy; if (zstyle == ATOM) fz = qe2f * q[i]*efield[i][2]; else fz = q[i]*ez; f[i][2] += fz; fsum[3] += fz; if (estyle == ATOM) fsum[0] += efield[0][3]; } } // dipole interactions // no force, torque = mu cross E if (muflag) { double **mu = atom->mu; double **t = atom->torque; double tx,ty,tz; for (int i = 0; i < nlocal; i++) if (mask[i] & groupbit) { if (region && !region->match(x[i][0],x[i][1],x[i][2])) continue; tx = ez*mu[i][1] - ey*mu[i][2]; ty = ex*mu[i][2] - ez*mu[i][0]; tz = ey*mu[i][0] - ex*mu[i][1]; t[i][0] += tx; t[i][1] += ty; t[i][2] += tz; } } } } /* ---------------------------------------------------------------------- */ void FixEfieldTIP4P::post_force_respa(int vflag, int ilevel, int /*iloop*/) // TIP4P { if (ilevel == ilevel_respa) post_force(vflag); } /* ---------------------------------------------------------------------- */ void FixEfieldTIP4P::min_post_force(int vflag) // TIP4P { post_force(vflag); } /* ---------------------------------------------------------------------- memory usage of local atom-based array ------------------------------------------------------------------------- */ double FixEfieldTIP4P::memory_usage() // TIP4P { double bytes = 0.0; if (varflag == ATOM) bytes = atom->nmax*4 * sizeof(double); return bytes; } /* ---------------------------------------------------------------------- return energy added by fix ------------------------------------------------------------------------- */ double FixEfieldTIP4P::compute_scalar(void) // TIP4P { if (force_flag == 0) { MPI_Allreduce(fsum,fsum_all,4,MPI_DOUBLE,MPI_SUM,world); force_flag = 1; } return fsum_all[0]; } /* ---------------------------------------------------------------------- return total extra force due to fix ------------------------------------------------------------------------- */ double FixEfieldTIP4P::compute_vector(int n) // TIP4P { if (force_flag == 0) { MPI_Allreduce(fsum,fsum_all,4,MPI_DOUBLE,MPI_SUM,world); force_flag = 1; } return fsum_all[n+1]; }