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

How to merge two labellist?

Register Blogs Community New Posts Updated Threads Search

Like Tree5Likes
  • 3 Post By tomislav_maric
  • 1 Post By mathslw
  • 1 Post By tomislav_maric

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   March 30, 2014, 12:14
Default How to merge two labellist?
  #1
New Member
 
Wei Liu
Join Date: Apr 2011
Location: West Lafayette, IN
Posts: 29
Rep Power: 15
mathslw is on a distinguished road
Hello,

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

Thanks!

Wei
mathslw is offline   Reply With Quote

Old   April 2, 2014, 07:58
Default
  #2
New Member
 
Wei Liu
Join Date: Apr 2011
Location: West Lafayette, IN
Posts: 29
Rep Power: 15
mathslw is on a distinguished road
Does anyone have any idea on this, thanks in advance!
mathslw is offline   Reply With Quote

Old   April 2, 2014, 09:27
Default
  #3
Senior Member
 
Tomislav Maric
Join Date: Mar 2009
Location: Darmstadt, Germany
Posts: 284
Blog Entries: 5
Rep Power: 21
tomislav_maric is on a distinguished road
Quote:
Originally Posted by mathslw View Post
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
salehi144, mhdhoseiny and hamedhiv like this.
tomislav_maric is offline   Reply With Quote

Old   April 2, 2014, 10:42
Default
  #4
New Member
 
Wei Liu
Join Date: Apr 2011
Location: West Lafayette, IN
Posts: 29
Rep Power: 15
mathslw is on a distinguished road
Quote:
Originally Posted by tomislav_maric View Post
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
mhdhoseiny likes this.
mathslw is offline   Reply With Quote

Old   April 2, 2014, 10:45
Default
  #5
Senior Member
 
Tomislav Maric
Join Date: Mar 2009
Location: Darmstadt, Germany
Posts: 284
Blog Entries: 5
Rep Power: 21
tomislav_maric is on a distinguished road
Quote:
Originally Posted by mathslw View Post
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.
tomislav_maric is offline   Reply With Quote

Old   May 20, 2014, 20:13
Default
  #6
ooo
Member
 
Join Date: Feb 2012
Posts: 49
Rep Power: 14
ooo is on a distinguished road
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) ?

Last edited by ooo; May 21, 2014 at 05:03.
ooo is offline   Reply With Quote

Old   May 21, 2014, 05:50
Default
  #7
Senior Member
 
Tomislav Maric
Join Date: Mar 2009
Location: Darmstadt, Germany
Posts: 284
Blog Entries: 5
Rep Power: 21
tomislav_maric is on a distinguished road
@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.
__________________
When asking a question, prepare a SSCCE.
tomislav_maric is offline   Reply With Quote

Old   May 21, 2014, 06:04
Default
  #8
ooo
Member
 
Join Date: Feb 2012
Posts: 49
Rep Power: 14
ooo is on a distinguished road
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 is offline   Reply With Quote

Old   May 21, 2014, 06:12
Default
  #9
ooo
Member
 
Join Date: Feb 2012
Posts: 49
Rep Power: 14
ooo is on a distinguished road
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...
ooo is offline   Reply With Quote

Old   May 21, 2014, 06:32
Default
  #10
Senior Member
 
Tomislav Maric
Join Date: Mar 2009
Location: Darmstadt, Germany
Posts: 284
Blog Entries: 5
Rep Power: 21
tomislav_maric is on a distinguished road
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..
ooo likes this.
__________________
When asking a question, prepare a SSCCE.
tomislav_maric is offline   Reply With Quote

Reply


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
[Other] mesh airfoil NACA0012 anand_30 OpenFOAM Meshing & Mesh Conversion 13 March 7, 2022 17:22
[blockMesh] non-orthogonal faces and incorrect orientation? nennbs OpenFOAM Meshing & Mesh Conversion 7 April 17, 2013 05:42
[blockMesh] error message with modeling a cube with a hold at the center hsingtzu OpenFOAM Meshing & Mesh Conversion 2 March 14, 2012 09:56
[blockMesh] BlockMesh FOAM warning gaottino OpenFOAM Meshing & Mesh Conversion 7 July 19, 2010 14:11
[blockMesh] Axisymmetrical mesh Rasmus Gjesing (Gjesing) OpenFOAM Meshing & Mesh Conversion 10 April 2, 2007 14:00


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