I have two programs : 2D and 3D versions. For example, this is a simplified 2D version :
program main
integer, parameter :: nDim = 2
integer :: size
real, dimension(:,:,:), allocatable :: myArray
size=4 ! Normally read from a file
allocate(myArray(1:size,1:size,1:nDim))
call initArray(myArray)
contains
subroutine initArray(myArray)
real, dimension(1:size,1:size,1:nDim) :: myArray
myArray(1:size,1:size,1:nDim) = 5.d0
return
end subroutine initArray
end program main
And the 3D version,
program main
integer, parameter :: nDim = 3
integer :: size
real, dimension(:,:,:,:), allocatable :: myArray
size=4 ! Normally read from a file
allocate(myArray(1:size,1:size,1:size,1:nDim))
call initArray(myArray)
contains
subroutine initArray(myArray)
real, dimension(1:size,1:size,1:size,1:nDim) :: myArray
myArray(1:size,1:size,1:size,1:nDim) = 5.d0
return
end subroutine initArray
end program main
These programs are very similar and I would like to have only one program where the parameter nDim
determine everything.
But I have problem with the following statements and instructions :
- For
dimension
, I have not the same number of dimensions (3 or 4) - For
allocate
, the number of arguments is variable (3 or 4) - For initialisating
myArray
, the number of dimension is variable (3 or 4)
Is there a solution purely in fortran or should I use the C preprocessor ?
Thanks for answer.