|
[Sponsors] |
October 20, 2015, 13:56 |
Merge two PtrLists to one
|
#1 |
Member
Alexander Nekris
Join Date: Feb 2015
Location: France
Posts: 32
Rep Power: 11 |
Hello all,
I wonder if it is possible in OpenFOAM to merge two PtrLists to one single list. I have two pointer lists and each PtrList points to a composition of specie concentration fields: PtrList<volScalarField>& Y1 = composition1.Y(); PtrList<volScalarField>& Y2 = composition2.Y(); I would like to combine Y1 and Y2 to a single PtrList e.g. Y. Does anybody know how to do it? Thanks in adwance! Regards, Alex |
|
October 20, 2015, 16:45 |
|
#2 |
Senior Member
|
Hi,
Maybe I did not get your question right but what is wrong with just creating new empty list and then appending to this new list elements of old lists in cycle? Here is an illustration of concept: Code:
#include "autoPtr.H" #include "PtrList.H" #include "Field.H" #include "scalar.H" using namespace Foam; int main(int argc, char *argv[]) { typedef Field<scalar> scalarField; PtrList<scalarField> l1, l2; PtrList<scalarField> l3; for(int i = 0; i < 10; i++) { l1.append(autoPtr<scalarField>(new scalarField(3, scalar(i)))); l2.append(autoPtr<scalarField>(new scalarField(3, scalar(100 + i)))); } forAll(l1, i) { l3.append(tmp<scalarField>(l1[i])); } forAll(l2, i) { l3.append(tmp<scalarField>(l2[i])); } forAll(l3, i) { Info<< l3[i] << endl; } return 0; } |
|
October 21, 2015, 06:16 |
|
#3 |
Member
Alexander Nekris
Join Date: Feb 2015
Location: France
Posts: 32
Rep Power: 11 |
Thank you Alexey,
it works perfectly. This is exactly, what I was looking for. Regards, Alex |
|
October 28, 2015, 06:46 |
|
#4 |
Member
Alexander Nekris
Join Date: Feb 2015
Location: France
Posts: 32
Rep Power: 11 |
Hello Alexey,
I have an additional question concerning merging two PtrLists to one. Here is what I did: basicMultiComponentMixture& composition1 = thermo1.composition(); PtrList<volScalarField>& Y1 = composition1.Y(); basicMultiComponentMixture& composition2 = thermo2.composition(); PtrList<volScalarField>& Y2 = composition2.Y(); PtrList<volScalarField> Y; forAll(Y1, i) { Y.append(volScalarField(Y1[i])); } forAll(Y2, i) { Y.append(volScalarField(Y2[i])); } I perfectly get my Y-List and Y[i]-Fields correspond to Y1[i] and Y2[i]-Fields. Afterwards I solve (try to solve) these scalar fields in a species conservation equation. I have a suspicion, that if the content of Y[i]-Fields changes, there is no feedback to the Y1- and Y2-Lists. So basically, Y1[i]- and Y2[i]-Fields dont change. This is a one-way-street from Y1 and Y2 to Y. Is it true? If yes, is there any possibility to define a two-way coppeling between Y1 and 2 AND Y? Thank you in advance! Regards Alex |
|
October 28, 2015, 17:49 |
|
#5 |
Senior Member
|
Well, both codes (my example and yours) create merged list of copies.
Guess, it is my lack of OpenFOAM's knowledge but here is an example that in fact creates merged list of pointers to original pointers: Code:
#include "autoPtr.H" #include "PtrList.H" #include "Field.H" #include "scalar.H" using namespace Foam; int main(int argc, char *argv[]) { char* mem = new char[sizeof(PtrList<scalarField>)]; void *p = mem; typedef Field<scalar> scalarField; PtrList<scalarField> l1, l2; PtrList<scalarField> *l3 = new (p) PtrList<scalarField>(); PtrList<scalarField>& l4 = *l3; for(int i = 0; i < 3; i++) { l1.append(new scalarField(3, scalar(i))); l2.append(new scalarField(3, scalar(100 + i))); } forAll(l1, i) { l4.append(const_cast<scalarField*>(l1(i))); } forAll(l2, i) { l4.append(const_cast<scalarField*>(l2(i))); } l4[0][0] = 200.0; l4[4][1] = 400.0; Info<< "l4" << endl; forAll(l4, i) { Info<< " " << l4[i] << endl; } Info<< "l1" << endl; forAll(l1, i) { Info<< " " << l1[i] << endl; } Info<< "l2" << endl; forAll(l2, i) { Info<< " " << l2[i] << endl; } delete []mem; return 0; } Code:
l4 3(200 0 0) 3{1} 3{2} 3{100} 3(101 400 101) 3{102} l1 3(200 0 0) 3{1} 3{2} l2 3{100} 3(101 400 101) 3{102} - I use const_cast to remove cost from const scalarField* returned by PtrList's () operator - This operation lead to creation of two pointers pointing to the same object, so upon destruction of l3 pointers in l1 and l2 becomes invalid, this in turn leads to double free error during destruction of l1 and l2. So our aim is to cancel l3 destructor to prevent double free error. Since objects stored in l1 and l2 are destructed we can hope there will be no memory leaks (and according to OS X Allocations tool there is really no memory leaks in the posted piece of code). And to prevent l3 destructor from being executed I was not able to come up with anything but placement new. Reference l4 was create for convenience. Maybe there is more correct and elegant solution to your problem. |
|
October 29, 2015, 11:56 |
|
#6 |
Member
Alexander Nekris
Join Date: Feb 2015
Location: France
Posts: 32
Rep Power: 11 |
Thank you Alexey,
it seems to work now in both directions. Again, thank you very much. Regards Alex |
|
Tags |
ptrlist |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
merge two connectors | v_herw | Pointwise & Gridgen | 1 | August 28, 2014 10:14 |
[ICEM] Two boxes connected by a cylinder, blocks merge where I don't want them to merge. | Polarbear | ANSYS Meshing & Geometry | 4 | April 30, 2014 16:21 |
[ICEM] Problem when I try to merge hexa & prism | pipolaki | ANSYS Meshing & Geometry | 7 | December 4, 2012 09:44 |
[ICEM] Merge mesh - non manifold vertices | BrolY | ANSYS Meshing & Geometry | 4 | October 30, 2010 23:07 |
about merge mesh | lian | Main CFD Forum | 3 | February 29, 2008 11:47 |