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

optimization of cell list

Register Blogs Community New Posts Updated Threads Search

Like Tree1Likes
  • 1 Post By raumpolizei

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   January 29, 2019, 05:24
Default optimization of cell list
  #1
Senior Member
 
krishna kant
Join Date: Feb 2016
Location: Hyderabad, India
Posts: 133
Rep Power: 10
kk415 is on a distinguished road
Hello Foamers,


I have a list of cells in which I don't want to run my cell loop, how can I subtract this list from the cellList ?
kk415 is offline   Reply With Quote

Old   January 29, 2019, 06:27
Default
  #2
Senior Member
 
krishna kant
Join Date: Feb 2016
Location: Hyderabad, India
Posts: 133
Rep Power: 10
kk415 is on a distinguished road
I have written this loop but I feel there is scope of optimizing this.



int nNew=0;
int check=0;
forAll(newMesh.cells(),cellI)
{
forAll(unrefCells,I)
{
if(cellI == unrefCells[I])
{
check=1;
break;
}
}
if(check == 0)
{
nNew++;
newCells.resize(nNew,cellI);
}
check=0;
}
kk415 is offline   Reply With Quote

Old   January 30, 2019, 10:03
Default
  #3
Member
 
Join Date: Dec 2018
Location: Darmstadt, Germany
Posts: 87
Rep Power: 7
raumpolizei is on a distinguished road
Hey,
here are some ideas that may improve perfomance :
1. Don't use a function call in your forAll macro. Maybe the compiler will optimize it but it is possible that the function gets called multiple times (when range checking is performed).
Code:
const cellList& newMeshCells = newMesh.cells();
forAll(newMeshCells,cellI)
// instead of
// forAll(newMesh.cells(),cellI)
I don't know if forAll works for const fields, otherwise make a for loop out of it.
2. The optimization of your second for loop depends on what data is in newMeshCells and also whether the data is sorted or not. A Foam::List<T> should be fine. You could for example take advantage of a sorted container and perform a binary search on newMeshCells. This will enhance performance especially if the size of your container is big.
Code:
#include<algorithm>
...
forAll(unRefCells,I)
{
  bool found=std::binary_search(newMeshCells.begin(),newMeshCells.end(),I);
  if(found)
  {
    break;
    ...
  }
  ...
}
What you definitely should do is check the algorithm section on cppreference.com (you'll find more info on binary search and a few helpful algorithms).
Good luck with your task.
Cheers
RP
kk415 likes this.
raumpolizei is offline   Reply With Quote

Old   February 7, 2019, 14:44
Default
  #4
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,714
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
Quote:
Originally Posted by kk415 View Post
I have written this loop but I feel there is scope of optimizing this.
int nNew=0;
int check=0;
forAll(newMesh.cells(),cellI)
{
forAll(unrefCells,I)
{
if(cellI == unrefCells[I])
{
check=1;
break;
}
}
if(check == 0)
{
nNew++;
newCells.resize(nNew,cellI);
}
check=0;
}

I can't really follow what you are trying to do, but it is certainly more efficient and easier if you try to work at a higher level. For example (don't really know if this is what you are trying)
Code:
bitSet  unrefCells = ...;  // Some selection coming from somewhere (your input?)

// Those were the cells you want to ignore, so we need the opposite.

labelList newCells;

if (unrefCells.none())
{
    // Nothing ignored - take all cells
    newCells = identity(newMesh.nCells());
}
else if (unrefCells.all())
{
    //  All ignored - we don't have any new cells?
}
else
{
    // Some ignored - get the other ones
    unrefCells.resize(newMesh.nCells());   // extra safety

    unrefCells.flip();
    newCells = unrefCells.toc();  // or sortedToc() - same thing
    unrefCells.flip();   // return to original state
 }
There isn't a simple and efficient way of getting the "unselected" values from the bitSet at the moment.

If it suits your requirement, you might also get some use out of subsetting too.

Code:
bitSet  unrefCells = ...;  // Some selection coming from somewhere (your input?)

scalarField inputValues = ...;

// Subset of the 'off' values:
scalarField values = subset(unrefCell, inputValue, true);
In any case, you were quite right to have a bad feeling about all of the resize() operations within a loop.


Cheers,
/mark
olesen is offline   Reply With Quote

Reply


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
[swak4Foam] funkyDoCalc with OF2.3 massflow NiFl OpenFOAM Community Contributions 14 November 25, 2020 04:30
Purpose and Map of this forum Tobi OpenFOAM Community Contributions 0 September 19, 2017 06:52
How to use "translation" in solidBodyMotionFunction in OpenFOAM rupesh_w OpenFOAM Running, Solving & CFD 5 August 16, 2016 05:27
Help for the small implementation in turbulence model shipman OpenFOAM Programming & Development 25 March 19, 2014 11:08
[swak4Foam] funkySetFields compilation error tayo OpenFOAM Community Contributions 39 December 3, 2012 06:18


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