CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Programming & Development

How to go through every element of a field?

Register Blogs Community New Posts Updated Threads Search

Like Tree2Likes
  • 1 Post By akidess
  • 1 Post By Democritus

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   April 15, 2016, 06:32
Default How to go through every element of a field?
  #1
New Member
 
Democritus's Avatar
 
Xiaoqiu HE
Join Date: Mar 2016
Location: Wuhan, China
Posts: 29
Rep Power: 10
Democritus is on a distinguished road
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 is offline   Reply With Quote

Old   April 15, 2016, 07:52
Default One possible way?
  #2
New Member
 
Democritus's Avatar
 
Xiaoqiu HE
Join Date: Mar 2016
Location: Wuhan, China
Posts: 29
Rep Power: 10
Democritus is on a distinguished road
As every field can be seen as UList<Type>, so it comes to me that if method inside the class template can help.
Democritus is offline   Reply With Quote

Old   April 15, 2016, 08:29
Default
  #3
Senior Member
 
akidess's Avatar
 
Anton Kidess
Join Date: May 2009
Location: Germany
Posts: 1,377
Rep Power: 30
akidess will become famous soon enough
You are looking "forAll" (search the forum for this keyword).
Democritus likes this.
__________________
*On twitter @akidTwit
*Spend as much time formulating your questions as you expect people to spend on their answer.
akidess is offline   Reply With Quote

Old   April 16, 2016, 03:40
Default Problem almost solved!
  #4
New Member
 
Democritus's Avatar
 
Xiaoqiu HE
Join Date: Mar 2016
Location: Wuhan, China
Posts: 29
Rep Power: 10
Democritus is on a distinguished road
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:

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 View Post
You are looking "forAll" (search the forum for this keyword).
akidess likes this.
Democritus is offline   Reply With Quote

Reply

Tags
programming, user code


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
funkySetBoundaryFields - Manipulation of existing field jhertel OpenFOAM Pre-Processing 16 May 18, 2020 07:32
''unknown radialModelType type Gidaspow'' PROBLEM WITH THE BED TUTORIAL AndoniBM OpenFOAM Running, Solving & CFD 2 March 25, 2015 19:44
Problems creating a volScalarField with expressionField function-object zfaraday OpenFOAM Running, Solving & CFD 2 January 12, 2015 08:49
New BoundaryCondition: how to modify a single component of a field? look86 OpenFOAM Programming & Development 4 August 28, 2012 17:48
Demagnetization field jan0385 ANSYS 0 April 12, 2012 10:09


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