Bertrand Meltz wrote:
...
> I find this clumsy, and inconvenient at times.
> I am trying to put my physics variables inside a derived type,
> which could look like :
>
> type :: Phys_vars
> real, dimension(4) :: r
> real :: ro
> real :: ux
> real :: uy
> real :: uz
> equivalence (ro, r(1))
> equivalence (ux, r(2))
> equivalence (uy, r(3))
> equivalence (uz, r(4))
> end type Phys_vars
>
> This looks perfect, but it is not allowed by the standard.
Derived types with the SEQUENCE attribute can be EQUIVALENCED
with other types. If the components of the derived type are all numeric
of some intrinsic type (so their sizes are all in terms of numeric storage
units), the EQUIVALENCE even has a standard interpretation. So, you
might try:
type :: phys_vars
real :: ro
real :: ux
real :: uy
real :: uz
end type Phys_vars
Then for each variable of that type, also declare an array to equivalence
to it:
type(phys_vars) :: x
real :: xa(4)
equivalence (xa, x)
Unfortunately, this idea does not extend to all the places you might want
to use it. If X above is a dummy argument (or XA, for that matter) you
can't equivalence it. If the components of the derived type have the
ALLOCATABLE attribute, you can't equivalence it (this doesn't apply
to your simplified exaample, but you may have additional needs). There
are a lot of arcane restrictions. But, it does work if you have the variables
in a MODULE and use them where you need them.
--
J. Giles
"I conclude that there are two ways of constructing a software
design: One way is to make it so simple that there are obviously
no deficiencies and the other way is to make it so complicated
that there are no obvious deficiencies." -- C. A. R. Hoare
|