silentdynamics logo silentdynamics

Vector Calculus in Bash Scripts

OSS CAE Blog · Hannes Kröger

Preparatory steps for CFD or FEM analyses often need vector parameters. Vector constants are easy to handle:

myMesh --direction "$1 0 0"

It quickly becomes complicated, however, when the vectors have to be manipulated, e.g. multiplied by scalars or rotated.

Octave as a calculator for shell scripts

A powerful solution is to let GNU Octave, the open-source Matlab clone, perform the operations. Two features of Octave are useful here:

  • the command line option --eval takes the expression to evaluate;
  • the function disp prints the result in a plain format on a single line.

An example:

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

Rotating a vector

More complicated operations are possible, but take care that the results are row vectors. 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"