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.8 $
38 //
39 // Revision Date : $Date: 2014/02/24 23:49:25 $
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 //-----------------------------------------------------------------------------
58 // Function : matrixFreeEpetraOperator
59 // Purpose : non-member constructor
60 // Special Notes :
61 // Scope : public
62 // Creator : Todd Coffey, 1414
63 // Creation Date : 9/4/08
64 //-----------------------------------------------------------------------------
65 RefCountPtr<N_NLS_MatrixFreeEpetraOperator> matrixFreeEpetraOperator(
66  RefCountPtr<N_NLS_NonLinearSolver> nonlinearSolver,
67  RefCountPtr<N_LAS_Vector> solVector,
68  RefCountPtr<N_LAS_Vector> rhsVector,
69  RefCountPtr<const Epetra_Map> solutionMap
70  )
71 {
72  RefCountPtr<N_NLS_MatrixFreeEpetraOperator> epetraOperator =
74  epetraOperator->initialize(nonlinearSolver,
75  solVector,
76  rhsVector,
77  solutionMap
78  );
79  return epetraOperator;
80 }
81 
82 
83 //-----------------------------------------------------------------------------
84 // Function : N_NLS_MatrixFreeEpetraOperator::N_NLS_MatrixFreeEpetraOperator
85 // Purpose : Constructor
86 // Special Notes :
87 // Scope : public
88 // Creator : Todd Coffey, 1414
89 // Creation Date : 9/4/08
90 //-----------------------------------------------------------------------------
92 {
93  isInitialized_ = false;
94 }
95 
96 //-----------------------------------------------------------------------------
97 // Function : N_NLS_MatrixFreeEpetraOperator::N_NLS_MatrixFreeEpetraOperator
98 // Purpose : Destructor
99 // Special Notes :
100 // Scope : public
101 // Creator : Todd Coffey, 1414
102 // Creation Date : 9/4/08
103 //-----------------------------------------------------------------------------
105 {
106 }
107 
108 //-----------------------------------------------------------------------------
109 // Function : N_NLS_MatrixFreeEpetraOperator::initialize
110 // Purpose : Initialization
111 // Special Notes :
112 // Scope : public
113 // Creator : Todd Coffey, 1414
114 // Creation Date : 9/4/08
115 //-----------------------------------------------------------------------------
117  RefCountPtr<N_NLS_NonLinearSolver> nonlinearSolver,
118  RefCountPtr<N_LAS_Vector> solVector,
119  RefCountPtr<N_LAS_Vector> rhsVector,
120  RefCountPtr<const Epetra_Map> solutionMap
121  )
122 {
123  nonlinearSolverRCPtr_ = nonlinearSolver;
124  solVectorRCPtr_ = solVector;
125  rhsVectorRCPtr_ = rhsVector;
126  solutionMap_ = solutionMap;
127  isInitialized_ = true;
128 }
129 
130 //-----------------------------------------------------------------------------
131 // Function : N_NLS_MatrixFreeEpetraOperator::SetUseTranspose
132 // Purpose : Define if transpose Apply and ApplyInverse is to be used.
133 // Special Notes :
134 // Scope : public
135 // Creator : Todd Coffey, 1414
136 // Creation Date : 9/4/08
137 //-----------------------------------------------------------------------------
139 {
140  // This is not supported for the HB load layers.
141  return -1;
142 }
143 
144 //-----------------------------------------------------------------------------
145 // Function : N_NLS_MatrixFreeEpetraOperator::Apply
146 // Purpose : Apply matrix free operator with Epetra_MultiVectors
147 // Special Notes :
148 // Scope : public
149 // Creator : Todd Coffey, 1414
150 // Creation Date : 9/4/08
151 //-----------------------------------------------------------------------------
153  const Epetra_MultiVector& X,
154  Epetra_MultiVector& Y
155  ) const
156 {
157  // Convert these to N_LAS_MultiVectors and call the other Apply
158 
159  // Cast away the const until the Apply which will enforce it.
160  // This is necessary because there is no const view function in N_LAS_MultiVector
161 // Epetra_MultiVector* Xptr = const_cast<Epetra_MultiVector*>(&X);
162 // 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.
163 // N_LAS_MultiVector las_Y(&Y);
164 
165  // COPY the multi-vector data into new objects on the stack.
166  Epetra_MultiVector* Xcopy = new Epetra_MultiVector(X); // This gets deleted by the N_LAS_MultiVector below
167  Epetra_MultiVector* Ycopy = new Epetra_MultiVector(Y); // This gets deleted by the N_LAS_MultiVector below
168  N_LAS_MultiVector las_X(Xcopy); // this co-ops the Epetra_MultiVector and uses (and owns) its memory
169  N_LAS_MultiVector las_Y(Ycopy); // this co-ops the Epetra_MultiVector and uses (and owns) its memory
170  int status = Apply(las_X,las_Y);
171  // COPY the Ycopy data back into Y
172  Y = las_Y.epetraObj();
173  return(status);
174 }
175 
176 //-----------------------------------------------------------------------------
177 // Function : N_NLS_MatrixFreeEpetraOperator::Apply
178 // Purpose : Apply matrix free operator with N_LAS_MultiVectors
179 // Special Notes :
180 // Scope : public
181 // Creator : Todd Coffey, 1414
182 // Creation Date : 9/4/08
183 //-----------------------------------------------------------------------------
185  const N_LAS_MultiVector& X,
186  N_LAS_MultiVector& Y
187  ) const
188 {
189  if (!isInitialized_)
190  {
191  std::string msg = "N_NLS_MatrixFreeEpetraOperator::Apply: I'm not initialized!";
192  N_ERH_ErrorMgr::report(N_ERH_ErrorMgr::DEV_FATAL_0, msg);
193  }
194  bool status = true;
195  for (int i=0 ; i<X.numVectors() ; ++i)
196  {
197  const N_LAS_Vector x(X.epetraVector(i));
198  N_LAS_Vector y(Y.epetraVector(i));
199  bool localStatus = nonlinearSolverRCPtr_->applyJacobian(x,y);
200  status = status && localStatus;
201  }
202  if (status)
203  {
204  return 0;
205  }
206  else
207  {
208  return -1;
209  }
210 }
211 //-----------------------------------------------------------------------------
212 // Function : N_NLS_MatrixFreeEpetraOperator::ApplyInverse
213 // Purpose : Apply inverse of matrix free operator with Epetra_MultiVectors
214 // Special Notes :
215 // Scope : public
216 // Creator : Todd Coffey, 1414
217 // Creation Date : 9/4/08
218 //-----------------------------------------------------------------------------
220  const Epetra_MultiVector& X,
221  Epetra_MultiVector& Y
222  ) const
223 {
224  std::string msg = "N_NLS_MatrixFreeEpetraOperator::ApplyInverse is not supported!";
225  N_ERH_ErrorMgr::report(N_ERH_ErrorMgr::DEV_FATAL_0, msg);
226  return -1;
227 }
228 
229 //-----------------------------------------------------------------------------
230 // Function : N_NLS_MatrixFreeEpetraOperator::ApplyInverse
231 // Purpose : Apply inverse of matrix free operator with N_LAS_MultiVectors
232 // Special Notes :
233 // Scope : public
234 // Creator : Todd Coffey, 1414
235 // Creation Date : 9/4/08
236 //-----------------------------------------------------------------------------
238  const N_LAS_MultiVector& X,
239  N_LAS_MultiVector& Y
240  ) const
241 {
242  std::string msg = "N_NLS_MatrixFreeEpetraOperator::ApplyInverse is not supported!";
243  N_ERH_ErrorMgr::report(N_ERH_ErrorMgr::DEV_FATAL_0, msg);
244  return -1;
245 }
246 
247 //-----------------------------------------------------------------------------
248 // Function : N_NLS_MatrixFreeEpetraOperator::NormInf
249 // Purpose : Norm Inf of matrix
250 // Special Notes :
251 // Scope : public
252 // Creator : Todd Coffey, 1414
253 // Creation Date : 9/4/08
254 //-----------------------------------------------------------------------------
256 {
257  std::string msg = "N_NLS_MatrixFreeEpetraOperator::NormInf is not supported!";
258  N_ERH_ErrorMgr::report(N_ERH_ErrorMgr::DEV_FATAL_0, msg);
259  return -1.0;
260 }
261 
262 //-----------------------------------------------------------------------------
263 // Function : N_NLS_MatrixFreeEpetraOperator::Label
264 // Purpose : Label for operator
265 // Special Notes :
266 // Scope : public
267 // Creator : Todd Coffey, 1414
268 // Creation Date : 9/4/08
269 //-----------------------------------------------------------------------------
271 {
272  return "Matrix Free Harmonic Balance Epetra Operator";
273 }
274 
275 //-----------------------------------------------------------------------------
276 // Function : N_NLS_MatrixFreeEpetraOperator::UseTranspose
277 // Purpose : Query for useTranspose setting
278 // Special Notes :
279 // Scope : public
280 // Creator : Todd Coffey, 1414
281 // Creation Date : 9/4/08
282 //-----------------------------------------------------------------------------
284 {
285  // Use Transpose is not supported, so always return false.
286  return false;
287 }
288 
289 //-----------------------------------------------------------------------------
290 // Function : N_NLS_MatrixFreeEpetraOperator::HasNormInf
291 // Purpose : Query for normInf support
292 // Special Notes :
293 // Scope : public
294 // Creator : Todd Coffey, 1414
295 // Creation Date : 9/4/08
296 //-----------------------------------------------------------------------------
298 {
299  // Norm Inf is not supported, so always return false.
300  return false;
301 }
302 
303 //-----------------------------------------------------------------------------
304 // Function : N_NLS_MatrixFreeEpetraOperator::Comm
305 // Purpose : Return Epetra_Comm object
306 // Special Notes :
307 // Scope : public
308 // Creator : Todd Coffey, 1414
309 // Creation Date : 9/4/08
310 //-----------------------------------------------------------------------------
311 const Epetra_Comm & N_NLS_MatrixFreeEpetraOperator::Comm() const
312 {
313  if (!isInitialized_)
314  {
315  std::string msg = "N_NLS_MatrixFreeEpetraOperator::Comm: I'm not initialized!";
316  N_ERH_ErrorMgr::report(N_ERH_ErrorMgr::DEV_FATAL_0, msg);
317  }
318  return(rhsVectorRCPtr_->epetraObj().Comm());
319 }
320 
321 //-----------------------------------------------------------------------------
322 // Function : N_NLS_MatrixFreeEpetraOperator::OperatorDomainMap
323 // Purpose : Return Epetra_Map corresponding to domain of operator
324 // Special Notes :
325 // Scope : public
326 // Creator : Todd Coffey, 1414
327 // Creation Date : 9/4/08
328 //-----------------------------------------------------------------------------
330 {
331  if (!isInitialized_)
332  {
333  std::string msg = "N_NLS_MatrixFreeEpetraOperator::OperatorDomainMap: I'm not initialized!";
334  N_ERH_ErrorMgr::report(N_ERH_ErrorMgr::DEV_FATAL_0, msg);
335  }
336  return(*solutionMap_);
337 }
338 
339 //-----------------------------------------------------------------------------
340 // Function : N_NLS_MatrixFreeEpetraOperator::OperatorRangeMap
341 // Purpose : Return Epetra_Map corresponding to range of operator
342 // Special Notes :
343 // Scope : public
344 // Creator : Todd Coffey, 1414
345 // Creation Date : 9/4/08
346 //-----------------------------------------------------------------------------
348 {
349  if (!isInitialized_)
350  {
351  std::string msg = "N_NLS_MatrixFreeEpetraOperator::OperatorRangeMap: I'm not initialized!";
352  N_ERH_ErrorMgr::report(N_ERH_ErrorMgr::DEV_FATAL_0, msg);
353  }
354  const Epetra_Map* emap = dynamic_cast<const Epetra_Map*>(&rhsVectorRCPtr_->epetraObj().Map());
355  return(*solutionMap_);
356 }
357