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

dynamic list (variable size)

Register Blogs Community New Posts Updated Threads Search

Like Tree1Likes
  • 1 Post By alquimista

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   July 18, 2013, 12:31
Default dynamic list (variable size)
  #1
Member
 
Join Date: Apr 2010
Posts: 61
Rep Power: 16
alquimista is on a distinguished road
Hello,

I have a list of scalars with unknown size at declaration time. During the simulation I want to remove given elements from the list at different times.

I know that you can define a DynamicList (DynamicList<scalar> test;) and then add some values (test.append(value)), clear the list (test.clear()) or maybe delete the last introduced value. The thing is if I need to delete some elements from the list It occurs to me to make a temporal copy with the new size or something like that.

So my question is... what is the best strategy to declare a list of scalars which is expected to delete some given elements (and then varying the size of the list)? How can I do it effectively?

Thank you in advanced.

Regards.

edit: the post is similar was treated before here and there's an interesting reply:

Quote:
Originally Posted by deepsterblue View Post
Georg,

DynamicList only allows appends and removal to/from the end of the list (think of a stack-like operation). If you're looking to remove from the middle, you'll have to resort to a copy to another list (followed by a List::transfer).

This might boil down to an appropriate choice of container - if you're looking to frequently remove arbitrary items from a list, a linked-list or hash table may be a better option.
Someone can explain it in detail?
alquimista is offline   Reply With Quote

Old   July 18, 2013, 17:37
Default
  #2
Senior Member
 
Laurence R. McGlashan
Join Date: Mar 2009
Posts: 370
Rep Power: 23
l_r_mcglashan will become famous soon enough
Depends. What is the size of your list, how often will you be deleting elements and how many will you remove?
__________________
Laurence R. McGlashan :: Website
l_r_mcglashan is offline   Reply With Quote

Old   July 18, 2013, 17:59
Default
  #3
Member
 
Join Date: Apr 2010
Posts: 61
Rep Power: 16
alquimista is on a distinguished road
Hello Laurance.

The list has a size in a range 100-1000 and I need to remove 1 element each time I check a condition. Every timestep It can happen for around 10-70% of the size of the list. (Eg: size 300, 40 elements to remove in a timestep).

Thank you for your hints.
alquimista is offline   Reply With Quote

Old   July 19, 2013, 04:53
Default
  #4
Senior Member
 
Laurence R. McGlashan
Join Date: Mar 2009
Posts: 370
Rep Power: 23
l_r_mcglashan will become famous soon enough
That's quite small. I'd either use a HashTable if you have to remove elements individually, or if you can remove them as a block then do this:

Code:
PackedBoolList elemToRemove(l.size());

forAll(l, i)
{
    if (checkCondition)
    {
        elemToRemove[i] = true;
    }
}

inplaceSubset(elemToRemove, l);
__________________
Laurence R. McGlashan :: Website
l_r_mcglashan is offline   Reply With Quote

Old   July 19, 2013, 07:25
Default
  #5
Member
 
Join Date: Apr 2010
Posts: 61
Rep Power: 16
alquimista is on a distinguished road
I don't have experience using HashTable, I actually need to remove elements individually from Dynamic and Sortable lists. I'm going to review everything with this new feedback.

Thanks.
alquimista is offline   Reply With Quote

Old   July 19, 2013, 13:29
Default
  #6
Member
 
Join Date: Apr 2010
Posts: 61
Rep Power: 16
alquimista is on a distinguished road
Here is how I did it to have a pairs of labels and scalars defined as a HashTable. With this new method I can efficiently remove an element. There's a strange issue, I'm using the findLower function but the problem comes when I erase a element of the HashTable the findLower still points to the removed index. I have checked that the element was erased correctly (apparently at least debugging) and the the size reduced.

I show an example:

Code:
    HashTable<scalar, label> testHash_;

    //- insert values with key 1, 2 and 3
    testHash_.insert(1,0.5);
    testHash_.insert(2,0.7);
    testHash_.insert(3,0.3);

    //- print HashTable
    Info << "testHash: " << testHash_ << nl << endl;

    //- find value lower than 0.6
    label j = findLower(testHash_,0.6);

    //- erase key j and his value
    testHash_.erase(j);

    //- print HashTable
    Info << "testHash: " << testHash_ << nl << endl;

    //- find another value lower than 0.6
    label k = findLower(testHash_,0.6);

    //- erase key k and his value
    testHash_.erase(k);

    Info << "testHash: " << testHash_ << nl;
OUTPUT

Code:
testHash: 
3
(
3 0.3
1 0.5
2 0.7
)

testHash: 
2
(
3 0.3
2 0.7
)



--> FOAM FATAL ERROR: 
1 not found in table.  Valid entries: 2(3 2)
still looking for key 1!!


The topic of the post has been resolved for me, thank you very much! Still I have that thing but maybe I made a mistake.
hua1015 likes this.
alquimista 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] Error bulding swak4Foam sfigato OpenFOAM Community Contributions 18 August 22, 2013 12:41
[swak4Foam] funkySetFields compilation error tayo OpenFOAM Community Contributions 39 December 3, 2012 05:18
[Gmsh] discretizer - gmshToFoam Andyjoe OpenFOAM Meshing & Mesh Conversion 13 March 14, 2012 04:35
OF 1.6 | Ubuntu 9.10 (64bit) | GLIBCXX_3.4.11 not found piprus OpenFOAM Installation 22 February 25, 2010 13:43
DxFoam reader update hjasak OpenFOAM Post-Processing 69 April 24, 2008 01:24


All times are GMT -4. The time now is 14:47.