How to handle errors in a function object and stop the simulation?
I am creating a custom function object, in which I want to have some checks (e.g. in the read() function for the user input) and exit the simulation with an error if something goes wrong.
I managed to output an error, using the FatalErrorIn() macro: Code:
FatalErrorIn("myFunctionObject::myFunction()") << "This is my error message." << nl << exit(FatalError); I tried to use the stopAt() method (before the exit()) to stop the simulation manually: Code:
runTime_.stopAt(Time::saNoWriteNow); How could I stop the simulation from the functionObject, but with a failure exit code? |
How to stop the simulation immediately?
Apart from the fact that OpenFOAM catches the functionObject's errors as "Warnings" (i.e. exiting the functionObject but not the solver itself), I have another relevant question.
The stopAt() method can take one of the following values as an argument: Code:
saEndTime stop when Time reaches the prescribed endTime I understand that the saNoWriteNow is the "most urgent" one. However, the solver still performs another iteration and executes the execute() method of the functionObject once more before it finishes. Is there a better way to stop the simulation immediately? I hope that this is an interesting question. |
What if you use abort() instead of exit()?
|
This works, but is it a good practice? Is it used anywhere else in OpenFOAM or third-party function objects?
I guess you mean the C++ abort(), because I don't find such an OpenFOAM function. |
When you write
Code:
FatalErrorIn("myFunctionObject::myFunction()") << "This is my error message." << nl << exit(FatalError); Code:
274 // Global error definitions https://cpp.openfoam.org/v3/a07673_source.html#l00168 |
You are right, there is an OpenFOAM abort(). In the beginning I didn't see that method...
However, it still catches the fatal error as a warning and continues the simulation: Code:
--> FOAM Warning : |
A "hacky" way to gracefully stop the simulation would also be to set the end time to the current time (or even to zero):
Code:
const_cast<Time&>(runTime_).setEndTime(runTime_.value()); This will not call any "end()" method of this or other function objects. Is there any reason to not do it this way? |
I found something interesting:
the behavior I described happens only in the read() method, as the Foam::functionObjectList::read() catches any exceptions. Using the macro FatalErrorInFunction works normally in the functionObject's execute() and the rest of the methods. In my case, the code I had in my read() can also go in the start() [correction: start() doesn't exist for a functionObject itself], so my specific problem is solved. Thank you for the help! :-D edit: Actually it can't, since Foam::functionObjectList::start() simply calls Foam::functionObjectList::read()... :-/ |
All times are GMT -4. The time now is 17:08. |