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 go through every element of a field? (https://www.cfd-online.com/Forums/openfoam-programming-development/169659-how-go-through-every-element-field.html)

Democritus April 15, 2016 05:32

How to go through every element of a field?
 
Hello Dear Foamers~!

In my project i want to define a complex source field. For example, I want solve the equation:
Code:

volScalarField p;
volScalarField sourceField;
solve( fvm::laplacian( p ) == sourceField )

Furthermore, the source filed is a complex space function, for example, such as
Quote:

f(x, y, z) = sin(x) * cos(y) * z
Here is my though about this problem:
1. one field of every cell center's position must be obtained, that can be achieved by:
Code:

volVectorField centerPostions = mesh.C();
2. there must be a method in order to read through every element of the field. such as:
Code:

for(i = first_element_index, i <= last_element_index, i++)
{
    sourceField.element(i) = sin( centerPosition.element(i).x() ) * .....
}

I was blocked in the second step, dear foamers, is there a method can accomplish that?
I am open for every advices!
Thanks very much!

Democritus April 15, 2016 06:52

One possible way?
 
As every field can be seen as UList<Type>, so it comes to me that if method inside the class template can help.

akidess April 15, 2016 07:29

You are looking "forAll" (search the forum for this keyword).

Democritus April 16, 2016 02:40

Problem almost solved!
 
Thank you very much for your response!
After searching and studying the threads of the forum with key word "forAll". I have got the basic understanding of the macro "forAll". What's more, I have developed my own "manual" method in order to scan a field:
Code:

    Foam::vector * i;
    Foam::label j = 0;
    volVectorField position = mesh.C();

    Info << "\n the first method of scanning the field " << endl;
    for ( i = position.begin(); i <= position.end(); i++) {
      Info << "\n the cell " << j << "'s position is " << position[j] << endl;
      j++;
      if (j == 5) {
        break;
      }
    }

    Info<< "\n the second method of scanning the field " << endl;
    j = 0;
    forAll( position, elmtID ){
      Info << "\n the cell " << elmtID << "'s position is " << position[elmtID] << endl;
      j++;
      if (j == 5) {
        break;
      }
    }

and the result is quite well::):D

Quote:

the first method of scanning the field

the cell 0's position is (1.25e-05 0.000125 0.0001)

the cell 1's position is (3.75e-05 0.000125 0.0001)

the cell 2's position is (6.25e-05 0.000125 0.0001)

the cell 3's position is (8.75e-05 0.000125 0.0001)

the cell 4's position is (0.0001125 0.000125 0.0001)

the second method of scanning the field

the cell 0's position is (1.25e-05 0.000125 0.0001)

the cell 1's position is (3.75e-05 0.000125 0.0001)

the cell 2's position is (6.25e-05 0.000125 0.0001)

the cell 3's position is (8.75e-05 0.000125 0.0001)

the cell 4's position is (0.0001125 0.000125 0.0001)
Quote:

Originally Posted by akidess (Post 595160)
You are looking "forAll" (search the forum for this keyword).



All times are GMT -4. The time now is 12:43.