Silverfrost Forums

Welcome to our forums

Passing arrays to subroutine

14 Feb 2013 2:44 #11556

hi all I already posted a subroutine which compute some stuff from a binary file. I was declaring the the dimensions of the output of my subroutine but now i changed that so these output are allocatable. the compiler told me (core dumped).

this is the calling program:

program ttz implicit none integer,parameteriwp = selected_real_kind(8) integer Fileunit=18,Nnodes character(len=9)::Filename='bgpdt.op2' real(iwp),allocatable ::dcoor(:,:) ! call newstuff(Fileunit,Filename,Nnodes,dcoor) ! end program ttz

and this is my subroutine

subroutine newstuff(Fileunit,Filename,Nnodes,dcoor) implicit none integer,parameter::iwp = selected_real_kind(8) type xyzcoord sequence integer,dimension(6)::wert real(iwp),dimension(3)xyz end type xyzcoord integeri,ii,j integer,intent(in)::Fileunit integer,intent(out)::Nnodes integer,dimension(7)::Ta character(len=*),intent(in)Filename type(xyzcoord),allocatabledata(:) real(iwp),allocatable,intent(out)::dcoor(:,:) ! open(unit=Fileunit,file=Filename,status='old',form='unformatted',action='read') ! rewind(Fileunit) ! i=1 do while(i.LT.13) read(Fileunit) i=i+1 end do read(Fileunit)(Ta(i),i=1,7) Nnodes=Ta(2) allocate(dcoor(Nnodes,3),data(Nnodes)) ! j=1 do while(i.LT.10) read(Fileunit) j=j+1 end do ! read(Fileunit)data ! forall(ii=1:Nnodes)dcoor(ii,:) = data(ii)%xyz ! close(Fileunit) end subroutine newstuff

my feeling is that i need to use an interface statement or something similar. since i am new to fortran these advanced methods are difficult to me . please help me

14 Feb 2013 8:33 #11562

Can you edit your post to remove the use of Smilies (tick Disable Smiles), then we can see better what your code is doing.

I think, you need to do something like the following. Note that the array argument is allocated in the main program, but not in the subroutine. Also you do need an explicit interface (as below) or better still, use a module.

program main

   interface
      subroutine zzz(a)
         real :: a(:)
      end subroutine zzz
   end interface

   real, allocatable :: a(:)

   allocate(a(100))

   call zzz(a)

end program main

subroutine zzz(a)
   real :: a(:)
   ! do something with a
end subroutine zzz
15 Feb 2013 7:34 #11565

Hi again in my subroutine , i have as input a binary file ( filename and fileunit). and my ouputs are Nnodes (integer) and dcoor (array); dcoor has a size Nnodes*3.

so Nnodes and dcoor are absolutly unknown for me.

subroutine newstuff(Fileunit,Filename,Nnodes,dcoor) implicit none integer,parameter::iwp = selected_real_kind(8) ! type xyzcoord sequence integer,dimension(6)::wert real(iwp),dimension(3)xyz end type xyzcoord ! integeri,ii,j integer,intent(in)::Fileunit integer,intent(out)::Nnodes integer,dimension(7)::Ta ! character(len=*),intent(in)Filename ****[color=red:7fd49e58ea] type(xyzcoord),allocatabledata(:) real(iwp),allocatable,intent(out)::dcoor(:, 😃[/color:7fd49e58ea]! !

**open(unit=Fileunit,file=Filename,status='old',form='unformatted',action='read') ! rewind(Fileunit) ! i=1 do while(i.LT.13) read(Fileunit) i=i+1 end do read(Fileunit)(Ta(i),i=1,7) Nnodes=Ta(2) allocate(dcoor(Nnodes,3),data(Nnodes)) ! j=1 do while(i.LT.10) read(Fileunit) j=j+1 end do ! read(Fileunit)data ! forall(ii=1:Nnodes)dcoor(ii,:)= data(ii)%xyz ! close(Fileunit) end subroutine newstuff **

Is this way a correct way ? of declaring the arrays data and dcoor. and how can i include it in a main program. this is my main calling programm but the compiler tell me (core dumped)

program ttz implicit none integer,parameteriwp = selected_real_kind(8) integer Fileunit=18,Nnodes character(len=9)::Filename='bgpdt.op2' real(iwp),allocatable ::dcoor(:,:) call newstuff(Fileunit,Filename,Nnodes,dcoor) write(,) '--------------------------' write(,) Nnodes
write(,) '--------------------------' write(,'(3F12.4)') dcoor(1,:)
write(
,) '--------------------------' write(,'(3F12.4)') dcoor(2,:)
write(,) '--------------------------' write(,'(3F12.4)') dcoor(3,:)
write(
,) '--------------------------' write(,'(3F12.4)') dcoor(4,:)
write(,) '--------------------------' end program ttz

Please login to reply.