Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increase array size on-the-fly in Fortran?

My program is running though 3D array, labelling 'clusters' that it finds and then doing some checks to see if any neighbouring clusters have a label higher than the current cluster. There's a second array that holds the 'proper' cluster label. If it finds that the nth adjoining cluster is labelled correctly, that element is assigned to 0, otherwise is assigns it to the correct label (for instance if the nth site has label 2, and a neighbour is labeled 3, the 3rd element of the labelArray is set to 2). I've got a good reason to do this, honest!

All I want is to be able to assign the nth element of the labelArray on the fly. I've looked at allocatable arrays and declaring things as labelArray(*) but I don't really understand these, despite searching the web, and StackOverflow.

So any help on doing this would be awesome.

like image 719
AncientSwordRage Avatar asked Dec 05 '11 11:12

AncientSwordRage


People also ask

Can we increase the size of array at runtime?

Size of an array If you create an array by initializing its values directly, the size will be the number of elements in it. Thus the size of the array is determined at the time of its creation or, initialization once it is done you cannot change the size of the array.

How do you increase the size of an array C?

Arrays are static so you won't be able to change it's size. You'll need to create the linked list data structure. The list can grow and shrink on demand. Save this answer.

What does Allocatable do in Fortran?

The ALLOCATABLE attribute allows you to declare an allocatable object. You can dynamically allocate the storage space of these objects by executing an ALLOCATE statement or by a derived-type assignment statement. If the object is an array, it is a deferred-shape array or an assumed-rank array.

How do I add an array in Fortran?

Declaring ArraysArrays are declared with the dimension attribute. The individual elements of arrays are referenced by specifying their subscripts. The first element of an array has a subscript of one. The array numbers contains five real variables –numbers(1), numbers(2), numbers(3), numbers(4), and numbers(5).


2 Answers

Here is a Stack Overflow question with some code examples showing several ways of using Fortran allocatable arrays: How to get priorly-unkown array as the output of a function in Fortran: declaring, allocating, testing for being already being allocated, using the new move_alloc and allocation on assignment. Not shown there is explicit deallocation, since the examples are using move_alloc and automatic deallocation on exit of a procedure.

P.S. If you want to repeatedly add one element you should think about your data structure approach. Adding one element at a time by growing an array is not an efficient approach. To grow an array from N elements to N+1 in Fortran will likely mean creating a new array and copying all of the existing elements. A more appropriate data structure might be a linked list. You can create a linked list in Fortran by creating a user-defined type and using pointers. You chain the members together, pointing from one to the next. The overhead to adding another member is minor. The drawback is that it is easiest to access the members of the list in order. You don't have the easy ability of an array, using indices, to access the members in any order.

Info about linked lists in Fortran that I found on the web: http://www-uxsup.csx.cam.ac.uk/courses/Fortran/paper_12.pdf and http://www.iag.uni-stuttgart.de/IAG/institut/abteilungen/numerik/images/4/4c/Pointer_Introduction.pdf

like image 167
M. S. B. Avatar answered Sep 24 '22 20:09

M. S. B.


If you declare an array allocatable, you use deffered shape in the form real,

allocatable :: labelArray(:,:)

, or

real,dimension(:,:),allocatable :: labelArray

with number of double colons meaning rank (number of your indexes) of your array.

If the array is unallocated you use

 allocate(labelarray(shapeyouwant))

with the correct number of indexes. For example allocate(labelarray(2:3,-1:5)) for array with indexes 2 to 3 in demension 1 and -1 to 5 in dimension 2.

For change of dimension you have to deallocate the array first using

deallocate(labelArray)

To reallocate an allocated array to a new shape you first need to allocate a new array with the new shape, copy the existing array to the new array and move the reference of the old array to the new array using move_alloc().

  call allocate(tmp(size_old+n_enlarge))
  tmp(1:size_old) = array(1:size_old)
  call move_alloc(tmp, array)

The old array is deallocated automatically when the new array reference is moved by move_alloc().


Fortran 95 deallocates arrays automatically, if they fall out of scope (end of their subroutine for example).

Fortran 2008 has a nice feature of automatic allocation on assignment. If you say array1=array2 and array1 is not allocated, it is automatically allocated to have the correct shape.

It can also be used for re-allocation (see also Fortran array automatically growing when adding a value and How to add new element to dynamical array in Fortran 90)

 labelArray = [labelArray, new_element]
like image 25
Vladimir F Героям слава Avatar answered Sep 22 '22 20:09

Vladimir F Героям слава