Final removal of equivalence - is this a valid approach?

Sorry for all the posts on equivalence but each time I complete 1, the next one creates a slightly different issue.

I am down to the final one in my old code. I have an included file that defines the following…

integer, parameter :: mj = 4000
integer, parameter :: mh = 4
integer, parameter :: me = 9

double precision :: element(mj, me)
double precision :: velement(mj, me)

double precision :: hydrogen(mj), vhydrogen(mj)

equivalence (element,hydrogen) , (velement,vhydrogen)

If I then search the actual file that uses this include file, I have 55 for element, 14 for velement, 66 for hydrogen and no matches for vhydrogen.
That tells me that I am only interested in the equivalence (element,hydrogen)

If I look at some of the element lines I get this such as…

element(j, k) = velement(j, k)
element(i, k) = element(mj, k)
element(j + 1, k)  =  element(j, k)

And so on.

Considering there are 55 for element and 66 for hydrogen (though many of them are not assignments that affect either variable’s contents), I feel I need to add the following…

element(j, k) = velement(j, k)
hydrogen(j) = velement(j,k)

element(i, k) = element(mj, k)
hydrogen(i) = element(mj, k)

element(j + 1, k)  =  element(j, k)
hydrogen(j + 1) = element(j, k)

The only issue is that each of those lines are within a loop that alters k, whilst hydrogen does not vary by this index; so I am assuming I could just as easily do this…

hydrogen(j) = velement(j, 1)

hydrogen(i) = element(mj, 1)

hydrogen(j + 1) = element(j, 1)

Again, my only thought is that it shouldn’t be for (2) = 1 but for (2) = k, where k is at it’s maximum value (which is me). So…

hydrogen(j) = velement(j, me)

hydrogen(i) = element(mj, me)

hydrogen(j + 1) = element(j, me)

Am I right in my thinking here?

@garynewport , see this thread.

By the way, you’ve previously mentioned you now have several of the books on modern Fortran.

I suggest reviewing those books closely and gaining good facility with modern Fortran before proceeding with your code. Because going by your original post here suggests you might eliminate what might be marked as obsolescent in the standard i.e., EQUIVALENCE only to create data duplication in your code that might lead to adverse consequences.

1 Like

Thank you. I now remember reading this first time around, yet so much time has past!

I was reading around pointers, probably because I recalled your original reply, and will look at that again.

I want to move away from the includes and use modules instead. I then need to continue the journey through each subroutine to identify specifically what each one is doing. I’m about half way through at present.

And, yes, I am reading through the books. However, 99% of this code makes absolute sense and I can work out exactly what it is doing anyway. It’s when I hit curiousities and find the books not helping with specifics that I run to here. :slight_smile:

Again, thank you.