Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replacing equivalence statements

Status
Not open for further replies.

Firebar

Programmer
Sep 6, 2005
3
GB
Hi,

Trying to modernise and tidy some old Fortran 77/90 code.
We have a scratch space in memory called restbuf_1 for a scientific model.

Currently our information array's are 2d and 3d, whereas the scratch is 1d. So we use Equivalence statements to slap the information from the 2d and 3d array's (rest2d_1 and rest3d_1) into the memory location of restbuf_1.

We'd like to move away from the whole equivalence thing so want to replace those chunks of code to perhaps be a bit more dynamic.

I'm perhaps having a bit of a blank day today, but does anyone have any suggestions on how to do this? Our original code is somewhat like;

real rest3d_1(jj,ll,kk) ! + Master + Model1
real rest2d_1(ll,kk) ! + Master + Model1
equivalence (restbuf_1(1),rest2d_1(1,1)) ! + Master + Model1
equivalence (restbuf_1(1),rest3d_1(1,1,1))

I understand that Pointer's and Target's from Fortran95 cannot be used to reference to an array of different dimensions.

Thanks for any help/suggestions.
 
The way dynamic arrays are done varies a lot. One way is to have a series of 1D arrays.

eg
threedp() is a set of pointers to pointers of reals. Each element points to an array of pointers.

twodp(n) is a set of pointers.
each element of twodp points to another 1D array which stores the values.

I'll see if I can find some coding examples.
 
Some sample code
Code:
subroutine Dump (arr, d1, d2, d3)
   integer, intent(in):: d1, d2, d3
   integer, intent(in):: arr(d1,d2, d3)
   integer:: i, j, k
   do k = 1, d3, 1
      do j = 1, d2, 1
         write (*, '(13I3)') (arr(i,j,k), i = 1, d1, 1)
      enddo
      print *
   enddo
   return 
end subroutine

subroutine Fill (arr, d1, d2, d3)
   integer, intent(in):: d1, d2, d3
   integer, intent(out):: arr(d1,d2,d3)
   integer:: i, j, k, ix
   ix = 1
   do k = 1, d3, 1
      do j = 1, d2, 1
         do i = 1, d1, 1
            arr(i,j,k) = ix
            ix = ix + 1
         end do
      end do
   end do

   return
end subroutine

program main
   integer, dimension(:,:,:), allocatable:: array
   integer:: D1MAX, D2MAX, D3MAX
   parameter (D1MAX = 6, D2MAX = 4, D3MAX = 4)

   allocate (array(D1MAX,D2MAX,D3MAX))
   print *, 'allocated' 
   call Fill (array, D1MAX, D2MAX, D3MAX)
   call Dump (array, D1MAX, D2MAX, D3MAX) 
   stop
end program
 
Thank you for your help, shall have a play around.
 
The example doesn't quite match what I said in the earlier posting. That was more of a C/C++ technique. I haven't figured out how to create an array of pointers in Fortran.

Don't forget to deallocate when you've finished otherwise you will end up with a gigantic memory leak.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top