Actual source code: newsolver.c

  1: #include "src/tao_impl.h"      /*I "tao_solver.h"  I*/

  5: /*@
  6:   TaoSetMethodFromOptions - Sets the TAO_SOLVER solver type from the options database, 
  7:   or sets a default if no method has been specified.

  9:    Collective on TAO_SOLVER

 11:    Input Parameter:
 12: .  solver - the TAO_SOLVER solver context

 14:    Options Database Keys:
 15: .  -tao_method <type> - tao_nls, tao_ntr, tao_lmvm, tao_cg, tao_tron, etc.

 17:    Level: intermediate

 19: .keywords: method, options, database

 21: .seealso: TaoSetOptions()
 22: @*/
 23: int TaoSetMethodFromOptions(TAO_SOLVER solver)
 24: {
 25:   char       type[256];
 26:   int        info;
 27:   TaoTruth flg;

 29:   TaoFunctionBegin;
 30:   TaoValidHeaderSpecific(solver,TAO_COOKIE,1);
 31:   if (solver->setupcalled) SETERRQ(1,"Must call prior to TaoSetUp()");

 33:   info = TaoOptionString("-tao_method","TaoMethod","TaoSetMethod","",type,256, &flg);CHKERRQ(info);
 34:   if (flg) {
 35:     info = TaoSetMethod(solver,(TaoMethod) type);CHKERRQ(info);
 36:   }
 37:   if (!solver->type_name) {
 38:     info = TaoSetMethod(solver,"tao_lmvm");CHKERRQ(info);
 39:   }
 40:   TaoFunctionReturn(0);
 41: }


 46: /*@
 47:    TaoSetFromOptions - Sets many TAO_SOLVER parameters from the command line arguments.  
 48:    This command does not set the solver type.

 50:    Collective on TAO_SOLVER

 52:    Input Parameter:
 53: .  solver - the TAO_SOLVER solver context

 55:    Options Database Keys:
 56: +  -tao_stol - convergence tolerance in terms of the norm
 57:                 of the change in the solution between steps
 58: .  -tao_fatol <fatol> - absolute tolerance of residual norm
 59: .  -tao_frtol <frtol> - relative decrease in tolerance norm from initial
 60: .  -tao_max_its <max_its> - maximum number of iterations
 61: .  -tao_max_funcs <max_funcs> - maximum number of function evaluations
 62: .  -tao_trtol <trtol> - trust region tolerance
 63: .  -tao_trust0 <radius> - initial trust region radius
 64: .  -tao_no_convergence_test - skip convergence test in minimization solver;
 65:                                hence iterations will continue until max_it
 66:                                or some other criterion is reached. Saves expense
 67:                                of convergence test
 68: .  -tao_monitor - prints residual norm at each iteration 
 69: .  -tao_vecmonitor - plots solution at each iteration
 70: .  -tao_vecmonitor_update - plots update to solution at each iteration 
 71: .  -tao_xmonitor - plots residual norm at each iteration 
 72: .  -tao_fd - use finite differences to compute Hessian; very slow, only for testing
 73: -  -tao_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration

 75:    Notes:
 76:    To see all options, run your program with the -help option or consult
 77:    the users manual.

 79:    Level: developer

 81: .keywords: options, converence, monitor, view, database

 83: .seealso: TaoSetMethodFromOptions()
 84: @*/
 85: int TaoSetFromOptions(TAO_SOLVER solver)
 86: {
 87:   TaoTruth flg;
 88:   int      info;
 89:   char      type[256];

 91:   TaoFunctionBegin;
 92:   TaoValidHeaderSpecific(solver,TAO_COOKIE,1);

 94:   info = TaoMethodsList("-tao_method","Select TAO method","TaoSetMethod",0,type,256,0);CHKERRQ(info);

 96:   if (solver->setfromoptions) {
 97:     info = (*solver->setfromoptions)(solver,solver->data); CHKERRQ(info);
 98:   }


101:   info = TaoOptionName("-tao_view","view TAO_SOLVER info after each minimization has completed","TaoView",&flg);CHKERRQ(info);
102:   if (flg) solver->viewtao = TAO_TRUE;
103:   info = TaoOptionName("-tao_kspview","view the Linear Solver used by the solver after minimization has completed","TaoViewLinearSolver",&flg);CHKERRQ(info);
104:   if (flg) solver->viewksptao = TAO_TRUE;
105:   
106:   info = TaoOptionDouble("-tao_fatol","Stop if solution within","TaoSetTolerances",solver->fatol,&solver->fatol,&flg);CHKERRQ(info);
107:   info = TaoOptionDouble("-tao_frtol","Stop if relative solution within","TaoSetTolerances",solver->frtol,&solver->frtol,&flg);CHKERRQ(info);
108:   info = TaoOptionDouble("-tao_catol","Stop if constraints violations within","TaoSetTolerances",solver->catol,&solver->catol,&flg);CHKERRQ(info);
109:   info = TaoOptionDouble("-tao_crtol","Stop if relative contraint violations within","TaoSetTolerances",solver->crtol,&solver->crtol,&flg);CHKERRQ(info);
110:   info = TaoOptionDouble("-tao_gatol","Stop if norm of gradient less than","TaoSetGradientTolerances",solver->gatol,&solver->gatol,&flg);CHKERRQ(info);
111:   info = TaoOptionDouble("-tao_grtol","Stop if norm of gradient divided by the function value is less than","TaoSetGradientTolerances",solver->grtol,&solver->grtol,&flg);CHKERRQ(info); 
112:   info = TaoOptionDouble("-tao_gttol","Stop if the norm of the gradient is less than the norm of the initial gradient times","TaoSetGradientTolerances",solver->gttol,&solver->gttol,&flg);CHKERRQ(info); 
113:   info = TaoOptionInt("-tao_max_its","Stop if iteration number exceeds",
114:                        "TaoSetMaximumIterates",solver->max_its,&solver->max_its,
115:                        &flg);CHKERRQ(info);
116:   info = TaoOptionInt("-tao_max_funcs","Stop if number of function evaluations exceeds","TaoSetMaximumFunctionEvaluations",solver->max_funcs,&solver->max_funcs,&flg);
117:   info = TaoOptionDouble("-tao_fmin","Stop if function less than","TaoSetFunctionLowerBound",solver->fmin,&solver->fmin,&flg);
118:   info = TaoOptionDouble("-tao_steptol","Stop if step size or trust region radius less than","TaoSetTrustRegionRadius",solver->trtol,&solver->trtol,&flg);CHKERRQ(info);
119:   info = TaoOptionDouble("-tao_trust0","Initial trust region radius","TaoSetTrustRegionRadius",solver->trust0,&solver->trust0,&flg);CHKERRQ(info);

121:   /*
122:   info = (*PetscHelpPrintf)(solver->comm," TAO_SOLVER Monitoring Options: Choose any of the following\n");CHKERRQ(info);
123:   */

125:   info = TaoOptionName("-tao_unitstep","Always use unit step length","TaoCreateUnitLineSearch",&flg);
126:   if (flg){info=TaoCreateUnitLineSearch(solver);CHKERRQ(info);}
127:   info = TaoOptionName("-tao_lmvmh","User supplies approximate hessian for LMVM solvers","TaoLMVMSetH0",&flg);
128:   if (flg){info=TaoBLMVMSetH0(solver,TAO_TRUE);CHKERRQ(info);info=TaoLMVMSetH0(solver,TAO_TRUE);CHKERRQ(info);}
129:   
130:   info = TaoOptionName("-tao_view_hessian","view Hessian after each evaluation","None",&flg);CHKERRQ(info);
131:   if (flg) solver->viewhessian = TAO_TRUE;
132:   info = TaoOptionName("-tao_view_gradient","view gradient after each evaluation","None",&flg);CHKERRQ(info);
133:   if (flg) solver->viewgradient = TAO_TRUE;
134:   info = TaoOptionName("-tao_view_jacobian","view jacobian after each evaluation","None",&flg);CHKERRQ(info);  
135:   if (flg) solver->viewjacobian = TAO_TRUE;
136:   info = TaoOptionName("-tao_view_constraints","view constraint function after each evaluation","None",&flg);CHKERRQ(info);  
137:   if (flg) solver->viewvfunc = TAO_TRUE;

139:   info = TaoOptionName("-tao_cancelmonitors","cancel all monitors hardwired in code","TaoClearMonitor",&flg);CHKERRQ(info);
140:   if (flg) {info = TaoClearMonitor(solver);CHKERRQ(info);}
141:   info = TaoOptionName("-tao_monitor","Use the default convergence monitor","TaoSetMonitor",&flg);CHKERRQ(info);
142:   if (flg && solver->defaultmonitor) {
143:     info = TaoSetMonitor(solver,solver->defaultmonitor,TAO_NULL);CHKERRQ(info);
144:   }
145:   info = TaoOptionName("-tao_smonitor","Use short monitor","None",&flg);CHKERRQ(info);
146:   if (flg) {info = TaoSetMonitor(solver,TaoDefaultSMonitor,TAO_NULL);CHKERRQ(info);}
147:   info = TaoOptionName("-tao_vecmonitor","Plot solution vector at each iteration","TaoVecViewMonitor",&flg);CHKERRQ(info);
148:   if (flg) {info = TaoSetMonitor(solver,TaoVecViewMonitor,TAO_NULL);CHKERRQ(info);}
149:   info = TaoOptionName("-tao_vecmonitor_update","plots step direction at each iteration","TaoVecViewMonitorUpdate",&flg);CHKERRQ(info);
150:   if (flg) {info = TaoSetMonitor(solver,TaoVecViewMonitorUpdate,TAO_NULL);CHKERRQ(info);}


153:   //  info = PetscOptionsEnd();CHKERRQ(info);

155:   //  info = TaoSetLinearSolverOptions(solver);CHKERRQ(info);

157:   TaoFunctionReturn(0); 
158: }


161: /* -----------------------------------------------------------*/
164: /*
165:    TaoCreateFull - Creates a TAO_SOLVER context.

167:    Collective on MPI_Comm

169:    Input Parameters:
170: +  method - A TAO method.  
171: .  prefix - a prefix to prepend to all option names (usually TAO_NULL)
172: -  comm - MPI communicator

174:    Output Parameter:
175: .  newsolver - the new TAO_SOLVER context

177:    Options Database Keys:
178: .   -tao_method - select which method TAO should use

180:    Level: developer

182: .keywords: Create, solver, method, context

184: .seealso: TaoCreate(), TaoSolve(), TaoSetMethod(), TaoDestroy()
185: */
186: int TaoCreateFull(TaoMethod method, const char* prefix, MPI_Comm comm, TAO_SOLVER *newsolver)
187: {
188:   TAO_SOLVER solver;
189:   int info;

191:   TaoFunctionBegin;

193:   *newsolver = 0;

195:   info = TaoInitialize(0,0,0,0);CHKERRQ(info);CHKERRQ(info);

197:   info = TaoObjectCreate(newsolver,comm);CHKERRQ(info);
198:   solver=*newsolver;

200:   //  info = TaoSetOptionsPrefix(solver,prefix);CHKERRQ(info);

202:   solver->vec_sol=0;
203:   solver->hessian=0;
204:   solver->vfunc=0;
205:   solver->jacobian=0;
206:   solver->RXL=0;
207:   solver->RXU=0;
208:   solver->CA=0;

210:   info = TaoResetSolver(solver); CHKERRQ(info);  /* Set some pointers to NULL */
211:   info = TaoSetDefaultParameters(solver);CHKERRQ(info);
212:   info = TaoSetDefaultStatistics(solver);CHKERRQ(info);
213:   info = TaoSetDefaultMonitors(solver);CHKERRQ(info);

215:   *newsolver = solver;
216:   if (method) {
217:     info=TaoSetMethod(solver,method);CHKERRQ(info);
218:   }

220:   TaoFunctionReturn(0);
221: }


224: /* -----------------------------------------------------------*/
227: /*@C
228:    TaoCreate - Creates a TAO_SOLVER context.

230:    Collective on MPI_Comm

232:    Input Parameters:
233: +  comm - MPI communicator
234: -  method - A TAO method.  

236:    Output Parameter:
237: .  newsolver - the new TAO_SOLVER context

239:    Options Database Keys:
240: .   -tao_method - select which method TAO should use

242:    Available methods include:
243: +    tao_nls - Newton's method with line search for unconstrained minimization
244: .    tao_ntr - Newton's method with trust region for unconstrained minimization
245: .    tao_lmvm - Limited memory variable metric method for unconstrained minimization
246: .    tao_cg - Nonlinear conjugate gradient method for unconstrained minimization
247: .    tao_nm - Nelder-Mead algorithm for derivate-free unconstrained minimization
248: .    tao_tron - Newton Trust Region method for bound constrained minimization
249: .    tao_gpcg - Newton Trust Region method for quadratic bound constrained minimization
250: .    tao_blmvm - Limited memory variable metric method for bound constrained minimization
251: .    tao_kt - Formulate a bound constrained problem as a complementarity problem
252: .    tao_bqpip - Interior point method for quadratic bound constrained minimization
253: .    tao_ssils - Infeasible semismooth method with a linesearch for complementarity problems
254: -    tao_ssfls - Feasible semismooth method with a linesearch for complementarity problems

256:    Level: beginner

258:    Note: 
259:    If the second argument specifies a TaoMethod, quotation marks should 
260:    surround the method.

262:    Note:
263:    The TaoMethod can be TAO_NULL (C/C++) or 
264:    TAO_NULL_CHARACTER (Fortran), in which case the
265:    method will be specified by the runtime option -tao_method

267:    If a particular optimization method is specified at runtime by
268:    the option '-tao_method', this choice will be used instead of
269:    any default that may have been specified as the input parameter
270:    "method" to this routine.

272: .keywords: Create, solver, method, context

274: .seealso: TaoSolve(), TaoSetMethod(), TaoSetApplication(), TaoDestroy()
275: @*/
276: int TaoCreate(MPI_Comm comm, TaoMethod method, TAO_SOLVER *newsolver)
277: {
278:   int info;

280:   TaoFunctionBegin;

282:   *newsolver = 0;

284:   info = TaoCreateFull(method,0,comm,newsolver);CHKERRQ(info);
285:   info = TaoSetMethodFromOptions(*newsolver);CHKERRQ(info);

287:   TaoFunctionReturn(0);
288: }



292: /* ----- Routines to initialize and destroy a minimization solver ---- */

296: /*@
297:    TaoSetDefaultParameters - Set the parameters used by all TAO solvers to a default value.  These
298:    parameter include convergence tolerances.  This routine is called before setting the
299:    method used by TAO

301:    Collective on TAO_SOLVER

303:    Input Parameters:
304: .  solver - the TAO_SOLVER solver context

306:    Level: developer

308: .keywords: options, defaults

310: .seealso: TaoCreate(), TaoSetDefaultStatistics(), TaoSetDefaultMonitors()
311: @*/
312: int TaoSetDefaultParameters(TAO_SOLVER solver){

314:   TaoFunctionBegin;
315:   solver->max_its            = 0;
316:   solver->max_funcs             = 100000000;
317:   solver->fatol              = 0.0;
318:   solver->frtol              = 0.0;
319:   solver->catol              = 0.0;
320:   solver->crtol              = 0.0;
321:   solver->gatol              = 0.0;
322:   solver->grtol              = 0.0;
323:   solver->xtol                     = 0.0;
324:   solver->trtol              = 0.0;
325:   solver->fmin               = -1.e30;
326:   TaoFunctionReturn(0);
327: }

331: /*@
332:    TaoSetDefaultStatistics - Initialize the statistics used by TAO for all of the solvers.
333:    These statistics include the iteration number, residual norms, and convergence status.
334:    This routine gets called before solving each optimization problem.

336:    Collective on TAO_SOLVER

338:    Input Parameters:
339: .  solver - the TAO_SOLVER solver context

341:    Level: developer

343: .keywords: options, defaults

345: .seealso: TaoCreate(), TaoSetDefaultParameters(), TaoSetDefaultMonitors(), TaoSolve()
346: @*/
347: int TaoSetDefaultStatistics(TAO_SOLVER solver){

349:   TaoFunctionBegin;
350:   solver->iter               = 0;
351:   solver->fc                 = 0;
352:   solver->norm                     = 0.0;
353:   solver->norm0                     = 0.0;
354:   solver->cnorm                     = 0.0;
355:   solver->cnorm0             = 0.0;
356:   solver->nfuncs             = 0;
357:   solver->ngrads             = 0;
358:   solver->nfgrads            = 0;
359:   solver->nhesss             = 0;
360:   solver->nvfunc             = 0;
361:   solver->njac               = 0;
362:   solver->linear_its         = 0;
363:   solver->lsflag             = 0;
364:   solver->reason             = TAO_CONTINUE_ITERATING;
365:   solver->step               = 1.0e+30;
366:   if (solver->conv_hist_reset == TAO_TRUE) solver->conv_hist_len = 0;
367:   TaoFunctionReturn(0);

369: }
372: /*@
373:    TaoSetDefaultMonitors - Set the default monitors and viewing options available in TAO.
374:    This routine is generally called only in TaoCreate().

376:    Collective on TAO_SOLVER

378:    Input Parameters:
379: .  solver - the TAO_SOLVER solver context

381:    Level: developer

383: .keywords: options, defaults

385: .seealso: TaoCreate(), TaoSetDefaultStatistics(), TaoSetDefaultParameters(), TaoSetMonitor(), TaoView()
386: @*/
387: int TaoSetDefaultMonitors(TAO_SOLVER solver){

389:   TaoFunctionBegin;
390:   solver->numbermonitors     = 0;
391:   solver->viewhessian        = TAO_FALSE;
392:   solver->viewgradient       = TAO_FALSE;
393:   solver->viewjacobian       = TAO_FALSE;
394:   solver->viewvfunc          = TAO_FALSE;
395:   solver->viewtao            = TAO_FALSE;
396:   solver->viewksptao        = TAO_FALSE;

398:   solver->converged          = TaoConverged_Default;
399:   solver->defaultmonitor     = TaoDefaultMonitor;

401:   solver->conv_hist_len      = 0;
402:   solver->conv_hist_max      = 0;
403:   solver->conv_hist          = TAO_NULL;
404:   solver->conv_hist_its      = TAO_NULL;
405:   solver->conv_hist_reset    = TAO_TRUE;

407:   solver->numberdestroyers   =0;

409:   TaoFunctionReturn(0);

411: }

413: /* ----- Routines to initialize and destroy a minimization solver ---- */

417: /*@
418:    TaoSetUp - Sets up the internal data structures for the later use
419:    of a minimization solver.

421:    Collective on TAO_SOLVER

423:    Input Parameters:
424: .  solver - the TAO_SOLVER solver context

426:    Notes:
427:    For basic use of the TAO_SOLVER solvers the user need not explicitly call
428:    TaoSetUp(), since these actions will automatically occur during
429:    the call to TaoSolve().  However, if one wishes to control this
430:    phase separately, TaoSetUp() should be called after TaoCreate()
431:    and optional routines of the form TaoSetXXX(), but before TaoSolve().  

433:    Level: developer

435: .keywords: Solve, setup

437: .seealso: TaoCreate(), TaoSolve(), TaoSetDown(), TaoDestroy()
438: @*/
439: int TaoSetUp(TAO_SOLVER solver)
440: {
441:   int      info;
442:   TaoTruth flag;
443:   TaoVec   *xx,*dx;
444:   TaoFunctionBegin;
445:   TaoValidHeaderSpecific(solver,TAO_COOKIE,1);

447:   //  info = TaoSetOptions(solver);CHKERRQ(info);
448:   if (!solver->set_method_called) {
449:     SETERRQ(1,"Must explicitly call TaoSetMethod() or TaoSetMethodFromOptions() before TaoSolve()");
450:   }

452:   /* Determine if the solver has already been set up with structures of the right dimension */
453:   if ( solver->setupcalled==TAO_TRUE) {
454:     info = TaoGetSolution(solver,&xx);CHKERRQ(info);
455:     if (!xx){SETERRQ(1,"Must explicitly call TaoSetApplication() and Set Variable Vector");}
456:     info = TaoGetStepDirectionVector(solver,&dx);CHKERRQ(info);
457:     if (dx){
458:       info = xx->Compatible(dx,&flag); CHKERRQ(info);
459:     } else {
460:       flag=TAO_FALSE;
461:     }
462:     if (flag==TAO_TRUE){
463:       info = TaoGetGradient(solver,&dx);CHKERRQ(info);
464:       if (dx){
465:         info = xx->Compatible(dx,&flag); CHKERRQ(info);
466:       } 
467:     }
468:     if (flag==TAO_FALSE){  /* Setup done, but data structures of wrong size */
469:       info = TaoSetDown(solver); CHKERRQ(info);
470:     }
471:   }

473:   if ( solver->setupcalled==TAO_FALSE) {
474:     if (solver->setup) {
475:       info = (*solver->setup)(solver,solver->data);CHKERRQ(info);    
476:     }
477:   } 
478:   solver->setupcalled=TAO_TRUE;
479:   info = TaoLineSearchSetUp(solver);CHKERRQ(info);
480:   TaoFunctionReturn(0);
481: }

485: /*@
486:    TaoDestroy - Destroys the TAO solver that was created with TaoCreate().

488:    Collective on TAO_SOLVER

490:    Input Parameter:
491: .  solver - the TAO_SOLVER solver context

493:    Level: beginner

495: .keywords: Destroy

497: .seealso: TaoCreate(), TaoSolve()
498: @*/
499: int TaoDestroy(TAO_SOLVER solver)
500: {
501:   int i,info;

503:   TaoFunctionBegin;
504:   TaoValidHeaderSpecific(solver,TAO_COOKIE,1);
505:   if (--solver->refct > 0) TaoFunctionReturn(0);

507:   for (i=0; i< solver->numberdestroyers; i++){
508:     info = (*solver->userdestroy[i])(solver->userctxdestroy[i]); CHKERRQ(info);
509:   }

511:   info = TaoResetSolver(solver);CHKERRQ(info);

513:   info = TaoObjectDestroy(solver); CHKERRQ(info);
514:   
515:   TaoFunctionReturn(0);
516: }

520: /*@
521:    TaoSetDown - Take down the data structures created in TaoSetUp().
522:    These structures typically include the work vectors, and linear solver.

524:    Collective on TAO_SOLVER

526:    Input Parameter:
527: .  solver - the TAO_SOLVER solver context

529:    Level: advanced

531: .keywords: Destroy

533: .seealso: TaoSetUp(), TaoDestroy()
534: @*/
535: int TaoSetDown(TAO_SOLVER solver)
536: {
537:   int info;

539:   TaoFunctionBegin;
540:   TaoValidHeaderSpecific(solver,TAO_COOKIE,1);

542:   if (solver->setupcalled){
543:     if (solver->setdown) {info = (*(solver)->setdown)(solver,solver->data);CHKERRQ(info);}
544:   }
545:   info = TaoSetLagrangianGradientVector(solver,0);CHKERRQ(info);
546:   info = TaoSetStepDirectionVector(solver,0);CHKERRQ(info);
547:   info = TaoSetVariableBounds(solver,0,0);CHKERRQ(info);
548:   solver->ksp = TAO_NULL;
549:   solver->setupcalled=TAO_FALSE;

551:   TaoFunctionReturn(0);
552: }

556: /*@
557:    TaoResetSolver - Take down the data structures created in TaoCreate__XXX().
558:    This routine destroys the line search, and the solver context.  It
559:    also set many of the solver routines for solving, options, duals, viewing,
560:    setup, and destroy to TAO_NULL.

562:    Collective on TAO_SOLVER

564:    Input Parameter:
565: .  solver - the TAO_SOLVER solver context

567:    Level: advanced

569: .keywords: Destroy

571: .seealso: TaoCreate(), TaoSetMethod(), TaoSetDown(), TaoDestroy()
572: @*/
573: int TaoResetSolver(TAO_SOLVER solver)
574: {
575:   int info;

577:   TaoFunctionBegin;
578:   TaoValidHeaderSpecific(solver,TAO_COOKIE,1);

580:   info = TaoSetDown(solver); CHKERRQ(info);
581:   if (solver->data){
582:     info = TaoFree(solver->data);CHKERRQ(info);
583:   }
584:   solver->data = 0;
585:   solver->CopyDuals=0;
586:   solver->solve=0;
587:   solver->data=0;
588:   solver->view=0;
589:   solver->setup=0;
590:   solver->setdown=0;
591:   solver->setfromoptions=0;

593:   info = TaoLineSearchDestroy(solver);CHKERRQ(info);
594:   info = TaoMeritFunctionDestroy(solver);CHKERRQ(info);

596:   /* Set Default Parameters */
597:   info = TaoSetDefaultParameters(solver); CHKERRQ(info);
598:   info = TaoSetDefaultMeritFunction(solver); CHKERRQ(info);

600:   solver->set_method_called  = TAO_FALSE;

602:   TaoFunctionReturn(0);
603: }



607: /* --------- Internal routines for TAO_SOLVER Package --------- */

611: /*@C
612:    TaoSetMethod - Sets the method for the unconstrained minimization solver.  

614:    Collective on TAO_SOLVER

616:    Input Parameters:
617: +  solver - the TAO_SOLVER solver context
618: -  type - a known method

620:    Options Database Key:
621: .  -tao_method <type> - Sets the method; use -help for a list
622:    of available methods (for instance, "-tao_method tao_lmvm" or 
623:    "-tao_method tao_tron")

625:    Available methods include:
626: +    tao_nls - Newton's method with line search for unconstrained minimization
627: .    tao_ntr - Newton's method with trust region for unconstrained minimization
628: .    tao_lmvm - Limited memory variable metric method for unconstrained minimization
629: .    tao_cg - Nonlinear conjugate gradient method for unconstrained minimization
630: .    tao_nm - Nelder-Mead algorithm for derivate-free unconstrained minimization
631: .    tao_tron - Newton Trust Region method for bound constrained minimization
632: .    tao_gpcg - Newton Trust Region method for quadratic bound constrained minimization
633: .    tao_blmvm - Limited memory variable metric method for bound constrained minimization
634: .    tao_kt - Formulate a bound constrained problem as a complementarity problem
635: .    tao_bqpip - Interior point method for quadratic bound constrained minimization
636: .    tao_ssils - Infeasible semismooth method with a linesearch for complementarity problems
637: -    tao_ssfls - Feasible semismooth method with a linesearch for complementarity problems

639:   Level: intermediate

641: .keywords: method, Create, solve

643: .seealso: TaoCreate(), TaoGetMethod()

645: @*/
646: int TaoSetMethod(TAO_SOLVER solver,TaoMethod type)
647: {
648:   int info;
649:   int (*r)(TAO_SOLVER);
650:   TaoTruth issame;

652:   TaoFunctionBegin;
653:   TaoValidHeaderSpecific(solver,TAO_COOKIE,1);

655:   info = TaoCompareMethod(solver,type,&issame);CHKERRQ(info);
656:   if (issame) TaoFunctionReturn(0);
657:   info = TaoResetSolver(solver); CHKERRQ(info);
658:   info = TaoFindSolver(solver,type,&r);CHKERRQ(info);  

660:   if (!r) SETERRQ1(1,"Unable to find requested TAO_SOLVER type %s",type);
661:   info = (*r)(solver);CHKERRQ(info);

663:   solver->set_method_called = TAO_TRUE;

665:   TaoFunctionReturn(0); 
666: }


669: /* --------------------------------------------------------------------- */
672: /*@C
673:   TaoSetTaoDualVariablesRoutine - Set a routine that can be called
674:   to compute the dual variables on the lower and upper bounds of the
675:   variables.

677:    Collective on TAO_SOLVER

679:    Input Parameters:
680: +  tao - the TAO_SOLVER context
681: -  dual - Dual variables routine

683:    Note: The calling sequence of the dual routine passes
684:    the TAO_SOLVER object in the first argument, an (optional)
685:    pointer to a TaoVec to put the duals of the lower bounds,
686:    an (optional) pointer to a TaoVec to put the duals of 
687:    the upper bounds, and the solver context passed to TAO
688:    in TaoSetSolver().

690:    Level: developer

692: .keywords: TAO_SOLVER, duals

694: .seealso: TaoSetTaoSolveRoutine()
695: @*/
696: int TaoSetTaoDualVariablesRoutine(TAO_SOLVER tao, 
697:                                   int (*duals)(TAO_SOLVER,TaoVec*,TaoVec*,void*))
698: {
699:   TaoFunctionBegin;
700:   tao->CopyDuals  = duals;
701:   TaoFunctionReturn(0);
702: }

706: /*@C
707:    TaoSetTaoSolveRoutine - Sets the routine that will solve an optimization application

709:    Collective on TAO_SOLVER

711:    Input Parameters:
712: +  solver - the TAO_SOLVER context
713: .  solve - routine that applies the algorithm
714: -  data - solver data structure (optional)

716:    Level: developer

718:    Note:
719:    This routine is generally used within a "TaoCreate_XXX" routine.
720:    TAO will call this routine as part of the TaoSolve() routine.

722:    Note:
723:    The first and third arguments of this routine will be used as
724:    the arguments of the solver routine provided here.

726: .keywords: TAO_SOLVER, solve

728: .seealso: TaoCreate(), TaoSolve(), TaoGetSolverContext()
729: @*/
730: int TaoSetTaoSolveRoutine(TAO_SOLVER tao, 
731:                           int (*solve)(TAO_SOLVER,void*), void*ctx)
732: {  
733:   TaoFunctionBegin;
734:   tao->solve                  = solve;
735:   tao->data                  = ctx;
736:   TaoFunctionReturn(0);
737: }

741: /*@C
742:    TaoSetTaoSetUpDownRoutines - Sets the routines that setup and destroy solver data structures

744:    Collective on TAO_SOLVER

746:    Input Parameters:
747: +  tao - the TAO_SOLVER context
748: .  setup - routine that creates the work vectors in a solver.
749: -  setdown - the routine that will destroy the work vectors of a solver

751:    Note:
752:    This routine is generally called within a "TaoCreate_XXX" routine.
753:    The routines set here will be called in the TaoSetApplication() and
754:    TaoDestroy() routines, respectively.  Vectors and other data structures
755:    needed by the solver can be created and destroyed within the TaoSolve_XXX()
756:    routine, or before and after this routine.  The advantage to doing it
757:    before and after is that the solver can be called multiple times
758:    without reallocated these structures -- improving efficiency.

760:    Note:
761:    When the 'setup' routine is called, the solution vector, and other
762:    data will be available to clone.
763:    
764:    Note:
765:    When TAO calls these routines, the second arguement will be the
766:    context specified in TaoSetTaoSolveRoutine().

768:    Level: developer

770: .keywords: TAO_SOLVER, setup, destroy

772: .seealso: TaoCreate(), TaoSetUp(), TaoSetDown(), TaoDestroy(), TaoSetTaoSolveRoutine()
773: @*/
774: int TaoSetTaoSetUpDownRoutines(TAO_SOLVER tao, 
775:                                  int (*setup)(TAO_SOLVER,void*),
776:                                  int (*setdown)(TAO_SOLVER,void*))
777: {
778:   TaoFunctionBegin;
779:   tao->setup                  = setup;
780:   tao->setdown                  = setdown;
781:   TaoFunctionReturn(0);
782: }

786: /*@C
787:    TaoSetTaoViewRoutine - Sets the routine that will display information
788:    about the optimization solver

790:    Collective on TAO_SOLVER

792:    Input Parameters:
793: +  tao - the TAO_SOLVER context
794: -  view - routine that views the solver

796:    Note:
797:    This routine is generally used within a "TaoCreate_XXX" routine.
798:    TAO will call this routine as part of the TaoView() routine.

800:    Note:
801:    When TAO calls these routines, the second arguement will be the
802:    context specified in TaoSetTaoSolveRoutine().

804:    Level: developer

806: .keywords: TAO_SOLVER, view

808: .seealso: TaoCreate(), TaoView(), TaoSetTaoSolveRoutine()
809: @*/
810: int TaoSetTaoViewRoutine(TAO_SOLVER tao, 
811:                          int (*view)(TAO_SOLVER,void*))
812: {  
813:   TaoFunctionBegin;
814:   tao->view               = view;
815:   TaoFunctionReturn(0);
816: }

820: /*@C
821:    TaoSetTaoSolveRoutine - Sets the routine that will set the options for
822:    the optimization solver.

824:    Collective on TAO_SOLVER

826:    Input Parameters:
827: +  solver - the TAO_SOLVER context
828: -  options - routine that views the solver

830:    Note:
831:    This routine is generally used within a "TaoCreate_XXX" routine.
832:    TAO will call this routine as part of the TaoSetOptions() routine.

834:    Note:
835:    When TAO calls these routines, the second argument will be the
836:    context specified in TaoSetTaoSolveRoutine().

838:    Level: developer

840: .keywords: TAO_SOLVER, solve

842: .seealso: TaoCreate(), TaoSetOptions(), TaoSetTaoSolveRoutine().
843: @*/
844: int TaoSetTaoOptionsRoutine(TAO_SOLVER tao, 
845:                             int (*options)(TAO_SOLVER,void*))
846: {  
847:   TaoFunctionBegin;
848:   tao->setfromoptions     = options;
849:   TaoFunctionReturn(0);
850: }


855: /*@C
856:    TaoGetApplication - Gets the user defined context for 
857:    the minimization solvers.  

859:    Collective on TAO_SOLVER

861:    Input Parameters:
862: +  tao - the TAO_SOLVER solver context
863: -  taoapp - user application context

865:    Level: advanced

867: .keywords: application, context

869: @*/
870: int TaoGetApplication(TAO_SOLVER tao, TaoApplication **taoapp){
871:   TaoFunctionBegin;
872:   *taoapp=tao->taoappl;
873:   TaoFunctionReturn(0);
874: }



880: /*@C
881:    TaoGetSolverContext - Gets the solver context for 
882:    the minimization solvers.  

884:    Collective on TAO_SOLVER

886:    Input Parameter:
887: +  tao - the TAO_SOLVER solver context
888: -  type - the name of the method

890:    Output Parameter:
891: .  solverctx - solver context IF the type matches.

893:    Level: developer

895: .keywords: application, context

897: .seealso: TaoSetTaoSolveRoutine()

899: @*/
900: int TaoGetSolverContext(TAO_SOLVER tao, TaoMethod type, void **solverctx){
901:   int info;
902:   TaoTruth issame;
903:   TaoFunctionBegin;
904:   if (solverctx){
905:     info = TaoCompareMethod(tao,type,&issame);CHKERRQ(info);
906:     if (issame) *solverctx=tao->data;
907:     else        *solverctx=0;
908:   }
909:   TaoFunctionReturn(0);
910: }