Friday, 15 May 2015

Array of unlimited polymorphic pointers as dummy variable -



Array of unlimited polymorphic pointers as dummy variable -

i want define subroutine has array of unlimited polymorphic pointers on input. simplified problem in 3 steps (the problem appears in step 3):

1. single pointer can done like:

module def type :: type1 real :: x end type type :: type2 real(8) :: y end type type(type1),target :: type(type2),target :: b class(*),pointer :: p contains subroutine printer(p) class(*),pointer :: p select type(p) type (type1) print*,p%x type (type2) print*,p%y end select end subroutine end module programme test utilize def a%x=3e0 b%y=5d0 p=>a phone call printer(p) p=>b phone call printer(p) end programme

2. if want generalize arrays of pointers need define type inculding pointer , define array of derived type. in first step defined type , tried 1 time again single pointer. here, realized need associate name within of subroutine select type build while rest extends straight forward:

module def type :: type1 real :: x end type type :: type2 real(8) :: y end type type(type1),target :: type(type2),target :: b type point class(*),pointer :: p end type type(point) :: p contains subroutine printer(z) class(point) :: z associate (o=>z%p) select type(o) type (type1) print*,o%x type (type2) print*,o%y end select end associate end subroutine end module programme test utilize def a%x=3e0 b%y=5d0 p%p=>a phone call printer(p) p%p=>b phone call printer(p) end programme

3. if define desired array of unlimited polymorphic pointers , alter subroutine accordingly, shown here:

module def type :: type1 real :: x end type type :: type2 real(8) :: y end type type(type1),target :: type(type2),target :: b type point class(*),pointer :: p end type type(point) :: p(1:2) contains subroutine printer(z) class(point),dimension(1:) :: z integer :: j associate (o=>z%p) j=1,size(z,1) select type(o) type (type1) print*,o(j)%x type (type2) print*,o(j)%y end select end end associate end subroutine end module programme test utilize def a%x=3e0 b%y=5d0 p(1)%p=>a p(2)%p=>b phone call printer(p) end programme

i error:

associate (o=>z%p) 1 error: component right of part reference nonzero rank must not have pointer attribute @ (1)

i understand error, arrays of pointers not part of fortran standard. @ moment don't see way around it. without associate build can't type selection, since produces error:

select type(z%p) 1 error: selector in select type @ (1) not named variable; utilize associate-name=>

just this

subroutine printer(z) class(point),dimension(1:) :: z integer :: j j=1,size(z,1) select type(o=>z(j)%p) type (type1) print*,o%x type (type2) print*,o%y end select end end subroutine

the association part of select type construct.

explanation of error message error: component right of part reference nonzero rank must not have pointer attribute @ (1): not allowed create reference a%b a array , b allocatable or pointer.

this because individual b's randomly placed in memory , arrays in fortran standard must describable descriptor constant strides between elements.

arrays pointers polymorphism fortran

No comments:

Post a Comment