Actual source code: iguess.c
petsc-3.9.4 2018-09-11
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: MatDestroy(&(*guess)->A);
104: PetscHeaderDestroy(guess);
105: return(0);
106: }
108: /*@C
109: KSPGuessView - View the KSPGuess object
111: Logically Collective on KSPGuess
113: Input Parameters:
114: + guess - the initial guess object for the Krylov method
115: - viewer - the viewer object
117: Notes:
119: Level: intermediate
121: .seealso: KSP, KSPGuess, KSPGuessType, KSPGuessRegister(), KSPGuessCreate(), PetscViewer
122: @*/
123: PetscErrorCode KSPGuessView(KSPGuess guess, PetscViewer view)
124: {
126: PetscBool ascii;
130: if (!view) {
131: PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)guess),&view);
132: }
135: PetscObjectTypeCompare((PetscObject)view,PETSCVIEWERASCII,&ascii);
136: if (ascii) {
137: PetscObjectPrintClassNamePrefixType((PetscObject)guess,view);
138: if (guess->ops->view) {
139: PetscViewerASCIIPushTab(view);
140: (*guess->ops->view)(guess,view);
141: PetscViewerASCIIPopTab(view);
142: }
143: }
144: return(0);
145: }
147: /*@
148: KSPGuessCreate - Creates the default KSPGuess context.
150: Collective on MPI_Comm
152: Input Parameter:
153: . comm - MPI communicator
155: Output Parameter:
156: . guess - location to put the KSPGuess context
158: Notes:
159: The default KSPGuess type is XXX
161: Level: beginner
163: .keywords: KSP, create, context
165: .seealso: KSPSolve(), KSPGuessDestroy(), KSPGuess, KSPGuessType, KSP
166: @*/
167: PetscErrorCode KSPGuessCreate(MPI_Comm comm,KSPGuess *guess)
168: {
169: KSPGuess tguess;
174: *guess = 0;
175: KSPInitializePackage();
176: PetscHeaderCreate(tguess,KSPGUESS_CLASSID,"KSPGuess","Initial guess for Krylov Method","KSPGuess",comm,KSPGuessDestroy,KSPGuessView);
177: tguess->omatstate = -1;
178: *guess = tguess;
179: return(0);
180: }
182: /*@C
183: KSPGuessSetType - Sets the type of a KSPGuess
185: Logically Collective on KSPGuess
187: Input Parameters:
188: + guess - the initial guess object for the Krylov method
189: - type - a known KSPGuess method
191: Options Database Key:
192: . -ksp_guess_type <method> - Sets the method; use -help for a list
193: of available methods
195: Notes:
197: Level: intermediate
199: .seealso: KSP, KSPGuess, KSPGuessType, KSPGuessRegister(), KSPGuessCreate()
201: @*/
202: PetscErrorCode KSPGuessSetType(KSPGuess guess, KSPGuessType type)
203: {
204: PetscErrorCode ierr,(*r)(KSPGuess);
205: PetscBool match;
211: PetscObjectTypeCompare((PetscObject)guess,type,&match);
212: if (match) return(0);
214: PetscFunctionListFind(KSPGuessList,type,&r);
215: if (!r) SETERRQ1(PetscObjectComm((PetscObject)guess),PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested KSPGuess type %s",type);
216: if (guess->ops->destroy) {
217: (*guess->ops->destroy)(guess);
218: guess->ops->destroy = NULL;
219: }
220: PetscMemzero(guess->ops,sizeof(struct _KSPGuessOps));
221: PetscObjectChangeTypeName((PetscObject)guess,type);
222: (*r)(guess);
223: return(0);
224: }
226: /*@C
227: KSPGuessGetType - Gets the KSPGuess type as a string from the KSPGuess object.
229: Not Collective
231: Input Parameter:
232: . guess - the initial guess context
234: Output Parameter:
235: . name - name of KSPGuess method
237: Level: intermediate
239: .keywords: KSP, get, method, name
241: .seealso: KSPGuessSetType()
242: @*/
243: PetscErrorCode KSPGuessGetType(KSPGuess guess,KSPGuessType *type)
244: {
248: *type = ((PetscObject)guess)->type_name;
249: return(0);
250: }
252: /*@
253: KSPGuessUpdate - Updates the guess object with the current solution and rhs vector
255: Collective on KSPGuess
257: Input Parameter:
258: + guess - the initial guess context
259: . rhs - the corresponding rhs
260: - sol - the computed solution
262: Level: intermediate
264: .keywords: KSP, get, method, name
266: .seealso: KSPGuessCreate(), KSPGuess
267: @*/
268: PetscErrorCode KSPGuessUpdate(KSPGuess guess, Vec rhs, Vec sol)
269: {
276: if (guess->ops->update) { (*guess->ops->update)(guess,rhs,sol); }
277: return(0);
278: }
280: /*@
281: KSPGuessFormGuess - Form the initial guess
283: Collective on KSPGuess
285: Input Parameter:
286: + guess - the initial guess context
287: . rhs - the current rhs vector
288: - sol - the initial guess vector
290: Level: intermediate
292: .keywords: KSP, get, method, name
294: .seealso: KSPGuessCreate(), KSPGuess
295: @*/
296: PetscErrorCode KSPGuessFormGuess(KSPGuess guess, Vec rhs, Vec sol)
297: {
304: if (guess->ops->formguess) { (*guess->ops->formguess)(guess,rhs,sol); }
305: return(0);
306: }
308: /*@
309: KSPGuessSetUp - Setup the initial guess object
311: Collective on KSPGuess
313: Input Parameter:
314: - guess - the initial guess context
316: Level: intermediate
318: .keywords: KSP, get, method, name
320: .seealso: KSPGuessCreate(), KSPGuess
321: @*/
322: PetscErrorCode KSPGuessSetUp(KSPGuess guess)
323: {
324: PetscErrorCode ierr;
325: PetscObjectState matstate;
326: PetscInt oM = 0, oN = 0, M, N;
327: Mat omat = NULL;
331: if (guess->A) {
332: omat = guess->A;
333: MatGetSize(guess->A,&oM,&oN);
334: }
335: KSPGetOperators(guess->ksp,&guess->A,NULL);
336: PetscObjectReference((PetscObject)guess->A);
337: MatGetSize(guess->A,&M,&N);
338: PetscObjectStateGet((PetscObject)guess->A,&matstate);
339: if (omat != guess->A || guess->omatstate != matstate || M != oM || N != oN) {
340: 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);
341: if (guess->ops->reset) { (*guess->ops->reset)(guess); }
342: } else {
343: PetscInfo(guess,"KSPGuess status unchanged\n");
344: }
345: if (guess->ops->setup) { (*guess->ops->setup)(guess); }
346: guess->omatstate = matstate;
347: MatDestroy(&omat);
348: return(0);
349: }