3

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)
5
  • See also this answer for some justification of why an explicit interface is required for such arguments. Also this question is related (with slightly different symptoms). Commented Mar 29, 2018 at 21:38
  • Thank you very much, is there a way to do it without all of the above? I understand that there are simpler and more time efficient ways to complete the task, but my lecturer hadn't taught us that yet. Commented Mar 29, 2018 at 21:46
  • Well, you don't need (from the sample you've shown) the argument pos to be allocatable (you aren't changing the allocation status of the argument) so you could rewrite the subroutine to use explicit shape arrays and then an explicit interface isn't required. However, the "module" approach is a good one to take. See this question, and my answer there, for some detail. Commented Mar 29, 2018 at 22:13
  • This is a much more appropriate duplicate target. It refers to allocatable dummies rather than assumed size. Commented Mar 29, 2018 at 22:15
  • What can I deduce from that error regarding the array being sent? Commented Mar 30, 2018 at 13:21

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.