|
[Sponsors] |
January 29, 2017, 02:52 |
how forAll works?
|
#1 |
Senior Member
A. Min
Join Date: Mar 2015
Posts: 308
Rep Power: 12 |
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 |
|
January 29, 2017, 14:10 |
|
#2 |
New Member
Mehdi
Join Date: Jul 2014
Location: Iran
Posts: 10
Rep Power: 12 |
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 ); Code:
scalar totalTime = mesh.time().value(); scalar dt = mesh.time().deltaTValue(); forAll(Tave, i) { Tave[i] = (totalTime * Tave[i] + dt * T[i] ) / (totalTime + dt); } 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. |
|
January 30, 2017, 01:02 |
|
#3 | |
Senior Member
A. Min
Join Date: Mar 2015
Posts: 308
Rep Power: 12 |
Quote:
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 |
||
January 30, 2017, 08:20 |
|
#4 | ||||||
New Member
Mehdi
Join Date: Jul 2014
Location: Iran
Posts: 10
Rep Power: 12 |
Quote:
Quote:
Quote:
you could also initialize Tave without declaring T0, like below: Code:
), mesh, dimensionedScalar("Tave",dimensionSet(0, 0, 0, 1, 0, 0, 0),0) ); Quote:
Quote:
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? Quote:
I hope I didn't mistake in responding. |
|||||||
January 30, 2017, 08:32 |
|
#5 | |
Senior Member
A. Min
Join Date: Mar 2015
Posts: 308
Rep Power: 12 |
Quote:
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 |
||
January 30, 2017, 09:40 |
|
#6 | |
New Member
Mehdi
Join Date: Jul 2014
Location: Iran
Posts: 10
Rep Power: 12 |
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:
How to get a single value from a volScalarField including the dimension regards, Mehdi |
||
January 31, 2017, 01:20 |
|
#7 | |
Senior Member
A. Min
Join Date: Mar 2015
Posts: 308
Rep Power: 12 |
Quote:
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 |
||
January 31, 2017, 04:31 |
|
#8 | |
Senior Member
Kevin van As
Join Date: Sep 2014
Location: TU Delft, The Netherlands
Posts: 252
Rep Power: 21 |
Quote:
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) |
||
January 31, 2017, 08:28 |
|
#9 | |
Senior Member
A. Min
Join Date: Mar 2015
Posts: 308
Rep Power: 12 |
Quote:
Code:
Ohhh! thank you. it was wrong! I had to write: forAll(T,i) This was for testing code tag :o |
||
November 13, 2019, 14:53 |
|
#10 | |
Member
David Andersson
Join Date: Oct 2019
Posts: 46
Rep Power: 7 |
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:
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 |
||
November 13, 2019, 20:25 |
#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 |
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) |
|
Tags |
average field, forall |
|
|
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 |