|
[Sponsors] |
April 25, 2019, 22:04 |
Batch scripts
|
#1 |
Senior Member
Brett
Join Date: May 2013
Posts: 212
Rep Power: 14 |
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? |
|
April 26, 2019, 02:15 |
|
#2 |
Senior Member
Tom-Robin Teschner
Join Date: Dec 2011
Location: Cranfield, UK
Posts: 211
Rep Power: 16 |
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).
|
|
April 26, 2019, 02:22 |
|
#3 |
Senior Member
Join Date: Aug 2015
Posts: 494
Rep Power: 15 |
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 Caelan Last edited by clapointe; April 26, 2019 at 11:50. |
|
April 26, 2019, 02:24 |
|
#4 |
Senior Member
Brett
Join Date: May 2013
Posts: 212
Rep Power: 14 |
its probably very straightforward, i just dont know the syntax. have you got some examples?
|
|
April 26, 2019, 02:26 |
|
#5 |
Senior Member
Brett
Join Date: May 2013
Posts: 212
Rep Power: 14 |
thanks for that.
I must admit I didn't understand any of it. What does each line mean? |
|
April 26, 2019, 07:33 |
|
#6 |
Member
Elwardi Fadeli
Join Date: Dec 2016
Location: Boumerdes, Algeria
Posts: 41
Rep Power: 10 |
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}} Code:
{ "patches": [ {"name":"patch01", "type":"fixedValue", "value":"1e-5"}, {"name":"patch02", "type":"fixedValue", "value":"3e-5"} ] } 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. |
|
April 26, 2019, 11:56 |
|
#7 |
Senior Member
Join Date: Aug 2015
Posts: 494
Rep Power: 15 |
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 |
|
April 29, 2019, 20:13 |
|
#8 |
Senior Member
Brett
Join Date: May 2013
Posts: 212
Rep Power: 14 |
Thanks Caelan,
Ill give it a go! |
|
April 29, 2019, 21:25 |
|
#9 |
Senior Member
Brett
Join Date: May 2013
Posts: 212
Rep Power: 14 |
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? |
|
April 29, 2019, 21:36 |
|
#10 |
Senior Member
Join Date: Aug 2015
Posts: 494
Rep Power: 15 |
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 |
|
April 29, 2019, 21:40 |
|
#11 |
Senior Member
Brett
Join Date: May 2013
Posts: 212
Rep Power: 14 |
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... |
|
April 29, 2019, 22:32 |
|
#12 |
Senior Member
Join Date: Aug 2015
Posts: 494
Rep Power: 15 |
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 |
|
April 30, 2019, 02:52 |
|
#13 |
Senior Member
Brett
Join Date: May 2013
Posts: 212
Rep Power: 14 |
cool.
Where does the solver log file appear? |
|
April 30, 2019, 03:02 |
|
#14 |
Senior Member
Brett
Join Date: May 2013
Posts: 212
Rep Power: 14 |
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 |
|
April 30, 2019, 03:59 |
|
#15 | |
Member
Adam
Join Date: Nov 2018
Posts: 36
Rep Power: 8 |
Quote:
How would one change that so that one simulation finishes before the next one starts? Last edited by Adam_K; April 30, 2019 at 07:49. |
||
April 30, 2019, 13:25 |
|
#16 |
Senior Member
Join Date: Aug 2015
Posts: 494
Rep Power: 15 |
@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 |
|
April 30, 2019, 20:05 |
|
#17 |
Senior Member
Brett
Join Date: May 2013
Posts: 212
Rep Power: 14 |
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?? |
|
April 30, 2019, 20:20 |
|
#18 |
Senior Member
Join Date: Aug 2015
Posts: 494
Rep Power: 15 |
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 Caelan |
|
April 30, 2019, 20:29 |
|
#19 |
Senior Member
Brett
Join Date: May 2013
Posts: 212
Rep Power: 14 |
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.
|
|
April 30, 2019, 20:34 |
|
#20 |
Senior Member
Join Date: Aug 2015
Posts: 494
Rep Power: 15 |
Did you try it? Essentially, yes -- although it is likely that future debugging will be necessary. Don't forget the "." in "./Allrun".
Caelan |
|
Tags |
batch job, programming |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Using Batch file in combination with the workbench | D0nT0m | CFX | 3 | February 6, 2017 06:28 |
Fluent batch command for unsteady | taekyu8 | FLUENT | 0 | January 7, 2013 16:18 |
Regarding about Fluent batch scripts | solefire | FLUENT | 3 | July 5, 2009 10:39 |
regarding about Fluent batch scripts | solefire | FLUENT | 3 | July 5, 2009 05:05 |
Running Job in Batch mode (EFD) | Nick Sessions | FloEFD, FloWorks & FloTHERM | 0 | April 16, 2008 17:44 |