Clarity on Equivalence

@garynewport ,

What has changed since the discussion in your threads around EQUIVALENCE over a year ago? For example, see this.

As to your original post here, note you have declared hydrogen as a rank-1 object, thus the reference to it as hydrogen(2,1) is invalid. The rest of your note looks mostly ok.

It appears you’re still struggling with legacy code in your PhD work and unable to quickly and fully refactor it to modern Fortran or to another language (Python/Julia) or even platform (MATLAB) and get to focus entirely on the science and computing in your research which is bothersome.

You may perhaps want to give a textbook another try, say the one by Nyhoff and Leestma, to get to the basics of this legacy code, especially the EQUIVALENCE semantics, in a way that you can finally have it in your rearview mirror.

Immediately on your questions, to make a long story short, you can see your hydrogen object as an alias to a particular column (the first one) in your rank-2 “data” array named element and thus any variable definition to the corresponding hydrogen array index value affects element array and vice versa. You can see this in the following snippet, give it a try and “play around with it”:

   integer, parameter :: me = 9
   integer, parameter :: mj = 4000
   double precision :: element(mj, me), hydrogen(mj)
   equivalence (hydrogen, element(1,1))
   element = 0D0
   element(:,1) = 99.0D0
   print *, "hydrogen(1) = ", hydrogen(1), "; expected is 99D0"
   print *, "hydrogen(1000) = ", hydrogen(1000), "; expected is 99D0"
   print *, "hydrogen(4000) = ", hydrogen(4000), "; expected is 99D0"
   hydrogen(999) = 42D0
   print *, "element(999,1) = ", element(999,1), "; expected is 42D0"
end 
C:\temp>gfortran p.f90 -o p.exe

C:\temp>p.exe
 hydrogen(1) =    99.000000000000000      ; expected is 99D0
 hydrogen(1000) =    99.000000000000000      ; expected is 99D0
 hydrogen(4000) =    99.000000000000000      ; expected is 99D0
 element(999,1) =    42.000000000000000      ; expected is 42D0

And note a modern Fortran code equivalent (bad pun intended) to above can be the following:

   integer, parameter :: me = 9
   integer, parameter :: mj = 4000
   double precision, target :: element(mj, me)
   double precision, pointer :: hydrogen(:)
   hydrogen => element(:,1)
   element = 0D0
   element(:,1) = 99.0D0
   print *, "hydrogen(1) = ", hydrogen(1), "; expected is 99D0"
   print *, "hydrogen(1000) = ", hydrogen(1000), "; expected is 99D0"
   print *, "hydrogen(4000) = ", hydrogen(4000), "; expected is 99D0"
   hydrogen(999) = 42D0
   print *, "element(999,1) = ", element(999,1), "; expected is 42D0"
end 
C:\temp>gfortran p.f90 -o p.exe

C:\temp>p.exe
 hydrogen(1) =    99.000000000000000      ; expected is 99D0
 hydrogen(1000) =    99.000000000000000      ; expected is 99D0
 hydrogen(4000) =    99.000000000000000      ; expected is 99D0
 element(999,1) =    42.000000000000000      ; expected is 42D0
1 Like