0

If I have the following code (thanks to M. Chinoune) in a subroutine

type :: vector
    integer, dimension(:), allocatable :: elements
end type vector

type :: ragged_array
    type(vector), dimension(:), allocatable :: vectors
end type ragged_array

type(ragged_array) :: raggar

allocate( raggar%vectors(2) )
allocate( raggar%vectors(1)%elements(3) )
allocate( raggar%vectors(2)%elements(4) )

raggar%vectors(1)%elements=0 
raggar%vectors(2)%elements=0 

if I want to pass raggar in an other subroutine to modify size of raggar. should I do something like :

CALL MySubroutine(raggar)

or

CALL MySubroutine(raggar%vectors%elements)

And then, in my subroutine how can I declare it ?

SUBROUTINE MySubroutine(raggar)
type :: vector
    integer, dimension(:), allocatable :: elements
end type vector

type :: ragged_array
    type(vector), dimension(:), allocatable :: vectors
end type ragged_array

type(ragged_array), INTENT(INOUT):: raggar

I did many tried but I always get error such as :

The type of the actual argument differs from the types of the dummy argument.

or

the shape matching rules of actual arguments and dummy argument have been violated

Dadep
  • 2,796
  • 5
  • 27
  • 40

1 Answers1

1

Place type definitions into a module and use it in a program and subroutine.

module my
type :: vector
    integer, dimension(:), allocatable :: elements
end type vector

type :: ragged_array
    type(vector), dimension(:), allocatable :: vectors
end type ragged_array
end module

program probe
use my
type(ragged_array) :: ragarr
allocate( ragarr%vectors(2) )
allocate( ragarr%vectors(1)%elements(3) )
allocate( ragarr%vectors(2)%elements(4) )
ragarr%vectors(1)%elements=0 
ragarr%vectors(2)%elements=0 
CALL MySubroutine(ragarr)
end program

SUBROUTINE MySubroutine(rr)
use my
type(ragged_array), INTENT(INOUT):: rr
end subroutine
Michael
  • 5,095
  • 2
  • 13
  • 35
  • I already have a module with my subroutines, is it better to create two separated module ? o can I but the subroutine in the same module ? thx! (because I want to call the subroutine in an other subroutine) – Dadep Jun 06 '17 at 17:44
  • This module contains the types shared by the main program and the subroutine. If you already have a module used by program and subroutine, you can place the definitions there. – Michael Jun 06 '17 at 17:49
  • Ok it work ! thanks a lot, and if I want to duplicate `rr` in `MySubroutine` is there an equivalent of `size(rr)` ? – Dadep Jun 06 '17 at 19:13
  • I have found a way using a loop reading each size of each `elements` ! thanks you so much to make me understand modules ! ! – Dadep Jun 06 '17 at 19:35