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

how forAll works?

Register Blogs Community New Posts Updated Threads Search

Like Tree13Likes
  • 6 Post By paradox
  • 1 Post By alimea
  • 4 Post By paradox
  • 1 Post By alimea
  • 1 Post By gcengineer

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   January 29, 2017, 02:52
Default how forAll works?
  #1
Senior Member
 
A. Min
Join Date: Mar 2015
Posts: 308
Rep Power: 12
alimea is on a distinguished road
Hi every body

I want to calculate average temperature in a laplacianFoam code (I know we have simpler ways to calculate it, I just want to learn programing).
1- I created a file "Average.H" in the solver folder and called it in solver.C by #include Average.H
2- in Average.H file I want to write a code similar to (this is what I want, but I don't know how it should be written in solver code in openFoam):
for n=1:1:N //for all the time steps

for i=1:1:I //for all the cells of domain

sum=sum+T(n,i); // after solving equation and finding T(n,i), calculate sum

end
Tave(n)=sum(n)/N; // At the end of every time step calculation, give me Tave (I want to plot it along the run process)

end

I know that I should use "forAll", but I don't know it's arguments and how it works! How I can calculate sum? Tave?

Thanks a lot
alimea is offline   Reply With Quote

Old   January 29, 2017, 14:10
Default
  #2
New Member
 
Mehdi
Join Date: Jul 2014
Location: Iran
Posts: 10
Rep Power: 12
paradox is on a distinguished road
Dear alimea,

At the first you need to declare and initialize "Tave" in your createFields.H,

Code:
    dimensionedScalar T0("zero",dimensionSet(0, 0, 0, 1, 0, 0, 0),0);
    volScalarField Tave
    (
        IOobject
        (
            "Tave",
            runTime.timeName(),
            mesh,
            IOobject::NO_READ,
            IOobject::AUTO_WRITE
        ),
        mesh,
        T0
    );
Then in your file "Average.H" you can loop over all cells by forAll:

Code:
scalar totalTime = mesh.time().value();
scalar dt = mesh.time().deltaTValue();
forAll(Tave, i)
{
	Tave[i] = (totalTime * Tave[i] + dt * T[i] ) / (totalTime + dt);
}
I hope this work for you.
This is a simple code and suitable for start averaging at zero time starting, But you cat improve it by adding startTimeAveraging and a few conditions to work fine.
86682164, dats, parthigcar and 3 others like this.
paradox is offline   Reply With Quote

Old   January 30, 2017, 01:02
Default
  #3
Senior Member
 
A. Min
Join Date: Mar 2015
Posts: 308
Rep Power: 12
alimea is on a distinguished road
Quote:
Originally Posted by paradox View Post
Dear alimea,

At the first you need to declare and initialize "Tave" in your createFields.H,

Code:
    dimensionedScalar T0("zero",dimensionSet(0, 0, 0, 1, 0, 0, 0),0);
    volScalarField Tave
    (
        IOobject
        (
            "Tave",
            runTime.timeName(),
            mesh,
            IOobject::NO_READ,
            IOobject::AUTO_WRITE
        ),
        mesh,
        T0
    );
Then in your file "Average.H" you can loop over all cells by forAll:

Code:
scalar totalTime = mesh.time().value();
scalar dt = mesh.time().deltaTValue();
forAll(Tave, i)
{
	Tave[i] = (totalTime * Tave[i] + dt * T[i] ) / (totalTime + dt);
}
I hope this work for you.
This is a simple code and suitable for start averaging at zero time starting, But you cat improve it by adding startTimeAveraging and a few conditions to work fine.



Dear Mehdi
I'm really thankfull for this helpful information.

I have some other questions about your code. I hope not to make you tired:
1- what is the "zero" at
dimensionedScalar T0("zero",dimensionSet(0, 0, 0, 1, 0, 0, 0),0);

2- can I define Tave (as a volScalarField) in Average.H? or this is a rule that we have to define it in createFields.h?

3- why do we write "T0" at
),
mesh,
T0
);

4- when we define an IOobject in form of "NO_READ", it means it isn't read from initial and boundary condition?

5- I have a general problem with these type of notation in openFoam:
mesh.time().value()
mesh.time().deltaTValue()
what are these? what's the meaning of them? what do "mesh", deltaTValue() and ".value()" do?
when do we use ".value()", ".deltaTValue()"?
could you please tell me the general meaning of them to learn this notation for ever?

6- I saw in some codes "counter" insted of "i". is it arbitrary?

7- I didn't get tour sentence "This is a simple code and suitable for start averaging at zero time starting".

Again I really appreciate your help
lpz456 likes this.
alimea is offline   Reply With Quote

Old   January 30, 2017, 08:20
Default
  #4
New Member
 
Mehdi
Join Date: Jul 2014
Location: Iran
Posts: 10
Rep Power: 12
paradox is on a distinguished road
Quote:
1- what is the "zero" at
dimensionedScalar T0("zero",dimensionSet(0, 0, 0, 1, 0, 0, 0),0);
I think it's just the name of you variable and it's arbitrary.

Quote:
2- can I define Tave (as a volScalarField) in Average.H? or this is a rule that we have to define it in createFields.h?
Tave should be create before and out of the loop. Average.H is in the loop.

Quote:
3- why do we write "T0" at
),
mesh,
T0
);
T0 is the initialize value for Tave and is equal to 0.
you could also initialize Tave without declaring T0, like below:
Code:
),
mesh,
dimensionedScalar("Tave",dimensionSet(0, 0, 0, 1, 0, 0, 0),0)
);
Quote:
4- when we define an IOobject in form of "NO_READ", it means it isn't read from initial and boundary condition?
Yes, that's right.

Quote:
5- I have a general problem with these type of notation in openFoam:
mesh.time().value()
mesh.time().deltaTValue()
what are these? what's the meaning of them? what do "mesh", deltaTValue() and ".value()" do?
when do we use ".value()", ".deltaTValue()"?
could you please tell me the general meaning of them to learn this notation for ever?
mesh.time().value() return the time of your simulation.
mesh.time().deltaTValue() return return the time step of simulation.
You need to know more about object oriented programming.

Code:
6- I saw in some codes "counter" insted of "i". is it arbitrary?
Yes, it is arbitrary.

Quote:
7- I didn't get tour sentence "This is a simple code and suitable for start averaging at zero time starting".
I said this algorithm work fine if you start simulation at zero, and is not suitable for averaging when you stop the run and start again from your last time.

I hope I didn't mistake in responding.
86682164, dats, choist31 and 1 others like this.
paradox is offline   Reply With Quote

Old   January 30, 2017, 08:32
Default
  #5
Senior Member
 
A. Min
Join Date: Mar 2015
Posts: 308
Rep Power: 12
alimea is on a distinguished road
Quote:
Originally Posted by paradox View Post
I think it's just the name of you variable and it's arbitrary.


Tave should be create before and out of the loop. Average.H is in the loop.


T0 is the initialize value for Tave and is equal to 0.
you could also initialize Tave without declaring T0, like below:
Code:
),
mesh,
dimensionedScalar("Tave",dimensionSet(0, 0, 0, 1, 0, 0, 0),0)
);

Yes, that's right.


mesh.time().value() return the time of your simulation.
mesh.time().deltaTValue() return return the time step of simulation.
You need to know more about object oriented programming.

Code:
6- I saw in some codes "counter" insted of "i". is it arbitrary?
Yes, it is arbitrary.


I said this algorithm work fine if you start simulation at zero, and is not suitable for averaging when you stop the run and start again from your last time.

I hope I didn't mistake in responding.
Thanks for your complete answers.
How or where can I know about object oriented programming (I just want to know about that type of notation)?

If I want to write:
a=a+T[i]
(that a is a scalar variable) openFoam gives me the error that you can't sum a scalar variable with a tensor! How can I sum a scalar and a component of a volScalarFild (like temperature)?

Regards
choist31 likes this.
alimea is offline   Reply With Quote

Old   January 30, 2017, 09:40
Default
  #6
New Member
 
Mehdi
Join Date: Jul 2014
Location: Iran
Posts: 10
Rep Power: 12
paradox is on a distinguished road
If you want to be a programmer in openFOAM, I think you need to know more about programming in C++ and OOP. Internet is full of lecture about them and chalmers course, specially is good for starting.

Quote:
If I want to write:
a=a+T[i]
(that a is a scalar variable) openFoam gives me the error that you can't sum a scalar variable with a tensor! How can I sum a scalar and a component of a volScalarFild (like temperature)?
This thread will be helpful for you.
How to get a single value from a volScalarField including the dimension

regards,
Mehdi
paradox is offline   Reply With Quote

Old   January 31, 2017, 01:20
Default
  #7
Senior Member
 
A. Min
Join Date: Mar 2015
Posts: 308
Rep Power: 12
alimea is on a distinguished road
Quote:
Originally Posted by paradox View Post
If you want to be a programmer in openFOAM, I think you need to know more about programming in C++ and OOP. Internet is full of lecture about them and chalmers course, specially is good for starting.


This thread will be helpful for you.
How to get a single value from a volScalarField including the dimension

regards,
Mehdi


Thanks dear Mehdi
I had a mistake in my last post! I want to access to one of the members of a volScalarField like temperature and do some algebraic operation:

dimensionedScalar A("zero",dimensionSet(0, 0, 0, 1, 0, 0, 0),0);
dimensionedScalar B("zero",dimensionSet(0, 0, 0, 1, 0, 0, 0),5);
dimensionedScalar C("zero",dimensionSet(0, 0, 0, 1, 0, 0, 0),0);

forAll(A,i)
{
A=2*T(i)+B; // T(i) is one one of the members of temperature Field
}
C=A*6;

is it correct that I defined A as a scalar?
The compiler gave me the error:

error: ‘Foam::dimensionedScalar’ has no member named ‘size’
for (Foam::label i=0; i<(list).size(); i++)
^
Average.H:45:2: note: in expansion of macro ‘forAll’
forAll(aa, i)
^

Regards
alimea is offline   Reply With Quote

Old   January 31, 2017, 04:31
Default
  #8
Senior Member
 
floquation's Avatar
 
Kevin van As
Join Date: Sep 2014
Location: TU Delft, The Netherlands
Posts: 252
Rep Power: 21
floquation will become famous soon enough
Quote:
Originally Posted by alimea View Post

forAll(A,i)

{
A=2*T(i)+B; // T(i) is one one of the members of temperature Field
}
C=A*6;

is it correct that I defined A as a scalar?
First of, always use [CODE]-tags to show code.

Now for your question: if you wish to iterate over the content of a scalar, then your code does exactly what you want: "forAll(A,i) = for every element of (scalar) A". However, since every scalar comprises only one number, the loop doesn't actually loop. In fact, it gives you your error.
What you, presumably, wish to do is iterate over the elements of T?
Code:
forAll(T,i)
But then I do not understand what the loop does, as A is then simply overwritten in every iteration.
floquation is offline   Reply With Quote

Old   January 31, 2017, 08:28
Red face
  #9
Senior Member
 
A. Min
Join Date: Mar 2015
Posts: 308
Rep Power: 12
alimea is on a distinguished road
Quote:
Originally Posted by floquation View Post
First of, always use [CODE]-tags to show code.

Now for your question: if you wish to iterate over the content of a scalar, then your code does exactly what you want: "forAll(A,i) = for every element of (scalar) A". However, since every scalar comprises only one number, the loop doesn't actually loop. In fact, it gives you your error.
What you, presumably, wish to do is iterate over the elements of T?
Code:
forAll(T,i)
But then I do not understand what the loop does, as A is then simply overwritten in every iteration.
Code:
Ohhh! thank you. it was wrong! I had to write: forAll(T,i)

This was for testing code tag :o
alimea is offline   Reply With Quote

Old   November 13, 2019, 14:53
Default
  #10
Member
 
David Andersson
Join Date: Oct 2019
Posts: 46
Rep Power: 7
sippanspojk is on a distinguished road
I want to do something similar as what Alimea did but I would like to loop over the pressure values of each face of a patch instead of the temperature. I am also quite new to C++ and to programming in OpenFOAM so please bare with me if my questions are trivial.

I have defined myFunctionObj.C and myFunctionObj.H as I understand from
Quote:
At the first you need to declare and initialize "Tave" in your createFields.H,

Code:
    dimensionedScalar T0("zero",dimensionSet(0, 0, 0, 1, 0, 0, 0),0);
    volScalarField Tave
    (
        IOobject
        (
            "Tave",
            runTime.timeName(),
            mesh,
            IOobject::NO_READ,
            IOobject::AUTO_WRITE
        ),
        mesh,
        T0
    );
I should define my volScalarField (adapted to my variables and correct units) in my myFunctionObj.H file, correct?

I do have some questions about this though and correct me if I'm wrong:
- Firstly, am I defining a scalarField named "Tave" with units K that I can reach and work with in my .C file?

- What is happening inside the brackets after
Code:
IOobjects 
( 
    "Tave" 
    ... 
),
?

- Do I need to #include any libraries or namespaces for this to work? I created a blank functionObject using foamNewFunctionObject so I have minimum libraries included.


I would be very thankful for any help on this.

Cheers,
David
sippanspojk is offline   Reply With Quote

Old   November 13, 2019, 20:25
Default #define forAll(list i) in stdFoam.H
  #11
Member
 
Glenn Carlson, PE, PhD (ret)
Join Date: Oct 2012
Location: US
Posts: 49
Rep Power: 14
gcengineer is on a distinguished road
FWIW. In OFv1906,

Code:
//- Loop across all elements in \a list
// \par Usage
// \code
// forAll(anyList, i)
// {
//      statements;
// }
// \endcode
// \sa forAllReverse
#define forAll(list, i) \
     for (Foam::label i=0; i<(list).size(); ++i)
Other variations of forAll -- forAllIters, forAllConstIters, forAllReverseIters, forAllConstReverseIters, forAllReverse, forAllIter, and forAllConstIter -- are also defined in stdFoam.H.
Jun_93 likes this.
gcengineer is offline   Reply With Quote

Reply

Tags
average field, forall


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
[OpenFOAM.com] Multiple Installation Issue: Parallel Processing No Longer Works dancfd OpenFOAM Installation 11 November 20, 2018 17:08
Basic question about forAll loop and volVectorField tayo OpenFOAM 2 January 10, 2014 02:56
[GAMBIT] Gambit works on Windows, but not in Linux victorz ANSYS Meshing & Geometry 8 April 14, 2013 21:40
basic question with 'ForAll' loop Pascal_doran OpenFOAM Post-Processing 10 December 14, 2012 18:39
Parallel runs with sonicDyMFoam crashes (works fine with sonicFoam) jnilsson OpenFOAM Running, Solving & CFD 0 March 9, 2012 07:45


All times are GMT -4. The time now is 18:01.