Merge branch 'develop' into collected-small-changes

This commit is contained in:
Axel Kohlmeyer
2023-08-10 06:27:44 -04:00
3 changed files with 95 additions and 38 deletions

View File

@ -8,7 +8,7 @@ Syntax
.. code-block:: LAMMPS
fix ID group-ID vector Nevery value1 value2 ...
fix ID group-ID vector Nevery value1 value2 ... keyword args ...
* ID, group-ID are documented in :doc:`fix <fix>` command
* vector = style name of this fix command
@ -25,6 +25,13 @@ Syntax
v_name = value calculated by an equal-style variable with name
v_name[I] = Ith component of vector-style variable with name
* zero or more keyword/args pairs may be appended
* keyword = *nmax*
.. parsed-literal::
*nmax* length = set maximal length of vector to <length>
Examples
""""""""
@ -32,21 +39,26 @@ Examples
fix 1 all vector 100 c_myTemp
fix 1 all vector 5 c_myTemp v_integral
fix 1 all vector 50 c_myTemp nmax 200
Description
"""""""""""
Use one or more global values as inputs every few timesteps, and
simply store them. For a single specified value, the values are
Use one or more global values as inputs every few timesteps, and simply
store them as a sequence. For a single specified value, the values are
stored as a global vector of growing length. For multiple specified
values, they are stored as rows in a global array, whose number of
rows is growing. The resulting vector or array can be used by other
values, they are stored as rows in a global array, whose number of rows
is growing. The resulting vector or array can be used by other
:doc:`output commands <Howto_output>`.
The optional *nmax* keyword can be used to restrict the length of the
vector to the given *length* value. Once the restricted vector is
filled, the oldest entry will be discarded when a entry is added.
One way to to use this command is to accumulate a vector that is
time-integrated using the :doc:`variable trap() <variable>` function.
For example the velocity auto-correlation function (VACF) can be
time-integrated, to yield a diffusion coefficient, as follows:
numerically integrated using the :doc:`variable trap() <variable>`
function. For example, the velocity auto-correlation function (VACF)
can be integrated, to yield a diffusion coefficient, as follows:
.. code-block:: LAMMPS
@ -77,6 +89,15 @@ The *Nevery* argument specifies on what timesteps the input values
will be used in order to be stored. Only timesteps that are a
multiple of *Nevery*, including timestep 0, will contribute values.
.. note::
:class: warning
If *Nevery* is a small number and the simulation runs for many
steps, the accumulated vector or array can become very large and
thus consume a lot of memory. The implementation limit is about
2 billion entries. Using the *nmax* keyword mentioned above can
avoid that by limiting the size of the vector.
Note that if you perform multiple runs, using the "pre no" option of
the :doc:`run <run>` command to avoid initialization on subsequent runs,
then you need to use the *stop* keyword with the first :doc:`run <run>`
@ -94,11 +115,12 @@ calculated by the compute is used.
Note that there is a :doc:`compute reduce <compute_reduce>` command
which can sum per-atom quantities into a global scalar or vector which
can thus be accessed by fix vector. Or it can be a compute defined
not in your input script, but by :doc:`thermodynamic output <thermo_style>` or other fixes such as :doc:`fix nvt <fix_nh>`
or :doc:`fix temp/rescale <fix_temp_rescale>`. See the doc pages for
these commands which give the IDs of these computes. Users can also
write code for their own compute styles and :doc:`add them to LAMMPS <Modify>`.
can thus be accessed by fix vector. Or it can be a compute defined not
in your input script, but by :doc:`thermodynamic output <thermo_style>`
or other fixes such as :doc:`fix nvt <fix_nh>` or :doc:`fix temp/rescale
<fix_temp_rescale>`. See the doc pages for these commands which give
the IDs of these computes. Users can also write code for their own
compute styles and :doc:`add them to LAMMPS <Modify>`.
If a value begins with "f\_", a fix ID must follow which has been
previously defined in the input script. If no bracketed term is
@ -108,7 +130,8 @@ calculated by the fix is used.
Note that some fixes only produce their values on certain timesteps,
which must be compatible with *Nevery*, else an error will result.
Users can also write code for their own fix styles and :doc:`add them to LAMMPS <Modify>`.
Users can also write code for their own fix styles and :doc:`add them to
LAMMPS <Modify>`.
If a value begins with "v\_", a variable name must follow which has
been previously defined in the input script. An equal-style or
@ -126,8 +149,9 @@ quantities to be stored by fix vector.
Restart, fix_modify, output, run start/stop, minimize info
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
No information about this fix is written to :doc:`binary restart files <restart>`. None of the :doc:`fix_modify <fix_modify>` options
are relevant to this fix.
No information about this fix is written to :doc:`binary restart files
<restart>`. None of the :doc:`fix_modify <fix_modify>` options are
relevant to this fix.
This fix produces a global vector or global array which can be
accessed by various :doc:`output commands <Howto_output>`. The values
@ -144,15 +168,15 @@ the vector are "intensive" or "extensive". If the fix produces an
array, then all elements in the array must be the same, either
"intensive" or "extensive". If a compute or fix provides the value
stored, then the compute or fix determines whether the value is
intensive or extensive; see the page for that compute or fix for
further info. Values produced by a variable are treated as intensive.
intensive or extensive; see the page for that compute or fix for further
info. Values produced by a variable are treated as intensive.
This fix can allocate storage for stored values accumulated over
multiple runs, using the *start* and *stop* keywords of the
:doc:`run <run>` command. See the :doc:`run <run>` command for details of
how to do this. If using the :doc:`run pre no <run>` command option,
this is required to allow the fix to allocate sufficient storage for
stored values.
multiple runs, using the *start* and *stop* keywords of the :doc:`run
<run>` command. See the :doc:`run <run>` command for details of how to
do this. If using the :doc:`run pre no <run>` command option, this is
required to allow the fix to allocate sufficient storage for stored
values.
This fix is not invoked during :doc:`energy minimization <minimize>`.
@ -165,7 +189,10 @@ Related commands
:doc:`compute <compute>`, :doc:`variable <variable>`
Default
"""""""
Defaults
""""""""
none
The default value of *nmax* is deduced from the number of steps
in a run (or multiple runs when using the *start* and *stop*
keywords of the :doc:`run command <run>`) divided by the choice
of *Nevery* plus 1.

View File

@ -35,10 +35,15 @@ 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;
// parse values
int iarg = 4;
values.clear();
for (int iarg = 4; iarg < narg; iarg++) {
while (iarg < narg) {
ArgInfo argi(arg[iarg]);
value_t val;
@ -47,10 +52,25 @@ FixVector::FixVector(LAMMPS *lmp, int narg, char **arg) :
val.id = argi.get_name();
val.val.c = nullptr;
if ((val.which == ArgInfo::UNKNOWN) || (val.which == ArgInfo::NONE) || (argi.get_dim() > 1))
if ((val.which == ArgInfo::UNKNOWN) || (argi.get_dim() > 1))
error->all(FLERR, "Invalid fix vector argument: {}", arg[iarg]);
if (val.which == ArgInfo::NONE) break;
values.push_back(val);
++iarg;
}
while (iarg < narg) {
if (strcmp(arg[iarg], "nmax") == 0) {
if (iarg + 2 > narg) utils::missing_cmd_args(FLERR, "fix vector nmax", error);
nmaxval = utils::bnumeric(FLERR, arg[iarg + 1], false, lmp);
if (nmaxval < 1) error->all(FLERR, "Invalid nmax value");
iarg += 2;
} else {
error->all(FLERR, "Unknown fix vector keyword: {}", arg[iarg]);
}
}
// setup and error check
@ -132,7 +152,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 +219,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 +242,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 +313,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 +324,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 +335,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];
}

View File

@ -50,8 +50,11 @@ class FixVector : public Fix {
bigint nextstep, initialstep;
int ncount; // # of values currently in growing vector or array
int ncountmax; // max # of values vector/array can hold
bigint ncount; // # of values processed and stored into growing vector or array
bigint ncountmax; // max # of values vector/array can hold
bigint nmaxval; // maximum allowed number of values
bigint nindex; // start index of data, may wrap around
double *vector;
double **array;
};