Search code examples
c++listalgorithmopenfoam

OpenFOAM how to remove some elements from a List?


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


Solution

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