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

Merge two PtrLists to one

Register Blogs Community New Posts Updated Threads Search

Like Tree6Likes
  • 1 Post By Neka
  • 1 Post By alexeym
  • 1 Post By Neka
  • 1 Post By Neka
  • 1 Post By alexeym
  • 1 Post By Neka

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   October 20, 2015, 13:56
Default Merge two PtrLists to one
  #1
Member
 
Alexander Nekris
Join Date: Feb 2015
Location: France
Posts: 32
Rep Power: 11
Neka is on a distinguished road
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
Zhiheng Wang likes this.
Neka is offline   Reply With Quote

Old   October 20, 2015, 16:45
Default
  #2
Senior Member
 
Alexey Matveichev
Join Date: Aug 2011
Location: Nancy, France
Posts: 1,938
Rep Power: 39
alexeym has a spectacular aura aboutalexeym has a spectacular aura about
Send a message via Skype™ to alexeym
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;
}
Also it is possible to allocate space for new list during construction and then use set method to avoid reallocations in append method.
Zhiheng Wang likes this.
alexeym is offline   Reply With Quote

Old   October 21, 2015, 06:16
Default
  #3
Member
 
Alexander Nekris
Join Date: Feb 2015
Location: France
Posts: 32
Rep Power: 11
Neka is on a distinguished road
Thank you Alexey,
it works perfectly.

This is exactly, what I was looking for.

Regards,
Alex
Zhiheng Wang likes this.
Neka is offline   Reply With Quote

Old   October 28, 2015, 06:46
Default
  #4
Member
 
Alexander Nekris
Join Date: Feb 2015
Location: France
Posts: 32
Rep Power: 11
Neka is on a distinguished road
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
Zhiheng Wang likes this.
Neka is offline   Reply With Quote

Old   October 28, 2015, 17:49
Default
  #5
Senior Member
 
Alexey Matveichev
Join Date: Aug 2011
Location: Nancy, France
Posts: 1,938
Rep Power: 39
alexeym has a spectacular aura aboutalexeym has a spectacular aura about
Send a message via Skype™ to alexeym
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;
}
Here is output

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}
The differences are:

- 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.
Zhiheng Wang likes this.
alexeym is offline   Reply With Quote

Old   October 29, 2015, 11:56
Default
  #6
Member
 
Alexander Nekris
Join Date: Feb 2015
Location: France
Posts: 32
Rep Power: 11
Neka is on a distinguished road
Thank you Alexey,
it seems to work now in both directions.
Again, thank you very much.

Regards
Alex
Zhiheng Wang likes this.
Neka is offline   Reply With Quote

Reply

Tags
ptrlist


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
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


All times are GMT -4. The time now is 22:12.