1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML3.2 EN">
<HTML>
<HEAD> <link rel="canonical" href="http://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Sys/UsingFortran.html" />
<META NAME="GENERATOR" CONTENT="DOCTEXT">
<TITLE>UsingFortran</TITLE>
</HEAD>
<BODY BGCOLOR="FFFFFF">
<div id="version" align=right><b>petsc-3.7.5 2017-01-01</b></div>
<div id="bugreport" align=right><a href="mailto:petsc-maint@mcs.anl.gov?subject=Typo or Error in Documentation &body=Please describe the typo or error in the documentation: petsc-3.7.5 v3.7.5 docs/manualpages/Sys/UsingFortran.html "><small>Report Typos and Errors</small></a></div>
<A NAME="UsingFortran"><H1>UsingFortran</H1></A>
Fortran can be used with PETSc in four distinct approaches
<pre>
1) classic Fortran 77 style
</pre>
.#include "petsc/finclude/petscXXX.h" to work with material from the XXX component of PETSc
</pre>
<pre>
XXX variablename
</pre>
<pre>
You cannot use this approach if you wish to use the Fortran 90 specific PETSc routines
</pre>
<pre>
which end in F90; such as <A HREF="../Vec/VecGetArrayF90.html#VecGetArrayF90">VecGetArrayF90</A>()
</pre>
<pre>
</pre>
<pre>
2) classic Fortran 90 style
</pre>
.#include "petsc/finclude/petscXXX.h"
</pre>
.#include "petsc/finclude/petscXXX.h90" to work with material from the XXX component of PETSc
</pre>
<pre>
XXX variablename
</pre>
<pre>
</pre>
<pre>
3) Using Fortran modules
</pre>
.#include "petsc/finclude/petscXXXdef.h"
</pre>
<pre>
use petscXXXX
</pre>
<pre>
XXX variablename
</pre>
<pre>
</pre>
<pre>
4) Use Fortran modules and Fortran data types for PETSc types
</pre>
.#include "petsc/finclude/petscXXXdef.h"
</pre>
<pre>
use petscXXXX
</pre>
<pre>
type(XXX) variablename
</pre>
<pre>
To use this approach you must ./configure PETSc with the additional
</pre>
<pre>
option --with-fortran-datatypes You cannot use the type(XXX) declaration approach without using Fortran modules
</pre>
<P>
Finally if you absolutely do not want to use any #include you can use either
<P>
<pre>
3a) skip the #include BUT you cannot use any PETSc data type names like <A HREF="../Vec/Vec.html#Vec">Vec</A>, <A HREF="../Mat/Mat.html#Mat">Mat</A>, <A HREF="../Sys/PetscInt.html#PetscInt">PetscInt</A>, <A HREF="../Sys/PetscErrorCode.html#PetscErrorCode">PetscErrorCode</A> etc
</pre>
<pre>
and you must declare the variables as integer, for example
</pre>
<pre>
integer variablename
</pre>
<pre>
</pre>
<pre>
4a) skip the #include, you use the object types like type(<A HREF="../Vec/Vec.html#Vec">Vec</A>) type(<A HREF="../Mat/Mat.html#Mat">Mat</A>) but cannot use the data type
</pre>
<pre>
names like <A HREF="../Sys/PetscErrorCode.html#PetscErrorCode">PetscErrorCode</A>, <A HREF="../Sys/PetscInt.html#PetscInt">PetscInt</A> etc. again for those you must use integer
</pre>
<P>
We recommend either 2 or 3. Approaches 2 and 3 provide type checking for most PETSc function calls; 4 has type checking
for only a few PETSc functions.
<P>
Fortran type checking with interfaces is strick, this means you cannot pass a scalar value when an array value
is expected (even though it is legal Fortran). For example when setting a single value in a matrix with <A HREF="../Mat/MatSetValues.html#MatSetValues">MatSetValues</A>()
you cannot have something like
<pre>
<A HREF="../Sys/PetscInt.html#PetscInt">PetscInt</A> row,col
</pre>
<pre>
<A HREF="../Sys/PetscScalar.html#PetscScalar">PetscScalar</A> val
</pre>
<pre>
...
</pre>
<pre>
call <A HREF="../Mat/MatSetValues.html#MatSetValues">MatSetValues</A>(mat,1,row,1,col,val,<A HREF="../Sys/INSERT_VALUES.html#INSERT_VALUES">INSERT_VALUES</A>,ierr)
</pre>
You must instead have
<pre>
<A HREF="../Sys/PetscInt.html#PetscInt">PetscInt</A> row(1),col(1)
</pre>
<pre>
<A HREF="../Sys/PetscScalar.html#PetscScalar">PetscScalar</A> val(1)
</pre>
<pre>
...
</pre>
<pre>
call <A HREF="../Mat/MatSetValues.html#MatSetValues">MatSetValues</A>(mat,1,row,1,col,val,<A HREF="../Sys/INSERT_VALUES.html#INSERT_VALUES">INSERT_VALUES</A>,ierr)
</pre>
<P>
<P>
See the example src/vec/vec/examples/tutorials/ex20f90.F90 for an example that can use all four approaches
<P>
Developer Notes: The petsc/finclude/petscXXXdef.h contain all the #defines (would be typedefs in C code) these
automatically include their predecessors; for example petsc/finclude/petscvecdef.h includes petsc/finclude/petscisdef.h
<P>
The petsc/finclude/petscXXXX.h contain all the parameter statements for that package. These automatically include
their petsc/finclude/petscXXXdef.h file but DO NOT automatically include their predecessors; for example
petsc/finclude/petscvec.h does NOT automatically include petsc/finclude/petscis.h
<P>
The petsc/finclude/ftn-custom/petscXXXdef.h90 are not intended to be used directly in code, they define the
Fortran data type type(XXX) (for example type(<A HREF="../Vec/Vec.html#Vec">Vec</A>)) when PETSc is ./configure with the --with-fortran-datatypes option.
<P>
The petsc/finclude/ftn-custom/petscXXX.h90 (not included directly by code) contain interface definitions for
the PETSc Fortran stubs that have different bindings then their C version (for example <A HREF="../Vec/VecGetArrayF90.html#VecGetArrayF90">VecGetArrayF90</A>).
<P>
The petsc/finclude/ftn-auto/petscXXX.h90 (not included directly by code) contain interface definitions generated
automatically by "make allfortranstubs".
<P>
The petsc/finclude/petscXXX.h90 includes the custom petsc/finclude/ftn-custom/petscXXX.h90 and if ./configure
was run with --with-fortran-interfaces it also includes the petsc/finclude/ftn-auto/petscXXX.h90 These DO NOT automatically
include their predecessors
<P>
<P>
<P><B><P><B><FONT COLOR="#CC3333">Level:</FONT></B>beginner
<BR><FONT COLOR="#CC3333">Location:</FONT></B><A HREF="../../../include/petscsys.h.html#UsingFortran">include/petscsys.h</A>
<BR><A HREF="./index.html">Index of all Sys routines</A>
<BR><A HREF="../../index.html">Table of Contents for all manual pages</A>
<BR><A HREF="../singleindex.html">Index of all manual pages</A>
<A HREF="../../../src/vec/vec/examples/tutorials/ex20f90.F90.html">src/vec/vec/examples/tutorials/ex20f90.F90.html</A><BR>
</BODY></HTML>
|