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

Batch scripts

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

Like Tree1Likes

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   April 25, 2019, 21:04
Default Batch scripts
  #1
Senior Member
 
Brett
Join Date: May 2013
Posts: 179
Rep Power: 10
Bdew8556 is on a distinguished road
Hey guys.

I'm new to batch programming and looking to write my first script.
Basically I have an openfoam simulation all set up and ready to go.

I have my geometry, I'm happy with the mesh and boundary conditions selection.

What I want to do is vary the input mass flow rate and record the area averaged pressure at the outlet so I can produce a graph of mass flow rate vs pressure drop.

Any thoughts on how to best write a script like this?
Bdew8556 is offline   Reply With Quote

Old   April 26, 2019, 01:15
Default
  #2
Senior Member
 
Tom-Robin Teschner
Join Date: Dec 2011
Location: Cranfield, UK
Posts: 193
Rep Power: 12
t.teschner is on a distinguished road
that should be pretty straight forward.you probably want to look at things like "sed" (to modify your openfoam scripts through your bash script) and how to work with regular expressions (so that you can find the average pressure at the outlet after the calculation is done, assuming the average pressure is written to screen).
t.teschner is offline   Reply With Quote

Old   April 26, 2019, 01:22
Default
  #3
Senior Member
 
Join Date: Aug 2015
Posts: 486
Rep Power: 11
clapointe is on a distinguished road
It is pretty straight forward. You can do a loop of simulations like this :

Code:
#!/bin/bash

#for loop from 0 to 0.4 in 0.1 intervals
for i in $(seq 0 0.1 0.4)
do
    #print case $i
    echo "Starting simulation $i"

    #copy a base case to the current case $i
    cp -rf baseCase baseCase_$i

    #set line number for sed to use
    lineNo1=49

    #replace lineNo1 (line 49) in 0/U with value uniform (0 $i 0)
    sed -i "${lineNo1}s/.*/value uniform (0 $i 0);/" baseCase_$i/0/U

    #enter current case directory
    cd baseCase_$i

    #run solver of choice (e.g. buoyantPimpleFoam)
    buoyantPimpleFoam > log.buoyantPimpleFOAM 2>&1 &

    #find line starting with "Patch Pressure :" in solver log file and save value of 4th set of characters (pretty sure)
    press=`grep -r "Patch Pressure : " log.buoyantPimpleFoam | awk '{print $4}'`

    #pipe saved value to data log file
    echo press >> log.dat

    cd ..

done
You can use a function object -- inlcuded in the controlDict -- to compute patch pressure at runTime or after the run is finished.

Caelan
joshmccraney likes this.

Last edited by clapointe; April 26, 2019 at 10:50.
clapointe is offline   Reply With Quote

Old   April 26, 2019, 01:24
Default
  #4
Senior Member
 
Brett
Join Date: May 2013
Posts: 179
Rep Power: 10
Bdew8556 is on a distinguished road
its probably very straightforward, i just dont know the syntax. have you got some examples?
Bdew8556 is offline   Reply With Quote

Old   April 26, 2019, 01:26
Default
  #5
Senior Member
 
Brett
Join Date: May 2013
Posts: 179
Rep Power: 10
Bdew8556 is on a distinguished road
thanks for that.
I must admit I didn't understand any of it.

What does each line mean?
Bdew8556 is offline   Reply With Quote

Old   April 26, 2019, 06:33
Default
  #6
Member
 
Elwardi Fadeli
Join Date: Dec 2016
Location: Boumerdes, Algeria
Posts: 39
Rep Power: 6
ELwardi is on a distinguished road
You're probably looking for a templating engine as it allows for more complex tasks to be achieved easily.

There are many good engines, most of them are geared towards HTML programming. I would suggest mustache for simple tasks:

Head to the demo page and replace the demo content of the "mustache template" with :
Code:
{{#patches}}
{{name}}
{
    type     {{type}};
    value    unifrom {{value}};
}
{{/patches}}
Then, in the JSON section, you would say for example:
Code:
{
  "patches": [
      {"name":"patch01", "type":"fixedValue", "value":"1e-5"},
       {"name":"patch02", "type":"fixedValue", "value":"3e-5"}
  ]
}
All you have to do now is to click on the "Render Template" button.

I like mustache mainly because it's simple, available in many languages (including Bash) and works fine with my text editor.

Some people also like to use a "Macro Language", like M4, for such tasks, but I don't think that's the most efficient approach for our line of work .

If you intend to use this for OpenFOAM-related tasks only, I think the best option would be to use a dedicated tool, like pyFoam's pyFoamPrepareCase.py. See this.
ELwardi is offline   Reply With Quote

Old   April 26, 2019, 10:56
Default
  #7
Senior Member
 
Join Date: Aug 2015
Posts: 486
Rep Power: 11
clapointe is on a distinguished road
I just edited the scrip with comments. Note that it is an old script I pared down to show something that can work, and comments are to my best recollection. You'll likely need to change the values used in the for loop, what's edited (in what file) for initial/boundary conditions, and what's pulled from the log file.

Caelan
clapointe is offline   Reply With Quote

Old   April 29, 2019, 19:13
Default
  #8
Senior Member
 
Brett
Join Date: May 2013
Posts: 179
Rep Power: 10
Bdew8556 is on a distinguished road
Thanks Caelan,

Ill give it a go!
Bdew8556 is offline   Reply With Quote

Old   April 29, 2019, 20:25
Default
  #9
Senior Member
 
Brett
Join Date: May 2013
Posts: 179
Rep Power: 10
Bdew8556 is on a distinguished road
what file is the base case? is that the top directory of my project? ie the one above o, constant and system?

also you mentioned a function object? whats that?
Bdew8556 is offline   Reply With Quote

Old   April 29, 2019, 20:36
Default
  #10
Senior Member
 
Join Date: Aug 2015
Posts: 486
Rep Power: 11
clapointe is on a distinguished road
Yes, you would run this scrip from a directory that contains the case template, baseCase. The name of course is arbitrary. Function objects are pretty standard openFoam : https://github.com/OpenFOAM/OpenFOAM...unctionObjects.

Caelan
clapointe is offline   Reply With Quote

Old   April 29, 2019, 20:40
Default
  #11
Senior Member
 
Brett
Join Date: May 2013
Posts: 179
Rep Power: 10
Bdew8556 is on a distinguished road
ok. but what does the case template look like? say im in my project directory, which I'll call "basecase" what will the file look like?
The functions look interesting. how do I do something with them? do I enter something in the command line after I've stopped the simulation? That link just gives H and C files, I'm not sure what to do with them. VERY basic level of skills at this point...
Bdew8556 is offline   Reply With Quote

Old   April 29, 2019, 21:32
Default
  #12
Senior Member
 
Join Date: Aug 2015
Posts: 486
Rep Power: 11
clapointe is on a distinguished road
The base case is a clean version of whatever case you are studying. 0, constant, and system files. So you would run from a folder containing the base case (empty), which would be copied by the bash script. Function objects are basically additional functionality that can be linked at runtime via the control dict. There are various tutorials that use them -- I linked the source files to show where they come from.

Caelan
clapointe is offline   Reply With Quote

Old   April 30, 2019, 01:52
Default
  #13
Senior Member
 
Brett
Join Date: May 2013
Posts: 179
Rep Power: 10
Bdew8556 is on a distinguished road
cool.

Where does the solver log file appear?
Bdew8556 is offline   Reply With Quote

Old   April 30, 2019, 02:02
Default
  #14
Senior Member
 
Brett
Join Date: May 2013
Posts: 179
Rep Power: 10
Bdew8556 is on a distinguished road
actually I set it up and just tried to run it, it produces this error:

$ batch.sh
./batch.sh: line 2: $'\r': command not found
./batch.sh: line 5: syntax error near unexpected token `$'do\r''
'/batch.sh: line 5: `do
Bdew8556 is offline   Reply With Quote

Old   April 30, 2019, 02:59
Default
  #15
Member
 
Adam
Join Date: Nov 2018
Posts: 36
Rep Power: 4
Adam_K is on a distinguished road
Quote:
Originally Posted by clapointe View Post
I just edited the scrip with comments. Note that it is an old script I pared down to show something that can work, and comments are to my best recollection. You'll likely need to change the values used in the for loop, what's edited (in what file) for initial/boundary conditions, and what's pulled from the log file.

Caelan
Thanks for that description!

How would one change that so that one simulation finishes before the next one starts?

Last edited by Adam_K; April 30, 2019 at 06:49.
Adam_K is offline   Reply With Quote

Old   April 30, 2019, 12:25
Default
  #16
Senior Member
 
Join Date: Aug 2015
Posts: 486
Rep Power: 11
clapointe is on a distinguished road
@Bdew8556 : if you make it an executable (chmod +x) and run it with "./runScriptName". It should work fine -- it works for me.

@Adam_K : this will do that by design. It takes some extra magic (coding) to allow multiple simulations to run at the same time.

Caelan
clapointe is offline   Reply With Quote

Old   April 30, 2019, 19:05
Default
  #17
Senior Member
 
Brett
Join Date: May 2013
Posts: 179
Rep Power: 10
Bdew8556 is on a distinguished road
Hey guys.

It doesn't start the first loop/simulation, it just shows the errors then brings up another command line, so nothing is executed.

What is a chmod+ and how would I do that?
It looks like the for loop syntax might not be right??
Bdew8556 is offline   Reply With Quote

Old   April 30, 2019, 19:20
Default
  #18
Senior Member
 
Join Date: Aug 2015
Posts: 486
Rep Power: 11
clapointe is on a distinguished road
It could be crashing for any number of reasons -- debugging is up to you. If you simply tried to execute the script without modifying it to fit your needs, of course it won't work (e.g. the way I put it you would have had to preran blockMesh in baseCase). That said, I just verified that the script works just fine to at least start a number of simulations. So to be explicitly clear :

Use chmod to make the script -- call it Allrun -- an executable :

Code:
chmod +x Allrun
.

Allrun can now be used as an executable :

Code:
./Allrun
That will execute the series of commands, as if you were waiting at the computer and typing them in sequentially. If you do not understand what a command does, google is your friend.

Caelan
clapointe is offline   Reply With Quote

Old   April 30, 2019, 19:29
Default
  #19
Senior Member
 
Brett
Join Date: May 2013
Posts: 179
Rep Power: 10
Bdew8556 is on a distinguished road
so if I name my script "Allrun" then type "chmod +x Allrun" into the command line, then type "/Allrun" it will work? I've changed the script to fit my file names and my purposes.
Bdew8556 is offline   Reply With Quote

Old   April 30, 2019, 19:34
Default
  #20
Senior Member
 
Join Date: Aug 2015
Posts: 486
Rep Power: 11
clapointe is on a distinguished road
Did you try it? Essentially, yes -- although it is likely that future debugging will be necessary. Don't forget the "." in "./Allrun".

Caelan
clapointe is offline   Reply With Quote

Reply

Tags
batch job, programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Using Batch file in combination with the workbench D0nT0m CFX 3 February 6, 2017 05:28
Fluent batch command for unsteady taekyu8 FLUENT 0 January 7, 2013 15:18
Regarding about Fluent batch scripts solefire FLUENT 3 July 5, 2009 09:39
regarding about Fluent batch scripts solefire FLUENT 3 July 5, 2009 04:05
Running Job in Batch mode (EFD) Nick Sessions FloEFD, FloWorks & FloTHERM 0 April 16, 2008 16:44


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