1

In OpenFOAM, I can access the list of times of my simulation, as follows:

const auto& tlist = mesh.time().times(); //or runTime.times();

Just think of this in the context of a custom function object, where you want to access the list of times.

When I print that list:

Foam::Info << tlist << Foam::endl;

and then run the function object via postProcess command, I get:

9
(
0   constant
0   0
0.001   0.001
0.002   0.002
0.003   0.003
0.004   0.004
0.005   0.005
0.006   0.006
0.007   0.007
)

End

I want to get rid of the first two elements from that list, i.e (0 constant) and (0 0). But I can't find any method to do that, all what I found is that if the object is a HashTable, then there is an erase method that removes the element by its key.

Any ideas how can I remove the first two elements from my list of times tlist? or at least how can I convert that list to another data structure that will allow me to do that?

Thank you

Edit:

Here is a link to the definition of List in the context of OpenFOAM: https://cpp.openfoam.org/v9/classFoam_1_1List.html

2 Answers2

2

Disclaimer, I never used OpenFOAM. Looks like List has iterators. https://cpp.openfoam.org/v9/classFoam_1_1UList.html. So you could try something like this (assuming iterators work like I'm used to from other libraries):

  const auto& tlist = mesh.time().times(); //or runTime.times();

  // assuming operator+ available on iterator. this will copy data
  auto sublist = List<instant>(tlist.begin()+2,tlist.end());

  Foam::Info << sublist << Foam::endl;

  // or you could try to loop over the elements manually, this won't copy data
  for (auto it = tlist.begin()+2; it != tlist.end(); ++it )
  {
      std::cout << *it << "\n";
  }
  std::cout << std::endl;
Pepijn Kramer
  • 9,356
  • 2
  • 8
  • 19
0

Rather than using iterators it makes more sense to use an OpenFOAM-specific idiom. Obtain the first list and you can use the SubList class to obtain a view into the original list. From there, it is your choice if you want to work with the SubList, or make a deep copy into another list.

EDIT (first part was scribbled from my phone):

Here is a simple example of how to work with a SubList:

instantList allTimes(runTime.times());

Info<< "I have " << allTimes.size() << " times:" << nl
    << allTimes << nl;

if (allTimes.size() > 2)
{
    SubList<instant> someTimes(allTimes, allTimes.size()-2, 2);

    Info<< "Subset of times: " << someTimes << nl;
}

The parameter order of the SubList constructor may be a bit confusing if you are thinking of string::substr(), but essentially:

  • the original list
  • the subset size
  • the offset (missing is 0)

Note that this SubList is an extremely lightweight view into the existing list, which you can use to modify subsections of a list etc. If you have a branching situation, where you may want to handle things uniformly, you can simply drop in a ternary. For example,

const SubList<instant>& myTimes =
(
    (allTimes.size() > 2)
  ? SubList<instant>(allTimes, allTimes.size()-2, 2)
  : SubList<instant>(allTimes)
);

You can also work with a labelRange to select things too. For example,

labelRange myslice(0, allTimes.size());
if (allTimes.size() > 2)
{
    myslice.reset(2, allTimes.size()-2);
}

// Now
Info<< "Selected times: "
    << SubList<instant>(allTimes, myslice) << nl;

Since SubList is also a type of a list, you can use a range-for on it etc.

Note that in the next release (Dec-2021), you will also be able to obtain a view directly. For example,

#if (OPENFOAM >= 2112)

Info<< "Selected times: " << allTimes.slice(2) << nl;

#endif
Mark.O
  • 94
  • 6
  • Could you please elaborate on that? it is not clear to me. – Iyach tharwa nambarek Oct 29 '21 at 20:55
  • Please see the updated comment with code examples of using SubList. Also exercise extreme caution if you try to construct an OpenFOAM List from two iterators. In several versions this has been removed since it has caused innumerable problems with 32/64 ambiguity. Eg, `labelList foo(4, 8)` is this a list of 4 elements, uniformly initialized with a value of 8, or is it an iterator pair. If OpenFOAM has been compiled with 64-bit labels, you will resolve to the iterator pair instead of the uniform initialization!! – Mark.O Nov 08 '21 at 10:37