From 9e1685211b1f3c6295758d7f413b14b8c801c7b3 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Fri, 15 Jul 2022 05:39:17 -0400 Subject: [PATCH] allow ramp(x,y) to be used in between runs (returning x) and avoid division by zero on run 0 --- doc/src/variable.rst | 4 +++- src/variable.cpp | 37 +++++++++++++++++++++++++------------ 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/doc/src/variable.rst b/doc/src/variable.rst index 9f76f78f45..ceaa4d7245 100644 --- a/doc/src/variable.rst +++ b/doc/src/variable.rst @@ -685,7 +685,9 @@ of a run, according to this formula: The run begins on startstep and ends on stopstep. Startstep and stopstep can span multiple runs, using the *start* and *stop* keywords of the :doc:`run ` command. See the :doc:`run ` command for -details of how to do this. +details of how to do this. If called in between runs or during a +:doc:`run 0 ` command, the ramp(x,y) function will return the +value of x. The stagger(x,y) function uses the current timestep to generate a new timestep. X,y > 0 and x > y are required. The generated timesteps diff --git a/src/variable.cpp b/src/variable.cpp index 878066f466..574aa26c86 100644 --- a/src/variable.cpp +++ b/src/variable.cpp @@ -2561,9 +2561,14 @@ double Variable::collapse_tree(Tree *tree) arg2 = collapse_tree(tree->second); if (tree->first->type != VALUE || tree->second->type != VALUE) return 0.0; tree->type = VALUE; - double delta = update->ntimestep - update->beginstep; - if (delta != 0.0) delta /= update->endstep - update->beginstep; - tree->value = arg1 + delta*(arg2-arg1); + if (update->whichflag == 0) { + tree->value = arg1; + } else { + double delta = update->ntimestep - update->beginstep; + if ((delta != 0.0) && (update->beginstep != update->endstep)) + delta /= update->endstep - update->beginstep; + tree->value = arg1 + delta*(arg2-arg1); + } return tree->value; } @@ -2935,9 +2940,14 @@ double Variable::eval_tree(Tree *tree, int i) if (tree->type == RAMP) { arg1 = eval_tree(tree->first,i); arg2 = eval_tree(tree->second,i); - double delta = update->ntimestep - update->beginstep; - if (delta != 0.0) delta /= update->endstep - update->beginstep; - arg = arg1 + delta*(arg2-arg1); + if (update->whichflag == 0) { + arg = arg1; + } else { + double delta = update->ntimestep - update->beginstep; + if ((delta != 0.0) && (update->beginstep != update->endstep)) + delta /= update->endstep - update->beginstep; + arg = arg1 + delta*(arg2-arg1); + } return arg; } @@ -3445,14 +3455,17 @@ int Variable::math_function(char *word, char *contents, Tree **tree, Tree **tree } else if (strcmp(word,"ramp") == 0) { if (narg != 2) print_var_error(FLERR,"Invalid math function in variable formula",ivar); - if (update->whichflag == 0) - print_var_error(FLERR,"Cannot use ramp in variable formula between runs",ivar); if (tree) newtree->type = RAMP; else { - double delta = update->ntimestep - update->beginstep; - if (delta != 0.0) delta /= update->endstep - update->beginstep; - double value = value1 + delta*(value2-value1); - argstack[nargstack++] = value; + if (update->whichflag == 0) { + argstack[nargstack++] = value1; + } else { + double delta = update->ntimestep - update->beginstep; + if ((delta != 0.0) && (update->beginstep != update->endstep)) + delta /= update->endstep - update->beginstep; + double value = value1 + delta*(value2-value1); + argstack[nargstack++] = value; + } } } else if (strcmp(word,"stagger") == 0) {