I'm taking a course in atomistic simulation, and the language is Fortran. I'm writing a program that simulates the movement of atoms in a box. I have got a main and numerous subroutines. The main program code is the following:
program Spheres
implicit none
integer :: nspheres
double precision :: rvolume
integer :: ncollisions
double precision :: sigma
double precision, allocatable :: pos(:,:)
integer :: k,j
write(*,*) '1st subroutine read_input:'
call read_input(nspheres,rvolume,ncollisions)
write(*,*) ' '
allocate(pos(1:3,1:nspheres))
write(*,*) '2nd subroutine validate_input:'
call validate_input(nspheres,rvolume)
write(*,*) ' '
write(*,*) '3rd subroutine compute_diameter:'
call compute_diameter(nspheres,rvolume,sigma)
write(*,*) ' '
do k = 1, 3
do j = 1, nspheres
pos(k,j) = k
end do
end do
write(*,*) '4th subroutine assign_positions:'
call assign_positions(pos)
write(*,*) ' '
end program Spheres
I've created a 2-D array, which I allocate according to an integer that's inputted by the user (integer named nspheres). After receiving nspheres from another subroutine, I'm allocating for the array.
Afterwards, the array is sent to another subroutine which decides the locations of the given amount of atoms (subroutine's name is assign_positions
).
The 'assign_positions' subroutine code is the following, with the relevant part of code:
subroutine assign_positions(pos)
double precision, dimension(:,:), intent(inout), allocatable :: pos
integer :: nspheres
...
write(*,*), size(pos,2)
nspheres = size(pos,2)
...
do k = 1, 3
do j = 1, nspheres
write(*,*), pos(k,j)
end do
end do
end subroutine assign_positions
The thing is my lecturer wants the subroutine "assign-positions" to calculate the nspheres from the size of the sent array, and not sending both the array and the nspheres integer.
Inside the subroutine I've created another array, in order to receive the sent array and another integer named nspheres (along with other variables..).
Outside that subroutine (namely, before it) size(pos,2)
gives back nspheres
, because the array was created for the nspheres, BUT inside that subroutine, size(pos,2)
give back a huge number, 1862125071.
I'm working on a virtual ubuntu environment, with gfortran compiler.
In order the test my code, I've entered some values to the array inside the main, and printed them, inside the main and then inside that suboutine. The program compiled, and upon going to that subroutine the next error appears:
Program received signal SIGBUS: Access to an undefined portion of a memory object.
Backtrace for this error:
#0 0x7FEF314C0E08
#1 0x7FEF314BFF90
#2 0x7FEF30E074AF
#3 0x7FEF3159863F
#4 0x7FEF3159B154
#5 0x7FEF3159BD3E
#6 0x400EC5 in assign_positions_
#7 0x40192D in MAIN__ at Spheres.f90:?
Bus error (core dumped)