fortran - store multi dimensional arrays to be operated on along all the dimensions -
foreword
the fortran program i'm writing should deal 1d, 2d , 3d problems depending on ndims
, can 1, 2 or 3 , read input file.
in these cases quantity/ies of interest can stored in arrays (one named phi
)
- of rank
dims
(allocatable(:)
orallocatable(:,:)
orallocatable(:,:,:)
), - or in arrays of rank 3 (
allocatable(:,:,:)
third dimension set equal 1 in 2d or both second , third dimensions equal 1 in 1d);
both cases explained in this answer. first approach seems more elegant me, in following assume second one, simpler.
these quantities have operated on several subroutines (e.g. mysub
) along ndims
dimensions (along "pencils" should give graphic idea), should call like
select case (ndims) ! 3d case case (3) j = ... k = ... call mysub(phi(:,j,k)) end end = ... k = ... call mysub(phi(i,:,k)) end end = ... j = ... call mysub(phi(i,j,:)) end end ! 2d case case (2) j = ... k = ... call mysub(phi(:,j,1)) end end = ... k = ... call mysub(phi(i,:,1)) end end ! 1d case case (1) j = ... k = ... call mysub(phi(:,1,1)) end end end select
actual question
can suggest me (or me to devise!) different way of store phi
(maybe involving derived data types?) can collapse preceding code follows?
do id = 1, ndims call mysub2(phi,id) end
(here mysub2
acts on mysub
's place.)
so question how should store phi, can substitute first code second one?
maybe return foreword , decide follow point 1., in case easier write generic interface. be, think, way "hide" select case
do. of 2 (select case
/generic interface
) more efficient?
are these 2 ways face problem?
perhaps have misunderstood, think answer specific question not make changes storage or declaration of phi @ all.
in original code, 3 dimensional data (differentiating rank of data rank of array used store data) processed in slices along first dimension, second, third. 2 dimensional data processed along first, second, , 1 dimensional data processed along first only.
so id going 1 number of dimensions in data, consider following implementation of mysub2
:
subroutine mysub2(phi, id) type(pink_elephant), intent(in) :: phi(:,:,:) integer, intent(in) :: id integer :: i, j, k select case (id) case (1) j = ... k = ... call mysub(phi(:,j,k)) end end case (2) = ... k = ... call mysub(phi(i,:,k)) end end case (3) = ... j = ... call mysub(phi(i,j,:)) end end end select end subroutine mysub2
~~
generic interfaces can resolved @ "compile time" - specific procedure (not type bound) or binding (type bound) invoked particular call statement or function reference can determined looking @ declarations in code.
if have situation "runtime" information going affect choice of procedure, there has other executable mechanism, other or additional resolution of generic, comes play - if statement, select case, dynamic dispatch, etc, etc, etc.
asking whether generic resolution more efficient executable decision therefore not particularly meaningful - different things.
Comments
Post a Comment