Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variable style 'string' substitutes variables in definition #948

Merged
merged 1 commit into from Jun 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions doc/src/variable.txt
Expand Up @@ -296,10 +296,11 @@ list of runs (e.g. 1000) without having to list N strings in the input
script.

For the {string} style, a single string is assigned to the variable.
The only difference between this and using the {index} style with a
single string is that a variable with {string} style can be redefined.
E.g. by another command later in the input script, or if the script is
read again in a loop.
Two differences between this this and using the {index} style exist:
a variable with {string} style can be redefined, e.g. by another command later
in the input script, or if the script is read again in a loop. The other
difference is that {string} performs variable substitution even if the
string parameter is quoted.

For the {format} style, an equal-style variable is specified along
with a C-style format string, e.g. "%f" or "%.10g", which must be
Expand Down
14 changes: 12 additions & 2 deletions src/variable.cpp
Expand Up @@ -284,12 +284,21 @@ void Variable::set(int narg, char **arg)

} else if (strcmp(arg[1],"string") == 0) {
if (narg != 3) error->all(FLERR,"Illegal variable command");

int maxcopy = strlen(arg[2]) + 1;
int maxwork = maxcopy;
char *scopy = new char[maxcopy];
char *work = new char[maxwork];
strcpy(scopy,arg[2]);
input->substitute(scopy,work,maxcopy,maxwork,1);
delete [] work;

int ivar = find(arg[0]);
if (ivar >= 0) {
if (style[ivar] != STRING)
error->all(FLERR,"Cannot redefine variable as a different style");
delete [] data[ivar][0];
copy(1,&arg[2],data[ivar]);
copy(1,&scopy,data[ivar]);
replaceflag = 1;
} else {
if (nvar == maxvar) grow();
Expand All @@ -298,8 +307,9 @@ void Variable::set(int narg, char **arg)
which[nvar] = 0;
pad[nvar] = 0;
data[nvar] = new char*[num[nvar]];
copy(1,&arg[2],data[nvar]);
copy(1,&scopy,data[nvar]);
}
delete [] scopy;

// GETENV
// remove pre-existing var if also style GETENV (allows it to be reset)
Expand Down
1 change: 1 addition & 0 deletions src/variable.h
Expand Up @@ -16,6 +16,7 @@

#include <cstdlib>
#include "pointers.h"
#include "input.h"

namespace LAMMPS_NS {

Expand Down