Hey there,
It’s Robin from CFD Engine & it’s three tiny tips time again – nothing long-winded, just three OpenFOAM tips that you might find useful.
This time out we have two little helpers (especially for those of us that get command-line anxiety) & a handy little functionObject
.
Let’s go…
fieldAverage
Have you ever felt the need to average a flow field over part of a run? Maybe it’s a time-average for a transient simulation, or the mean of a steady-state case that’s not quite so steady.
fieldAverage
to the rescue – a functionObject
that calculates the mean of any of our fields during the solve.
For example, the snippet below would average the U
& p
fields, of a steady-state simulation, starting at 1000 iterations, writing out their mean equivalents (including the vector components) at every subsequent write.

You can’t use it with postProcess
as it does the averaging whilst iterating, it’s not magic. But just add it to the functions
sub-dictionary of controlDict
(as you would any other functionObject
) & plot some lovely averaged fields.
Check out the documentation for more options. They include recording the variation about the mean (prime2mean
), the ability to apply a window to give a rolling mean and a handful of settings to control when it starts/stops and what to do in the event of a restart.
foamListTimes
For many people (particularly new users) their OpenFOAM headaches often stem from a lack of confidence on the command-line. If you’ve been on Windows forever & you’re suddenly faced with doing stuff on the command-line, it can get a little tense.
There are a few OpenFOAM commands that are essentially wrappers around command-line operations, that try to make common operations a little friendlier / easier to use.
An example is foamListTimes
which makes it easier to work with multiple time directories. It works in parallel (add -processor
) and can be used to both list & remove directories.
For example, the following:
foamListTimes -rm -time '7:' -processor -verbose
would delete the time directories (-rm
), from time 7 to the end (-time '7:'
), across all the processors (-processor
) & let you know how it’s progressing (-verbose
).
I’ve started using it more often & found it pretty handy when you have lots of time directories. It’s also easily readable which makes it a great choice for adding to scripts. When someone else (who’s not as handy as you on the command-line) picks up the script, it’s easy to understand what it does.
Whatever the case, be careful when deleting data, there’s no undo button on the command-line 😳
restore0Dir
The first time you run potentialFoam
and realise that it writes to your 0
directory, you might decide to start keeping your boundary conditions safely tucked away in a 0.orig
directory & copy it to 0
just before running instead.
restore0Dir
simply blows away all your existing 0
directories & replaces them with a copy of 0.orig
(including in all the processor directories, if you add the -processor
switch). It also does some magic with #include
files if you’re using the collated
file format.
As with foamListTimes
, it’s nothing you couldn’t do with a couple of Unix commands, but it’s handy & sits nicely in a run script.
It’s not a stand-alone command though, it lives in the RunFunctions
script which I’ve mentioned before & which you need to source using:
. ${WM_PROJECT_DIR:?}/bin/tools/RunFunctions
Add that line near the top of your run scripts to make things like restore0Dir
, runParallel
& runApplication
available for use.
Not to be confused with foamRestoreFields
which does something similar, but different.
Over to you
That’s all folks – just three tiny tips – deleting time directories, restoring zero directories & averaging fields to help things make sense.
Did you know these already? Did I miss anything? Let me know what you reckon so that I can tailor future tips.
Likewise, if you’ve found a gem that you’d like me to steal share, then drop me a note & I’ll add it to the list.
Until then, stay safe,