Xyce  6.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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-2014 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.9 $
38 //
39 // Revision Date : $Date: 2014/08/07 23:08:54 $
40 //
41 // Current Owner : $Author: dgbaur $
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 RefCountPtr<MatrixFreeEpetraOperator> matrixFreeEpetraOperator(
69  RefCountPtr<NonLinearSolver> nonlinearSolver,
70  RefCountPtr<N_LAS_Vector> solVector,
71  RefCountPtr<N_LAS_Vector> rhsVector,
72  RefCountPtr<const Epetra_Map> solutionMap
73  )
74 {
75  RefCountPtr<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  RefCountPtr<NonLinearSolver> nonlinearSolver,
121  RefCountPtr<N_LAS_Vector> solVector,
122  RefCountPtr<N_LAS_Vector> rhsVector,
123  RefCountPtr<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 N_LAS_MultiVectors and call the other Apply
161 
162  // Cast away the const until the Apply which will enforce it.
163  // This is necessary because there is no const view function in N_LAS_MultiVector
164 // Epetra_MultiVector* Xptr = const_cast<Epetra_MultiVector*>(&X);
165 // N_LAS_MultiVector las_X(Xptr); // This is the wrong thing to do, when it goes out of scope, it deletes the Epetra_MultiVector Ptr.
166 // N_LAS_MultiVector las_Y(&Y);
167 
168  // COPY the multi-vector data into new objects on the stack.
169  Epetra_MultiVector* Xcopy = new Epetra_MultiVector(X); // This gets deleted by the N_LAS_MultiVector below
170  Epetra_MultiVector* Ycopy = new Epetra_MultiVector(Y); // This gets deleted by the N_LAS_MultiVector below
171  N_LAS_MultiVector las_X(Xcopy); // this co-ops the Epetra_MultiVector and uses (and owns) its memory
172  N_LAS_MultiVector las_Y(Ycopy); // this co-ops the Epetra_MultiVector and uses (and owns) its memory
173  int status = Apply(las_X,las_Y);
174  // COPY the Ycopy data back into Y
175  Y = las_Y.epetraObj();
176  return(status);
177 }
178 
179 //-----------------------------------------------------------------------------
180 // Function : MatrixFreeEpetraOperator::Apply
181 // Purpose : Apply matrix free operator with N_LAS_MultiVectors
182 // Special Notes :
183 // Scope : public
184 // Creator : Todd Coffey, 1414
185 // Creation Date : 9/4/08
186 //-----------------------------------------------------------------------------
188  const N_LAS_MultiVector& X,
189  N_LAS_MultiVector& Y
190  ) const
191 {
192  if (!isInitialized_)
193  {
194  std::string msg = "MatrixFreeEpetraOperator::Apply: I'm not initialized!";
195  N_ERH_ErrorMgr::report(N_ERH_ErrorMgr::DEV_FATAL_0, msg);
196  }
197  bool status = true;
198  for (int i=0 ; i<X.numVectors() ; ++i)
199  {
200  const N_LAS_Vector x(X.epetraVector(i));
201  N_LAS_Vector y(Y.epetraVector(i));
202  bool localStatus = nonlinearSolverRCPtr_->applyJacobian(x,y);
203  status = status && localStatus;
204  }
205  if (status)
206  {
207  return 0;
208  }
209  else
210  {
211  return -1;
212  }
213 }
214 //-----------------------------------------------------------------------------
215 // Function : MatrixFreeEpetraOperator::ApplyInverse
216 // Purpose : Apply inverse of matrix free operator with Epetra_MultiVectors
217 // Special Notes :
218 // Scope : public
219 // Creator : Todd Coffey, 1414
220 // Creation Date : 9/4/08
221 //-----------------------------------------------------------------------------
223  const Epetra_MultiVector& X,
224  Epetra_MultiVector& Y
225  ) const
226 {
227  std::string msg = "MatrixFreeEpetraOperator::ApplyInverse is not supported!";
228  N_ERH_ErrorMgr::report(N_ERH_ErrorMgr::DEV_FATAL_0, msg);
229  return -1;
230 }
231 
232 //-----------------------------------------------------------------------------
233 // Function : MatrixFreeEpetraOperator::ApplyInverse
234 // Purpose : Apply inverse of matrix free operator with N_LAS_MultiVectors
235 // Special Notes :
236 // Scope : public
237 // Creator : Todd Coffey, 1414
238 // Creation Date : 9/4/08
239 //-----------------------------------------------------------------------------
241  const N_LAS_MultiVector& X,
242  N_LAS_MultiVector& Y
243  ) const
244 {
245  std::string msg = "MatrixFreeEpetraOperator::ApplyInverse is not supported!";
246  N_ERH_ErrorMgr::report(N_ERH_ErrorMgr::DEV_FATAL_0, msg);
247  return -1;
248 }
249 
250 //-----------------------------------------------------------------------------
251 // Function : MatrixFreeEpetraOperator::NormInf
252 // Purpose : Norm Inf of matrix
253 // Special Notes :
254 // Scope : public
255 // Creator : Todd Coffey, 1414
256 // Creation Date : 9/4/08
257 //-----------------------------------------------------------------------------
259 {
260  std::string msg = "MatrixFreeEpetraOperator::NormInf is not supported!";
261  N_ERH_ErrorMgr::report(N_ERH_ErrorMgr::DEV_FATAL_0, msg);
262  return -1.0;
263 }
264 
265 //-----------------------------------------------------------------------------
266 // Function : MatrixFreeEpetraOperator::Label
267 // Purpose : Label for operator
268 // Special Notes :
269 // Scope : public
270 // Creator : Todd Coffey, 1414
271 // Creation Date : 9/4/08
272 //-----------------------------------------------------------------------------
274 {
275  return "Matrix Free Harmonic Balance Epetra Operator";
276 }
277 
278 //-----------------------------------------------------------------------------
279 // Function : MatrixFreeEpetraOperator::UseTranspose
280 // Purpose : Query for useTranspose setting
281 // Special Notes :
282 // Scope : public
283 // Creator : Todd Coffey, 1414
284 // Creation Date : 9/4/08
285 //-----------------------------------------------------------------------------
287 {
288  // Use Transpose is not supported, so always return false.
289  return false;
290 }
291 
292 //-----------------------------------------------------------------------------
293 // Function : MatrixFreeEpetraOperator::HasNormInf
294 // Purpose : Query for normInf support
295 // Special Notes :
296 // Scope : public
297 // Creator : Todd Coffey, 1414
298 // Creation Date : 9/4/08
299 //-----------------------------------------------------------------------------
301 {
302  // Norm Inf is not supported, so always return false.
303  return false;
304 }
305 
306 //-----------------------------------------------------------------------------
307 // Function : MatrixFreeEpetraOperator::Comm
308 // Purpose : Return Epetra_Comm object
309 // Special Notes :
310 // Scope : public
311 // Creator : Todd Coffey, 1414
312 // Creation Date : 9/4/08
313 //-----------------------------------------------------------------------------
314 const Epetra_Comm & MatrixFreeEpetraOperator::Comm() const
315 {
316  if (!isInitialized_)
317  {
318  std::string msg = "MatrixFreeEpetraOperator::Comm: I'm not initialized!";
319  N_ERH_ErrorMgr::report(N_ERH_ErrorMgr::DEV_FATAL_0, msg);
320  }
321  return(rhsVectorRCPtr_->epetraObj().Comm());
322 }
323 
324 //-----------------------------------------------------------------------------
325 // Function : MatrixFreeEpetraOperator::OperatorDomainMap
326 // Purpose : Return Epetra_Map corresponding to domain of operator
327 // Special Notes :
328 // Scope : public
329 // Creator : Todd Coffey, 1414
330 // Creation Date : 9/4/08
331 //-----------------------------------------------------------------------------
333 {
334  if (!isInitialized_)
335  {
336  std::string msg = "MatrixFreeEpetraOperator::OperatorDomainMap: I'm not initialized!";
337  N_ERH_ErrorMgr::report(N_ERH_ErrorMgr::DEV_FATAL_0, msg);
338  }
339  return(*solutionMap_);
340 }
341 
342 //-----------------------------------------------------------------------------
343 // Function : MatrixFreeEpetraOperator::OperatorRangeMap
344 // Purpose : Return Epetra_Map corresponding to range of operator
345 // Special Notes :
346 // Scope : public
347 // Creator : Todd Coffey, 1414
348 // Creation Date : 9/4/08
349 //-----------------------------------------------------------------------------
351 {
352  if (!isInitialized_)
353  {
354  std::string msg = "MatrixFreeEpetraOperator::OperatorRangeMap: I'm not initialized!";
355  N_ERH_ErrorMgr::report(N_ERH_ErrorMgr::DEV_FATAL_0, msg);
356  }
357  const Epetra_Map* emap = dynamic_cast<const Epetra_Map*>(&rhsVectorRCPtr_->epetraObj().Map());
358  return(*solutionMap_);
359 }
360 
361 } // namespace Nonlinear
362 } // namespace Xyce