Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Module and shared data

gelarimer
Beginner
527 Views

I would like to declare the arrays X, Y, DX, DY, and A in the shared data of theMODULE below, and eliminate the associated dummy arguements from Subroutine SplineSetup(). These arrays areused by Sub, Spline() in the same MODULE, but are not used outside the MODULE. However,the DIMENSION of the arraysis a variable 'N' which is a dummy arg for Sub. SplineSetup(), and this seems to prevent including the arrays in the MODULE's shared data. Is there a way to do this?

Thanks for any comments.

[plain]MODULE SplineFit

! shared data here

CONTAINS

SUBROUTINE SplineSetup(N,X,Y,DX,DY,A)
!
! code below is modified version of NA 300 piece wise cubic spline interpolation
! example code
!
! N -- number of data points
! X -- X(1:N) array or X input values to be used for interpolation
! Y -- Y(1:N) array or Y input values to be used for interpolation
! DX, DY, A coefficient arrays returned

IMPLICIT NONE

INTEGER N, N1
INTEGER ib
INTEGER i

REAL, DIMENSION(N) :: X, Y
REAL, DIMENSION(N) :: DX, DY, A
REAL, DIMENSION(N) :: T
REAL PIV

       A(1) = 0.0
       A(N) = 0.0
              
       N1 = N - 1
       
       do i = 1, N1
       
         DX(i) = X(i+1) - X(i)
         DY(i) = (Y(i+1) - Y(i))/DX(i)
         
       end do
       
       T(1) = 0.0
       
       do i = 2, N1
       
         PIV = 2.0*(DX(i-1)*DX(i)) - DX(i-1)*T(i-1)
         T(i) = DX(i)/PIV
         A(i) = (DY(i) - DY(i-1) - DX(i-1)*A(i-1))/PIV
         
       end do
       
       do ib = 2, N1
       
         i = N + 1 - ib
         A(i) = A(i) - T(i)*A(i+1)
         
       end do
              
!       iError = 1
       RETURN
       
END SUBROUTINE

SUBROUTINE Spline(N,XX,S)
!
! INPUT -- XX any value
! OUTPUT -- S = Spline evaluated at XX

INTEGER K
INTEGER N

REAL XX, S
REAL W, V
REAL, DIMENSION(N) :: X, Y
REAL, DIMENSION(N) :: DX, DY, A

       K = 1
       
       ! lower bound
       do
       
         if(XX .LT. X(K)) then
       
           K = K-1
           
           if(K .EQ. 0) CYCLE
           
           K = 1
           
           S = Y(1) + (DY(1) - DX(1)*A(2))*(XX-X(1))
           
           RETURN
           
         end if
         
         ! if(XX .GE. X(K)) then EXIT do loop
         EXIT
           
       end do
       
       ! upper bound
       do
       
         if(XX .GE. X(K+1)) then
         
           K = K+1
           
           if(K .NE. N) CYCLE
           
           K = N - 1
           
           S = Y(N) + (DY(K) + DX(K)*A(K))*(XX-X(N))
           
           RETURN
           
         end if
         
         ! if(XX .LT. X(K+1)) then EXIT do loop
         EXIT
         
       end do
       
       W = (XX - X(K))/DX(K)
       
       V = 1.00 - W
       
       S = W*Y(K+1) + V*Y(K) + (DX(K)**2)*((W**3 - W)*A(K+1) + (V**3 - V)*A(K))
       
       RETURN
           
END SUBROUTINE
    
END MODULE[/plain]
0 Kudos
3 Replies
Les_Neilson
Valued Contributor II
527 Views
Put the arrays before the contains and make them allocatable.

Module SplineFit

real, allocatable :: X(:)
real, allocatable :: Y(:)
real, allocatable :: DX(:)
real, allocatable :: DY(:)
real, allocatable :: A(:)


CONTAINS
Subroutine SplineSetup(N)

real T(N) ! Automatic local array

! remove the declarations for X,Y,DX,DY,A
! They will be available from host association

! Allocate the arrays.
! If you are calling this routine many times with different values of N you will need to
! deallocate any existing allocations.
! It is also a good idea to check for allocate errors as a precaution.

integer :: ierr
allocate(X(N),stat=ierr)
repeat for Y, DX, DY and A


remove the declarations of real X, Y DX, DY and A from Subroutine Spline, they will be available via host association once they have been allocated by SplineSetup

Les
0 Kudos
gelarimer
Beginner
527 Views

Les, yourexplanation was easy to follow, thank you.
0 Kudos
tropfen
New Contributor I
527 Views
if you want to keep the arrays only useable inside the module add ',private' in the declaration.

real, allocatable,private :: X(:)

Frank
0 Kudos
Reply