add optional nmax keyword to fix vector to allow using it as a sliding window
This commit is contained in:
@ -35,10 +35,20 @@ FixVector::FixVector(LAMMPS *lmp, int narg, char **arg) :
|
||||
nevery = utils::inumeric(FLERR, arg[3], false, lmp);
|
||||
if (nevery <= 0) error->all(FLERR, "Invalid fix vector every argument: {}", nevery);
|
||||
|
||||
nmaxval = MAXSMALLINT;
|
||||
nindex = 0;
|
||||
|
||||
int iarg = 4;
|
||||
if (strcmp(arg[iarg], "nmax") == 0) {
|
||||
nmaxval = utils::inumeric(FLERR, arg[iarg + 1], false, lmp);
|
||||
if (nmaxval < 1) error->all(FLERR, "Invalid nmax value");
|
||||
iarg += 2;
|
||||
}
|
||||
|
||||
// parse values
|
||||
|
||||
values.clear();
|
||||
for (int iarg = 4; iarg < narg; iarg++) {
|
||||
while (iarg < narg) {
|
||||
ArgInfo argi(arg[iarg]);
|
||||
|
||||
value_t val;
|
||||
@ -51,6 +61,7 @@ FixVector::FixVector(LAMMPS *lmp, int narg, char **arg) :
|
||||
error->all(FLERR, "Invalid fix vector argument: {}", arg[iarg]);
|
||||
|
||||
values.push_back(val);
|
||||
++iarg;
|
||||
}
|
||||
|
||||
// setup and error check
|
||||
@ -132,7 +143,7 @@ FixVector::FixVector(LAMMPS *lmp, int narg, char **arg) :
|
||||
|
||||
vector = nullptr;
|
||||
array = nullptr;
|
||||
ncount = ncountmax = 0;
|
||||
ncount = ncountmax = nindex = 0;
|
||||
if (values.size() == 1)
|
||||
size_vector = 0;
|
||||
else
|
||||
@ -199,6 +210,7 @@ void FixVector::init()
|
||||
bigint finalstep = update->endstep / nevery * nevery;
|
||||
if (finalstep > update->endstep) finalstep -= nevery;
|
||||
ncountmax = (finalstep - initialstep) / nevery + 1;
|
||||
if (ncountmax > nmaxval) ncountmax = nmaxval;
|
||||
if (values.size() == 1)
|
||||
memory->grow(vector, ncountmax, "vector:vector");
|
||||
else
|
||||
@ -221,16 +233,18 @@ void FixVector::end_of_step()
|
||||
// skip if not step which requires doing something
|
||||
|
||||
if (update->ntimestep != nextstep) return;
|
||||
if (ncount == ncountmax) error->all(FLERR, "Overflow of allocated fix vector storage");
|
||||
|
||||
// wrap around when vector/array is full
|
||||
nindex = ncount % ncountmax;
|
||||
|
||||
// accumulate results of computes,fixes,variables to local copy
|
||||
// compute/fix/variable may invoke computes so wrap with clear/add
|
||||
|
||||
double *result;
|
||||
if (values.size() == 1)
|
||||
result = &vector[ncount];
|
||||
result = &vector[nindex];
|
||||
else
|
||||
result = array[ncount];
|
||||
result = array[nindex];
|
||||
|
||||
modify->clearstep_compute();
|
||||
|
||||
@ -290,9 +304,9 @@ void FixVector::end_of_step()
|
||||
|
||||
ncount++;
|
||||
if (values.size() == 1)
|
||||
size_vector++;
|
||||
size_vector = MIN(size_vector + 1, ncountmax);
|
||||
else
|
||||
size_array_rows++;
|
||||
size_array_rows = MIN(size_array_rows + 1, ncountmax);
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
@ -301,7 +315,9 @@ void FixVector::end_of_step()
|
||||
|
||||
double FixVector::compute_vector(int i)
|
||||
{
|
||||
return vector[i];
|
||||
int idx = i;
|
||||
if (ncount >= ncountmax) idx = (i + ncount) % ncountmax;
|
||||
return vector[idx];
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
@ -310,5 +326,7 @@ double FixVector::compute_vector(int i)
|
||||
|
||||
double FixVector::compute_array(int i, int j)
|
||||
{
|
||||
return array[i][j];
|
||||
int idx = i;
|
||||
if (ncount >= ncountmax) idx = (i + ncount) % ncountmax;
|
||||
return array[idx][j];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user