xref: /petsc/src/mat/impls/composite/mcomposite.c (revision db36af275f3dcf14e1746c1b2beff84d232bbd9c)
1793850ffSBarry Smith 
2af0996ceSBarry Smith #include <petsc/private/matimpl.h>        /*I "petscmat.h" I*/
3793850ffSBarry Smith 
4793850ffSBarry Smith typedef struct _Mat_CompositeLink *Mat_CompositeLink;
5793850ffSBarry Smith struct _Mat_CompositeLink {
6793850ffSBarry Smith   Mat               mat;
76d7c1e57SBarry Smith   Vec               work;
86d7c1e57SBarry Smith   Mat_CompositeLink next,prev;
9793850ffSBarry Smith };
10793850ffSBarry Smith 
11793850ffSBarry Smith typedef struct {
126d7c1e57SBarry Smith   MatCompositeType  type;
136d7c1e57SBarry Smith   Mat_CompositeLink head,tail;
14793850ffSBarry Smith   Vec               work;
15e4fc5df0SBarry Smith   PetscScalar       scale;        /* scale factor supplied with MatScale() */
16e4fc5df0SBarry Smith   Vec               left,right;   /* left and right diagonal scaling provided with MatDiagonalScale() */
17e4fc5df0SBarry Smith   Vec               leftwork,rightwork;
18793850ffSBarry Smith } Mat_Composite;
19793850ffSBarry Smith 
20793850ffSBarry Smith PetscErrorCode MatDestroy_Composite(Mat mat)
21793850ffSBarry Smith {
22793850ffSBarry Smith   PetscErrorCode    ierr;
232c33bbaeSSatish Balay   Mat_Composite     *shell = (Mat_Composite*)mat->data;
246d7c1e57SBarry Smith   Mat_CompositeLink next   = shell->head,oldnext;
25793850ffSBarry Smith 
26793850ffSBarry Smith   PetscFunctionBegin;
27793850ffSBarry Smith   while (next) {
286bf464f9SBarry Smith     ierr = MatDestroy(&next->mat);CHKERRQ(ierr);
296d7c1e57SBarry Smith     if (next->work && (!next->next || next->work != next->next->work)) {
306bf464f9SBarry Smith       ierr = VecDestroy(&next->work);CHKERRQ(ierr);
316d7c1e57SBarry Smith     }
326d7c1e57SBarry Smith     oldnext = next;
33793850ffSBarry Smith     next    = next->next;
346d7c1e57SBarry Smith     ierr    = PetscFree(oldnext);CHKERRQ(ierr);
35793850ffSBarry Smith   }
366bf464f9SBarry Smith   ierr = VecDestroy(&shell->work);CHKERRQ(ierr);
376bf464f9SBarry Smith   ierr = VecDestroy(&shell->left);CHKERRQ(ierr);
386bf464f9SBarry Smith   ierr = VecDestroy(&shell->right);CHKERRQ(ierr);
396bf464f9SBarry Smith   ierr = VecDestroy(&shell->leftwork);CHKERRQ(ierr);
406bf464f9SBarry Smith   ierr = VecDestroy(&shell->rightwork);CHKERRQ(ierr);
416bf464f9SBarry Smith   ierr = PetscFree(mat->data);CHKERRQ(ierr);
42793850ffSBarry Smith   PetscFunctionReturn(0);
43793850ffSBarry Smith }
44793850ffSBarry Smith 
456d7c1e57SBarry Smith PetscErrorCode MatMult_Composite_Multiplicative(Mat A,Vec x,Vec y)
466d7c1e57SBarry Smith {
476d7c1e57SBarry Smith   Mat_Composite     *shell = (Mat_Composite*)A->data;
486d7c1e57SBarry Smith   Mat_CompositeLink next   = shell->head;
496d7c1e57SBarry Smith   PetscErrorCode    ierr;
506d7c1e57SBarry Smith   Vec               in,out;
516d7c1e57SBarry Smith 
526d7c1e57SBarry Smith   PetscFunctionBegin;
53e32f2f54SBarry Smith   if (!next) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must provide at least one matrix with MatCompositeAddMat()");
546d7c1e57SBarry Smith   in = x;
55e4fc5df0SBarry Smith   if (shell->right) {
56e4fc5df0SBarry Smith     if (!shell->rightwork) {
57e4fc5df0SBarry Smith       ierr = VecDuplicate(shell->right,&shell->rightwork);CHKERRQ(ierr);
58e4fc5df0SBarry Smith     }
59e4fc5df0SBarry Smith     ierr = VecPointwiseMult(shell->rightwork,shell->right,in);CHKERRQ(ierr);
60e4fc5df0SBarry Smith     in   = shell->rightwork;
61e4fc5df0SBarry Smith   }
626d7c1e57SBarry Smith   while (next->next) {
636d7c1e57SBarry Smith     if (!next->work) { /* should reuse previous work if the same size */
642a7a6963SBarry Smith       ierr = MatCreateVecs(next->mat,NULL,&next->work);CHKERRQ(ierr);
656d7c1e57SBarry Smith     }
666d7c1e57SBarry Smith     out  = next->work;
676d7c1e57SBarry Smith     ierr = MatMult(next->mat,in,out);CHKERRQ(ierr);
686d7c1e57SBarry Smith     in   = out;
696d7c1e57SBarry Smith     next = next->next;
706d7c1e57SBarry Smith   }
716d7c1e57SBarry Smith   ierr = MatMult(next->mat,in,y);CHKERRQ(ierr);
72e4fc5df0SBarry Smith   if (shell->left) {
73e4fc5df0SBarry Smith     ierr = VecPointwiseMult(y,shell->left,y);CHKERRQ(ierr);
74e4fc5df0SBarry Smith   }
75e4fc5df0SBarry Smith   ierr = VecScale(y,shell->scale);CHKERRQ(ierr);
766d7c1e57SBarry Smith   PetscFunctionReturn(0);
776d7c1e57SBarry Smith }
786d7c1e57SBarry Smith 
796d7c1e57SBarry Smith PetscErrorCode MatMultTranspose_Composite_Multiplicative(Mat A,Vec x,Vec y)
806d7c1e57SBarry Smith {
816d7c1e57SBarry Smith   Mat_Composite     *shell = (Mat_Composite*)A->data;
826d7c1e57SBarry Smith   Mat_CompositeLink tail   = shell->tail;
836d7c1e57SBarry Smith   PetscErrorCode    ierr;
846d7c1e57SBarry Smith   Vec               in,out;
856d7c1e57SBarry Smith 
866d7c1e57SBarry Smith   PetscFunctionBegin;
87e32f2f54SBarry Smith   if (!tail) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must provide at least one matrix with MatCompositeAddMat()");
886d7c1e57SBarry Smith   in = x;
89e4fc5df0SBarry Smith   if (shell->left) {
90e4fc5df0SBarry Smith     if (!shell->leftwork) {
91e4fc5df0SBarry Smith       ierr = VecDuplicate(shell->left,&shell->leftwork);CHKERRQ(ierr);
92e4fc5df0SBarry Smith     }
93e4fc5df0SBarry Smith     ierr = VecPointwiseMult(shell->leftwork,shell->left,in);CHKERRQ(ierr);
94e4fc5df0SBarry Smith     in   = shell->leftwork;
95e4fc5df0SBarry Smith   }
966d7c1e57SBarry Smith   while (tail->prev) {
976d7c1e57SBarry Smith     if (!tail->prev->work) { /* should reuse previous work if the same size */
982a7a6963SBarry Smith       ierr = MatCreateVecs(tail->mat,NULL,&tail->prev->work);CHKERRQ(ierr);
996d7c1e57SBarry Smith     }
1006d7c1e57SBarry Smith     out  = tail->prev->work;
1016d7c1e57SBarry Smith     ierr = MatMultTranspose(tail->mat,in,out);CHKERRQ(ierr);
1026d7c1e57SBarry Smith     in   = out;
1036d7c1e57SBarry Smith     tail = tail->prev;
1046d7c1e57SBarry Smith   }
1056d7c1e57SBarry Smith   ierr = MatMultTranspose(tail->mat,in,y);CHKERRQ(ierr);
106e4fc5df0SBarry Smith   if (shell->right) {
107e4fc5df0SBarry Smith     ierr = VecPointwiseMult(y,shell->right,y);CHKERRQ(ierr);
108e4fc5df0SBarry Smith   }
109e4fc5df0SBarry Smith   ierr = VecScale(y,shell->scale);CHKERRQ(ierr);
1106d7c1e57SBarry Smith   PetscFunctionReturn(0);
1116d7c1e57SBarry Smith }
1126d7c1e57SBarry Smith 
113793850ffSBarry Smith PetscErrorCode MatMult_Composite(Mat A,Vec x,Vec y)
114793850ffSBarry Smith {
115793850ffSBarry Smith   Mat_Composite     *shell = (Mat_Composite*)A->data;
116793850ffSBarry Smith   Mat_CompositeLink next   = shell->head;
117793850ffSBarry Smith   PetscErrorCode    ierr;
118e4fc5df0SBarry Smith   Vec               in;
119793850ffSBarry Smith 
120793850ffSBarry Smith   PetscFunctionBegin;
121e32f2f54SBarry Smith   if (!next) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must provide at least one matrix with MatCompositeAddMat()");
122e4fc5df0SBarry Smith   in = x;
123e4fc5df0SBarry Smith   if (shell->right) {
124e4fc5df0SBarry Smith     if (!shell->rightwork) {
125e4fc5df0SBarry Smith       ierr = VecDuplicate(shell->right,&shell->rightwork);CHKERRQ(ierr);
126793850ffSBarry Smith     }
127e4fc5df0SBarry Smith     ierr = VecPointwiseMult(shell->rightwork,shell->right,in);CHKERRQ(ierr);
128e4fc5df0SBarry Smith     in   = shell->rightwork;
129e4fc5df0SBarry Smith   }
130e4fc5df0SBarry Smith   ierr = MatMult(next->mat,in,y);CHKERRQ(ierr);
131e4fc5df0SBarry Smith   while ((next = next->next)) {
132e4fc5df0SBarry Smith     ierr = MatMultAdd(next->mat,in,y,y);CHKERRQ(ierr);
133e4fc5df0SBarry Smith   }
134e4fc5df0SBarry Smith   if (shell->left) {
135e4fc5df0SBarry Smith     ierr = VecPointwiseMult(y,shell->left,y);CHKERRQ(ierr);
136e4fc5df0SBarry Smith   }
137e4fc5df0SBarry Smith   ierr = VecScale(y,shell->scale);CHKERRQ(ierr);
138793850ffSBarry Smith   PetscFunctionReturn(0);
139793850ffSBarry Smith }
140793850ffSBarry Smith 
141793850ffSBarry Smith PetscErrorCode MatMultTranspose_Composite(Mat A,Vec x,Vec y)
142793850ffSBarry Smith {
143793850ffSBarry Smith   Mat_Composite     *shell = (Mat_Composite*)A->data;
144793850ffSBarry Smith   Mat_CompositeLink next   = shell->head;
145793850ffSBarry Smith   PetscErrorCode    ierr;
146e4fc5df0SBarry Smith   Vec               in;
147793850ffSBarry Smith 
148793850ffSBarry Smith   PetscFunctionBegin;
149e32f2f54SBarry Smith   if (!next) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must provide at least one matrix with MatCompositeAddMat()");
150e4fc5df0SBarry Smith   in = x;
151e4fc5df0SBarry Smith   if (shell->left) {
152e4fc5df0SBarry Smith     if (!shell->leftwork) {
153e4fc5df0SBarry Smith       ierr = VecDuplicate(shell->left,&shell->leftwork);CHKERRQ(ierr);
154793850ffSBarry Smith     }
155e4fc5df0SBarry Smith     ierr = VecPointwiseMult(shell->leftwork,shell->left,in);CHKERRQ(ierr);
156e4fc5df0SBarry Smith     in   = shell->leftwork;
157e4fc5df0SBarry Smith   }
158e4fc5df0SBarry Smith   ierr = MatMultTranspose(next->mat,in,y);CHKERRQ(ierr);
159e4fc5df0SBarry Smith   while ((next = next->next)) {
160e4fc5df0SBarry Smith     ierr = MatMultTransposeAdd(next->mat,in,y,y);CHKERRQ(ierr);
161e4fc5df0SBarry Smith   }
162e4fc5df0SBarry Smith   if (shell->right) {
163e4fc5df0SBarry Smith     ierr = VecPointwiseMult(y,shell->right,y);CHKERRQ(ierr);
164e4fc5df0SBarry Smith   }
165e4fc5df0SBarry Smith   ierr = VecScale(y,shell->scale);CHKERRQ(ierr);
166793850ffSBarry Smith   PetscFunctionReturn(0);
167793850ffSBarry Smith }
168793850ffSBarry Smith 
169793850ffSBarry Smith PetscErrorCode MatGetDiagonal_Composite(Mat A,Vec v)
170793850ffSBarry Smith {
171793850ffSBarry Smith   Mat_Composite     *shell = (Mat_Composite*)A->data;
172793850ffSBarry Smith   Mat_CompositeLink next   = shell->head;
173793850ffSBarry Smith   PetscErrorCode    ierr;
174793850ffSBarry Smith 
175793850ffSBarry Smith   PetscFunctionBegin;
176e32f2f54SBarry Smith   if (!next) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must provide at least one matrix with MatCompositeAddMat()");
177e32f2f54SBarry Smith   if (shell->right || shell->left) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot get diagonal if left or right scaling");
178e4fc5df0SBarry Smith 
179793850ffSBarry Smith   ierr = MatGetDiagonal(next->mat,v);CHKERRQ(ierr);
180793850ffSBarry Smith   if (next->next && !shell->work) {
181793850ffSBarry Smith     ierr = VecDuplicate(v,&shell->work);CHKERRQ(ierr);
182793850ffSBarry Smith   }
183793850ffSBarry Smith   while ((next = next->next)) {
184793850ffSBarry Smith     ierr = MatGetDiagonal(next->mat,shell->work);CHKERRQ(ierr);
185793850ffSBarry Smith     ierr = VecAXPY(v,1.0,shell->work);CHKERRQ(ierr);
186793850ffSBarry Smith   }
187e4fc5df0SBarry Smith   ierr = VecScale(v,shell->scale);CHKERRQ(ierr);
188793850ffSBarry Smith   PetscFunctionReturn(0);
189793850ffSBarry Smith }
190793850ffSBarry Smith 
191793850ffSBarry Smith PetscErrorCode MatAssemblyEnd_Composite(Mat Y,MatAssemblyType t)
192793850ffSBarry Smith {
193b52f573bSBarry Smith   PetscErrorCode ierr;
194ace3abfcSBarry Smith   PetscBool      flg = PETSC_FALSE;
195b52f573bSBarry Smith 
196793850ffSBarry Smith   PetscFunctionBegin;
197c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)Y)->options,((PetscObject)Y)->prefix,"-mat_composite_merge",&flg,NULL);CHKERRQ(ierr);
198b52f573bSBarry Smith   if (flg) {
199b52f573bSBarry Smith     ierr = MatCompositeMerge(Y);CHKERRQ(ierr);
200b52f573bSBarry Smith   }
201793850ffSBarry Smith   PetscFunctionReturn(0);
202793850ffSBarry Smith }
203793850ffSBarry Smith 
204e4fc5df0SBarry Smith PetscErrorCode MatScale_Composite(Mat inA,PetscScalar alpha)
205e4fc5df0SBarry Smith {
206e4fc5df0SBarry Smith   Mat_Composite *a = (Mat_Composite*)inA->data;
207e4fc5df0SBarry Smith 
208e4fc5df0SBarry Smith   PetscFunctionBegin;
209321429dbSBarry Smith   a->scale *= alpha;
210e4fc5df0SBarry Smith   PetscFunctionReturn(0);
211e4fc5df0SBarry Smith }
212e4fc5df0SBarry Smith 
213e4fc5df0SBarry Smith PetscErrorCode MatDiagonalScale_Composite(Mat inA,Vec left,Vec right)
214e4fc5df0SBarry Smith {
215e4fc5df0SBarry Smith   Mat_Composite  *a = (Mat_Composite*)inA->data;
216e4fc5df0SBarry Smith   PetscErrorCode ierr;
217e4fc5df0SBarry Smith 
218e4fc5df0SBarry Smith   PetscFunctionBegin;
219e4fc5df0SBarry Smith   if (left) {
220321429dbSBarry Smith     if (!a->left) {
221e4fc5df0SBarry Smith       ierr = VecDuplicate(left,&a->left);CHKERRQ(ierr);
222e4fc5df0SBarry Smith       ierr = VecCopy(left,a->left);CHKERRQ(ierr);
223321429dbSBarry Smith     } else {
224321429dbSBarry Smith       ierr = VecPointwiseMult(a->left,left,a->left);CHKERRQ(ierr);
225321429dbSBarry Smith     }
226e4fc5df0SBarry Smith   }
227e4fc5df0SBarry Smith   if (right) {
228321429dbSBarry Smith     if (!a->right) {
229e4fc5df0SBarry Smith       ierr = VecDuplicate(right,&a->right);CHKERRQ(ierr);
230e4fc5df0SBarry Smith       ierr = VecCopy(right,a->right);CHKERRQ(ierr);
231321429dbSBarry Smith     } else {
232321429dbSBarry Smith       ierr = VecPointwiseMult(a->right,right,a->right);CHKERRQ(ierr);
233321429dbSBarry Smith     }
234e4fc5df0SBarry Smith   }
235e4fc5df0SBarry Smith   PetscFunctionReturn(0);
236e4fc5df0SBarry Smith }
237793850ffSBarry Smith 
238793850ffSBarry Smith static struct _MatOps MatOps_Values = {0,
239793850ffSBarry Smith                                        0,
240793850ffSBarry Smith                                        0,
241793850ffSBarry Smith                                        MatMult_Composite,
242793850ffSBarry Smith                                        0,
243793850ffSBarry Smith                                 /*  5*/ MatMultTranspose_Composite,
244793850ffSBarry Smith                                        0,
245793850ffSBarry Smith                                        0,
246793850ffSBarry Smith                                        0,
247793850ffSBarry Smith                                        0,
248793850ffSBarry Smith                                 /* 10*/ 0,
249793850ffSBarry Smith                                        0,
250793850ffSBarry Smith                                        0,
251793850ffSBarry Smith                                        0,
252793850ffSBarry Smith                                        0,
253793850ffSBarry Smith                                 /* 15*/ 0,
254793850ffSBarry Smith                                        0,
255793850ffSBarry Smith                                        MatGetDiagonal_Composite,
256e4fc5df0SBarry Smith                                        MatDiagonalScale_Composite,
257793850ffSBarry Smith                                        0,
258793850ffSBarry Smith                                 /* 20*/ 0,
259793850ffSBarry Smith                                        MatAssemblyEnd_Composite,
260793850ffSBarry Smith                                        0,
261793850ffSBarry Smith                                        0,
262d519adbfSMatthew Knepley                                /* 24*/ 0,
263793850ffSBarry Smith                                        0,
264793850ffSBarry Smith                                        0,
265793850ffSBarry Smith                                        0,
266793850ffSBarry Smith                                        0,
267d519adbfSMatthew Knepley                                /* 29*/ 0,
268793850ffSBarry Smith                                        0,
269793850ffSBarry Smith                                        0,
270793850ffSBarry Smith                                        0,
271793850ffSBarry Smith                                        0,
272d519adbfSMatthew Knepley                                /* 34*/ 0,
273793850ffSBarry Smith                                        0,
274793850ffSBarry Smith                                        0,
275793850ffSBarry Smith                                        0,
276793850ffSBarry Smith                                        0,
277d519adbfSMatthew Knepley                                /* 39*/ 0,
278793850ffSBarry Smith                                        0,
279793850ffSBarry Smith                                        0,
280793850ffSBarry Smith                                        0,
281793850ffSBarry Smith                                        0,
282d519adbfSMatthew Knepley                                /* 44*/ 0,
283e4fc5df0SBarry Smith                                        MatScale_Composite,
2847d68702bSBarry Smith                                        MatShift_Basic,
285793850ffSBarry Smith                                        0,
286793850ffSBarry Smith                                        0,
287d519adbfSMatthew Knepley                                /* 49*/ 0,
288793850ffSBarry Smith                                        0,
289793850ffSBarry Smith                                        0,
290793850ffSBarry Smith                                        0,
291793850ffSBarry Smith                                        0,
292d519adbfSMatthew Knepley                                /* 54*/ 0,
293793850ffSBarry Smith                                        0,
294793850ffSBarry Smith                                        0,
295793850ffSBarry Smith                                        0,
296793850ffSBarry Smith                                        0,
297d519adbfSMatthew Knepley                                /* 59*/ 0,
298793850ffSBarry Smith                                        MatDestroy_Composite,
299793850ffSBarry Smith                                        0,
300793850ffSBarry Smith                                        0,
301793850ffSBarry Smith                                        0,
302d519adbfSMatthew Knepley                                /* 64*/ 0,
303793850ffSBarry Smith                                        0,
304793850ffSBarry Smith                                        0,
305793850ffSBarry Smith                                        0,
306793850ffSBarry Smith                                        0,
307d519adbfSMatthew Knepley                                /* 69*/ 0,
308793850ffSBarry Smith                                        0,
309793850ffSBarry Smith                                        0,
310793850ffSBarry Smith                                        0,
311793850ffSBarry Smith                                        0,
312d519adbfSMatthew Knepley                                /* 74*/ 0,
313793850ffSBarry Smith                                        0,
314793850ffSBarry Smith                                        0,
315793850ffSBarry Smith                                        0,
316793850ffSBarry Smith                                        0,
317d519adbfSMatthew Knepley                                /* 79*/ 0,
318793850ffSBarry Smith                                        0,
319793850ffSBarry Smith                                        0,
320793850ffSBarry Smith                                        0,
321793850ffSBarry Smith                                        0,
322d519adbfSMatthew Knepley                                /* 84*/ 0,
323793850ffSBarry Smith                                        0,
324793850ffSBarry Smith                                        0,
325793850ffSBarry Smith                                        0,
326793850ffSBarry Smith                                        0,
327d519adbfSMatthew Knepley                                /* 89*/ 0,
328793850ffSBarry Smith                                        0,
329793850ffSBarry Smith                                        0,
330793850ffSBarry Smith                                        0,
331793850ffSBarry Smith                                        0,
332d519adbfSMatthew Knepley                                /* 94*/ 0,
333793850ffSBarry Smith                                        0,
334793850ffSBarry Smith                                        0,
3353964eb88SJed Brown                                        0,
3363964eb88SJed Brown                                        0,
3373964eb88SJed Brown                                 /*99*/ 0,
3383964eb88SJed Brown                                        0,
3393964eb88SJed Brown                                        0,
3403964eb88SJed Brown                                        0,
3413964eb88SJed Brown                                        0,
3423964eb88SJed Brown                                /*104*/ 0,
3433964eb88SJed Brown                                        0,
3443964eb88SJed Brown                                        0,
3453964eb88SJed Brown                                        0,
3463964eb88SJed Brown                                        0,
3473964eb88SJed Brown                                /*109*/ 0,
3483964eb88SJed Brown                                        0,
3493964eb88SJed Brown                                        0,
3503964eb88SJed Brown                                        0,
3513964eb88SJed Brown                                        0,
3523964eb88SJed Brown                                /*114*/ 0,
3533964eb88SJed Brown                                        0,
3543964eb88SJed Brown                                        0,
3553964eb88SJed Brown                                        0,
3563964eb88SJed Brown                                        0,
3573964eb88SJed Brown                                /*119*/ 0,
3583964eb88SJed Brown                                        0,
3593964eb88SJed Brown                                        0,
3603964eb88SJed Brown                                        0,
3613964eb88SJed Brown                                        0,
3623964eb88SJed Brown                                /*124*/ 0,
3633964eb88SJed Brown                                        0,
3643964eb88SJed Brown                                        0,
3653964eb88SJed Brown                                        0,
3663964eb88SJed Brown                                        0,
3673964eb88SJed Brown                                /*129*/ 0,
3683964eb88SJed Brown                                        0,
3693964eb88SJed Brown                                        0,
3703964eb88SJed Brown                                        0,
3713964eb88SJed Brown                                        0,
3723964eb88SJed Brown                                /*134*/ 0,
3733964eb88SJed Brown                                        0,
3743964eb88SJed Brown                                        0,
3753964eb88SJed Brown                                        0,
3763964eb88SJed Brown                                        0,
3773964eb88SJed Brown                                /*139*/ 0,
378f9426fe0SMark Adams                                        0,
3793964eb88SJed Brown                                        0
3803964eb88SJed Brown };
381793850ffSBarry Smith 
382793850ffSBarry Smith /*MC
3836d7c1e57SBarry Smith    MATCOMPOSITE - A matrix defined by the sum (or product) of one or more matrices (all matrices are of same size and parallel layout).
3846d7c1e57SBarry Smith 
3856d7c1e57SBarry Smith    Notes: to use the product of the matrices call MatCompositeSetType(mat,MAT_COMPOSITE_MULTIPLICATIVE);
386793850ffSBarry Smith 
387793850ffSBarry Smith   Level: advanced
388793850ffSBarry Smith 
3896d7c1e57SBarry Smith .seealso: MatCreateComposite(), MatCompositeAddMat(), MatSetType(), MatCompositeMerge(), MatCompositeSetType(), MatCompositeType
390793850ffSBarry Smith M*/
391793850ffSBarry Smith 
3928cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatCreate_Composite(Mat A)
393793850ffSBarry Smith {
394793850ffSBarry Smith   Mat_Composite  *b;
395793850ffSBarry Smith   PetscErrorCode ierr;
396793850ffSBarry Smith 
397793850ffSBarry Smith   PetscFunctionBegin;
398b00a9115SJed Brown   ierr    = PetscNewLog(A,&b);CHKERRQ(ierr);
399793850ffSBarry Smith   A->data = (void*)b;
40038f2d2fdSLisandro Dalcin   ierr    = PetscMemcpy(A->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
401793850ffSBarry Smith 
40226283091SBarry Smith   ierr = PetscLayoutSetUp(A->rmap);CHKERRQ(ierr);
40326283091SBarry Smith   ierr = PetscLayoutSetUp(A->cmap);CHKERRQ(ierr);
404793850ffSBarry Smith 
405793850ffSBarry Smith   A->assembled    = PETSC_TRUE;
4063db03f37SJed Brown   A->preallocated = PETSC_TRUE;
4076d7c1e57SBarry Smith   b->type         = MAT_COMPOSITE_ADDITIVE;
408e4fc5df0SBarry Smith   b->scale        = 1.0;
409793850ffSBarry Smith   ierr            = PetscObjectChangeTypeName((PetscObject)A,MATCOMPOSITE);CHKERRQ(ierr);
410793850ffSBarry Smith   PetscFunctionReturn(0);
411793850ffSBarry Smith }
412793850ffSBarry Smith 
4132c0821f3SBarry Smith /*@
414793850ffSBarry Smith    MatCreateComposite - Creates a matrix as the sum of zero or more matrices
415793850ffSBarry Smith 
416793850ffSBarry Smith   Collective on MPI_Comm
417793850ffSBarry Smith 
418793850ffSBarry Smith    Input Parameters:
419793850ffSBarry Smith +  comm - MPI communicator
420793850ffSBarry Smith .  nmat - number of matrices to put in
421793850ffSBarry Smith -  mats - the matrices
422793850ffSBarry Smith 
423793850ffSBarry Smith    Output Parameter:
424*db36af27SMatthew G. Knepley .  mat - the matrix
425793850ffSBarry Smith 
426793850ffSBarry Smith    Level: advanced
427793850ffSBarry Smith 
428793850ffSBarry Smith    Notes:
429793850ffSBarry Smith      Alternative construction
430793850ffSBarry Smith $       MatCreate(comm,&mat);
431793850ffSBarry Smith $       MatSetSizes(mat,m,n,M,N);
432793850ffSBarry Smith $       MatSetType(mat,MATCOMPOSITE);
433793850ffSBarry Smith $       MatCompositeAddMat(mat,mats[0]);
434793850ffSBarry Smith $       ....
435793850ffSBarry Smith $       MatCompositeAddMat(mat,mats[nmat-1]);
436b52f573bSBarry Smith $       MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);
437b52f573bSBarry Smith $       MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);
438793850ffSBarry Smith 
4396d7c1e57SBarry Smith      For the multiplicative form the product is mat[nmat-1]*mat[nmat-2]*....*mat[0]
4406d7c1e57SBarry Smith 
4416d7c1e57SBarry Smith .seealso: MatDestroy(), MatMult(), MatCompositeAddMat(), MatCompositeMerge(), MatCompositeSetType(), MatCompositeType
442793850ffSBarry Smith 
443793850ffSBarry Smith @*/
4447087cfbeSBarry Smith PetscErrorCode  MatCreateComposite(MPI_Comm comm,PetscInt nmat,const Mat *mats,Mat *mat)
445793850ffSBarry Smith {
446793850ffSBarry Smith   PetscErrorCode ierr;
447793850ffSBarry Smith   PetscInt       m,n,M,N,i;
448793850ffSBarry Smith 
449793850ffSBarry Smith   PetscFunctionBegin;
450e32f2f54SBarry Smith   if (nmat < 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Must pass in at least one matrix");
451f3f84630SBarry Smith   PetscValidPointer(mat,3);
452793850ffSBarry Smith 
453793850ffSBarry Smith   ierr = MatGetLocalSize(mats[0],&m,&n);CHKERRQ(ierr);
454793850ffSBarry Smith   ierr = MatGetSize(mats[0],&M,&N);CHKERRQ(ierr);
455793850ffSBarry Smith   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
456793850ffSBarry Smith   ierr = MatSetSizes(*mat,m,n,M,N);CHKERRQ(ierr);
457793850ffSBarry Smith   ierr = MatSetType(*mat,MATCOMPOSITE);CHKERRQ(ierr);
458793850ffSBarry Smith   for (i=0; i<nmat; i++) {
459793850ffSBarry Smith     ierr = MatCompositeAddMat(*mat,mats[i]);CHKERRQ(ierr);
460793850ffSBarry Smith   }
461b52f573bSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
462b52f573bSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
463793850ffSBarry Smith   PetscFunctionReturn(0);
464793850ffSBarry Smith }
465793850ffSBarry Smith 
466793850ffSBarry Smith /*@
467793850ffSBarry Smith     MatCompositeAddMat - add another matrix to a composite matrix
468793850ffSBarry Smith 
469793850ffSBarry Smith    Collective on Mat
470793850ffSBarry Smith 
471793850ffSBarry Smith     Input Parameters:
472793850ffSBarry Smith +   mat - the composite matrix
473793850ffSBarry Smith -   smat - the partial matrix
474793850ffSBarry Smith 
475793850ffSBarry Smith    Level: advanced
476793850ffSBarry Smith 
477793850ffSBarry Smith .seealso: MatCreateComposite()
478793850ffSBarry Smith @*/
4797087cfbeSBarry Smith PetscErrorCode  MatCompositeAddMat(Mat mat,Mat smat)
480793850ffSBarry Smith {
48138f2d2fdSLisandro Dalcin   Mat_Composite     *shell;
482793850ffSBarry Smith   PetscErrorCode    ierr;
483793850ffSBarry Smith   Mat_CompositeLink ilink,next;
484793850ffSBarry Smith 
485793850ffSBarry Smith   PetscFunctionBegin;
4860700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
4870700a824SBarry Smith   PetscValidHeaderSpecific(smat,MAT_CLASSID,2);
488b00a9115SJed Brown   ierr        = PetscNewLog(mat,&ilink);CHKERRQ(ierr);
489793850ffSBarry Smith   ilink->next = 0;
490793850ffSBarry Smith   ierr        = PetscObjectReference((PetscObject)smat);CHKERRQ(ierr);
491c3122656SLisandro Dalcin   ilink->mat  = smat;
492793850ffSBarry Smith 
49338f2d2fdSLisandro Dalcin   shell = (Mat_Composite*)mat->data;
494793850ffSBarry Smith   next  = shell->head;
4952205254eSKarl Rupp   if (!next) shell->head = ilink;
4962205254eSKarl Rupp   else {
497793850ffSBarry Smith     while (next->next) {
498793850ffSBarry Smith       next = next->next;
499793850ffSBarry Smith     }
500793850ffSBarry Smith     next->next  = ilink;
5016d7c1e57SBarry Smith     ilink->prev = next;
5026d7c1e57SBarry Smith   }
5036d7c1e57SBarry Smith   shell->tail = ilink;
5046d7c1e57SBarry Smith   PetscFunctionReturn(0);
5056d7c1e57SBarry Smith }
5066d7c1e57SBarry Smith 
5072c0821f3SBarry Smith /*@
5086d7c1e57SBarry Smith    MatCompositeSetType - Indicates if the matrix is defined as the sum of a set of matrices or the product
5096d7c1e57SBarry Smith 
5106d7c1e57SBarry Smith   Collective on MPI_Comm
5116d7c1e57SBarry Smith 
5126d7c1e57SBarry Smith    Input Parameters:
5136d7c1e57SBarry Smith .  mat - the composite matrix
5146d7c1e57SBarry Smith 
5156d7c1e57SBarry Smith 
5166d7c1e57SBarry Smith    Level: advanced
5176d7c1e57SBarry Smith 
5186d7c1e57SBarry Smith    Notes:
5196d7c1e57SBarry Smith       The MatType of the resulting matrix will be the same as the MatType of the FIRST
5206d7c1e57SBarry Smith     matrix in the composite matrix.
5216d7c1e57SBarry Smith 
5226d7c1e57SBarry Smith .seealso: MatDestroy(), MatMult(), MatCompositeAddMat(), MatCreateComposite(), MATCOMPOSITE
5236d7c1e57SBarry Smith 
5246d7c1e57SBarry Smith @*/
5257087cfbeSBarry Smith PetscErrorCode  MatCompositeSetType(Mat mat,MatCompositeType type)
5266d7c1e57SBarry Smith {
5276d7c1e57SBarry Smith   Mat_Composite  *b = (Mat_Composite*)mat->data;
528ace3abfcSBarry Smith   PetscBool      flg;
5296d7c1e57SBarry Smith   PetscErrorCode ierr;
5306d7c1e57SBarry Smith 
5316d7c1e57SBarry Smith   PetscFunctionBegin;
532251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)mat,MATCOMPOSITE,&flg);CHKERRQ(ierr);
533e32f2f54SBarry Smith   if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Can only use with composite matrix");
5346d7c1e57SBarry Smith   if (type == MAT_COMPOSITE_MULTIPLICATIVE) {
5356d7c1e57SBarry Smith     mat->ops->getdiagonal   = 0;
5366d7c1e57SBarry Smith     mat->ops->mult          = MatMult_Composite_Multiplicative;
5376d7c1e57SBarry Smith     mat->ops->multtranspose = MatMultTranspose_Composite_Multiplicative;
5386d7c1e57SBarry Smith     b->type                 = MAT_COMPOSITE_MULTIPLICATIVE;
5396d7c1e57SBarry Smith   } else {
5406d7c1e57SBarry Smith     mat->ops->getdiagonal   = MatGetDiagonal_Composite;
5416d7c1e57SBarry Smith     mat->ops->mult          = MatMult_Composite;
5426d7c1e57SBarry Smith     mat->ops->multtranspose = MatMultTranspose_Composite;
5436d7c1e57SBarry Smith     b->type                 = MAT_COMPOSITE_ADDITIVE;
544793850ffSBarry Smith   }
545793850ffSBarry Smith   PetscFunctionReturn(0);
546793850ffSBarry Smith }
547793850ffSBarry Smith 
5486d7c1e57SBarry Smith 
5497e8dbf47SJed Brown /*@
550b52f573bSBarry Smith    MatCompositeMerge - Given a composite matrix, replaces it with a "regular" matrix
551b52f573bSBarry Smith      by summing all the matrices inside the composite matrix.
552b52f573bSBarry Smith 
553b52f573bSBarry Smith   Collective on MPI_Comm
554b52f573bSBarry Smith 
555b52f573bSBarry Smith    Input Parameters:
556b52f573bSBarry Smith .  mat - the composite matrix
557b52f573bSBarry Smith 
558b52f573bSBarry Smith 
559b52f573bSBarry Smith    Options Database:
560b52f573bSBarry Smith .  -mat_composite_merge  (you must call MatAssemblyBegin()/MatAssemblyEnd() to have this checked)
561b52f573bSBarry Smith 
562b52f573bSBarry Smith    Level: advanced
563b52f573bSBarry Smith 
564b52f573bSBarry Smith    Notes:
565b52f573bSBarry Smith       The MatType of the resulting matrix will be the same as the MatType of the FIRST
566b52f573bSBarry Smith     matrix in the composite matrix.
567b52f573bSBarry Smith 
568b52f573bSBarry Smith .seealso: MatDestroy(), MatMult(), MatCompositeAddMat(), MatCreateComposite(), MATCOMPOSITE
569b52f573bSBarry Smith 
570b52f573bSBarry Smith @*/
5717087cfbeSBarry Smith PetscErrorCode  MatCompositeMerge(Mat mat)
572b52f573bSBarry Smith {
573b52f573bSBarry Smith   Mat_Composite     *shell = (Mat_Composite*)mat->data;
5746d7c1e57SBarry Smith   Mat_CompositeLink next   = shell->head, prev = shell->tail;
575b52f573bSBarry Smith   PetscErrorCode    ierr;
5766d7c1e57SBarry Smith   Mat               tmat,newmat;
5771ba60692SJed Brown   Vec               left,right;
5781ba60692SJed Brown   PetscScalar       scale;
579b52f573bSBarry Smith 
580b52f573bSBarry Smith   PetscFunctionBegin;
581e32f2f54SBarry Smith   if (!next) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must provide at least one matrix with MatCompositeAddMat()");
582b52f573bSBarry Smith 
583b52f573bSBarry Smith   PetscFunctionBegin;
5846d7c1e57SBarry Smith   if (shell->type == MAT_COMPOSITE_ADDITIVE) {
585b52f573bSBarry Smith     ierr = MatDuplicate(next->mat,MAT_COPY_VALUES,&tmat);CHKERRQ(ierr);
586b52f573bSBarry Smith     while ((next = next->next)) {
587b52f573bSBarry Smith       ierr = MatAXPY(tmat,1.0,next->mat,DIFFERENT_NONZERO_PATTERN);CHKERRQ(ierr);
588b52f573bSBarry Smith     }
5896d7c1e57SBarry Smith   } else {
5906d7c1e57SBarry Smith     ierr = MatDuplicate(next->mat,MAT_COPY_VALUES,&tmat);CHKERRQ(ierr);
5916d7c1e57SBarry Smith     while ((prev = prev->prev)) {
5926d7c1e57SBarry Smith       ierr = MatMatMult(tmat,prev->mat,MAT_INITIAL_MATRIX,PETSC_DECIDE,&newmat);CHKERRQ(ierr);
5936bf464f9SBarry Smith       ierr = MatDestroy(&tmat);CHKERRQ(ierr);
5946d7c1e57SBarry Smith       tmat = newmat;
5956d7c1e57SBarry Smith     }
5966d7c1e57SBarry Smith   }
5971ba60692SJed Brown 
5981ba60692SJed Brown   scale = shell->scale;
5991ba60692SJed Brown   if ((left = shell->left)) {ierr = PetscObjectReference((PetscObject)left);CHKERRQ(ierr);}
6001ba60692SJed Brown   if ((right = shell->right)) {ierr = PetscObjectReference((PetscObject)right);CHKERRQ(ierr);}
6011ba60692SJed Brown 
60228be2f97SBarry Smith   ierr = MatHeaderReplace(mat,&tmat);CHKERRQ(ierr);
6031ba60692SJed Brown 
6041ba60692SJed Brown   ierr = MatDiagonalScale(mat,left,right);CHKERRQ(ierr);
6051ba60692SJed Brown   ierr = MatScale(mat,scale);CHKERRQ(ierr);
6061ba60692SJed Brown   ierr = VecDestroy(&left);CHKERRQ(ierr);
6071ba60692SJed Brown   ierr = VecDestroy(&right);CHKERRQ(ierr);
608b52f573bSBarry Smith   PetscFunctionReturn(0);
609b52f573bSBarry Smith }
610