Actual source code: iguess.c

  1: #include <petsc/private/kspimpl.h>

  3: PetscFunctionList KSPGuessList = NULL;
  4: static PetscBool KSPGuessRegisterAllCalled;

  6: /*
  7:   KSPGuessRegister -  Adds a method for initial guess computation in Krylov subspace solver package.

  9:    Not Collective

 11:    Input Parameters:
 12: +  name_solver - name of a new user-defined solver
 13: -  routine_create - routine to create method context

 15:    Notes:
 16:    KSPGuessRegister() may be called multiple times to add several user-defined solvers.

 18:    Sample usage:
 19: .vb
 20:    KSPGuessRegister("my_initial_guess",MyInitialGuessCreate);
 21: .ve

 23:    Then, it can be chosen with the procedural interface via
 24: $     KSPSetGuessType(ksp,"my_initial_guess")
 25:    or at runtime via the option
 26: $     -ksp_guess_type my_initial_guess

 28:    Level: advanced

 30: .seealso: KSPGuess, KSPGuessRegisterAll()

 32: @*/
 33: PetscErrorCode  KSPGuessRegister(const char sname[],PetscErrorCode (*function)(KSPGuess))
 34: {
 35:   KSPInitializePackage();
 36:   PetscFunctionListAdd(&KSPGuessList,sname,function);
 37:   return 0;
 38: }

 40: /*
 41:   KSPGuessRegisterAll - Registers all KSPGuess implementations in the KSP package.

 43:   Not Collective

 45:   Level: advanced

 47: .seealso: KSPRegisterAll(),  KSPInitializePackage()
 48: */
 49: PetscErrorCode KSPGuessRegisterAll(void)
 50: {
 51:   if (KSPGuessRegisterAllCalled) return 0;
 52:   KSPGuessRegisterAllCalled = PETSC_TRUE;
 53:   KSPGuessRegister(KSPGUESSFISCHER,KSPGuessCreate_Fischer);
 54:   KSPGuessRegister(KSPGUESSPOD,KSPGuessCreate_POD);
 55:   return 0;
 56: }

 58: /*@
 59:     KSPGuessSetFromOptions - Sets the options for a KSPGuess from the options database

 61:     Collective on guess

 63:     Input Parameter:
 64: .    guess - KSPGuess object

 66:    Level: intermediate

 68: .seealso: KSPGuess, KSPGetGuess(), KSPSetGuessType(), KSPGuessType
 69: @*/
 70: PetscErrorCode KSPGuessSetFromOptions(KSPGuess guess)
 71: {
 73:   if (guess->ops->setfromoptions) (*guess->ops->setfromoptions)(guess);
 74:   return 0;
 75: }

 77: /*@
 78:     KSPGuessSetTolerance - Sets the relative tolerance used in either eigenvalue (POD) or singular value (Fischer type 3) calculations. Ignored by the first and second Fischer types.

 80:     Collective on guess

 82:     Input Parameter:
 83: .    guess - KSPGuess object

 85:    Level: intermediate

 87: .seealso: KSPGuess, KSPGuessType, KSPGuessSetFromOptions()
 88: @*/
 89: PetscErrorCode KSPGuessSetTolerance(KSPGuess guess, PetscReal tol)
 90: {
 92:   if (guess->ops->settolerance) (*guess->ops->settolerance)(guess,tol);
 93:   return 0;
 94: }

 96: /*@
 97:    KSPGuessDestroy - Destroys KSPGuess context.

 99:    Collective on kspGuess

101:    Input Parameter:
102: .  guess - initial guess object

104:    Level: beginner

106: .seealso: KSPGuessCreate(), KSPGuess, KSPGuessType
107: @*/
108: PetscErrorCode  KSPGuessDestroy(KSPGuess *guess)
109: {
110:   if (!*guess) return 0;
112:   if (--((PetscObject)(*guess))->refct > 0) {*guess = NULL; return 0;}
113:   if ((*guess)->ops->destroy) (*(*guess)->ops->destroy)(*guess);
114:   MatDestroy(&(*guess)->A);
115:   PetscHeaderDestroy(guess);
116:   return 0;
117: }

119: /*@C
120:    KSPGuessView - View the KSPGuess object

122:    Logically Collective on guess

124:    Input Parameters:
125: +  guess  - the initial guess object for the Krylov method
126: -  viewer - the viewer object

128:    Notes:

130:   Level: intermediate

132: .seealso: KSP, KSPGuess, KSPGuessType, KSPGuessRegister(), KSPGuessCreate(), PetscViewer
133: @*/
134: PetscErrorCode  KSPGuessView(KSPGuess guess, PetscViewer view)
135: {
136:   PetscBool      ascii;

139:   if (!view) {
140:     PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)guess),&view);
141:   }
144:   PetscObjectTypeCompare((PetscObject)view,PETSCVIEWERASCII,&ascii);
145:   if (ascii) {
146:     PetscObjectPrintClassNamePrefixType((PetscObject)guess,view);
147:     if (guess->ops->view) {
148:       PetscViewerASCIIPushTab(view);
149:       (*guess->ops->view)(guess,view);
150:       PetscViewerASCIIPopTab(view);
151:     }
152:   }
153:   return 0;
154: }

156: /*@
157:    KSPGuessCreate - Creates the default KSPGuess context.

159:    Collective

161:    Input Parameter:
162: .  comm - MPI communicator

164:    Output Parameter:
165: .  guess - location to put the KSPGuess context

167:    Notes:
168:    The default KSPGuess type is XXX

170:    Level: beginner

172: .seealso: KSPSolve(), KSPGuessDestroy(), KSPGuess, KSPGuessType, KSP
173: @*/
174: PetscErrorCode  KSPGuessCreate(MPI_Comm comm,KSPGuess *guess)
175: {
176:   KSPGuess       tguess;

179:   *guess = NULL;
180:   KSPInitializePackage();
181:   PetscHeaderCreate(tguess,KSPGUESS_CLASSID,"KSPGuess","Initial guess for Krylov Method","KSPGuess",comm,KSPGuessDestroy,KSPGuessView);
182:   tguess->omatstate = -1;
183:   *guess = tguess;
184:   return 0;
185: }

187: /*@C
188:    KSPGuessSetType - Sets the type of a KSPGuess

190:    Logically Collective on guess

192:    Input Parameters:
193: +  guess - the initial guess object for the Krylov method
194: -  type  - a known KSPGuess method

196:    Options Database Key:
197: .  -ksp_guess_type  <method> - Sets the method; use -help for a list
198:     of available methods

200:    Notes:

202:   Level: intermediate

204: .seealso: KSP, KSPGuess, KSPGuessType, KSPGuessRegister(), KSPGuessCreate()

206: @*/
207: PetscErrorCode  KSPGuessSetType(KSPGuess guess, KSPGuessType type)
208: {
209:   PetscBool      match;
210:   PetscErrorCode (*r)(KSPGuess);


215:   PetscObjectTypeCompare((PetscObject)guess,type,&match);
216:   if (match) return 0;

218:   PetscFunctionListFind(KSPGuessList,type,&r);
220:   if (guess->ops->destroy) {
221:     (*guess->ops->destroy)(guess);
222:     guess->ops->destroy = NULL;
223:   }
224:   PetscMemzero(guess->ops,sizeof(struct _KSPGuessOps));
225:   PetscObjectChangeTypeName((PetscObject)guess,type);
226:   (*r)(guess);
227:   return 0;
228: }

230: /*@C
231:    KSPGuessGetType - Gets the KSPGuess type as a string from the KSPGuess object.

233:    Not Collective

235:    Input Parameter:
236: .  guess - the initial guess context

238:    Output Parameter:
239: .  name - name of KSPGuess method

241:    Level: intermediate

243: .seealso: KSPGuessSetType()
244: @*/
245: PetscErrorCode  KSPGuessGetType(KSPGuess guess,KSPGuessType *type)
246: {
249:   *type = ((PetscObject)guess)->type_name;
250:   return 0;
251: }

253: /*@
254:     KSPGuessUpdate - Updates the guess object with the current solution and rhs vector

256:    Collective on guess

258:    Input Parameters:
259: +  guess - the initial guess context
260: .  rhs   - the corresponding rhs
261: -  sol   - the computed solution

263:    Level: intermediate

265: .seealso: KSPGuessCreate(), KSPGuess
266: @*/
267: PetscErrorCode  KSPGuessUpdate(KSPGuess guess, Vec rhs, Vec sol)
268: {
272:   if (guess->ops->update) (*guess->ops->update)(guess,rhs,sol);
273:   return 0;
274: }

276: /*@
277:     KSPGuessFormGuess - Form the initial guess

279:    Collective on guess

281:    Input Parameters:
282: +  guess - the initial guess context
283: .  rhs   - the current rhs vector
284: -  sol   - the initial guess vector

286:    Level: intermediate

288: .seealso: KSPGuessCreate(), KSPGuess
289: @*/
290: PetscErrorCode  KSPGuessFormGuess(KSPGuess guess, Vec rhs, Vec sol)
291: {
295:   if (guess->ops->formguess) (*guess->ops->formguess)(guess,rhs,sol);
296:   return 0;
297: }

299: /*@
300:     KSPGuessSetUp - Setup the initial guess object

302:    Collective on guess

304:    Input Parameter:
305: -  guess - the initial guess context

307:    Level: intermediate

309: .seealso: KSPGuessCreate(), KSPGuess
310: @*/
311: PetscErrorCode  KSPGuessSetUp(KSPGuess guess)
312: {
313:   PetscObjectState matstate;
314:   PetscInt         oM = 0, oN = 0, M, N;
315:   Mat              omat = NULL;
316:   PC               pc;
317:   PetscBool        reuse;

320:   if (guess->A) {
321:     omat = guess->A;
322:     MatGetSize(guess->A,&oM,&oN);
323:   }
324:   KSPGetOperators(guess->ksp,&guess->A,NULL);
325:   KSPGetPC(guess->ksp,&pc);
326:   PCGetReusePreconditioner(pc,&reuse);
327:   PetscObjectReference((PetscObject)guess->A);
328:   MatGetSize(guess->A,&M,&N);
329:   PetscObjectStateGet((PetscObject)guess->A,&matstate);
330:   if (M != oM || N != oN) {
331:     PetscInfo(guess,"Resetting KSPGuess since matrix sizes have changed (%D != %D, %D != %D)\n",oM,M,oN,N);
332:   } else if (!reuse && (omat != guess->A || guess->omatstate != matstate)) {
333:     PetscInfo(guess,"Resetting KSPGuess since %s has changed\n",omat != guess->A ? "matrix" : "matrix state");
334:     if (guess->ops->reset) (*guess->ops->reset)(guess);
335:   } else if (reuse) {
336:     PetscInfo(guess,"Not resettting KSPGuess since reuse preconditioner has been specified\n");
337:   } else {
338:     PetscInfo(guess,"KSPGuess status unchanged\n");
339:   }
340:   if (guess->ops->setup) (*guess->ops->setup)(guess);
341:   guess->omatstate = matstate;
342:   MatDestroy(&omat);
343:   return 0;
344: }