#include "fix_setforce_spin.h"
#include "atom.h"
#include "update.h"
#include "modify.h"
#include "domain.h"
#include "region.h"
#include "input.h"
#include "variable.h"
#include "memory.h"
using namespace LAMMPS_NS;
using namespace FixConst;
enum{NONE,CONSTANT,EQUAL,ATOM};
FixSetForceSpin::FixSetForceSpin(LAMMPS *lmp, int narg, char **arg) :
FixSetForce(lmp, narg, arg) {}
void FixSetForceSpin::post_force(int )
{
double **x = atom->x;
double **fm = atom->fm;
int *mask = atom->mask;
int nlocal = atom->nlocal;
Region *region = NULL;
if (iregion >= 0) {
region = domain->regions[iregion];
region->prematch();
}
if (varflag == ATOM && atom->nmax > maxatom) {
maxatom = atom->nmax;
memory->destroy(sforce);
memory->create(sforce,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) {
if (region && !region->match(x[i][0],x[i][1],x[i][2])) continue;
foriginal[0] += fm[i][0];
foriginal[1] += fm[i][1];
foriginal[2] += fm[i][2];
if (xstyle) fm[i][0] = xvalue;
if (ystyle) fm[i][1] = yvalue;
if (zstyle) fm[i][2] = zvalue;
}
} else {
modify->clearstep_compute();
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);
modify->addstep_compute(update->ntimestep + 1);
for (int i = 0; i < nlocal; i++)
if (mask[i] & groupbit) {
if (region && !region->match(x[i][0],x[i][1],x[i][2])) continue;
foriginal[0] += fm[i][0];
foriginal[1] += fm[i][1];
foriginal[2] += fm[i][2];
if (xstyle == ATOM) fm[i][0] = sforce[i][0];
else if (xstyle) fm[i][0] = xvalue;
if (ystyle == ATOM) fm[i][1] = sforce[i][1];
else if (ystyle) fm[i][1] = yvalue;
if (zstyle == ATOM) fm[i][2] = sforce[i][2];
else if (zstyle) fm[i][2] = zvalue;
}
}
}
void FixSetForceSpin::single_setforce_spin(int i, double fmi[3])
{
double **x = atom->x;
int *mask = atom->mask;
Region *region = NULL;
if (iregion >= 0) {
region = domain->regions[iregion];
region->prematch();
}
if (varflag == ATOM && atom->nmax > maxatom) {
maxatom = atom->nmax;
memory->destroy(sforce);
memory->create(sforce,maxatom,3,"setforce:sforce");
}
foriginal[0] = foriginal[1] = foriginal[2] = 0.0;
force_flag = 0;
if (varflag == CONSTANT) {
if (mask[i] & groupbit) {
if (region && !region->match(x[i][0],x[i][1],x[i][2])) return;
foriginal[0] += fmi[0];
foriginal[1] += fmi[1];
foriginal[2] += fmi[2];
if (xstyle) fmi[0] = xvalue;
if (ystyle) fmi[1] = yvalue;
if (zstyle) fmi[2] = zvalue;
}
} else {
modify->clearstep_compute();
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);
modify->addstep_compute(update->ntimestep + 1);
if (mask[i] & groupbit) {
if (region && !region->match(x[i][0],x[i][1],x[i][2])) return;
foriginal[0] += fmi[0];
foriginal[1] += fmi[1];
foriginal[2] += fmi[2];
if (xstyle == ATOM) fmi[0] = sforce[i][0];
else if (xstyle) fmi[0] = xvalue;
if (ystyle == ATOM) fmi[1] = sforce[i][1];
else if (ystyle) fmi[1] = yvalue;
if (zstyle == ATOM) fmi[2] = sforce[i][2];
else if (zstyle) fmi[2] = zvalue;
}
}
}
void FixSetForceSpin::post_force_respa(int vflag, int ilevel, int )
{
if (ilevel == ilevel_respa) post_force(vflag);
else {
Region *region = NULL;
if (iregion >= 0) {
region = domain->regions[iregion];
region->prematch();
}
double **x = atom->x;
double **fm = atom->fm;
int *mask = atom->mask;
int nlocal = atom->nlocal;
for (int i = 0; i < nlocal; i++)
if (mask[i] & groupbit) {
if (region && !region->match(x[i][0],x[i][1],x[i][2])) continue;
if (xstyle) fm[i][0] = 0.0;
if (ystyle) fm[i][1] = 0.0;
if (zstyle) fm[i][2] = 0.0;
}
}
}