Xyce  6.1
N_NLS_MatrixFreeEpetraOperator.C
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 // Copyright Notice
3 //
4 // Copyright 2002 Sandia Corporation. Under the terms
5 // of Contract DE-AC04-94AL85000 with Sandia Corporation, the U.S.
6 // Government retains certain rights in this software.
7 //
8 // Xyce(TM) Parallel Electrical Simulator
9 // Copyright (C) 2002-2015 Sandia Corporation
10 //
11 // This program is free software: you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation, either version 3 of the License, or
14 // (at your option) any later version.
15 //
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program. If not, see <http://www.gnu.org/licenses/>.
23 //-----------------------------------------------------------------------------
24 
25 //-----------------------------------------------------------------------------
26 // Filename : $RCSfile: N_NLS_MatrixFreeEpetraOperator.C,v $
27 //
28 // Purpose :
29 //
30 // Creator : Todd Coffey, 1414
31 //
32 // Creation Date : 09/04/08
33 //
34 // Revision Information:
35 // ---------------------
36 //
37 // Revision Number: $Revision: 1.15 $
38 //
39 // Revision Date : $Date: 2015/04/08 19:18:29 $
40 //
41 // Current Owner : $Author: tvrusso $
42 //-------------------------------------------------------------------------
43 
44 #include <Xyce_config.h>
45 
46 
47 // ---------- Standard Includes ----------
48 
49 // ---------- Xyce Includes ----------
50 
51 #include <N_ERH_ErrorMgr.h>
52 #include <N_NLS_NonLinearSolver.h>
54 #include <N_LAS_Vector.h>
55 #include <N_PDS_ParMap.h>
56 
57 namespace Xyce {
58 namespace Nonlinear {
59 
60 //-----------------------------------------------------------------------------
61 // Function : matrixFreeEpetraOperator
62 // Purpose : non-member constructor
63 // Special Notes :
64 // Scope : public
65 // Creator : Todd Coffey, 1414
66 // Creation Date : 9/4/08
67 //-----------------------------------------------------------------------------
68 RCP<MatrixFreeEpetraOperator> matrixFreeEpetraOperator(
69  RCP<NonLinearSolver> nonlinearSolver,
70  RCP<Linear::Vector> solVector,
71  RCP<Linear::Vector> rhsVector,
72  RCP<const Epetra_Map> solutionMap
73  )
74 {
75  RCP<MatrixFreeEpetraOperator> epetraOperator =
76  rcp(new MatrixFreeEpetraOperator);
77  epetraOperator->initialize(nonlinearSolver,
78  solVector,
79  rhsVector,
80  solutionMap
81  );
82  return epetraOperator;
83 }
84 
85 
86 //-----------------------------------------------------------------------------
87 // Function : MatrixFreeEpetraOperator::MatrixFreeEpetraOperator
88 // Purpose : Constructor
89 // Special Notes :
90 // Scope : public
91 // Creator : Todd Coffey, 1414
92 // Creation Date : 9/4/08
93 //-----------------------------------------------------------------------------
95 {
96  isInitialized_ = false;
97 }
98 
99 //-----------------------------------------------------------------------------
100 // Function : MatrixFreeEpetraOperator::MatrixFreeEpetraOperator
101 // Purpose : Destructor
102 // Special Notes :
103 // Scope : public
104 // Creator : Todd Coffey, 1414
105 // Creation Date : 9/4/08
106 //-----------------------------------------------------------------------------
108 {
109 }
110 
111 //-----------------------------------------------------------------------------
112 // Function : MatrixFreeEpetraOperator::initialize
113 // Purpose : Initialization
114 // Special Notes :
115 // Scope : public
116 // Creator : Todd Coffey, 1414
117 // Creation Date : 9/4/08
118 //-----------------------------------------------------------------------------
120  RCP<NonLinearSolver> nonlinearSolver,
121  RCP<Linear::Vector> solVector,
122  RCP<Linear::Vector> rhsVector,
123  RCP<const Epetra_Map> solutionMap
124  )
125 {
126  nonlinearSolverRCPtr_ = nonlinearSolver;
127  solVectorRCPtr_ = solVector;
128  rhsVectorRCPtr_ = rhsVector;
129  solutionMap_ = solutionMap;
130  isInitialized_ = true;
131 }
132 
133 //-----------------------------------------------------------------------------
134 // Function : MatrixFreeEpetraOperator::SetUseTranspose
135 // Purpose : Define if transpose Apply and ApplyInverse is to be used.
136 // Special Notes :
137 // Scope : public
138 // Creator : Todd Coffey, 1414
139 // Creation Date : 9/4/08
140 //-----------------------------------------------------------------------------
142 {
143  // This is not supported for the HB load layers.
144  return -1;
145 }
146 
147 //-----------------------------------------------------------------------------
148 // Function : MatrixFreeEpetraOperator::Apply
149 // Purpose : Apply matrix free operator with Epetra_MultiVectors
150 // Special Notes :
151 // Scope : public
152 // Creator : Todd Coffey, 1414
153 // Creation Date : 9/4/08
154 //-----------------------------------------------------------------------------
156  const Epetra_MultiVector& X,
157  Epetra_MultiVector& Y
158  ) const
159 {
160  // Convert these to Linear::MultiVectors and call the other Apply
161 
162  // COPY the multi-vector data into new objects on the stack.
163  Epetra_MultiVector* Xcopy = new Epetra_MultiVector(X); // This gets deleted by the Linear::MultiVector below
164  Epetra_MultiVector* Ycopy = new Epetra_MultiVector(Y); // This gets deleted by the Linear::MultiVector below
165  Linear::MultiVector las_X(Xcopy, true); // this co-ops the Epetra_MultiVector and uses (and owns) its memory
166  Linear::MultiVector las_Y(Ycopy, true); // this co-ops the Epetra_MultiVector and uses (and owns) its memory
167  int status = Apply(las_X,las_Y);
168  // COPY the Ycopy data back into Y
169  Y = las_Y.epetraObj();
170  return(status);
171 }
172 
173 //-----------------------------------------------------------------------------
174 // Function : MatrixFreeEpetraOperator::Apply
175 // Purpose : Apply matrix free operator with Linear::MultiVectors
176 // Special Notes :
177 // Scope : public
178 // Creator : Todd Coffey, 1414
179 // Creation Date : 9/4/08
180 //-----------------------------------------------------------------------------
182  const Linear::MultiVector& X,
183  Linear::MultiVector& Y
184  ) const
185 {
186  if (!isInitialized_)
187  {
188  std::string msg = "MatrixFreeEpetraOperator::Apply: I'm not initialized!";
189  N_ERH_ErrorMgr::report(N_ERH_ErrorMgr::DEV_FATAL_0, msg);
190  }
191  bool status = true;
192  for (int i=0 ; i<X.numVectors() ; ++i)
193  {
194  const Linear::Vector x(X.epetraVector(i), true);
195  Linear::Vector y(Y.epetraVector(i), true);
196  bool localStatus = nonlinearSolverRCPtr_->applyJacobian(x,y);
197  status = status && localStatus;
198  }
199  if (status)
200  {
201  return 0;
202  }
203  else
204  {
205  return -1;
206  }
207 }
208 //-----------------------------------------------------------------------------
209 // Function : MatrixFreeEpetraOperator::ApplyInverse
210 // Purpose : Apply inverse of matrix free operator with Epetra_MultiVectors
211 // Special Notes :
212 // Scope : public
213 // Creator : Todd Coffey, 1414
214 // Creation Date : 9/4/08
215 //-----------------------------------------------------------------------------
217  const Epetra_MultiVector& X,
218  Epetra_MultiVector& Y
219  ) const
220 {
221  std::string msg = "MatrixFreeEpetraOperator::ApplyInverse is not supported!";
222  N_ERH_ErrorMgr::report(N_ERH_ErrorMgr::DEV_FATAL_0, msg);
223  return -1;
224 }
225 
226 //-----------------------------------------------------------------------------
227 // Function : MatrixFreeEpetraOperator::ApplyInverse
228 // Purpose : Apply inverse of matrix free operator with Linear::MultiVectors
229 // Special Notes :
230 // Scope : public
231 // Creator : Todd Coffey, 1414
232 // Creation Date : 9/4/08
233 //-----------------------------------------------------------------------------
235  const Linear::MultiVector& X,
236  Linear::MultiVector& Y
237  ) const
238 {
239  std::string msg = "MatrixFreeEpetraOperator::ApplyInverse is not supported!";
240  N_ERH_ErrorMgr::report(N_ERH_ErrorMgr::DEV_FATAL_0, msg);
241  return -1;
242 }
243 
244 //-----------------------------------------------------------------------------
245 // Function : MatrixFreeEpetraOperator::NormInf
246 // Purpose : Norm Inf of matrix
247 // Special Notes :
248 // Scope : public
249 // Creator : Todd Coffey, 1414
250 // Creation Date : 9/4/08
251 //-----------------------------------------------------------------------------
253 {
254  std::string msg = "MatrixFreeEpetraOperator::NormInf is not supported!";
255  N_ERH_ErrorMgr::report(N_ERH_ErrorMgr::DEV_FATAL_0, msg);
256  return -1.0;
257 }
258 
259 //-----------------------------------------------------------------------------
260 // Function : MatrixFreeEpetraOperator::Label
261 // Purpose : Label for operator
262 // Special Notes :
263 // Scope : public
264 // Creator : Todd Coffey, 1414
265 // Creation Date : 9/4/08
266 //-----------------------------------------------------------------------------
268 {
269  return "Matrix Free Harmonic Balance Epetra Operator";
270 }
271 
272 //-----------------------------------------------------------------------------
273 // Function : MatrixFreeEpetraOperator::UseTranspose
274 // Purpose : Query for useTranspose setting
275 // Special Notes :
276 // Scope : public
277 // Creator : Todd Coffey, 1414
278 // Creation Date : 9/4/08
279 //-----------------------------------------------------------------------------
281 {
282  // Use Transpose is not supported, so always return false.
283  return false;
284 }
285 
286 //-----------------------------------------------------------------------------
287 // Function : MatrixFreeEpetraOperator::HasNormInf
288 // Purpose : Query for normInf support
289 // Special Notes :
290 // Scope : public
291 // Creator : Todd Coffey, 1414
292 // Creation Date : 9/4/08
293 //-----------------------------------------------------------------------------
295 {
296  // Norm Inf is not supported, so always return false.
297  return false;
298 }
299 
300 //-----------------------------------------------------------------------------
301 // Function : MatrixFreeEpetraOperator::Comm
302 // Purpose : Return Epetra_Comm object
303 // Special Notes :
304 // Scope : public
305 // Creator : Todd Coffey, 1414
306 // Creation Date : 9/4/08
307 //-----------------------------------------------------------------------------
308 const Epetra_Comm & MatrixFreeEpetraOperator::Comm() const
309 {
310  if (!isInitialized_)
311  {
312  std::string msg = "MatrixFreeEpetraOperator::Comm: I'm not initialized!";
313  N_ERH_ErrorMgr::report(N_ERH_ErrorMgr::DEV_FATAL_0, msg);
314  }
315  return(rhsVectorRCPtr_->epetraObj().Comm());
316 }
317 
318 //-----------------------------------------------------------------------------
319 // Function : MatrixFreeEpetraOperator::OperatorDomainMap
320 // Purpose : Return Epetra_Map corresponding to domain of operator
321 // Special Notes :
322 // Scope : public
323 // Creator : Todd Coffey, 1414
324 // Creation Date : 9/4/08
325 //-----------------------------------------------------------------------------
327 {
328  if (!isInitialized_)
329  {
330  std::string msg = "MatrixFreeEpetraOperator::OperatorDomainMap: I'm not initialized!";
331  N_ERH_ErrorMgr::report(N_ERH_ErrorMgr::DEV_FATAL_0, msg);
332  }
333  return(*solutionMap_);
334 }
335 
336 //-----------------------------------------------------------------------------
337 // Function : MatrixFreeEpetraOperator::OperatorRangeMap
338 // Purpose : Return Epetra_Map corresponding to range of operator
339 // Special Notes :
340 // Scope : public
341 // Creator : Todd Coffey, 1414
342 // Creation Date : 9/4/08
343 //-----------------------------------------------------------------------------
345 {
346  if (!isInitialized_)
347  {
348  std::string msg = "MatrixFreeEpetraOperator::OperatorRangeMap: I'm not initialized!";
349  N_ERH_ErrorMgr::report(N_ERH_ErrorMgr::DEV_FATAL_0, msg);
350  }
351  const Epetra_Map* emap = dynamic_cast<const Epetra_Map*>(&rhsVectorRCPtr_->epetraObj().Map());
352  return(*solutionMap_);
353 }
354 
355 } // namespace Nonlinear
356 } // namespace Xyce
bool UseTranspose() const
Returns the current UseTranspose setting.
Pure virtual class to augment a linear system.
const char * Label() const
Returns a character string describing the operator.
int ApplyInverse(const Epetra_MultiVector &X, Epetra_MultiVector &Y) const
Returns the result of a Epetra_Operator inverse applied to an Epetra_MultiVector X in Y...
const Epetra_Map & OperatorDomainMap() const
Returns the Epetra_Map object associated with the domain of this operator.
const Epetra_Comm & Comm() const
Returns a pointer to the Epetra_Comm communicator associated with this operator.
int SetUseTranspose(bool UseTranspose)
If set true, transpose of this operator will be applied.
void initialize(RCP< NonLinearSolver > nonlinearSolver, RCP< Linear::Vector > solVector, RCP< Linear::Vector > rhsVector, RCP< const Epetra_Map > solutionMap)
bool HasNormInf() const
Returns true if the this object can provide an approximate Inf-norm, false otherwise.
double NormInf() const
Returns the infinity norm of the global matrix.
const Epetra_Map & OperatorRangeMap() const
Returns the Epetra_Map object associated with the range of this operator.
RCP< MatrixFreeEpetraOperator > matrixFreeEpetraOperator(RCP< NonLinearSolver > nonlinearSolver, RCP< Linear::Vector > solVector, RCP< Linear::Vector > rhsVector, RCP< const Epetra_Map > solutionMap)
int Apply(const Epetra_MultiVector &X, Epetra_MultiVector &Y) const
Returns the result of a Epetra_Operator applied to a Epetra_MultiVector X in Y.