CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Running, Solving & CFD (https://www.cfd-online.com/Forums/openfoam-solving/)
-   -   PtrList problems (https://www.cfd-online.com/Forums/openfoam-solving/84765-ptrlist-problems.html)

kuczmas February 8, 2011 08:24

PtrList problems
 
Dear forumers,

Firstly I will shortly describe my problem. I would like to set up an Array of Arrays (2d array), which is of size: N_boundFaces x N_boundFaces. Such an array will correspond to the coefficients of heat exchange between a given boundary-cell-face and the rest of boundary-cell-faces. Having read some hints in this forum, I came up with an idea of constructing a PtrList. But at the beginning I have problems with setting up a single 1d-PtrList:

The code is following...:
(...)
const int N_boundFaces = mesh.boundaryMesh().size() * mesh.boundaryMesh()[0].size(); //N_boundFaces = N_patches * N_faces_in_patch
PtrList< int > D(N_boundFaces);
Info << "size of PtrList D = " << D.size() << endl ;

for( int i = 0; i != N_boundFaces ; i++ )
{
D.set(i, 2); // set all the values in the PtrList to 2
}
(...)


and the compiling errors are:
(...)
In file included from /opt/openfoam171/src/OpenFOAM/lnInclude/tmp.H:142,
from /opt/openfoam171/src/OpenFOAM/lnInclude/PtrListI.H:29,
from /opt/openfoam171/src/OpenFOAM/lnInclude/PtrList.H:311,
from /opt/openfoam171/src/OpenFOAM/lnInclude/List.C:30,
from /opt/openfoam171/src/OpenFOAM/lnInclude/List.H:262,
from /opt/openfoam171/src/OpenFOAM/lnInclude/labelList.H:36,
from /opt/openfoam171/src/OpenFOAM/lnInclude/Pstream.H:43,
from /opt/openfoam171/src/OpenFOAM/lnInclude/OPstream.H:35,
from /opt/openfoam171/src/OpenFOAM/lnInclude/parRun.H:35,
from /opt/openfoam171/src/finiteVolume/lnInclude/fvCFD.H:4,
from laplacianFoamModifiedRandomNumberV03.C:32:
/opt/openfoam171/src/OpenFOAM/lnInclude/tmpI.H: In destructor ‘Foam::tmp<T>::~tmp() [with T = int]’:
rayTracing.H:43: instantiated from here
/opt/openfoam171/src/OpenFOAM/lnInclude/tmpI.H:76: error: request for member ‘okToDelete’ in ‘*((Foam::tmp<int>*)this)->Foam::tmp<int>::ptr_’, which is of non-class type ‘int’
/opt/openfoam171/src/OpenFOAM/lnInclude/tmpI.H:83: error: request for member ‘operator--’ in ‘*((Foam::tmp<int>*)this)->Foam::tmp<int>::ptr_’, which is of non-class type ‘int’
/opt/openfoam171/src/OpenFOAM/lnInclude/tmpI.H: In member function ‘T* Foam::tmp<T>::ptr() const [with T = int]’:
/opt/openfoam171/src/OpenFOAM/lnInclude/PtrListI.H:90: instantiated from ‘Foam::autoPtr<T> Foam::PtrList<T>::set(Foam::label, const Foam::tmp<T>&) [with T = int]’
rayTracing.H:43: instantiated from here
/opt/openfoam171/src/OpenFOAM/lnInclude/tmpI.H:127: error: request for member ‘resetRefCount’ in ‘* ptr’, which is of non-class type ‘int’
make: *** [Make/linuxGccDPOpt/laplacianFoamModifiedRandomNumberV03.o] Błąd 1


So my questions are the following:
1. what is the best way to loop over the elements in PtrList?
a) use simple for loop with int counter: for( int i = 0; != N_boundFaces; i++ )
b) use iterator: for ( PtrList<int>::iterator i = D.begin(); i != D.end(); ++i)
c) use: forAll( D, i )
2. how successfuly replace a given entity in the PtrList? use D.set(i, 200)??
3. how print out the value of a given entity, e.g. D[i]? Info << D[i] ;??

I know that some answers to my questions were previously mentioned, but I couldn't manage to solve my problem. Every help would be greatly appreciated.

Thanks!

kuczmas February 10, 2011 03:14

I'm still tackling the problem, but hopefully I see an end. I will try to post some conclusions from my little experience :-).

Still I really appreciate any help.

hjasak February 11, 2011 04:56

PtrList is a container for pointers to objects and there is a bunch of templates in it. Why don't you just use a labelList?

If you want an example of PtrList, try a PtrList<fvPatch> or similar.

Hrv

kuczmas February 16, 2011 06:41

Hello Hrvoje,

Thanks for your suggestions! I have to lean more about them. I'm fresh user of OpenFoam, so for now I managed to solve the problem just by creating a new scalar array. I decided not to use ptrList because the size of the array is constant during the program execution. Once again thanks.

kuczmas May 18, 2011 09:44

solution to the problems
 
Dear forumers,

Finally I succeded in finding out how to do basic operations with ptrList< type >. Below I posted some lines of my code. Hope it will be useful, because previously I searched the forum and didn't find basic info on this subject.

Code:

    /* Defining pointer list of scalars */
    PtrList< scalar > U_ ;
    scalar A = 3.1415;
    scalar *Aptr ;

    Aptr = &A ;

    U_.setSize( 10 ) ;

    for (label i = 0; i < 10; i++)
    {
        U_.set(i, Aptr) ;            // the ptrList can be filled in passing the pointer Aptr to scalar A
        Info << " if is set U_[i] = " << U_.set(i);    //checking if the list element U_[i] is already set --> if not the value of U_.set(i) == 0 ;
        Info << " U_[i] = " << U_[i] << endl ;        // prints out the values of prtList
    }

Bye! :-)

olesen May 19, 2011 02:17

Quote:

Originally Posted by kuczmas (Post 308148)
Dear forumers,

Finally I succeded in finding out how to do basic operations with ptrList< type >. Below I posted some lines of my code. Hope it will be useful, because previously I searched the forum and didn't find basic info on this subject.

Code:

    /* Defining pointer list of scalars */
    PtrList< scalar > U_ ;
    scalar A = 3.1415;
    scalar *Aptr ;
 
    Aptr = &A ;
 
    U_.setSize( 10 ) ;
 
    for (label i = 0; i < 10; i++)
    {
        U_.set(i, Aptr) ;            // the ptrList can be filled in passing the pointer Aptr to scalar A
        Info << " if is set U_[i] = " << U_.set(i);    //checking if the list element U_[i] is already set --> if not the value of U_.set(i) == 0 ;
        Info << " U_[i] = " << U_[i] << endl ;        // prints out the values of prtList
    }

Bye! :-)


Are you really certain that your code works correctly with PtrList?
Have you checked with valgrind that everything actually works properly when the U_ variable is destroyed? I have my doubts. Take a look at the destructor for PtrList and you'll see that it is doing a 'delete'. Your code will not mix well with this at all.

kuczmas May 26, 2011 04:50

Dear olesen, many thanks for pointing out the problem. As I said before I am a beginner in openFoam, so I really appreciate all replies. I tried checking the code with valgrind and it shouts for some errors indeed. You have a valgrind response below:

Code:

==4315== Invalid free() / delete / delete[]
==4315==    at 0x4C26DCF: operator delete(void*) (vg_replace_malloc.c:387)
==4315==    by 0x5445C23: Foam::PtrList<double>::~PtrList() (PtrList.C:133)
==4315==    by 0x5442D79: Foam::pkModel::pk::checkDij_reciprocity()
==4315==    by 0x44D031: main (hEqn.H:19)
==4315==  Address 0x7feffc260 is on thread 1's stack
==4315==

...
...


==4315==
==4315== HEAP SUMMARY:
==4315==    in use at exit: 300 bytes in 11 blocks
==4315==  total heap usage: 387,720 allocs, 387,719 frees, 41,168,150 bytes allocated
==4315==
==4315== LEAK SUMMARY:
==4315==    definitely lost: 60 bytes in 1 blocks
==4315==    indirectly lost: 240 bytes in 10 blocks
==4315==      possibly lost: 0 bytes in 0 blocks
==4315==    still reachable: 0 bytes in 0 blocks
==4315==        suppressed: 0 bytes in 0 blocks
==4315== Rerun with --leak-check=full to see details of leaked memory
==4315==
==4315== For counts of detected and suppressed errors, rerun with: -v
==4315== ERROR SUMMARY: 10 errors from 1 contexts (suppressed: 4 from 4)

In that case could you show how to properly define and use PtrList<> in order not to have memory problems?

kuczmas.

olesen May 26, 2011 05:25

Quote:

Originally Posted by kuczmas (Post 309302)
...
In that case could you show how to properly define and use PtrList<> in order not to have memory problems?

The fundamental question you should ask yourself: "why use PtrList if List will do?"

Code:


// Defining list of scalars
    List<scalar> U_ ;
    scalar A = 3.1415;

    U_.setSize(10) ;
    forAll(U_, i)
    {
        U_[i] = A;
        Info << " U_[i] = " << U_[i] << endl;
    }


Code:


// Defining list of scalars
    PtrList<scalar> U_ ;
    scalar A = 3.1415;

    U_.setSize(10) ;
    forAll(U_, i)
    {
        U_.set(i, new scalar(A));
        Info << " U_[i] = " << U_[i] << endl;
    }

If you don't actually need pointers for something special, you should generally stick with a "normal" List instead of using a PtrList.

kuczmas May 26, 2011 07:07

Dear Mark,

That was the piece of the code I actually was interested in and it works perfectly without any errors. Thanks a lot :) !


All times are GMT -4. The time now is 10:25.