Hey there 👋
It’s Robin from CFD Engine & as someone who likes building CFD “black boxes”, I’m always on the lookout for ways to make them a bit smarter.
To give them a bit of logic so that they can be used across a wider range conditions, whilst reducing the number of edits needed to crank out that next case.
The fewer dictionaries I have to touch, the fewer mistakes I’ll make 🫣
I was considering my options & tripped over some “old, but new-to-me” OpenFOAM features that I should be using, and so I thought I’d share.
Simple automation
Despite being an automation nut, I keep my OpenFOAM case templates pretty simple.
I store all of my case parameters (flow variables, geometry filenames etc) in short, simple text files & I use #include
to bring them into my dictionaries.
I use the built-in macro expansion to transform those variables into values at runtime.
I use #eval
to do some (in my case, pretty basic) calculations inside my dictionaries & to reduce the number of times I need to copy-paste from a spreadsheet.
And that’s about it, nothing too trick, the same stuff you’ll find all over the tutorials.
There were a few options I’d missed though, here are two really useful ones (plus two that won’t rock your world, but are worth knowing) 😉
if
& ifeq
Did you know that you’ve been able to use if
statements inside your OpenFOAM dictionaries for years? No need to fall back to the shell (or into Python) to use conditional logic.
There are two options: #ifeq
compares two arguments to see if they’re equal & #if
takes one argument & evaluates it as true or false.
The link above gives an example of using one to change the decomposition method based on the application you’re running.
Here’s another that uses #ifeq
to automatically switch time schemes, based on the solver you’re using.
Combine that with an #eval
expression & you can test for almost anything.
There’s a bunch of examples in the IO
tutorials directory to help you figure out their use.
Here are some that use #if
– take a look & then run the following to see how they get evaluated…
foamDictionary $FOAM_TUTORIALS/IO/dictionary/good-if2.dict
I reckon I might do something like this with them…
…i.e. use #ifeq
to change wheel boundary conditions, based on whether the ground is moving or not 🤔
message
& word
While you’re here, you might want to cast an eye over these two little directives.
#message
is for printing something directly into OpenFOAM’s output stream.
And #word
is for concatenating strings to make new words, useful for naming results, fields & other outputs.
You’re probably not going to use these too much, but they’re useful for debugging &/or logging.
For example, why not add a #message
to your #if
statements to log which conditions were executed?
There’s a tutorial file that demonstrates how both #message
& #word
work – check out the examples in here & then run…
foamDictionary $FOAM_TUTORIALS/IO/dictionary/good-word-expand.dict
…to see if you can figure out what’s going on 🤣
Over to you
There you have it, two really useful (& two slightly-less-useful) built-in features that you can use to make your dictionaries a bit smarter, without any of that shell (or Python) business.
There are so many ways to do this kind of automation/templating/case-building in OpenFOAM, so I’m interested to know how you do it.
Do you edit all of your dictionaries manually? Do you use a bit of sed
& awk
magic? Do you use OpenFOAM’s built-in features (macro expansion, expressions etc)? Maybe you have a fleet of Python scripts to set-up your cases? Or perhaps you use a template engine?
I like the idea of the last one, but I also like using OpenFOAM built-ins wherever possible 🤔 How do you do it? – drop me a note.
Until next week, stay safe,