CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > General Forums > Lounge

fortran coding - growing array size

Register Blogs Community New Posts Updated Threads Search

Like Tree6Likes
  • 1 Post By prartcor
  • 1 Post By sbaffini
  • 1 Post By sbaffini
  • 1 Post By saurya
  • 2 Post By sbaffini

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   October 5, 2014, 12:32
Default fortran coding - growing array size
  #1
New Member
 
ali
Join Date: May 2014
Posts: 19
Rep Power: 12
sciense is on a distinguished road
hi
i shoud define some array that dont have specific shape and their size must increase during programe execute
how can i define such arrays?
tnx in advanced
sciense is offline   Reply With Quote

Old   October 5, 2014, 14:28
Default
  #2
New Member
 
Join Date: Oct 2014
Posts: 5
Rep Power: 12
prartcor is on a distinguished road
hi
you can define your variables as allocatable and then allocate your array.
when you want to change the array size, first deallocate it and then allocate it again.
sciense likes this.
prartcor is offline   Reply With Quote

Old   October 5, 2014, 14:46
Default
  #3
New Member
 
ali
Join Date: May 2014
Posts: 19
Rep Power: 12
sciense is on a distinguished road
tnx replying
when i deallocate , array perivious data completly erase from memory but i need to add size to my perivious array
sciense is offline   Reply With Quote

Old   October 5, 2014, 14:46
Default
  #4
Senior Member
 
sbaffini's Avatar
 
Paolo Lampitella
Join Date: Mar 2009
Location: Italy
Posts: 2,192
Blog Entries: 29
Rep Power: 39
sbaffini will become famous soon enoughsbaffini will become famous soon enough
Send a message via Skype™ to sbaffini
Short answer is, to the best of my knowledge, that you can't defer the shape of an array (1d, 2d, etc) but only its dimension (size).

Still, with derived types (possibly parametric too) with allocatable components, you are plenty of possibilities. The first coming to my mind is allocatable arrays of allocatable arrays. But really there are many.

For what concerns the size you still have few options. The trivial one is having a dummy variable which you allocate according to your old dimension, copy in the old values, deallocate the original variable and reallocate it with the new dimension, copy back the old values in the reallocated old variable with the new dimension.

The last option may or not fit your specific needs (basically, you double your memory every time you need a resize). Another option is using linked lists (note that modern fortran allows having such things as linked lists with components of mixed types), but in this case you need to bring in pointers and your memory footprint ends up being very messed up (forget cache blocking and compiler side optimizations). Basically the choice is up to you, according to your needs.
sciense likes this.
sbaffini is offline   Reply With Quote

Old   October 5, 2014, 15:02
Default
  #5
New Member
 
ali
Join Date: May 2014
Posts: 19
Rep Power: 12
sciense is on a distinguished road
Quote:
Originally Posted by sbaffini View Post
Short answer is, to the best of my knowledge, that you can't defer the shape of an array (1d, 2d, etc) but only its dimension (size).

Still, with derived types (possibly parametric too) with allocatable components, you are plenty of possibilities. The first coming to my mind is allocatable arrays of allocatable arrays. But really there are many.

For what concerns the size you still have few options. The trivial one is having a dummy variable which you allocate according to your old dimension, copy in the old values, deallocate the original variable and reallocate it with the new dimension, copy back the old values in the reallocated old variable with the new dimension.

The last option may or not fit your specific needs (basically, you double your memory every time you need a resize). Another option is using linked lists (note that modern fortran allows having such things as linked lists with components of mixed types), but in this case you need to bring in pointers and your memory footprint ends up being very messed up (forget cache blocking and compiler side optimizations). Basically the choice is up to you, according to your needs.
for your last option i should check fortran manual since i am some how begginer fortran user and it can be time consuming for me
but i must consider your second option and thanks for your really helpful suggestion
sciense is offline   Reply With Quote

Old   October 5, 2014, 15:05
Default
  #6
Senior Member
 
sbaffini's Avatar
 
Paolo Lampitella
Join Date: Mar 2009
Location: Italy
Posts: 2,192
Blog Entries: 29
Rep Power: 39
sbaffini will become famous soon enoughsbaffini will become famous soon enough
Send a message via Skype™ to sbaffini
It's easier than you think:

http://fortranwiki.org/fortran/show/Linked+list
sciense likes this.
sbaffini is offline   Reply With Quote

Old   October 11, 2014, 00:40
Default
  #7
New Member
 
saurya
Join Date: Oct 2014
Posts: 2
Rep Power: 0
saurya is on a distinguished road
Hi,
You can use either allocatable or pointer option to declare the variables in your fortran program to achieve dynamic memory allocation.

The syntax for fortran code is:

real,allocatable :: variable_name (: , : )
! for declaration of the variable
! (: ,: ) denotes a 1D array, similarly you can define multi-dimension arrays
! using (:,: ), (:,:,: ) for 2D and 3D; respectively.

allocate(variable_name(size_i,size_j))
! for allocating the size of the array when the dimensions are available in the code.

deallocate(variable_name)
! to free up the memory after usage.

"allocatable" option in fortran is better than "pointer", as it can be used inside a user defined data structure containing a set of dynamic array variables.
-Saurya
sciense likes this.
saurya is offline   Reply With Quote

Old   October 11, 2014, 15:47
Default
  #8
New Member
 
ali
Join Date: May 2014
Posts: 19
Rep Power: 12
sciense is on a distinguished road
Quote:
Originally Posted by saurya View Post
Hi,

deallocate(variable_name)
! to free up the memory after usage.

"allocatable" option in fortran is better than "pointer", as it can be used inside a user defined data structure containing a set of dynamic array variables.
-Saurya
when you deallocate variable , that variable's data completly erase from memory but i need my array size growing up
sciense is offline   Reply With Quote

Old   October 13, 2014, 19:06
Default
  #9
Senior Member
 
sbaffini's Avatar
 
Paolo Lampitella
Join Date: Mar 2009
Location: Italy
Posts: 2,192
Blog Entries: 29
Rep Power: 39
sbaffini will become famous soon enoughsbaffini will become famous soon enough
Send a message via Skype™ to sbaffini
I agree that allocatable arrays are a better option, but not on the reason.

Pointers can be allocated/deallocated and be part of derived data types as well. I used them back in 2006/2007 when allocatable components of derived data types were not yet implemented.

The point in using pointers is really that the compiler is unable to perform a lot of optimizations because of the nature of the pointers. Moreover, last time i used them, i remember a lot of headache because of a not straightforward logic in using them as actual variables (and not as pointers to other variables).

However, in order to avoid the loss of data and the temporary doubling of the allocated memory, they really are the only option here (i'm not even considering writing data back and fort from the disk, as we would not be talking the same language).

On the bright side, the usage logic of the pointers is, in this case, pretty straightforward. You have your derived data type (it might also have a single real or integer component) and in it you also have a pointer to a variable of the same derived type. Whenever you need to allocate a new component you simply allocate the pointer to the new component. An object so composed is also known as linked list.

If you're thinking of replacing vectors with them, forget performance. You need to travel along the list all the times you need access to one of the components. Usually, the better performances are achieved by a mixture of fixed size array or allocatable arrays and linked lists.

For example imagine a flow solver requiring to read multiple (but the number is unknown in advance) grid pieces to collect together. Also, local grid refinement might be required. You might define a 'grid' data type which can allocate a single grid and collect all the grids, trough pointers, in a linked list of grids. Just an example to highlight this: use pointers for large blocks of contiguous data.
sciense and bornax like this.
sbaffini is offline   Reply With Quote

Reply

Tags
fortran


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
[ICEM] Effect of globle mesh size and mesh independency sujay ANSYS Meshing & Geometry 20 September 29, 2019 08:36
Comparison between C/C++ and Fortran? rick Main CFD Forum 45 September 6, 2011 01:52
Fortran CFD coding on Macintosh OS 9mile Main CFD Forum 2 August 23, 2009 02:38
fortran coding for explosion dj CFX 0 September 25, 2003 02:33
'C' or FORTRAN or 'C++' Yogesh Talekar Main CFD Forum 20 October 21, 1999 05:00


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