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