Actual source code: iguess.c

petsc-3.8.2 2017-11-09
Report Typos and Errors
  1:  #include <petsc/private/kspimpl.h>

  3: PetscFunctionList KSPGuessList = 0;
  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: .keywords: KSP, register, KSPGuess

 32: .seealso: KSPGuess, KSPGuessRegisterAll()

 34: @*/
 35: PetscErrorCode  KSPGuessRegister(const char sname[],PetscErrorCode (*function)(KSPGuess))
 36: {

 40:   PetscFunctionListAdd(&KSPGuessList,sname,function);
 41:   return(0);
 42: }

 44: /*
 45:   KSPGuessRegisterAll - Registers all KSPGuess implementations in the KSP package.

 47:   Not Collective

 49:   Level: advanced

 51: .keywords: KSPGuess, register, all

 53: .seealso: KSPRegisterAll(),  KSPInitializePackage()
 54: */
 55: PetscErrorCode KSPGuessRegisterAll(void)
 56: {

 60:   if (KSPGuessRegisterAllCalled) return(0);
 61:   KSPGuessRegisterAllCalled = PETSC_TRUE;
 62:   KSPGuessRegister(KSPGUESSFISCHER,KSPGuessCreate_Fischer);
 63:   KSPGuessRegister(KSPGUESSPOD,KSPGuessCreate_POD);
 64:   return(0);
 65: }

 67: /*@
 68:     KSPGuessSetFromOptions
 69: @*/
 70: PetscErrorCode KSPGuessSetFromOptions(KSPGuess guess)
 71: {

 76:   if (guess->ops->setfromoptions) { (*guess->ops->setfromoptions)(guess); }
 77:   return(0);
 78: }

 80: /*@
 81:    KSPGuessDestroy - Destroys KSPGuess context.

 83:    Collective on KSPGuess

 85:    Input Parameter:
 86: .  guess - initial guess object

 88:    Level: beginner

 90: .keywords: KSP, destroy

 92: .seealso: KSPGuessCreate(), KSPGuess, KSPGuessType
 93: @*/
 94: PetscErrorCode  KSPGuessDestroy(KSPGuess *guess)
 95: {

 99:   if (!*guess) return(0);
101:   if (--((PetscObject)(*guess))->refct > 0) {*guess = 0; return(0);}
102:   if ((*guess)->ops->destroy) { (*(*guess)->ops->destroy)(*guess); }
103:   PetscHeaderDestroy(guess);
104:   return(0);
105: }

107: /*@C
108:    KSPGuessView - View the KSPGuess object

110:    Logically Collective on KSPGuess

112:    Input Parameters:
113: +  guess  - the initial guess object for the Krylov method
114: -  viewer - the viewer object

116:    Notes:

118:   Level: intermediate

120: .seealso: KSP, KSPGuess, KSPGuessType, KSPGuessRegister(), KSPGuessCreate(), PetscViewer
121: @*/
122: PetscErrorCode  KSPGuessView(KSPGuess guess, PetscViewer view)
123: {
125:   PetscBool      ascii;

129:   if (!view) {
130:     PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)guess),&view);
131:   }
134:   PetscObjectTypeCompare((PetscObject)view,PETSCVIEWERASCII,&ascii);
135:   if (ascii) {
136:     PetscObjectPrintClassNamePrefixType((PetscObject)guess,view);
137:     if (guess->ops->view) {
138:       PetscViewerASCIIPushTab(view);
139:       (*guess->ops->view)(guess,view);
140:       PetscViewerASCIIPopTab(view);
141:     }
142:   }
143:   return(0);
144: }

146: /*@
147:    KSPGuessCreate - Creates the default KSPGuess context.

149:    Collective on MPI_Comm

151:    Input Parameter:
152: .  comm - MPI communicator

154:    Output Parameter:
155: .  guess - location to put the KSPGuess context

157:    Notes:
158:    The default KSPGuess type is XXX

160:    Level: beginner

162: .keywords: KSP, create, context

164: .seealso: KSPSolve(), KSPGuessDestroy(), KSPGuess, KSPGuessType, KSP
165: @*/
166: PetscErrorCode  KSPGuessCreate(MPI_Comm comm,KSPGuess *guess)
167: {
168:   KSPGuess       tguess;

173:   *guess = 0;
174:   KSPInitializePackage();
175:   PetscHeaderCreate(tguess,KSPGUESS_CLASSID,"KSPGuess","Initial guess for Krylov Method","KSPGuess",comm,KSPGuessDestroy,KSPGuessView);
176:   *guess = tguess;
177:   return(0);
178: }

180: /*@C
181:    KSPGuessSetType - Sets the type of a KSPGuess

183:    Logically Collective on KSPGuess

185:    Input Parameters:
186: +  guess - the initial guess object for the Krylov method
187: -  type  - a known KSPGuess method

189:    Options Database Key:
190: .  -ksp_guess_type  <method> - Sets the method; use -help for a list
191:     of available methods

193:    Notes:

195:   Level: intermediate

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

199: @*/
200: PetscErrorCode  KSPGuessSetType(KSPGuess guess, KSPGuessType type)
201: {
202:   PetscErrorCode ierr,(*r)(KSPGuess);
203:   PetscBool      match;


209:   PetscObjectTypeCompare((PetscObject)guess,type,&match);
210:   if (match) return(0);

212:    PetscFunctionListFind(KSPGuessList,type,&r);
213:   if (!r) SETERRQ1(PetscObjectComm((PetscObject)guess),PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested KSPGuess type %s",type);
214:   if (guess->ops->destroy) {
215:     (*guess->ops->destroy)(guess);
216:     guess->ops->destroy = NULL;
217:   }
218:   PetscMemzero(guess->ops,sizeof(struct _KSPGuessOps));
219:   PetscObjectChangeTypeName((PetscObject)guess,type);
220:   (*r)(guess);
221:   return(0);
222: }

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

227:    Not Collective

229:    Input Parameter:
230: .  guess - the initial guess context

232:    Output Parameter:
233: .  name - name of KSPGuess method

235:    Level: intermediate

237: .keywords: KSP, get, method, name

239: .seealso: KSPGuessSetType()
240: @*/
241: PetscErrorCode  KSPGuessGetType(KSPGuess guess,KSPGuessType *type)
242: {
246:   *type = ((PetscObject)guess)->type_name;
247:   return(0);
248: }

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

253:    Collective on KSPGuess

255:    Input Parameter:
256: +  guess - the initial guess context
257: .  rhs   - the corresponding rhs
258: -  sol   - the computed solution

260:    Level: intermediate

262: .keywords: KSP, get, method, name

264: .seealso: KSPGuessCreate(), KSPGuess
265: @*/
266: PetscErrorCode  KSPGuessUpdate(KSPGuess guess, Vec rhs, Vec sol)
267: {

274:   if (guess->ops->update) { (*guess->ops->update)(guess,rhs,sol); }
275:   return(0);
276: }

278: /*@
279:     KSPGuessFormGuess - Form the initial guess

281:    Collective on KSPGuess

283:    Input Parameter:
284: +  guess - the initial guess context
285: .  rhs   - the current rhs vector
286: -  sol   - the initial guess vector

288:    Level: intermediate

290: .keywords: KSP, get, method, name

292: .seealso: KSPGuessCreate(), KSPGuess
293: @*/
294: PetscErrorCode  KSPGuessFormGuess(KSPGuess guess, Vec rhs, Vec sol)
295: {

302:   if (guess->ops->formguess) { (*guess->ops->formguess)(guess,rhs,sol); }
303:   return(0);
304: }

306: /*@
307:     KSPGuessSetUp - Setup the initial guess object

309:    Collective on KSPGuess

311:    Input Parameter:
312: -  guess - the initial guess context

314:    Level: intermediate

316: .keywords: KSP, get, method, name

318: .seealso: KSPGuessCreate(), KSPGuess
319: @*/
320: PetscErrorCode  KSPGuessSetUp(KSPGuess guess)
321: {
322:   PetscErrorCode   ierr;
323:   PetscObjectState omatstate = -1, matstate;
324:   PetscInt         oM = 0, oN = 0, M, N;

328:   if (guess->A) {
329:     PetscObjectStateGet((PetscObject)guess->A,&omatstate);
330:     MatGetSize(guess->A,&oM,&oN);
331:   }
332:   KSPGetOperators(guess->ksp,&guess->A,NULL);
333:   MatGetSize(guess->A,&M,&N);
334:   PetscObjectStateGet((PetscObject)guess->A,&matstate);
335:   if (omatstate != matstate || M != oM || N != oN) {
336:     PetscInfo6(guess,"Resetting KSPGuess since mat state or sizes have changed (%D != %D, %D != %D, %D != %D)\n",omatstate,matstate,oM,M,oN,N);
337:     if (guess->ops->reset) { (*guess->ops->reset)(guess); }
338:   } else {
339:     PetscInfo(guess,"KSPGuess status unchanged\n");
340:   }
341:   if (guess->ops->setup) { (*guess->ops->setup)(guess); }
342:   return(0);
343: }