Unverified Commit 08331da9 authored by Steve Plimpton's avatar Steve Plimpton Committed by GitHub
Browse files

Merge pull request #950 from martok/subst-equal-fmt

Add support for inline format strings to immediate variable substitution
parents ab0f3deb c5b51df0
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -129,6 +129,15 @@ region 1 block $((xlo+xhi)/2+sqrt(v_area)) 2 INF INF EDGE EDGE :pre

so that you do not have to define (or discard) a temporary variable X.

Additionally, the "immediate" expression may be followed by a colon,
followed by a C-style format string, e.g. "%f" or "%.10g", which must be
appropriate for formatting a double-precision floating-point value. The
format string will be used to output the result of the variable evaluation,
so you do not have to define a temporary "format-style variable"_variable.html.
This may be used for formatting print output:

print "Final energy per atom: $(pe/atoms:%10.3f) eV/atom" :pre

Note that neither the curly-bracket or immediate form of variables can
contain nested $ characters for other variables to substitute for.
Thus you cannot do this:
+8 −1
Original line number Diff line number Diff line
@@ -516,7 +516,14 @@ void Input::substitute(char *&str, char *&str2, int &max, int &max2, int flag)
        if (var[i] == '\0') error->one(FLERR,"Invalid immediate variable");
        var[i] = '\0';
        beyond = ptr + strlen(var) + 3;
        sprintf(immediate,"%.20g",variable->compute_equal(var));
        // check if an inline format specifier was given
        char fmtstr[64] = "%.20g";
        char *fmtflag;
        if ((fmtflag=strrchr(var, ':')) && (fmtflag[1]=='%')) {
          strncpy(fmtstr,&fmtflag[1],sizeof(fmtstr)-1);
          *fmtflag='\0';
        }
        sprintf(immediate,fmtstr,variable->compute_equal(var));
        value = immediate;

        // single character variable name, e.g. $a