CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (https://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   Automate execution of command line (https://www.cfd-online.com/Forums/openfoam-programming-development/99524-automate-execution-command-line.html)

Rider April 5, 2012 06:42

Automate execution of command line
 
Hi,

I would like to automate execution of command line with a script (On Ubuntu). Moreover, I want that the second line begin when the first line is finished.

For exemple :

* cd User/Cas1
* blockMesh
* snappyHexMesh
* sudo cp Cas1/0 Cas1/3 // copy of the boundary conditions
* decomposePar
* foamJob -parallel -screen simpleFoam
* reconstructPar
* cd User/Cas2
* blockMesh
* snappyHexMesh
* sudo cp Cas1/0 Cas1/3 // copy of the boundary conditions
* decomposePar
* foamJob -parallel -screen simpleFoam
* reconstructPar
* cd User/Cas2
* etc ...

Thanks all for your help

akidess April 5, 2012 07:01

Quote:

Originally Posted by Rider (Post 353275)
Moreover, I want that the second line begin when the first line is finished.

That's the default behaviour. Just put all the commands from above in a file that starts with the line "#!/bin/bash", and then run "chmod +x [scriptname]" to make the file executable. That's it.

Rider April 5, 2012 07:54

Ok.

The file is a text file (.txt) ?

akidess April 5, 2012 08:05

The file is plain text, but the extension is not important. I usually end them in .sh, but if you prefer you can use .txt, or no extension at all.

Rider April 5, 2012 08:36

Ok, thanks.

Edit : It works :)

Rider April 6, 2012 05:52

Is it possible to use the script to modify the parameters of velocity inlet in the text file ?

akidess April 6, 2012 07:34

Yes. Learn how to use sed or awk to manipulate text files.

gschaider April 9, 2012 18:04

Quote:

Originally Posted by akidess (Post 353427)
Yes. Learn how to use sed or awk to manipulate text files.

That is a possibilty. Although these straight replacements always have to make an assumption about what they have to replace ("look for value 42 and replace it with the inlet velocity"). I personally find a solution similar to http://openfoamwiki.net/index.php/Co...ions_for_walls much easier to maintain (but of course I'm not objective)

But of course sed has the advantage to be installed everywhere

Rider April 10, 2012 08:34

What did you think about gnu m4 ? (
http://www.somogyibence.hu/documents...ckMesh_m4.html)

gschaider April 10, 2012 11:16

Quote:

Originally Posted by Rider (Post 353969)
What did you think about gnu m4 ? (
http://www.somogyibence.hu/documents...ckMesh_m4.html)

Sincerely hate it since I had to configure a sendmail-server. But I'm a man of many prejudices, so don't take my word.

Seriously: I think the main purpose of M4 is generating text files, not analyzing them. So it might be just the opposite of what you're looking for

Rider April 11, 2012 02:17

Ok, thanks for your response.

Rider April 11, 2012 03:51

Have you an example of BlockMesh script ? (Example of your script wrote with PyFoam)

gschaider April 11, 2012 16:02

Quote:

Originally Posted by Rider (Post 354118)
Have you an example of BlockMesh script ? (Example of your script wrote with PyFoam)

Have a look at slide 42ff of http://web.student.chalmers.se/group...amAdvanced.pdf

I'm planing to add a more sophisticated templating-mechanism to later versions of PyFoam

samiam1000 August 6, 2012 06:55

Dear All,

pardon the question, but I think that this does not work.

In fact I have my launch.sh file which is the following:

Code:

'#!/bin/bash'

foamJob -p buoyantPimpleFoam;

reconstructPar;

cd ../1DegNew/;

mapFields -consistent ../1DegStartNew -sourceTime 100;

decomposePar;

buoyantPimpleDyMFoam_MS;

sed -i 's/1/2/' system/controlDict;

buoyantPimpleDyMFoam_MS;

sed -i 's/2/3/' system/controlDict;

buoyantPimpleDyMFoam_MS;

sed -i 's/3/4/' system/controlDict;

buoyantPimpleDyMFoam_MS;

sed -i 's/4/5/' system/controlDict;

buoyantPimpleDyMFoam_MS;

sed -i 's/5/6/' system/controlDict;

The problem is that the reconstructPar starts before the foamJob -p buoyantPimpleFoam is ended.

Any idea about that?

Thanks a lot,
Samuele

Phicau August 6, 2012 07:05

Hi

foamJob sends the solving process to the background and redirects its output to a log file, hence your following commands get to start as soon as the case starts.

samiam1000 August 6, 2012 07:48

Dear Pablo,

thanks for answering.

I know that it is as you say, i.e. that foamJob solves in BG.. What I would like to know is if it is possible to launch all the commands that I give in BG on a server, executing them one after the other.

Thanks a lot,
Samuele

Phicau August 6, 2012 07:54

Hi

it is very easy:

Code:

mpirun -np 8 buoyantPimpleFoam -parallel > log
where you just have to change the number of processors.

Regards

samiam1000 August 6, 2012 08:02

If I give the command you suggested (i.e. mpirun -np 8 buoyantPimpleFoam -parallel > log), I'll launch the program in foreground and when I close the terminal where I am running it, the execution stops.

Let me try to explain what I wanna do: maybe you can help me.

I am performing some simulations on a server. I connect via ssh, from my laptop.

The point is that if I am working in serial, I can give
Code:

buoyantPimpleFoam > log
and then
Code:

ctrl z + bg
and then
Code:

disown
.

With mpirun, this syntax doesn't work, hence I user foamJob.

What should I do, to you?

Thanks a lot,
Samuele

gschaider August 6, 2012 17:55

Quote:

Originally Posted by samiam1000 (Post 375590)
If I give the command you suggested (i.e. mpirun -np 8 buoyantPimpleFoam -parallel > log), I'll launch the program in foreground and when I close the terminal where I am running it, the execution stops.

Let me try to explain what I wanna do: maybe you can help me.

I am performing some simulations on a server. I connect via ssh, from my laptop.

The point is that if I am working in serial, I can give
Code:

buoyantPimpleFoam > log
and then
Code:

ctrl z + bg
and then
Code:

disown
.

With mpirun, this syntax doesn't work, hence I user foamJob.

What should I do, to you?

Thanks a lot,
Samuele

Use the nohup command before mpirun. This makes it ignore the "death" of the shell

samiam1000 August 7, 2012 07:59

If I understood well, I can write a launch.sh file like this:

Code:

'#!/bin/bash'

nohup foamJob -p buoyantPimpleFoam;

reconstructPar;

cd ../1DegNew/;

mapFields -consistent ../1DegStartNew -sourceTime 100;

decomposePar;

buoyantPimpleDyMFoam_MS;

sed -i 's/1/2/' system/controlDict;

buoyantPimpleDyMFoam_MS;

sed -i 's/2/3/' system/controlDict;

buoyantPimpleDyMFoam_MS;

sed -i 's/3/4/' system/controlDict;

buoyantPimpleDyMFoam_MS;

sed -i 's/4/5/' system/controlDict;

buoyantPimpleDyMFoam_MS;

sed -i 's/5/6/' system/controlDict;

Is that correct?

Thanks a lot,
Samuele


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