Segmentation fault problem in fortran 90

In summary, the conversation discusses a code with a line segmentation fault error and asks for advice on debugging it. The code is for a 1D scalar wave equation using the fd method with a stability condition of 1. The code includes variable declarations and loops, and it seems that a questionable declaration of the variable u may be the source of the error. Suggestions are made to fix the declaration and addresses issues with how x and u are used in the code.
  • #1
s_hy
61
0
dear all,

i have a code with line segmentation fault error. please anyone advice me how to debug this code. thank you...
Mentor note: Added code tags, combined numerous variable declarations, and indented some do loops
Fortran:
!##################################################################

Program Main

IMPLICIT NONE

call fd1d
return
END PROGRAM Main! 1d scalar wave equation with fd method and stability condition = 1
SUBROUTINE fd1d
implicit none

real,dimension(1) :: x
real :: x_delta,  t_delta, c, xlast, xinit, tinit, tlast
real :: k, x0
real,dimension(:,,allocatable :: u
real :: alpha
integer :: iinit, ilast, ninit, nlast, i, n
real, parameter :: pi = 3.141592654

allocate (u(0:200,0:600))

!initialisation
c = 300
x_delta = 0.005
t_delta = x_delta/c
alpha = c*t_delta/x_delta
xlast = 1.0
xinit = 0
iinit = 0
ilast = int ((xlast-xinit)/x_delta)
tlast = 0.01
tinit = 0
ninit = 0
nlast = int ((tlast-tinit)/t_delta)
x(iinit) = xinit
x(ilast) = xlast

!initial profile
k = 1000.0
x0 = 0.3
do i = iinit,ilast
    u(i,0) = 0.0
    u(i,-1) = 0.0
end do

open (unit=100,file='n0.dat',action='write')
do i = iinit, ilast
    x(i) = x(iinit) + i*x_delta
    u(i,0) = exp (-k*(x(i)-x0)**2)
    u(i,-1) = exp (-k*(x(i)-x0)**2)
    Print *, u(i,0)
    write (*,*) u(i,0)
end do
close (unit = 100)

open (unit=110,file='n1.dat',action='write')
do n = 0, nlast
    do i = iinit, ilast
        u(i,n+1) = 2*(1-(alpha**2))*u(i,n)-u(i,n-1)+(alpha**2)*(u(i+1,n)+u(i-1,n))
    end do !i

    !boundary condition
    u(0,n+1) = 0
    u(ilast,n+1) = 0
    !end of boundary condition
    !export data
    print *, u(i,n+1)
    write (110,*) u(i,n+1)
end do !n
close (unit = 110)

return
end SUBROUTINE fd1d
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
@s_hy, if you're still around, something that could be the source of your problem is what seems to be a questionable declaration of the variable u.
Line 19 has this:
Fortran:
real,dimension(:,,allocatable :: u
For one thing, there is a missing right parenthesis. Per this web page I found from a class at Princeton (http://kea.princeton.edu/che422/arrays.htm), the above should look more like this, at least in Fortran 90/95:
Fortran:
real, allocatable :: u(:,:)

After the array has been declared to be allocatable, you then use allocate to actually set aside storage on the heap for the array itself, as you did in line 24 of your code:
Fortran:
allocate (u(0:200,0:600))

The declaration in line 16 is an error, I believe...
Fortran:
real,dimension(1) :: x
... at least insofar as how you used x in lines 39, 40, and 52. The array x is capable of holding one real number, so the only valid index is 1. Lines 39, 40, and 52 attempt to put numbers in places other than at index 1, if iinit, ilast are any values other than 1.
Fortran:
x(iinit) = xinit
x(ilast) = xlast
...
x(i) = x(iinit) + i*x_delta

According to how the array u is defined, the first index can be any integer from 0 through 200, and the second index can be any integer from 0 through 600. In lines 47 and 54 the second index is -1, which is not in the range of indexes you specified.
 
  • #3
Mark44 said:
Per this web page I found from a class at Princeton (http://kea.princeton.edu/che422/arrays.htm), the above should look more like this, at least in Fortran 90/95:
Fortran:
real, allocatable :: u(:,:)
For completeness, let me say that I would prefer
Fortran:
real, dimension(:,:), allocatable :: u
I think it is "neater" and more readable to have all attributes appear before the ::
 

1. What is a segmentation fault problem in Fortran 90?

A segmentation fault, also known as a segfault, is a type of error that occurs when a program attempts to access a memory address that it does not have permission to access. In Fortran 90, a segmentation fault can happen when there is a programming error, such as accessing an array element that is out of bounds or trying to write to a read-only portion of memory.

2. How do I identify a segmentation fault problem in Fortran 90?

When a program encounters a segmentation fault, it will often crash or terminate unexpectedly. In Fortran 90, the compiler may also provide an error message or traceback that can help identify the source of the problem. It is important to carefully review the code and check for any potential errors, such as invalid array indices or uninitialized variables.

3. How can I debug a segmentation fault problem in Fortran 90?

Debugging a segmentation fault problem in Fortran 90 can be challenging because it often occurs at runtime and can be caused by a variety of errors. One approach is to use a debugger, such as GDB, to step through the code and identify the source of the problem. Additionally, using print statements or writing to a log file can help track the flow of the program and identify where the error is occurring.

4. Are there any common causes of segmentation fault problems in Fortran 90?

Some common causes of segmentation fault problems in Fortran 90 include accessing out-of-bounds array elements, passing incorrect data types to functions, and using uninitialized variables. It is also important to check for any memory leaks or memory access errors when working with dynamically allocated arrays.

5. How can I prevent segmentation fault problems in Fortran 90?

To prevent segmentation fault problems in Fortran 90, it is important to thoroughly test and debug the code before running it. This includes checking for potential errors and using proper error handling techniques. It is also helpful to use tools, such as memory checkers, to identify any potential memory access errors. Additionally, regularly reviewing and refactoring the code can help prevent future segmentation fault problems.

Similar threads

  • Programming and Computer Science
Replies
4
Views
16K
  • Programming and Computer Science
Replies
8
Views
6K
  • Programming and Computer Science
Replies
4
Views
601
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
1
Views
936
  • Programming and Computer Science
Replies
4
Views
26K
  • Programming and Computer Science
Replies
12
Views
2K
Replies
1
Views
4K
  • Programming and Computer Science
Replies
4
Views
12K
  • Programming and Computer Science
Replies
8
Views
1K
Back
Top