forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Passing arrays to subroutine

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General
View previous topic :: View next topic  
Author Message
joemezni



Joined: 14 Jan 2013
Posts: 8

PostPosted: Thu Feb 14, 2013 3:44 pm    Post subject: Passing arrays to subroutine Reply with quote

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,parameter::iwp = selected_real_kind(Cool
integer:: Fileunit=18,Nnodes
character(len=9)::Filename='bgpdt.op2'
real(iwp),allocatable ::dcoor(:,Smile
!
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(Cool
type xyzcoord
sequence
integer,dimension(6)::wert
real(iwp),dimension(3)::xyz
end type xyzcoord
integer::i,ii,j
integer,intent(in)::Fileunit
integer,intent(out)::Nnodes
integer,dimension(7)::Ta
character(len=*),intent(in)::Filename
type(xyzcoord),allocatable::data(Smile
real(iwp),allocatable,intent(out)::dcoor(:,Smile
!
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,Smile = 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
Back to top
View user's profile Send private message
davidb



Joined: 17 Jul 2009
Posts: 560
Location: UK

PostPosted: Thu Feb 14, 2013 9:33 pm    Post subject: Reply with quote

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.

Code:

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

_________________
Programmer in: Fortran 77/95/2003/2008, C, C++ (& OpenMP), java, Python, Perl
Back to top
View user's profile Send private message
joemezni



Joined: 14 Jan 2013
Posts: 8

PostPosted: Fri Feb 15, 2013 8:34 am    Post subject: Reply with quote

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
!
integer::i,ii,j
integer,intent(in)::Fileunit
integer,intent(out)::Nnodes
integer,dimension(7)::Ta
!
character(len=*),intent(in)::Filename

type(xyzcoord),allocatable::data(:)
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


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,parameter::iwp = 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
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group