xref: /petsc/src/snes/tests/ex18f90.F90 (revision 77d968b72e8e27b79bcc994c018975de390644ed)
1c4762a1bSJed Brown!
2c4762a1bSJed Brown! Example usage of Fortran 2003/2008 classes (extended derived types) as
3c4762a1bSJed Brown! user-defined contexts in PETSc. Example contributed by Glenn Hammond.
4c4762a1bSJed Brown!
5*77d968b7SBarry Smithmodule ex18f90base_module
6c4762a1bSJed Brown#include "petsc/finclude/petscsnes.h"
7c4762a1bSJed Brown      implicit none
8c4762a1bSJed Brown  private
9c4762a1bSJed Brown
10c4762a1bSJed Brown  type, public :: base_type
11c4762a1bSJed Brown    PetscInt :: A  ! junk
12c4762a1bSJed Brown    PetscReal :: I ! junk
13c4762a1bSJed Brown  contains
14c4762a1bSJed Brown    procedure, public :: Print => BasePrint
15c4762a1bSJed Brown  end type base_type
16c4762a1bSJed Browncontains
17c4762a1bSJed Brownsubroutine BasePrint(this)
18c4762a1bSJed Brown  implicit none
19c4762a1bSJed Brown  class(base_type) :: this
20c4762a1bSJed Brown  print *
21c4762a1bSJed Brown  print *, 'Base printout'
22c4762a1bSJed Brown  print *
23c4762a1bSJed Brownend subroutine BasePrint
24*77d968b7SBarry Smithend module ex18f90base_module
25c4762a1bSJed Brown
26*77d968b7SBarry Smithmodule ex18f90extended_module
27*77d968b7SBarry Smith  use ex18f90base_module
28c4762a1bSJed Brown  implicit none
29c4762a1bSJed Brown  private
30c4762a1bSJed Brown  type, public, extends(base_type) :: extended_type
31c4762a1bSJed Brown    PetscInt :: B  ! junk
32c4762a1bSJed Brown    PetscReal :: J ! junk
33c4762a1bSJed Brown  contains
34c4762a1bSJed Brown    procedure, public :: Print =>  ExtendedPrint
35c4762a1bSJed Brown  end type extended_type
36c4762a1bSJed Browncontains
37c4762a1bSJed Brownsubroutine ExtendedPrint(this)
38c4762a1bSJed Brown  implicit none
39c4762a1bSJed Brown  class(extended_type) :: this
40c4762a1bSJed Brown  print *
41c4762a1bSJed Brown  print *, 'Extended printout'
42c4762a1bSJed Brown  print *
43c4762a1bSJed Brownend subroutine ExtendedPrint
44*77d968b7SBarry Smithend module ex18f90extended_module
45c4762a1bSJed Brown
46*77d968b7SBarry Smithmodule ex18f90function_module
47c4762a1bSJed Brown  use petscsnes
48c4762a1bSJed Brown  implicit none
49c4762a1bSJed Brown  public :: TestFunction
50c4762a1bSJed Brown  contains
51c4762a1bSJed Brownsubroutine TestFunction(snes,xx,r,ctx,ierr)
52*77d968b7SBarry Smith  use ex18f90base_module
53c4762a1bSJed Brown  implicit none
54c4762a1bSJed Brown  SNES :: snes
55c4762a1bSJed Brown  Vec :: xx
56c4762a1bSJed Brown  Vec :: r
57c4762a1bSJed Brown  class(base_type) :: ctx ! yes, this should be base_type in order to handle all
58c4762a1bSJed Brown  PetscErrorCode :: ierr  ! polymorphic extensions
59c4762a1bSJed Brown  call ctx%Print()
60c4762a1bSJed Brownend subroutine TestFunction
61*77d968b7SBarry Smithend module ex18f90function_module
62c4762a1bSJed Brown
63c4762a1bSJed Brownprogram ex18f90
64c4762a1bSJed Brown
65*77d968b7SBarry Smith  use ex18f90base_module
66*77d968b7SBarry Smith  use ex18f90extended_module
67*77d968b7SBarry Smith  use ex18f90function_module
68c4762a1bSJed Brown  implicit none
69c4762a1bSJed Brown
70c4762a1bSJed Brown! ifort on windows requires this interface definition
71c4762a1bSJed Browninterface
72c4762a1bSJed Brown  subroutine SNESSetFunction(snes_base,x,TestFunction,base,ierr)
73*77d968b7SBarry Smith    use ex18f90base_module
74c4762a1bSJed Brown    use petscsnes
75c4762a1bSJed Brown    SNES snes_base
76c4762a1bSJed Brown    Vec x
77c4762a1bSJed Brown    external TestFunction
78c4762a1bSJed Brown    class(base_type) :: base
79c4762a1bSJed Brown    PetscErrorCode ierr
80c4762a1bSJed Brown  end subroutine
81c4762a1bSJed Brownend interface
82c4762a1bSJed Brown
83c4762a1bSJed Brown  PetscMPIInt :: size
84c4762a1bSJed Brown  PetscMPIInt :: rank
85c4762a1bSJed Brown
86c4762a1bSJed Brown  SNES :: snes_base, snes_extended
87c4762a1bSJed Brown  Vec :: x
88c4762a1bSJed Brown  class(base_type), pointer :: base
89c4762a1bSJed Brown  class(extended_type), pointer :: extended
90c4762a1bSJed Brown  PetscErrorCode :: ierr
91c4762a1bSJed Brown
92c4762a1bSJed Brown  print *, 'Start of Fortran2003 test program'
93c4762a1bSJed Brown
94c4762a1bSJed Brown  nullify(base)
95c4762a1bSJed Brown  nullify(extended)
96c4762a1bSJed Brown  allocate(base)
97c4762a1bSJed Brown  allocate(extended)
98d8606c27SBarry Smith  PetscCallA(PetscInitialize(ierr))
99d8606c27SBarry Smith  PetscCallMPIA(MPI_Comm_size(PETSC_COMM_WORLD,size,ierr))
100d8606c27SBarry Smith  PetscCallMPIA(MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr))
101c4762a1bSJed Brown
102d8606c27SBarry Smith  PetscCallA(VecCreate(PETSC_COMM_WORLD,x,ierr))
103c4762a1bSJed Brown
104c4762a1bSJed Brown  ! use the base class as the context
105c4762a1bSJed Brown  print *
106c4762a1bSJed Brown  print *, 'the base class will succeed by printing out Base printout below'
107d8606c27SBarry Smith  PetscCallA(SNESCreate(PETSC_COMM_WORLD,snes_base,ierr))
108d8606c27SBarry Smith  PetscCallA(SNESSetFunction(snes_base,x,TestFunction,base,ierr))
109d8606c27SBarry Smith  PetscCallA(SNESComputeFunction(snes_base,x,x,ierr))
110d8606c27SBarry Smith  PetscCallA(SNESDestroy(snes_base,ierr))
111c4762a1bSJed Brown
112c4762a1bSJed Brown  ! use the extended class as the context
113c4762a1bSJed Brown  print *, 'the extended class will succeed by printing out Extended printout below'
114d8606c27SBarry Smith  PetscCallA(SNESCreate(PETSC_COMM_WORLD,snes_extended,ierr))
115d8606c27SBarry Smith  PetscCallA(SNESSetFunction(snes_extended,x,TestFunction,extended,ierr))
116d8606c27SBarry Smith  PetscCallA(SNESComputeFunction(snes_extended,x,x,ierr))
117d8606c27SBarry Smith  PetscCallA(VecDestroy(x,ierr))
118d8606c27SBarry Smith  PetscCallA(SNESDestroy(snes_extended,ierr))
119c4762a1bSJed Brown  if (associated(base)) deallocate(base)
120c4762a1bSJed Brown  if (associated(extended)) deallocate(extended)
121d8606c27SBarry Smith  PetscCallA(PetscFinalize(ierr))
122c4762a1bSJed Brown
123c4762a1bSJed Brown  print *, 'End of Fortran2003 test program'
124c4762a1bSJed Brownend program ex18f90
125c4762a1bSJed Brown
126c4762a1bSJed Brown!/*TEST
127c4762a1bSJed Brown!
128c4762a1bSJed Brown!   build:
129dfd57a17SPierre Jolivet!      requires: defined(PETSC_USING_F2003) defined(PETSC_USING_F90FREEFORM)
130c4762a1bSJed Brown!   test:
131c4762a1bSJed Brown!     requires: !pgf90_compiler
132c4762a1bSJed Brown!
133c4762a1bSJed Brown!TEST*/
134