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?

  • $\begingroup$ I think this kind of question is better asked on the OpenFOAM MessageBoard as it is very program-specific $\endgroup$
    – bgschaid
    Jul 5, 2012 at 18:59
  • $\begingroup$ @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... $\endgroup$ Jul 6, 2012 at 6:45

2 Answers 2


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

  • $\begingroup$ This is perfect, great job! $\endgroup$ Jul 6, 2012 at 6:42
  • $\begingroup$ bgschaid: welcome to scicomp! $\endgroup$ Jul 6, 2012 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);
  • $\begingroup$ 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? $\endgroup$
    – Bill Barth
    Jun 30, 2012 at 22:26
  • $\begingroup$ 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. $\endgroup$
    – akid
    Jul 1, 2012 at 13:36
  • $\begingroup$ 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. $\endgroup$ Jul 1, 2012 at 14:05
  • $\begingroup$ 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? $\endgroup$ Jul 2, 2012 at 6:07
  • $\begingroup$ Aron, I updated the answer. $\endgroup$
    – akid
    Jul 2, 2012 at 7:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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