xref: /petsc/src/mat/matfd/ftn-custom/zfdmatrixf.c (revision bebe2cf65d55febe21a5af8db2bd2e168caaa2e7)
1 #include <petsc/private/fortranimpl.h>
2 #include <petsc/private/matimpl.h>
3 
4 /* Declare these pointer types instead of void* for clarity, but do not include petscts.h so that this code does have an actual reverse dependency. */
5 typedef struct _p_TS *TS;
6 typedef struct _p_SNES *SNES;
7 
8 #if defined(PETSC_HAVE_FORTRAN_CAPS)
9 #define matfdcoloringsetfunctionts_      MATFDCOLORINGSETFUNCTIONTS
10 #define matfdcoloringsetfunction_        MATFDCOLORINGSETFUNCTION
11 #define matfdcoloringview_               MATFDCOLORINGVIEW
12 #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
13 #define matfdcoloringsetfunctionts_      matfdcoloringsetfunctionts
14 #define matfdcoloringsetfunction_        matfdcoloringsetfunction
15 #define matfdcoloringview_               matfdcoloringview
16 #endif
17 
18 
19 /* These are not extern C because they are passed into non-extern C user level functions */
20 static PetscErrorCode ourmatfdcoloringfunctionts(TS ts,PetscReal t,Vec x,Vec y,MatFDColoring fd)
21 {
22   PetscErrorCode ierr = 0;
23   (*(void (PETSC_STDCALL *)(TS*,PetscReal*,Vec*,Vec*,void*,PetscErrorCode*))(fd->ftn_func_pointer)) (&ts,&t,&x,&y,fd->ftn_func_cntx,&ierr);
24   return ierr;
25 }
26 
27 static PetscErrorCode ourmatfdcoloringfunctionsnes(SNES snes,Vec x,Vec y,MatFDColoring fd)
28 {
29   PetscErrorCode ierr = 0;
30   (*(void (PETSC_STDCALL *)(SNES*,Vec*,Vec*,void*,PetscErrorCode*))(fd->ftn_func_pointer)) (&snes,&x,&y,fd->ftn_func_cntx,&ierr);
31   return ierr;
32 }
33 
34 /*
35         MatFDColoringSetFunction sticks the Fortran function and its context into the MatFDColoring structure and passes the MatFDColoring object
36     in as the function context. ourmafdcoloringfunctionsnes() and ourmatfdcoloringfunctionts()  then access the function and its context from the
37     MatFDColoring that is passed in. This is the same way that fortran_func_pointers is used in PETSc objects.
38 
39    NOTE: FORTRAN USER CANNOT PUT IN A NEW J OR B currently.
40 */
41 
42 
43 PETSC_EXTERN void PETSC_STDCALL matfdcoloringsetfunctionts_(MatFDColoring *fd,void (PETSC_STDCALL *f)(TS*,double*,Vec*,Vec*,void*,PetscErrorCode*),void *ctx,PetscErrorCode *ierr)
44 {
45   (*fd)->ftn_func_pointer =  (void (*)(void)) f;
46   (*fd)->ftn_func_cntx    = ctx;
47 
48   *ierr = MatFDColoringSetFunction(*fd,(PetscErrorCodeFunction)ourmatfdcoloringfunctionts,*fd);
49 }
50 
51 PETSC_EXTERN void PETSC_STDCALL matfdcoloringsetfunction_(MatFDColoring *fd,void (PETSC_STDCALL *f)(SNES*,Vec*,Vec*,void*,PetscErrorCode*),void *ctx,PetscErrorCode *ierr)
52 {
53   (*fd)->ftn_func_pointer = (void (*)(void)) f;
54   (*fd)->ftn_func_cntx    = ctx;
55 
56   *ierr = MatFDColoringSetFunction(*fd,(PetscErrorCodeFunction)ourmatfdcoloringfunctionsnes,*fd);
57 }
58 
59 PETSC_EXTERN void PETSC_STDCALL matfdcoloringview_(MatFDColoring *c,PetscViewer *vin,PetscErrorCode *ierr)
60 {
61   PetscViewer v;
62 
63   PetscPatchDefaultViewers_Fortran(vin,v);
64   *ierr = MatFDColoringView(*c,v);
65 }
66 
67