CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (https://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   How to include a geometricField value to an if condition (https://www.cfd-online.com/Forums/openfoam-programming-development/140591-how-include-geometricfield-value-if-condition.html)

kcn August 19, 2014 02:11

How to include a geometricField value to an if condition
 
Hello,

I am developing a solver for temperature transport in a region where some places have porous structure. I need to solve a different transport equation in porous regions and another in ordinary regions. I plan to do this using an if condition as indicated below.

if (porosity == 1)
{ solve Equation A;}
else
{solve Equation B;}

I have defined porosity as a geometricField.

When I try to compile the code it does not recognize the condition " porosity == 1"

Can someone please tell me how to include the value of a geometricField in an if condition in OpenFOAM.

jhoepken August 19, 2014 04:17

Hi,

if you do that, you have to check in each cell and this in turn involves a manual loop over all cells. This is slow as hell. Or even slower.

Why don't you use a blending field, which is 1 where equation 1 should be solved and 0 where equation 2 should be solved. This blending field - basically like the VoF rule of mixture approach - is then multiplied with the particular equations.

Otherwise, as I said, you have to loop over all cells and check locally using

Code:

forAll(porosity, pI)
{
    if(porosity[pI] == 1)
....
}


kcn August 19, 2014 23:10

if condition
 
Dear jhoepken,

Thanks a lot for your help. I'm bit new to openfoam. Can you please explain a little bit about using a blending field for this purpose. Actually, its the first time I heard about it. Are there any built in solvers that uses blending fields so that I can have a look at code?

Thanks
kcn

kcn August 20, 2014 01:23

if condition
 
Dear jhoepken,

I managed to solve the problem using your idea. I used a scalarfield of 1 and 0 as you said and multiplied some terms in my equations with that field. Now it easily calculates using different equations in different zones.

I also tried the for loop method using the code you provided. As you said it took a long time to run even in a coarse mesh.

Thanks a really lot. :)

kcn

jhoepken August 20, 2014 02:57

Hi kcn,

great that you've figured it out. As a general rule of thumb, you should avoid looping over cells and accessing field values wherever possible.


All times are GMT -4. The time now is 11:21.