# Testing an equivalence with a module variable

**URL:** <https://fortran-lang.discourse.group/t/testing-an-equivalence-with-a-module-variable/5772>\
**Category:** Help\
**Created:** [May 7, 2023, 11:07am UTC](https://fortran-lang.discourse.group/t/testing-an-equivalence-with-a-module-variable/5772 "2023-05-07T11:07:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![garynewport](https://avatars.discourse-cdn.com/v4/letter/g/898d66/32.png) [@garynewport](https://fortran-lang.discourse.group/u/garynewport)\
**Post date:** [May 7, 2023, 11:07am UTC](https://fortran-lang.discourse.group/t/testing-an-equivalence-with-a-module-variable/5772/1 "2023-05-07T11:07:54Z")

</div>

Well, I thought I had finished the model, ran it for the maximum time I could think of and it keeps crashing. Through a series of error checking processes (each test taking several hours to complete) I have identified the following:

1. Certain variables are being set to `NaN`
2. The offence seems to be occurring within my `henyey` function
3. The issue seems to be with a variable I have that is shared across the model through a module

So, I have gone back to the very first code (for a variety of reasons) and return to a problem I asked about some time ago; an equivalence within this function.

Originally, this function was a subroutine and all variables were implied. The top section of the original code looked like this:

```auto
    subroutine henyey(mode)
    
        include 'parm.h'
        include 'xvar.h'
        logical lconv
        dimension d(mh,mh+1),ca(mh),dx(mh)
        equivalence (ha(1,mh+1),d(1,1)) , (d(1,mh+1),ca(1))
        save

```

`ha` (within the equivalence) was defined as `HA(MH,2*MH+1)` within the included file `xvar.h`

Originally, I replaced `ca(1)` in the code by accepting that `ca(i)` became `d(i, 5)` (since `mh` was defined as 4 in `parm.h`).  
I did the same with `d(1, 1)` and `ha(1, mh + 1)`; making `d(i, j)` into `ha(i, j + 4)`

However, I had a problem in that another subroutine needed `d` to be passed in (and taken out). I obviously hadn’t explored how I might transfer in the relevant section of `ha` (nor how to assign the results of this subroutine to that section - in the aim of making the subroutine a function). I simply did a loop to assign…

```auto
for i = 1 to mh
    for j = 1 to mh
        d(i, j) = ha(i, j + 4)
    end if
end if

call girl(d, m, n)

for i = 1 to mh
    for j = 1 to mh
        ha(i, j + 4) = d(i, j)
    end if
end if

```

I believe this assignment is wrong but also concerned that my other assumptions might be flawed.  
I am also curious if there is a way of changing the subroutine girl such that I pass in only the part of `ha` of interest, and girl returns the results, assigned to the relevant part of `ha`, such that girl becomes a function instead?

So, `call girl(ha, m, n)` and `call girl(d, m, n)` become `ha = girl(ha, m, n)` and either ````d = girl(d, m, n)`or something along the lines of `ha(1:i, 1:j) = girl(ha(1:i, 1:j), m, n)```

I’ve been trying to test the old code in my model, but since I removed the use of includes, moving to `use` instead, then I cannot test using the `equivalence` statement; getting the error `EQUIVALENCE attribute conflicts with USE ASSOCIATED attribute in 'ha' at...`

Any help gratefully received as always. I think I have included sufficient detail but happy to provide more if needed.

---

<div class="post-metadata">

**Author:** ![Jcollins](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/jcollins/32/540_2.png) [@Jcollins](https://fortran-lang.discourse.group/u/Jcollins)\
**Post date:** [May 7, 2023, 3:00pm UTC](https://fortran-lang.discourse.group/t/testing-an-equivalence-with-a-module-variable/5772/2 "2023-05-07T15:00:39Z")

</div>

Are you sure that you have the array indices the right way round? This is what I think is the memory map of your EQUIVALENCE block:

Address ha d c

0 (1,1) o-o-b o-o-b  
1 (2,1) o-o-b o-o-b  
2 (3,1) o-o-b o-o-b  
3 (4,1) o-o-b o-o-b  
4 (1,2) o-o-b o-o-b  
:  
:  
14 (3,4) o-o-b o-o-b  
15 (4,4) o-o-b o-o-b  
16 (1,5) (1,1) o-o-b  
17 (2,5) (2,1) o-o-b  
18 (3,5) (3,1) o-o-b  
19 (4,5) (4,1) o-o-b  
20 o-o-b (1,2) o-o-b  
22 o-o-b (2,2) o-o-b  
23 o-o-b (3.2) o-o-b  
24 o-o-b (4,2) o-o-b  
:  
:  
31 o-o-b (4,4) o-o-b  
32 o-o-b (1,5) (1)  
33 o-o-b (1,5) (2)  
34 o-o-b (1,5) (3)  
35 o-o-b (1,5) (4)  
36 o-o-b o-o-b (5)

where o-o-b means out-of-bounds.

---

<div class="post-metadata">

**Author:** ![garynewport](https://avatars.discourse-cdn.com/v4/letter/g/898d66/32.png) [@garynewport](https://fortran-lang.discourse.group/u/garynewport)\
**Post date:** [May 7, 2023, 3:17pm UTC](https://fortran-lang.discourse.group/t/testing-an-equivalence-with-a-module-variable/5772/3 "2023-05-07T15:17:04Z")

</div>

I think that is really my question. 🙂

I know I have seen somewhere (probably in one of the replies I have received), where the transference from one array to another requires the indices to be swapped.
