Print

Print


J. Giles wrote :

>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.

Actually some compiler have the possibility of what Bertrand asked as an
extention with a different syntax. It is the "old" structure-union-map
originally from VAX-fortran. All modern fortrans derived from this (and
maybe others too) support it. DECC and Intel fortran support it.
The syntax is old (dating from the early F77 area) so I'm not sure if
ALLOCATABLE arrays can be components, but Bertrands examples would be easy:


structure /Phys_vars/
  union
    map
      real r(4)
    end map
    map
      real ro,ux,uy,uz
    end map
  end union
end structure

record /Phys_vars/ var

var.ro = 5.0
write(*,*) var.r(1)


        Jouk


Bush : All votes are equal but some votes are more equal than others.

>------------------------------------------------------------------------------<

  Jouk Jansen

  [log in to unmask]

  Technische Universiteit Delft        tttttttttt  uu     uu  ddddddd
  Kavli Institute of Nanoscience       tttttttttt  uu     uu  dd    dd
  Nationaal centrum voor HREM              tt      uu     uu  dd     dd
  Rotterdamseweg 137                       tt      uu     uu  dd     dd
  2628 AL Delft                            tt      uu     uu  dd     dd
  Nederland                                tt      uu     uu  dd    dd
  tel. 31-15-2782272                       tt       uuuuuuu   ddddddd

>------------------------------------------------------------------------------<