Vector Calculus in Bash Scripts

When performing preparatory operations for CFD or FEM analyses, for example, it is often necessary to specify vector parameters. Vector constants are easy to handle:

myMesh --direction "$1 0 0"

However, it quickly becomes complicated if the vectors have to be manipulated, e.g. multiplied by scalars or rotated.

A powerful solution is to use the Matlab Octave clone to perform the operations. Two features of Octave are useful in this context:

  • the command-line option “--eval” to specify the expression to handle
  • The `disp` function outputs results in a plain format on a single line.

Here's the translation: An example:

DIR="1 0 0"
VELOCITY=$(octave --eval "disp( 11.*[$DIR] )")

myMesh --direction "$DIR"
mySolver --velocity "$VELOCITY"

More complicated operations are possible, though one needs to take care to produce line vectors as results. Rotation is an example:

DIR="1 0 0"
ANG=45

DIR_ROT=$(octave --eval "pkg load linear-algebra; disp( (rotv([0 0 1], $ANG*pi/180)*[$DIR]')' )")

myMesh --direction "$DIR_ROT"