2010-09-17 03:34:48 +08:00
|
|
|
/* ----------------------------------------------------------------------
|
|
|
|
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
|
2012-06-07 06:47:51 +08:00
|
|
|
certain rights in this software. This software is distributed under
|
2010-09-17 03:34:48 +08:00
|
|
|
the GNU General Public License.
|
|
|
|
|
|
|
|
See the README file in the top-level LAMMPS directory.
|
|
|
|
------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
#include "stdio.h"
|
|
|
|
#include "string.h"
|
2013-01-05 01:15:32 +08:00
|
|
|
#include "stdlib.h"
|
2010-09-17 03:34:48 +08:00
|
|
|
#include "fix_external.h"
|
|
|
|
#include "atom.h"
|
|
|
|
#include "update.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "error.h"
|
2013-06-29 03:00:58 +08:00
|
|
|
#include "force.h"
|
2010-09-17 03:34:48 +08:00
|
|
|
|
|
|
|
using namespace LAMMPS_NS;
|
2012-02-02 00:27:41 +08:00
|
|
|
using namespace FixConst;
|
2010-09-17 03:34:48 +08:00
|
|
|
|
2013-01-05 01:15:32 +08:00
|
|
|
enum{PF_CALLBACK,PF_ARRAY};
|
|
|
|
|
2010-09-17 03:34:48 +08:00
|
|
|
/* ---------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
FixExternal::FixExternal(LAMMPS *lmp, int narg, char **arg) :
|
|
|
|
Fix(lmp, narg, arg)
|
|
|
|
{
|
2013-01-05 01:15:32 +08:00
|
|
|
if (narg < 4) error->all(FLERR,"Illegal fix external command");
|
|
|
|
|
2013-07-30 08:07:18 +08:00
|
|
|
scalar_flag = 1;
|
|
|
|
global_freq = 1;
|
|
|
|
extscalar = 1;
|
|
|
|
|
2013-01-05 01:15:32 +08:00
|
|
|
if (strcmp(arg[3],"pf/callback") == 0) {
|
|
|
|
if (narg != 6) error->all(FLERR,"Illegal fix external command");
|
|
|
|
mode = PF_CALLBACK;
|
2013-06-29 03:00:58 +08:00
|
|
|
ncall = force->inumeric(FLERR,arg[4]);
|
|
|
|
napply = force->inumeric(FLERR,arg[5]);
|
2013-01-05 01:15:32 +08:00
|
|
|
if (ncall <= 0 || napply <= 0)
|
|
|
|
error->all(FLERR,"Illegal fix external command");
|
|
|
|
} else if (strcmp(arg[3],"pf/array") == 0) {
|
|
|
|
if (narg != 5) error->all(FLERR,"Illegal fix external command");
|
|
|
|
mode = PF_ARRAY;
|
2013-06-29 03:00:58 +08:00
|
|
|
napply = force->inumeric(FLERR,arg[4]);
|
2013-01-05 01:15:32 +08:00
|
|
|
if (napply <= 0) error->all(FLERR,"Illegal fix external command");
|
|
|
|
} else error->all(FLERR,"Illegal fix external command");
|
2010-09-17 03:34:48 +08:00
|
|
|
|
|
|
|
callback = NULL;
|
|
|
|
|
2013-01-05 01:15:32 +08:00
|
|
|
// perform initial allocation of atom-based array
|
|
|
|
// register with Atom class
|
|
|
|
|
2010-09-17 03:34:48 +08:00
|
|
|
fexternal = NULL;
|
2013-01-05 01:15:32 +08:00
|
|
|
grow_arrays(atom->nmax);
|
|
|
|
atom->add_callback(0);
|
2013-07-30 08:07:18 +08:00
|
|
|
|
|
|
|
user_energy = 0.0;
|
2010-09-17 03:34:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
FixExternal::~FixExternal()
|
|
|
|
{
|
2013-01-05 01:15:32 +08:00
|
|
|
// unregister callbacks to this fix from Atom class
|
|
|
|
|
|
|
|
atom->delete_callback(id,0);
|
|
|
|
|
2011-03-26 05:13:51 +08:00
|
|
|
memory->destroy(fexternal);
|
2010-09-17 03:34:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
int FixExternal::setmask()
|
|
|
|
{
|
|
|
|
int mask = 0;
|
2013-01-05 01:15:32 +08:00
|
|
|
if (mode == PF_CALLBACK || mode == PF_ARRAY) {
|
|
|
|
mask |= POST_FORCE;
|
2013-07-30 08:07:18 +08:00
|
|
|
mask |= THERMO_ENERGY;
|
2013-01-05 01:15:32 +08:00
|
|
|
mask |= MIN_POST_FORCE;
|
|
|
|
}
|
2010-09-17 03:34:48 +08:00
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
void FixExternal::init()
|
|
|
|
{
|
2013-01-05 01:15:32 +08:00
|
|
|
if (mode == PF_CALLBACK && callback == NULL)
|
2012-05-19 00:35:19 +08:00
|
|
|
error->all(FLERR,"Fix external callback function not set");
|
2010-09-17 03:34:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
void FixExternal::setup(int vflag)
|
|
|
|
{
|
|
|
|
post_force(vflag);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------- */
|
|
|
|
|
2010-11-22 23:58:44 +08:00
|
|
|
void FixExternal::min_setup(int vflag)
|
|
|
|
{
|
|
|
|
post_force(vflag);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------- */
|
|
|
|
|
2010-09-17 03:34:48 +08:00
|
|
|
void FixExternal::post_force(int vflag)
|
|
|
|
{
|
2013-01-05 01:15:32 +08:00
|
|
|
bigint ntimestep = update->ntimestep;
|
2010-09-17 03:34:48 +08:00
|
|
|
|
|
|
|
// invoke the callback in driver program
|
|
|
|
// it will fill fexternal with forces
|
|
|
|
|
2013-03-23 06:16:40 +08:00
|
|
|
if (mode == PF_CALLBACK && ntimestep % ncall == 0)
|
2013-01-05 01:15:32 +08:00
|
|
|
(this->callback)(ptr_caller,update->ntimestep,
|
|
|
|
atom->nlocal,atom->tag,atom->x,fexternal);
|
2010-09-17 03:34:48 +08:00
|
|
|
|
|
|
|
// add forces from fexternal to atoms in group
|
|
|
|
|
2013-03-23 06:16:40 +08:00
|
|
|
if (ntimestep % napply == 0) {
|
2013-01-05 01:15:32 +08:00
|
|
|
double **f = atom->f;
|
|
|
|
int *mask = atom->mask;
|
|
|
|
int nlocal = atom->nlocal;
|
|
|
|
|
|
|
|
for (int i = 0; i < nlocal; i++)
|
|
|
|
if (mask[i] & groupbit) {
|
|
|
|
f[i][0] += fexternal[i][0];
|
|
|
|
f[i][1] += fexternal[i][1];
|
|
|
|
f[i][2] += fexternal[i][2];
|
|
|
|
}
|
|
|
|
}
|
2010-09-17 03:34:48 +08:00
|
|
|
}
|
|
|
|
|
2010-11-22 23:58:44 +08:00
|
|
|
/* ---------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
void FixExternal::min_post_force(int vflag)
|
|
|
|
{
|
|
|
|
post_force(vflag);
|
|
|
|
}
|
|
|
|
|
2013-07-30 08:07:18 +08:00
|
|
|
/* ---------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
void FixExternal::set_energy(double eng)
|
|
|
|
{
|
|
|
|
user_energy = eng;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------
|
|
|
|
potential energy of added force
|
|
|
|
up to user to set it via set_energy()
|
|
|
|
------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
double FixExternal::compute_scalar()
|
|
|
|
{
|
|
|
|
return user_energy;
|
|
|
|
}
|
|
|
|
|
2013-01-05 01:15:32 +08:00
|
|
|
/* ----------------------------------------------------------------------
|
|
|
|
memory usage of local atom-based array
|
|
|
|
------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
double FixExternal::memory_usage()
|
|
|
|
{
|
|
|
|
double bytes = 3*atom->nmax * sizeof(double);
|
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------
|
|
|
|
allocate atom-based array
|
|
|
|
------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
void FixExternal::grow_arrays(int nmax)
|
|
|
|
{
|
|
|
|
memory->grow(fexternal,nmax,3,"external:fexternal");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------
|
|
|
|
copy values within local atom-based array
|
|
|
|
------------------------------------------------------------------------- */
|
|
|
|
|
2013-02-09 00:44:04 +08:00
|
|
|
void FixExternal::copy_arrays(int i, int j, int delflag)
|
2013-01-05 01:15:32 +08:00
|
|
|
{
|
|
|
|
fexternal[j][0] = fexternal[i][0];
|
|
|
|
fexternal[j][1] = fexternal[i][1];
|
|
|
|
fexternal[j][2] = fexternal[i][2];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------
|
|
|
|
pack values in local atom-based array for exchange with another proc
|
|
|
|
------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
int FixExternal::pack_exchange(int i, double *buf)
|
|
|
|
{
|
|
|
|
buf[0] = fexternal[i][0];
|
|
|
|
buf[1] = fexternal[i][1];
|
|
|
|
buf[2] = fexternal[i][2];
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------
|
|
|
|
unpack values in local atom-based array from exchange with another proc
|
|
|
|
------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
int FixExternal::unpack_exchange(int nlocal, double *buf)
|
|
|
|
{
|
|
|
|
fexternal[nlocal][0] = buf[0];
|
|
|
|
fexternal[nlocal][1] = buf[1];
|
|
|
|
fexternal[nlocal][2] = buf[2];
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
2010-09-17 03:34:48 +08:00
|
|
|
/* ----------------------------------------------------------------------
|
|
|
|
external caller sets a callback function to invoke in post_force()
|
|
|
|
------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
void FixExternal::set_callback(FnPtr caller_callback, void *caller_ptr)
|
|
|
|
{
|
|
|
|
callback = caller_callback;
|
|
|
|
ptr_caller = caller_ptr;
|
|
|
|
}
|