git-svn-id: svn://svn.icms.temple.edu/lammps-ro/trunk@1272 f3b2605a-c512-4ea7-a41b-209d697bcdaa

This commit is contained in:
sjplimp
2008-01-02 19:24:46 +00:00
parent 428ca84042
commit b2e9f7dc10
145 changed files with 2801 additions and 2615 deletions

View File

@ -22,21 +22,25 @@
#include "modify.h"
#include "compute.h"
#include "group.h"
#include "input.h"
#include "variable.h"
#include "memory.h"
#include "error.h"
using namespace LAMMPS_NS;
enum{COMPUTE,FIX};
enum{SCALAR,VECTOR,BOTH};
enum{COMPUTE,FIX,VARIABLE};
enum{ONE,RUNNING,WINDOW};
#define INVOKED_SCALAR 1 // same as in computes
#define INVOKED_VECTOR 2
/* ---------------------------------------------------------------------- */
FixAveTime::FixAveTime(LAMMPS *lmp, int narg, char **arg) :
Fix(lmp, narg, arg)
{
if (narg < 8) error->all("Illegal fix ave/time command");
if (narg < 7) error->all("Illegal fix ave/time command");
MPI_Comm_rank(world,&me);
@ -44,37 +48,55 @@ FixAveTime::FixAveTime(LAMMPS *lmp, int narg, char **arg) :
nrepeat = atoi(arg[4]);
nfreq = atoi(arg[5]);
if (strcmp(arg[6],"compute") == 0) which = COMPUTE;
else if (strcmp(arg[6],"fix") == 0) which = FIX;
else error->all("Illegal fix ave/time command");
// parse values until one isn't recognized
int n = strlen(arg[7]) + 1;
id = new char[n];
strcpy(id,arg[7]);
which = new int[narg-6];
argindex = new int[narg-6];
ids = new char*[narg-6];
value2index = new int[narg-6];
nvalues = 0;
int iarg = 6;
while (iarg < narg) {
if ((strncmp(arg[iarg],"c_",2) == 0) ||
(strncmp(arg[iarg],"f_",2) == 0) ||
(strncmp(arg[iarg],"v_",2) == 0)) {
if (arg[iarg][0] == 'c') which[nvalues] = COMPUTE;
else if (arg[iarg][0] == 'f') which[nvalues] = FIX;
else if (arg[iarg][0] == 'v') which[nvalues] = VARIABLE;
int n = strlen(arg[iarg]);
char *suffix = new char[n];
strcpy(suffix,&arg[iarg][2]);
char *ptr = strchr(suffix,'[');
if (ptr) {
if (suffix[strlen(suffix)-1] != ']')
error->all("Illegal fix ave/time command");
argindex[nvalues] = atoi(ptr+1);
*ptr = '\0';
} else argindex[nvalues] = 0;
n = strlen(suffix) + 1;
ids[nvalues] = new char[n];
strcpy(ids[nvalues],suffix);
nvalues++;
delete [] suffix;
} else break;
iarg++;
}
// option defaults
sflag = 1;
vflag = 0;
fp = NULL;
ave = ONE;
// optional args
int iarg = 8;
while (iarg < narg) {
if (strcmp(arg[iarg],"type") == 0) {
if (iarg+2 > narg) error->all("Illegal fix ave/time command");
if (strcmp(arg[iarg+1],"scalar") == 0) {
sflag = 1;
vflag = 0;
} else if (strcmp(arg[iarg+1],"vector") == 0) {
sflag = 0;
vflag = 1;
} else if (strcmp(arg[iarg+1],"both") == 0) sflag = vflag = 1;
else error->all("Illegal fix ave/time command");
iarg += 2;
} else if (strcmp(arg[iarg],"file") == 0) {
if (strcmp(arg[iarg],"file") == 0) {
if (iarg+2 > narg) error->all("Illegal fix ave/time command");
if (me == 0) {
fp = fopen(arg[iarg+1],"w");
@ -107,85 +129,108 @@ FixAveTime::FixAveTime(LAMMPS *lmp, int narg, char **arg) :
if (nfreq < nevery || nfreq % nevery || (nrepeat-1)*nevery >= nfreq)
error->all("Illegal fix ave/time command");
int icompute,ifix;
if (which == COMPUTE) {
icompute = modify->find_compute(id);
if (icompute < 0) error->all("Compute ID for fix ave/time does not exist");
} else {
ifix = modify->find_fix(id);
if (ifix < 0) error->all("Fix ID for fix ave/time does not exist");
for (int i = 0; i < nvalues; i++) {
if (which[i] == COMPUTE) {
int icompute = modify->find_compute(ids[i]);
if (icompute < 0)
error->all("Compute ID for fix ave/time does not exist");
if (argindex[i] == 0 && modify->compute[icompute]->scalar_flag == 0)
error->all("Fix ave/time compute does not calculate a scalar");
if (argindex[i] && modify->compute[icompute]->vector_flag == 0)
error->all("Fix ave/time compute does not calculate a vector");
if (argindex[i] && argindex[i] > modify->compute[icompute]->size_vector)
error->all("Fix ave/time compute vector is accessed out-of-range");
} else if (which[i] == FIX) {
int ifix = modify->find_fix(ids[i]);
if (ifix < 0)
error->all("Fix ID for fix ave/time does not exist");
if (argindex[i] && modify->fix[ifix]->scalar_flag == 0)
error->all("Fix ave/time fix does not calculate a scalar");
if (argindex[i] && modify->fix[ifix]->vector_flag == 0)
error->all("Fix ave/time fix does not calculate a vector");
if (argindex[i] && argindex[i] > modify->fix[ifix]->size_vector)
error->all("Fix ave/time fix vector is accessed out-of-range");
} else if (which[i] == VARIABLE) {
int ivariable = input->variable->find(ids[i]);
if (ivariable < 0)
error->all("Variable name for fix ave/time does not exist");
if (input->variable->equalstyle(ivariable) == 0)
error->all("Fix ave/time variable is not equal-style variable");
}
}
if (which == COMPUTE) {
if (sflag && modify->compute[icompute]->scalar_flag == 0)
error->all("Fix ave/time compute does not calculate a scalar");
if (vflag && modify->compute[icompute]->vector_flag == 0)
error->all("Fix ave/time compute does not calculate a vector");
} else {
if (sflag && modify->fix[ifix]->scalar_flag == 0)
error->all("Fix ave/time fix does not calculate a scalar");
if (vflag && modify->fix[ifix]->vector_flag == 0)
error->all("Fix ave/time fix does not calculate a vector");
}
// setup list of computes to call, including pre-computes
compute = NULL;
if (which == COMPUTE) {
ncompute = 1 + modify->compute[icompute]->npre;
compute = new Compute*[ncompute];
} else ncompute = 0;
// print header into file
if (fp && me == 0) {
if (which == COMPUTE)
fprintf(fp,"Time-averaged data for fix %s, group %s, and compute %s\n",
id,group->names[modify->compute[icompute]->igroup],id);
else
fprintf(fp,"Time-averaged data for fix %s, group %s, and fix %s\n",
id,group->names[modify->fix[ifix]->igroup],id);
if (sflag and !vflag)
fprintf(fp,"TimeStep Value\n");
else if (!sflag and vflag)
fprintf(fp,"TimeStep Vector-values\n");
else if (!sflag and vflag)
fprintf(fp,"TimeStep Scalar-value Vector-values\n");
fprintf(fp,"Time-averaged data for fix %s\n",id);
fprintf(fp,"TimeStep");
for (int i = 0; i < nvalues; i++)
if (which[i] == COMPUTE) fprintf(fp," c_%s",ids[i]);
else if (which[i] == FIX) fprintf(fp," f_%s",ids[i]);
else if (which[i] == VARIABLE) fprintf(fp," v_%s",ids[i]);
fprintf(fp,"\n");
}
// allocate memory for averaging
// allocate and initialize memory for averaging
vector = vector_total = NULL;
if (vflag) {
if (which == COMPUTE) size_vector = modify->compute[icompute]->size_vector;
else size_vector = modify->fix[ifix]->size_vector;
vector = new double[size_vector];
vector_total = new double[size_vector];
}
vector = new double[nvalues];
vector_total = new double[nvalues];
for (int i = 0; i < nvalues; i++) vector_total[i] = 0.0;
scalar_list = NULL;
vector_list = NULL;
if (sflag && ave == WINDOW) scalar_list = new double[nwindow];
if (vflag && ave == WINDOW)
vector_list = memory->create_2d_double_array(nwindow,size_vector,
if (ave == WINDOW)
vector_list = memory->create_2d_double_array(nwindow,nvalues,
"ave/time:vector_list");
// enable this fix to produce a global scalar and/or vector
// this fix produces either a global scalar or vector
// intensive/extensive flags set by compute,fix,variable that produces value
if (sflag) scalar_flag = 1;
if (vflag) vector_flag = 1;
scalar_vector_freq = nfreq;
if (which == COMPUTE) extensive = modify->compute[icompute]->extensive;
else extensive = modify->fix[ifix]->extensive;
extlist = NULL;
if (nvalues == 1) {
scalar_flag = 1;
if (which[0] == COMPUTE) {
Compute *compute = modify->compute[modify->find_compute(ids[0])];
if (argindex[0] == 0) extscalar = compute->extscalar;
else if (compute->extvector >= 0) extscalar = compute->extvector;
else extscalar = compute->extlist[argindex[0]-1];
} else if (which[0] == FIX) {
Fix *fix = modify->fix[modify->find_fix(ids[0])];
if (argindex[0] == 0) extscalar = fix->extscalar;
else if (fix->extvector >= 0) extscalar = fix->extvector;
else extscalar = fix->extlist[argindex[0]-1];
} else if (which[0] == VARIABLE)
extscalar = 0;
} else {
vector_flag = 1;
size_vector = nvalues;
extvector = -1;
extlist = new int[nvalues];
for (int i = 0; i < nvalues; i++) {
if (which[i] == COMPUTE) {
Compute *compute = modify->compute[modify->find_compute(ids[i])];
if (argindex[i] == 0) extlist[i] = compute->extscalar;
else if (compute->extvector >= 0) extlist[i] = compute->extvector;
else extlist[i] = compute->extlist[argindex[i]-1];
} else if (which[i] == FIX) {
Fix *fix = modify->fix[modify->find_fix(ids[i])];
if (argindex[i] == 0) extlist[i] = fix->extscalar;
else if (fix->extvector >= 0) extlist[i] = fix->extvector;
else extlist[i] = fix->extlist[argindex[i]-1];
} else if (which[i] == VARIABLE)
extlist[i] = 0;
}
}
// initializations
// set scalar and vector total to zero since they accumulate
// set vector total to zero since it accumulates
irepeat = 0;
iwindow = window_limit = 0;
norm = 0;
scalar_total = 0.0;
if (vflag) for (int i = 0; i < size_vector; i++) vector_total[i] = 0.0;
for (int i = 0; i < nvalues; i++) vector_total[i] = 0.0;
// nvalid = next step on which end_of_step does something
// can be this timestep if multiple of nfreq and nrepeat = 1
@ -198,24 +243,27 @@ FixAveTime::FixAveTime(LAMMPS *lmp, int narg, char **arg) :
nvalid -= (nrepeat-1)*nevery;
if (nvalid < update->ntimestep) nvalid += nfreq;
// set timestep for all computes that store invocation times
// since don't know a priori which are invoked by this fix
// add nvalid to ALL computes that store invocation times
// since don't know a priori which are invoked by this fix
// once in end_of_step() can just set timestep for ones actually invoked
for (int i = 0; i < modify->ncompute; i++)
if (modify->compute[i]->timeflag) modify->compute[i]->add_step(nvalid);
modify->addstep_compute_all(nvalid);
}
/* ---------------------------------------------------------------------- */
FixAveTime::~FixAveTime()
{
delete [] id;
delete [] which;
delete [] argindex;
for (int i = 0; i < nvalues; i++) delete [] ids[i];
delete [] ids;
delete [] value2index;
delete [] extlist;
if (fp && me == 0) fclose(fp);
delete [] compute;
delete [] vector;
delete [] vector_total;
delete [] scalar_list;
memory->destroy_2d_double_array(vector_list);
}
@ -232,36 +280,33 @@ int FixAveTime::setmask()
void FixAveTime::init()
{
// set ptrs to compute and its pre-computes called each end-of-step
// put pre-computes in list before compute
if (which == COMPUTE) {
int icompute = modify->find_compute(id);
if (icompute < 0)
error->all("Compute ID for fix ave/time does not exist");
ncompute = 0;
if (modify->compute[icompute]->npre)
for (int i = 0; i < modify->compute[icompute]->npre; i++) {
int ic = modify->find_compute(modify->compute[icompute]->id_pre[i]);
if (ic < 0)
error->all("Precompute ID for fix ave/time does not exist");
compute[ncompute++] = modify->compute[ic];
}
compute[ncompute++] = modify->compute[icompute];
}
// set ptr to fix ID
// set indices and check validity of all computes,fixes,variables
// check that fix frequency is acceptable
if (which == FIX) {
int ifix = modify->find_fix(id);
if (ifix < 0)
error->all("Fix ID for fix ave/time does not exist");
fix = modify->fix[ifix];
if (nevery % fix->scalar_vector_freq)
error->all("Fix ave/time and fix not computed at compatible times");
int ilist = 0;
for (int i = 0; i < nvalues; i++) {
if (which[i] == COMPUTE) {
int icompute = modify->find_compute(ids[i]);
if (icompute < 0)
error->all("Compute ID for fix ave/time does not exist");
value2index[i] = icompute;
} else if (which[i] == FIX) {
int ifix = modify->find_fix(ids[i]);
if (ifix < 0)
error->all("Fix ID for fix ave/time does not exist");
value2index[i] = ifix;
if (nevery % modify->fix[ifix]->scalar_vector_freq)
error->all("Fix for fix ave/time not computed at compatible time");
} else if (which[i] == VARIABLE) {
int ivariable = input->variable->find(ids[i]);
if (ivariable < 0)
error->all("Variable name for fix ave/time does not exist");
value2index[i] = ivariable;
}
}
}
@ -278,7 +323,8 @@ void FixAveTime::setup()
void FixAveTime::end_of_step()
{
int i;
int i,j,k,m;
double tmp;
// skip if not step which requires doing something
@ -286,33 +332,44 @@ void FixAveTime::end_of_step()
// zero if first step
if (irepeat == 0) {
scalar = 0.0;
if (vflag)
for (i = 0; i < size_vector; i++) vector[i] = 0.0;
}
if (irepeat == 0)
for (i = 0; i < nvalues; i++) vector[i] = 0.0;
// accumulate results of compute or fix to local copy
if (which == COMPUTE) {
modify->clearstep_compute();
// accumulate results of computes,fixes,variables to local copy
// compute/fix/variable may invoke computes so wrap with clear/add
if (sflag) {
double value;
for (i = 0; i < ncompute; i++) value = compute[i]->compute_scalar();
scalar += value;
}
if (vflag) {
for (i = 0; i < ncompute; i++) compute[i]->compute_vector();
double *cvector = compute[ncompute-1]->vector;
for (i = 0; i < size_vector; i++) vector[i] += cvector[i];
}
modify->clearstep_compute();
} else {
if (sflag) scalar += fix->compute_scalar();
if (vflag)
for (i = 0; i < size_vector; i++)
vector[i] += fix->compute_vector(i);
int ilist = 0;
for (i = 0; i < nvalues; i++) {
m = value2index[i];
// invoke compute if not previously invoked
if (which[i] == COMPUTE) {
Compute *compute = modify->compute[m];
if (argindex[i] == 0) {
if (compute->invoked & INVOKED_SCALAR) vector[i] += compute->scalar;
else vector[i] += compute->compute_scalar();
} else {
if (!(compute->invoked & INVOKED_VECTOR)) compute->compute_vector();
vector[i] += compute->vector[argindex[i]-1];
}
// access fix fields, guaranteed to be ready
} else if (which[i] == FIX) {
if (argindex[i] == 0)
vector[i] += modify->fix[m]->compute_scalar();
else
vector[i] += modify->fix[m]->compute_vector(argindex[i]-1);
// evaluate equal-style variable
} else if (which[i] == VARIABLE)
vector[i] += input->variable->compute_equal(m);
}
// done if irepeat < nrepeat
@ -321,46 +378,36 @@ void FixAveTime::end_of_step()
irepeat++;
if (irepeat < nrepeat) {
nvalid += nevery;
if (which == COMPUTE) modify->addstep_compute(nvalid);
modify->addstep_compute(nvalid);
return;
}
irepeat = 0;
nvalid = update->ntimestep+nfreq - (nrepeat-1)*nevery;
if (which == COMPUTE) modify->addstep_compute(nvalid);
modify->addstep_compute(nvalid);
// average the final result for the Nfreq timestep
double repeat = nrepeat;
if (sflag) scalar /= repeat;
if (vflag) for (i = 0; i < size_vector; i++) vector[i] /= repeat;
for (i = 0; i < nvalues; i++) vector[i] /= repeat;
// if ave = ONE, only single Nfreq timestep value is needed
// if ave = RUNNING, combine with all previous Nfreq timestep values
// if ave = WINDOW, comine with nwindow most recent Nfreq timestep values
if (ave == ONE) {
if (sflag) scalar_total = scalar;
if (vflag) for (i = 0; i < size_vector; i++) vector_total[i] = vector[i];
for (i = 0; i < nvalues; i++) vector_total[i] = vector[i];
norm = 1;
} else if (ave == RUNNING) {
if (sflag) scalar_total += scalar;
if (vflag) for (i = 0; i < size_vector; i++) vector_total[i] += vector[i];
for (i = 0; i < nvalues; i++) vector_total[i] += vector[i];
norm++;
} else if (ave == WINDOW) {
if (sflag) {
scalar_total += scalar;
if (window_limit) scalar_total -= scalar_list[iwindow];
scalar_list[iwindow] = scalar;
}
if (vflag) {
for (i = 0; i < size_vector; i++) {
vector_total[i] += vector[i];
if (window_limit) vector_total[i] -= vector_list[iwindow][i];
vector_list[iwindow][i] = vector[i];
}
for (i = 0; i < nvalues; i++) {
vector_total[i] += vector[i];
if (window_limit) vector_total[i] -= vector_list[iwindow][i];
vector_list[iwindow][i] = vector[i];
}
iwindow++;
@ -376,9 +423,7 @@ void FixAveTime::end_of_step()
if (fp && me == 0) {
fprintf(fp,"%d",update->ntimestep);
if (sflag) fprintf(fp," %g",scalar_total/norm);
if (vflag)
for (i = 0; i < size_vector; i++) fprintf(fp," %g",vector_total[i]/norm);
for (i = 0; i < nvalues; i++) fprintf(fp," %g",vector_total[i]/norm);
fprintf(fp,"\n");
fflush(fp);
}
@ -391,7 +436,7 @@ void FixAveTime::end_of_step()
double FixAveTime::compute_scalar()
{
if (norm) return scalar_total/norm;
if (norm) return vector_total[0]/norm;
return 0.0;
}