|
[Sponsors] |
March 30, 2014, 12:14 |
How to merge two labellist?
|
#1 |
New Member
Wei Liu
Join Date: Apr 2011
Location: West Lafayette, IN
Posts: 29
Rep Power: 15 |
Hello,
I have two labellist and they share some labels, so I want to merge them. Thanks! Wei |
|
April 2, 2014, 07:58 |
|
#2 |
New Member
Wei Liu
Join Date: Apr 2011
Location: West Lafayette, IN
Posts: 29
Rep Power: 15 |
Does anyone have any idea on this, thanks in advance!
|
|
April 2, 2014, 09:27 |
|
#3 | |
Senior Member
Tomislav Maric
Join Date: Mar 2009
Location: Darmstadt, Germany
Posts: 284
Blog Entries: 5
Rep Power: 21 |
Quote:
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; } // ************************************************************************* // 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 |
||
April 2, 2014, 10:42 |
|
#4 | |
New Member
Wei Liu
Join Date: Apr 2011
Location: West Lafayette, IN
Posts: 29
Rep Power: 15 |
Quote:
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 |
||
April 2, 2014, 10:45 |
|
#5 | |
Senior Member
Tomislav Maric
Join Date: Mar 2009
Location: Darmstadt, Germany
Posts: 284
Blog Entries: 5
Rep Power: 21 |
Quote:
Code:
labelList l1(5,0) 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. |
||
May 20, 2014, 20:13 |
|
#6 |
Member
Join Date: Feb 2012
Posts: 49
Rep Power: 14 |
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. |
|
May 21, 2014, 05:50 |
|
#7 |
Senior Member
Tomislav Maric
Join Date: Mar 2009
Location: Darmstadt, Germany
Posts: 284
Blog Entries: 5
Rep Power: 21 |
@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. |
|
May 21, 2014, 06:04 |
|
#8 |
Member
Join Date: Feb 2012
Posts: 49
Rep Power: 14 |
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); } } 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 } |
|
May 21, 2014, 06:12 |
|
#9 |
Member
Join Date: Feb 2012
Posts: 49
Rep Power: 14 |
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... |
|
May 21, 2014, 06:32 |
|
#10 |
Senior Member
Tomislav Maric
Join Date: Mar 2009
Location: Darmstadt, Germany
Posts: 284
Blog Entries: 5
Rep Power: 21 |
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; Code:
tmpCellCells = 1(5(500 500 600 300 700)) uniqueCellCells = 1(4(3 1 2 4)) uniqueValues = 300 500 600 700 Code:
//- Generate (sorted) indices corresponding to unique list values template<class T> void uniqueOrder(const UList<T>&, labelList& order); Hope this helps..
__________________
When asking a question, prepare a SSCCE. |
|
|
|
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 |