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

Passing envars to dictionary's

Register Blogs Community New Posts Updated Threads Search

Like Tree2Likes
  • 2 Post By floquation

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   January 31, 2017, 09:32
Default Passing envars to dictionary's
  #1
Member
 
Victor Koppejan
Join Date: May 2015
Posts: 40
Rep Power: 11
vkoppejan is on a distinguished road
Hi everyone,

I was wondering if there's a way to pass environment variables to OpenFOAM discitonaries.

For instance, say I define ndoms = 4 (via export ndoms = 4), could I somehow use $ndoms in the decomposeParDict? I start my runs from slurm so all var's are defined for a single simulation only.

Cheers,

Victor
vkoppejan is offline   Reply With Quote

Old   January 31, 2017, 09:43
Default
  #2
Senior Member
 
floquation's Avatar
 
Kevin van As
Join Date: Sep 2014
Location: TU Delft, The Netherlands
Posts: 252
Rep Power: 20
floquation will become famous soon enough
I use "pyFoamFromTemplate.py" in a "prepareScript.sh" script to substitute variables into dictionaries.
Parameters I specify in a local file called "params.sh", but environmental variables are possible as well, as long as you define what I called "PARAMS_SETFIELD".

Minimal example:
./params.sh:
Code:
# Geometry
Wz=`echo "2*0.005" | bc -l`
Gamma=0.5

Wx=`echo "$Wz*$Gamma" | bc -l`
Wy=$Wx

# Bubble
h_b=`echo "0.1*$Wz" | bc -l` # initial bubble height
r_b=`echo "0.05*$Wz" | bc -l` # initial bubble radius

x_b=`echo "$Wx/2" | bc -l`
y_b=$x_b

# (...)

PARAMS_SETFIELD="{'x_b':$x_b,'y_b':$y_b,'h_b':$h_b,'r_b':$r_b}"
./prepareScript.sh:
Code:
usage()
{
        echo " Usage: $0"
        echo " A file named \"params.sh\" should be in your cwd. "
        exit;
}
#### ####### #####

#### PARAMETERS #####
if [ -f "./params.sh" ]; then
    . ./params.sh
else
    usage
fi

# (...)

pyFoamFromTemplate.py ./system/setFieldsDict $PARAMS_SETFIELD || exit 1

# (...)

# setFields
echo "----> setFields"
mv "0/alpha.water.org" "0/alpha.water"
setFields > ./logs/setFields.log || exit 1
./system/setFieldsDict.template:
Code:
/*--------------------------------*- C++ -*----------------------------------*\
| =========                 |                                                 |
| \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox           |
|  \\    /   O peration     | Version:  4.0                                   |
|   \\  /    A nd           | Web:      www.OpenFOAM.org                      |
|    \\/     M anipulation  |                                                 |
\*---------------------------------------------------------------------------*/
FoamFile
{
    version     2.0;
    format      ascii;
    class       dictionary;
    location    "system";
    object      setFieldsDict;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

defaultFieldValues
(
    volScalarFieldValue alpha.water 1
);

regions
(
    sphereToCell
    {
        centre ($x_b$ $y_b$ $h_b$);
        radius $r_b$;
        fieldValues
        (
            volScalarFieldValue alpha.water 0
        );
    }
 );


// ************************************************************************* //
arvindpj and vkoppejan like this.
floquation is offline   Reply With Quote

Old   January 31, 2017, 10:42
Default
  #3
Member
 
Victor Koppejan
Join Date: May 2015
Posts: 40
Rep Power: 11
vkoppejan is on a distinguished road
Looks nice, I haven't scripted OF with python before but this looks clever.

Cheers,

Victor
vkoppejan is offline   Reply With Quote

Old   January 31, 2017, 10:47
Default
  #4
Senior Member
 
floquation's Avatar
 
Kevin van As
Join Date: Sep 2014
Location: TU Delft, The Netherlands
Posts: 252
Rep Power: 20
floquation will become famous soon enough
Quote:
Originally Posted by vkoppejan View Post
Looks nice, I haven't scripted OF with python before but this looks clever.
I'm lazy, so I want the computer to do as much as possible for me.
That... Or I like writing code more than typing parameters and running a case.

Note though, I did not actually write any python myself here. Of course, pyFoam is a python library, but all I did was write bash.
floquation 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
CFD by anderson, chp 10.... supersonic flow over flat plate varunjain89 Main CFD Forum 18 May 11, 2018 07:31
Retrieve query point coordonate to create a plane passing through this point. potofus EnSight 2 April 6, 2016 07:26
Finding the number of particles passing through a plane in a transient DPM simulation tengteng FLUENT 2 February 10, 2016 02:25
Inlet passing wake sherifkadry OpenFOAM 0 June 26, 2012 20:04
gambit meshing for the flow passing cylinder iilw1314 Main CFD Forum 5 April 11, 2010 19:28


All times are GMT -4. The time now is 12:22.