Actual source code: iguess.c
petsc-3.12.2 2019-11-22
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: .seealso: KSPGuess, KSPGuessRegisterAll()
32: @*/
33: PetscErrorCode KSPGuessRegister(const char sname[],PetscErrorCode (*function)(KSPGuess))
34: {
38: KSPInitializePackage();
39: PetscFunctionListAdd(&KSPGuessList,sname,function);
40: return(0);
41: }
43: /*
44: KSPGuessRegisterAll - Registers all KSPGuess implementations in the KSP package.
46: Not Collective
48: Level: advanced
50: .seealso: KSPRegisterAll(), KSPInitializePackage()
51: */
52: PetscErrorCode KSPGuessRegisterAll(void)
53: {
57: if (KSPGuessRegisterAllCalled) return(0);
58: KSPGuessRegisterAllCalled = PETSC_TRUE;
59: KSPGuessRegister(KSPGUESSFISCHER,KSPGuessCreate_Fischer);
60: KSPGuessRegister(KSPGUESSPOD,KSPGuessCreate_POD);
61: return(0);
62: }
64: /*@
65: KSPGuessSetFromOptions
66: @*/
67: PetscErrorCode KSPGuessSetFromOptions(KSPGuess guess)
68: {
73: if (guess->ops->setfromoptions) { (*guess->ops->setfromoptions)(guess); }
74: return(0);
75: }
77: /*@
78: KSPGuessDestroy - Destroys KSPGuess context.
80: Collective on kspGuess
82: Input Parameter:
83: . guess - initial guess object
85: Level: beginner
87: .seealso: KSPGuessCreate(), KSPGuess, KSPGuessType
88: @*/
89: PetscErrorCode KSPGuessDestroy(KSPGuess *guess)
90: {
94: if (!*guess) return(0);
96: if (--((PetscObject)(*guess))->refct > 0) {*guess = 0; return(0);}
97: if ((*guess)->ops->destroy) { (*(*guess)->ops->destroy)(*guess); }
98: MatDestroy(&(*guess)->A);
99: PetscHeaderDestroy(guess);
100: return(0);
101: }
103: /*@C
104: KSPGuessView - View the KSPGuess object
106: Logically Collective on guess
108: Input Parameters:
109: + guess - the initial guess object for the Krylov method
110: - viewer - the viewer object
112: Notes:
114: Level: intermediate
116: .seealso: KSP, KSPGuess, KSPGuessType, KSPGuessRegister(), KSPGuessCreate(), PetscViewer
117: @*/
118: PetscErrorCode KSPGuessView(KSPGuess guess, PetscViewer view)
119: {
121: PetscBool ascii;
125: if (!view) {
126: PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)guess),&view);
127: }
130: PetscObjectTypeCompare((PetscObject)view,PETSCVIEWERASCII,&ascii);
131: if (ascii) {
132: PetscObjectPrintClassNamePrefixType((PetscObject)guess,view);
133: if (guess->ops->view) {
134: PetscViewerASCIIPushTab(view);
135: (*guess->ops->view)(guess,view);
136: PetscViewerASCIIPopTab(view);
137: }
138: }
139: return(0);
140: }
142: /*@
143: KSPGuessCreate - Creates the default KSPGuess context.
145: Collective
147: Input Parameter:
148: . comm - MPI communicator
150: Output Parameter:
151: . guess - location to put the KSPGuess context
153: Notes:
154: The default KSPGuess type is XXX
156: Level: beginner
158: .seealso: KSPSolve(), KSPGuessDestroy(), KSPGuess, KSPGuessType, KSP
159: @*/
160: PetscErrorCode KSPGuessCreate(MPI_Comm comm,KSPGuess *guess)
161: {
162: KSPGuess tguess;
167: *guess = 0;
168: KSPInitializePackage();
169: PetscHeaderCreate(tguess,KSPGUESS_CLASSID,"KSPGuess","Initial guess for Krylov Method","KSPGuess",comm,KSPGuessDestroy,KSPGuessView);
170: tguess->omatstate = -1;
171: *guess = tguess;
172: return(0);
173: }
175: /*@C
176: KSPGuessSetType - Sets the type of a KSPGuess
178: Logically Collective on guess
180: Input Parameters:
181: + guess - the initial guess object for the Krylov method
182: - type - a known KSPGuess method
184: Options Database Key:
185: . -ksp_guess_type <method> - Sets the method; use -help for a list
186: of available methods
188: Notes:
190: Level: intermediate
192: .seealso: KSP, KSPGuess, KSPGuessType, KSPGuessRegister(), KSPGuessCreate()
194: @*/
195: PetscErrorCode KSPGuessSetType(KSPGuess guess, KSPGuessType type)
196: {
197: PetscErrorCode ierr,(*r)(KSPGuess);
198: PetscBool match;
204: PetscObjectTypeCompare((PetscObject)guess,type,&match);
205: if (match) return(0);
207: PetscFunctionListFind(KSPGuessList,type,&r);
208: if (!r) SETERRQ1(PetscObjectComm((PetscObject)guess),PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested KSPGuess type %s",type);
209: if (guess->ops->destroy) {
210: (*guess->ops->destroy)(guess);
211: guess->ops->destroy = NULL;
212: }
213: PetscMemzero(guess->ops,sizeof(struct _KSPGuessOps));
214: PetscObjectChangeTypeName((PetscObject)guess,type);
215: (*r)(guess);
216: return(0);
217: }
219: /*@C
220: KSPGuessGetType - Gets the KSPGuess type as a string from the KSPGuess object.
222: Not Collective
224: Input Parameter:
225: . guess - the initial guess context
227: Output Parameter:
228: . name - name of KSPGuess method
230: Level: intermediate
232: .seealso: KSPGuessSetType()
233: @*/
234: PetscErrorCode KSPGuessGetType(KSPGuess guess,KSPGuessType *type)
235: {
239: *type = ((PetscObject)guess)->type_name;
240: return(0);
241: }
243: /*@
244: KSPGuessUpdate - Updates the guess object with the current solution and rhs vector
246: Collective on guess
248: Input Parameter:
249: + guess - the initial guess context
250: . rhs - the corresponding rhs
251: - sol - the computed solution
253: Level: intermediate
255: .seealso: KSPGuessCreate(), KSPGuess
256: @*/
257: PetscErrorCode KSPGuessUpdate(KSPGuess guess, Vec rhs, Vec sol)
258: {
265: if (guess->ops->update) { (*guess->ops->update)(guess,rhs,sol); }
266: return(0);
267: }
269: /*@
270: KSPGuessFormGuess - Form the initial guess
272: Collective on guess
274: Input Parameter:
275: + guess - the initial guess context
276: . rhs - the current rhs vector
277: - sol - the initial guess vector
279: Level: intermediate
281: .seealso: KSPGuessCreate(), KSPGuess
282: @*/
283: PetscErrorCode KSPGuessFormGuess(KSPGuess guess, Vec rhs, Vec sol)
284: {
291: if (guess->ops->formguess) { (*guess->ops->formguess)(guess,rhs,sol); }
292: return(0);
293: }
295: /*@
296: KSPGuessSetUp - Setup the initial guess object
298: Collective on guess
300: Input Parameter:
301: - guess - the initial guess context
303: Level: intermediate
305: .seealso: KSPGuessCreate(), KSPGuess
306: @*/
307: PetscErrorCode KSPGuessSetUp(KSPGuess guess)
308: {
309: PetscErrorCode ierr;
310: PetscObjectState matstate;
311: PetscInt oM = 0, oN = 0, M, N;
312: Mat omat = NULL;
316: if (guess->A) {
317: omat = guess->A;
318: MatGetSize(guess->A,&oM,&oN);
319: }
320: KSPGetOperators(guess->ksp,&guess->A,NULL);
321: PetscObjectReference((PetscObject)guess->A);
322: MatGetSize(guess->A,&M,&N);
323: PetscObjectStateGet((PetscObject)guess->A,&matstate);
324: if (omat != guess->A || guess->omatstate != matstate || M != oM || N != oN) {
325: 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);
326: if (guess->ops->reset) { (*guess->ops->reset)(guess); }
327: } else {
328: PetscInfo(guess,"KSPGuess status unchanged\n");
329: }
330: if (guess->ops->setup) { (*guess->ops->setup)(guess); }
331: guess->omatstate = matstate;
332: MatDestroy(&omat);
333: return(0);
334: }