From 549ea506d4d8cd9a7e85aee85fe4fd59fc8aef62 Mon Sep 17 00:00:00 2001 From: Jacob Gissinger Date: Sun, 29 Oct 2023 15:46:55 -0400 Subject: [PATCH] add var keyword to fix_deposit borrowed from create_atoms --- src/fix_deposit.cpp | 88 +++++++++++++++++++++++++++++++++++++++++++-- src/fix_deposit.h | 6 +++- 2 files changed, 91 insertions(+), 3 deletions(-) diff --git a/src/fix_deposit.cpp b/src/fix_deposit.cpp index d2bcb3e855..7f11987ad7 100644 --- a/src/fix_deposit.cpp +++ b/src/fix_deposit.cpp @@ -20,6 +20,7 @@ #include "domain.h" #include "error.h" #include "fix.h" +#include "input.h" #include "lattice.h" #include "math_const.h" #include "math_extra.h" @@ -29,6 +30,7 @@ #include "random_park.h" #include "region.h" #include "update.h" +#include "variable.h" #include #include @@ -209,6 +211,10 @@ FixDeposit::~FixDeposit() delete [] idrigid; delete [] idshake; delete [] idregion; + delete [] vstr; + delete [] xstr; + delete [] ystr; + delete [] zstr; memory->destroy(coords); memory->destroy(imageflags); } @@ -361,6 +367,8 @@ void FixDeposit::pre_exchange() } while (iregion->match(coord[0],coord[1],coord[2]) == 0); } else error->all(FLERR,"Unknown particle distribution in fix deposit"); + if (varflag && vartest(coord[0],coord[1],coord[2]) == 0) continue; + // adjust vertical coord by offset if (dimension == 2) coord[1] += offset; @@ -583,8 +591,10 @@ void FixDeposit::pre_exchange() // warn if not successful b/c too many attempts - if (!success && comm->me == 0) - error->warning(FLERR,"Particle deposition was unsuccessful"); + if (warnflag && !success && comm->me == 0) { + error->warning(FLERR,"One or more particle depositions were unsuccessful"); + warnflag = 0; + } // reset global natoms,nbonds,etc // increment maxtag_all and maxmol_all if necessary @@ -661,6 +671,8 @@ void FixDeposit::options(int narg, char **arg) iregion = nullptr; idregion = nullptr; + varflag = 0; + vstr = xstr = ystr = zstr = nullptr; mode = ATOM; molfrac = nullptr; rigidflag = 0; @@ -680,6 +692,7 @@ void FixDeposit::options(int narg, char **arg) scaleflag = 1; targetflag = 0; orientflag = 0; + warnflag = 1; rx = 0.0; ry = 0.0; rz = 0.0; @@ -693,6 +706,27 @@ void FixDeposit::options(int narg, char **arg) idregion = utils::strdup(arg[iarg+1]); iarg += 2; + } else if (strcmp(arg[iarg], "var") == 0) { + if (iarg + 2 > narg) utils::missing_cmd_args(FLERR, "create_atoms var", error); + delete[] vstr; + vstr = utils::strdup(arg[iarg + 1]); + varflag = 1; + iarg += 2; + } else if (strcmp(arg[iarg], "set") == 0) { + if (iarg + 3 > narg) utils::missing_cmd_args(FLERR, "create_atoms set", error); + if (strcmp(arg[iarg + 1], "x") == 0) { + delete[] xstr; + xstr = utils::strdup(arg[iarg + 2]); + } else if (strcmp(arg[iarg + 1], "y") == 0) { + delete[] ystr; + ystr = utils::strdup(arg[iarg + 2]); + } else if (strcmp(arg[iarg + 1], "z") == 0) { + delete[] zstr; + zstr = utils::strdup(arg[iarg + 2]); + } else + error->all(FLERR, "Unknown create_atoms set option {}", arg[iarg + 2]); + iarg += 3; + } else if (strcmp(arg[iarg],"mol") == 0) { if (iarg+2 > narg) error->all(FLERR,"Illegal fix deposit command"); int imol = atom->find_molecule(arg[iarg+1]); @@ -815,6 +849,39 @@ void FixDeposit::options(int narg, char **arg) iarg += 4; } else error->all(FLERR,"Illegal fix deposit command"); } + + // error check and further setup for variable test + + if (!vstr && (xstr || ystr || zstr)) + error->all(FLERR, "Incomplete use of variables in create_atoms command"); + if (vstr && (!xstr && !ystr && !zstr)) + error->all(FLERR, "Incomplete use of variables in create_atoms command"); + + if (varflag) { + vvar = input->variable->find(vstr); + if (vvar < 0) error->all(FLERR, "Variable {} for create_atoms does not exist", vstr); + if (!input->variable->equalstyle(vvar)) + error->all(FLERR, "Variable for create_atoms is invalid style"); + + if (xstr) { + xvar = input->variable->find(xstr); + if (xvar < 0) error->all(FLERR, "Variable {} for create_atoms does not exist", xstr); + if (!input->variable->internalstyle(xvar)) + error->all(FLERR, "Variable for create_atoms is invalid style"); + } + if (ystr) { + yvar = input->variable->find(ystr); + if (yvar < 0) error->all(FLERR, "Variable {} for create_atoms does not exist", ystr); + if (!input->variable->internalstyle(yvar)) + error->all(FLERR, "Variable for create_atoms is invalid style"); + } + if (zstr) { + zvar = input->variable->find(zstr); + if (zvar < 0) error->all(FLERR, "Variable {} for create_atoms does not exist", zstr); + if (!input->variable->internalstyle(zvar)) + error->all(FLERR, "Variable for create_atoms is invalid style"); + } + } } /* ---------------------------------------------------------------------- @@ -908,3 +975,20 @@ void *FixDeposit::extract(const char *str, int &itype) return nullptr; } + +/* ---------------------------------------------------------------------- + test a generated atom position against variable evaluation + first set x,y,z values in internal variables +------------------------------------------------------------------------- */ + +int FixDeposit::vartest(double x, double y, double z) +{ + if (xstr) input->variable->internal_set(xvar, x); + if (ystr) input->variable->internal_set(yvar, y); + if (zstr) input->variable->internal_set(zvar, z); + + double value = input->variable->compute_equal(vvar); + + if (value == 0.0) return 0; + return 1; +} diff --git a/src/fix_deposit.h b/src/fix_deposit.h index 2b866dc068..5d2824fba3 100644 --- a/src/fix_deposit.h +++ b/src/fix_deposit.h @@ -40,7 +40,8 @@ class FixDeposit : public Fix { private: int ninsert, ntype, nfreq, seed; int globalflag, localflag, maxattempt, rateflag, scaleflag, targetflag; - int mode, rigidflag, shakeflag, idnext, distflag, orientflag; + int mode, rigidflag, shakeflag, idnext, distflag, orientflag, warnflag; + int varflag, vvar, xvar, yvar, zvar; double lo, hi, deltasq, nearsq, rate, sigma; double vxlo, vxhi, vylo, vyhi, vzlo, vzhi; double xlo, xhi, ylo, yhi, zlo, zhi, xmid, ymid, zmid; @@ -48,6 +49,8 @@ class FixDeposit : public Fix { class Region *iregion; char *idregion; char *idrigid, *idshake; + char *vstr, *xstr, *ystr, *zstr; + char *xstr_copy, *ystr_copy, *zstr_copy; class Molecule **onemols; int nmol, natom_max; @@ -64,6 +67,7 @@ class FixDeposit : public Fix { void find_maxid(); void options(int, char **); + int vartest(double, double, double); // evaluate a variable with new atom position }; } // namespace LAMMPS_NS