Actual source code: tao_lsolver.c

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

      6: /*@C
      7:   TaoCreateLinearSolver - Create a linear solver for use in TAO

      9:    Input Parameters:
     10: +  tao - the TAO_SOLVER context
     11: .  MM  - the matrix associated with the solver
     12: -  stype - the type of linear solver

     14:    Output Parameters:
     15: .  ksp - a linear solver

     17:    Types of linear solvers:
     18: +  100 - Solver for any square matrix
     19: .  110 - GMRES
     20: .  200 - Any symmetric matrix
     21: .  210 - MINRES
     22: .  220 - CG with Trust Region
     23: .  230 - SYMMLQ
     24: .  300 - Any symmetric positive definite
     25: -  310 - Conjugate Gradient

     27:    Level: developer

     29: .seealso: TaoLinearSolve()

     31: .keywords: linear solver
     32: @*/
     33: int TaoCreateLinearSolver(TAO_SOLVER tao, TaoMat *MM, int stype, TaoLinearSolver **ksp)
     34: {
     35:   int info;
     36:   TaoVec *xx;
     37:   TaoLinearSolver* nksp=0;

     39:   TaoFunctionBegin;
     40:   TaoValidHeaderSpecific(tao,TAO_COOKIE,1);
     41:   info = TaoGetSolution(tao,&xx);CHKERRQ(info);
     42:   if (MM){
     43:     if (tao->taoappl){
     44:       info = tao->taoappl->GetLinearSolver(MM,stype,&nksp);CHKERRQ(info);
     45:       if (ksp){
     46:         *ksp = nksp;
     47:       }
     48:       tao->ksp = nksp;
     49:     }
     50:   } else {
     51:     SETERRQ(1,"No matrix specified.");
     52:   }
     53:   TaoFunctionReturn(0);
     54: }

     58: /*@C
     59:   TaoDestroyLinearSolver - Destroy the linear solver used in TAO

     61:    Input Parameters:
     62: .  tao - the TAO_SOLVER context

     64:    Level: developer

     66: .seealso: TaoGetLinearSolver()

     68: .keywords: linear solver
     69: @*/
     70: int TaoDestroyLinearSolver(TAO_SOLVER tao)
     71: {
     72:   int info;

     74:   TaoFunctionBegin;
     75:   TaoValidHeaderSpecific(tao,TAO_COOKIE,1);
     76:   if (tao->ksp){
     77:     info=TaoLinearSolverDestroy(tao->ksp);CHKERRQ(info);
     78:     tao->ksp=0;
     79:   }
     80:   TaoFunctionReturn(0);
     81: }

     86: /*@C
     87:   TaoLinearSolve - Solve a linear system

     89:    Input Parameters:
     90: +  tao - the TAO_SOLVER context
     91: .  AA - the matrix
     92: -  bb - the right hand side

     94:    Output Parameters:
     95: +  xx - the solution
     96: -  success - false if linear solve is not successful

     98:    Level: developer

    100: .seealso: TaoSolve()

    102: .keywords: linear solver
    103: @*/
    104: int TaoLinearSolve(TAO_SOLVER tao, TaoMat *AA, TaoVec *bb, TaoVec* xx, TaoTruth *success)
    105: {
    106:   int info,lits;
    107:   TaoFunctionBegin;
    108:   TaoValidHeaderSpecific(tao,TAO_COOKIE,1);
    109:   if (tao->ksp){
    110:     info = tao->ksp->Solve(bb,xx,success);CHKERRQ(info);
    111:     info = tao->ksp->GetNumberIterations(&lits);CHKERRQ(info);
    112:     tao->linear_its += lits;
    113:     TaoFunctionReturn(0);
    114:   } else {
    115:     TaoFunctionReturn(1);
    116:   }
    117: }

    121: /*@C
    122:   TaoMinQPTrust - Minimize a quadratic function over a trust region

    124:    Input Parameters:
    125: +  tao - the TAO_SOLVER context
    126: .  AA - the matrix
    127: .  bb - the right hand side
    128: -  r - the trust region radius

    130:    Output Parameters:
    131: +  xx - the solution
    132: -  success - false if minimization is not successful

    134:    Level: developer

    136: .seealso: TaoLinearSolve()

    138: .keywords: linear solver
    139: @*/
    140: int TaoMinQPTrust(TAO_SOLVER tao, TaoMat *AA, TaoVec *bb, TaoVec* xx, double r, TaoTruth *success)
    141: {
    142:   int lits,info;
    143:   TaoFunctionBegin;
    144:   TaoValidHeaderSpecific(tao,TAO_COOKIE,1);
    145:   info = tao->ksp->MinQuadraticTrustRegion(bb,xx,r,success); CHKERRQ(info);
    146:   info = tao->ksp->GetNumberIterations(&lits);CHKERRQ(info);
    147:   tao->linear_its += lits;
    148:   TaoFunctionReturn(0);
    149: }

    155: /*@C
    156:   TaoPreLinearSolve - Prepare to solve a linear system

    158:    Input Parameters:
    159: +  tao - the TAO_SOLVER context
    160: -  AA - the matrix

    162:    Level: developer

    164: .seealso: TaoLinearSolve()

    166: .keywords: linear solver
    167: @*/
    168: int TaoPreLinearSolve(TAO_SOLVER tao, TaoMat *AA)
    169: {
    170:   int info;
    171:   TaoFunctionBegin;
    172:   TaoValidHeaderSpecific(tao,TAO_COOKIE,1);
    173:   info = tao->ksp->PreSolve(AA);CHKERRQ(info);
    174:   TaoFunctionReturn(0);
    175: }

    180: /*@C
    181:    TaoGetLinearSolver - Returns the KSP context for a TAO_SOLVER solver.

    183:    Not Collective, but if TAO_SOLVER object is parallel, then KSP object is parallel

    185:    Input Parameter:
    186: .  solver -  a TAO optimization solver

    188:    Output Parameter:
    189: .  ksp - the KSP context

    191:    Notes:
    192:    The user can then directly modify the linear solver context to modify the Krylov method, preconditioner,
    193:    and tolerances.

    195:    Level: developer

    197: .keywords: Linear Solver, context
    198: x
    199: .seealso: TaoGetKSP()
    200: @*/
    201: int TaoGetLinearSolver(TAO_SOLVER tao,TaoLinearSolver **ksp)
    202: {
    203:   TaoFunctionBegin;
    204:   TaoValidHeaderSpecific(tao,TAO_COOKIE,1);
    205:   if (ksp){
    206:     *ksp = tao->ksp;
    207:   }
    208:   TaoFunctionReturn(0);
    209: }

    214: /*@C
    215:   TaoViewLinearSolver - View the linear solver

    217:    Input Parameters:
    218: .  tao - the TAO_SOLVER context

    220:    Options Database Key:
    221: .  -tao_kspview - Calls TaoViewLinearSolver() at end of TaoSolve()

    223:    Level: intermediate

    225: .seealso: TaoView(), TaoLinearSolve()

    227: .keywords: linear solver
    228: @*/
    229: int TaoViewLinearSolver(TAO_SOLVER solver){
    230:   int info;
    231:   TaoFunctionBegin;
    232:   TaoValidHeaderSpecific(solver,TAO_COOKIE,1);
    233:   if (solver->ksp){
    234:     info = solver->ksp->View();CHKERRQ(info);
    235:   }
    236:   TaoFunctionReturn(0);
    237: }

    241: /*@C
    242:    TaoSetLinearSolverOptions - Set options for the linear solver

    244:    Collective on TAO_SOLVER

    246:    Input Parameter:
    247: .  tao - the TAO_SOLVER solver context

    249:    Level: developer

    251: .keywords: line search, options

    253: .seealso: TaoGetLinearSolver()
    254: @*/
    255: int TaoSetLinearSolverOptions(TAO_SOLVER tao){
    256:   int info;
    257:   TaoFunctionBegin;
    258:   TaoValidHeaderSpecific(tao,TAO_COOKIE,1);
    259:   if (tao->ksp){
    260:     info = tao->ksp->SetOptions();CHKERRQ(info);
    261:   }
    262:   TaoFunctionReturn(0);
    263: }