xref: /petsc/src/sys/objects/ftn-custom/zstartf.c (revision 5167406814f7f49a97e0d17844aa49dba92c178e)
1ba9a049bSBarry Smith 
2ba9a049bSBarry Smith #include <petsc-private/fortranimpl.h>
3ba9a049bSBarry Smith 
4519f805aSKarl Rupp #if defined(PETSC_HAVE_FORTRAN_CAPS)
5ba9a049bSBarry Smith #define petscinitializefortran_       PETSCINITIALIZEFORTRAN
6ba9a049bSBarry Smith #define petscsetcommonblock_          PETSCSETCOMMONBLOCK
7ba9a049bSBarry Smith #define petscsetfortranbasepointers_  PETSCSETFORTRANBASEPOINTERS
8ba9a049bSBarry Smith #define petsc_null_function_          PETSC_NULL_FUNCTION
9ba9a049bSBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
10ba9a049bSBarry Smith #define petscinitializefortran_       petscinitializefortran
11ba9a049bSBarry Smith #define petscsetcommonblock_          petscsetcommonblock
12ba9a049bSBarry Smith #define petscsetfortranbasepointers_  petscsetfortranbasepointers
13ba9a049bSBarry Smith #define petsc_null_function_          petsc_null_function
14ba9a049bSBarry Smith #endif
15ba9a049bSBarry Smith 
16ba9a049bSBarry Smith #if defined(PETSC_HAVE_FORTRAN_UNDERSCORE_UNDERSCORE)
17ba9a049bSBarry Smith #define petsc_null_function_  petsc_null_function__
18ba9a049bSBarry Smith #endif
19ba9a049bSBarry Smith 
20*51674068SBarry Smith PETSC_EXTERN void PETSC_STDCALL petscsetcommonblock_(MPI_Fint*,MPI_Fint*);
21*51674068SBarry Smith PETSC_EXTERN void PETSC_STDCALL petscsetcommonblockmpi_(MPI_Fint*,MPI_Fint*,MPI_Fint*);
22ba9a049bSBarry Smith 
23ba9a049bSBarry Smith /*@C
24ba9a049bSBarry Smith    PetscInitializeFortran - Routine that should be called soon AFTER
25ba9a049bSBarry Smith    the call to PetscInitialize() if one is using a C main program
26ba9a049bSBarry Smith    that calls Fortran routines that in turn call PETSc routines.
27ba9a049bSBarry Smith 
28ba9a049bSBarry Smith    Collective on PETSC_COMM_WORLD
29ba9a049bSBarry Smith 
30ba9a049bSBarry Smith    Level: beginner
31ba9a049bSBarry Smith 
32ba9a049bSBarry Smith    Notes:
33ba9a049bSBarry Smith    PetscInitializeFortran() initializes some of the default viewers,
34ba9a049bSBarry Smith    communicators, etc. for use in the Fortran if a user's main program is
35ba9a049bSBarry Smith    written in C.  PetscInitializeFortran() is NOT needed if a user's main
36ba9a049bSBarry Smith    program is written in Fortran; in this case, just calling
37ba9a049bSBarry Smith    PetscInitialize() in the main (Fortran) program is sufficient.
38ba9a049bSBarry Smith 
39ba9a049bSBarry Smith .seealso:  PetscInitialize()
40ba9a049bSBarry Smith 
41ba9a049bSBarry Smith .keywords: Mixing C and Fortran, passing PETSc objects to Fortran
42ba9a049bSBarry Smith @*/
43ba9a049bSBarry Smith PetscErrorCode PetscInitializeFortran(void)
44ba9a049bSBarry Smith {
45*51674068SBarry Smith   MPI_Fint c1=0,c2=0;
46ba9a049bSBarry Smith 
47a297a907SKarl Rupp   if (PETSC_COMM_WORLD) c1 =  MPI_Comm_c2f(PETSC_COMM_WORLD);
48ba9a049bSBarry Smith   c2 =  MPI_Comm_c2f(PETSC_COMM_SELF);
49ba9a049bSBarry Smith   petscsetcommonblock_(&c1,&c2);
50*51674068SBarry Smith 
51*51674068SBarry Smith #if defined(PETSC_USE___FLOAT128)
52*51674068SBarry Smith   {
53*51674068SBarry Smith     MPI_Fint freal,fscalar,fsum;
54*51674068SBarry Smith     ierr = MPI_Type_c2f(MPIU_REAL,&freal);CHKERRQ(ierr);
55*51674068SBarry Smith     ierr = MPI_Type_c2f(MPIU_COMPLEX,&fcomplex);CHKERRQ(ierr);
56*51674068SBarry Smith     ierr = MPI_Op_c2f(MPIU_SUM,&fsum);CHKERRQ(ierr);
57*51674068SBarry Smith     petscsetcommonblockmpi_(&freal,&fcomplex,&fsum);
58*51674068SBarry Smith   }
59*51674068SBarry Smith #endif
60ba9a049bSBarry Smith   return 0;
61ba9a049bSBarry Smith }
62ba9a049bSBarry Smith 
638cc058d9SJed Brown PETSC_EXTERN void PETSC_STDCALL petscinitializefortran_(int *ierr)
64ba9a049bSBarry Smith {
65ba9a049bSBarry Smith   *ierr = PetscInitializeFortran();
66ba9a049bSBarry Smith }
67ba9a049bSBarry Smith 
688cc058d9SJed Brown PETSC_EXTERN void PETSC_STDCALL petscsetfortranbasepointers_(char *fnull_character PETSC_MIXED_LEN(len),
690298fd71SBarry Smith                                   void *fnull_integer,void *fnull_scalar,void * fnull_double,
70ba9a049bSBarry Smith                                   void *fnull_real,void *fnull_object,
71ba9a049bSBarry Smith                                   void* fnull_truth,void (*fnull_function)(void) PETSC_END_LEN(len))
72ba9a049bSBarry Smith {
73ba9a049bSBarry Smith   PETSC_NULL_CHARACTER_Fortran = fnull_character;
74ba9a049bSBarry Smith   PETSC_NULL_INTEGER_Fortran   = fnull_integer;
75ba9a049bSBarry Smith   PETSC_NULL_SCALAR_Fortran    = fnull_scalar;
76ba9a049bSBarry Smith   PETSC_NULL_DOUBLE_Fortran    = fnull_double;
77ba9a049bSBarry Smith   PETSC_NULL_REAL_Fortran      = fnull_real;
78ba9a049bSBarry Smith   PETSC_NULL_OBJECT_Fortran    = fnull_object;
79ba9a049bSBarry Smith   PETSC_NULL_BOOL_Fortran      = fnull_truth;
80ba9a049bSBarry Smith   PETSC_NULL_FUNCTION_Fortran  = fnull_function;
81ba9a049bSBarry Smith }
82ba9a049bSBarry Smith 
83ba9a049bSBarry Smith /*
84ba9a049bSBarry Smith   A valid address for the fortran variable PETSC_NULL_FUNCTION
85ba9a049bSBarry Smith */
868cc058d9SJed Brown PETSC_EXTERN void petsc_null_function_(void)
87ba9a049bSBarry Smith {
88ba9a049bSBarry Smith   return;
89ba9a049bSBarry Smith }
90ba9a049bSBarry Smith 
91ba9a049bSBarry Smith 
92