|
[Sponsors] |
October 27, 2011, 08:34 |
Append scalar to scalarField
|
#1 |
Senior Member
Hisham Elsafti
Join Date: Apr 2011
Location: Braunschweig, Germany
Posts: 257
Blog Entries: 10
Rep Power: 17 |
Dear Foamers,
I need to expand a scalarField size by one for each time step. How can I append a scalar to a scalarField? Regards Hisham |
|
October 27, 2011, 09:36 |
|
#2 |
Senior Member
Sandeep Menon
Join Date: Mar 2009
Location: Amherst, MA
Posts: 403
Rep Power: 25 |
You can use the setSize(const label, const T&) member function in the List class (where Field is derived from) to increment its size (by 1 or any other number). This automatically copies values from the old field, and fills the remaining space with a value that you specify.
Take a look at List.C / List.H. |
|
October 27, 2011, 11:31 |
|
#4 | |
Senior Member
Tomislav Maric
Join Date: Mar 2009
Location: Darmstadt, Germany
Posts: 284
Blog Entries: 5
Rep Power: 21 |
Quote:
Will this not be expensive because of the memory copying taking place? Code:
T* nv = new T[label(newSize)]; Code:
void * memcpy ( void * destination, const void * source, size_t num ); Copy block of memory Copies the values of num bytes from the location pointed by source directly to the memory block pointed by destination. |
||
October 28, 2011, 11:02 |
|
#6 |
Senior Member
Tomislav Maric
Join Date: Mar 2009
Location: Darmstadt, Germany
Posts: 284
Blog Entries: 5
Rep Power: 21 |
Hi Hisham:
that feeling was wrong for the both of us. It seems that the DynamicList is nothing more than a wrapper for List<T> when it comes to expanding the storage. If you take a look at DynamicListI.H and inspect the append method: Code:
00309 inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::append 00310 ( 00311 const T& t 00312 ) 00313 { 00314 label elemI = List<T>::size(); 00315 setSize(elemI + 1); 00316 00317 this->operator[](elemI) = t; 00318 } 00319 guess what the DynamicList::setSize does: Code:
00173 if (nElem > capacity_) 00174 { 00175 // TODO: convince the compiler that division by zero does not occur 00176 // if (SizeInc && (!SizeMult || !SizeDiv)) 00177 // { 00178 // // resize with SizeInc as the granularity 00179 // capacity_ = nElem; 00180 // unsigned pad = SizeInc - (capacity_ % SizeInc); 00181 // if (pad != SizeInc) 00182 // { 00183 // capacity_ += pad; 00184 // } 00185 // } 00186 // else 00187 { 00188 capacity_ = max 00189 ( 00190 nElem, 00191 label(SizeInc + capacity_ * SizeMult / SizeDiv) 00192 ); 00193 } 00194 00195 List<T>::setSize(capacity_); 00196 } 00197 What I don't get is why would one wrap a List<T> into something called "DynamicList" if the underlying engine is based on something like (from UList<T>): Code:
this->v_ = new T[this->size_]; list that allows for appending without memcopy would be LList (Linked List), but I need to check that out... Of course it was wrong, why would it be right? T. |
|
October 28, 2011, 11:26 |
|
#7 | |
Senior Member
Sandeep Menon
Join Date: Mar 2009
Location: Amherst, MA
Posts: 403
Rep Power: 25 |
Quote:
If and when increased capacity is required, additional memory is allocated (typically twice current capacity), so appends / removes can continue happily. |
||
October 28, 2011, 16:38 |
|
#8 | |
Senior Member
Tomislav Maric
Join Date: Mar 2009
Location: Darmstadt, Germany
Posts: 284
Blog Entries: 5
Rep Power: 21 |
Quote:
I didn't say it was "just a simple wrapper around List", I said: " DynamicList is nothing more than a wrapper for List<T> when it comes to expanding the storage." and that's, well, true. memcpy or not, it uses simple arrays on the low level. This means that it has to create a completely new list in one way or the other (for the sake of this argument, it makes absolutely no difference in which way the copying is taking place), in order to append something to the existing one, in a logical sense. From List::setSize: Code:
00327 if (newSize != this->size_) 00328 { 00329 if (newSize > 0) 00330 { 00331 T* nv = new T[label(newSize)]; That's what's in the code anyway.... or am I reading it wrong? T. |
||
October 28, 2011, 16:45 |
|
#9 |
Senior Member
Sandeep Menon
Join Date: Mar 2009
Location: Amherst, MA
Posts: 403
Rep Power: 25 |
The new list is created only when the underlying storage is filled to capacity. Anyhow, I can't think of a better way to do this, so I consider this optimal.
|
|
October 28, 2011, 16:58 |
|
#10 | |
Senior Member
Tomislav Maric
Join Date: Mar 2009
Location: Darmstadt, Germany
Posts: 284
Blog Entries: 5
Rep Power: 21 |
Quote:
If I want to use a linked list, it may save more time, but may not, because there will be a price to pay if I need to acces n-th element directly in the fact that it will take (textbook quote) "linear time" to get to it by using an iterator. Now the real question is, for small lists that span from 10 to 100 elements, say hundred thousand of them within a time-step scope, do I use "dynamic" storage of DynamicList, or dynamic storage provided by a Linked List. The only thing that comes to my mind is to benchmark this somehow, but what I read about benchmarking makes it seem evil. The question I had was because I'm used to thinking about dynamical storage in the sense of lists that actually create memory space for an element, and use pointers to access that. This is why the name DynamicList threw me off. I have no idea for the motivation behind this (I'm not the developer, nor had any of the guys/girls that wrote this explained the background to me), and I'm not saying its wrong or something, it's just that I find the name confusing (especially with the coment that says something about automatically resizing... ). |
||
October 28, 2011, 17:07 |
|
#11 |
Senior Member
Sandeep Menon
Join Date: Mar 2009
Location: Amherst, MA
Posts: 403
Rep Power: 25 |
Choosing a particular container depends on how you would like to use it. If you want random access into a linked list, then a hash table would be a good choice, but you'll take a hit with performance due to cache misses. If linear traversal is of priority, then nothing beats a regular array.
I've used the DynamicList / DynamicField functionality quite extensively, and I find that it strikes an optimal balance with speed and flexibility of storage. But again, something that works for me doesn't necessarily translate to general applicability. |
|
October 28, 2011, 17:17 |
|
#12 |
Senior Member
Tomislav Maric
Join Date: Mar 2009
Location: Darmstadt, Germany
Posts: 284
Blog Entries: 5
Rep Power: 21 |
Well, right now I've coded everything with DynamicList, and it seems to be working with O.K. speed. I don't think I'll touch this untill the coupling with the CFD solution... that's something that just came to my mind.... heaven knows how will all this compare to the governing system solution. If the total computational time is in order of few % of the governing system solution, it will make no sense to change it.
That is if I ever get to the point of solving the system. Wish me luck.. I'll need it. |
|
October 28, 2011, 20:42 |
|
#13 |
Senior Member
Hisham Elsafti
Join Date: Apr 2011
Location: Braunschweig, Germany
Posts: 257
Blog Entries: 10
Rep Power: 17 |
Hi Tomislav and Sandeep,
Thanks a lot for your posts. Much food for thought My problem is about a dynamic container that is unique for the domain (actually they are two). Its size is a user input and after a little thought I need to trim the first element and append a new element at the end and renumber the elements for each time step (so the defined size is constant). Luckily, this is not for each cell. I started using a dynamic allocation of an array. I will look to your suggestions in more details. Nevertheless, considering the new infos, how can one does this in the optimum Foam way? Regards, Hisham |
|
Tags |
append, scalarfield |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Solving for an additional species CO in coalChemistryFoam | N. A. | OpenFOAM Programming & Development | 3 | February 18, 2019 06:58 |
dieselFoam problem!! trying to introduce a new heat transfer model | vivek070176 | OpenFOAM Programming & Development | 10 | December 24, 2014 00:48 |
Specifying nonuniform boundary condition | maka | OpenFOAM Running, Solving & CFD | 59 | October 22, 2014 15:52 |
Climbing inlet pressure with simpleFoam and directMappedPatches | chegdan | OpenFOAM Running, Solving & CFD | 1 | January 2, 2012 20:35 |
CFX12 rif errors | romance | CFX | 4 | October 26, 2009 14:41 |