Skip to content

Commit

Permalink
Merge pull request lammps#4212 from akohlmey/collected-small-changes
Browse files Browse the repository at this point in the history
Collected small changes and fixes
  • Loading branch information
akohlmey authored Jul 2, 2024
2 parents a74500f + dc07a75 commit 56a9192
Show file tree
Hide file tree
Showing 14 changed files with 339 additions and 41 deletions.
1 change: 0 additions & 1 deletion cmake/presets/mingw-cross.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ set(WIN_PACKAGES
FEP
GPU
GRANULAR
INTEL
INTERLAYER
KSPACE
LEPTON
Expand Down
24 changes: 24 additions & 0 deletions doc/src/Developer_write_pair.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,30 @@ We are looking at the following cases:
- `Case 3: a potential requiring communication`_
- `Case 4: potentials without a compute() function`_

Package and build system considerations
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

In general, new pair styles should be added to the :ref:`EXTRA-PAIR
package <PKG-EXTRA-PAIR>` unless they are an accelerated pair style and
then they should be added to the corresponding accelerator package
(:ref:`GPU <PKG-GPU>`, :ref:`INTEL <PKG-INTEL>`, :ref:`KOKKOS
<PKG-KOKKOS>`, :ref:`OPENMP <PKG-OPENMP>`, :ref:`OPT <PKG-OPT>`). If
you feel that your contribution should be added to a different package,
please consult with the LAMMPS developers first.

The contributed code needs to support the :doc:`traditional GNU make
build process <Build_make>` **and** the :doc:`CMake build process
<Build_cmake>`. For the GNU make process and if the package has an
``Install.sh`` file, most likely that file needs to be updated to
correctly copy the sources when installing the package and properly
delete them when uninstalling. This is particularly important when
added a new pair style that is a derived class from an existing pair
style in a package, so that its installation depends on the the
installation status of the package of the derived class. For the CMake
process, it is sometimes necessary to make changes to the package
specific CMake scripting in ``cmake/Modules/Packages``.


----

Case 1: a pairwise additive model
Expand Down
18 changes: 16 additions & 2 deletions doc/src/variable.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Syntax
bound(group,dir,region), gyration(group,region), ke(group,reigon),
angmom(group,dim,region), torque(group,dim,region),
inertia(group,dimdim,region), omega(group,dim,region)
special functions = sum(x), min(x), max(x), ave(x), trap(x), slope(x), sort(x), rsort(x), gmask(x), rmask(x), grmask(x,y), next(x), is_file(name), is_os(name), extract_setting(name), label2type(kind,label), is_typelabel(kind,label)
special functions = sum(x), min(x), max(x), ave(x), trap(x), slope(x), sort(x), rsort(x), gmask(x), rmask(x), grmask(x,y), next(x), is_file(name), is_os(name), extract_setting(name), label2type(kind,label), is_typelabel(kind,label), is_timeout()
feature functions = is_available(category,feature), is_active(category,feature), is_defined(category,id)
atom value = id[i], mass[i], type[i], mol[i], x[i], y[i], z[i], vx[i], vy[i], vz[i], fx[i], fy[i], fz[i], q[i]
atom vector = id, mass, type, mol, radius, q, x, y, z, vx, vy, vz, fx, fy, fz
Expand Down Expand Up @@ -547,7 +547,7 @@ variables.
+------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Region functions | count(ID,IDR), mass(ID,IDR), charge(ID,IDR), xcm(ID,dim,IDR), vcm(ID,dim,IDR), fcm(ID,dim,IDR), bound(ID,dir,IDR), gyration(ID,IDR), ke(ID,IDR), angmom(ID,dim,IDR), torque(ID,dim,IDR), inertia(ID,dimdim,IDR), omega(ID,dim,IDR) |
+------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Special functions | sum(x), min(x), max(x), ave(x), trap(x), slope(x), sort(x), rsort(x), gmask(x), rmask(x), grmask(x,y), next(x), is_file(name), is_os(name), extract_setting(name), label2type(kind,label), is_typelabel(kind,label) |
| Special functions | sum(x), min(x), max(x), ave(x), trap(x), slope(x), sort(x), rsort(x), gmask(x), rmask(x), grmask(x,y), next(x), is_file(name), is_os(name), extract_setting(name), label2type(kind,label), is_typelabel(kind,label), is_timeout() |
+------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Feature functions | is_available(category,feature), is_active(category,feature), is_defined(category,id) |
+------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
Expand Down Expand Up @@ -1042,6 +1042,20 @@ label2type(), but returns 1 if the type label has been assigned,
otherwise it returns 0. This function can be used to check if a
particular type label already exists in the simulation.

.. versionadded:: TBD

The is_timeout() function returns 1 when the :doc:`timer timeout
<timer>` has expired otherwise it returns 0. This function can be used
to check inputs in combination with the :doc:`if command <if>` to
execute commands after the timer has expired. Example:

.. code-block:: LAMMPS
variable timeout equal is_timeout()
timer timeout 0:10:00 every 10
run 10000
if ${timeout} then "print 'Timer has expired'"
----------

Feature Functions
Expand Down
25 changes: 14 additions & 11 deletions src/REPLICA/neb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ void NEB::run()
if (me == 0)
color = 0;
else
color = 1;
color = MPI_UNDEFINED;
MPI_Comm_split(uworld, color, 0, &roots);

auto fixes = modify->get_fix_by_style("^neb$");
Expand Down Expand Up @@ -608,17 +608,20 @@ void NEB::open(char *file)
void NEB::print_status()
{
double fnorm2 = sqrt(update->minimize->fnorm_sqr());
double fmaxreplica;
MPI_Allreduce(&fnorm2, &fmaxreplica, 1, MPI_DOUBLE, MPI_MAX, roots);
double fnorminf = update->minimize->fnorm_inf();
double fmaxatom;
MPI_Allreduce(&fnorminf, &fmaxatom, 1, MPI_DOUBLE, MPI_MAX, roots);
double fmaxreplica = 0.0;
double fmaxatom = 0.0;

if (print_mode == VERBOSE) {
freplica = new double[nreplica];
MPI_Allgather(&fnorm2, 1, MPI_DOUBLE, &freplica[0], 1, MPI_DOUBLE, roots);
fmaxatomInRepl = new double[nreplica];
MPI_Allgather(&fnorminf, 1, MPI_DOUBLE, &fmaxatomInRepl[0], 1, MPI_DOUBLE, roots);
if (me == 0) {
MPI_Allreduce(&fnorm2, &fmaxreplica, 1, MPI_DOUBLE, MPI_MAX, roots);
MPI_Allreduce(&fnorminf, &fmaxatom, 1, MPI_DOUBLE, MPI_MAX, roots);

if (print_mode == VERBOSE) {
freplica = new double[nreplica];
MPI_Allgather(&fnorm2, 1, MPI_DOUBLE, &freplica[0], 1, MPI_DOUBLE, roots);
fmaxatomInRepl = new double[nreplica];
MPI_Allgather(&fnorminf, 1, MPI_DOUBLE, &fmaxatomInRepl[0], 1, MPI_DOUBLE, roots);
}
}

double one[7];
Expand Down Expand Up @@ -703,7 +706,7 @@ void NEB::print_status()
fflush(universe->ulogfile);
}
}
if (print_mode == VERBOSE) {
if ((me == 0) && (print_mode == VERBOSE)) {
delete[] freplica;
delete[] fmaxatomInRepl;
}
Expand Down
31 changes: 20 additions & 11 deletions src/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2420,7 +2420,7 @@ use to avoid a memory leak. Example:
.. code-block:: c
double *dptr = (double *) lammps_extract_variable(handle,name,NULL);
double *dptr = (double *) lammps_extract_variable(handle, name, NULL);
double value = *dptr;
lammps_free((void *)dptr);
Expand All @@ -2431,16 +2431,25 @@ content will not be updated in case the variable is re-evaluated.
To avoid a memory leak, this pointer needs to be freed after use in
the calling program.
For *vector*\ -style variables, the returned pointer is to actual LAMMPS data.
The pointer should not be deallocated. Its length depends on the variable,
compute, or fix data used to construct the *vector*\ -style variable.
This length can be fetched by calling this function with *group* set to the
constant "LMP_SIZE_VECTOR", which returns a ``void *`` pointer that can be
dereferenced to an integer that is the length of the vector. This pointer
needs to be deallocated when finished with it to avoid memory leaks.
For *vector*\ -style variables, the returned pointer points to actual
LAMMPS data and thus it should **not** be deallocated. Its length
depends on the variable, compute, or fix data used to construct the
*vector*\ -style variable. This length can be fetched by calling this
function with *group* set to a non-NULL pointer (NULL returns the vector).
In that case it will return the vector length as an allocated int
pointer cast to a ``void *`` pointer. That pointer can be recast and
dereferenced to an integer yielding the length of the vector. This pointer
must be deallocated when finished with it to avoid memory leaks. Example:
.. code-block:: c
double *vectvals = (double *) lammps_extract_variable(handle, name, NULL);
int *intptr = (int *) lammps_extract_variable(handle, name, 1);
int vectlen = *intptr;
lammps_free((void *)intptr);
For other variable styles the returned pointer needs to be cast to
a char pointer. It should not be deallocated.
a char pointer and it should **not** be deallocated. Example:
.. code-block:: c
Expand All @@ -2452,7 +2461,7 @@ a char pointer. It should not be deallocated.
LAMMPS cannot easily check if it is valid to access the data
referenced by the variables (e.g., computes, fixes, or thermodynamic
info), so it may fail with an error. The caller has to make certain
that the data are extracted only when it safe to evaluate the variable
that the data is extracted only when it safe to evaluate the variable
and thus an error or crash are avoided.
\endverbatim
Expand Down Expand Up @@ -2487,7 +2496,7 @@ void *lammps_extract_variable(void *handle, const char *name, const char *group)
} else if (lmp->input->variable->vectorstyle(ivar)) {
double *values = nullptr;
int nvector = lmp->input->variable->compute_vector(ivar, &values);
if (group != nullptr && strcmp(group,"LMP_SIZE_VECTOR") == 0) {
if (group) {
int* nvecptr = (int *) malloc(sizeof(int));
*nvecptr = nvector;
return (void *) nvecptr;
Expand Down
6 changes: 3 additions & 3 deletions src/library.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,9 @@ void *lammps_extract_atom(void *handle, const char *name);
* Library functions to access data from computes, fixes, variables in LAMMPS
* ---------------------------------------------------------------------- */

void *lammps_extract_compute(void *handle, const char *, int, int);
void *lammps_extract_fix(void *handle, const char *, int, int, int, int);
void *lammps_extract_variable(void *handle, const char *, const char *);
void *lammps_extract_compute(void *handle, const char *id, int style, int type);
void *lammps_extract_fix(void *handle, const char *id, int style, int type, int nrow, int ncol);
void *lammps_extract_variable(void *handle, const char *name, const char *group);
int lammps_extract_variable_datatype(void *handle, const char *name);
int lammps_set_variable(void *handle, const char *name, const char *str);
int lammps_set_string_variable(void *handle, const char *name, const char *str);
Expand Down
6 changes: 3 additions & 3 deletions src/timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ Timer::Timer(LAMMPS *_lmp) : Pointers(_lmp)
{
_level = NORMAL;
_sync = OFF;
_timeout = -1;
_s_timeout = -1;
_timeout = -1.0;
_s_timeout = -1.0;
_checkfreq = 10;
_nextcheck = -1;
this->_stamp(RESET);
Expand Down Expand Up @@ -251,7 +251,7 @@ void Timer::modify_params(int narg, char **arg)

// format timeout setting
std::string timeout = "off";
if (_timeout >= 0) {
if (_timeout >= 0.0) {
std::tm tv = fmt::gmtime((std::time_t) _timeout);
timeout = fmt::format("{:02d}:{:%M:%S}", tv.tm_yday * 24 + tv.tm_hour, tv);
}
Expand Down
12 changes: 6 additions & 6 deletions src/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ class Timer : protected Pointers {
double previous_cpu;
double previous_wall;
double timeout_start;
int _level; // level of detail: off=0,loop=1,normal=2,full=3
int _sync; // if nonzero, synchronize tasks before setting the timer
int _timeout; // max allowed wall time in seconds. infinity if negative
int _s_timeout; // copy of timeout for restoring after a forced timeout
int _checkfreq; // frequency of timeout checking
int _nextcheck; // loop number of next timeout check
double _timeout; // max allowed wall time in seconds. infinity if negative
double _s_timeout; // copy of timeout for restoring after a forced timeout
int _level; // level of detail: off=0,loop=1,normal=2,full=3
int _sync; // if nonzero, synchronize tasks before setting the timer
int _checkfreq; // frequency of timeout checking
int _nextcheck; // loop number of next timeout check

// update one specific timer array
void _stamp(enum ttype);
Expand Down
2 changes: 1 addition & 1 deletion src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1677,7 +1677,7 @@ double utils::timespec2seconds(const std::string &timespec)
try {
for (i = 0; i < 3; i++) {
if (!values.has_next()) break;
vals[i] = values.next_int();
vals[i] = values.next_double();
}
} catch (TokenizerException &) {
return -1.0;
Expand Down
23 changes: 20 additions & 3 deletions src/variable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "random_mars.h"
#include "region.h"
#include "thermo.h"
#include "timer.h"
#include "tokenizer.h"
#include "universe.h"
#include "update.h"
Expand Down Expand Up @@ -4276,8 +4277,9 @@ Region *Variable::region_function(char *id, int ivar)
return 0 if not a match, 1 if successfully processed
customize by adding a special function:
sum(x),min(x),max(x),ave(x),trap(x),slope(x),
gmask(x),rmask(x),grmask(x,y),next(x),is_file(x),is_ox(x),
extract_setting(x),label2type(x,y),is_typelabel(x,y)
gmask(x),rmask(x),grmask(x,y),next(x),is_file(x),is_os(x),
extract_setting(x),label2type(x,y),is_tpelabel(x,y)
is_timeout()
------------------------------------------------------------------------- */

// to simplify finding matches and assigning constants for functions operating on vectors
Expand All @@ -4286,7 +4288,7 @@ static const std::unordered_map<std::string,int> special_function_map = {
{"sum", SUM}, {"min", XMIN}, {"max", XMAX}, {"ave", AVE}, {"trap", TRAP}, {"slope", SLOPE},
{"sort", SORT}, {"rsort", RSORT}, {"gmask", NOVECTOR}, {"rmask", NOVECTOR}, {"grmask", NOVECTOR},
{"next", NOVECTOR}, {"is_file", NOVECTOR}, {"is_os", NOVECTOR}, {"extract_setting", NOVECTOR},
{"label2type", NOVECTOR}, {"is_typelabel", NOVECTOR} };
{"label2type", NOVECTOR}, {"is_typelabel", NOVECTOR}, {"is_timeout", NOVECTOR} };

int Variable::special_function(const std::string &word, char *contents, Tree **tree,
Tree **treestack, int &ntreestack, double *argstack,
Expand Down Expand Up @@ -4765,6 +4767,21 @@ int Variable::special_function(const std::string &word, char *contents, Tree **t
newtree->value = value;
treestack[ntreestack++] = newtree;
} else argstack[nargstack++] = value;

} else if (word == "is_timeout") {
if ((narg != 1) || (std::string(args[0]).size() != 0))
print_var_error(FLERR,"Invalid is_timeout() function in variable formula",ivar);
value = timer->is_timeout() ? 1.0 : 0.0;

// save value in tree or on argstack

if (tree) {
auto newtree = new Tree();
newtree->type = VALUE;
newtree->value = value;
treestack[ntreestack++] = newtree;
} else argstack[nargstack++] = value;

}

// delete stored args
Expand Down
6 changes: 6 additions & 0 deletions unittest/c-library/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ target_compile_definitions(test_library_properties PRIVATE -DTEST_INPUT_FOLDER=$
add_test(NAME LibraryProperties COMMAND test_library_properties)
set_tests_properties(LibraryProperties PROPERTIES ENVIRONMENT "LAMMPS_POTENTIALS=${LAMMPS_POTENTIALS_DIR}")

add_executable(test_library_objects test_library_objects.cpp test_main.cpp)
target_link_libraries(test_library_objects PRIVATE lammps GTest::GMock)
target_compile_definitions(test_library_objects PRIVATE -DTEST_INPUT_FOLDER=${CMAKE_CURRENT_SOURCE_DIR})
add_test(NAME LibraryObjects COMMAND test_library_objects)
set_tests_properties(LibraryObjects PROPERTIES ENVIRONMENT "LAMMPS_POTENTIALS=${LAMMPS_POTENTIALS_DIR}")

add_executable(test_library_scatter_gather test_library_scatter_gather.cpp test_main.cpp)
target_link_libraries(test_library_scatter_gather PRIVATE lammps GTest::GMock)
target_compile_definitions(test_library_scatter_gather PRIVATE -DTEST_INPUT_FOLDER=${CMAKE_CURRENT_SOURCE_DIR})
Expand Down
Loading

0 comments on commit 56a9192

Please sign in to comment.