git-svn-id: svn://svn.icms.temple.edu/lammps-ro/trunk@12201 f3b2605a-c512-4ea7-a41b-209d697bcdaa

This commit is contained in:
sjplimp 2014-07-28 18:46:56 +00:00
parent cf005551ea
commit 33d9ff4bf4
2 changed files with 55 additions and 20 deletions

View File

@ -44,6 +44,7 @@ using namespace MathConst;
#define MAXLEVEL 4
#define MAXLINE 256
#define CHUNK 1024
#define VALUELENGTH 64
#define MYROUND(a) (( a-floor(a) ) >= .5) ? ceil(a) : floor(a)
@ -306,7 +307,7 @@ void Variable::set(int narg, char **arg)
pad[nvar] = 0;
data[nvar] = new char*[num[nvar]];
copy(1,&arg[2],data[nvar]);
data[nvar][1] = NULL;
data[nvar][1] = new char[VALUELENGTH];
// SCALARFILE for strings or numbers
// which = 1st value
@ -360,7 +361,7 @@ void Variable::set(int narg, char **arg)
pad[nvar] = 0;
data[nvar] = new char*[num[nvar]];
copy(2,&arg[2],data[nvar]);
data[nvar][2] = NULL;
data[nvar][2] = new char[VALUELENGTH];
// EQUAL
// replace pre-existing var if also style EQUAL (allows it to be reset)
@ -376,7 +377,6 @@ void Variable::set(int narg, char **arg)
delete [] data[ivar][0];
if (data[ivar][1]) delete [] data[ivar][1];
copy(1,&arg[2],data[ivar]);
data[ivar][1] = NULL;
replaceflag = 1;
} else {
if (nvar == maxvar) grow();
@ -386,7 +386,7 @@ void Variable::set(int narg, char **arg)
pad[nvar] = 0;
data[nvar] = new char*[num[nvar]];
copy(1,&arg[2],data[nvar]);
data[nvar][1] = NULL;
data[nvar][1] = new char[VALUELENGTH];
}
// ATOM
@ -625,32 +625,24 @@ char *Variable::retrieve(char *name)
strcpy(data[ivar][0],result);
str = data[ivar][0];
} else if (style[ivar] == EQUAL) {
char result[64];
double answer = evaluate(data[ivar][0],NULL);
sprintf(result,"%.15g",answer);
int n = strlen(result) + 1;
if (data[ivar][1]) delete [] data[ivar][1];
data[ivar][1] = new char[n];
strcpy(data[ivar][1],result);
sprintf(data[ivar][1],"%.15g",answer);
str = data[ivar][1];
} else if (style[ivar] == FORMAT) {
char result[64];
int jvar = find(data[ivar][0]);
if (jvar == -1) return NULL;
if (!equalstyle(jvar)) return NULL;
double answer = evaluate(data[jvar][0],NULL);
sprintf(result,data[ivar][1],answer);
int n = strlen(result) + 1;
if (data[ivar][2]) delete [] data[ivar][2];
data[ivar][2] = new char[n];
strcpy(data[ivar][2],result);
sprintf(data[ivar][2],data[ivar][1],answer);
str = data[ivar][2];
} else if (style[ivar] == GETENV) {
const char *result = getenv(data[ivar][0]);
if (data[ivar][1]) delete [] data[ivar][1];
if (result == NULL) result = (const char *) "";
int n = strlen(result) + 1;
if (n > VALUELENGTH) {
delete [] data[ivar][1];
data[ivar][1] = new char[n];
}
strcpy(data[ivar][1],result);
str = data[ivar][1];
} else if (style[ivar] == ATOM || style[ivar] == ATOMFILE) return NULL;
@ -782,6 +774,45 @@ int Variable::atomstyle(int ivar)
return 0;
}
/* ----------------------------------------------------------------------
save copy of EQUAL style ivar formula in copy
allocate copy here, later equal_restore() call will free it
insure data[ivar][0] is of VALUELENGTH since will be overridden
------------------------------------------------------------------------- */
void Variable::equal_save(int ivar, char *&copy)
{
int n = strlen(data[ivar][0]) + 1;
copy = new char[n];
strcpy(copy,data[ivar][0]);
delete [] data[ivar][0];
data[ivar][0] = new char[VALUELENGTH];
}
/* ----------------------------------------------------------------------
restore formula string of EQUAL style ivar from copy
then free copy, allocated in equal_save()
------------------------------------------------------------------------- */
void Variable::equal_restore(int ivar, char *copy)
{
delete [] data[ivar][0];
int n = strlen(copy) + 1;
data[ivar][0] = new char[n];
strcpy(data[ivar][0],copy);
delete [] copy;
}
/* ----------------------------------------------------------------------
override EQUAL style ivar formula with value converted to string
data[ivar][0] was set to length 64 in equal_save()
------------------------------------------------------------------------- */
void Variable::equal_override(int ivar, double value)
{
sprintf(data[ivar][0],"%.15g",value);
}
/* ----------------------------------------------------------------------
remove Nth variable from list and compact list
delete reader explicitly if it exists

View File

@ -36,10 +36,15 @@ class Variable : protected Pointers {
int int_between_brackets(char *&, int);
double evaluate_boolean(char *);
void equal_save(int, char *&);
void equal_restore(int, char *);
void equal_override(int, double);
unsigned int data_mask(int ivar);
unsigned int data_mask(char *str);
private:
int me;
int nvar; // # of defined variables
int maxvar; // max # of variables following lists can hold
char **names; // name of each variable
@ -57,7 +62,6 @@ class Variable : protected Pointers {
int precedence[17]; // precedence level of math operators
// set length to include up to OR in enum
int me;
struct Tree { // parse tree for atom-style variables
double value; // single scalar