1

My code compiles, but it is not running properly. I'm not sure whether its inability to run is due to a mathematical error and/or if there is an issue in my coding syntax. The code is shown below. If anyone could me figure out what is wrong, please let me know.

Program received signal SIGSEGV: Segmentation fault - invalid memory reference.
Backtrace for this error:
#0  0x7F1E52D74E08
#1  0x7F1E52D73F90
#2  0x7F1E526BB4AF
#3  0x400BBC in legendrepoly.3381 at project.f90:?
#4  0x40138C in MAIN__ at project.f90:?
Segmentation fault (core dumped)



program Project

use iso_fortran_env
implicit none

integer(int32)               :: Nmax   ! maximum order of Legendre polynomial
integer(int32)               :: step   ! step size in angle from 0 to 180 degrees
real(real64), parameter      :: pi=3.1415926
real(real64)                 :: mu
real(real64)                 :: theta=0
real(real64), dimension(:,:), allocatable :: P, dP      ! Legendre polynomial
integer(int32)               :: k, h, i, n, l

! User Input
print *, "Please enter maximum order of Legendre polynomials:"
read (*,*) Nmax
print *, "Please enter integer step size (in degrees between 0 and 180):"
read (*,*) step

! Output
mu = cos(theta*(pi/180.0_real64))
k = 180.0/step
h = 2*cos(real(step)*(pi/180.0_real64))
allocate (  P(Nmax+1,180)  )
allocate ( dP(Nmax+1,180) )

call legendrepoly(l,mu,Nmax,step)
print *, dP

contains
subroutine legendrepoly(l,mu,Nmax,step)
real(real64), dimension(:,:), allocatable :: P, dP
real(real64)                 :: a
real(real64), intent(in)     :: mu
integer(int32)               :: i, Nmax, step
integer(int32), intent(in)   :: l

a = real(l)
do i = 1, 181, step
   P(1,i-1) = 1.0
   P(2,i-1) = mu
end do

do i = 1, 181, step
   do n = 2, Nmax
      P(l+1,i-1) = ((2.0*a+1.0) * mu * P(l,i-1) - a*P(l-1,i-1)) / (a+1.0)
      dP(l+1,i-1) = (P(l+1,i)-P(l+1,i-2))/2
   end do
end do

end subroutine legendrepoly

end program Project
Micah
  • 11
  • 1
  • 5
    Welcome. You use the variable `P` in the `subroutine legendrepoly` overruling the global `P` (similar for `dP`). Note also that you allocate `P` from 1 to 180, but you use also element 0 (compile with boundary checking). – albert Dec 07 '18 at 19:05
  • 1
    To start with, you should define PI as [`4*atan(1_real64)`](https://stackoverflow.com/questions/2157920/why-define-pi-4atan1-d0) and not 3.1415926. You are missing half of your precision! – kvantour Dec 08 '18 at 13:06
  • 1
    @kvantour, `1._real64`, not `1_real64`. – francescalus Dec 09 '18 at 00:12

0 Answers0