#include "fix_nve_dot.h"
#include "math_extra.h"
#include "atom.h"
#include "atom_vec_ellipsoid.h"
#include "update.h"
#include "error.h"
using namespace LAMMPS_NS;
using namespace FixConst;
using namespace MathExtra;
#define INERTIA 0.2
FixNVEDot::FixNVEDot(LAMMPS *lmp, int narg, char **arg) :
FixNVE(lmp, narg, arg) {}
void FixNVEDot::init()
{
avec = (AtomVecEllipsoid *) atom->style_match("ellipsoid");
if (!avec)
error->all(FLERR,"Compute nve/dot requires atom style ellipsoid");
int *ellipsoid = atom->ellipsoid;
int *mask = atom->mask;
int nlocal = atom->nlocal;
for (int i = 0; i < nlocal; i++)
if (mask[i] & groupbit)
if (ellipsoid[i] < 0)
error->one(FLERR,"Fix nve/dot requires extended particles");
FixNVE::init();
}
void FixNVEDot::initial_integrate(int )
{
double *shape,*quat;
double fquat[4],conjqm[4],inertia[3];
AtomVecEllipsoid::Bonus *bonus = avec->bonus;
int *ellipsoid = atom->ellipsoid;
double **x = atom->x;
double **v = atom->v;
double **f = atom->f;
double **angmom = atom->angmom;
double **torque = atom->torque;
double *rmass = atom->rmass;
int *mask = atom->mask;
int nlocal = atom->nlocal;
if (igroup == atom->firstgroup) nlocal = atom->nfirst;
dt = update->dt;
dthlf = 0.5 * dt;
for (int i = 0; i < nlocal; i++)
if (mask[i] & groupbit) {
dthlfm = dthlf / rmass[i];
quat = bonus[ellipsoid[i]].quat;
shape = bonus[ellipsoid[i]].shape;
v[i][0] += dthlfm * f[i][0];
v[i][1] += dthlfm * f[i][1];
v[i][2] += dthlfm * f[i][2];
x[i][0] += dt * v[i][0];
x[i][1] += dt * v[i][1];
x[i][2] += dt * v[i][2];
vec3_to_vec4(quat,angmom[i],conjqm);
conjqm[0] *= 2.0;
conjqm[1] *= 2.0;
conjqm[2] *= 2.0;
conjqm[3] *= 2.0;
vec3_to_vec4(quat,torque[i],fquat);
conjqm[0] += dt * fquat[0];
conjqm[1] += dt * fquat[1];
conjqm[2] += dt * fquat[2];
conjqm[3] += dt * fquat[3];
inertia[0] = INERTIA*rmass[i] * (shape[1]*shape[1]+shape[2]*shape[2]);
inertia[1] = INERTIA*rmass[i] * (shape[0]*shape[0]+shape[2]*shape[2]);
inertia[2] = INERTIA*rmass[i] * (shape[0]*shape[0]+shape[1]*shape[1]);
no_squish_rotate(3,conjqm,quat,inertia,dthlf);
no_squish_rotate(2,conjqm,quat,inertia,dthlf);
no_squish_rotate(1,conjqm,quat,inertia,dt);
no_squish_rotate(2,conjqm,quat,inertia,dthlf);
no_squish_rotate(3,conjqm,quat,inertia,dthlf);
qnormalize(quat);
vec4_to_vec3(quat,conjqm,angmom[i]);
angmom[i][0] *= 0.5;
angmom[i][1] *= 0.5;
angmom[i][2] *= 0.5;
}
}
void FixNVEDot::final_integrate()
{
double *quat;
double fquat[4],conjqm[4];
double conjqm_dot_quat;
AtomVecEllipsoid::Bonus *bonus = avec->bonus;
int *ellipsoid = atom->ellipsoid;
double **v = atom->v;
double **f = atom->f;
double **angmom = atom->angmom;
double **torque = atom->torque;
double *rmass = atom->rmass;
int *mask = atom->mask;
int nlocal = atom->nlocal;
if (igroup == atom->firstgroup) nlocal = atom->nfirst;
dt = update->dt;
dthlf = 0.5 * dt;
for (int i = 0; i < nlocal; i++)
if (mask[i] & groupbit) {
dthlfm = dthlf / rmass[i];
quat = bonus[ellipsoid[i]].quat;
v[i][0] += dthlfm * f[i][0];
v[i][1] += dthlfm * f[i][1];
v[i][2] += dthlfm * f[i][2];
vec3_to_vec4(quat,angmom[i],conjqm);
conjqm[0] *= 2.0;
conjqm[1] *= 2.0;
conjqm[2] *= 2.0;
conjqm[3] *= 2.0;
vec3_to_vec4(quat,torque[i],fquat);
conjqm[0] += dt * fquat[0];
conjqm[1] += dt * fquat[1];
conjqm[2] += dt * fquat[2];
conjqm[3] += dt * fquat[3];
conjqm_dot_quat = conjqm[0]*quat[0] + conjqm[1]*quat[1] + conjqm[2]*quat[2] + conjqm[3]*quat[3];
conjqm[0] -= conjqm_dot_quat * quat[0];
conjqm[1] -= conjqm_dot_quat * quat[1];
conjqm[2] -= conjqm_dot_quat * quat[2];
conjqm[3] -= conjqm_dot_quat * quat[3];
vec4_to_vec3(quat,conjqm,angmom[i]);
angmom[i][0] *= 0.5;
angmom[i][1] *= 0.5;
angmom[i][2] *= 0.5;
}
}