xref: /petsc/doc/manual/fortran.md (revision 7c884152b42c97ddc3072479f95dbd7f62e52288)
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
77f296bb3SBarry Smith## Basic Fortran API Differences
87f296bb3SBarry Smith
97f296bb3SBarry Smith(sec_fortran_includes)=
107f296bb3SBarry Smith
117f296bb3SBarry Smith### Modules and Include Files
127f296bb3SBarry Smith
137f296bb3SBarry SmithYou must use both PETSc include files and modules.
147f296bb3SBarry SmithAt the beginning of every function and module definition you need something like
157f296bb3SBarry Smith
167f296bb3SBarry Smith```fortran
177f296bb3SBarry Smith#include "petsc/finclude/petscXXX.h"
187f296bb3SBarry Smith   use petscXXX
197f296bb3SBarry Smith```
207f296bb3SBarry Smith
217f296bb3SBarry SmithThe Fortran include files for PETSc are located in the directory
227f296bb3SBarry Smith`$PETSC_DIR/include/petsc/finclude` and the module files are located in `$PETSC_DIR/$PETSC_ARCH/include`
237f296bb3SBarry Smith
247f296bb3SBarry SmithThe include files are nested, that is, for example, `petsc/finclude/petscmat.h` automatically includes
257f296bb3SBarry Smith`petsc/finclude/petscvec.h` and so on. Except for `petscsys` which is nested in the other modules,
267f296bb3SBarry Smithmodules are **not** nested. Thus if your routine uses, for example, both
277f296bb3SBarry Smith`Mat` and `Vec` operations you need
287f296bb3SBarry Smith
297f296bb3SBarry Smith```c
307f296bb3SBarry Smithuse petscvec
317f296bb3SBarry Smithuse petscmat
327f296bb3SBarry Smith```
337f296bb3SBarry Smith
347f296bb3SBarry SmithThe reason they are not nested is that they are very large and including all of them slows down the compile time.
357f296bb3SBarry SmithOne can use
367f296bb3SBarry Smith
377f296bb3SBarry Smith```c
387f296bb3SBarry Smithuse petsc
397f296bb3SBarry Smith```
407f296bb3SBarry Smith
417f296bb3SBarry Smithto include all of them. In addition, if you have a routine that does not have function calls for an object, but has
427f296bb3SBarry Smiththe object as an argument you can use, for example,
437f296bb3SBarry Smith
447f296bb3SBarry Smith```c
457f296bb3SBarry Smithsubroutine FormFunction(snes,x,f,dummy,ierr)
467f296bb3SBarry Smith  use petscvec
477f296bb3SBarry Smith  use petscsnesdef
487f296bb3SBarry Smith  implicit none
497f296bb3SBarry Smith```
507f296bb3SBarry Smith
517f296bb3SBarry Smith### Declaring PETSc Object Variables
527f296bb3SBarry Smith
537f296bb3SBarry SmithYou can declare PETSc object variables using either of the following:
547f296bb3SBarry Smith
557f296bb3SBarry Smith```fortran
567f296bb3SBarry SmithXXX variablename
577f296bb3SBarry Smith```
587f296bb3SBarry Smith
597f296bb3SBarry Smith```fortran
607f296bb3SBarry Smithtype(tXXX) variablename
617f296bb3SBarry Smith```
627f296bb3SBarry Smith
637f296bb3SBarry SmithFor example,
647f296bb3SBarry Smith
657f296bb3SBarry Smith```fortran
667f296bb3SBarry Smith#include "petsc/finclude/petscvec.h"
677f296bb3SBarry Smith  use petscvec
687f296bb3SBarry Smith
697f296bb3SBarry Smith  Vec b
707f296bb3SBarry Smith  type(tVec) x
717f296bb3SBarry Smith```
727f296bb3SBarry Smith
737f296bb3SBarry SmithPETSc types like `PetscInt` and `PetscReal` are simply aliases for basic Fortran types and cannot be written as `type(tPetscInt)`
747f296bb3SBarry Smith
757f296bb3SBarry SmithPETSc objects are always automatically initialized when declared so you do not need to (and should not) do
767f296bb3SBarry Smith
777f296bb3SBarry Smith```fortran
787f296bb3SBarry Smithtype(tXXX) x = PETSC_NULL_XXX
797f296bb3SBarry SmithXXX x = PETSC_NULL_XXX
807f296bb3SBarry Smith```
817f296bb3SBarry Smith
827f296bb3SBarry Smith### Calling Sequences
837f296bb3SBarry Smith
847f296bb3SBarry SmithThe calling sequences for the Fortran version are in most cases
857f296bb3SBarry Smithidentical to the C version, except for the error checking variable
867f296bb3SBarry Smithdiscussed in {any}`sec_fortran_errors`.
877f296bb3SBarry Smith
887f296bb3SBarry SmithThe key differences in handling arguments when calling PETSc functions from Fortran are
897f296bb3SBarry Smith
907f296bb3SBarry Smith- One cannot pass a scalar variable to a function expecting an array, {any}`sec_passarray`.
917f296bb3SBarry Smith- One must use type specific `PETSC_NULL` arguments, such as `PETSC_NULL_INTEGER`, {any}`sec_nullptr`.
927f296bb3SBarry Smith- One must pass pointers to arrays for arguments that output an array, for example `PetscScalar, pointer \:\: a(\:)`,
937f296bb3SBarry Smith  {any}`sec_fortranarrays`.
947f296bb3SBarry Smith- `PETSC_DECIDE` and friends need to match the argument type, for example `PETSC_DECIDE_INTEGER`.
957f296bb3SBarry Smith
967f296bb3SBarry SmithWhen passing floating point numbers into PETSc Fortran subroutines, always
977f296bb3SBarry Smithmake sure you have them marked as double precision (e.g., pass in `10.d0`
987f296bb3SBarry Smithinstead of `10.0` or declare them as PETSc variables, e.g.
997f296bb3SBarry Smith`PetscScalar one = 1.0`). Otherwise, the compiler interprets the input as a single
1007f296bb3SBarry Smithprecision number, which can cause crashes or other mysterious problems.
1017f296bb3SBarry SmithWe **highly** recommend using the `implicit none`
1027f296bb3SBarry Smithoption at the beginning of each Fortran subroutine and declaring all variables.
1037f296bb3SBarry Smith
1047f296bb3SBarry Smith(sec_fortran_errors)=
1057f296bb3SBarry Smith
1067f296bb3SBarry Smith### Error Checking
1077f296bb3SBarry Smith
1087f296bb3SBarry SmithIn the Fortran version, each PETSc routine has as its final argument an
1097f296bb3SBarry Smithinteger error variable. The error code is
1107f296bb3SBarry Smithnonzero if an error has been detected; otherwise, it is zero. For
1117f296bb3SBarry Smithexample, the Fortran and C variants of `KSPSolve()` are given,
1127f296bb3SBarry Smithrespectively, below, where `ierr` denotes the `PetscErrorCode` error variable:
1137f296bb3SBarry Smith
1147f296bb3SBarry Smith```fortran
1157f296bb3SBarry Smithcall KSPSolve(ksp, b, x, ierr) ! Fortran
1167f296bb3SBarry Smithierr = KSPSolve(ksp, b, x);    // C
1177f296bb3SBarry Smith```
1187f296bb3SBarry Smith
1197f296bb3SBarry SmithFor proper error handling one should not use the above syntax instead one should use
1207f296bb3SBarry Smith
1217f296bb3SBarry Smith```fortran
1227f296bb3SBarry SmithPetscCall(KSPSolve(ksp, b, x, ierr))   ! Fortran subroutines
1237f296bb3SBarry SmithPetscCallA(KSPSolve(ksp, b, x, ierr))  ! Fortran main program
1247f296bb3SBarry SmithPetscCall(KSPSolve(ksp, b, x))         // C
1257f296bb3SBarry Smith```
1267f296bb3SBarry Smith
1277f296bb3SBarry Smith(sec_passarray)=
1287f296bb3SBarry Smith
1297f296bb3SBarry Smith### Passing Arrays To PETSc Functions
1307f296bb3SBarry Smith
1317f296bb3SBarry SmithMany PETSc functions take arrays as arguments; in Fortran they must be passed as arrays even if the "array"
1327f296bb3SBarry Smithis of length one (unlike Fortran 77 where one can pass scalars to functions expecting arrays). When passing
1337f296bb3SBarry Smitha single value one can use the Fortran [] notation to pass the scalar as an array, for example
1347f296bb3SBarry Smith
1357f296bb3SBarry Smith```fortran
1367f296bb3SBarry SmithPetscCall(VecSetValues(v, one, [i], [val], ierr))
1377f296bb3SBarry Smith```
1387f296bb3SBarry Smith
1397f296bb3SBarry SmithThis trick can only be used for arrays used to pass data into a PETSc routine, it cannot be used
1407f296bb3SBarry Smithfor arrays used to receive data from a PETSc routine. For example,
1417f296bb3SBarry Smith
1427f296bb3SBarry Smith```fortran
1437f296bb3SBarry SmithPetscCall(VecGetValues(v, one, idx, [val], ierr))
1447f296bb3SBarry Smith```
1457f296bb3SBarry Smith
1467f296bb3SBarry Smithis invalid and will not set `val` with the correct value.
1477f296bb3SBarry Smith
1487f296bb3SBarry Smith(sec_nullptr)=
1497f296bb3SBarry Smith
1507f296bb3SBarry Smith### Passing null pointers to PETSc functions
1517f296bb3SBarry Smith
1527f296bb3SBarry SmithMany PETSc C functions have the option of passing a `NULL`
1537f296bb3SBarry Smithargument (for example, the fifth argument of `MatCreateSeqAIJ()`).
1547f296bb3SBarry SmithFrom Fortran, users *must* pass `PETSC_NULL_XXX` to indicate a null
1557f296bb3SBarry Smithargument (where `XXX` is `INTEGER`, `DOUBLE`, `CHARACTER`,
156*7c884152SBarry Smith`SCALAR`, `VEC`, `MAT`, etc depending on the argument type). For example, when no options prefix is desired
1577f296bb3SBarry Smithin the routine `PetscOptionsGetInt()`, one must use the following
1587f296bb3SBarry Smithcommand in Fortran:
1597f296bb3SBarry Smith
1607f296bb3SBarry Smith```fortran
1617f296bb3SBarry SmithPetscCall(PetscOptionsGetInt(PETSC_NULL_OPTIONS, PETSC_NULL_CHARACTER, PETSC_NULL_CHARACTER, '-name', N, flg, ierr))
1627f296bb3SBarry Smith```
1637f296bb3SBarry Smith
1647f296bb3SBarry SmithWhere the code expects an array, then use `PETSC_NULL_XXX_ARRAY`. For example:
1657f296bb3SBarry Smith
1667f296bb3SBarry Smith```fortran
1677f296bb3SBarry SmithPetscCall(MatCreateSeqDense(comm, m, n, PETSC_NULL_SCALAR_ARRAY, A))
1687f296bb3SBarry Smith```
1697f296bb3SBarry Smith
1707f296bb3SBarry SmithWhen a PETSc function returns multiple arrays, such as `DMDAGetOwnershipRanges()` and the user does not need
1717f296bb3SBarry Smithcertain arrays they must pass `PETSC_NULL_XXX_POINTER` as the argument. For example,
1727f296bb3SBarry Smith
1737f296bb3SBarry Smith```fortran
1747f296bb3SBarry SmithPetscInt, pointer :: lx(:), ly(:)
1757f296bb3SBarry SmithPetscCallA(DMDAGetOwnershipRanges(da, lx, ly, PETSC_NULL_INTEGER_POINTER, ierr))
1767f296bb3SBarry SmithPetscCallA(DMDARestoreOwnershipRanges(da, lx, ly, PETSC_NULL_INTEGER_POINTER, ierr))
1777f296bb3SBarry Smith```
1787f296bb3SBarry Smith
179*7c884152SBarry SmithArguments that are fully defined Fortran derived types (C structs), such as `MatFactorInfo` or `PetscSFNode`,
180*7c884152SBarry Smithcannot be passed as null from Fortran. A properly defined variable must be passed in for those arguments.
181*7c884152SBarry Smith
1827f296bb3SBarry SmithFinally when a subroutine returns a `PetscObject` through an argument, to check if it is `NULL` you must use:
1837f296bb3SBarry Smith
1847f296bb3SBarry Smith```fortran
1857f296bb3SBarry Smithif (PetscObjectIsNull(dm)) then
1867f296bb3SBarry Smithif (.not. PetscObjectIsNull(dm)) then
1877f296bb3SBarry Smith```
1887f296bb3SBarry Smith
1897f296bb3SBarry Smithyou cannot use
1907f296bb3SBarry Smith
1917f296bb3SBarry Smith```fortran
1927f296bb3SBarry Smithif (dm .eq. PETSC_NULL_DM) then
1937f296bb3SBarry Smith```
1947f296bb3SBarry Smith
1957f296bb3SBarry SmithNote that
1967f296bb3SBarry Smith
1977f296bb3SBarry Smith```fortran
1987f296bb3SBarry Smithif (PetscObjectIsNull(PETSC_NULL_VEC)) then
1997f296bb3SBarry Smith```
2007f296bb3SBarry Smith
2017f296bb3SBarry Smithwill always return true, for any PETSc object.
2027f296bb3SBarry Smith
2037f296bb3SBarry SmithThese specializations with `NULL` types are required because of Fortran's strict type checking system and lack of a concept of `NULL`,
2047f296bb3SBarry Smiththe Fortran compiler will often warn you if the wrong `NULL` type is passed.
2057f296bb3SBarry Smith
2067f296bb3SBarry Smith(sec_fortranarrays)=
2077f296bb3SBarry Smith
2087f296bb3SBarry Smith### Output Arrays from PETSc functions
2097f296bb3SBarry Smith
2107f296bb3SBarry SmithFor PETSc routine arguments that return an array of `PetscInt`, `PetscScalar`, `PetscReal` or of PETSc objects,
2117f296bb3SBarry Smithone passes in a pointer to an array and the PETSc routine returns an array containing the values. For example,
2127f296bb3SBarry Smith
2137f296bb3SBarry Smith```c
2147f296bb3SBarry SmithPetscScalar *a;
2157f296bb3SBarry SmithVec         v;
2167f296bb3SBarry SmithVecGetArray(v, &a);
2177f296bb3SBarry Smith```
2187f296bb3SBarry Smith
2197f296bb3SBarry Smithis in Fortran,
2207f296bb3SBarry Smith
2217f296bb3SBarry Smith```fortran
2227f296bb3SBarry SmithPetscScalar, pointer :: a(:)
2237f296bb3SBarry SmithVec,         v
2247f296bb3SBarry SmithVecGetArray(v, a, ierr)
2257f296bb3SBarry Smith```
2267f296bb3SBarry Smith
2277f296bb3SBarry SmithFor PETSc routine arguments that return a character string (array), e.g. `const char *str[]` pass a string long enough to hold the
2287f296bb3SBarry Smithresult. For example,
2297f296bb3SBarry Smith
2307f296bb3SBarry Smith```fortran
2317f296bb3SBarry Smithcharacter*(80)  str
2327f296bb3SBarry SmithPetscCall(KSPGetType(ksp,str,ierr))
2337f296bb3SBarry Smith```
2347f296bb3SBarry Smith
2357f296bb3SBarry SmithThe result is copied into `str`.
2367f296bb3SBarry Smith
2377f296bb3SBarry 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`.
2387f296bb3SBarry SmithIn Fortran pass a string long enough to hold the result, but not the separate length argument. For example,
2397f296bb3SBarry Smith
2407f296bb3SBarry Smith```fortran
2417f296bb3SBarry Smithcharacter*(80)  str
2427f296bb3SBarry SmithPetscCall(PetscGetHostName(name,ierr))
2437f296bb3SBarry Smith```
2447f296bb3SBarry Smith
2457f296bb3SBarry Smith### Matrix, Vector and IS Indices
2467f296bb3SBarry Smith
2477f296bb3SBarry SmithAll matrices, vectors and `IS` in PETSc use zero-based indexing in the PETSc API
2487f296bb3SBarry Smithregardless of whether C or Fortran is being used. For example,
2497f296bb3SBarry Smith`MatSetValues()` and `VecSetValues()` always use
2507f296bb3SBarry Smithzero indexing. See {any}`sec_matoptions` for further
2517f296bb3SBarry Smithdetails.
2527f296bb3SBarry Smith
2537f296bb3SBarry SmithIndexing into Fortran arrays, for example obtained with `VecGetArray()`, uses the Fortran
2547f296bb3SBarry Smithconvention and generally begin with 1 except for special routines such as `DMDAVecGetArray()` which uses the ranges
2557f296bb3SBarry Smithprovided by `DMDAGetCorners()`.
2567f296bb3SBarry Smith
2577f296bb3SBarry Smith### Setting Routines and Contexts
2587f296bb3SBarry Smith
2597f296bb3SBarry SmithSome PETSc functions take as arguments user-functions and contexts for the function. For example
2607f296bb3SBarry Smith
2617f296bb3SBarry Smith```fortran
2627f296bb3SBarry Smithexternal func
2637f296bb3SBarry SmithSNESSetFunction(snes, r, func, ctx, ierr)
2647f296bb3SBarry SmithSNES snes
2657f296bb3SBarry SmithVec r
2667f296bb3SBarry SmithPetscErrorCode ierr
2677f296bb3SBarry Smith```
2687f296bb3SBarry Smith
2697f296bb3SBarry Smithwhere `func` has the calling sequence
2707f296bb3SBarry Smith
2717f296bb3SBarry Smith```fortran
2727f296bb3SBarry Smithsubroutine func(snes, x, f, ctx, ierr)
2737f296bb3SBarry SmithSNES snes
2747f296bb3SBarry SmithVec x,f
2757f296bb3SBarry SmithPetscErrorCode ierr
2767f296bb3SBarry Smith```
2777f296bb3SBarry Smith
2787f296bb3SBarry Smithand `ctx` can be almost anything (represented as `void *` in C).
2797f296bb3SBarry Smith
2807f296bb3SBarry SmithIt can be a Fortran derived type as in
2817f296bb3SBarry Smith
2827f296bb3SBarry Smith```fortran
2837f296bb3SBarry Smithsubroutine func(snes, x, f, ctx, ierr)
2847f296bb3SBarry SmithSNES snes
2857f296bb3SBarry SmithVec x,f
2867f296bb3SBarry Smithtype (userctx)   ctx
2877f296bb3SBarry SmithPetscErrorCode ierr
2887f296bb3SBarry Smith...
2897f296bb3SBarry Smith
2907f296bb3SBarry Smithexternal func
2917f296bb3SBarry SmithSNESSetFunction(snes, r, func, ctx, ierr)
2927f296bb3SBarry SmithSNES snes
2937f296bb3SBarry SmithVec r
2947f296bb3SBarry SmithPetscErrorCode ierr
2957f296bb3SBarry Smithtype (userctx)   ctx
2967f296bb3SBarry Smith```
2977f296bb3SBarry Smith
2987f296bb3SBarry Smithor a PETSc object
2997f296bb3SBarry Smith
3007f296bb3SBarry Smith```fortran
3017f296bb3SBarry Smithsubroutine func(snes, x, f, ctx, ierr)
3027f296bb3SBarry SmithSNES snes
3037f296bb3SBarry SmithVec x,f
3047f296bb3SBarry SmithVec ctx
3057f296bb3SBarry SmithPetscErrorCode ierr
3067f296bb3SBarry Smith...
3077f296bb3SBarry Smith
3087f296bb3SBarry Smithexternal func
3097f296bb3SBarry SmithSNESSetFunction(snes, r, func, ctx, ierr)
3107f296bb3SBarry SmithSNES snes
3117f296bb3SBarry SmithVec r
3127f296bb3SBarry SmithPetscErrorCode ierr
3137f296bb3SBarry SmithVec ctx
3147f296bb3SBarry Smith```
3157f296bb3SBarry Smith
3167f296bb3SBarry Smithor nothing
3177f296bb3SBarry Smith
3187f296bb3SBarry Smith```fortran
3197f296bb3SBarry Smithsubroutine func(snes, x, f, dummy, ierr)
3207f296bb3SBarry SmithSNES snes
3217f296bb3SBarry SmithVec x,f
3227f296bb3SBarry Smithinteger dummy(*)
3237f296bb3SBarry SmithPetscErrorCode ierr
3247f296bb3SBarry Smith...
3257f296bb3SBarry Smith
3267f296bb3SBarry Smithexternal func
3277f296bb3SBarry SmithSNESSetFunction(snes, r, func, 0, ierr)
3287f296bb3SBarry SmithSNES snes
3297f296bb3SBarry SmithVec r
3307f296bb3SBarry SmithPetscErrorCode ierr
3317f296bb3SBarry Smith```
3327f296bb3SBarry Smith
3337f296bb3SBarry SmithWhen a function pointer (declared as external in Fortran) is passed as an argument to a PETSc function,
3347f296bb3SBarry Smithit is assumed that this
3357f296bb3SBarry Smithfunction references a routine written in the same language as the PETSc
3367f296bb3SBarry Smithinterface function that was called. For instance, if
3377f296bb3SBarry Smith`SNESSetFunction()` is called from C, the function must be a C function. Likewise, if it is called from Fortran, the
3387f296bb3SBarry Smithfunction must be (a subroutine) written in Fortran.
3397f296bb3SBarry Smith
3407f296bb3SBarry SmithIf you are using Fortran classes that have bound functions (methods) as in
3417f296bb3SBarry 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
3427f296bb3SBarry Smithto function pointer setting routines, such as `SNESSetFunction()`. Instead, one must use `SNESSetFunctionNoInterface()`,
3437f296bb3SBarry Smithand define the interface directly in the user code, see
3447f296bb3SBarry Smith<a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/snes/tests/ex18f90.F90.html">ex18f90.F90</a>
3457f296bb3SBarry Smithfor a full demonstration.
3467f296bb3SBarry Smith
3477f296bb3SBarry Smith(sec_fortcompile)=
3487f296bb3SBarry Smith
3497f296bb3SBarry Smith### Compiling and Linking Fortran Programs
3507f296bb3SBarry Smith
3517f296bb3SBarry SmithSee {any}`sec_writing_application_codes`.
3527f296bb3SBarry Smith
3537f296bb3SBarry Smith### Duplicating Multiple Vectors
3547f296bb3SBarry Smith
3557f296bb3SBarry SmithThe Fortran interface to `VecDuplicateVecs()` differs slightly from
3567f296bb3SBarry Smiththe C/C++ variant. To create `n` vectors of the same
3577f296bb3SBarry Smithformat as an existing vector, the user must declare a vector array,
3587f296bb3SBarry Smith`v_new` of size `n`. Then, after `VecDuplicateVecs()` has been
3597f296bb3SBarry Smithcalled, `v_new` will contain (pointers to) the new PETSc vector
3607f296bb3SBarry Smithobjects. When finished with the vectors, the user should destroy them by
3617f296bb3SBarry Smithcalling `VecDestroyVecs()`. For example, the following code fragment
3627f296bb3SBarry Smithduplicates `v_old` to form two new vectors, `v_new(1)` and
3637f296bb3SBarry Smith`v_new(2)`.
3647f296bb3SBarry Smith
3657f296bb3SBarry Smith```fortran
3667f296bb3SBarry SmithVec          v_old, v_new(2)
3677f296bb3SBarry SmithPetscInt     ierr
3687f296bb3SBarry SmithPetscScalar  alpha
3697f296bb3SBarry Smith....
3707f296bb3SBarry SmithPetscCall(VecDuplicateVecs(v_old, 2, v_new, ierr))
3717f296bb3SBarry Smithalpha = 4.3
3727f296bb3SBarry SmithPetscCall(VecSet(v_new(1), alpha, ierr))
3737f296bb3SBarry Smithalpha = 6.0
3747f296bb3SBarry SmithPetscCall(VecSet(v_new(2), alpha, ierr))
3757f296bb3SBarry Smith....
3767f296bb3SBarry SmithPetscCall(VecDestroyVecs(2, v_new, ierr))
3777f296bb3SBarry Smith```
3787f296bb3SBarry Smith
3797f296bb3SBarry Smith(sec_fortran_examples)=
3807f296bb3SBarry Smith
3817f296bb3SBarry Smith## Sample Fortran Programs
3827f296bb3SBarry Smith
3837f296bb3SBarry SmithSample programs that illustrate the PETSc interface for Fortran are
3847f296bb3SBarry Smithgiven below, corresponding to
3857f296bb3SBarry Smith<a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/vec/vec/tests/ex19f.F90.html">Vec Test ex19f</a>,
3867f296bb3SBarry Smith<a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/vec/vec/tutorials/ex4f.F90.html">Vec Tutorial ex4f</a>,
3877f296bb3SBarry Smith<a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/sys/classes/draw/tests/ex5f.F90.html">Draw Test ex5f</a>,
3887f296bb3SBarry Smithand
3897f296bb3SBarry Smith<a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/snes/tutorials/ex1f.F90.html">SNES Tutorial ex1f</a>,
3907f296bb3SBarry Smithrespectively. We also refer Fortran programmers to the C examples listed
3917f296bb3SBarry Smiththroughout the manual, since PETSc usage within the two languages
3927f296bb3SBarry Smithdiffers only slightly.
3937f296bb3SBarry Smith
3947f296bb3SBarry Smith:::{admonition} Listing: `src/vec/vec/tests/ex19f.F90`
3957f296bb3SBarry Smith:name: vec-test-ex19f
3967f296bb3SBarry Smith
3977f296bb3SBarry Smith```{literalinclude} /../src/vec/vec/tests/ex19f.F90
3987f296bb3SBarry Smith:end-at: end
3997f296bb3SBarry Smith:language: fortran
4007f296bb3SBarry Smith```
4017f296bb3SBarry Smith:::
4027f296bb3SBarry Smith
4037f296bb3SBarry Smith(listing_vec_ex4f)=
4047f296bb3SBarry Smith
4057f296bb3SBarry Smith:::{admonition} Listing: `src/vec/vec/tutorials/ex4f.F90`
4067f296bb3SBarry Smith:name: vec-ex4f
4077f296bb3SBarry Smith
4087f296bb3SBarry Smith```{literalinclude} /../src/vec/vec/tutorials/ex4f.F90
4097f296bb3SBarry Smith:end-before: '!/*TEST'
4107f296bb3SBarry Smith:language: fortran
4117f296bb3SBarry Smith```
4127f296bb3SBarry Smith:::
4137f296bb3SBarry Smith
4147f296bb3SBarry Smith:::{admonition} Listing: `src/sys/classes/draw/tests/ex5f.F90`
4157f296bb3SBarry Smith:name: draw-test-ex5f
4167f296bb3SBarry Smith
4177f296bb3SBarry Smith```{literalinclude} /../src/sys/classes/draw/tests/ex5f.F90
4187f296bb3SBarry Smith:end-at: end
4197f296bb3SBarry Smith:language: fortran
4207f296bb3SBarry Smith```
4217f296bb3SBarry Smith:::
4227f296bb3SBarry Smith
4237f296bb3SBarry Smith:::{admonition} Listing: `src/snes/tutorials/ex1f.F90`
4247f296bb3SBarry Smith:name: snes-ex1f
4257f296bb3SBarry Smith
4267f296bb3SBarry Smith```{literalinclude} /../src/snes/tutorials/ex1f.F90
4277f296bb3SBarry Smith:end-before: '!/*TEST'
4287f296bb3SBarry Smith:language: fortran
4297f296bb3SBarry Smith```
4307f296bb3SBarry Smith:::
4317f296bb3SBarry Smith
4327f296bb3SBarry Smith### Calling Fortran Routines from C (and C Routines from Fortran)
4337f296bb3SBarry Smith
4347f296bb3SBarry SmithThe information here applies only if you plan to call your **own**
4357f296bb3SBarry SmithC functions from Fortran or Fortran functions from C.
4367f296bb3SBarry SmithDifferent compilers have different methods of naming Fortran routines
4377f296bb3SBarry Smithcalled from C (or C routines called from Fortran). Most Fortran
4387f296bb3SBarry Smithcompilers change the capital letters in Fortran routines to
4397f296bb3SBarry Smithall lowercase. With some compilers, the Fortran compiler appends an underscore
4407f296bb3SBarry Smithto the end of each Fortran routine name; for example, the Fortran
4417f296bb3SBarry Smithroutine `Dabsc()` would be called from C with `dabsc_()`. Other
4427f296bb3SBarry Smithcompilers change all the letters in Fortran routine names to capitals.
4437f296bb3SBarry Smith
4447f296bb3SBarry SmithPETSc provides two macros (defined in C/C++) to help write portable code
4457f296bb3SBarry Smiththat mixes C/C++ and Fortran. They are `PETSC_HAVE_FORTRAN_UNDERSCORE`
4467f296bb3SBarry Smithand `PETSC_HAVE_FORTRAN_CAPS` , which will be defined in the file
4477f296bb3SBarry Smith`$PETSC_DIR/$PETSC_ARCH/include/petscconf.h` based on the compilers
4487f296bb3SBarry Smithconventions. The macros are used,
4497f296bb3SBarry Smithfor example, as follows:
4507f296bb3SBarry Smith
4517f296bb3SBarry Smith```fortran
4527f296bb3SBarry Smith#if defined(PETSC_HAVE_FORTRAN_CAPS)
4537f296bb3SBarry Smith#define dabsc_ DABSC
4547f296bb3SBarry Smith#elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
4557f296bb3SBarry Smith#define dabsc_ dabsc
4567f296bb3SBarry Smith#endif
4577f296bb3SBarry Smith.....
4587f296bb3SBarry Smithdabsc_( &n,x,y); /* call the Fortran function */
4597f296bb3SBarry Smith```
460