xref: /petsc/src/tao/interface/taosolver_hj.c (revision 87f595a510d358797c1cc8101e6f6c6930cb072f)
1 #include "tao-private/taosolver_impl.h" /*I "taosolver.h" I*/
2 
3 #undef __FUNCT__
4 #define __FUNCT__ "TaoSetHessianRoutine"
5 /*@C
6    TaoSetHessianRoutine - Sets the function to compute the Hessian as well as the location to store the matrix.
7 
8    Logically collective on TaoSolver
9 
10    Input Parameters:
11 +  tao - the TaoSolver context
12 .  H - Matrix used for the hessian
13 .  Hpre - Matrix that will be used operated on by preconditioner, can be same as H
14 .  hess - Hessian evaluation routine
15 -  ctx - [optional] user-defined context for private data for the
16          Hessian evaluation routine (may be NULL)
17 
18    Calling sequence of hess:
19 $    hess (TaoSolver tao,Vec x,Mat *H,Mat *Hpre,MatStructure *flag,void *ctx);
20 
21 +  tao - the TaoSolver  context
22 .  x - input vector
23 .  H - Hessian matrix
24 .  Hpre - preconditioner matrix, usually the same as H
25 .  flag - flag indicating information about the preconditioner matrix
26    structure (see below)
27 -  ctx - [optional] user-defined Hessian context
28 
29 
30    Notes:
31 
32    The function hess() takes Mat * as the matrix arguments rather than Mat.
33    This allows the Hessian evaluation routine to replace A and/or B with a
34    completely new new matrix structure (not just different matrix elements)
35    when appropriate, for instance, if the nonzero structure is changing
36    throughout the global iterations.
37 
38    The flag can be used to eliminate unnecessary work in the preconditioner
39    during the repeated solution of linear systems of the same size.  The
40    available options are
41 $    SAME_PRECONDITIONER -
42 $      Hpre is identical during successive linear solves.
43 $      This option is intended for folks who are using
44 $      different Amat and Pmat matrices and want to reuse the
45 $      same preconditioner matrix.  For example, this option
46 $      saves work by not recomputing incomplete factorization
47 $      for ILU/ICC preconditioners.
48 $    SAME_NONZERO_PATTERN -
49 $      Hpre has the same nonzero structure during
50 $      successive linear solves.
51 $    DIFFERENT_NONZERO_PATTERN -
52 $      Hpre does not have the same nonzero structure.
53 
54    Caution:
55    If you specify SAME_NONZERO_PATTERN, the software believes your assertion
56    and does not check the structure of the matrix.  If you erroneously
57    claim that the structure is the same when it actually is not, the new
58    preconditioner will not function correctly.  Thus, use this optimization
59    feature carefully!
60 
61    If in doubt about whether your preconditioner matrix has changed
62    structure or not, use the flag DIFFERENT_NONZERO_PATTERN.
63 
64    Level: beginner
65 
66 @*/
67 PetscErrorCode TaoSetHessianRoutine(TaoSolver tao, Mat H, Mat Hpre, PetscErrorCode (*func)(TaoSolver, Vec, Mat*, Mat *, MatStructure *, void*), void *ctx)
68 {
69     PetscErrorCode ierr;
70     PetscFunctionBegin;
71     PetscValidHeaderSpecific(tao,TAOSOLVER_CLASSID,1);
72     if (H) {
73 	PetscValidHeaderSpecific(H,MAT_CLASSID,2);
74 	PetscCheckSameComm(tao,1,H,2);
75     }
76     if (Hpre) {
77 	PetscValidHeaderSpecific(Hpre,MAT_CLASSID,3);
78 	PetscCheckSameComm(tao,1,Hpre,3);
79     }
80     if (ctx) {
81 	tao->user_hessP = ctx;
82     }
83     if (func) {
84 	tao->ops->computehessian = func;
85     }
86 
87 
88     if (H) {
89 	ierr = PetscObjectReference((PetscObject)H); CHKERRQ(ierr);
90 	if (tao->hessian) {   ierr = MatDestroy(&tao->hessian); CHKERRQ(ierr);}
91 	tao->hessian = H;
92     }
93     if (Hpre) {
94 	ierr = PetscObjectReference((PetscObject)Hpre); CHKERRQ(ierr);
95 	if (tao->hessian_pre) { ierr = MatDestroy(&tao->hessian_pre); CHKERRQ(ierr);}
96 	tao->hessian_pre=Hpre;
97     }
98     PetscFunctionReturn(0);
99 }
100 
101 #undef __FUNCT__
102 #define __FUNCT__ "TaoComputeHessian"
103 /*@C
104    TaoComputeHessian - Computes the Hessian matrix that has been
105    set with TaoSetHessianRoutine().
106 
107    Collective on TaoSolver
108 
109    Input Parameters:
110 +  solver - the TaoSolver solver context
111 -  xx - input vector
112 
113    Output Parameters:
114 +  H - Hessian matrix
115 .  Hpre - Preconditioning matrix
116 -  flag - flag indicating matrix structure (SAME_NONZERO_PATTERN, DIFFERENT_NONZERO_PATTERN, or SAME_PRECONDITIONER)
117 
118    Notes:
119    Most users should not need to explicitly call this routine, as it
120    is used internally within the minimization solvers.
121 
122    TaoComputeHessian() is typically used within minimization
123    implementations, so most users would not generally call this routine
124    themselves.
125 
126    Level: developer
127 
128 .seealso:  TaoComputeObjective(), TaoComputeObjectiveAndGradient(), TaoSetHessian()
129 
130 @*/
131 PetscErrorCode TaoComputeHessian(TaoSolver tao, Vec X, Mat *H, Mat *Hpre, MatStructure *flg)
132 {
133   PetscErrorCode ierr;
134 
135   PetscFunctionBegin;
136   PetscValidHeaderSpecific(tao,TAOSOLVER_CLASSID,1);
137   PetscValidHeaderSpecific(X, VEC_CLASSID,2);
138   PetscValidPointer(flg,5);
139   PetscCheckSameComm(tao,1,X,2);
140 
141   if (!tao->ops->computehessian) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TaoSetHessian() first");
142   *flg = DIFFERENT_NONZERO_PATTERN;
143   ++tao->nhess;
144   ierr = PetscLogEventBegin(TaoSolver_HessianEval,tao,X,*H,*Hpre); CHKERRQ(ierr);
145   PetscStackPush("TaoSolver user Hessian function");
146   CHKMEMQ;
147   ierr = (*tao->ops->computehessian)(tao,X,H,Hpre,flg,tao->user_hessP); CHKERRQ(ierr);
148   CHKMEMQ;
149   PetscStackPop;
150   ierr = PetscLogEventEnd(TaoSolver_HessianEval,tao,X,*H,*Hpre); CHKERRQ(ierr);
151   PetscFunctionReturn(0);
152 }
153 
154 #undef __FUNCT__
155 #define __FUNCT__ "TaoComputeJacobian"
156 /*@C
157    TaoComputeJacobian - Computes the Jacobian matrix that has been
158    set with TaoSetJacobianRoutine().
159 
160    Collective on TaoSolver
161 
162    Input Parameters:
163 +  solver - the TaoSolver solver context
164 -  xx - input vector
165 
166    Output Parameters:
167 +  H - Jacobian matrix
168 .  Hpre - Preconditioning matrix
169 -  flag - flag indicating matrix structure (SAME_NONZERO_PATTERN, DIFFERENT_NONZERO_PATTERN, or SAME_PRECONDITIONER)
170 
171    Notes:
172    Most users should not need to explicitly call this routine, as it
173    is used internally within the minimization solvers.
174 
175    TaoComputeJacobian() is typically used within minimization
176    implementations, so most users would not generally call this routine
177    themselves.
178 
179    Level: developer
180 
181 .seealso:  TaoComputeObjective(), TaoComputeObjectiveAndGradient(), TaoSetJacobian()
182 
183 @*/
184 PetscErrorCode TaoComputeJacobian(TaoSolver tao, Vec X, Mat *J, Mat *Jpre, MatStructure *flg)
185 {
186   PetscErrorCode ierr;
187 
188   PetscFunctionBegin;
189   PetscValidHeaderSpecific(tao,TAOSOLVER_CLASSID,1);
190   PetscValidHeaderSpecific(X, VEC_CLASSID,2);
191   PetscValidPointer(flg,5);
192   PetscCheckSameComm(tao,1,X,2);
193 
194   if (!tao->ops->computejacobian) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TaoSetJacobian() first");
195   *flg = DIFFERENT_NONZERO_PATTERN;
196   ++tao->njac;
197   ierr = PetscLogEventBegin(TaoSolver_JacobianEval,tao,X,*J,*Jpre); CHKERRQ(ierr);
198   PetscStackPush("TaoSolver user Jacobian function");
199   CHKMEMQ;
200   ierr = (*tao->ops->computejacobian)(tao,X,J,Jpre,flg,tao->user_jacP); CHKERRQ(ierr);
201   CHKMEMQ;
202   PetscStackPop;
203   ierr = PetscLogEventEnd(TaoSolver_JacobianEval,tao,X,*J,*Jpre); CHKERRQ(ierr);
204   PetscFunctionReturn(0);
205 }
206 
207 #undef __FUNCT__
208 #define __FUNCT__ "TaoComputeJacobianState"
209 /*@C
210    TaoComputeJacobianState - Computes the Jacobian matrix that has been
211    set with TaoSetJacobianStateRoutine().
212 
213    Collective on TaoSolver
214 
215    Input Parameters:
216 +  solver - the TaoSolver solver context
217 -  xx - input vector
218 
219    Output Parameters:
220 +  H - Jacobian matrix
221 .  Hpre - Preconditioning matrix
222 -  flag - flag indicating matrix structure (SAME_NONZERO_PATTERN, DIFFERENT_NONZERO_PATTERN, or SAME_PRECONDITIONER)
223 
224    Notes:
225    Most users should not need to explicitly call this routine, as it
226    is used internally within the minimization solvers.
227 
228    TaoComputeJacobianState() is typically used within minimization
229    implementations, so most users would not generally call this routine
230    themselves.
231 
232    Level: developer
233 
234 .seealso:  TaoComputeObjective(), TaoComputeObjectiveAndGradient(), TaoSetJacobianStateRoutine(), TaoComputeJacobianDesign(), TaoSetStateDesignIS()
235 
236 @*/
237 PetscErrorCode TaoComputeJacobianState(TaoSolver tao, Vec X, Mat *J, Mat *Jpre, Mat *Jinv, MatStructure *flg)
238 {
239   PetscErrorCode ierr;
240   PetscFunctionBegin;
241   PetscValidHeaderSpecific(tao,TAOSOLVER_CLASSID,1);
242   PetscValidHeaderSpecific(X, VEC_CLASSID,2);
243   PetscValidPointer(flg,5);
244   PetscCheckSameComm(tao,1,X,2);
245 
246   if (!tao->ops->computejacobianstate) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TaoSetJacobianState() first");
247   *flg = DIFFERENT_NONZERO_PATTERN;
248   ++tao->njac_state;
249   ierr = PetscLogEventBegin(TaoSolver_JacobianEval,tao,X,*J,*Jpre); CHKERRQ(ierr);
250   PetscStackPush("TaoSolver user Jacobian(state) function");
251   CHKMEMQ;
252   ierr = (*tao->ops->computejacobianstate)(tao,X,J,Jpre,Jinv,flg,tao->user_jac_stateP); CHKERRQ(ierr);
253   CHKMEMQ;
254   PetscStackPop;
255   ierr = PetscLogEventEnd(TaoSolver_JacobianEval,tao,X,*J,*Jpre); CHKERRQ(ierr);
256   PetscFunctionReturn(0);
257 }
258 
259 #undef __FUNCT__
260 #define __FUNCT__ "TaoComputeJacobianDesign"
261 /*@C
262    TaoComputeJacobianDesign - Computes the Jacobian matrix that has been
263    set with TaoSetJacobianDesignRoutine().
264 
265    Collective on TaoSolver
266 
267    Input Parameters:
268 +  solver - the TaoSolver solver context
269 -  xx - input vector
270 
271    Output Parameters:
272 .  H - Jacobian matrix
273 
274    Notes:
275    Most users should not need to explicitly call this routine, as it
276    is used internally within the minimization solvers.
277 
278    TaoComputeJacobianDesign() is typically used within minimization
279    implementations, so most users would not generally call this routine
280    themselves.
281 
282    Level: developer
283 
284 .seealso:  TaoComputeObjective(), TaoComputeObjectiveAndGradient(), TaoSetJacobianDesignRoutine(), TaoComputeJacobianDesign(), TaoSetStateDesignIS()
285 
286 @*/
287 PetscErrorCode TaoComputeJacobianDesign(TaoSolver tao, Vec X, Mat *J)
288 {
289   PetscErrorCode ierr;
290 
291   PetscFunctionBegin;
292   PetscValidHeaderSpecific(tao,TAOSOLVER_CLASSID,1);
293   PetscValidHeaderSpecific(X, VEC_CLASSID,2);
294   PetscCheckSameComm(tao,1,X,2);
295 
296   if (!tao->ops->computejacobiandesign) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TaoSetJacobianDesign() first");
297   ++tao->njac_design;
298   ierr = PetscLogEventBegin(TaoSolver_JacobianEval,tao,X,*J,NULL); CHKERRQ(ierr);
299   PetscStackPush("TaoSolver user Jacobian(design) function");
300   CHKMEMQ;
301   ierr = (*tao->ops->computejacobiandesign)(tao,X,J,tao->user_jac_designP); CHKERRQ(ierr);
302   CHKMEMQ;
303   PetscStackPop;
304   ierr = PetscLogEventEnd(TaoSolver_JacobianEval,tao,X,*J,NULL); CHKERRQ(ierr);
305   PetscFunctionReturn(0);
306 }
307 
308 
309 
310 #undef __FUNCT__
311 #define __FUNCT__ "TaoSetJacobianRoutine"
312 /*@C
313    TaoSetJacobianRoutine - Sets the function to compute the Jacobian as well as the location to store the matrix.
314 
315    Logically collective on TaoSolver
316 
317    Input Parameters:
318 +  tao - the TaoSolver context
319 .  J - Matrix used for the jacobian
320 .  Jpre - Matrix that will be used operated on by preconditioner, can be same as J
321 .  jac - Jacobian evaluation routine
322 -  ctx - [optional] user-defined context for private data for the
323          Jacobian evaluation routine (may be NULL)
324 
325    Calling sequence of jac:
326 $    jac (TaoSolver tao,Vec x,Mat *J,Mat *Jpre,MatStructure *flag,void *ctx);
327 
328 +  tao - the TaoSolver  context
329 .  x - input vector
330 .  J - Jacobian matrix
331 .  Jpre - preconditioner matrix, usually the same as J
332 .  flag - flag indicating information about the preconditioner matrix
333    structure (see below)
334 -  ctx - [optional] user-defined Jacobian context
335 
336 
337    Notes:
338 
339    The function jac() takes Mat * as the matrix arguments rather than Mat.
340    This allows the Jacobian evaluation routine to replace A and/or B with a
341    completely new new matrix structure (not just different matrix elements)
342    when appropriate, for instance, if the nonzero structure is changing
343    throughout the global iterations.
344 
345    The flag can be used to eliminate unnecessary work in the preconditioner
346    during the repeated solution of linear systems of the same size.  The
347    available options are
348 $    SAME_PRECONDITIONER -
349 $      Jpre is identical during successive linear solves.
350 $      This option is intended for folks who are using
351 $      different Amat and Pmat matrices and want to reuse the
352 $      same preconditioner matrix.  For example, this option
353 $      saves work by not recomputing incomplete factorization
354 $      for ILU/ICC preconditioners.
355 $    SAME_NONZERO_PATTERN -
356 $      Jpre has the same nonzero structure during
357 $      successive linear solves.
358 $    DIFFERENT_NONZERO_PATTERN -
359 $      Jpre does not have the same nonzero structure.
360 
361    Caution:
362    If you specify SAME_NONZERO_PATTERN, the software believes your assertion
363    and does not check the structure of the matrix.  If you erroneously
364    claim that the structure is the same when it actually is not, the new
365    preconditioner will not function correctly.  Thus, use this optimization
366    feature carefully!
367 
368    If in doubt about whether your preconditioner matrix has changed
369    structure or not, use the flag DIFFERENT_NONZERO_PATTERN.
370 
371    Level: intermediate
372 
373 @*/
374 PetscErrorCode TaoSetJacobianRoutine(TaoSolver tao, Mat J, Mat Jpre, PetscErrorCode (*func)(TaoSolver, Vec, Mat*, Mat *, MatStructure *, void*), void *ctx)
375 {
376     PetscErrorCode ierr;
377     PetscFunctionBegin;
378     PetscValidHeaderSpecific(tao,TAOSOLVER_CLASSID,1);
379     if (J) {
380 	PetscValidHeaderSpecific(J,MAT_CLASSID,2);
381 	PetscCheckSameComm(tao,1,J,2);
382     }
383     if (Jpre) {
384 	PetscValidHeaderSpecific(Jpre,MAT_CLASSID,3);
385 	PetscCheckSameComm(tao,1,Jpre,3);
386     }
387     if (ctx) {
388 	tao->user_jacP = ctx;
389     }
390     if (func) {
391 	tao->ops->computejacobian = func;
392     }
393 
394 
395     if (J) {
396 	ierr = PetscObjectReference((PetscObject)J); CHKERRQ(ierr);
397 	if (tao->jacobian) {   ierr = MatDestroy(&tao->jacobian); CHKERRQ(ierr);}
398 	tao->jacobian = J;
399     }
400     if (Jpre) {
401 	ierr = PetscObjectReference((PetscObject)Jpre); CHKERRQ(ierr);
402 	if (tao->jacobian_pre) { ierr = MatDestroy(&tao->jacobian_pre); CHKERRQ(ierr);}
403 	tao->jacobian_pre=Jpre;
404     }
405     PetscFunctionReturn(0);
406 }
407 
408 
409 #undef __FUNCT__
410 #define __FUNCT__ "TaoSetJacobianStateRoutine"
411 /*@C
412    TaoSetJacobianStateRoutine - Sets the function to compute the Jacobian
413    (and its inverse) of the constraint function with respect to the state variables.
414    Used only for pde-constrained optimization.
415 
416    Logically collective on TaoSolver
417 
418    Input Parameters:
419 +  tao - the TaoSolver context
420 .  J - Matrix used for the jacobian
421 .  Jpre - Matrix that will be used operated on by PETSc preconditioner, can be same as J.  Only used if Jinv is NULL
422 .  Jinv - [optional] Matrix used to apply the inverse of the state jacobian. Use NULL to default to PETSc KSP solvers to apply the inverse.
423 .  jac - Jacobian evaluation routine
424 -  ctx - [optional] user-defined context for private data for the
425          Jacobian evaluation routine (may be NULL)
426 
427    Calling sequence of jac:
428 $    jac (TaoSolver tao,Vec x,Mat *J,Mat *Jpre,MatStructure *flag,void *ctx);
429 
430 +  tao - the TaoSolver  context
431 .  x - input vector
432 .  J - Jacobian matrix
433 .  Jpre - preconditioner matrix, usually the same as J
434 .  Jinv - inverse of J
435 .  flag - flag indicating information about the preconditioner matrix
436    structure (see below)
437 -  ctx - [optional] user-defined Jacobian context
438 
439 
440    Notes:
441    Because of the structure of the jacobian matrix,
442    It may be more efficient for a pde-constrained application to provide
443    its own Jinv matrix.
444 
445    The function jac() takes Mat * as the matrix arguments rather than Mat.
446    This allows the Jacobian evaluation routine to replace A and/or B with a
447    completely new new maitrix structure (not just different matrix elements)
448    when appropriate, for instance, if the nonzero structure is changing
449    throughout the global iterations.
450 
451    The flag can be used to eliminate unnecessary work in the preconditioner
452    during the repeated solution of linear systems of the same size.  The
453    available options are
454 $    SAME_PRECONDITIONER -
455 $      Jpre is identical during successive linear solves.
456 $      This option is intended for folks who are using
457 $      different Amat and Pmat matrices and want to reuse the
458 $      same preconditioner matrix.  For example, this option
459 $      saves work by not recomputing incomplete factorization
460 $      for ILU/ICC preconditioners.
461 $    SAME_NONZERO_PATTERN -
462 $      Jpre has the same nonzero structure during
463 $      successive linear solves.
464 $    DIFFERENT_NONZERO_PATTERN -
465 $      Jpre does not have the same nonzero structure.
466 
467    Caution:
468    If you specify SAME_NONZERO_PATTERN, the software believes your assertion
469    and does not check the structure of the matrix.  If you erroneously
470    claim that the structure is the same when it actually is not, the new
471    preconditioner will not function correctly.  Thus, use this optimization
472    feature carefully!
473 
474    If in doubt about whether your preconditioner matrix has changed
475    structure or not, use the flag DIFFERENT_NONZERO_PATTERN.
476 
477    Level: intermediate
478 .seealse: TaoComputeJacobianState(), TaoSetJacobianDesignRoutine(), TaoSetStateDesignIS()
479 @*/
480 PetscErrorCode TaoSetJacobianStateRoutine(TaoSolver tao, Mat J, Mat Jpre, Mat Jinv, PetscErrorCode (*func)(TaoSolver, Vec, Mat*, Mat *, Mat *, MatStructure *, void*), void *ctx)
481 {
482     PetscErrorCode ierr;
483     PetscFunctionBegin;
484     PetscValidHeaderSpecific(tao,TAOSOLVER_CLASSID,1);
485     if (J) {
486 	PetscValidHeaderSpecific(J,MAT_CLASSID,2);
487 	PetscCheckSameComm(tao,1,J,2);
488     }
489     if (Jpre) {
490 	PetscValidHeaderSpecific(Jpre,MAT_CLASSID,3);
491 	PetscCheckSameComm(tao,1,Jpre,3);
492     }
493     if (Jinv) {
494       PetscValidHeaderSpecific(Jinv,MAT_CLASSID,4);
495       PetscCheckSameComm(tao,1,Jinv,4);
496     }
497     if (ctx) {
498 	tao->user_jac_stateP = ctx;
499     }
500     if (func) {
501 	tao->ops->computejacobianstate = func;
502     }
503 
504 
505     if (J) {
506 	ierr = PetscObjectReference((PetscObject)J); CHKERRQ(ierr);
507 	if (tao->jacobian_state) {   ierr = MatDestroy(&tao->jacobian_state); CHKERRQ(ierr);}
508 	tao->jacobian_state = J;
509     }
510     if (Jpre) {
511 	ierr = PetscObjectReference((PetscObject)Jpre); CHKERRQ(ierr);
512 	if (tao->jacobian_state_pre) { ierr = MatDestroy(&tao->jacobian_state_pre); CHKERRQ(ierr);}
513 	tao->jacobian_state_pre=Jpre;
514     }
515     if (Jinv) {
516       ierr = PetscObjectReference((PetscObject)Jinv); CHKERRQ(ierr);
517       if (tao->jacobian_state_inv) {ierr = MatDestroy(&tao->jacobian_state_inv); CHKERRQ(ierr);}
518       tao->jacobian_state_inv=Jinv;
519     }
520     PetscFunctionReturn(0);
521 }
522 
523 #undef __FUNCT__
524 #define __FUNCT__ "TaoSetJacobianDesignRoutine"
525 /*@C
526    TaoSetJacobianDesignRoutine - Sets the function to compute the Jacobian of
527    the constraint function with respect to the design variables.  Used only for
528    pde-constrained optimization.
529 
530    Logically collective on TaoSolver
531 
532    Input Parameters:
533 +  tao - the TaoSolver context
534 .  J - Matrix used for the jacobian
535 .  jac - Jacobian evaluation routine
536 -  ctx - [optional] user-defined context for private data for the
537          Jacobian evaluation routine (may be NULL)
538 
539    Calling sequence of jac:
540 $    jac (TaoSolver tao,Vec x,Mat *J,void *ctx);
541 
542 +  tao - the TaoSolver  context
543 .  x - input vector
544 .  J - Jacobian matrix
545 -  ctx - [optional] user-defined Jacobian context
546 
547 
548    Notes:
549 
550    The function jac() takes Mat * as the matrix arguments rather than Mat.
551    This allows the Jacobian evaluation routine to replace A and/or B with a
552    completely new new matrix structure (not just different matrix elements)
553    when appropriate, for instance, if the nonzero structure is changing
554    throughout the global iterations.
555 
556    Level: intermediate
557 .seealso: TaoComputeJacobianDesign(), TaoSetJacobianStateRoutine(), TaoSetStateDesignIS()
558 @*/
559 PetscErrorCode TaoSetJacobianDesignRoutine(TaoSolver tao, Mat J, PetscErrorCode (*func)(TaoSolver, Vec, Mat*, void*), void *ctx)
560 {
561     PetscErrorCode ierr;
562     PetscFunctionBegin;
563     PetscValidHeaderSpecific(tao,TAOSOLVER_CLASSID,1);
564     if (J) {
565 	PetscValidHeaderSpecific(J,MAT_CLASSID,2);
566 	PetscCheckSameComm(tao,1,J,2);
567     }
568     if (ctx) {
569 	tao->user_jac_designP = ctx;
570     }
571     if (func) {
572 	tao->ops->computejacobiandesign = func;
573     }
574 
575 
576     if (J) {
577 	ierr = PetscObjectReference((PetscObject)J); CHKERRQ(ierr);
578 	if (tao->jacobian_design) {   ierr = MatDestroy(&tao->jacobian_design); CHKERRQ(ierr);}
579 	tao->jacobian_design = J;
580     }
581     PetscFunctionReturn(0);
582 }
583 
584 #undef __FUNCT__
585 #define __FUNCT__ "TaoSetStateDesignIS"
586 /*@
587    TaoSetStateDesignIS - Indicate to the TaoSolver which variables in the
588    solution vector are state variables and which are design.  Only applies to
589    pde-constrained optimization.
590 
591    Logically Collective on TaoSolver
592 
593    Input Parameters:
594 +  tao - The TaoSolver context
595 .  s_is - the index set corresponding to the state variables
596 -  d_is - the index set corresponding to the design variables
597 
598    Level: intermediate
599 
600 .seealso: TaoSetJacobianStateRoutine(), TaoSetJacobianDesignRoutine()
601 @*/
602 PetscErrorCode TaoSetStateDesignIS(TaoSolver tao, IS s_is, IS d_is)
603 {
604   PetscErrorCode ierr;
605   if (tao->state_is) {
606     ierr = PetscObjectDereference((PetscObject)(tao->state_is)); CHKERRQ(ierr);
607   }
608   if (tao->design_is) {
609     ierr = PetscObjectDereference((PetscObject)(tao->design_is)); CHKERRQ(ierr);
610   }
611   tao->state_is = s_is;
612   tao->design_is = d_is;
613   if (s_is) {
614     ierr = PetscObjectReference((PetscObject)(tao->state_is)); CHKERRQ(ierr);
615   }
616   if (d_is) {
617     ierr = PetscObjectReference((PetscObject)(tao->design_is)); CHKERRQ(ierr);
618   }
619   PetscFunctionReturn(0);
620 }
621 
622 
623 #undef __FUNCT__
624 #define __FUNCT__ "TaoComputeJacobianEquality"
625 /*@C
626    TaoComputeJacobianEquality - Computes the Jacobian matrix that has been
627    set with TaoSetJacobianEqualityRoutine().
628 
629    Collective on TaoSolver
630 
631    Input Parameters:
632 +  solver - the TaoSolver solver context
633 -  xx - input vector
634 
635    Output Parameters:
636 +  H - Jacobian matrix
637 .  Hpre - Preconditioning matrix
638 -  flag - flag indicating matrix structure (SAME_NONZERO_PATTERN, DIFFERENT_NONZERO_PATTERN, or SAME_PRECONDITIONER)
639 
640    Notes:
641    Most users should not need to explicitly call this routine, as it
642    is used internally within the minimization solvers.
643 
644    Level: developer
645 
646 .seealso:  TaoComputeObjective(), TaoComputeObjectiveAndGradient(), TaoSetJacobianStateRoutine(), TaoComputeJacobianDesign(), TaoSetStateDesignIS()
647 
648 @*/
649 PetscErrorCode TaoComputeJacobianEquality(TaoSolver tao, Vec X, Mat *J, Mat *Jpre, MatStructure *flg)
650 {
651   PetscErrorCode ierr;
652   PetscFunctionBegin;
653   PetscValidHeaderSpecific(tao,TAOSOLVER_CLASSID,1);
654   PetscValidHeaderSpecific(X, VEC_CLASSID,2);
655   PetscValidPointer(flg,5);
656   PetscCheckSameComm(tao,1,X,2);
657 
658   if (!tao->ops->computejacobianequality) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TaoSetJacobianEquality() first");
659   *flg = DIFFERENT_NONZERO_PATTERN;
660   ++tao->njac_equality;
661   ierr = PetscLogEventBegin(TaoSolver_JacobianEval,tao,X,*J,*Jpre); CHKERRQ(ierr);
662   PetscStackPush("TaoSolver user Jacobian(equality) function");
663   CHKMEMQ;
664   ierr = (*tao->ops->computejacobianequality)(tao,X,J,Jpre,flg,tao->user_jac_equalityP); CHKERRQ(ierr);
665   CHKMEMQ;
666   PetscStackPop;
667   ierr = PetscLogEventEnd(TaoSolver_JacobianEval,tao,X,*J,*Jpre); CHKERRQ(ierr);
668   PetscFunctionReturn(0);
669 }
670 
671 #undef __FUNCT__
672 #define __FUNCT__ "TaoComputeJacobianInequality"
673 /*@C
674    TaoComputeJacobianInequality - Computes the Jacobian matrix that has been
675    set with TaoSetJacobianInequalityRoutine().
676 
677    Collective on TaoSolver
678 
679    Input Parameters:
680 +  solver - the TaoSolver solver context
681 -  xx - input vector
682 
683    Output Parameters:
684 +  H - Jacobian matrix
685 .  Hpre - Preconditioning matrix
686 -  flag - flag indicating matrix structure (SAME_NONZERO_PATTERN, DIFFERENT_NONZERO_PATTERN, or SAME_PRECONDITIONER)
687 
688    Notes:
689    Most users should not need to explicitly call this routine, as it
690    is used internally within the minimization solvers.
691 
692    Level: developer
693 
694 .seealso:  TaoComputeObjective(), TaoComputeObjectiveAndGradient(), TaoSetJacobianStateRoutine(), TaoComputeJacobianDesign(), TaoSetStateDesignIS()
695 
696 @*/
697 PetscErrorCode TaoComputeJacobianInequality(TaoSolver tao, Vec X, Mat *J, Mat *Jpre, MatStructure *flg)
698 {
699   PetscErrorCode ierr;
700 
701   PetscFunctionBegin;
702   PetscValidHeaderSpecific(tao,TAOSOLVER_CLASSID,1);
703   PetscValidHeaderSpecific(X, VEC_CLASSID,2);
704   PetscValidPointer(flg,5);
705   PetscCheckSameComm(tao,1,X,2);
706 
707   if (!tao->ops->computejacobianinequality) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TaoSetJacobianInequality() first");
708   *flg = DIFFERENT_NONZERO_PATTERN;
709   ++tao->njac_inequality;
710   ierr = PetscLogEventBegin(TaoSolver_JacobianEval,tao,X,*J,*Jpre); CHKERRQ(ierr);
711   PetscStackPush("TaoSolver user Jacobian(inequality) function");
712   CHKMEMQ;
713   ierr = (*tao->ops->computejacobianinequality)(tao,X,J,Jpre,flg,tao->user_jac_inequalityP); CHKERRQ(ierr);
714   CHKMEMQ;
715   PetscStackPop;
716   ierr = PetscLogEventEnd(TaoSolver_JacobianEval,tao,X,*J,*Jpre); CHKERRQ(ierr);
717   PetscFunctionReturn(0);
718 }
719 
720 #undef __FUNCT__
721 #define __FUNCT__ "TaoSetJacobianEqualityRoutine"
722 /*@C
723    TaoSetJacobianEqualityRoutine - Sets the function to compute the Jacobian
724    (and its inverse) of the constraint function with respect to the equality variables.
725    Used only for pde-constrained optimization.
726 
727    Logically collective on TaoSolver
728 
729    Input Parameters:
730 +  tao - the TaoSolver context
731 .  J - Matrix used for the jacobian
732 .  Jpre - Matrix that will be used operated on by PETSc preconditioner, can be same as J.
733 .  jac - Jacobian evaluation routine
734 -  ctx - [optional] user-defined context for private data for the
735          Jacobian evaluation routine (may be NULL)
736 
737    Calling sequence of jac:
738 $    jac (TaoSolver tao,Vec x,Mat *J,Mat *Jpre,MatStructure *flag,void *ctx);
739 
740 +  tao - the TaoSolver  context
741 .  x - input vector
742 .  J - Jacobian matrix
743 .  Jpre - preconditioner matrix, usually the same as J
744 .  flag - flag indicating information about the preconditioner matrix
745    structure (see below)
746 -  ctx - [optional] user-defined Jacobian context
747 
748 
749    Notes:
750    Because of the structure of the jacobian matrix,
751    It may be more efficient for a pde-constrained application to provide
752    its own Jinv matrix.
753 
754    The function jac() takes Mat * as the matrix arguments rather than Mat.
755    This allows the Jacobian evaluation routine to replace A and/or B with a
756    completely new new maitrix structure (not just different matrix elements)
757    when appropriate, for instance, if the nonzero structure is changing
758    throughout the global iterations.
759 
760    The flag can be used to eliminate unnecessary work in the preconditioner
761    during the repeated solution of linear systems of the same size.  The
762    available options are
763 $    SAME_PRECONDITIONER -
764 $      Jpre is identical during successive linear solves.
765 $      This option is intended for folks who are using
766 $      different Amat and Pmat matrices and want to reuse the
767 $      same preconditioner matrix.  For example, this option
768 $      saves work by not recomputing incomplete factorization
769 $      for ILU/ICC preconditioners.
770 $    SAME_NONZERO_PATTERN -
771 $      Jpre has the same nonzero structure during
772 $      successive linear solves.
773 $    DIFFERENT_NONZERO_PATTERN -
774 $      Jpre does not have the same nonzero structure.
775 
776    Caution:
777    If you specify SAME_NONZERO_PATTERN, the software believes your assertion
778    and does not check the structure of the matrix.  If you erroneously
779    claim that the structure is the same when it actually is not, the new
780    preconditioner will not function correctly.  Thus, use this optimization
781    feature carefully!
782 
783    If in doubt about whether your preconditioner matrix has changed
784    structure or not, use the flag DIFFERENT_NONZERO_PATTERN.
785 
786    Level: intermediate
787 .seealse: TaoComputeJacobianEquality(), TaoSetJacobianDesignRoutine(), TaoSetEqualityDesignIS()
788 @*/
789 PetscErrorCode TaoSetJacobianEqualityRoutine(TaoSolver tao, Mat J, Mat Jpre, PetscErrorCode (*func)(TaoSolver, Vec, Mat*, Mat *, MatStructure *, void*), void *ctx)
790 {
791     PetscErrorCode ierr;
792     PetscFunctionBegin;
793     PetscValidHeaderSpecific(tao,TAOSOLVER_CLASSID,1);
794     if (J) {
795 	PetscValidHeaderSpecific(J,MAT_CLASSID,2);
796 	PetscCheckSameComm(tao,1,J,2);
797     }
798     if (Jpre) {
799 	PetscValidHeaderSpecific(Jpre,MAT_CLASSID,3);
800 	PetscCheckSameComm(tao,1,Jpre,3);
801     }
802     if (ctx) {
803 	tao->user_jac_equalityP = ctx;
804     }
805     if (func) {
806 	tao->ops->computejacobianequality = func;
807     }
808 
809 
810     if (J) {
811 	ierr = PetscObjectReference((PetscObject)J); CHKERRQ(ierr);
812 	if (tao->jacobian_equality) {   ierr = MatDestroy(&tao->jacobian_equality); CHKERRQ(ierr);}
813 	tao->jacobian_equality = J;
814     }
815     if (Jpre) {
816 	ierr = PetscObjectReference((PetscObject)Jpre); CHKERRQ(ierr);
817 	if (tao->jacobian_equality_pre) { ierr = MatDestroy(&tao->jacobian_equality_pre); CHKERRQ(ierr);}
818 	tao->jacobian_equality_pre=Jpre;
819     }
820     PetscFunctionReturn(0);
821 }
822 
823 #undef __FUNCT__
824 #define __FUNCT__ "TaoSetJacobianInequalityRoutine"
825 /*@C
826    TaoSetJacobianInequalityRoutine - Sets the function to compute the Jacobian
827    (and its inverse) of the constraint function with respect to the inequality variables.
828    Used only for pde-constrained optimization.
829 
830    Logically collective on TaoSolver
831 
832    Input Parameters:
833 +  tao - the TaoSolver context
834 .  J - Matrix used for the jacobian
835 .  Jpre - Matrix that will be used operated on by PETSc preconditioner, can be same as J.
836 .  jac - Jacobian evaluation routine
837 -  ctx - [optional] user-defined context for private data for the
838          Jacobian evaluation routine (may be NULL)
839 
840    Calling sequence of jac:
841 $    jac (TaoSolver tao,Vec x,Mat *J,Mat *Jpre,MatStructure *flag,void *ctx);
842 
843 +  tao - the TaoSolver  context
844 .  x - input vector
845 .  J - Jacobian matrix
846 .  Jpre - preconditioner matrix, usually the same as J
847 .  flag - flag indicating information about the preconditioner matrix
848    structure (see below)
849 -  ctx - [optional] user-defined Jacobian context
850 
851 
852    Notes:
853    Because of the structure of the jacobian matrix,
854    It may be more efficient for a pde-constrained application to provide
855    its own Jinv matrix.
856 
857    The function jac() takes Mat * as the matrix arguments rather than Mat.
858    This allows the Jacobian evaluation routine to replace A and/or B with a
859    completely new new maitrix structure (not just different matrix elements)
860    when appropriate, for instance, if the nonzero structure is changing
861    throughout the global iterations.
862 
863    The flag can be used to eliminate unnecessary work in the preconditioner
864    during the repeated solution of linear systems of the same size.  The
865    available options are
866 $    SAME_PRECONDITIONER -
867 $      Jpre is identical during successive linear solves.
868 $      This option is intended for folks who are using
869 $      different Amat and Pmat matrices and want to reuse the
870 $      same preconditioner matrix.  For example, this option
871 $      saves work by not recomputing incomplete factorization
872 $      for ILU/ICC preconditioners.
873 $    SAME_NONZERO_PATTERN -
874 $      Jpre has the same nonzero structure during
875 $      successive linear solves.
876 $    DIFFERENT_NONZERO_PATTERN -
877 $      Jpre does not have the same nonzero structure.
878 
879    Caution:
880    If you specify SAME_NONZERO_PATTERN, the software believes your assertion
881    and does not check the structure of the matrix.  If you erroneously
882    claim that the structure is the same when it actually is not, the new
883    preconditioner will not function correctly.  Thus, use this optimization
884    feature carefully!
885 
886    If in doubt about whether your preconditioner matrix has changed
887    structure or not, use the flag DIFFERENT_NONZERO_PATTERN.
888 
889    Level: intermediate
890 .seealse: TaoComputeJacobianInequality(), TaoSetJacobianDesignRoutine(), TaoSetInequalityDesignIS()
891 @*/
892 PetscErrorCode TaoSetJacobianInequalityRoutine(TaoSolver tao, Mat J, Mat Jpre, PetscErrorCode (*func)(TaoSolver, Vec, Mat*, Mat *, MatStructure *, void*), void *ctx)
893 {
894     PetscErrorCode ierr;
895     PetscFunctionBegin;
896     PetscValidHeaderSpecific(tao,TAOSOLVER_CLASSID,1);
897     if (J) {
898 	PetscValidHeaderSpecific(J,MAT_CLASSID,2);
899 	PetscCheckSameComm(tao,1,J,2);
900     }
901     if (Jpre) {
902 	PetscValidHeaderSpecific(Jpre,MAT_CLASSID,3);
903 	PetscCheckSameComm(tao,1,Jpre,3);
904     }
905     if (ctx) {
906 	tao->user_jac_inequalityP = ctx;
907     }
908     if (func) {
909 	tao->ops->computejacobianinequality = func;
910     }
911 
912 
913     if (J) {
914 	ierr = PetscObjectReference((PetscObject)J); CHKERRQ(ierr);
915 	if (tao->jacobian_inequality) {   ierr = MatDestroy(&tao->jacobian_inequality); CHKERRQ(ierr);}
916 	tao->jacobian_inequality = J;
917     }
918     if (Jpre) {
919 	ierr = PetscObjectReference((PetscObject)Jpre); CHKERRQ(ierr);
920 	if (tao->jacobian_inequality_pre) { ierr = MatDestroy(&tao->jacobian_inequality_pre); CHKERRQ(ierr);}
921 	tao->jacobian_inequality_pre=Jpre;
922     }
923     PetscFunctionReturn(0);
924 }
925