|
[Sponsors] |
October 5, 2014, 12:32 |
fortran coding - growing array size
|
#1 |
New Member
ali
Join Date: May 2014
Posts: 19
Rep Power: 12 |
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 |
|
October 5, 2014, 14:28 |
|
#2 |
New Member
Join Date: Oct 2014
Posts: 5
Rep Power: 12 |
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. |
|
October 5, 2014, 14:46 |
|
#3 |
New Member
ali
Join Date: May 2014
Posts: 19
Rep Power: 12 |
tnx replying
when i deallocate , array perivious data completly erase from memory but i need to add size to my perivious array |
|
October 5, 2014, 14:46 |
|
#4 |
Senior Member
|
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. |
|
October 5, 2014, 15:02 |
|
#5 | |
New Member
ali
Join Date: May 2014
Posts: 19
Rep Power: 12 |
Quote:
but i must consider your second option and thanks for your really helpful suggestion |
||
October 5, 2014, 15:05 |
|
#6 |
Senior Member
|
||
October 11, 2014, 00:40 |
|
#7 |
New Member
saurya
Join Date: Oct 2014
Posts: 2
Rep Power: 0 |
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 |
|
October 11, 2014, 15:47 |
|
#8 |
New Member
ali
Join Date: May 2014
Posts: 19
Rep Power: 12 |
when you deallocate variable , that variable's data completly erase from memory but i need my array size growing up
|
|
October 13, 2014, 19:06 |
|
#9 |
Senior Member
|
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. |
|
Tags |
fortran |
|
|
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 |