xref: /petsc/src/mat/impls/shell/shell.c (revision 20563c6b1ea7b82b48c81bbd22ce9170a8c92d3b)
1e51e0e81SBarry Smith 
2e51e0e81SBarry Smith /*
3*20563c6bSBarry Smith    This provides a simple shell for Fortran (and C programmers) to
4*20563c6bSBarry Smith   create a very simple matrix class for use with KSP without coding
5*20563c6bSBarry Smith   mush of anything.
6e51e0e81SBarry Smith */
7e51e0e81SBarry Smith 
8e51e0e81SBarry Smith #include "petsc.h"
9e51e0e81SBarry Smith #include "matimpl.h"        /*I "mat.h" I*/
10e51e0e81SBarry Smith #include "vec/vecimpl.h"
11e51e0e81SBarry Smith 
12*20563c6bSBarry Smith typedef struct {
13*20563c6bSBarry Smith   int  m,n;
14*20563c6bSBarry Smith   int  (*mult)(void *,Vec,Vec);
15*20563c6bSBarry Smith   void *ctx;
16*20563c6bSBarry Smith } MatShell;
17e51e0e81SBarry Smith 
18*20563c6bSBarry Smith static int MatShellMult(Mat mat,Vec x,Vec y)
19e51e0e81SBarry Smith {
20*20563c6bSBarry Smith   MatShell *shell;
21*20563c6bSBarry Smith   shell = (MatShell *) mat->data;
22*20563c6bSBarry Smith   return (*shell->mult)(shell->ctx,x,y);
23e51e0e81SBarry Smith }
24*20563c6bSBarry Smith static int MatShellDestroy(PetscObject obj)
25e51e0e81SBarry Smith {
26*20563c6bSBarry Smith   Mat      mat = (Mat) obj;
27*20563c6bSBarry Smith   MatShell *shell;
28*20563c6bSBarry Smith   shell = (MatShell *) mat->data;
29*20563c6bSBarry Smith   FREE(shell); FREE(mat);
30e51e0e81SBarry Smith   return 0;
31e51e0e81SBarry Smith }
32e51e0e81SBarry Smith 
33*20563c6bSBarry Smith static struct _MatOps MatOps = {0,0,
34*20563c6bSBarry Smith        0,
35*20563c6bSBarry Smith        MatShellMult,0,0,0,
36*20563c6bSBarry Smith        0,0,0,0,
37*20563c6bSBarry Smith        0,0,
38*20563c6bSBarry Smith        0,
39*20563c6bSBarry Smith        0,
40*20563c6bSBarry Smith        0,0,0,
41*20563c6bSBarry Smith        0,
42*20563c6bSBarry Smith        0,0,0,
43*20563c6bSBarry Smith        0,0,
44*20563c6bSBarry Smith        0,
45*20563c6bSBarry Smith        0,0,0,0,
46*20563c6bSBarry Smith        0,0 };
47e51e0e81SBarry Smith 
48e51e0e81SBarry Smith /*@
49*20563c6bSBarry Smith    MatShellCreate - creates a new matrix class for use with your
50*20563c6bSBarry Smith           own private data storage format. This is intended to
51*20563c6bSBarry Smith           provide a simple class to use with KSP. You should
52*20563c6bSBarry Smith           not use this if you plan to make a complete class.
53e51e0e81SBarry Smith 
54e51e0e81SBarry Smith   Input Parameters:
55*20563c6bSBarry Smith .  m,n - number of rows and columns in matrix
56*20563c6bSBarry Smith .  ctx - pointer to your data needed by matrix multiply.
57e51e0e81SBarry Smith 
58e51e0e81SBarry Smith   Output Parameters:
59e51e0e81SBarry Smith .  mat - the matrix
60e51e0e81SBarry Smith 
61*20563c6bSBarry Smith   Keywords: matrix, shell
62e51e0e81SBarry Smith 
63*20563c6bSBarry Smith   Usage:
64*20563c6bSBarry Smith .             int (*mult)(void *,Vec,Vec);
65*20563c6bSBarry Smith .             MatShellCreate(m,n,ctx,&mat);
66*20563c6bSBarry Smith .             MatShellSetMult(mat,mult);
67e51e0e81SBarry Smith 
68e51e0e81SBarry Smith @*/
69*20563c6bSBarry Smith int MatShellCreate(int m, int n, void *ctx,Mat *mat)
70e51e0e81SBarry Smith {
71*20563c6bSBarry Smith   Mat      newmat;
72*20563c6bSBarry Smith   MatShell *shell;
73*20563c6bSBarry Smith   CREATEHEADER(newmat,_Mat);
74*20563c6bSBarry Smith   *mat           = newmat;
75*20563c6bSBarry Smith   newmat->cookie = MAT_COOKIE;
76*20563c6bSBarry Smith   newmat->factor = 0;
77*20563c6bSBarry Smith   newmat->row    = 0;
78*20563c6bSBarry Smith   newmat->col    = 0;
79*20563c6bSBarry Smith   newmat->destroy= MatShellDestroy;
80*20563c6bSBarry Smith   newmat->ops    = &MatOps;
81*20563c6bSBarry Smith   shell          = NEW(MatShell); CHKPTR(shell);
82*20563c6bSBarry Smith   newmat->data   = (void *) shell;
83*20563c6bSBarry Smith   shell->mult    = 0;
84*20563c6bSBarry Smith   shell->m       = m;
85*20563c6bSBarry Smith   shell->n       = n;
86*20563c6bSBarry Smith   shell->ctx     = ctx;
87e51e0e81SBarry Smith   return 0;
88e51e0e81SBarry Smith }
89e51e0e81SBarry Smith 
90e51e0e81SBarry Smith /*@
91*20563c6bSBarry Smith    MatShellSetMult - sets routine to use as matrix vector multiply.
92e51e0e81SBarry Smith 
93e51e0e81SBarry Smith   Input Parameters:
94*20563c6bSBarry Smith .  mat - the matrix to add the operation to, created with MatShellCreate()
95*20563c6bSBarry Smith .  mult - the matrix vector multiply routine.
96e51e0e81SBarry Smith 
97*20563c6bSBarry Smith   Keywords: matrix, multiply
98e51e0e81SBarry Smith @*/
99*20563c6bSBarry Smith int MatShellSetMult(Mat mat, int (*mult)(void*,Vec,Vec))
100e51e0e81SBarry Smith {
101*20563c6bSBarry Smith   MatShell *shell;
102e51e0e81SBarry Smith   VALIDHEADER(mat,MAT_COOKIE);
103*20563c6bSBarry Smith   shell = (MatShell *) mat->data;
104*20563c6bSBarry Smith   shell->mult = mult;
105*20563c6bSBarry Smith   return 0;
106e51e0e81SBarry Smith }
107