Computational Science Stack Exchange is a question and answer site for scientists using computers to solve scientific problems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When the absolute pressure becomes negative or $U$ exceeds the speed of light, things have pretty obviously gone wrong (be that bad boundary conditions, a too coarse mesh, a too large timestep etc.).

So, is there any way to have a simulation abort with an error message when values exceed definable non-physical ranges?

share|improve this question
I think this kind of question is better asked on the OpenFOAM MessageBoard as it is very program-specific – bgschaid Jul 5 '12 at 18:59
@bgschaid I did, I forgot to put a link here. But honestly, I prefer the SE format since anyone else having a similar question will directly see the accepted answer (yours in this case) instead of having to browse through a potential discussion leading to the solution followed by someone else asking a similar question with an actually different answer etc... – Tobias Kienzler Jul 6 '12 at 6:45
up vote 4 down vote accepted

If you don't want to write C++: In the simpleFunctionObjects there is a functionObject called panicDump. Something like

    pressureOurOfRange {
        type panicDump;
        fieldName p;
        minimum 0;
        maximum 1e7;

in system/controlDict would stop the simulation if the pressure blows up. Together with the expressionField-functionObject from swak4Foam you can also test for non-scalar fields.

Disclaimer: I'm the original developer of simpleFunctionObjects so this answer could be seen as advertisement

share|improve this answer
This is perfect, great job! – Tobias Kienzler Jul 6 '12 at 6:42
bgschaid: welcome to scicomp! – Aron Ahmadia Jul 6 '12 at 11:37

This is basic C++ - just add whatever you are interested in to the code and recompile. For example, copy and paste the following code right before the runTime.write(); call (which is at the end of the main function of every solver) to execute the sanity check at the end of every time step:

// sanity check
if (min(p) < 0)
        << "Negative pressure" << nl
        << exit(FatalError);
share|improve this answer
This answer seems highly unlikely to be specific enough to OpenFOAM to work. Can you suggest where in OpenFOAM he should put this if it is? – Bill Barth Jun 30 '12 at 22:26
Thanks for your comment, I updated the answer. However, where to paste the snippet really depends on where you want to have the sanity check, e.g. at the end of each time step vs. every inner iteration of the pressure-velocity coupling. – akid Jul 1 '12 at 13:36
upvoted, though wouldn't it be a bit more idiomatic to raise a Foam Exception than to just return -1? Warning: fragile doxygen link to error.h in Foam source. – Aron Ahmadia Jul 1 '12 at 14:05
Thanks akid, I think having the check at every timestep (or even only at every writing timestep) is sufficient - anything that aborts the calculation sooner than "late" is welcome... But is there a possibility to achieve this in a way that does not require a modification of the respective solver used? Maybe some #codeStream magic in controlDict? – Tobias Kienzler Jul 2 '12 at 6:07
Aron, I updated the answer. – akid Jul 2 '12 at 7:49

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.