xref: /petsc/src/sys/objects/ftn-custom/zstartf.c (revision 443a01528d9168b7d94707dfbd9b588579a2fa29)
1 
2 #include <petsc/private/fortranimpl.h>
3 
4 #if defined(PETSC_HAVE_FORTRAN_CAPS)
5 #define petscinitializefortran_       PETSCINITIALIZEFORTRAN
6 #define petscsetmoduleblock_          PETSCSETMODULEBLOCK
7 #define petscsetfortranbasepointers_  PETSCSETFORTRANBASEPOINTERS
8 #define petsc_null_function_          PETSC_NULL_FUNCTION
9 #define petscsetmoduleblocknumeric_   PETSCSETMODULEBLOCKNUMERIC
10 #define petscsetcomm_                 PETSCSETCOMM
11 #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
12 #define petscinitializefortran_       petscinitializefortran
13 #define petscsetmoduleblock_          petscsetmoduleblock
14 #define petscsetfortranbasepointers_  petscsetfortranbasepointers
15 #define petsc_null_function_          petsc_null_function
16 #define petscsetmoduleblocknumeric_   petscsetmoduleblocknumeric
17 #define petscsetcomm_                 petscsetcomm
18 #endif
19 
20 #if defined(PETSC_HAVE_FORTRAN_UNDERSCORE_UNDERSCORE)
21 #define petsc_null_function_  petsc_null_function__
22 #endif
23 
24 PETSC_EXTERN void PETSC_STDCALL petscsetmoduleblock_();
25 PETSC_EXTERN void PETSC_STDCALL petscsetmoduleblockmpi_(MPI_Fint*,MPI_Fint*,MPI_Fint*);
26 PETSC_EXTERN void PETSC_STDCALL petscsetmoduleblocknumeric_(PetscReal*,PetscReal*,PetscReal*,PetscReal*,PetscReal*,PetscReal*,PetscReal*,PetscReal*);
27 PETSC_EXTERN void PETSC_STDCALL petscsetcomm_(MPI_Fint*,MPI_Fint*);
28 
29 /*@C
30    PetscInitializeFortran - Routine that should be called soon AFTER
31    the call to PetscInitialize() if one is using a C main program
32    that calls Fortran routines that in turn call PETSc routines.
33 
34    Collective on PETSC_COMM_WORLD
35 
36    Level: beginner
37 
38    Notes:
39    PetscInitializeFortran() initializes some of the default viewers,
40    communicators, etc. for use in the Fortran if a user's main program is
41    written in C.  PetscInitializeFortran() is NOT needed if a user's main
42    program is written in Fortran; in this case, just calling
43    PetscInitialize() in the main (Fortran) program is sufficient.
44 
45 .seealso:  PetscInitialize()
46 
47 .keywords: Mixing C and Fortran, passing PETSc objects to Fortran
48 @*/
49 PetscErrorCode PetscInitializeFortran(void)
50 {
51   MPI_Fint c1=0,c2=0;
52 
53   if (PETSC_COMM_WORLD) c1 =  MPI_Comm_c2f(PETSC_COMM_WORLD);
54   c2 =  MPI_Comm_c2f(PETSC_COMM_SELF);
55   petscsetmoduleblock_();
56   petscsetcomm_(&c1,&c2);
57 
58 #if defined(PETSC_USE_REAL___FLOAT128)
59   {
60     MPI_Fint freal,fscalar,fsum;
61     freal   = MPI_Type_c2f(MPIU_REAL);
62     fscalar = MPI_Type_c2f(MPIU_SCALAR);
63     fsum    = MPI_Op_c2f(MPIU_SUM);
64     petscsetmoduleblockmpi_(&freal,&fscalar,&fsum);
65   }
66 #endif
67 
68   {
69     PetscReal pi = PETSC_PI;
70     PetscReal maxreal = PETSC_MAX_REAL;
71     PetscReal minreal = PETSC_MIN_REAL;
72     PetscReal eps = PETSC_MACHINE_EPSILON;
73     PetscReal seps = PETSC_SQRT_MACHINE_EPSILON;
74     PetscReal small = PETSC_SMALL;
75     PetscReal pinf = PETSC_INFINITY;
76     PetscReal pninf = PETSC_NINFINITY;
77     petscsetmoduleblocknumeric_(&pi,&maxreal,&minreal,&eps,&seps,&small,&pinf,&pninf);
78   }
79   return 0;
80 }
81 
82 PETSC_EXTERN void PETSC_STDCALL petscinitializefortran_(int *ierr)
83 {
84   *ierr = PetscInitializeFortran();
85 }
86 
87 PETSC_EXTERN void PETSC_STDCALL petscsetfortranbasepointers_(char *fnull_character PETSC_MIXED_LEN(len),
88                                   void *fnull_integer,void *fnull_scalar,void * fnull_double,
89                                   void *fnull_real,
90                                   void* fnull_truth,void (*fnull_function)(void) PETSC_END_LEN(len))
91 {
92   PETSC_NULL_CHARACTER_Fortran = fnull_character;
93   PETSC_NULL_INTEGER_Fortran   = fnull_integer;
94   PETSC_NULL_SCALAR_Fortran    = fnull_scalar;
95   PETSC_NULL_DOUBLE_Fortran    = fnull_double;
96   PETSC_NULL_REAL_Fortran      = fnull_real;
97   PETSC_NULL_BOOL_Fortran      = fnull_truth;
98   PETSC_NULL_FUNCTION_Fortran  = fnull_function;
99 }
100 
101 /*
102   A valid address for the fortran variable PETSC_NULL_FUNCTION
103 */
104 PETSC_EXTERN void petsc_null_function_(void)
105 {
106   return;
107 }
108 
109 
110