#include "fix_event.h"
#include "atom.h"
#include "domain.h"
#include "error.h"
#include "memory.h"
using namespace LAMMPS_NS;
using namespace FixConst;
FixEvent::FixEvent(LAMMPS *lmp, int narg, char **arg) :
Fix(lmp, narg, arg), xevent(NULL), xold(NULL), vold(NULL),
imageold(NULL), xorig(NULL), vorig(NULL), imageorig(NULL)
{
if (narg != 3) error->all(FLERR,"Illegal fix event command");
restart_global = 1;
grow_arrays(atom->nmax);
atom->add_callback(0);
}
FixEvent::~FixEvent()
{
atom->delete_callback(id,0);
memory->destroy(xevent);
memory->destroy(xold);
memory->destroy(vold);
memory->destroy(imageold);
memory->destroy(xorig);
memory->destroy(vorig);
memory->destroy(imageorig);
}
int FixEvent::setmask()
{
return 0;
}
void FixEvent::store_event()
{
double **x = atom->x;
imageint *image = atom->image;
int nlocal = atom->nlocal;
for (int i = 0; i < nlocal; i++)
domain->unmap(x[i],image[i],xevent[i]);
}
void FixEvent::restore_event()
{
double **x = atom->x;
imageint *image = atom->image;
int nlocal = atom->nlocal;
for (int i = 0; i < nlocal; i++) {
x[i][0] = xevent[i][0];
x[i][1] = xevent[i][1];
x[i][2] = xevent[i][2];
image[i] = ((imageint) IMGMAX << IMG2BITS) |
((imageint) IMGMAX << IMGBITS) | IMGMAX;
domain->remap(x[i],image[i]);
}
}
void FixEvent::store_state_quench()
{
double **x = atom->x;
double **v = atom->v;
imageint *image = atom->image;
int nlocal = atom->nlocal;
for (int i = 0; i < nlocal; i++) {
xold[i][0] = x[i][0];
xold[i][1] = x[i][1];
xold[i][2] = x[i][2];
vold[i][0] = v[i][0];
vold[i][1] = v[i][1];
vold[i][2] = v[i][2];
imageold[i] = image[i];
}
}
void FixEvent::restore_state_quench()
{
double **x = atom->x;
double **v = atom->v;
imageint *image = atom->image;
int nlocal = atom->nlocal;
for (int i = 0; i < nlocal; i++) {
x[i][0] = xold[i][0];
x[i][1] = xold[i][1];
x[i][2] = xold[i][2];
v[i][0] = vold[i][0];
v[i][1] = vold[i][1];
v[i][2] = vold[i][2];
image[i] = imageold[i];
}
}
void FixEvent::store_state_dephase()
{
double **x = atom->x;
double **v = atom->v;
imageint *image = atom->image;
int nlocal = atom->nlocal;
for (int i = 0; i < nlocal; i++) {
xorig[i][0] = x[i][0];
xorig[i][1] = x[i][1];
xorig[i][2] = x[i][2];
vorig[i][0] = v[i][0];
vorig[i][1] = v[i][1];
vorig[i][2] = v[i][2];
imageorig[i] = image[i];
}
}
void FixEvent::restore_state_dephase()
{
double **x = atom->x;
double **v = atom->v;
imageint *image = atom->image;
int nlocal = atom->nlocal;
for (int i = 0; i < nlocal; i++) {
x[i][0] = xorig[i][0];
x[i][1] = xorig[i][1];
x[i][2] = xorig[i][2];
v[i][0] = vorig[i][0];
v[i][1] = vorig[i][1];
v[i][2] = vorig[i][2];
image[i] = imageorig[i];
}
}
double FixEvent::memory_usage()
{
double bytes = 12*atom->nmax * sizeof(double);
bytes += atom->nmax*sizeof(int);
return bytes;
}
void FixEvent::grow_arrays(int nmax)
{
memory->grow(xevent,nmax,3,"event:xevent");
memory->grow(xold,nmax,3,"event:xold");
memory->grow(vold,nmax,3,"event:vold");
memory->grow(imageold,nmax,"event:imageold");
memory->grow(xorig,nmax,3,"event:xorig");
memory->grow(vorig,nmax,3,"event:vorig");
memory->grow(imageorig,nmax,"event:imageorig");
array_atom = xevent;
}
void FixEvent::copy_arrays(int i, int j, int )
{
xevent[j][0] = xevent[i][0];
xevent[j][1] = xevent[i][1];
xevent[j][2] = xevent[i][2];
xold[j][0] = xold[i][0];
xold[j][1] = xold[i][1];
xold[j][2] = xold[i][2];
vold[j][0] = vold[i][0];
vold[j][1] = vold[i][1];
vold[j][2] = vold[i][2];
imageold[j] = imageold[i];
xorig[j][0] = xorig[i][0];
xorig[j][1] = xorig[i][1];
xorig[j][2] = xorig[i][2];
vorig[j][0] = vorig[i][0];
vorig[j][1] = vorig[i][1];
vorig[j][2] = vorig[i][2];
imageorig[j] = imageorig[i];
}
int FixEvent::pack_exchange(int i, double *buf)
{
buf[0] = xevent[i][0];
buf[1] = xevent[i][1];
buf[2] = xevent[i][2];
buf[3] = xold[i][0];
buf[4] = xold[i][1];
buf[5] = xold[i][2];
buf[6] = vold[i][0];
buf[7] = vold[i][1];
buf[8] = vold[i][2];
buf[9] = imageold[i];
buf[10] = xorig[i][0];
buf[11] = xorig[i][1];
buf[12] = xorig[i][2];
buf[13] = vorig[i][0];
buf[14] = vorig[i][1];
buf[15] = vorig[i][2];
buf[16] = imageorig[i];
return 17;
}
int FixEvent::unpack_exchange(int nlocal, double *buf)
{
xevent[nlocal][0] = buf[0];
xevent[nlocal][1] = buf[1];
xevent[nlocal][2] = buf[2];
xold[nlocal][0] = buf[3];
xold[nlocal][1] = buf[4];
xold[nlocal][2] = buf[5];
vold[nlocal][0] = buf[6];
vold[nlocal][1] = buf[7];
vold[nlocal][2] = buf[8];
imageold[nlocal] = static_cast<imageint>(buf[9]);
xorig[nlocal][0] = buf[10];
xorig[nlocal][1] = buf[11];
xorig[nlocal][2] = buf[12];
vorig[nlocal][0] = buf[13];
vorig[nlocal][1] = buf[14];
vorig[nlocal][2] = buf[15];
imageorig[nlocal] = static_cast<imageint>(buf[16]);
return 17;
}