CFD Online Discussion Forums

CFD Online Discussion Forums (
-   OpenFOAM (
-   -   Instruction if (

bluemind April 27, 2012 06:48

Instruction if

I'm a student and new user of OpenFOAM, I am currently trying to modify the solver twoLiquidMixingFoam by adding the energy equation. Since I haven't use C++ so far, I'm having some trouble when I use the instruction if, for example in my case I just want to implement these simple lines :

if ( alpha1 == 0 ) // l : 34


I have the following error :

energyEqn.H:34: error: could not convert ‘alpha1.Foam::GeometricField<Type, PatchField, GeoMesh>::operator== [with Type = double, PatchField = Foam::fvPatchField, GeoMesh = Foam::volMesh](((const Foam::dimensioned<double>&)(& Foam::dimensioned<double>(((const double&)((const double*)(&0.0)))))))’ to ‘bool’

Since alpha1 is an object, I don't think this is the appropriated way to do this. Is it possible to have some help ?


bluemind April 27, 2012 08:07

I found the command :

forAll( alpha1,celli){
if ( alpha1[celli] < 1.e-5 )


Bernhard April 29, 2012 04:25

Hi Nabil,

Welcome to CFD-online!

The method you found should work fine, but it really depends on what you want to obtain. If you can explain a bit more on what you want exactly, there may be a nicer way.

marupio April 29, 2012 13:28

Seconded. What you've got now is an indexed for loop over all elements in the mesh. These types of loops should be avoided whenever possible as they can be 10x slower than OpenFOAM's built-in field operations, which use optimised pointer loops.

block June 14, 2016 14:14

Problem with if Loop in interFoam
2 Attachment(s)
Hi guys i am trying to implement a if loop in one of the libraries of interFoam where a variable called "kappaf" is depend on the alpha1f value, where i want to impose a condition where kappaf is alpha1f * DI for alpha1f >0.5 or zero if alphaf < 0.5.

I have written the loop

Attachment 48267

I am getting this error

Attachment 48268

I am new to openfoam and C++ coding so not familiar with how to write this.

Kind Regards


All times are GMT -4. The time now is 14:19.