CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums

Advanced tips for working with the OpenFOAM shell environment

Register Blogs Members List Search Today's Posts Mark Forums Read

Rating: 3 votes, 5.00 average.

Advanced tips for working with the OpenFOAM shell environment

Posted August 28, 2011 at 07:51 by wyldckat
Updated January 1, 2014 at 11:11 by wyldckat (Please go to http://openfoamwiki.net/index.php/Installation/Working_with_the_Shell)

Finally finished writing a better page than this one at openfoamwiki.net: http://openfoamwiki.net/index.php/In...with_the_Shell

----------------------------------
This blog post envisions to explain the advanced usages of OpenFOAM's shell environment. For now, only the most common ideas will be explained here, but the idea is to expand it to a size worthy for then moving to openfoamwiki.net.
  1. New to Linux? Then Google "Linux tutorial" Although, if you are lazy, here's some good start: Why? Because this blog post currently doesn't explain all of the basics about Linux, only the things more closely related to OpenFOAM... with a hint of stuff from Linux
    But... Why? You must learn that with OpenFOAM you will be doing a lot of searching! So you better get use to searching for knowledge about OpenFOAM, instead of simply going around asking in forums what to do next
    _________________________________
  2. OpenFOAM shell environment

    The shell environment is where all of the variables that are allocated in your currently running shell. To know which variables exist on your current shell, try running one of these commands:
    Code:
    env
    export
    If you already have a working OpenFOAM environment, you should see these variables among the output from the above commands:
    Code:
    WM_PROJECT_DIR
    FOAM_APPBIN
    Try these commands if you can't seem them clearly:
    • If env worked:
      Code:
      env | grep WM_PROJECT_DIR
    • If export worked:
      Code:
      export | grep WM_PROJECT_DIR

    Variables started with "WM_" and "FOAM_" are usually related to OpenFOAM. If you want to see the content of one of those variables, run:
    Code:
    echo $WM_PROJECT_DIR
    In case it points to a file or folder, you can see what it has or even go to it:
    Code:
    ls -l $WM_PROJECT_DIR
    cd $WM_PROJECT_DIR
    _________________________________
  3. Starting the OpenFOAM shell environment


    Traditionally, you are simply instructed to add something like the following line to the file "~/.bashrc":
    Code:
    . $HOME/OpenFOAM/OpenFOAM-X.Y.Z/etc/bashrc
    Or:
    Code:
    source $HOME/OpenFOAM/OpenFOAM-X.Y.Z/etc/bashrc
    The dot at the beginning of the line does the same as source, namely it will run the file as if it where a local script, therefore setting all of the necessary environment variables.

    Note: The file "~/.bashrc" is the same as "$HOME/.bashrc", which you can see the location for it by running:
    Code:
    echo $HOME/.bashrc
    Well, this is all fine and dandy, but what happens when you need more than one version of OpenFOAM? The main idea is that people should upgrade to the latest version, since it should have less bugs than the previous one. But adapting solvers, utilities and cases from one version to another can sometimes be complicated. And then there is also the Extend project, which provides an extended version of OpenFOAM with GGI and a whole lot of other stuff.. with the downside of being 1 to 2 years behind the official version

    Anyway, you have a lot of version you want/need to be available on your machine. So what to do? Resort to aliases! Edit "~/.bashrc", remove or comment any lines you've got there and add lines like these:
    Code:
    alias of15='. $HOME/OpenFOAM/OpenFOAM-1.5/etc/bashrc'
    alias of15x='. $HOME/OpenFOAM/OpenFOAM-1.5.x/etc/bashrc'
    alias of16='. $HOME/OpenFOAM/OpenFOAM-1.6/etc/bashrc'
    alias of16x='. $HOME/OpenFOAM/OpenFOAM-1.6.x/etc/bashrc'
    alias of170='. $HOME/OpenFOAM/OpenFOAM-1.7.0/etc/bashrc'
    alias of171='. $HOME/OpenFOAM/OpenFOAM-1.7.1/etc/bashrc'
    alias of17x='. $HOME/OpenFOAM/OpenFOAM-1.7.x/etc/bashrc'
    alias of200='. $HOME/OpenFOAM/OpenFOAM-2.0.0/etc/bashrc'
    alias of201='. $HOME/OpenFOAM/OpenFOAM-2.0.1/etc/bashrc'
    alias of20x='. $HOME/OpenFOAM/OpenFOAM-2.0.x/etc/bashrc'
    alias of15dev='. $HOME/OpenFOAM/OpenFOAM-1.5-dev/etc/bashrc'
    alias of16ext='. $HOME/OpenFOAM/OpenFOAM-1.6-ext/etc/bashrc'
    Add only the ones you need and adapt to suit your installation!!!

    This way, the game play has changed: now whenever you start a new terminal/console window or tab, the OpenFOAM environment is not available by default. You have to run one of the alias commands to activate the desired environment. So, start a new terminal and run one of the alias commands... for example:
    Code:
    of20x
    If all goes well, you now have on your terminal the ready to be used environment for the chosen OpenFOAM.
    This now means that whenever you need another version of OpenFOAM, you must start a new terminal!

There is still more to come...

Note: read the second comment for more information!
« Prev     Main     Next »
Total Comments 5

Comments

  1. Old Comment
    Hi Bruno.

    I would propose a change:
    I would make the alias point to the global openfoam bashrc, and not the local one
    When you install openfoam (I means SGI OF 2.1.1) the installer suggest to source your .bashrc with the "global" bashrc, not the local one
    . /opt/openfoam211/etc/bashrc

    http://www.openfoam.org/download/ubuntu.php...
    even for suse users :-)
    http://www.openfoam.org/download/suse.php

    (even more, my special account "cfduser" doesn't even have the ~/etc directory...)

    ------------------------------------------
    Next my two questions:

    1) your main idea is to REMOVE the sourcing of the OF bashrc, and to SUBSTITUTE it with the alias. Right ?


    2) I had the idea to create two different user (say cfduser2.1.1. and cfduser 1.7.1) and install two different bashrc in the two.
    Your trick seems clearly better, because can keep the same user, but
    does even manage the different programs like
    -paraview
    -snappy
    -...

    that the different distribution may install ?
    permalink
    Posted October 14, 2012 at 14:59 by Giuliano69 Giuliano69 is offline
  2. Old Comment
    Hi Giuliano,

    Well, I did write on the blog post that it isn't complete yet...

    Nonetheless, OpenFOAM can be installed for all users (in "/opt" or "/usr/local" or anything similar) or be installed for a single user (usually "$HOME/OpenFOAM").
    If you look at the instructions for building from source and git repository, you'll see that "$HOME/OpenFOAM" is standard installation path for these instructions!

    This alias methodology (FYI: it was not me who discovered it ) can be used for any installations of OpenFOAM. For example, you can easily have this:
    Code:
    alias of20x='. $HOME/OpenFOAM/OpenFOAM-2.0.x/etc/bashrc'
    alias of211='source /opt/openfoam211/etc/bashrc'
    alias of21x='source $HOME/OpenFOAM/OpenFOAM-2.1.x/etc/bashrc'
    alias of16ext='. $HOME/OpenFOAM/OpenFOAM-1.6-ext/etc/bashrc'
    Note: The source command and the dot command are basically identical.

    As for where to place these aliases:
    • Do not confuse these two files:
      • "OpenFOAM-2.1.x/etc/bashrc" - this file has the settings for OpenFOAM.
      • "~/.bashrc" is the personal file for setting up the shell environment.
    • You do not have to define these aliases in "~/.bashrc", because there are dedicated files for this, as you can see from the source code of that file. Examples:
      • Ubuntu has a dedicated user file for aliases: "~/.bash_aliases"
      • openSUSE has this one: "~/.alias"
      Both are called from the user's "~/.bashrc" file.
    • And yes, the idea is to replace the conventional line, such as this one:
      Code:
      source /opt/openfoam211/etc/bashrc
      And replace that line for the various aliases you wish!
    This way you don't need a user per version of OpenFOAM!


    As for running in parallel, the trick is to use foamJob, which relies on foamExec to setup the correct environment on the remote machines.


    As for system-wide installations, the advice is basically this:
    1. Build with a normal user account and follow the desired building instructions.
    2. When finished, go into root mode, copy the whole installation to the desired global folder, such as "/opt/OpenFOAM".
    3. Then change the permissions on this whole folder, to avoid unintentional code/binary modifications.
    4. Edit the file "OpenFOAM-*/etc/bashrc" and find this big block of unmistakable code:
      Quote:
      Code:
      ################################################################################
      # USER EDITABLE PART: Changes made here may be lost with the next upgrade
      #
      # either set $FOAM_INST_DIR before sourcing this file or set
      # 'foamInstall' below to where OpenFOAM is installed
      #
      # Location of the OpenFOAM installation
      # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      foamInstall=$HOME/$WM_PROJECT
      # foamInstall=~$WM_PROJECT
      # foamInstall=/opt/$WM_PROJECT
      # foamInstall=/usr/local/$WM_PROJECT
      #
      # END OF (NORMAL) USER EDITABLE PART
      ################################################################################
      I think it explains for itself
    5. Switch back to normal user and check if everything is working as intended.

    You can easily create a global files with aliases for all users to use, such as "/opt/OpenFOAM/.ofaliases" and tell users to add the following line to their own "~/.bashrc" file:
    Code:
    source /opt/OpenFOAM/.ofaliases
    Best regards,
    Bruno
    permalink
    Posted October 14, 2012 at 15:38 by wyldckat wyldckat is offline
  3. Old Comment
    Hi, I have OpenFoam2.1.1 installed on my system already. And now I need to have OpenFoam1.5 as well. So now I have this line in my .bashrc: source /opt/OpenFOAM-2.1.1/etc/bashrc
    So should I now just simply comment that line out and add the below line?
    alias of211= 'source /opt/OpenFOAM-2.1.1/etc/bashrc'
    I did that but when I type of211 in my terminal, I get the error "command not found"
    please let me know what I'm missing,

    Thanks
    Mohsen
    permalink
    Posted November 15, 2012 at 18:26 by mhsn mhsn is offline
  4. Old Comment
    Greetings Mohsen,

    Sorry for the late reply. I think the problem is that you left a space between the = and ':
    Quote:
    Code:
    alias of211= 'source /opt/OpenFOAM-2.1.1/etc/bashrc'
    When there shouldn't be a space after the equal sign:
    Code:
    alias of211='source /opt/OpenFOAM-2.1.1/etc/bashrc'
    Best regards,
    Bruno
    permalink
    Posted November 26, 2012 at 15:13 by wyldckat wyldckat is offline
  5. Old Comment
    I want to open a terminal without sourcing OpenFoam's bashrc. What should I do?
    I have tried bash --norc , $ bash --noprofile --norc and env -i bash --rcfile /etc/profile, but non of them worked
    permalink
    Posted January 31, 2019 at 17:02 by Samsam2 Samsam2 is offline
 

All times are GMT -4. The time now is 02:33.