git-svn-id: svn://svn.icms.temple.edu/lammps-ro/trunk@4028 f3b2605a-c512-4ea7-a41b-209d697bcdaa
This commit is contained in:
269
src/fix_setforce.cpp
Normal file
269
src/fix_setforce.cpp
Normal file
@ -0,0 +1,269 @@
|
||||
/* ----------------------------------------------------------------------
|
||||
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
|
||||
http://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.
|
||||
------------------------------------------------------------------------- */
|
||||
|
||||
#include "string.h"
|
||||
#include "stdlib.h"
|
||||
#include "fix_setforce.h"
|
||||
#include "atom.h"
|
||||
#include "update.h"
|
||||
#include "respa.h"
|
||||
#include "input.h"
|
||||
#include "variable.h"
|
||||
#include "memory.h"
|
||||
#include "error.h"
|
||||
|
||||
using namespace LAMMPS_NS;
|
||||
|
||||
enum{NONE,CONSTANT,EQUAL,ATOM};
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
FixSetForce::FixSetForce(LAMMPS *lmp, int narg, char **arg) :
|
||||
Fix(lmp, narg, arg)
|
||||
{
|
||||
if (narg != 6) error->all("Illegal fix setforce command");
|
||||
|
||||
vector_flag = 1;
|
||||
size_vector = 3;
|
||||
global_freq = 1;
|
||||
extvector = 1;
|
||||
|
||||
xstr = ystr = zstr = NULL;
|
||||
|
||||
if (strstr(arg[3],"v_") == arg[3]) {
|
||||
int n = strlen(&arg[3][2]) + 1;
|
||||
xstr = new char[n];
|
||||
strcpy(xstr,&arg[3][2]);
|
||||
} else if (strcmp(arg[3],"NULL") == 0) {
|
||||
xstyle = NONE;
|
||||
} else {
|
||||
xvalue = atof(arg[3]);
|
||||
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 if (strcmp(arg[4],"NULL") == 0) {
|
||||
ystyle = NONE;
|
||||
} else {
|
||||
yvalue = atof(arg[4]);
|
||||
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 if (strcmp(arg[5],"NULL") == 0) {
|
||||
zstyle = NONE;
|
||||
} else {
|
||||
zvalue = atof(arg[5]);
|
||||
zstyle = CONSTANT;
|
||||
}
|
||||
|
||||
force_flag = 0;
|
||||
foriginal[0] = foriginal[1] = foriginal[2] = 0.0;
|
||||
|
||||
maxatom = 0;
|
||||
sforce = NULL;
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
FixSetForce::~FixSetForce()
|
||||
{
|
||||
delete [] xstr;
|
||||
delete [] ystr;
|
||||
delete [] zstr;
|
||||
memory->destroy_2d_double_array(sforce);
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
int FixSetForce::setmask()
|
||||
{
|
||||
int mask = 0;
|
||||
mask |= POST_FORCE;
|
||||
mask |= POST_FORCE_RESPA;
|
||||
mask |= MIN_POST_FORCE;
|
||||
return mask;
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
void FixSetForce::init()
|
||||
{
|
||||
// check variables
|
||||
|
||||
if (xstr) {
|
||||
xvar = input->variable->find(xstr);
|
||||
if (xvar < 0) error->all("Variable name for fix setforce does not exist");
|
||||
if (input->variable->equalstyle(xvar)) xstyle = EQUAL;
|
||||
else if (input->variable->atomstyle(xvar)) xstyle = ATOM;
|
||||
else error->all("Variable for fix setforce is invalid style");
|
||||
}
|
||||
if (ystr) {
|
||||
yvar = input->variable->find(ystr);
|
||||
if (yvar < 0) error->all("Variable name for fix setforce does not exist");
|
||||
if (input->variable->equalstyle(yvar)) ystyle = EQUAL;
|
||||
else if (input->variable->atomstyle(yvar)) ystyle = ATOM;
|
||||
else error->all("Variable for fix setforce is invalid style");
|
||||
}
|
||||
if (zstr) {
|
||||
zvar = input->variable->find(zstr);
|
||||
if (zvar < 0) error->all("Variable name for fix setforce does not exist");
|
||||
if (input->variable->equalstyle(zvar)) zstyle = EQUAL;
|
||||
else if (input->variable->atomstyle(zvar)) zstyle = ATOM;
|
||||
else error->all("Variable for fix setforce is invalid style");
|
||||
}
|
||||
|
||||
if (xstyle == ATOM || ystyle == ATOM || zstyle == ATOM)
|
||||
varflag = ATOM;
|
||||
else if (xstyle == EQUAL || ystyle == EQUAL || zstyle == EQUAL)
|
||||
varflag = EQUAL;
|
||||
else varflag = CONSTANT;
|
||||
|
||||
if (strcmp(update->integrate_style,"respa") == 0)
|
||||
nlevels_respa = ((Respa *) update->integrate)->nlevels;
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
void FixSetForce::setup(int vflag)
|
||||
{
|
||||
if (strcmp(update->integrate_style,"verlet") == 0)
|
||||
post_force(vflag);
|
||||
else
|
||||
for (int ilevel = 0; ilevel < nlevels_respa; ilevel++) {
|
||||
((Respa *) update->integrate)->copy_flevel_f(ilevel);
|
||||
post_force_respa(vflag,ilevel,0);
|
||||
((Respa *) update->integrate)->copy_f_flevel(ilevel);
|
||||
}
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
void FixSetForce::min_setup(int vflag)
|
||||
{
|
||||
post_force(vflag);
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
void FixSetForce::post_force(int vflag)
|
||||
{
|
||||
double **f = atom->f;
|
||||
int *mask = atom->mask;
|
||||
int nlocal = atom->nlocal;
|
||||
|
||||
// reallocate sforce array if necessary
|
||||
|
||||
if (varflag == ATOM && nlocal > maxatom) {
|
||||
maxatom = atom->nmax;
|
||||
memory->destroy_2d_double_array(sforce);
|
||||
sforce = memory->create_2d_double_array(maxatom,3,"setforce:sforce");
|
||||
}
|
||||
|
||||
foriginal[0] = foriginal[1] = foriginal[2] = 0.0;
|
||||
force_flag = 0;
|
||||
|
||||
if (varflag == CONSTANT) {
|
||||
for (int i = 0; i < nlocal; i++)
|
||||
if (mask[i] & groupbit) {
|
||||
foriginal[0] += f[i][0];
|
||||
foriginal[1] += f[i][1];
|
||||
foriginal[2] += f[i][2];
|
||||
if (xstyle) f[i][0] = xvalue;
|
||||
if (ystyle) f[i][1] = yvalue;
|
||||
if (zstyle) f[i][2] = zvalue;
|
||||
}
|
||||
|
||||
} else {
|
||||
if (xstyle == EQUAL) xvalue = input->variable->compute_equal(xvar);
|
||||
else if (xstyle == ATOM)
|
||||
input->variable->compute_atom(xvar,igroup,&sforce[0][0],3,0);
|
||||
if (ystyle == EQUAL) yvalue = input->variable->compute_equal(yvar);
|
||||
else if (ystyle == ATOM)
|
||||
input->variable->compute_atom(yvar,igroup,&sforce[0][1],3,0);
|
||||
if (zstyle == EQUAL) zvalue = input->variable->compute_equal(zvar);
|
||||
else if (zstyle == ATOM)
|
||||
input->variable->compute_atom(zvar,igroup,&sforce[0][2],3,0);
|
||||
|
||||
for (int i = 0; i < nlocal; i++)
|
||||
if (mask[i] & groupbit) {
|
||||
foriginal[0] += f[i][0];
|
||||
foriginal[1] += f[i][1];
|
||||
foriginal[2] += f[i][2];
|
||||
if (xstyle == ATOM) f[i][0] = sforce[i][0];
|
||||
else if (xstyle) f[i][0] = xvalue;
|
||||
if (ystyle == ATOM) f[i][1] = sforce[i][1];
|
||||
else if (ystyle) f[i][1] = yvalue;
|
||||
if (zstyle == ATOM) f[i][2] = sforce[i][2];
|
||||
else if (zstyle) f[i][2] = zvalue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
void FixSetForce::post_force_respa(int vflag, int ilevel, int iloop)
|
||||
{
|
||||
// set force to desired value on outermost level, 0.0 on other levels
|
||||
|
||||
if (ilevel == nlevels_respa-1) post_force(vflag);
|
||||
else {
|
||||
double **f = atom->f;
|
||||
int *mask = atom->mask;
|
||||
int nlocal = atom->nlocal;
|
||||
|
||||
for (int i = 0; i < nlocal; i++)
|
||||
if (mask[i] & groupbit) {
|
||||
if (xstyle) f[i][0] = 0.0;
|
||||
if (ystyle) f[i][1] = 0.0;
|
||||
if (zstyle) f[i][2] = 0.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
void FixSetForce::min_post_force(int vflag)
|
||||
{
|
||||
post_force(vflag);
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
return components of total force on fix group before force was changed
|
||||
------------------------------------------------------------------------- */
|
||||
|
||||
double FixSetForce::compute_vector(int n)
|
||||
{
|
||||
// only sum across procs one time
|
||||
|
||||
if (force_flag == 0) {
|
||||
MPI_Allreduce(foriginal,foriginal_all,3,MPI_DOUBLE,MPI_SUM,world);
|
||||
force_flag = 1;
|
||||
}
|
||||
return foriginal_all[n];
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
memory usage of local atom-based array
|
||||
------------------------------------------------------------------------- */
|
||||
|
||||
double FixSetForce::memory_usage()
|
||||
{
|
||||
double bytes = 0.0;
|
||||
if (varflag == ATOM) bytes = atom->nmax*3 * sizeof(double);
|
||||
return bytes;
|
||||
}
|
||||
Reference in New Issue
Block a user