CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Programming & Development

OpenFoam Bash Script

Register Blogs Community New Posts Updated Threads Search

Like Tree3Likes
  • 1 Post By Tobermory
  • 1 Post By Tobermory
  • 1 Post By olesen

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   June 4, 2021, 10:18
Default OpenFoam Bash Script
  #1
New Member
 
Subodh
Join Date: Sep 2013
Location: Canada
Posts: 24
Rep Power: 12
Subodh21 is on a distinguished road
Hi i am new to learning BASH and wondering if someone here can explain me what this bash code is doing.

#!/bin/sh

usage () {
exec 1>&2
while [ "$#" -ge 1 ]; do echo "$1"; shift; done
cat <<USAGE
Usage: ${0##*/} [OPTIONS]

Options:
-i | -interface no refinment in vertical direction of the mesh
-l | -local mesh with local refinment
-h | -help help

Ship hull simulation to demonstrate two different meshing strategies that can be
used with PLIC type schemes.

USAGE
exit 1
}

meshType=0

# OPTIONS
while [ "$#" -gt 0 ]
do
case "$1" in
-i | -interface)
meshType=1
break
;;
-l | -local)
meshType=2
break
;;
-h | -help)
usage
;;
-test)
shift
;;
-*)
usage "Invalid option '$1'"
;;
*)
usage "Invalid option '$1'"
break
;;
esac
done


# Run from this directory
cd "${0%/*}" || exit 1

# Source tutorial run functions
. "$WM_PROJECT_DIR/bin/tools/RunFunctions"

if [ $meshType -eq 0 ] || [ $meshType -eq 1 ]; then
{
./Allmesh.1
}
elif [$meshType -eq 2 ]; then
{
./Allmesh.2
}
fi

runApplication setFields

runApplication decomposePar

runParallel $(getApplication)

runApplication reconstructPar

#------------------------------------------------------------------------------


Thanks in advance!!
Subodh21 is offline   Reply With Quote

Old   June 6, 2021, 08:42
Default
  #2
Senior Member
 
Join Date: Apr 2020
Location: UK
Posts: 669
Rep Power: 14
Tobermory will become famous soon enough
Okay, well here goes:

Code:
#!/bin/sh
defines this script as a Bourne shell script; note that if you want it to be interpreted as a bash script, then this should be /bin/bash

Code:
usage ()
   ...
}
defines a function that prints out help info on how to run the command

Code:
while [ "$#" -gt 0 ]
do
  case "$1" in
    -i | -interface)
      meshType=1
      break
      ;;
    -l | -local)
      meshType=2
      break
      ;;
    -h | -help)
      usage
      ;;
    -test)
      shift
      ;;
    -*)
      usage "Invalid option '$1'"
      ;;
    *)
      usage "Invalid option '$1'"
      break
      ;;
  esac
done
parses the parameters that the command was called with and sets variable meshType

Code:
cd "${0%/*}" || exit 1
changes working directory to the folder that this file is located in (without this command, if you called the script from another folder then the working directory would be that other folder)

Code:
. "$WM_PROJECT_DIR/bin/tools/RunFunctions"
loads the macro functions from this OF script (includes runApplication etc.)

Code:
if [ $meshType -eq 0 ] || [ $meshType -eq 1 ]; then
  {
    ./Allmesh.1
  }
elif [$meshType -eq 2 ]; then
  {
   ./Allmesh.2
  }
fi
run Allmesh.1 if meshType is 0 or 1, or Allmesh.2 if it's 2.

Code:
runApplication setFields
runApplication decomposePar
run applications setFields and decomposePar (single core)

Code:
runParallel $(getApplication)
runApplication reconstructPar
get the application name from the controlDict file and then run the application in parallel and reconstruct the processor files.

Happy?
petros likes this.
Tobermory is offline   Reply With Quote

Old   June 7, 2021, 10:07
Default
  #3
New Member
 
Subodh
Join Date: Sep 2013
Location: Canada
Posts: 24
Rep Power: 12
Subodh21 is on a distinguished road
Quote:
Originally Posted by Tobermory View Post
Okay, well here goes:

Code:
#!/bin/sh
defines this script as a Bourne shell script; note that if you want it to be interpreted as a bash script, then this should be /bin/bash

Code:
usage ()
   ...
}
defines a function that prints out help info on how to run the command

Code:
while [ "$#" -gt 0 ]
do
  case "$1" in
    -i | -interface)
      meshType=1
      break
      ;;
    -l | -local)
      meshType=2
      break
      ;;
    -h | -help)
      usage
      ;;
    -test)
      shift
      ;;
    -*)
      usage "Invalid option '$1'"
      ;;
    *)
      usage "Invalid option '$1'"
      break
      ;;
  esac
done
parses the parameters that the command was called with and sets variable meshType

Code:
cd "${0%/*}" || exit 1
changes working directory to the folder that this file is located in (without this command, if you called the script from another folder then the working directory would be that other folder)

Code:
. "$WM_PROJECT_DIR/bin/tools/RunFunctions"
loads the macro functions from this OF script (includes runApplication etc.)

Code:
if [ $meshType -eq 0 ] || [ $meshType -eq 1 ]; then
  {
    ./Allmesh.1
  }
elif [$meshType -eq 2 ]; then
  {
   ./Allmesh.2
  }
fi
run Allmesh.1 if meshType is 0 or 1, or Allmesh.2 if it's 2.

Code:
runApplication setFields
runApplication decomposePar
run applications setFields and decomposePar (single core)

Code:
runParallel $(getApplication)
runApplication reconstructPar
get the application name from the controlDict file and then run the application in parallel and reconstruct the processor files.

Happy?
Thanks Tobermory,

Appreciate your reply.
Can you expand a bit more on what these lines of code are doing

usage () {
exec 1>&2
while [ "$#" -ge 1 ]; do echo "$1"; shift; done
cat <<USAGE
Usage: ${0##*/} [OPTIONS]

Also, if you can suggest some resources to learn about Bourne shell scripts, especially from the point of view of Open foam application.
Thanks again!
Subodh21 is offline   Reply With Quote

Old   June 7, 2021, 10:42
Default
  #4
Senior Member
 
Join Date: Apr 2020
Location: UK
Posts: 669
Rep Power: 14
Tobermory will become famous soon enough
Ok - let me paste the whole of the function in and run through it.

Start at the top - the following line denotes that this is a function definition
Code:
usage () {
This next line redirects stdout (ie. output from prints to screen) into stderr (the ouput for error messages)
Code:
exec 1>&2
$# holds the number of parameters that the script was called with, so the while loop keeps executing whilst there are 1 or more parameters in the stack; the code that is executed is as follows: print to the screen the first parameter ($1), then "shift", ie throw away the first parameter and shuffle all the remaining parameters up one place. So all it does it print out all the parameters.
Code:
while [ "$#" -ge 1 ]; do echo "$1"; shift; done
The following treats all of the text between the two instances of USAGE as a block of text, and cat's (ie prints) it out to screen.
Code:
cat <<USAGE
  Usage: ${0##*/} [OPTIONS]

  Options:
  -i | -interface no refinment in vertical direction of the mesh
  -l | -local mesh with local refinment
  -h | -help help

  Ship hull simulation to demonstrate two different meshing strategies that can be
  used with PLIC type schemes.

USAGE
exit is a shell command to stop the script; the 1 is an optional error code. The curly brace denotes the end of the function definition.
Code:
exit 1
}
As for good references - I can't help you there, I am afraid. The best way of learning is by doing ... and by googling: there is a LOT of useful stuff on the web, explaining bash scripts, and lots of blogs, training material etc. Good luck!
petros likes this.
Tobermory is offline   Reply With Quote

Old   June 23, 2021, 13:58
Default
  #5
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,689
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
Quote:
Originally Posted by Tobermory View Post
As for good references - I can't help you there, I am afraid. The best way of learning is by doing ... and by googling: there is a LOT of useful stuff on the web, explaining bash scripts, and lots of blogs, training material etc. Good luck!
Nice of you to have taken the time to explain things. I agree with you that learning by doing is the best (the only?) way. There are a reason enough number of shell scripts within OpenFOAM. Combine those with "man bash" and most of it should be understandable.
For good measure, the old references are still really good:

https://tldp.org/LDP/Bash-Beginners-...tml/index.html
https://tldp.org/LDP/abs/html/
Tobermory likes this.
olesen is offline   Reply With Quote

Old   July 8, 2021, 10:06
Default
  #6
Member
 
Petros Ampatzidis
Join Date: Oct 2018
Location: Bath, UK
Posts: 64
Rep Power: 7
petros is on a distinguished road
Thanks both for the very useful information provided.
petros is offline   Reply With Quote

Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Salome] Script for converting a mesh from Salome-Platform to OpenFOAM nsf OpenFOAM Meshing & Mesh Conversion 86 February 8, 2023 10:30
Running OpenFOAM case through shell script (bash) ilaj OpenFOAM Programming & Development 2 January 28, 2020 13:14
[Other] Assistance needed to download openfoam and bash ubuntu for windows 10 nagaman OpenFOAM Installation 1 April 25, 2019 07:37
OpenFOAM Training Jan-Apr 2017, Virtual, London, Houston, Berlin cfd.direct OpenFOAM Announcements from Other Sources 0 September 21, 2016 11:50
OpenFOAM v3.0.1 Training, London, Houston, Berlin, Jan-Mar 2016 cfd.direct OpenFOAM Announcements from Other Sources 0 January 5, 2016 03:18


All times are GMT -4. The time now is 15:36.