# Use not working?

**URL:** <https://fortran-lang.discourse.group/t/use-not-working/5392>\
**Category:** Help\
**Created:** [March 16, 2023, 11:19am UTC](https://fortran-lang.discourse.group/t/use-not-working/5392 "2023-03-16T11:19:23Z")\
**Posts on this page:** 15\
**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:** [March 16, 2023, 11:19am UTC](https://fortran-lang.discourse.group/t/use-not-working/5392/1 "2023-03-16T11:19:23Z")

</div>

I have the remnants of an include file still sitting in my code. In various functions and subroutines I have the include declaration to this file:

```auto
include 'xvar.h'

```

Inside the `xvar.h` file is this (this is only a segment of the file, for brevity):

```auto
      double precision :: nitrogen(mj), oxygen(mj)
      common/helium/nitrogen, oxygen

```

To help remove this I created a module in my `modules.f90` file:

```auto
module elements

	use stellar_values, only: DP, mj
	implicit none
    real(DP) :: nitrogen(mj), oxygen(mj)
    save
   
end module

```

I then simply `use` this module in the same functions/subroutines as had the `include` statement:

```auto
    use elements, only: nitrogen, oxygen

```

I remove references to `nitrogen` and `oxygen` from the `xvar.h` file, and the `common` block `helium` is not referenced in the remaining code anywhere.

When I run the code I should get the following results for each element:

```auto
.4177792D-04, .2703794D-03

```

Yet, I now get this…

```auto
.0000000D+00, .0000000D+00

```

This approach has worked elsewhere but not here?  
Any ideas?

I should add that the program later crashes on a `memory segmentation error`.

---

<div class="post-metadata">

**Author:** ![urbanjost](https://avatars.discourse-cdn.com/v4/letter/u/0ea827/32.png) [@urbanjost](https://fortran-lang.discourse.group/u/urbanjost)\
**Post date:** [March 16, 2023, 11:57am UTC](https://fortran-lang.discourse.group/t/use-not-working/5392/2 "2023-03-16T11:57:18Z")

</div>

were you initializing the arrays in a BLOCKDATA that you did not convert to be in the new module; or is “mj” the correct size? If you have not, turn on all debug flags, particularly array bound checks and traceback. If the size is not correct you could easily be going out of bounds;  
if the values are not being initialized that would explain the zeros.

---

<div class="post-metadata">

**Author:** ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)\
**Post date:** [March 16, 2023, 4:01pm UTC](https://fortran-lang.discourse.group/t/use-not-working/5392/3 "2023-03-16T16:01:10Z")

</div>

> [@urbanjost](#):
>
> … were you initializing the arrays in a BLOCKDATA …

+1

Say the INCLUDE file is as follows:

```fortran
      INTEGER MJ
      PARAMETER (MJ=2)
      DOUBLE PRECISION :: NITROGEN(MJ), OXYGEN(MJ)
      COMMON/HELIUM/NITROGEN, OXYGEN

```

And the legacy code may be as follows:

```fortran
      SUBROUTINE SUB()
      INCLUDE 'xvar.h'
      PRINT "(A,2(G11.6,1X))", "IN SUB: NITROGEN = ", NITROGEN
      END SUBROUTINE 
      BLOCK DATA
      INCLUDE 'xvar.h'
      DATA NITROGEN / .4177792D-04, .2703794D-03 /
      DATA OXYGEN / .4177792D-04, .2703794D-03 /
      END BLOCK DATA
      CALL SUB()
      END 

```

Now running a program based on this, a processor can generate this:

```auto
C:\temp>gfortran p.f -o p.exe

C:\temp>p.exe
IN SUB: NITROGEN = .417779E-04 .270379E-03

```

Refactoring the above using a `MODULE` can possibly like this:

```fortran
module xxx
   .
   integer, parameter :: MJ = 2
   real(DP) :: NITROGEN(MJ) = [.4177792E-04_dp, .2703794E-03_dp]
   real(DP) :: OXYGEN(MJ) = [.4177792E-04_dp, .2703794E-03_dp]
   .
end module 

```

But now if the values for each element are not to be modified, add the `PARAMETER` attribute to each of them:

```fortran
   .
   real(DP), parameter :: NITROGEN(MJ) = [.4177792E-04_dp, .2703794E-03_dp]
   .

```

---

<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:** [March 17, 2023, 8:18am UTC](https://fortran-lang.discourse.group/t/use-not-working/5392/4 "2023-03-17T08:18:19Z")

</div>

No, this was not in a BLOCKDATA. The full `xvar.h` file is this:

```auto
      double precision :: element(mj, me), deuterium(mj), helium3(mj), helium4(mj), carbon(mj)
      double precision :: lithium(mj), erest(mj), hydrogen(mj), nitrogen(mj), oxygen(mj)
      
      common/helium/hydrogen, deuterium, helium3, helium4, carbon, lithium, erest, nitrogen, oxygen

	  equivalence (hydrogen, element(1,1))

```

Whilst this is being `include`d the program works fine. If I remove `nitrogen` and `oxygen` from the above, then `use` the module instead (keeping the `xvar.h` include for the other variables), then the result is wrong.

---

<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:** [March 17, 2023, 8:24am UTC](https://fortran-lang.discourse.group/t/use-not-working/5392/5 "2023-03-17T08:24:09Z")

</div>

No, `nitrogen` and `oxygen` values change in a number of suboutined and functions and are not in a BLOCKDATA.

The zeroes are the constant output for those two variables, whilst all the other, relevant, variables (`element`, `helium`, etc) have varying values (as expected).

If I re-add `nitrogen` and `oxygen` to the `xvar.h` file then these, too, give me changing values.

Sorry, I should have been clearer on the fact that the values should be non-zero AND changing.

---

<div class="post-metadata">

**Author:** ![Arjen](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Arjen](https://fortran-lang.discourse.group/u/Arjen)\
**Post date:** [March 17, 2023, 9:00am UTC](https://fortran-lang.discourse.group/t/use-not-working/5392/6 "2023-03-17T09:00:53Z")

</div>

Well, what is missing from the module is the “equivalence” statement. This causes, together with the COMMON statement, the initialisation of the variables, if you initialise “element” somewhere. You will either have to use another “equivalence” statement (but then the “element” array must be seen as well) or set the initial values expressly. The latter is probably the easiest solution.

---

<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:** [March 17, 2023, 9:26am UTC](https://fortran-lang.discourse.group/t/use-not-working/5392/7 "2023-03-17T09:26:46Z")

</div>

But the `equivalence has no relevance to nitrogen or oxygen? Equally, though hydrogen is defined, it is never used anywhere in the program. Any reference to `hydrogen`has been replaced with the equivalent`element``` reference - which has worked fine.

The `equivalence` IS an issue, and one I am unpicking slowly. However, it should not affect the values of `nitrogen` or `oxygen`; should it?

---

<div class="post-metadata">

**Author:** ![Arjen](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Arjen](https://fortran-lang.discourse.group/u/Arjen)\
**Post date:** [March 17, 2023, 9:31am UTC](https://fortran-lang.discourse.group/t/use-not-working/5392/8 "2023-03-17T09:31:43Z")

</div>

Yes, it does: due to the COMMON statement the variables mentioned in there are “storage-related”. So, they occupy memory locations that are related. In other words:

element(1…mj,1) == hydrogen  
element(1…mj,2) == deuterium  
etc.

---

<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:** [March 17, 2023, 9:36am UTC](https://fortran-lang.discourse.group/t/use-not-working/5392/9 "2023-03-17T09:36:20Z")

</div>

And that makes perfect sense!

I had missed that about the COMMON statement and equivalence. I assume, therefore, that the order is directly related to their position in the COMMON block?

I had assumed, wrongly, that it only connected `hydrogen` with `element(1...mj, 1)` only.

Thank you. 🙂

---

<div class="post-metadata">

**Author:** ![Arjen](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Arjen](https://fortran-lang.discourse.group/u/Arjen)\
**Post date:** [March 17, 2023, 10:42am UTC](https://fortran-lang.discourse.group/t/use-not-working/5392/10 "2023-03-17T10:42:18Z")

</div>

Re order: yes, nothing magical there 🙂

---

<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:** [March 17, 2023, 12:46pm UTC](https://fortran-lang.discourse.group/t/use-not-working/5392/11 "2023-03-17T12:46:37Z")

</div>

This is now under test but it is looking good so far.

Thank you. If this does work (and it makes total sense, so it should) then I can resolve an issue that has been annoying me on and off for almost 2 years! Hahahahaha!

It is also the last remaining hurdle before I finalise the code and can focus entirely on the Python interface, alongside the actual science behind the whole thing.

Honestly, thank you so much for your help.

---

<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:** [March 17, 2023, 2:57pm UTC](https://fortran-lang.discourse.group/t/use-not-working/5392/12 "2023-03-17T14:57:25Z")

</div>

Just to state, this all worked!

Again, thank you. You have no idea how pleased I am. Project almost finished now.

That has been a stubborn remnant for almost 2 years!

---

<div class="post-metadata">

**Author:** ![Arjen](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Arjen](https://fortran-lang.discourse.group/u/Arjen)\
**Post date:** [March 17, 2023, 3:25pm UTC](https://fortran-lang.discourse.group/t/use-not-working/5392/13 "2023-03-17T15:25:30Z")

</div>

Good to hear that!

---

<div class="post-metadata">

**Author:** ![RonShepard](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@RonShepard](https://fortran-lang.discourse.group/u/RonShepard)\
**Post date:** [March 18, 2023, 1:31am UTC](https://fortran-lang.discourse.group/t/use-not-working/5392/14 "2023-03-18T01:31:04Z")

</div>

> [@garynewport](#):
>
> No, this was not in a BLOCKDATA.

If the values are not set in a block data, then how were they set? By assignment? By reading from an external data file?

---

<div class="post-metadata">

**Author:** ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)\
**Post date:** [March 18, 2023, 2:29am UTC](https://fortran-lang.discourse.group/t/use-not-working/5392/15 "2023-03-18T02:29:30Z")

</div>

> [@RonShepard](#):
>
> If the values are not set in a block data, then how were they set? …

As explained by @Arjen and confirmed by OP, the values are set by the storage association semantics c.f. section 19 of the Fortran standard.

Readers needing to support code which makes use of such semantics or looking to refactor them can use the following snippet to see a silly example of how the storage association comes into play via the `COMMON` block and the `EQUIVALENCE` statement:

```fortran
      SUBROUTINE SUB()
      REAL X(2), Y(2)
C X, Y objects follow consecutive storage sequence due to COMMON block DAT
      COMMON / DAT / X, Y
      REAL A(2,2)
C Equivalence of object X with a consecutive storage sequence of object A
C associates object Y as well
      EQUIVALENCE( X, A(1,1) )
      A(1,1) = -1.0
      A(2,1) = -2.0
      A(1,2) = 1.0
      A(2,2) = 2.0
      END SUBROUTINE
C
      PROGRAM P
      REAL X(2), Y(2)
      COMMON / DAT / X, Y
      CALL SUB()
C Y values get defined via storage association 
      PRINT *, "Y = ", Y
      END PROGRAM P

```

```auto
C:\temp>gfortran p.f -o p.exe

C:\temp>p.exe
 Y = 1.00000000 2.00000000

```
