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

 Register Blogs Members List Search Today's Posts Mark Forums Read

 August 18, 2016, 09:20 Create List of List of List of scalars and iterate over it #1 Member   Thomas Oliveira Join Date: Apr 2015 Posts: 92 Rep Power: 4 Sponsored Links 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 > > var(Nz,List >(Ny,List(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

 August 18, 2016, 12:31 #2 Member   Jerry Join Date: Oct 2013 Location: Salt Lake City, UT, USA Posts: 52 Rep Power: 5 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 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<

 August 18, 2016, 13:43 #3 Member   Thomas Oliveira Join Date: Apr 2015 Posts: 92 Rep Power: 4 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

 August 18, 2016, 14:38 #4 Member   Jerry Join Date: Oct 2013 Location: Salt Lake City, UT, USA Posts: 52 Rep Power: 5 In that case, it's easy to replace forAll with the C++ for loop.

August 19, 2016, 03:11
#5
Senior Member

Gerhard Holzinger
Join Date: Feb 2012
Location: Austria
Posts: 207
Rep Power: 19
Quote:
 Originally Posted by Jerryfan 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++)```

 August 19, 2016, 15:15 #6 Senior Member     Sergei Join Date: Dec 2009 Posts: 190 Rep Power: 10 http://cpptruths.blogspot.ru/2011/10...ys-in-c11.html: Code: ```#include #include template struct MultiDimArray { typedef typename MultiDimArray::type Nested; typedef std::array type; }; template struct MultiDimArray { typedef std::array type; }; int main() { MultiDimArray::type floats {{ {{ {111,112}, {121,122} }}, {{ {211,212}, {221,222} }} }}; for(size_t i=0; i

 August 31, 2016, 14:43 #7 Member   Thomas Oliveira Join Date: Apr 2015 Posts: 92 Rep Power: 4 Dear Sergei, Thank you! It was something like that that I was looking for. Best regards, Thomas

 Tags lists

 Thread Tools Display Modes Linear Mode

 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 OffTrackbacks are On Pingbacks are On Refbacks are On Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post maybee OpenFOAM Programming & Development 0 February 14, 2014 11:00