xref: /petsc/src/sys/objects/finit.c (revision 09d3f17c186ba66d048d21778d55f0974d6c26af)
1*09d3f17cSBarry Smith #include <petsc/private/petscimpl.h> /*I  "petscsys.h"   I*/
2*09d3f17cSBarry Smith 
3*09d3f17cSBarry Smith #if defined(PETSC_USE_FORTRAN_BINDINGS)
4*09d3f17cSBarry Smith   #if defined(PETSC_HAVE_FORTRAN_CAPS)
5*09d3f17cSBarry Smith     #define petscinitializefortran_     PETSCINITIALIZEFORTRAN
6*09d3f17cSBarry Smith     #define petscsetmoduleblock_        PETSCSETMODULEBLOCK
7*09d3f17cSBarry Smith     #define petscsetmoduleblockmpi_     PETSCSETMODULEBLOCKMPI
8*09d3f17cSBarry Smith     #define petscsetmoduleblocknumeric_ PETSCSETMODULEBLOCKNUMERIC
9*09d3f17cSBarry Smith     #define petscsetcomm_               PETSCSETCOMM
10*09d3f17cSBarry Smith   #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
11*09d3f17cSBarry Smith     #define petscinitializefortran_     petscinitializefortran
12*09d3f17cSBarry Smith     #define petscsetmoduleblock_        petscsetmoduleblock
13*09d3f17cSBarry Smith     #define petscsetmoduleblockmpi_     petscsetmoduleblockmpi
14*09d3f17cSBarry Smith     #define petscsetmoduleblocknumeric_ petscsetmoduleblocknumeric
15*09d3f17cSBarry Smith     #define petscsetcomm_               petscsetcomm
16*09d3f17cSBarry Smith   #endif
17*09d3f17cSBarry Smith 
18*09d3f17cSBarry Smith PETSC_EXTERN void petscsetmoduleblock_(void);
19*09d3f17cSBarry Smith PETSC_EXTERN void petscsetmoduleblockmpi_(MPI_Fint *, MPI_Fint *, MPI_Fint *, MPI_Fint *);
20*09d3f17cSBarry Smith PETSC_EXTERN void petscsetmoduleblocknumeric_(PetscReal *, PetscReal *, PetscReal *, PetscReal *, PetscReal *, PetscReal *, PetscReal *, PetscReal *);
21*09d3f17cSBarry Smith PETSC_EXTERN void petscsetcomm_(MPI_Fint *, MPI_Fint *);
22*09d3f17cSBarry Smith #endif
23*09d3f17cSBarry Smith 
24*09d3f17cSBarry Smith /*@C
25*09d3f17cSBarry Smith   PetscInitializeFortran - Routine that should be called soon AFTER
26*09d3f17cSBarry Smith   the call to `PetscInitialize()` if one is using a C main program
27*09d3f17cSBarry Smith   that calls Fortran routines that in turn call PETSc routines.
28*09d3f17cSBarry Smith 
29*09d3f17cSBarry Smith   Collective on `PETSC_COMM_WORLD`
30*09d3f17cSBarry Smith 
31*09d3f17cSBarry Smith   Level: beginner
32*09d3f17cSBarry Smith 
33*09d3f17cSBarry Smith   Notes:
34*09d3f17cSBarry Smith   `PetscInitializeFortran()` initializes some of the default viewers,
35*09d3f17cSBarry Smith   communicators, etc. for use in the Fortran if a user's main program is
36*09d3f17cSBarry Smith   written in C.  `PetscInitializeFortran()` is NOT needed if a user's main
37*09d3f17cSBarry Smith   program is written in Fortran; in this case, just calling
38*09d3f17cSBarry Smith   `PetscInitialize()` in the main (Fortran) program is sufficient.
39*09d3f17cSBarry Smith 
40*09d3f17cSBarry Smith   This function exists and can be called even if PETSc has been configured
41*09d3f17cSBarry Smith   with --with-fortran-bindings=0 or --with-fc=0. It just does nothing
42*09d3f17cSBarry Smith   in that case.
43*09d3f17cSBarry Smith 
44*09d3f17cSBarry Smith .seealso: `PetscInitialize()`
45*09d3f17cSBarry Smith @*/
46*09d3f17cSBarry Smith PetscErrorCode PetscInitializeFortran(void)
47*09d3f17cSBarry Smith {
48*09d3f17cSBarry Smith #if defined(PETSC_USE_FORTRAN_BINDINGS)
49*09d3f17cSBarry Smith   MPI_Fint c1 = 0, c2 = 0;
50*09d3f17cSBarry Smith 
51*09d3f17cSBarry Smith   if (PETSC_COMM_WORLD) c1 = MPI_Comm_c2f(PETSC_COMM_WORLD);
52*09d3f17cSBarry Smith   c2 = MPI_Comm_c2f(PETSC_COMM_SELF);
53*09d3f17cSBarry Smith   petscsetmoduleblock_();
54*09d3f17cSBarry Smith   petscsetcomm_(&c1, &c2);
55*09d3f17cSBarry Smith 
56*09d3f17cSBarry Smith   {
57*09d3f17cSBarry Smith     MPI_Fint freal, fscalar, fsum, fint;
58*09d3f17cSBarry Smith     freal   = MPI_Type_c2f(MPIU_REAL);
59*09d3f17cSBarry Smith     fscalar = MPI_Type_c2f(MPIU_SCALAR);
60*09d3f17cSBarry Smith     fsum    = MPI_Op_c2f(MPIU_SUM);
61*09d3f17cSBarry Smith     fint    = MPI_Type_c2f(MPIU_INT);
62*09d3f17cSBarry Smith     petscsetmoduleblockmpi_(&freal, &fscalar, &fsum, &fint);
63*09d3f17cSBarry Smith   }
64*09d3f17cSBarry Smith 
65*09d3f17cSBarry Smith   {
66*09d3f17cSBarry Smith     PetscReal pi      = PETSC_PI;
67*09d3f17cSBarry Smith     PetscReal maxreal = PETSC_MAX_REAL;
68*09d3f17cSBarry Smith     PetscReal minreal = PETSC_MIN_REAL;
69*09d3f17cSBarry Smith     PetscReal eps     = PETSC_MACHINE_EPSILON;
70*09d3f17cSBarry Smith     PetscReal seps    = PETSC_SQRT_MACHINE_EPSILON;
71*09d3f17cSBarry Smith     PetscReal small   = PETSC_SMALL;
72*09d3f17cSBarry Smith     PetscReal pinf    = PETSC_INFINITY;
73*09d3f17cSBarry Smith     PetscReal pninf   = PETSC_NINFINITY;
74*09d3f17cSBarry Smith     petscsetmoduleblocknumeric_(&pi, &maxreal, &minreal, &eps, &seps, &small, &pinf, &pninf);
75*09d3f17cSBarry Smith   }
76*09d3f17cSBarry Smith #endif
77*09d3f17cSBarry Smith   return PETSC_SUCCESS;
78*09d3f17cSBarry Smith }
79