fortran66のブログ

fortran について書きます。

SELECT CLASS

Intel Fortran の v.11.1 が出ました。まだ完全ではありませんが、ようやく Fortran2003 のオブジェクト指向部分にも対応が進みました。

Fortran95/2003 Explained 16.5 から、サンプル例を実行できるように補完したものば・・

実行結果

ソース

!================================= Fortran95/2003 Explained 16.5
MODULE m_test
IMPLICIT NONE
!
TYPE :: vector
 REAL :: x, y, z
END TYPE vector
!
TYPE :: particle
 REAL :: mass
 TYPE(vector) :: position, velocity
END TYPE particle
!
TYPE, EXTENDS(particle) :: charged_particle
 REAL :: charge
END TYPE charged_particle
!
CONTAINS
!------------------------------------
SUBROUTINE descrive_vector(text, vec)
CHARACTER(*), INTENT(IN) :: text
TYPE(vector), INTENT(IN) :: vec
PRINT '(1x, a, 3f10.5)', TRIM(text), vec%x, vec%y, vec%z
RETURN
END SUBROUTINE descrive_vector
!------------------------------------
SUBROUTINE describe_particle(p)
CLASS(particle), INTENT(IN) :: p
!
PRINT *
CALL descrive_vector('Position:', p%position)
CALL descrive_vector('Velocity:', p%velocity)
PRINT *, 'Mass    :', p%mass
!
SELECT TYPE (p)
 TYPE IS (charged_particle)
  PRINT *, 'Charge  :', p%charge
 CLASS IS (charged_particle)
  PRINT *, 'Charge  :', p%charge
  PRINT *, '... may have other (unknown) attributes. '
 TYPE IS (particle)
  !
 CLASS default
  PRINT *, '... may have other (unknown) attributes. '
 END SELECT
!
RETURN
END SUBROUTINE describe_particle
!------------------------------------
END MODULE m_test
!======================================
PROGRAM test
USE m_test
IMPLICIT NONE
TYPE, EXTENDS(particle) :: new_particle0
END TYPE new_particle0
TYPE, EXTENDS(charged_particle) :: new_particle1
END TYPE new_particle1
!
TYPE (particle) :: p0
TYPE (charged_particle) :: p1
TYPE (new_particle0) :: q0
TYPE (new_particle1) :: q1
!
CALL describe_particle(p0)
CALL describe_particle(p1)
CALL describe_particle(q0)
CALL describe_particle(q1)
!
STOP
END PROGRAM test