xref: /petsc/doc/manual/fortran.md (revision b06eb4cd3db6f436e3907d9ad23211c2914d8916)
17f296bb3SBarry Smith(ch_fortran)=
27f296bb3SBarry Smith
37f296bb3SBarry Smith# PETSc for Fortran Users
47f296bb3SBarry Smith
57f296bb3SBarry SmithMake sure the suffix of your Fortran files is .F90, not .f or .f90.
67f296bb3SBarry Smith
7*b06eb4cdSBarry Smith## Fortran and MPI
8*b06eb4cdSBarry Smith
9*b06eb4cdSBarry SmithBy default PETSc uses the MPI Fortran module `mpi`. To use `mpi_f08` run `./configure` with `--with-mpi-ftn-module=mpi_f08`.
10*b06eb4cdSBarry Smith
11*b06eb4cdSBarry SmithWe do not recommend it but it is possible to write Fortran code that works with both `use mpi` or `use mpi_f08`.
12*b06eb4cdSBarry SmithYou must declare MPI objects using `MPIU_XXX` (for example `MPIU_Comm`, which the PETSc include files map to either `integer4` or `type(MPI_XXX)`.
13*b06eb4cdSBarry SmithIn addition, you must handle `MPIU_Status` declarations and access to entries using the Fortran preprocessor. For example,
14*b06eb4cdSBarry Smith
15*b06eb4cdSBarry Smith```fortran
16*b06eb4cdSBarry Smith#if defined(PETSC_USE_MPI_F08)
17*b06eb4cdSBarry Smith  MPIU_Status status
18*b06eb4cdSBarry Smith#else
19*b06eb4cdSBarry Smith  MPIU_Status status(MPI_STATUS_SIZE)
20*b06eb4cdSBarry Smith#endif
21*b06eb4cdSBarry Smith```
22*b06eb4cdSBarry Smith
23*b06eb4cdSBarry Smithand then
24*b06eb4cdSBarry Smith
25*b06eb4cdSBarry Smith```fortran
26*b06eb4cdSBarry Smith#if defined(PETSC_USE_MPI_F08)
27*b06eb4cdSBarry Smith      tag = status%MPI_TAG
28*b06eb4cdSBarry Smith#else
29*b06eb4cdSBarry Smith      tag = status(MPI_TAG)
30*b06eb4cdSBarry Smith#endif
31*b06eb4cdSBarry Smith```
32*b06eb4cdSBarry Smith
33*b06eb4cdSBarry Smith## Numerical Constants
34*b06eb4cdSBarry Smith
35*b06eb4cdSBarry SmithSince Fortran compilers do not automatically change the length of numerical constant arguments (integer and real) in subroutine calls to the expected length PETSc provides parameters that indicate the constants' kind.
36*b06eb4cdSBarry Smith
37*b06eb4cdSBarry Smith```fortran
38*b06eb4cdSBarry Smith   interface
39*b06eb4cdSBarry Smith     subroutine SampleSubroutine(real, complex, integer, MPIinteger)
40*b06eb4cdSBarry Smith       PetscReal real         ! real(PETSC_REAL_KIND)
41*b06eb4cdSBarry Smith       PetscComplex complex   ! real(PETSC_REAL_KIND) or complex(PETSC_REAL_KIND)
42*b06eb4cdSBarry Smith       PetscInt integer       ! integer(PETSC_INT_KIND)
43*b06eb4cdSBarry Smith       PetscMPIInt MPIinteger ! integer(PETSC_MPIINT_KIND)
44*b06eb4cdSBarry Smith     end subroutine
45*b06eb4cdSBarry Smith   end interface
46*b06eb4cdSBarry Smith   ...
47*b06eb4cdSBarry Smith   ! Fortran () automatically sets the complex KIND to correspond to the KIND of constant arguments
48*b06eb4cdSBarry Smith   call SampleSubroutine(real = 1.0_PETSC_REAL_KIND, complex = (0.1_PETSC_REAL_KIND, 0.0_PETSC_REAL_KIND), integer = 1_PETSC_INT_KIND, MPIinteger = 1_PETSC_MPIINT_KIND)
49*b06eb4cdSBarry Smith   ! For variable arguments one must set the complex kind explicitly or it defaults to single precision
50*b06eb4cdSBarry Smith   PetscReal a = 0.1, b = 0.0
51*b06eb4cdSBarry Smith   call SampleSubroutine(real = 1.0_PETSC_REAL_KIND, complex = cmplx(a, b, PETSC_REAL_KIND), integer = 1_PETSC_INT_KIND, MPIinteger = 1_PETSC_MPIINT_KIND)
52*b06eb4cdSBarry Smith```
53*b06eb4cdSBarry Smith
547f296bb3SBarry Smith## Basic Fortran API Differences
557f296bb3SBarry Smith
567f296bb3SBarry Smith(sec_fortran_includes)=
577f296bb3SBarry Smith
587f296bb3SBarry Smith### Modules and Include Files
597f296bb3SBarry Smith
607f296bb3SBarry SmithYou must use both PETSc include files and modules.
617f296bb3SBarry SmithAt the beginning of every function and module definition you need something like
627f296bb3SBarry Smith
637f296bb3SBarry Smith```fortran
647f296bb3SBarry Smith#include "petsc/finclude/petscXXX.h"
657f296bb3SBarry Smith   use petscXXX
667f296bb3SBarry Smith```
677f296bb3SBarry Smith
687f296bb3SBarry SmithThe Fortran include files for PETSc are located in the directory
69*b06eb4cdSBarry Smith`$PETSC_DIR/$PETSC_ARCH/include/petsc/finclude` and the module files are located in `$PETSC_DIR/$PETSC_ARCH/include`
707f296bb3SBarry Smith
717f296bb3SBarry SmithThe include files are nested, that is, for example, `petsc/finclude/petscmat.h` automatically includes
72*b06eb4cdSBarry Smith`petsc/finclude/petscvec.h` and so on. The modules are also nested. One can use
737f296bb3SBarry Smith
747f296bb3SBarry Smith```c
757f296bb3SBarry Smithuse petsc
767f296bb3SBarry Smith```
777f296bb3SBarry Smith
78*b06eb4cdSBarry Smithto include all of them.
797f296bb3SBarry Smith
807f296bb3SBarry Smith### Declaring PETSc Object Variables
817f296bb3SBarry Smith
827f296bb3SBarry SmithYou can declare PETSc object variables using either of the following:
837f296bb3SBarry Smith
847f296bb3SBarry Smith```fortran
857f296bb3SBarry SmithXXX variablename
867f296bb3SBarry Smith```
877f296bb3SBarry Smith
887f296bb3SBarry Smith```fortran
897f296bb3SBarry Smithtype(tXXX) variablename
907f296bb3SBarry Smith```
917f296bb3SBarry Smith
927f296bb3SBarry SmithFor example,
937f296bb3SBarry Smith
947f296bb3SBarry Smith```fortran
957f296bb3SBarry Smith#include "petsc/finclude/petscvec.h"
967f296bb3SBarry Smith  use petscvec
977f296bb3SBarry Smith
987f296bb3SBarry Smith  Vec b
997f296bb3SBarry Smith  type(tVec) x
1007f296bb3SBarry Smith```
1017f296bb3SBarry Smith
1027f296bb3SBarry SmithPETSc types like `PetscInt` and `PetscReal` are simply aliases for basic Fortran types and cannot be written as `type(tPetscInt)`
1037f296bb3SBarry Smith
1047f296bb3SBarry SmithPETSc objects are always automatically initialized when declared so you do not need to (and should not) do
1057f296bb3SBarry Smith
1067f296bb3SBarry Smith```fortran
1077f296bb3SBarry Smithtype(tXXX) x = PETSC_NULL_XXX
1087f296bb3SBarry SmithXXX x = PETSC_NULL_XXX
1097f296bb3SBarry Smith```
1107f296bb3SBarry Smith
111a7a02aaeSBarry SmithTo make a variable no longer point to its previously assigned PETSc object use, for example,
112a7a02aaeSBarry Smith
113a7a02aaeSBarry Smith```fortran
114a7a02aaeSBarry Smith   Vec x, y
115a7a02aaeSBarry Smith   PetscInt one = 1
116a7a02aaeSBarry Smith   PetscCallA(VecCreateMPI(PETSC_COMM_WORLD, one, PETSC_DETERMINE, x, ierr))
117a7a02aaeSBarry Smith   y = x
118a7a02aaeSBarry Smith   PetscCallA(VecDestroy(x, ierr))
119a7a02aaeSBarry Smith   PetscObjectNullify(y)
120a7a02aaeSBarry Smith```
121a7a02aaeSBarry Smith
122a7a02aaeSBarry SmithOtherwise `y` will be a dangling pointer whose access will cause a crash.
123a7a02aaeSBarry Smith
124a7a02aaeSBarry Smith
1257f296bb3SBarry Smith### Calling Sequences
1267f296bb3SBarry Smith
1277f296bb3SBarry SmithThe calling sequences for the Fortran version are in most cases
1287f296bb3SBarry Smithidentical to the C version, except for the error checking variable
1297f296bb3SBarry Smithdiscussed in {any}`sec_fortran_errors`.
1307f296bb3SBarry Smith
1317f296bb3SBarry SmithThe key differences in handling arguments when calling PETSc functions from Fortran are
1327f296bb3SBarry Smith
1337f296bb3SBarry Smith- One cannot pass a scalar variable to a function expecting an array, {any}`sec_passarray`.
1347f296bb3SBarry Smith- One must use type specific `PETSC_NULL` arguments, such as `PETSC_NULL_INTEGER`, {any}`sec_nullptr`.
1357f296bb3SBarry Smith- One must pass pointers to arrays for arguments that output an array, for example `PetscScalar, pointer \:\: a(\:)`,
1367f296bb3SBarry Smith  {any}`sec_fortranarrays`.
1377f296bb3SBarry Smith- `PETSC_DECIDE` and friends need to match the argument type, for example `PETSC_DECIDE_INTEGER`.
1387f296bb3SBarry Smith
1397f296bb3SBarry SmithWhen passing floating point numbers into PETSc Fortran subroutines, always
1407f296bb3SBarry Smithmake sure you have them marked as double precision (e.g., pass in `10.d0`
1417f296bb3SBarry Smithinstead of `10.0` or declare them as PETSc variables, e.g.
1427f296bb3SBarry Smith`PetscScalar one = 1.0`). Otherwise, the compiler interprets the input as a single
1437f296bb3SBarry Smithprecision number, which can cause crashes or other mysterious problems.
1447f296bb3SBarry SmithWe **highly** recommend using the `implicit none`
1457f296bb3SBarry Smithoption at the beginning of each Fortran subroutine and declaring all variables.
1467f296bb3SBarry Smith
1477f296bb3SBarry Smith(sec_fortran_errors)=
1487f296bb3SBarry Smith
1497f296bb3SBarry Smith### Error Checking
1507f296bb3SBarry Smith
1517f296bb3SBarry SmithIn the Fortran version, each PETSc routine has as its final argument an
1527f296bb3SBarry Smithinteger error variable. The error code is
1537f296bb3SBarry Smithnonzero if an error has been detected; otherwise, it is zero. For
1547f296bb3SBarry Smithexample, the Fortran and C variants of `KSPSolve()` are given,
1557f296bb3SBarry Smithrespectively, below, where `ierr` denotes the `PetscErrorCode` error variable:
1567f296bb3SBarry Smith
1577f296bb3SBarry Smith```fortran
1587f296bb3SBarry Smithcall KSPSolve(ksp, b, x, ierr) ! Fortran
1597f296bb3SBarry Smithierr = KSPSolve(ksp, b, x);    // C
1607f296bb3SBarry Smith```
1617f296bb3SBarry Smith
1627f296bb3SBarry SmithFor proper error handling one should not use the above syntax instead one should use
1637f296bb3SBarry Smith
1647f296bb3SBarry Smith```fortran
1657f296bb3SBarry SmithPetscCall(KSPSolve(ksp, b, x, ierr))   ! Fortran subroutines
1667f296bb3SBarry SmithPetscCallA(KSPSolve(ksp, b, x, ierr))  ! Fortran main program
1677f296bb3SBarry SmithPetscCall(KSPSolve(ksp, b, x))         // C
1687f296bb3SBarry Smith```
1697f296bb3SBarry Smith
1707f296bb3SBarry Smith(sec_passarray)=
1717f296bb3SBarry Smith
1727f296bb3SBarry Smith### Passing Arrays To PETSc Functions
1737f296bb3SBarry Smith
1747f296bb3SBarry SmithMany PETSc functions take arrays as arguments; in Fortran they must be passed as arrays even if the "array"
1757f296bb3SBarry Smithis of length one (unlike Fortran 77 where one can pass scalars to functions expecting arrays). When passing
1767f296bb3SBarry Smitha single value one can use the Fortran [] notation to pass the scalar as an array, for example
1777f296bb3SBarry Smith
1787f296bb3SBarry Smith```fortran
1797f296bb3SBarry SmithPetscCall(VecSetValues(v, one, [i], [val], ierr))
1807f296bb3SBarry Smith```
1817f296bb3SBarry Smith
1827f296bb3SBarry SmithThis trick can only be used for arrays used to pass data into a PETSc routine, it cannot be used
1837f296bb3SBarry Smithfor arrays used to receive data from a PETSc routine. For example,
1847f296bb3SBarry Smith
1857f296bb3SBarry Smith```fortran
1867f296bb3SBarry SmithPetscCall(VecGetValues(v, one, idx, [val], ierr))
1877f296bb3SBarry Smith```
1887f296bb3SBarry Smith
1897f296bb3SBarry Smithis invalid and will not set `val` with the correct value.
1907f296bb3SBarry Smith
1917f296bb3SBarry Smith(sec_nullptr)=
1927f296bb3SBarry Smith
1937f296bb3SBarry Smith### Passing null pointers to PETSc functions
1947f296bb3SBarry Smith
1957f296bb3SBarry SmithMany PETSc C functions have the option of passing a `NULL`
1967f296bb3SBarry Smithargument (for example, the fifth argument of `MatCreateSeqAIJ()`).
1977f296bb3SBarry SmithFrom Fortran, users *must* pass `PETSC_NULL_XXX` to indicate a null
1987f296bb3SBarry Smithargument (where `XXX` is `INTEGER`, `DOUBLE`, `CHARACTER`,
1997c884152SBarry Smith`SCALAR`, `VEC`, `MAT`, etc depending on the argument type). For example, when no options prefix is desired
2007f296bb3SBarry Smithin the routine `PetscOptionsGetInt()`, one must use the following
2017f296bb3SBarry Smithcommand in Fortran:
2027f296bb3SBarry Smith
2037f296bb3SBarry Smith```fortran
2047f296bb3SBarry SmithPetscCall(PetscOptionsGetInt(PETSC_NULL_OPTIONS, PETSC_NULL_CHARACTER, PETSC_NULL_CHARACTER, '-name', N, flg, ierr))
2057f296bb3SBarry Smith```
2067f296bb3SBarry Smith
2077f296bb3SBarry SmithWhere the code expects an array, then use `PETSC_NULL_XXX_ARRAY`. For example:
2087f296bb3SBarry Smith
2097f296bb3SBarry Smith```fortran
2107f296bb3SBarry SmithPetscCall(MatCreateSeqDense(comm, m, n, PETSC_NULL_SCALAR_ARRAY, A))
2117f296bb3SBarry Smith```
2127f296bb3SBarry Smith
2137f296bb3SBarry SmithWhen a PETSc function returns multiple arrays, such as `DMDAGetOwnershipRanges()` and the user does not need
2147f296bb3SBarry Smithcertain arrays they must pass `PETSC_NULL_XXX_POINTER` as the argument. For example,
2157f296bb3SBarry Smith
2167f296bb3SBarry Smith```fortran
2177f296bb3SBarry SmithPetscInt, pointer :: lx(:), ly(:)
2187f296bb3SBarry SmithPetscCallA(DMDAGetOwnershipRanges(da, lx, ly, PETSC_NULL_INTEGER_POINTER, ierr))
2197f296bb3SBarry SmithPetscCallA(DMDARestoreOwnershipRanges(da, lx, ly, PETSC_NULL_INTEGER_POINTER, ierr))
2207f296bb3SBarry Smith```
2217f296bb3SBarry Smith
2227c884152SBarry SmithArguments that are fully defined Fortran derived types (C structs), such as `MatFactorInfo` or `PetscSFNode`,
2237c884152SBarry Smithcannot be passed as null from Fortran. A properly defined variable must be passed in for those arguments.
2247c884152SBarry Smith
2257f296bb3SBarry SmithFinally when a subroutine returns a `PetscObject` through an argument, to check if it is `NULL` you must use:
2267f296bb3SBarry Smith
2277f296bb3SBarry Smith```fortran
2287f296bb3SBarry Smithif (PetscObjectIsNull(dm)) then
2297f296bb3SBarry Smithif (.not. PetscObjectIsNull(dm)) then
2307f296bb3SBarry Smith```
2317f296bb3SBarry Smith
2327f296bb3SBarry Smithyou cannot use
2337f296bb3SBarry Smith
2347f296bb3SBarry Smith```fortran
2357f296bb3SBarry Smithif (dm .eq. PETSC_NULL_DM) then
2367f296bb3SBarry Smith```
2377f296bb3SBarry Smith
2387f296bb3SBarry SmithNote that
2397f296bb3SBarry Smith
2407f296bb3SBarry Smith```fortran
2417f296bb3SBarry Smithif (PetscObjectIsNull(PETSC_NULL_VEC)) then
2427f296bb3SBarry Smith```
2437f296bb3SBarry Smith
2447f296bb3SBarry Smithwill always return true, for any PETSc object.
2457f296bb3SBarry Smith
2467f296bb3SBarry SmithThese specializations with `NULL` types are required because of Fortran's strict type checking system and lack of a concept of `NULL`,
2477f296bb3SBarry Smiththe Fortran compiler will often warn you if the wrong `NULL` type is passed.
2487f296bb3SBarry Smith
2497f296bb3SBarry Smith(sec_fortranarrays)=
2507f296bb3SBarry Smith
2517f296bb3SBarry Smith### Output Arrays from PETSc functions
2527f296bb3SBarry Smith
2537f296bb3SBarry SmithFor PETSc routine arguments that return an array of `PetscInt`, `PetscScalar`, `PetscReal` or of PETSc objects,
2547f296bb3SBarry Smithone passes in a pointer to an array and the PETSc routine returns an array containing the values. For example,
2557f296bb3SBarry Smith
2567f296bb3SBarry Smith```c
2577f296bb3SBarry SmithPetscScalar *a;
2587f296bb3SBarry SmithVec         v;
2597f296bb3SBarry SmithVecGetArray(v, &a);
2607f296bb3SBarry Smith```
2617f296bb3SBarry Smith
2627f296bb3SBarry Smithis in Fortran,
2637f296bb3SBarry Smith
2647f296bb3SBarry Smith```fortran
2657f296bb3SBarry SmithPetscScalar, pointer :: a(:)
2667f296bb3SBarry SmithVec,         v
2677f296bb3SBarry SmithVecGetArray(v, a, ierr)
2687f296bb3SBarry Smith```
2697f296bb3SBarry Smith
2707f296bb3SBarry SmithFor PETSc routine arguments that return a character string (array), e.g. `const char *str[]` pass a string long enough to hold the
2717f296bb3SBarry Smithresult. For example,
2727f296bb3SBarry Smith
2737f296bb3SBarry Smith```fortran
2747f296bb3SBarry Smithcharacter*(80)  str
2757f296bb3SBarry SmithPetscCall(KSPGetType(ksp,str,ierr))
2767f296bb3SBarry Smith```
2777f296bb3SBarry Smith
2787f296bb3SBarry SmithThe result is copied into `str`.
2797f296bb3SBarry Smith
2807f296bb3SBarry SmithSimilarly, for PETSc routines where the user provides a character array (to be filled) followed by the array's length, e.g. `char name[], size_t nlen`.
2817f296bb3SBarry SmithIn Fortran pass a string long enough to hold the result, but not the separate length argument. For example,
2827f296bb3SBarry Smith
2837f296bb3SBarry Smith```fortran
2847f296bb3SBarry Smithcharacter*(80)  str
2857f296bb3SBarry SmithPetscCall(PetscGetHostName(name,ierr))
2867f296bb3SBarry Smith```
2877f296bb3SBarry Smith
2887f296bb3SBarry Smith### Matrix, Vector and IS Indices
2897f296bb3SBarry Smith
2907f296bb3SBarry SmithAll matrices, vectors and `IS` in PETSc use zero-based indexing in the PETSc API
2917f296bb3SBarry Smithregardless of whether C or Fortran is being used. For example,
2927f296bb3SBarry Smith`MatSetValues()` and `VecSetValues()` always use
2937f296bb3SBarry Smithzero indexing. See {any}`sec_matoptions` for further
2947f296bb3SBarry Smithdetails.
2957f296bb3SBarry Smith
2967f296bb3SBarry SmithIndexing into Fortran arrays, for example obtained with `VecGetArray()`, uses the Fortran
2977f296bb3SBarry Smithconvention and generally begin with 1 except for special routines such as `DMDAVecGetArray()` which uses the ranges
2987f296bb3SBarry Smithprovided by `DMDAGetCorners()`.
2997f296bb3SBarry Smith
3007f296bb3SBarry Smith### Setting Routines and Contexts
3017f296bb3SBarry Smith
3027f296bb3SBarry SmithSome PETSc functions take as arguments user-functions and contexts for the function. For example
3037f296bb3SBarry Smith
3047f296bb3SBarry Smith```fortran
3057f296bb3SBarry Smithexternal func
3067f296bb3SBarry SmithSNESSetFunction(snes, r, func, ctx, ierr)
3077f296bb3SBarry SmithSNES snes
3087f296bb3SBarry SmithVec r
3097f296bb3SBarry SmithPetscErrorCode ierr
3107f296bb3SBarry Smith```
3117f296bb3SBarry Smith
3127f296bb3SBarry Smithwhere `func` has the calling sequence
3137f296bb3SBarry Smith
3147f296bb3SBarry Smith```fortran
3157f296bb3SBarry Smithsubroutine func(snes, x, f, ctx, ierr)
3167f296bb3SBarry SmithSNES snes
3177f296bb3SBarry SmithVec x,f
3187f296bb3SBarry SmithPetscErrorCode ierr
3197f296bb3SBarry Smith```
3207f296bb3SBarry Smith
3217f296bb3SBarry Smithand `ctx` can be almost anything (represented as `void *` in C).
3227f296bb3SBarry Smith
3237f296bb3SBarry SmithIt can be a Fortran derived type as in
3247f296bb3SBarry Smith
3257f296bb3SBarry Smith```fortran
3267f296bb3SBarry Smithsubroutine func(snes, x, f, ctx, ierr)
3277f296bb3SBarry SmithSNES snes
3287f296bb3SBarry SmithVec x,f
3297f296bb3SBarry Smithtype (userctx)   ctx
3307f296bb3SBarry SmithPetscErrorCode ierr
3317f296bb3SBarry Smith...
3327f296bb3SBarry Smith
3337f296bb3SBarry Smithexternal func
3347f296bb3SBarry SmithSNESSetFunction(snes, r, func, ctx, ierr)
3357f296bb3SBarry SmithSNES snes
3367f296bb3SBarry SmithVec r
3377f296bb3SBarry SmithPetscErrorCode ierr
3387f296bb3SBarry Smithtype (userctx)   ctx
3397f296bb3SBarry Smith```
3407f296bb3SBarry Smith
3417f296bb3SBarry Smithor a PETSc object
3427f296bb3SBarry Smith
3437f296bb3SBarry Smith```fortran
3447f296bb3SBarry Smithsubroutine func(snes, x, f, ctx, ierr)
3457f296bb3SBarry SmithSNES snes
3467f296bb3SBarry SmithVec x,f
3477f296bb3SBarry SmithVec ctx
3487f296bb3SBarry SmithPetscErrorCode ierr
3497f296bb3SBarry Smith...
3507f296bb3SBarry Smith
3517f296bb3SBarry Smithexternal func
3527f296bb3SBarry SmithSNESSetFunction(snes, r, func, ctx, ierr)
3537f296bb3SBarry SmithSNES snes
3547f296bb3SBarry SmithVec r
3557f296bb3SBarry SmithPetscErrorCode ierr
3567f296bb3SBarry SmithVec ctx
3577f296bb3SBarry Smith```
3587f296bb3SBarry Smith
3597f296bb3SBarry Smithor nothing
3607f296bb3SBarry Smith
3617f296bb3SBarry Smith```fortran
3627f296bb3SBarry Smithsubroutine func(snes, x, f, dummy, ierr)
3637f296bb3SBarry SmithSNES snes
3647f296bb3SBarry SmithVec x,f
3657f296bb3SBarry Smithinteger dummy(*)
3667f296bb3SBarry SmithPetscErrorCode ierr
3677f296bb3SBarry Smith...
3687f296bb3SBarry Smith
3697f296bb3SBarry Smithexternal func
3707f296bb3SBarry SmithSNESSetFunction(snes, r, func, 0, ierr)
3717f296bb3SBarry SmithSNES snes
3727f296bb3SBarry SmithVec r
3737f296bb3SBarry SmithPetscErrorCode ierr
3747f296bb3SBarry Smith```
3757f296bb3SBarry Smith
3767f296bb3SBarry SmithWhen a function pointer (declared as external in Fortran) is passed as an argument to a PETSc function,
3777f296bb3SBarry Smithit is assumed that this
3787f296bb3SBarry Smithfunction references a routine written in the same language as the PETSc
3797f296bb3SBarry Smithinterface function that was called. For instance, if
3807f296bb3SBarry Smith`SNESSetFunction()` is called from C, the function must be a C function. Likewise, if it is called from Fortran, the
3817f296bb3SBarry Smithfunction must be (a subroutine) written in Fortran.
3827f296bb3SBarry Smith
3837f296bb3SBarry SmithIf you are using Fortran classes that have bound functions (methods) as in
3847f296bb3SBarry Smith<a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/snes/tests/ex18f90.F90.html">src/snes/tests/ex18f90.F90</a>, the context cannot be passed
3857f296bb3SBarry Smithto function pointer setting routines, such as `SNESSetFunction()`. Instead, one must use `SNESSetFunctionNoInterface()`,
3867f296bb3SBarry Smithand define the interface directly in the user code, see
3877f296bb3SBarry Smith<a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/snes/tests/ex18f90.F90.html">ex18f90.F90</a>
3887f296bb3SBarry Smithfor a full demonstration.
3897f296bb3SBarry Smith
3907f296bb3SBarry Smith(sec_fortcompile)=
3917f296bb3SBarry Smith
3927f296bb3SBarry Smith### Compiling and Linking Fortran Programs
3937f296bb3SBarry Smith
3947f296bb3SBarry SmithSee {any}`sec_writing_application_codes`.
3957f296bb3SBarry Smith
3967f296bb3SBarry Smith
3977f296bb3SBarry Smith(sec_fortran_examples)=
3987f296bb3SBarry Smith
3997f296bb3SBarry Smith## Sample Fortran Programs
4007f296bb3SBarry Smith
4017f296bb3SBarry SmithSample programs that illustrate the PETSc interface for Fortran are
4027f296bb3SBarry Smithgiven below, corresponding to
4037f296bb3SBarry Smith<a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/vec/vec/tests/ex19f.F90.html">Vec Test ex19f</a>,
4047f296bb3SBarry Smith<a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/vec/vec/tutorials/ex4f.F90.html">Vec Tutorial ex4f</a>,
4057f296bb3SBarry Smith<a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/sys/classes/draw/tests/ex5f.F90.html">Draw Test ex5f</a>,
4067f296bb3SBarry Smithand
4077f296bb3SBarry Smith<a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/snes/tutorials/ex1f.F90.html">SNES Tutorial ex1f</a>,
4087f296bb3SBarry Smithrespectively. We also refer Fortran programmers to the C examples listed
4097f296bb3SBarry Smiththroughout the manual, since PETSc usage within the two languages
4107f296bb3SBarry Smithdiffers only slightly.
4117f296bb3SBarry Smith
4127f296bb3SBarry Smith:::{admonition} Listing: `src/vec/vec/tests/ex19f.F90`
4137f296bb3SBarry Smith:name: vec-test-ex19f
4147f296bb3SBarry Smith
4157f296bb3SBarry Smith```{literalinclude} /../src/vec/vec/tests/ex19f.F90
4167f296bb3SBarry Smith:end-at: end
4177f296bb3SBarry Smith:language: fortran
4187f296bb3SBarry Smith```
4197f296bb3SBarry Smith:::
4207f296bb3SBarry Smith
4217f296bb3SBarry Smith(listing_vec_ex4f)=
4227f296bb3SBarry Smith
4237f296bb3SBarry Smith:::{admonition} Listing: `src/vec/vec/tutorials/ex4f.F90`
4247f296bb3SBarry Smith:name: vec-ex4f
4257f296bb3SBarry Smith
4267f296bb3SBarry Smith```{literalinclude} /../src/vec/vec/tutorials/ex4f.F90
4277f296bb3SBarry Smith:end-before: '!/*TEST'
4287f296bb3SBarry Smith:language: fortran
4297f296bb3SBarry Smith```
4307f296bb3SBarry Smith:::
4317f296bb3SBarry Smith
4327f296bb3SBarry Smith:::{admonition} Listing: `src/sys/classes/draw/tests/ex5f.F90`
4337f296bb3SBarry Smith:name: draw-test-ex5f
4347f296bb3SBarry Smith
4357f296bb3SBarry Smith```{literalinclude} /../src/sys/classes/draw/tests/ex5f.F90
4367f296bb3SBarry Smith:end-at: end
4377f296bb3SBarry Smith:language: fortran
4387f296bb3SBarry Smith```
4397f296bb3SBarry Smith:::
4407f296bb3SBarry Smith
4417f296bb3SBarry Smith:::{admonition} Listing: `src/snes/tutorials/ex1f.F90`
4427f296bb3SBarry Smith:name: snes-ex1f
4437f296bb3SBarry Smith
4447f296bb3SBarry Smith```{literalinclude} /../src/snes/tutorials/ex1f.F90
4457f296bb3SBarry Smith:end-before: '!/*TEST'
4467f296bb3SBarry Smith:language: fortran
4477f296bb3SBarry Smith```
4487f296bb3SBarry Smith:::
4497f296bb3SBarry Smith
4507f296bb3SBarry Smith### Calling Fortran Routines from C (and C Routines from Fortran)
4517f296bb3SBarry Smith
4527f296bb3SBarry SmithThe information here applies only if you plan to call your **own**
4537f296bb3SBarry SmithC functions from Fortran or Fortran functions from C.
4547f296bb3SBarry SmithDifferent compilers have different methods of naming Fortran routines
4557f296bb3SBarry Smithcalled from C (or C routines called from Fortran). Most Fortran
4567f296bb3SBarry Smithcompilers change the capital letters in Fortran routines to
4577f296bb3SBarry Smithall lowercase. With some compilers, the Fortran compiler appends an underscore
4587f296bb3SBarry Smithto the end of each Fortran routine name; for example, the Fortran
4597f296bb3SBarry Smithroutine `Dabsc()` would be called from C with `dabsc_()`. Other
4607f296bb3SBarry Smithcompilers change all the letters in Fortran routine names to capitals.
4617f296bb3SBarry Smith
4627f296bb3SBarry SmithPETSc provides two macros (defined in C/C++) to help write portable code
4637f296bb3SBarry Smiththat mixes C/C++ and Fortran. They are `PETSC_HAVE_FORTRAN_UNDERSCORE`
4647f296bb3SBarry Smithand `PETSC_HAVE_FORTRAN_CAPS` , which will be defined in the file
4657f296bb3SBarry Smith`$PETSC_DIR/$PETSC_ARCH/include/petscconf.h` based on the compilers
4667f296bb3SBarry Smithconventions. The macros are used,
4677f296bb3SBarry Smithfor example, as follows:
4687f296bb3SBarry Smith
4697f296bb3SBarry Smith```fortran
4707f296bb3SBarry Smith#if defined(PETSC_HAVE_FORTRAN_CAPS)
4717f296bb3SBarry Smith#define dabsc_ DABSC
4727f296bb3SBarry Smith#elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
4737f296bb3SBarry Smith#define dabsc_ dabsc
4747f296bb3SBarry Smith#endif
4757f296bb3SBarry Smith.....
4767f296bb3SBarry Smithdabsc_( &n,x,y); /* call the Fortran function */
4777f296bb3SBarry Smith```
478