0

I am trying to define an array of 1 dimension phi0(ngrains) where each element is a matrix of dimension (taille,taille), so whene I look for phi0(1) for example it should display a matrix of taille*taille elements, and the same case for phi0(2) .... phi0(ngrains).

How it can be done in Fortran?

This is what I have tried so far: but I keep getting error messages

PROGRAM DERIVED_TYP

    INTEGER :: i,ngrains,nmax=4

    TYPE INITIAL        
        REAL, DIMENSION(:,:), ALLOCATABLE :: zeros(:,:)
    END TYPE INITIAL

    TYPE (INITIAL), dimension(:), allocatable :: phi0


    ALLOCATE (phi0(1)%zeros(nmax,nmax))
    ALLOCATE (phi0(2)%zeros(nmax,nmax)) 

    print*,phi0(1)%zeros

    !phi0(2)=INITIAL(0.)


END PROGRAM DERIVED_TYP
  • What error messages do you get? I suspect, they are related to the definition of `zeros`, which looks fishy. Did you try `real, allocatable :: zeros(:,:)` instead? – PVitt Nov 08 '16 at 16:25
  • You haven't allocated the `phi0` container array yet, so when you do `ALLOCATE (phi0(1)%zeros(nmax,nmax))` you're accessing an element of an unallocated array. Try adding `ngrains=2 ; allocate(phi0(ngrains))` before this line. – d_1999 Nov 08 '16 at 16:28
  • Have you tried searching? This has bean treated in duplicates before, – Vladimir F Героям слава Nov 08 '16 at 16:35
  • 2
    " but I keep getting error messages" You **have to** show those messages, really. – Vladimir F Героям слава Nov 08 '16 at 16:36
  • thanks guys for your interaction, @d_1999 you are right, that was exactly my mistake i dont get the message error any more. now is it possible to use a do loop to Allocate each element (ngrains elements) of Phi0?? secondly I want that each matrix of phi0 to be a matrix of zeros, so whene i look for phi0(34) for example it should display a matrix of (nmax*nmax) zeros, and the same case for all phi0 element – dimo_ahmed Nov 08 '16 at 16:48
  • I seconded the duplicate request because the title is "How to define an array of arrays?" and that is well treated there. If the question was to explain some error message we were not shown then the question should be more specific. – Vladimir F Героям слава Nov 08 '16 at 17:57

1 Answers1

1

As I mentioned earlier in the comments, you don't allocate phi0 before you try to allocate it's members. A modified version of the code which avoids this is something like:

PROGRAM DERIVED_TYP

    INTEGER :: i
    INTEGER, parameter :: ngrains=2 ,nmax=4

    TYPE INITIAL        
        REAL, DIMENSION(:,:), ALLOCATABLE :: zeros
    END TYPE INITIAL

    TYPE (INITIAL), dimension(:), allocatable :: phi0

    !First allocate ngrains elements in our container array
    ALLOCATE (phi0(ngrains))
    !Now allocate our nmax by nmax zeros array in each of our 
    !container elements.
    do i=1,ngrains
        ALLOCATE (phi0(i)%zeros(nmax,nmax))
    end do

    print*,phi0(1)%zeros

END PROGRAM DERIVED_TYP

Note I've modified the INITIAL definition as well, along the lines of the comment by @PVitt although my compiler didn't complain about the original form.

I should also note that whilst we allocate the zeros arrays we don't actually set them to anything so what you get when you print the array is not well defined, may not be repeatable and is likely to also depend upon your compiler.

d_1999
  • 854
  • 6
  • 16
  • Looks good. But as long as ngrains is a parameter, you could define `phi0` as `type(initial) :: phi0(ngrains)` to avoid the allocation. – PVitt Nov 08 '16 at 16:37
  • 1
    Indeed you could. I didn't do that here to make the original issue/fix (hopefully) more transparent. – d_1999 Nov 08 '16 at 16:38
  • sure, just for the records :) – PVitt Nov 08 '16 at 16:39
  • That was very helpful @d_1999, and indeed how can i set the values of zeros(nmax,nmax) to be =0. ?? can it be done in the Allocate statement ?? – dimo_ahmed Nov 08 '16 at 17:02
  • @dimo_ahmed to fill the arrays with 0, you can use `phi0(i)%zeros = 0` – PVitt Nov 08 '16 at 22:28