- #1
yomar
- 2
- 0
Hi,
I am having a segmentation fault error just when I ask the code to print the value ipos in the program. However, If I change the print to the module, the segmentation fault does not appear. Could anyone can help know, why I can not transfer the variable ipos from the module to the main program or how eliminate that segmentation fault once I request print the variable in the program? Thanks.
!---------------------------------------------------
Program pro
Use Random
Implicit None
real*8 , Allocatable , Dimension (:) :: ipos
integer :: i , m
Real*8 :: u , t
m = 20
t = 1
do i = 1 , m
call Particle_alloc (ipos, m)
Print*, ipos (i)
end do
End Program pro
!----------------------------------------------------
Module Random
Implicit None
CONTAINS
!---------------------------------------------------------------------------------------------------
Subroutine Particle_alloc (ipos , m)
Implicit None
integer*4 :: m , j
real*8, Allocatable, dimension(:) :: ipos
Allocate (ipos (m))
do j = 1 , m
call init_random_seed
! Generate random number for position
call random_number(ipos)
end do
deallocate (ipos)
End Subroutine Particle_alloc
!---------------------------------------------------------------------------------------------------
Subroutine init_random_seed
!-----------------------------------------------------------------------
! Subroutine obtained from: http://gcc.gnu.org/onlinedocs/gfortran/RANDOM_005fSEED.html
!-----------------------------------------------------------------------
integer :: i, n, clock
integer, dimension(:), allocatable :: rseed
call random_seed(size = n)
allocate( rseed(n) )
call system_clock(count=clock)
rseed = clock + 37 * (/ (i - 1, i = 1, n) /)
call random_seed(PUT = rseed)
deallocate(rseed)
End Subroutine
!-----------------------------------------------------------------------------------------------------
End Module Random
I am having a segmentation fault error just when I ask the code to print the value ipos in the program. However, If I change the print to the module, the segmentation fault does not appear. Could anyone can help know, why I can not transfer the variable ipos from the module to the main program or how eliminate that segmentation fault once I request print the variable in the program? Thanks.
!---------------------------------------------------
Program pro
Use Random
Implicit None
real*8 , Allocatable , Dimension (:) :: ipos
integer :: i , m
Real*8 :: u , t
m = 20
t = 1
do i = 1 , m
call Particle_alloc (ipos, m)
Print*, ipos (i)
end do
End Program pro
!----------------------------------------------------
Module Random
Implicit None
CONTAINS
!---------------------------------------------------------------------------------------------------
Subroutine Particle_alloc (ipos , m)
Implicit None
integer*4 :: m , j
real*8, Allocatable, dimension(:) :: ipos
Allocate (ipos (m))
do j = 1 , m
call init_random_seed
! Generate random number for position
call random_number(ipos)
end do
deallocate (ipos)
End Subroutine Particle_alloc
!---------------------------------------------------------------------------------------------------
Subroutine init_random_seed
!-----------------------------------------------------------------------
! Subroutine obtained from: http://gcc.gnu.org/onlinedocs/gfortran/RANDOM_005fSEED.html
!-----------------------------------------------------------------------
integer :: i, n, clock
integer, dimension(:), allocatable :: rseed
call random_seed(size = n)
allocate( rseed(n) )
call system_clock(count=clock)
rseed = clock + 37 * (/ (i - 1, i = 1, n) /)
call random_seed(PUT = rseed)
deallocate(rseed)
End Subroutine
!-----------------------------------------------------------------------------------------------------
End Module Random