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:   TaoFunctionBegin;
 73:   TaoValidHeaderSpecific(tao,TAO_COOKIE,1);
 74:   tao->ksp=0;
 75:   TaoFunctionReturn(0);
 76: }


 81: /*@C
 82:   TaoLinearSolve - Solve a linear system

 84:    Input Parameters:
 85: +  tao - the TAO_SOLVER context
 86: .  AA - the matrix
 87: -  bb - the right hand side

 89:    Output Parameters:
 90: +  xx - the solution
 91: -  success - false if linear solve is not successful 

 93:    Level: developer

 95: .seealso: TaoSolve()

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

116: /*@C
117:   TaoLinearSolveTrustRegion - Minimize the quadratic function <x, Qx - b> 
118:   with the trust region constraint norm(x, M) <= r, where M is a symmetric 
119:   positive definite preconditioner.

121:    Input Parameters:
122: +  tao - the TAO_SOLVER context
123: .  Q - the matrix
124: .  b - the right hand side
125: -  r - the trust region radius

127:    Output Parameters:
128: +  x - the solution
129: -  success - false if minimization is not successful

131:    Level: developer

133: .seealso: TaoLinearSolve()

135: .keywords: linear solver
136: @*/
137: int TaoLinearSolveTrustRegion(TAO_SOLVER tao, TaoMat *Q, TaoVec *b, TaoVec *x, 
138:                               double r, TaoTruth *success)
139: {
140:   int lits,info;
141:   TaoFunctionBegin;
142:   TaoValidHeaderSpecific(tao,TAO_COOKIE,1);
143:   info = tao->ksp->SolveTrustRegion(b, x, r, success); CHKERRQ(info);
144:   info = tao->ksp->GetNumberIterations(&lits);CHKERRQ(info);
145:   tao->linear_its += lits;
146:   TaoFunctionReturn(0);
147: }

151: /*@C
152:   TaoPreLinearSolve - Prepare to solve a linear system

154:    Input Parameters:
155: +  tao - the TAO_SOLVER context
156: -  AA - the matrix

158:    Level: developer

160: .seealso: TaoLinearSolve()

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


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

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

181:    Input Parameter:
182: .  solver -  a TAO optimization solver

184:    Output Parameter:
185: .  ksp - the KSP context

187:    Notes:
188:    The user can then directly modify the linear solver context to modify the Krylov method, preconditioner,
189:    and tolerances.

191:    Level: developer

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


210: /*@C
211:   TaoViewLinearSolver - View the linear solver

213:    Input Parameters:
214: .  tao - the TAO_SOLVER context

216:    Options Database Key:
217: .  -tao_kspview - Calls TaoViewLinearSolver() at end of TaoSolve()

219:    Level: intermediate

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

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

237: /*@C
238:    TaoSetLinearSolverOptions - Set options for the linear solver

240:    Collective on TAO_SOLVER

242:    Input Parameter:
243: .  tao - the TAO_SOLVER solver context

245:    Level: developer

247: .keywords: line search, options

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