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

Create List of List of List of scalars and iterate over it

Register Blogs Community New Posts Updated Threads Search

Like Tree2Likes
  • 1 Post By t.oliveira
  • 1 Post By Zeppo

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   August 18, 2016, 09:20
Default Create List of List of List of scalars and iterate over it
  #1
Senior Member
 
Thomas Oliveira
Join Date: Apr 2015
Posts: 114
Rep Power: 12
t.oliveira is on a distinguished road
Dear all,

I was able to write the following code that:
- creates a 3D list of scalars of size Nx x Ny x Nz
- initializes all the scalars to 0
- iterates over the 3D list varying first the coordinate of the inner list, then of the intermediate list, and then of the outer list.

This code looks ugly and unreadable. Is it possible to write it in a better way?

Code:
label Nx = 10;
label Ny = 12;
label Nz = 15;

List<List<List<scalar> > > var(Nz,List<List<scalar> >(Ny,List<scalar>(Nx,0)));

forAll(var, k)
    forAll(var[k], j)
        forAll(var[k][j], i)
            cout << k << " " << j << " " << i << " " << var[k][j][i] << "\n";
Best regards,
Thomas
joshwilliams likes this.
t.oliveira is offline   Reply With Quote

Old   August 18, 2016, 12:31
Default
  #2
Member
 
Jerry
Join Date: Oct 2013
Location: Salt Lake City, UT, USA
Posts: 52
Rep Power: 12
Jerryfan is on a distinguished road
Hi Thomas,


I think it's better to stick to a 1D array or list of size Nx x Ny x Nz as in O.F., most of the field variables are defined in terms of a 1D array or list.
You can do it maybe this way to simplify your problem.

DimensionedField<scalar, volMesh> var
(
IOobject
(
"var",
runTime.timeName(),
mesh,
IOobject::MUST_READ,
IOobject::AUTO_WRITE,
false
),
mesh,
dimless,
false
);

Then, it would become easier to set all the values as well as printing the member values. Here I am assuming that your mesh size has to be Nx x Ny x Nz, with Nx cells in the x axis, y cells in the y axis and z in the z axis, respectively.

forAll (var, cellID)
{
var[cellID] = cellID;
Info<<var[cellID]<<endl;
}
Jerryfan is offline   Reply With Quote

Old   August 18, 2016, 13:43
Default
  #3
Senior Member
 
Thomas Oliveira
Join Date: Apr 2015
Posts: 114
Rep Power: 12
t.oliveira is on a distinguished road
Dear Jerry,

Thank you for your time!

I need to write the values to a file in that order to interface OpenFOAM with another program, so I think I need a direct mapping between the position of the cell in a cartesian grid and the position of var in the list. That is why the OpenFOAMic syntax forAll (var, cellID) may not be appropriate in this case.

Best wishes,
Thomas
t.oliveira is offline   Reply With Quote

Old   August 18, 2016, 14:38
Default
  #4
Member
 
Jerry
Join Date: Oct 2013
Location: Salt Lake City, UT, USA
Posts: 52
Rep Power: 12
Jerryfan is on a distinguished road
In that case, it's easy to replace forAll with the C++ for loop.
Jerryfan is offline   Reply With Quote

Old   August 19, 2016, 03:11
Default
  #5
Senior Member
 
Gerhard Holzinger
Join Date: Feb 2012
Location: Austria
Posts: 339
Rep Power: 28
GerhardHolzinger will become famous soon enoughGerhardHolzinger will become famous soon enough
Quote:
Originally Posted by Jerryfan View Post
In that case, it's easy to replace forAll with the C++ for loop.
The forAll statement is a macro defining a C++ for loop. forAll is just a handy shorthand.
So, there is no need to change the code from using forAll to C++'s for list syntax.

This is the definition of forAll in UList.H:

Code:
#define forAll(list, i) \
    for (Foam::label i=0; i<(list).size(); i++)
GerhardHolzinger is offline   Reply With Quote

Old   August 19, 2016, 15:15
Default
  #6
Senior Member
 
Zeppo's Avatar
 
Sergei
Join Date: Dec 2009
Posts: 261
Rep Power: 21
Zeppo will become famous soon enough
http://cpptruths.blogspot.ru/2011/10...ys-in-c11.html:
Code:
#include <iostream>
#include <array>

template <class T, size_t I, size_t... J>
struct MultiDimArray 
{
  typedef typename MultiDimArray<T, J...>::type Nested;
  typedef std::array<Nested, I> type;
};
 
template <class T, size_t I>
struct MultiDimArray<T, I> 
{
  typedef std::array<T, I> type;
};

int main()
{
    MultiDimArray<float, 2, 2, 2>::type floats {{ {{ {111,112}, {121,122} }}, 
                                                  {{ {211,212}, {221,222} }} }};
    for(size_t i=0; i<floats.size(); ++i)
    {
        for(size_t j=0; j<floats[i].size(); ++j)
        {
            for(size_t k=0; k<floats[k].size(); ++k)    
                std::cout << floats[i][j][k] << "\t";
            std::cout << endl;
        }
        std::cout << endl;
    }
}
t.oliveira likes this.

Last edited by Zeppo; September 2, 2016 at 11:59. Reason: An interfering colon have been added to the link accidentally
Zeppo is offline   Reply With Quote

Old   August 31, 2016, 14:43
Default
  #7
Senior Member
 
Thomas Oliveira
Join Date: Apr 2015
Posts: 114
Rep Power: 12
t.oliveira is on a distinguished road
Dear Sergei,

Thank you! It was something like that that I was looking for.

Best regards,
Thomas
t.oliveira is offline   Reply With Quote

Reply

Tags
lists


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
Iterate through all scalars of a volScalarField maybee OpenFOAM Programming & Development 0 February 14, 2014 10:00


All times are GMT -4. The time now is 16:34.