From 6ad01457097f06ea4c6d98e8c00f821b36479dd3 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Sat, 29 Jun 2024 04:02:33 -0400 Subject: [PATCH] new special variable function is_timeout() --- doc/src/variable.rst | 18 ++++++++++++++++-- src/variable.cpp | 23 ++++++++++++++++++++--- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/doc/src/variable.rst b/doc/src/variable.rst index d19f256451..330e44139e 100644 --- a/doc/src/variable.rst +++ b/doc/src/variable.rst @@ -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 @@ -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) | +------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ @@ -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 +` has expired otherwise it returns 0. This function can be used +to check inputs in combination with the :doc:`if command ` 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 diff --git a/src/variable.cpp b/src/variable.cpp index f381eecde6..823a68a506 100644 --- a/src/variable.cpp +++ b/src/variable.cpp @@ -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" @@ -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 @@ -4286,7 +4288,7 @@ static const std::unordered_map 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, @@ -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