OOMMF Home next up previous index
Next: Sample MIF 2.1 File Up: MIF 2.1 Previous: Specify Conventions


Variable Substitution

One powerful consequence of the evaluation of MIF 2.1 input files by Tcl is the ability to define and use variables. For example, the Oxs interfaces (Oxsii and Boxsi) use the -parameter command line option in conjunction with the MIF Parameter command to set variables from the command line for use inside the MIF input file. Variables in Tcl are evaluated (i.e., value substituted) by prefixing the variable name with the symbol ``$''. For example, if cellsize is a variable holding the value 5e-9, then $cellsize evaluates to 5e-9.

Unfortunately, there are complications in using variables inside Specify blocks. Consider this simple example:

Parameter cellsize 5e-9
Specify Oxs_RectangularMesh:BadExample {
  comment {NOTE: THIS DOESN'T WORK!!!}
  cellsize {$cellsize $cellsize $cellsize}
  atlas :atlas
}
This doesn't work, because the curly braces used to set off the Specify initialization string also inhibit variable substitution. There are several ways to work around this, but the easiest is usually to embed the initialization string inside a subst (substitution) command:
Parameter cellsize 5e-9
Specify Oxs_RectangularMesh:GoodExample [subst {
  comment {NOTE: This works.}
  cellsize {$cellsize $cellsize $cellsize}
  atlas :atlas
}]
Here the square brackets, ``['' and ``]'', cause Tcl to perform command substitution, i.e., execute the string inside the square brackets as a Tcl command, in this case the subst command. See the Tcl documentation for subst for details, but the default usage illustrated above performs variable, command and backslash substitutions on the argument string.

One more example, this time involving both variable and command substitution:

set pi [expr {4*atan(1.0)}]
set mu0 [expr {4*$pi*1e-7}]
Specify Oxs_UZeeman [subst {
  comment {Set units to mT}
  Hscale [expr {0.001/$mu0}]
  Hrange {
     {  0  0  0   10  0  0   2 }
     { 10  0  0  -10  0  0   2 }
  }
}]
Note that the subst command is evaluated at global scope, so that the global variable mu0 is directly accessible.


OOMMF Home next up previous index

OOMMF Documentation Team
September 30, 2022