Actual source code: iguess.c
petsc-3.10.5 2019-03-28
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: KSPInitializePackage();
41: PetscFunctionListAdd(&KSPGuessList,sname,function);
42: return(0);
43: }
45: /*
46: KSPGuessRegisterAll - Registers all KSPGuess implementations in the KSP package.
48: Not Collective
50: Level: advanced
52: .keywords: KSPGuess, register, all
54: .seealso: KSPRegisterAll(), KSPInitializePackage()
55: */
56: PetscErrorCode KSPGuessRegisterAll(void)
57: {
61: if (KSPGuessRegisterAllCalled) return(0);
62: KSPGuessRegisterAllCalled = PETSC_TRUE;
63: KSPGuessRegister(KSPGUESSFISCHER,KSPGuessCreate_Fischer);
64: KSPGuessRegister(KSPGUESSPOD,KSPGuessCreate_POD);
65: return(0);
66: }
68: /*@
69: KSPGuessSetFromOptions
70: @*/
71: PetscErrorCode KSPGuessSetFromOptions(KSPGuess guess)
72: {
77: if (guess->ops->setfromoptions) { (*guess->ops->setfromoptions)(guess); }
78: return(0);
79: }
81: /*@
82: KSPGuessDestroy - Destroys KSPGuess context.
84: Collective on KSPGuess
86: Input Parameter:
87: . guess - initial guess object
89: Level: beginner
91: .keywords: KSP, destroy
93: .seealso: KSPGuessCreate(), KSPGuess, KSPGuessType
94: @*/
95: PetscErrorCode KSPGuessDestroy(KSPGuess *guess)
96: {
100: if (!*guess) return(0);
102: if (--((PetscObject)(*guess))->refct > 0) {*guess = 0; return(0);}
103: if ((*guess)->ops->destroy) { (*(*guess)->ops->destroy)(*guess); }
104: MatDestroy(&(*guess)->A);
105: PetscHeaderDestroy(guess);
106: return(0);
107: }
109: /*@C
110: KSPGuessView - View the KSPGuess object
112: Logically Collective on KSPGuess
114: Input Parameters:
115: + guess - the initial guess object for the Krylov method
116: - viewer - the viewer object
118: Notes:
120: Level: intermediate
122: .seealso: KSP, KSPGuess, KSPGuessType, KSPGuessRegister(), KSPGuessCreate(), PetscViewer
123: @*/
124: PetscErrorCode KSPGuessView(KSPGuess guess, PetscViewer view)
125: {
127: PetscBool ascii;
131: if (!view) {
132: PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)guess),&view);
133: }
136: PetscObjectTypeCompare((PetscObject)view,PETSCVIEWERASCII,&ascii);
137: if (ascii) {
138: PetscObjectPrintClassNamePrefixType((PetscObject)guess,view);
139: if (guess->ops->view) {
140: PetscViewerASCIIPushTab(view);
141: (*guess->ops->view)(guess,view);
142: PetscViewerASCIIPopTab(view);
143: }
144: }
145: return(0);
146: }
148: /*@
149: KSPGuessCreate - Creates the default KSPGuess context.
151: Collective on MPI_Comm
153: Input Parameter:
154: . comm - MPI communicator
156: Output Parameter:
157: . guess - location to put the KSPGuess context
159: Notes:
160: The default KSPGuess type is XXX
162: Level: beginner
164: .keywords: KSP, create, context
166: .seealso: KSPSolve(), KSPGuessDestroy(), KSPGuess, KSPGuessType, KSP
167: @*/
168: PetscErrorCode KSPGuessCreate(MPI_Comm comm,KSPGuess *guess)
169: {
170: KSPGuess tguess;
175: *guess = 0;
176: KSPInitializePackage();
177: PetscHeaderCreate(tguess,KSPGUESS_CLASSID,"KSPGuess","Initial guess for Krylov Method","KSPGuess",comm,KSPGuessDestroy,KSPGuessView);
178: tguess->omatstate = -1;
179: *guess = tguess;
180: return(0);
181: }
183: /*@C
184: KSPGuessSetType - Sets the type of a KSPGuess
186: Logically Collective on KSPGuess
188: Input Parameters:
189: + guess - the initial guess object for the Krylov method
190: - type - a known KSPGuess method
192: Options Database Key:
193: . -ksp_guess_type <method> - Sets the method; use -help for a list
194: of available methods
196: Notes:
198: Level: intermediate
200: .seealso: KSP, KSPGuess, KSPGuessType, KSPGuessRegister(), KSPGuessCreate()
202: @*/
203: PetscErrorCode KSPGuessSetType(KSPGuess guess, KSPGuessType type)
204: {
205: PetscErrorCode ierr,(*r)(KSPGuess);
206: PetscBool match;
212: PetscObjectTypeCompare((PetscObject)guess,type,&match);
213: if (match) return(0);
215: PetscFunctionListFind(KSPGuessList,type,&r);
216: if (!r) SETERRQ1(PetscObjectComm((PetscObject)guess),PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested KSPGuess type %s",type);
217: if (guess->ops->destroy) {
218: (*guess->ops->destroy)(guess);
219: guess->ops->destroy = NULL;
220: }
221: PetscMemzero(guess->ops,sizeof(struct _KSPGuessOps));
222: PetscObjectChangeTypeName((PetscObject)guess,type);
223: (*r)(guess);
224: return(0);
225: }
227: /*@C
228: KSPGuessGetType - Gets the KSPGuess type as a string from the KSPGuess object.
230: Not Collective
232: Input Parameter:
233: . guess - the initial guess context
235: Output Parameter:
236: . name - name of KSPGuess method
238: Level: intermediate
240: .keywords: KSP, get, method, name
242: .seealso: KSPGuessSetType()
243: @*/
244: PetscErrorCode KSPGuessGetType(KSPGuess guess,KSPGuessType *type)
245: {
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 KSPGuess
258: Input Parameter:
259: + guess - the initial guess context
260: . rhs - the corresponding rhs
261: - sol - the computed solution
263: Level: intermediate
265: .keywords: KSP, get, method, name
267: .seealso: KSPGuessCreate(), KSPGuess
268: @*/
269: PetscErrorCode KSPGuessUpdate(KSPGuess guess, Vec rhs, Vec sol)
270: {
277: if (guess->ops->update) { (*guess->ops->update)(guess,rhs,sol); }
278: return(0);
279: }
281: /*@
282: KSPGuessFormGuess - Form the initial guess
284: Collective on KSPGuess
286: Input Parameter:
287: + guess - the initial guess context
288: . rhs - the current rhs vector
289: - sol - the initial guess vector
291: Level: intermediate
293: .keywords: KSP, get, method, name
295: .seealso: KSPGuessCreate(), KSPGuess
296: @*/
297: PetscErrorCode KSPGuessFormGuess(KSPGuess guess, Vec rhs, Vec sol)
298: {
305: if (guess->ops->formguess) { (*guess->ops->formguess)(guess,rhs,sol); }
306: return(0);
307: }
309: /*@
310: KSPGuessSetUp - Setup the initial guess object
312: Collective on KSPGuess
314: Input Parameter:
315: - guess - the initial guess context
317: Level: intermediate
319: .keywords: KSP, get, method, name
321: .seealso: KSPGuessCreate(), KSPGuess
322: @*/
323: PetscErrorCode KSPGuessSetUp(KSPGuess guess)
324: {
325: PetscErrorCode ierr;
326: PetscObjectState matstate;
327: PetscInt oM = 0, oN = 0, M, N;
328: Mat omat = NULL;
332: if (guess->A) {
333: omat = guess->A;
334: MatGetSize(guess->A,&oM,&oN);
335: }
336: KSPGetOperators(guess->ksp,&guess->A,NULL);
337: PetscObjectReference((PetscObject)guess->A);
338: MatGetSize(guess->A,&M,&N);
339: PetscObjectStateGet((PetscObject)guess->A,&matstate);
340: if (omat != guess->A || guess->omatstate != matstate || M != oM || N != oN) {
341: PetscInfo7(guess,"Resetting KSPGuess since matrix, mat state or sizes have changed (mat %d, %D != %D, %D != %D, %D != %D)\n",(PetscBool)(omat != guess->A),guess->omatstate,matstate,oM,M,oN,N);
342: if (guess->ops->reset) { (*guess->ops->reset)(guess); }
343: } else {
344: PetscInfo(guess,"KSPGuess status unchanged\n");
345: }
346: if (guess->ops->setup) { (*guess->ops->setup)(guess); }
347: guess->omatstate = matstate;
348: MatDestroy(&omat);
349: return(0);
350: }