Reputation: 321
I am trying to understand a Fortran code for an internship but I am a c++ programmer and I am stuck what this code bit does. can some one enlighten me?
real(kind=kind_real), pointer :: c(:,:,:) (I think this created a pointer to a matrix )
self%c => self%gfld3d(:,:,ioff+1:) (this is in the constructor of the class)
flds%c(ibx,iby,2:flds%nc*flds%nl:2) (this is a function that calls the c variable of the class but I don't understand how the parameters work and what does the colon operator do as a a parameter)
Upvotes: 2
Views: 667
Reputation: 133
In addition to the comments: Unless specifically defined otherwise, fortran arrays always start with element number 1 and then have elements numbers 1 up to the dimension.
real(kind=kind_real), dimension(3) :: a
real(kind=kind_real), dimension(0:2) :: b
real(kind=kind_real), dimension(5:7) :: c
These three static, real valued arrays all have three elements each. Array 'a' starts at element a(1), array b at element b(0) and c at element c(5)
Upvotes: 3
Reputation: 1209
real(kind=kind_real), pointer :: c(:,:,:)
As you already wrote, this line declares a pointer variable c
that points to a 3-dimensional array of real
numbers of the kind kind_real
. The colon here means that the sizes for each dimension are yet unknown. Assuming this is inside a type
block, c
is a member of the type. Notice that in Fortran, ()
is used both for array subscripts and for function calls.
self%c => self%gfld3d(:,:,ioff+1:)
In the constructor, the pointer is assigned a target, which is a portion of the 3-dimensional array self%gfld3d
, which is also a member of the same type. Pointers in Fortran contain information about the dimensions of the array slice they point to. In this case, the first two colons mean that the array slice spans the whole two first dimensions of the target array. ioff+1:
means that in the third dimension, the pointer target includes all values in the self%gfld3d
starting from the given value ioff+1
. This is a simplified case of the general slice notation, where a(start:end:step)
represents a slice of the array a
, starting from index start
, ending at end
, in steps of step
.
flds%c(ibx,iby,2:flds%nc*flds%nl:2)
Based on the above, this is not a function call. It is an expression that evaluates to a 1-dimensional, non-contiguous array slice. From the target of flds%c
, at indexes ibx
and iby
in the first two dimensions, it contains every second value in the third dimension, starting at the index 2 and stopping at flds%nc * flds%nl
. The first two dimensions in the output array are left out, since the indices in those dimensions are simple scalars. This expression cannot be on its own in a statement, so I assume you left some part of the line out in your question.
Upvotes: 6