I have a module in which I store 2D arrays (since my computational domain is a 2D grid) as a 1D vector since 99% of the other subroutines in my code work with 1D vectors. However, I have a subroutine MYsubr that works with 2D arrays , and since I am not aware of a way to reshape the vector into a matrix directly inside the "use module" I just pass it as an argument of the subroutine and then reshape it in the subroutine. Is there a better way to do this? My module is:
module myMODULE
integer,allocatable :: var(:)
.....
..... (thousands of other variables needed in mysubr)
end
and the main program calls a subroutine with "var" as an argument, but also var is recalled by the module myMODULE:
program main
implicit none
integer :: Nmax
integer :: Mmax
Nmax = 100
Mmax = 200
.....
allocate(var(Nmax*Mmax))
.....
.....
call MYsub(var,Nmax,Mmax)
end
subroutine MYsub(var,Nmax,Mmax)
use myMODULE
integer :: var(Nmax,Mmax)
.....
....,
end
In this way I can reshape var. What is the risk of this? It perfectly compiles. As long as I dont change the value of var in MYsub its clearly fine, what if I change it? should I use in MYsub this formulation to be safer:
use myMODULE, unused => var
so in this way I avoid aliasing the variable.