|
[Sponsors] |
Create List of List of List of scalars and iterate over it |
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
August 18, 2016, 10:20 |
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 |
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"; Thomas |
|
August 18, 2016, 13:31 |
|
#2 |
Member
Jerry
Join Date: Oct 2013
Location: Salt Lake City, UT, USA
Posts: 52
Rep Power: 13 |
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; } |
|
August 18, 2016, 14:43 |
|
#3 |
Senior Member
Thomas Oliveira
Join Date: Apr 2015
Posts: 114
Rep Power: 12 |
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, 15:38 |
|
#4 |
Member
Jerry
Join Date: Oct 2013
Location: Salt Lake City, UT, USA
Posts: 52
Rep Power: 13 |
In that case, it's easy to replace forAll with the C++ for loop.
|
|
August 19, 2016, 04:11 |
|
#5 | |
Senior Member
Gerhard Holzinger
Join Date: Feb 2012
Location: Austria
Posts: 342
Rep Power: 28 |
Quote:
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, 16:15 |
|
#6 |
Senior Member
Sergei
Join Date: Dec 2009
Posts: 261
Rep Power: 22 |
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; } } Last edited by Zeppo; September 2, 2016 at 12:59. Reason: An interfering colon have been added to the link accidentally |
|
August 31, 2016, 15:43 |
|
#7 |
Senior Member
Thomas Oliveira
Join Date: Apr 2015
Posts: 114
Rep Power: 12 |
Dear Sergei,
Thank you! It was something like that that I was looking for. Best regards, Thomas |
|
Tags |
lists |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Iterate through all scalars of a volScalarField | maybee | OpenFOAM Programming & Development | 0 | February 14, 2014 11:00 |