How to create a String out of 2 other strings?

Submitted by JoshuaP on Thu, 11/13/2014 - 16:13

Hey,

it is possible to do

"compute ${omeg} all reduce ave c_atomprops[1]
compute ${vy} all reduce ave c_atomprops[2]
variable a string c_${omeg}
variable b string c_${vy}"

and also it is possible to give a string to the next:
"variable allresult string ${a}"

but why is it not possible to create a string out of two or more strings?
"variable allresult string ${a} ${b}"

doing so yields the following error:
ERROR: Illegal variable command (../variable.cpp:278)

in the cpp it is written:
if (narg != 3) error->all(FLERR,"Illegal variable command");

so what is the problem if narg is not equal 3?

how can I best deal with that?

Thanks

JoshuaP | Thu, 11/13/2014 - 17:24

I can use universe instead of string

richti83's picture

richti83 | Thu, 11/13/2014 - 19:18

I have a hack for this problem:
in variable.cpp somewhere arround line 272 (must not exactly match)

// STRING
// remove pre-existing var if also style STRING (allows it to be reset)
// num = 1, which = 1st value
// data = 1 value, string to eval
} else if (strcmp(arg[1],"string") == 0) {
if (narg < 3) {printf("narg=%d",narg);error->all(FLERR,"Illegal variable command");}
if (find(arg[0]) >= 0) {
if (style[find(arg[0])] != STRING)
error->all(FLERR,"Cannot redefine variable as a different style");
remove(find(arg[0]));
}
if (nvar == maxvar) grow();
style[nvar] = STRING;
num[nvar] = 1;
which[nvar] = 0;
pad[nvar] = 0;
data[nvar] = new char*[num[nvar]];
if (narg==3) {
int n=strlen(arg[2])+1;
data[nvar][0]=new char[n];
strcpy(data[nvar][0],arg[2]);
} else if (narg>=4) {
int n=strlen(arg[2])+1; //preserve one for \0
for (int i=3;i<narg;i++)
n+=strlen(arg[i])+1; //preserve one for each space
data[nvar][0]=new char[n];
strcpy(data[nvar][0],arg[2]);
for (int i=3;i < narg;i++) {
strcat(data[nvar][0]," "); //space between the arguments
strcat(data[nvar][0],arg[i]);
}
}
// GETENV

This add a string concatination function to variable string with a whitespace between the arguments.
Best,
Christian.

I'm not an associate of DCS GmbH and not a core developer of LIGGGHTS®
ResearchGate | Contact

JoshuaP | Fri, 11/14/2014 - 10:14

that is exactly what I'm looking for! You helped me a lot.
Now I can create a long string with whitespaces in between and use this string for setting up the variablenames for the output.
Thanks