CFD Online Discussion Forums

CFD Online Discussion Forums (
-   OpenFOAM Post-Processing (
-   -   Calculate variable div zero (

braennstroem January 16, 2008 13:44

Hi, a pretty simple one... I
a pretty simple one... I hope...

I would like to create a new postprocessing variable using:

volScalarField test


This works fine as long as U.component(i) has some kind of value, but if the denominator is zero I get obviously an error. Can anyone give me an advice, how an 'if' question could look like? I tried somehting like: if (U.component(i)==0.0){...}

but I get:

test.C: In function 'int main(int, char**)':
test.C:128: error: expected primary-expression before 'if'


braennstroem January 21, 2008 12:08

Hi, me again... does nobody
me again... does nobody has an idea!?


eugene January 21, 2008 12:41

General practice is to add a s
General practice is to add a small constant to the denominator:

fvc::grad(U)/(smallU + mag(fvc::laplacian(U.component(i))

where smallU is something like:

dimensionedScalar("smallU", dimensionSet(0,0,-1,0,0), SMALL);

grtabor January 21, 2008 13:07

Your "if" statement would not
Your "if" statement would not work because U.component(i) would never be identically equal to 0.0. Eugene is correct; the usual thing is to add a small value to the denominator. However you might also want to think about what the value of the numerator is when the denominator is zero. If the denominator is non-zero, then the expression is diverging at that point; you might want to think whether your modelling is correct at this point.
introducing smallU on the denominator will set this to an artificial (if quite large) number - is this what you want?

On the other hand, if the denominator is also zero, you have 0/0 from the fraction. This is not zero; it is UNDEFINED. The fraction may in fact tend towards a non-zero value; you need to look at 1st and higher derivatives of the numerator/denominator to determine this. Again, using smallU will fix the value to zero, which may not be what you want in practice.


braennstroem January 21, 2008 14:10

Hi Eugene, thanks, good ide
Hi Eugene,

thanks, good idea! It works with a small adjustmentdimensionedScalar smallU ("smallU", dimensionSet(0,1,-1,0,0), SMALL);
. Is there any recommendation where to put the smallU declaration? I put it on top of the .C file!?


braennstroem January 21, 2008 15:14

Hi Gavin, I was a bit to sl
Hi Gavin,

I was a bit to slow submitting the post... Thanks for your help. The SMALL number will actually work for my case, but is there a chance to use some kind of range for the if-condition in this 'template' (this is an template, isn't?)?


eugene January 22, 2008 09:37

I normally put stuff like that
I normally put stuff like that in "createFields.H", but it doesn't really matter.

braennstroem January 24, 2008 15:55


braennstroem January 28, 2008 11:02


All times are GMT -4. The time now is 13:07.