CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (https://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   How to merge two labellist? (https://www.cfd-online.com/Forums/openfoam-programming-development/132353-how-merge-two-labellist.html)

mathslw March 30, 2014 12:14

How to merge two labellist?
 
Hello,

I have two labellist and they share some labels, so I want to merge them.

Thanks!

Wei

mathslw April 2, 2014 07:58

Does anyone have any idea on this, thanks in advance!

tomislav_maric April 2, 2014 09:27

Quote:

Originally Posted by mathslw (Post 482875)
Hello,

I have two labellist and they share some labels, so I want to merge them.

Thanks!

Wei

You will get more help in any forum, if you show what you have already tried.

Be careful with the following example: STL set and unique deliver proper results, but in case of std::unique, you have to understand the invalidation that happens to the list iterator.

When OpenFOAM algorithm "uniqueSorted" is used from ListOps.H (btw, this is the file you need to investigate to find available List operations in OF), all repeated values are removed from the result.

If you have small lists with repeating indices, consider using sets. I think there is even a native OF set (HashTable<label, label>, or HashSet<label>), but you should make sure that the set is really - a set : has unique elements. Do this by unit testing the class.

Here is the code:

Code:

#include "fvCFD.H"
#include "ListOps.H"
#include <algorithm>
#include <set>

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:

int main(int argc, char *argv[])
{
    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
   
    labelList l1 (5,0);

    l1[0] = 1;
    l1[1] = 2;
    l1[2] = 3;
    l1[3] = 3;
    l1[4] = 5;

    Info << l1 << endl;

    labelList l2 (5,0);

    l2[0] = 4;
    l2[1] = 3;
    l2[2] = 3;
    l2[3] = 9;
    l2[4] = 0;

    Info << l2 << endl;

    // Need this later on for HashSet<label>
    labelList appended;

    appended.append(l1);
    appended.append(l2);

    // Work with this one.
    labelList result = appended;

    Info << "appended : " << result << endl;

    Foam::sort(result);

    Info << "appended sorted : " << result << endl;

    // New result for OF uniqueOrder.
    labelList uniqueResult;

    // OpenFOAM:
    Foam::uniqueOrder(result, uniqueResult);

    // Woops, uniqueOrder seems to remove the repeated 3 completely.
    Info << "OpenFOAM uniqueOrder - note removed 3: " << uniqueResult << endl;

    // STL - using unique algorithm

    // Careful STL unique invalidates iterators : list.end() is invalid,
    // take one resulted from the algorithm call.
    labelList::iterator last = std::unique(result.begin(), result.end());

    Info << "STL corrected for end output: ";
    for (labelList::iterator it = result.begin(); it != last; ++it)
    {
        Info << *it << " ";
    }
    Info << endl;

    Info << "unique STL invalidates the list end: " << result << endl;

    std::set<label> setUnique;

    forAll(appended, I)
    {
        setUnique.insert(appended[I]);
    }

    Info << "STL set output: ";
    forAllConstIter(std::set<label>, setUnique, it)
    {
        Info << *it << " "; 
    }
    Info << endl;

    Info<< "\nEnd\n" << endl;
    return 0;
}


// ************************************************************************* //

Resulting with:

Code:

5(1 2 3 3 5)
5(4 3 3 9 0)
appended : 10(1 2 3 3 5 4 3 3 9 0)
appended sorted : 10(0 1 2 3 3 3 3 4 5 9)
OpenFOAM uniqueOrder - note removed 3: 7(0 1 2 6 7 8 9)
STL corrected for end output: 0 1 2 3 4 5 9
unique STL invalidates the list end: 10(0 1 2 3 4 5 9 4 5 9)
STL set output: 0 1 2 3 4 5 9

End


mathslw April 2, 2014 10:42

Quote:

Originally Posted by tomislav_maric (Post 483451)
You will get more help in any forum, if you show what you have already tried.

Be careful with the following example: STL set and unique deliver proper results, but in case of std::unique, you have to understand the invalidation that happens to the list iterator.

When OpenFOAM algorithm "uniqueSorted" is used from ListOps.H (btw, this is the file you need to investigate to find available List operations in OF), all repeated values are removed from the result.

If you have small lists with repeating indices, consider using sets. I think there is even a native OF set (HashTable<label, label>, or HashSet<label>), but you should make sure that the set is really - a set : has unique elements. Do this by unit testing the class.

Here is the code:

Code:

#include "fvCFD.H"
#include "ListOps.H"
#include <algorithm>
#include <set>

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:

int main(int argc, char *argv[])
{
    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
   
    labelList l1 (5,0);

    l1[0] = 1;
    l1[1] = 2;
    l1[2] = 3;
    l1[3] = 3;
    l1[4] = 5;

    Info << l1 << endl;

    labelList l2 (5,0);

    l2[0] = 4;
    l2[1] = 3;
    l2[2] = 3;
    l2[3] = 9;
    l2[4] = 0;

    Info << l2 << endl;

    // Need this later on for HashSet<label>
    labelList appended;

    appended.append(l1);
    appended.append(l2);

    // Work with this one.
    labelList result = appended;

    Info << "appended : " << result << endl;

    Foam::sort(result);

    Info << "appended sorted : " << result << endl;

    // New result for OF uniqueOrder.
    labelList uniqueResult;

    // OpenFOAM:
    Foam::uniqueOrder(result, uniqueResult);

    // Woops, uniqueOrder seems to remove the repeated 3 completely.
    Info << "OpenFOAM uniqueOrder - note removed 3: " << uniqueResult << endl;

    // STL - using unique algorithm

    // Careful STL unique invalidates iterators : list.end() is invalid,
    // take one resulted from the algorithm call.
    labelList::iterator last = std::unique(result.begin(), result.end());

    Info << "STL corrected for end output: ";
    for (labelList::iterator it = result.begin(); it != last; ++it)
    {
        Info << *it << " ";
    }
    Info << endl;

    Info << "unique STL invalidates the list end: " << result << endl;

    std::set<label> setUnique;

    forAll(appended, I)
    {
        setUnique.insert(appended[I]);
    }

    Info << "STL set output: ";
    forAllConstIter(std::set<label>, setUnique, it)
    {
        Info << *it << " "; 
    }
    Info << endl;

    Info<< "\nEnd\n" << endl;
    return 0;
}


// ************************************************************************* //

Resulting with:

Code:

5(1 2 3 3 5)
5(4 3 3 9 0)
appended : 10(1 2 3 3 5 4 3 3 9 0)
appended sorted : 10(0 1 2 3 3 3 3 4 5 9)
OpenFOAM uniqueOrder - note removed 3: 7(0 1 2 6 7 8 9)
STL corrected for end output: 0 1 2 3 4 5 9
unique STL invalidates the list end: 10(0 1 2 3 4 5 9 4 5 9)
STL set output: 0 1 2 3 4 5 9

End


Hi Tomislav,

Many thanks for the example codes.
I have a question in your codes. You defined a label list by labelList l1 (5,0), what does the 0 in (5,0) mean?

Best,

Wei

tomislav_maric April 2, 2014 10:45

Quote:

Originally Posted by mathslw (Post 483471)
Hi Tomislav,

I have a question in your codes. You defined a label list by labelList l1 (5,0), what does the 0 in (5,0) mean?

Np, glad it helped. If you have problems finding what does a specific line of code do, I suggest you try searching for it in the source code, or in the Doxygen documentation.

Code:

labelList l1(5,0)
This is an expression that initializes an object of type labelList named l1 with arguments (5,0). Initialization is performed in C++ by class constructors: find the labelList class and you will find the answer to your question.

Edit: Hint, my advice is a tricky advice, labelList class is instantiation of the List<T> template, where T = label. So check out List<T> source.

ooo May 20, 2014 20:13

Dear Tomislav,

I have a labelListList, contains some duplicated value like this :
Info<< tmpMyLists[cellI]; -> 7 (44 44 55 66 77 88 99).
to remove the extra '44' when i use this :
Foam::uniqueOrder(tmpMyLists[cellI],MyLists[cellI]);
the output is some value like this ->5 ( 4 3 0 6 ...)//completely different of the expected list.
(using std::unique:
std::unique(tmpMyLists[cellI].begin(),tmpMyLists[cellI].end() )
does not do anything, the output would be again -> 7(44 44 55 66 ... )
Is there anything i would miss?
How can i get this -> 6 (44 55 66 77 88 99) ?

tomislav_maric May 21, 2014 05:50

@ooo

Can you post the code that you programmed? If you post a working example it's easier for me + anyone else to see what you did and find the problem.

ooo May 21, 2014 06:04

sure :

Code:

List<DynamicList<label>> tmpCellCells(mesh.cells().size());
List<DynamicList<label>> cellCells(mesh.cells().size());
forAll(mesh.cells(),cellI)
        {
        labelList layer1 = mesh.cellCells()[cellI];
        tmpCellCells[cellI].append(layer1) ;
        for(int i=0; i<layer1.size(); i++)
        {
        labelList layer2 = mesh.cellCells()[layer1[i]];
        tmpCellCells[cellI].append(layer2);
        }
        }

The above loops fill each tmpCellCells[cellI] with a label list including cellCells of cellI, and also cellCells of (cellCells(cellI)).But there is some duplication there.
So:
Code:

forAll(mesh.cells(),cellI) {
  std::unique(tmpCellCells[cellI].begin(),tmpCellCells[cellI].end());//If i use this, each labelList of tmpCellCells[cellI] remains unchanged without removing the extra duplications
  uniqueOrder(tmpCellCells[cellI], cellCells[cellI]);// If i use this, then printing out the cellCells[cellI] only gives me some constant value like -> 5 (3 5 2 0...) and not the ID of the surrounding cells
  }


ooo May 21, 2014 06:12

and these are some sample outputs :

forAll(mesh.cells(),cellI) {

//before the try to remove duplications:
Info << tmpCellCells[cellI] -> 41 ( 500 800 500 600 300 ....)

//After using uniqueOrder(sorted) :
Info << cellCells[cellI] -> 30 ( 0 2 4 5 ... 40) !

//After using std::unique:
Info << tmpCellCells[cellI] -> 41 ( 500 800 500 600 300 ....)

}

What i want -> xx ( 500 800 600 300 ... ) //removing the extra 500...

tomislav_maric May 21, 2014 06:32

I'll just answer for the Foam algorithms. The STL I've commented in my answer below - please re-read it , you invalidate iterators with std::unique and need to be careful what you do afterwards.

Here is a working small code example:

Code:

    typedef List<DynamicList<label> > listDynamicLabelList;

    listDynamicLabelList tmpCellCells(1);

    tmpCellCells[0].append(500);
    tmpCellCells[0].append(500);
    tmpCellCells[0].append(600);
    tmpCellCells[0].append(300);
    tmpCellCells[0].append(700);

    Info << "tmpCellCells = " << tmpCellCells << endl;

    listDynamicLabelList uniqueCellCells(1);

    uniqueOrder(tmpCellCells[0], uniqueCellCells[0]);

    Info << "uniqueCellCells = " << uniqueCellCells << endl;

    Info << "uniqueValues = ";
    forAll (uniqueCellCells[0], I)
    {
        Info << tmpCellCells[0][uniqueCellCells[0][I]] << " ";
    }
    Info << endl;

With the following output:

Code:

tmpCellCells = 1(5(500 500 600 300 700))
uniqueCellCells = 1(4(3 1 2 4))
uniqueValues = 300 500 600 700

If you check out the ListOps.H comments, you will find this:

Code:

//- Generate (sorted) indices corresponding to unique list values
template<class T>
void uniqueOrder(const UList<T>&, labelList& order);

Obviously, the labels that you get are not some strange values, they are "indices corresponding to unique list values" in your case in the tmpCellCells.

Hope this helps.. :)


All times are GMT -4. The time now is 05:13.