1: /*
2: MFN routines related to problem setup.
4: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
5: SLEPc - Scalable Library for Eigenvalue Problem Computations
6: Copyright (c) 2002-2014, Universitat Politecnica de Valencia, Spain
8: This file is part of SLEPc.
10: SLEPc is free software: you can redistribute it and/or modify it under the
11: terms of version 3 of the GNU Lesser General Public License as published by
12: the Free Software Foundation.
14: SLEPc is distributed in the hope that it will be useful, but WITHOUT ANY
15: WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16: FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
17: more details.
19: You should have received a copy of the GNU Lesser General Public License
20: along with SLEPc. If not, see <http://www.gnu.org/licenses/>.
21: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
22: */
24: #include <slepc-private/mfnimpl.h> /*I "slepcmfn.h" I*/
28: /*@
29: MFNSetUp - Sets up all the internal data structures necessary for the
30: execution of the matrix function solver.
32: Collective on MFN 34: Input Parameter:
35: . mfn - matrix function context
37: Notes:
38: This function need not be called explicitly in most cases, since MFNSolve()
39: calls it. It can be useful when one wants to measure the set-up time
40: separately from the solve time.
42: Level: advanced
44: .seealso: MFNCreate(), MFNSolve(), MFNDestroy()
45: @*/
46: PetscErrorCode MFNSetUp(MFN mfn) 47: {
49: PetscInt N;
53: if (mfn->setupcalled) return(0);
54: PetscLogEventBegin(MFN_SetUp,mfn,0,0,0);
56: /* reset the convergence flag from the previous solves */
57: mfn->reason = MFN_CONVERGED_ITERATING;
59: /* Set default solver type (MFNSetFromOptions was not called) */
60: if (!((PetscObject)mfn)->type_name) {
61: MFNSetType(mfn,MFNKRYLOV);
62: }
63: if (!mfn->ds) { MFNGetDS(mfn,&mfn->ds); }
64: DSReset(mfn->ds);
65: if (!((PetscObject)mfn->rand)->type_name) {
66: PetscRandomSetFromOptions(mfn->rand);
67: }
69: /* Check problem dimensions */
70: if (!mfn->A) SETERRQ(PetscObjectComm((PetscObject)mfn),PETSC_ERR_ARG_WRONGSTATE,"MFNSetOperator must be called first");
71: MatGetSize(mfn->A,&N,NULL);
72: if (mfn->ncv > N) mfn->ncv = N;
74: /* Set default function */
75: if (!mfn->function) {
76: MFNSetFunction(mfn,SLEPC_FUNCTION_EXP);
77: }
79: /* call specific solver setup */
80: (*mfn->ops->setup)(mfn);
82: /* set tolerance if not yet set */
83: if (mfn->tol==PETSC_DEFAULT) mfn->tol = SLEPC_DEFAULT_TOL;
85: PetscLogEventEnd(MFN_SetUp,mfn,0,0,0);
86: mfn->setupcalled = 1;
87: return(0);
88: }
92: /*@
93: MFNSetOperator - Sets the matrix for which the matrix function is to be computed.
95: Collective on MFN and Mat
97: Input Parameters:
98: + mfn - the matrix function context
99: - A - the problem matrix
101: Notes:
102: It must be called after MFNSetUp(). If it is called again after MFNSetUp() then
103: the MFN object is reset.
105: Level: beginner
107: .seealso: MFNSolve(), MFNSetUp(), MFNReset()
108: @*/
109: PetscErrorCode MFNSetOperator(MFN mfn,Mat A)110: {
112: PetscInt m,n;
119: MatGetSize(A,&m,&n);
120: if (m!=n) SETERRQ(PetscObjectComm((PetscObject)mfn),PETSC_ERR_ARG_WRONG,"A is a non-square matrix");
121: if (mfn->setupcalled) { MFNReset(mfn); }
122: PetscObjectReference((PetscObject)A);
123: MatDestroy(&mfn->A);
124: mfn->A = A;
125: return(0);
126: }
130: /*@
131: MFNGetOperator - Gets the matrix associated with the MFN object.
133: Collective on MFN and Mat
135: Input Parameter:
136: . mfn - the MFN context
138: Output Parameters:
139: . A - the matrix for which the matrix function is to be computed
141: Level: intermediate
143: .seealso: MFNSolve(), MFNSetOperator()
144: @*/
145: PetscErrorCode MFNGetOperator(MFN mfn,Mat *A)146: {
150: *A = mfn->A;
151: return(0);
152: }
156: /*@
157: MFNAllocateSolution - Allocate memory storage for common variables such
158: as the basis vectors.
160: Collective on MFN162: Input Parameters:
163: + mfn - eigensolver context
164: - extra - number of additional positions, used for methods that require a
165: working basis slightly larger than ncv
167: Developers Note:
168: This is PETSC_EXTERN because it may be required by user plugin MFN169: implementations.
171: Level: developer
172: @*/
173: PetscErrorCode MFNAllocateSolution(MFN mfn,PetscInt extra)174: {
176: PetscInt oldsize,requested;
177: Vec t;
180: requested = mfn->ncv + extra;
182: /* oldsize is zero if this is the first time setup is called */
183: BVGetSizes(mfn->V,NULL,NULL,&oldsize);
185: /* allocate basis vectors */
186: if (!mfn->V) { MFNGetBV(mfn,&mfn->V); }
187: if (!oldsize) {
188: if (!((PetscObject)(mfn->V))->type_name) {
189: BVSetType(mfn->V,BVSVEC);
190: }
191: MatGetVecs(mfn->A,&t,NULL);
192: BVSetSizesFromVec(mfn->V,t,requested);
193: VecDestroy(&t);
194: } else {
195: BVResize(mfn->V,requested,PETSC_FALSE);
196: }
197: return(0);
198: }