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/)
-   -   forAll does not loop over boundary cells (https://www.cfd-online.com/Forums/openfoam-programming-development/194607-forall-does-not-loop-over-boundary-cells.html)

gaza October 19, 2017 12:30

forAll does not loop over boundary cells
 
Hi All
I coded something like this
Code:

forAll(alfa, celli)
{
    alfa[celli] = scalar(2);
}

where alfa is volScalarField. Further in my code I divide by alfa
and I get run-time error that it is divided by zero. I found out that boundary cells of alfa are set to zero.

Do I have to do the second loop over boundary cells or there is a much finer solution to loop over internal and boundaryField in one loop?

Jibran October 30, 2017 05:07

Quote:

Originally Posted by gaza (Post 668486)
Hi All
I coded something like this
Code:

forAll(alfa, celli)
{
    alfa[celli] = scalar(2);
}

where alfa is volScalarField. Further in my code I divide by alfa
and I get run-time error that it is divided by zero. I found out that boundary cells of alfa are set to zero.

Do I have to do the second loop over boundary cells or there is a much finer solution to loop over internal and boundaryField in one loop?

Hello,

OpenFOAM treats internal and boundary fields separately. Generally, the boundary values are specified using an appropriate boundary condition in the '0' folder. However, you can still access and modify the boundary values within your solver.

Code:

forAll(mesh.boundary(), patchID)
{
    forAll (mesh.boundary()[patchID],facei)
    {
        alfa.boundaryField()[patchID][facei] = 2.0;
    }
}

Moreover, if you just want to perform operations on the internal field or boundary field, you can access them through alfa.internalField() or alfa.boundaryField() respectively.

gaza October 30, 2017 05:23

Hi Jibran,
Thank you for your reply.
It means that I always has to do two loops, through internal and boundary field.
Ok, thank you for explanations.

ELwardi August 29, 2019 17:09

Yes, you have to treat internalField and boundaryField differently in most cases (or at least use field.correctBoundaryConditions() after the loop on internalField) especially if using some macro (like forAll) which will certainly hide C++ syntax, and most new programmers will become confused.


Of course, there are some containers that aren't compatible with forAll even though applying it on them seems logical.


Recent versions of OpenFOAM will accept volScalarField::operator[](int) for example to access "internalField" (either dimensioned or not, I don't remember :eek:).


More on this in this blog post of mine if you intend to dig a little deeper.


All times are GMT -4. The time now is 23:00.