#include "fix_manifoldforce.h"
#include <mpi.h>
#include <cstring>
#include "atom.h"
#include "update.h"
#include "respa.h"
#include "error.h"
#include "force.h"
#include "manifold.h"
#include "manifold_factory.h"
using namespace LAMMPS_NS;
using namespace FixConst;
using namespace user_manifold;
static bool was_var( const char *arg )
{
return strstr( arg, "v_" ) == arg;
}
static bool str_eq( const char *str1, const char *str2 )
{
return strcmp(str1,str2) == 0;
}
FixManifoldForce::FixManifoldForce(LAMMPS *lmp, int narg, char **arg) :
Fix(lmp, narg, arg)
{
int me = -1;
MPI_Comm_rank(world,&me);
if (narg < 5) {
error->all(FLERR,"Illegal fix manifoldforce! No manifold given");
}
const char *m_name = arg[3];
ptr_m = create_manifold(m_name,lmp,narg,arg);
if (!ptr_m) {
char msg[2048];
snprintf(msg,2048,"Manifold pointer for manifold '%s' was NULL for some reason", arg[3]);
error->all(FLERR,msg);
}
nvars = ptr_m->nparams();
if (narg < nvars+4) {
char msg[2048];
sprintf(msg,"Manifold %s needs at least %d argument(s)!",
m_name, nvars);
error->all(FLERR,msg);
}
ptr_m->params = new double[nvars];
if (ptr_m->params == NULL) {
error->all(FLERR,"Parameter pointer was NULL!");
}
double *params = ptr_m->params;
for( int i = 0; i < nvars; ++i ){
if (was_var( arg[i+4] ))
error->all(FLERR,"Equal-style variables not allowed with fix manifoldforce");
params[i] = force->numeric(FLERR,arg[i+4]);
}
ptr_m->post_param_init();
}
int FixManifoldForce::setmask()
{
int mask = 0;
mask |= POST_FORCE;
mask |= POST_FORCE_RESPA;
mask |= MIN_POST_FORCE;
return mask;
}
void FixManifoldForce::init()
{
const bool is_good_min_style = str_eq(update->minimize_style,"hftn")
|| str_eq(update->minimize_style,"quickmin");
if (!is_good_min_style) {
error->all(FLERR,"Fix manifoldforce requires min_style hftn or quickmin");
}
}
void FixManifoldForce::setup(int vflag)
{
if (strstr(update->integrate_style,"verlet"))
post_force(vflag);
else {
int nlevels_respa = ((Respa *) update->integrate)->nlevels;
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 FixManifoldForce::min_setup(int vflag)
{
post_force(vflag);
}
void FixManifoldForce::post_force(int )
{
double **x = atom->x;
double **f = atom->f;
int *mask = atom->mask;
int nlocal = atom->nlocal;
double n[3];
double invn2;
double dot;
for (int i = 0; i < nlocal; i++){
if (mask[i] & groupbit) {
ptr_m->n(x[i],n);
invn2 = 1.0 / ( n[0]*n[0] + n[1]*n[1] + n[2]*n[2] );
dot = f[i][0]*n[0] + f[i][1]*n[1] + f[i][2]*n[2];
f[i][0] -= dot*n[0] * invn2;
f[i][1] -= dot*n[1] * invn2;
f[i][2] -= dot*n[2] * invn2;
}
}
}
void FixManifoldForce::post_force_respa(int vflag, int , int )
{
post_force(vflag);
}
void FixManifoldForce::min_post_force(int vflag)
{
post_force(vflag);
}