ArpackSolver.cxx
1 // Copyright (C) 2010 Lin Wu
2 //
3 // This file is part of the linear-algebra library Seldon,
4 // http://seldon.sourceforge.net/.
5 //
6 // Seldon is free software; you can redistribute it and/or modify it under the
7 // terms of the GNU Lesser General Public License as published by the Free
8 // Software Foundation; either version 2.1 of the License, or (at your option)
9 // any later version.
10 //
11 // Seldon is distributed in the hope that it will be useful, but WITHOUT ANY
12 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
14 // more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with Seldon. If not, see http://www.gnu.org/licenses/.
18 
19 
20 #ifndef SELDON_FILE_ARPACKSOLVER_CXX
21 
22 #include "ArpackSolver.hxx"
23 
24 #ifndef SELDON_WITH_COMPILED_LIBRARY
25 ArpackLog debug_;
26 #endif
27 
28 namespace Seldon
29 {
30 
32  template<class T, class Y>
34  {
35  }
36 
37 
39  template<class T, class Y>
41  {
42  Clear();
43  }
44 
45 
47 
63  template<class T, class Y>
65  ::Init(int n, int nev, int ncv, int maxit, T tol,
66  string solver_type, int mode, string which,
67  char bmat, char HowMny, bool with_arpack_verbose)
68  {
69  n_ = n;
70  nev_ = nev;
71  ncv_ = ncv;
72  maxit_ = maxit;
73  tol_ = tol;
74  solver_type_ = solver_type;
75  mode_ = mode;
76  which_ = which;
77  bmat_ = bmat;
78  HowMny_ = HowMny;
79 
80  // Initializations.
81  info_ = 0;
82  ido_ = 0;
83 
84  // Algorithm mode.
85  ishfts_ = 1;
86 
87  iparam_[0] = ishfts_;
88  iparam_[2] = maxit_;
89  iparam_[6] = mode_;
90 
91  // Dimensions.
92  ldv_ = n_;
93  lworkl_ = ncv_ * (ncv_ + 8);
94 
95  // Check parameters.
96  CheckParameter();
97 
98  // Allocations.
99  Allocate();
100 
101  // Verbose.
102  if (with_arpack_verbose)
103  SetArpackVerbose();
104  }
105 
106 
108  template<class T, class Y>
110  {
111  if (solver_type_ != "symmetric" &&
112  solver_type_ != "non-symmetric" &&
113  solver_type_ != "complex-single" &&
114  solver_type_ != "complex-double")
115  throw Error("ArpackSolver::Continue",
116  "Unsupported solver type \"" + solver_type_ + "\".");
117  }
118 
119 
121  template<class T, class Y>
123  {
124  n_ = 0;
125  lworkl_ = 0;
126  ldv_ = 0;
127 
128  Deallocate();
129  }
130 
131 
133  template<class T, class Y>
135  {
136  v_ = new Y[ldv_ * ncv_];
137  workl_ = new Y[ncv_ * (ncv_ + 8)];
138  workd_ = new Y[3 * n_];
139 
140  eig_val_ = new Y[ncv_];
141  resid_ = new Y[n_];
142 
143  pselect_ = new int[ncv_];
144  }
145 
146 
148  template<class T, class Y>
150  {
151  if (v_) delete[] v_;
152  if (workl_) delete[] workl_;
153  if (workd_) delete[] workd_;
154 
155  if (eig_val_) delete[] eig_val_;
156  if (resid_) delete[] resid_;
157 
158  if (pselect_) delete[] pselect_;
159  }
160 
161 
163  template<class T, class Y>
165  {
166  SetArpackVerbose(solver_type_);
167  }
168 
169 
171  template<class T, class Y>
172  void ArpackSolver<T, Y>::SetArpackVerbose(string solver_type)
173  {
174  if (solver_type == "symmetric")
175  {
176  debug_.logfil = 6;
177  debug_.ndigit = -3;
178  debug_.msgets = 0;
179  debug_.msaitr = 0;
180  debug_.msapps = 0;
181  debug_.msaupd = 1;
182  debug_.msaup2 = 0;
183  debug_.mseigt = 0;
184  debug_.mseupd = 0;
185  }
186  else if (solver_type == "non-symmetric")
187  {
188  debug_.logfil = 6;
189  debug_.ndigit = -3;
190  debug_.mgetv0 = 0;
191  debug_.mnaupd = 1;
192  debug_.mnaup2 = 0;
193  debug_.mnaitr = 0;
194  debug_.mneigt = 0;
195  debug_.mnapps = 0;
196  debug_.mngets = 0;
197  debug_.mneupd = 0;
198  }
199  else if (solver_type == "complex-single" ||
200  solver_type == "complex-double")
201  {
202  debug_.logfil = 6;
203  debug_.ndigit = -3;
204  debug_.mgetv0 = 0;
205  debug_.mcaupd = 1;
206  debug_.mcaup2 = 0;
207  debug_.mcaitr = 0;
208  debug_.mceigt = 0;
209  debug_.mcapps = 0;
210  debug_.mcgets = 0;
211  debug_.mceupd = 0;
212  }
213  }
214 
215 
217  template<class T, class Y>
219  {
220  debug_.logfil = 6;
221  debug_.ndigit = 0;
222  debug_.mgetv0 = 0;
223  debug_.msaupd = 0;
224  debug_.msaup2 = 0;
225  debug_.msaitr = 0;
226  debug_.mseigt = 0;
227  debug_.msapps = 0;
228  debug_.msgets = 0;
229  debug_.mseupd = 0;
230  debug_.mnaupd = 0;
231  debug_.mnaup2 = 0;
232  debug_.mnaitr = 0;
233  debug_.mneigt = 0;
234  debug_.mnapps = 0;
235  debug_.mngets = 0;
236  debug_.mneupd = 0;
237  debug_.mcaupd = 0;
238  debug_.mcaup2 = 0;
239  debug_.mcaitr = 0;
240  debug_.mceigt = 0;
241  debug_.mcapps = 0;
242  debug_.mcgets = 0;
243  debug_.mceupd = 0;
244  }
245 
246 
248  template<class T, class Y>
250  {
251  return workd_ + ipntr_[0] - 1;
252  }
253 
254 
256  template<class T, class Y>
258  {
259  return workd_ + ipntr_[1] - 1;
260  }
261 
262 
264 
267  template<class T, class Y>
269  {
270  return v_ + index * ldv_;
271  }
272 
273 
275 
278  template<class T, class Y>
280  {
281  return eig_val_[index];
282  }
283 
284 
286  template<class T, class Y>
288  {
289  return ido_;
290  }
291 
292 
294 
297  template<class T, class Y>
299  {
300  ido_ = ido;
301  }
302 
303 
305  template<class T, class Y>
307  {
308  return info_;
309  }
310 
311 
313 
316  template<class T, class Y>
318  {
319  info_ = info;
320  }
321 
322 
324  template<class T, class Y>
326  {
327  return nconv_;
328  }
329 
330 
332  template<class T, class Y>
334  {
335 #ifdef SELDON_WITH_MPI
336  int comm(0);
337  if (solver_type_ == "symmetric")
338  saupd(comm, ido_, bmat_, n_, (char *)which_.c_str(), nev_, tol_, resid_, ncv_,
339  v_, ldv_, iparam_, ipntr_, workd_, workl_, lworkl_, info_);
340 #else
341  if (solver_type_ == "symmetric")
342  saupd(ido_, bmat_, n_, (char *)which_.c_str(), nev_, tol_, resid_, ncv_,
343  v_, ldv_, iparam_, ipntr_, workd_, workl_, lworkl_, info_);
344 #endif
345 
346  return (ido_ != 99);
347  }
348 
349 
351  template<class T, class Y>
353  {
354  bool i_success = (info_ >= 0);
355 
356  if (i_success)
357  {
358  rvec_ = true;
359 
360 #ifdef SELDON_WITH_MPI
361  int comm(0);
362  seupd(comm, int(rvec_), 'A', pselect_, eig_val_, v_, ldv_,
363  sigma_, bmat_, n_, (char *) which_.c_str(), nev_, tol_,
364  resid_, ncv_, v_, ldv_, iparam_, ipntr_, workd_, workl_,
365  lworkl_, ierr_);
366 #else
367  seupd(int(rvec_), 'A', pselect_, eig_val_, v_, ldv_,
368  sigma_, bmat_, n_, (char *) which_.c_str(), nev_, tol_,
369  resid_, ncv_, v_, ldv_, iparam_, ipntr_, workd_, workl_,
370  lworkl_, ierr_);
371 #endif
372  }
373 
374  bool p_success = (ierr_ == 0);
375 
376  if (p_success)
377  nconv_ = iparam_[4];
378 
379  return (i_success && p_success);
380  }
381 
382 } // namespace Seldon.
383 
384 
385 #define SELDON_FILE_ARPACKSOLVER_CXX
386 #endif
Seldon::ArpackSolver::GetSecondWorkVector
Y * GetSecondWorkVector()
Gets the address of the second vector in the ARPACK working array.
Definition: ArpackSolver.cxx:257
Seldon::ArpackSolver::Deallocate
void Deallocate()
Deallocates arrays.
Definition: ArpackSolver.cxx:149
Seldon::ArpackSolver::SetReverseCommunicationFlag
void SetReverseCommunicationFlag(int ido)
Sets the reverse communication flag.
Definition: ArpackSolver.cxx:298
Seldon::ArpackSolver::CheckParameter
void CheckParameter()
Check parameters.
Definition: ArpackSolver.cxx:109
Seldon::seupd
void seupd(ARPACK_LOGICAL rvec, char HowMny, ARPACK_LOGICAL *select, ARPACK_DOUBLEREAL *d, ARPACK_DOUBLEREAL *Z, ARPACK_INTEGER ldz, ARPACK_DOUBLEREAL sigma, char bmat, ARPACK_INTEGER n, char *which, ARPACK_INTEGER nev, ARPACK_DOUBLEREAL tol, ARPACK_DOUBLEREAL *resid, ARPACK_INTEGER ncv, ARPACK_DOUBLEREAL *V, ARPACK_INTEGER ldv, ARPACK_INTEGER *iparam, ARPACK_INTEGER *ipntr, ARPACK_DOUBLEREAL *workd, ARPACK_DOUBLEREAL *workl, ARPACK_INTEGER lworkl, ARPACK_INTEGER &info)
Postprocessing for symmetric problems in double precision.
Definition: ArpackInline.cxx:161
Seldon::ArpackSolver::~ArpackSolver
~ArpackSolver()
Destructor.
Definition: ArpackSolver.cxx:40
Seldon::ArpackSolver::GetReverseCommunicationFlag
int GetReverseCommunicationFlag()
Gets the reverse communication flag.
Definition: ArpackSolver.cxx:287
Seldon::ArpackSolver::Finish
bool Finish()
Post-processing.
Definition: ArpackSolver.cxx:352
Seldon::Error
Definition: Errors.hxx:38
Seldon::ArpackSolver::Continue
bool Continue()
Calls ARPACK computation routine.
Definition: ArpackSolver.cxx:333
Seldon::ArpackSolver::GetInfoFlag
int GetInfoFlag()
Gets the info flag.
Definition: ArpackSolver.cxx:306
Seldon::ArpackSolver::GetConvergedNumber
int GetConvergedNumber()
Gets the number of "converged" Ritz values.
Definition: ArpackSolver.cxx:325
Seldon::saupd
void saupd(ARPACK_INTEGER &ido, char bmat, ARPACK_INTEGER n, char *which, ARPACK_INTEGER nev, ARPACK_DOUBLEREAL &tol, ARPACK_DOUBLEREAL *resid, ARPACK_INTEGER ncv, ARPACK_DOUBLEREAL *V, ARPACK_INTEGER ldv, ARPACK_INTEGER *iparam, ARPACK_INTEGER *ipntr, ARPACK_DOUBLEREAL *workd, ARPACK_DOUBLEREAL *workl, ARPACK_INTEGER lworkl, ARPACK_INTEGER &info)
Symmetric problems in double precision.
Definition: ArpackInline.cxx:136
Seldon::ArpackSolver::GetEigenValue
Y GetEigenValue(int index)
Gets one eigenvalue.
Definition: ArpackSolver.cxx:279
Seldon::ArpackSolver::SetArpackVerbose
void SetArpackVerbose()
tells to Arpack to display debug information
Definition: ArpackSolver.cxx:164
Seldon::ArpackSolver::Init
void Init(int n, int nev, int ncv, int maxit, T tol, string solver_type, int mode, string which, char bmat, char HowMny, bool with_arpack_verbose=false)
Initializations.
Definition: ArpackSolver.cxx:65
ArpackLog
Definition: carpack.h:16
Seldon::ArpackSolver::ClearArpackVerbose
void ClearArpackVerbose()
No ARPACK info is to be displayed.
Definition: ArpackSolver.cxx:218
Seldon::ArpackSolver::ArpackSolver
ArpackSolver()
Constructor.
Definition: ArpackSolver.cxx:33
Seldon::ArpackSolver::SetInfoFlag
void SetInfoFlag(int info)
Sets the info flag.
Definition: ArpackSolver.cxx:317
Seldon::ArpackSolver::GetEigenVector
Y * GetEigenVector(int index)
Gets one eigenvector.
Definition: ArpackSolver.cxx:268
Seldon::ArpackSolver::GetFirstWorkVector
Y * GetFirstWorkVector()
Gets the address of the first vector in the ARPACK working array.
Definition: ArpackSolver.cxx:249
Seldon
Seldon namespace.
Definition: Array.cxx:24
Seldon::ArpackSolver::Clear
void Clear()
Clear memories.
Definition: ArpackSolver.cxx:122
Seldon::ArpackSolver::Allocate
void Allocate()
Allocates arrays.
Definition: ArpackSolver.cxx:134