CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Post-Processing (https://www.cfd-online.com/Forums/openfoam-post-processing/)
-   -   Execution time (https://www.cfd-online.com/Forums/openfoam-post-processing/215483-execution-time.html)

Ximena March 6, 2019 14:03

Execution time
 
Hello,

I'm new at openFoam. I could run a simulation and now I'm wondering how I could get the total time that the simulation took.
I open the generated logRun file and it says:

Build : 5.0
Exec : simpleFoam -parallel
Date : Mar 06 2019
Time : 14:37:33
Host : "stro045.vub.ac.be"
PID : 16906
I/O : uncollated
Case : /1234/OpenFOAM/ximena5.0/run/casedirectory3
nProcs : 16
Slaves : 15

Then i suppose that the time that the simpleFoam command took is Time:14:37:33. For me it does not look like real time (hr.min.sec.)
Is that the real time? if not, how can i convert that to real time (sec)?
and how could I get the complete time that my simulation took, I mean generating the blockMesh, snappyHexmexh, decompePar etc? (by the way, I run all the simulation separatelly not just running an Allrun script.

Regards
Ximena ;)

Krapf March 7, 2019 11:23

Hi Ximena,

14:37:33 was the time of the day when simpleFoam started. Some commands print the execution time in its log file, you could add them.

Kind regards,
Krapf

lebc March 11, 2019 09:01

Hi Ximena,

I wrote this Python script to calculate the elapsed time between start and end of any process, you just have to run it before executing the commands and after executing the commands. I should be placed in the same folder as you are running the simulations.

Code:

import time
import os

## Check if the file elapsed_time exists.
## If it doesn't, it is the beginning of execution of the code;
## If it do, it is the end of execution, the elapsed time wil be calculated.

check_file = os.path.exists("elapsed_time")

if check_file == False:
    ## time in seconds since The Epoch, January 1st, 1970
    current_time = time.time()

    get_time_file = open('elapsed_time', 'w')
    get_time_file.write(str(current_time))
    get_time_file.close()

else:
    initial_time_file = open('elapsed_time', 'r')
    lines = initial_time_file.readlines() ## read file
    initial_time = lines[0].split()
    initial_time = float(initial_time[0])
    initial_time_file.close()

    current_time = time.time()
   
    get_time_file = open('elapsed_time', 'w')
    get_time_file.write(str(current_time - initial_time))
    get_time_file.close()

Best Regards,
Luis

mzzmrt March 11, 2019 14:59

You can also use the "time" tool in the terminal with any execution for blockMesh, simpleFoam etc. such as;



time simpleFoam


at the end of run the time data will be declared...

Ximena April 4, 2019 10:24

Dear lebc,
Thanks a lot for your reply, I tried to implement your Python script by by adding that in the case directory and executing the command importtime and other variations, unfortunately I did not get any results. What is the right way to make it works?

Regards
Ximena
:)
____________


Dear mzzmrt,
thanks for your reply, it was really useful.
I got the time at the end of the execution as is shown hereunder, by executing the command:
mpirun -np 8 time snappyHexMesh -overwrite -parallel >logMsh &
I just have two more questions, the time the execution time took is 18.31 minutes, didn't it?, it is possible to record thats information into the logFile? because that info was available just on my terminal window.

End
Finalising parallel run
428.14user 626.69system 18:31.63elapsed 94%CPU (0avgtext+0avgdata 352268maxresident)k
0inputs+167672outputs (6major+922154minor)pagefaults 0swaps
419.01user 653.49system 18:31.64elapsed 96%CPU (0avgtext+0avgdata 344700maxresident)k
0inputs+165800outputs (2major+991625minor)pagefaults 0swaps
428.49user 637.03system 18:31.62elapsed 95%CPU (0avgtext+0avgdata 332980maxresident)k
0inputs+167240outputs (6major+938355minor)pagefaults 0swaps.......

Regards
Ximena :)

lebc April 5, 2019 16:31

You have to save the code I sent you in a .py file and run it before and after your simulation.

Let say that you have an Allrun script that runs everything. This Allrun file would look like something like this:

Code:

python elapsed_time.py

"everything you want to run in OpenFOAM"

python elapsed_time.py

After that, you would have this Allrun file, the .py file and you OpenFOAM case in the same folder, and you just have to open the terminal and put ./Allrun. Make sure you have the permission to run this file.

If you have troubles running it, send me the commands you have to run and I can help you with this Allrun file, I use it all the time!

Best Regards,
Luis

Ximena April 11, 2019 09:11

Dear lebc,

I really apreciate your help. I was trying to create Allrun file, but it looks more complicated in parallel (since my case is a little too much for my computer, I run it remotely and in parallel) and those are the commands I run:

1. blockMesh
2. decomposePar
3. mpirun -np 32 time snappyHexMesh -overwrite -parallel >logMesh &
4. tail -f logMesh (to visualise when it is finished)
5. reconstructParMesh -constant
6. cp -r 0.boundaryConditions/* 0/
7. rm -r processor*
8. decomposePar -latestTime
9. mpirun -np 32 time simpleFoam -parallel >logRun &
10. tail -f logRun (to visualise when it is finished)
11. reconstructPar -latestTime
12. checkMesh >logCheck & (to visualise the number of cells)

by the way, I create the python "elapsed_time.py" file just by adding the extension .py to the textedit file and coping that to the remote computer, is that right?
Thanks again for you help :)

lebc April 12, 2019 18:00

Hi Ximena,

So your Allrun file using the commands you sent should look like this:

Code:

#!/bin/sh
cd ${0%/*} || exit 1    # Run from this directory

python elapsed_time.py

blockMesh
decomposePar
mpirun -np 32 time snappyHexMesh -overwrite -parallel >logMesh &
tail -f logMesh
reconstructParMesh -constant
cp -r 0.boundaryConditions/* 0/
rm -r processor*
decomposePar -latestTime
mpirun -np 32 time simpleFoam -parallel >logRun &
tail -f logRun
reconstructPar -latestTime
checkMesh >logCheck &

python elapsed_time.py

What you can try, to have a cleaner file, is:


Code:

#!/bin/sh
cd ${0%/*} || exit 1    # Run from this directory

python elapsed_time.py

# Source tutorial run functions
. $WM_PROJECT_DIR/bin/tools/RunFunctions

runApplication blockMesh
runApplication decomposePar
mpirun -np 32 snappyHexMesh -parallel -overwrite < /dev/null > log.snappyHexMesh 2>&1

find . -type f -iname "*level*" -exec rm {} \;
ls -d processor* | xargs -I {} 0.boundaryConditions/* ./{}/0 $1

mpirun -np 32 simpleFoam -parallel < /dev/null > log.mysimpleFoam 2>&1

runApplication reconstructParMesh -constant
runApplication reconstructPar -latestTime

runApplication checkMesh


python elapsed_time.py

You don't need to reconstruct your case after generating the mesh and decompose it again, if you only need to copy the boundary conditions to the processors folder you can use a command for it.

I haven't tested the script, so maybe you will need to adjust one or two commands...

All the logs should be saved in files, so you can check them whenever you want.

Best Regards,
Luis

boffin5 August 13, 2023 11:14

How can I tweak your python file to show result in minutes?
 
Thanks lebc,

I got your python file to work, for showing the elapsed time for running a simulation. However, the file it creates shows the ET in seconds, and for a
long run time, this can be a large number. In that event, it would be more
meaningful for me to see the result in terms of minutes.
Can you show how your python file could be edited to do this?

Tobermory August 15, 2023 11:21

... just divide through by 60!

Code:

get_time_file.write(str((current_time - initial_time)/60))
If you wanted to be fancy, use some conditional statements to check if the time is greater than 100, and if so, divide by 60 to convert to mins. Be sure to add units, so you can keep track of what the number means.


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