complex_matrices.h
Go to the documentation of this file.
1 // LIC// ====================================================================
2 // LIC// This file forms part of oomph-lib, the object-oriented,
3 // LIC// multi-physics finite-element library, available
4 // LIC// at http://www.oomph-lib.org.
5 // LIC//
6 // LIC// Copyright (C) 2006-2022 Matthias Heil and Andrew Hazel
7 // LIC//
8 // LIC// This library is free software; you can redistribute it and/or
9 // LIC// modify it under the terms of the GNU Lesser General Public
10 // LIC// License as published by the Free Software Foundation; either
11 // LIC// version 2.1 of the License, or (at your option) any later version.
12 // LIC//
13 // LIC// This library is distributed in the hope that it will be useful,
14 // LIC// but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // LIC// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // LIC// Lesser General Public License for more details.
17 // LIC//
18 // LIC// You should have received a copy of the GNU Lesser General Public
19 // LIC// License along with this library; if not, write to the Free Software
20 // LIC// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 // LIC// 02110-1301 USA.
22 // LIC//
23 // LIC// The authors may be contacted at oomph-lib@maths.man.ac.uk.
24 // LIC//
25 // LIC//====================================================================
26 // This header file contains classes and inline function definitions for
27 // matrices of complex numbers and their derived types
28 
29 // Include guards to prevent multiple inclusion of the header
30 #ifndef OOMPH_COMPLEX_MATRICES_HEADER
31 #define OOMPH_COMPLEX_MATRICES_HEADER
32 
33 // Config header generated by autoconfig
34 #ifdef HAVE_CONFIG_H
35 #include <oomph-lib-config.h>
36 #endif
37 
38 #ifdef OOMPH_HAS_MPI
39 #include "mpi.h"
40 #endif
41 
42 // Of course we need the complex type
43 #include <complex>
44 
45 // Also need the standard matrices header
46 #include "matrices.h"
47 
48 namespace oomph
49 {
50  //===================================================================
54  //===================================================================
56  {
57  public:
60 
63 
65  void operator=(const ComplexMatrixBase&) = delete;
66 
68  virtual unsigned long nrow() const = 0;
69 
71  virtual unsigned long ncol() const = 0;
72 
74  virtual ~ComplexMatrixBase() {}
75 
79  virtual std::complex<double> operator()(const unsigned long& i,
80  const unsigned long& j) const = 0;
81 
84  virtual int ludecompose()
85  {
86  throw OomphLibError(
87  "ludecompose() has not been written for this matrix class\n",
90 
92  return 1;
93  }
94 
97  virtual void lubksub(Vector<std::complex<double>>& rhs)
98  {
99  throw OomphLibError(
100  "lubksub() has not been written for this matrix class\n",
103  }
104 
105 
109  virtual void solve(Vector<std::complex<double>>& rhs);
110 
113  virtual void solve(const Vector<std::complex<double>>& rhs,
114  Vector<std::complex<double>>& soln);
115 
117  virtual void residual(const Vector<std::complex<double>>& x,
118  const Vector<std::complex<double>>& b,
119  Vector<std::complex<double>>& residual) = 0;
120 
124  virtual double max_residual(const Vector<std::complex<double>>& x,
125  const Vector<std::complex<double>>& rhs)
126  {
127  unsigned long n = rhs.size();
129  residual(x, rhs, res);
130  double ans = 0.0;
131  for (unsigned long i = 0; i < n; i++)
132  {
133  ans = std::max(ans, std::abs(res[i]));
134  }
135  return ans;
136  }
137 
139  virtual void multiply(const Vector<std::complex<double>>& x,
140  Vector<std::complex<double>>& soln) = 0;
141 
143  virtual void multiply_transpose(const Vector<std::complex<double>>& x,
144  Vector<std::complex<double>>& soln) = 0;
145  };
146 
147 
148  //=================================================================
152  //=================================================================
154  public DenseMatrix<std::complex<double>>
155  {
156  private:
159 
161  std::complex<double>* LU_factors;
162 
166 
169  void delete_lu_factors();
170 
171  public:
174  : DenseMatrix<std::complex<double>>(),
175  Index(0),
176  LU_factors(0),
178  {
179  }
180 
182  DenseComplexMatrix(const unsigned long& n)
183  : DenseMatrix<std::complex<double>>(n),
184  Index(0),
185  LU_factors(0),
187  {
188  }
189 
191  DenseComplexMatrix(const unsigned long& n, const unsigned long& m)
192  : DenseMatrix<std::complex<double>>(n, m),
193  Index(0),
194  LU_factors(0),
196  {
197  }
198 
201  DenseComplexMatrix(const unsigned long& n,
202  const unsigned long& m,
203  const std::complex<double>& initial_val)
204  : DenseMatrix<std::complex<double>>(n, m, initial_val),
205  Index(0),
206  LU_factors(0),
208  {
209  }
210 
211 
214 
216  void operator=(const DenseComplexMatrix&) = delete;
217 
219  inline unsigned long nrow() const
220  {
222  }
223 
225  inline unsigned long ncol() const
226  {
228  }
229 
232  inline std::complex<double> operator()(const unsigned long& i,
233  const unsigned long& j) const
234  {
236  }
237 
240  inline std::complex<double>& operator()(const unsigned long& i,
241  const unsigned long& j)
242  {
244  }
245 
247  virtual ~DenseComplexMatrix();
248 
252  int ludecompose();
253 
256  void lubksub(Vector<std::complex<double>>& rhs);
257 
260  void residual(const Vector<std::complex<double>>& x,
261  const Vector<std::complex<double>>& rhs,
262  Vector<std::complex<double>>& residual);
263 
265  void multiply(const Vector<std::complex<double>>& x,
266  Vector<std::complex<double>>& soln);
267 
269  void multiply_transpose(const Vector<std::complex<double>>& x,
270  Vector<std::complex<double>>& soln);
271  };
272 
273 
274  //=================================================================
276  //=================================================================
277  class CRComplexMatrix : public CRMatrix<std::complex<double>>,
278  public ComplexMatrixBase
279  {
280  private:
282  void* F_factors;
283 
285  int Info;
286 
287  public:
290  {
291  // By default SuperLU solves linear systems quietly
292  Doc_stats_during_solve = false;
293  }
294 
298  CRComplexMatrix(const Vector<std::complex<double>>& value,
299  const Vector<int>& column_index,
300  const Vector<int>& row_start,
301  const unsigned long& n,
302  const unsigned long& m)
304  F_factors(0),
305  Info(0)
306  {
307  // By default SuperLU solves linear systems quietly
308  Doc_stats_during_solve = false;
309  }
310 
311 
314 
316  void operator=(const CRComplexMatrix&) = delete;
317 
320  {
321  clean_up_memory();
322  }
323 
327  {
328  Doc_stats_during_solve = true;
329  }
330 
331  // Set flag to indicate that stats are not to be displayed during
334  {
335  Doc_stats_during_solve = false;
336  }
337 
339  inline unsigned long nrow() const
340  {
342  }
343 
345  inline unsigned long ncol() const
346  {
348  }
349 
351  inline std::complex<double> operator()(const unsigned long& i,
352  const unsigned long& j) const
353  {
355  }
356 
358  int ludecompose();
359 
361  void lubksub(Vector<std::complex<double>>& rhs);
362 
364  void clean_up_memory();
365 
367  void residual(const Vector<std::complex<double>>& x,
368  const Vector<std::complex<double>>& b,
369  Vector<std::complex<double>>& residual);
370 
372  void multiply(const Vector<std::complex<double>>& x,
373  Vector<std::complex<double>>& soln);
374 
375 
377  void multiply_transpose(const Vector<std::complex<double>>& x,
378  Vector<std::complex<double>>& soln);
379 
380  protected:
384  };
385 
386 
387  //=================================================================
389  //=================================================================
391  public CCMatrix<std::complex<double>>
392  {
393  private:
395  void* F_factors;
396 
398  int Info;
399 
400  protected:
404 
405  public:
408  {
409  // By default SuperLU solves linear systems quietly
410  Doc_stats_during_solve = false;
411  }
412 
418  CCComplexMatrix(const Vector<std::complex<double>>& value,
419  const Vector<int>& row_index,
420  const Vector<int>& column_start,
421  const unsigned long& n,
422  const unsigned long& m)
424  F_factors(0),
425  Info(0)
426  {
427  // By default SuperLU solves linear systems quietly
428  Doc_stats_during_solve = false;
429  }
430 
433 
435  void operator=(const CCComplexMatrix&) = delete;
436 
439  {
440  clean_up_memory();
441  }
442 
446  {
447  Doc_stats_during_solve = true;
448  }
449 
450  // Set flag to indicate that stats are not to be displayed during
453  {
454  Doc_stats_during_solve = false;
455  }
456 
458  inline unsigned long nrow() const
459  {
461  }
462 
464  inline unsigned long ncol() const
465  {
467  }
468 
471  inline std::complex<double> operator()(const unsigned long& i,
472  const unsigned long& j) const
473  {
475  }
476 
478  int ludecompose();
479 
481  void lubksub(Vector<std::complex<double>>& rhs);
482 
484  void clean_up_memory();
485 
487  void residual(const Vector<std::complex<double>>& x,
488  const Vector<std::complex<double>>& b,
489  Vector<std::complex<double>>& residual);
490 
491 
493  void multiply(const Vector<std::complex<double>>& x,
494  Vector<std::complex<double>>& soln);
495 
496 
498  void multiply_transpose(const Vector<std::complex<double>>& x,
499  Vector<std::complex<double>>& soln);
500  };
501 } // namespace oomph
502 
503 #endif
AnnoyingScalar abs(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:135
int i
Definition: BiCGSTAB_step_by_step.cpp:9
const unsigned n
Definition: CG3DPackingUnitTest.cpp:11
cout<< "Here is the matrix m:"<< endl<< m<< endl;Matrix< ptrdiff_t, 3, 1 > res
Definition: PartialRedux_count.cpp:3
Scalar * b
Definition: benchVecAdd.cpp:17
A class for compressed column matrices that store doubles.
Definition: complex_matrices.h:392
virtual ~CCComplexMatrix()
Destructor: Kill the LU factors if they have been setup.
Definition: complex_matrices.h:438
std::complex< double > operator()(const unsigned long &i, const unsigned long &j) const
Definition: complex_matrices.h:471
void multiply(const Vector< std::complex< double >> &x, Vector< std::complex< double >> &soln)
Multiply the matrix by the vector x: soln=Ax.
Definition: complex_matrices.cc:854
int Info
Info flag for the SuperLU solver.
Definition: complex_matrices.h:398
void lubksub(Vector< std::complex< double >> &rhs)
LU back solve for given RHS.
Definition: complex_matrices.cc:668
unsigned long nrow() const
Return the number of rows of the matrix.
Definition: complex_matrices.h:458
CCComplexMatrix()
Default constructor.
Definition: complex_matrices.h:407
unsigned long ncol() const
Return the number of columns of the matrix.
Definition: complex_matrices.h:464
int ludecompose()
LU decomposition using SuperLU.
Definition: complex_matrices.cc:614
void clean_up_memory()
LU clean up (perhaps this should happen in the destructor)
Definition: complex_matrices.cc:750
bool Doc_stats_during_solve
Definition: complex_matrices.h:403
void operator=(const CCComplexMatrix &)=delete
Broken assignment operator.
void residual(const Vector< std::complex< double >> &x, const Vector< std::complex< double >> &b, Vector< std::complex< double >> &residual)
Find the residulal to x of Ax=b, ie r=b-Ax.
Definition: complex_matrices.cc:791
CCComplexMatrix(const CCComplexMatrix &matrix)=delete
Broken copy constructor.
void * F_factors
Storage for the LU factors as required by SuperLU.
Definition: complex_matrices.h:395
void multiply_transpose(const Vector< std::complex< double >> &x, Vector< std::complex< double >> &soln)
Multiply the transposed matrix by the vector x: soln=A^T x.
Definition: complex_matrices.cc:896
void disable_doc_stats()
the solve
Definition: complex_matrices.h:452
CCComplexMatrix(const Vector< std::complex< double >> &value, const Vector< int > &row_index, const Vector< int > &column_start, const unsigned long &n, const unsigned long &m)
Definition: complex_matrices.h:418
void enable_doc_stats()
Definition: complex_matrices.h:445
Definition: matrices.h:2585
int * column_start()
Access to C-style column_start array.
Definition: matrices.h:2692
std::complex< double > get_entry(const unsigned long &i, const unsigned long &j) const
Definition: matrices.h:2654
int * row_index()
Access to C-style row index array.
Definition: matrices.h:2704
A class for compressed row matrices.
Definition: complex_matrices.h:279
unsigned long nrow() const
Return the number of rows of the matrix.
Definition: complex_matrices.h:339
std::complex< double > operator()(const unsigned long &i, const unsigned long &j) const
Overload the round-bracket access operator for read-only access.
Definition: complex_matrices.h:351
void * F_factors
Storage for the LU factors as required by SuperLU.
Definition: complex_matrices.h:282
void enable_doc_stats()
Definition: complex_matrices.h:326
void multiply(const Vector< std::complex< double >> &x, Vector< std::complex< double >> &soln)
Multiply the matrix by the vector x: soln=Ax.
Definition: complex_matrices.cc:1169
virtual ~CRComplexMatrix()
Destructor: Kill the LU decomposition if it has been computed.
Definition: complex_matrices.h:319
unsigned long ncol() const
Return the number of columns of the matrix.
Definition: complex_matrices.h:345
bool Doc_stats_during_solve
Definition: complex_matrices.h:383
int ludecompose()
LU decomposition using SuperLU.
Definition: complex_matrices.cc:946
CRComplexMatrix(const CRComplexMatrix &matrix)=delete
Broken copy constructor.
void disable_doc_stats()
the solve
Definition: complex_matrices.h:333
CRComplexMatrix()
Default constructor.
Definition: complex_matrices.h:289
void lubksub(Vector< std::complex< double >> &rhs)
LU back solve for given RHS.
Definition: complex_matrices.cc:999
void operator=(const CRComplexMatrix &)=delete
Broken assignment operator.
int Info
Info flag for the SuperLU solver.
Definition: complex_matrices.h:285
void residual(const Vector< std::complex< double >> &x, const Vector< std::complex< double >> &b, Vector< std::complex< double >> &residual)
Find the residual to x of Ax=b, i.e. r=b-Ax.
Definition: complex_matrices.cc:1120
void multiply_transpose(const Vector< std::complex< double >> &x, Vector< std::complex< double >> &soln)
Multiply the transposed matrix by the vector x: soln=A^T x.
Definition: complex_matrices.cc:1207
CRComplexMatrix(const Vector< std::complex< double >> &value, const Vector< int > &column_index, const Vector< int > &row_start, const unsigned long &n, const unsigned long &m)
Definition: complex_matrices.h:298
void clean_up_memory()
LU clean up (perhaps this should happen in the destructor)
Definition: complex_matrices.cc:1080
Definition: matrices.h:682
int * row_start()
Access to C-style row_start array.
Definition: matrices.h:785
int * column_index()
Access to C-style column index array.
Definition: matrices.h:797
std::complex< double > get_entry(const unsigned long &i, const unsigned long &j) const
Definition: matrices.h:748
Definition: complex_matrices.h:56
virtual int ludecompose()
Definition: complex_matrices.h:84
virtual ~ComplexMatrixBase()
virtual (empty) destructor
Definition: complex_matrices.h:74
virtual double max_residual(const Vector< std::complex< double >> &x, const Vector< std::complex< double >> &rhs)
Definition: complex_matrices.h:124
virtual std::complex< double > operator()(const unsigned long &i, const unsigned long &j) const =0
virtual unsigned long ncol() const =0
Return the number of columns of the matrix.
virtual void multiply(const Vector< std::complex< double >> &x, Vector< std::complex< double >> &soln)=0
Multiply the matrix by the vector x: soln=Ax.
virtual void lubksub(Vector< std::complex< double >> &rhs)
Definition: complex_matrices.h:97
void operator=(const ComplexMatrixBase &)=delete
Broken assignment operator.
virtual void residual(const Vector< std::complex< double >> &x, const Vector< std::complex< double >> &b, Vector< std::complex< double >> &residual)=0
Find the residual, i.e. r=b-Ax the residual.
virtual void multiply_transpose(const Vector< std::complex< double >> &x, Vector< std::complex< double >> &soln)=0
Multiply the transposed matrix by the vector x: soln=A^T x.
virtual unsigned long nrow() const =0
Return the number of rows of the matrix.
ComplexMatrixBase()
(Empty) constructor.
Definition: complex_matrices.h:59
virtual void solve(Vector< std::complex< double >> &rhs)
Definition: complex_matrices.cc:34
ComplexMatrixBase(const ComplexMatrixBase &matrix)=delete
Broken copy constructor.
Definition: complex_matrices.h:155
std::complex< double > & operator()(const unsigned long &i, const unsigned long &j)
Definition: complex_matrices.h:240
bool Overwrite_matrix_storage
Definition: complex_matrices.h:165
void lubksub(Vector< std::complex< double >> &rhs)
Back substitute an LU decomposed matrix.
Definition: complex_matrices.cc:374
virtual ~DenseComplexMatrix()
Destructor, delete the storage for Index vector and LU storage (if any)
Definition: complex_matrices.cc:104
unsigned long ncol() const
Return the number of columns of the matrix.
Definition: complex_matrices.h:225
Vector< long > * Index
Pointer to storage for the index of permutations in the LU solve.
Definition: complex_matrices.h:158
void residual(const Vector< std::complex< double >> &x, const Vector< std::complex< double >> &rhs, Vector< std::complex< double >> &residual)
Find the residual of Ax=b, i.e. r=b-Ax.
Definition: complex_matrices.cc:450
void multiply(const Vector< std::complex< double >> &x, Vector< std::complex< double >> &soln)
Multiply the matrix by the vector x: soln=Ax.
Definition: complex_matrices.cc:507
int ludecompose()
Definition: complex_matrices.cc:121
DenseComplexMatrix(const unsigned long &n, const unsigned long &m, const std::complex< double > &initial_val)
Definition: complex_matrices.h:201
DenseComplexMatrix(const DenseComplexMatrix &matrix)=delete
Broken copy constructor.
void delete_lu_factors()
Definition: complex_matrices.cc:87
DenseComplexMatrix(const unsigned long &n, const unsigned long &m)
Constructor to build a matrix with n rows and m columns.
Definition: complex_matrices.h:191
void operator=(const DenseComplexMatrix &)=delete
Broken assignment operator.
DenseComplexMatrix(const unsigned long &n)
Constructor to build a square n by n matrix.
Definition: complex_matrices.h:182
DenseComplexMatrix()
Empty constructor, simply assign the lengths N and M to 0.
Definition: complex_matrices.h:173
std::complex< double > operator()(const unsigned long &i, const unsigned long &j) const
Definition: complex_matrices.h:232
std::complex< double > * LU_factors
Pointer to storage for the LU decomposition.
Definition: complex_matrices.h:161
unsigned long nrow() const
Return the number of rows of the matrix.
Definition: complex_matrices.h:219
void multiply_transpose(const Vector< std::complex< double >> &x, Vector< std::complex< double >> &soln)
Multiply the transposed matrix by the vector x: soln=A^T x.
Definition: complex_matrices.cc:545
Definition: matrices.h:386
std::complex< double > & entry(const unsigned long &i, const unsigned long &j)
Definition: matrices.h:447
std::complex< double > get_entry(const unsigned long &i, const unsigned long &j) const
Definition: matrices.h:457
Definition: oomph_definitions.h:222
T * value()
Access to C-style value array.
Definition: matrices.h:616
Definition: oomph-lib/src/generic/Vector.h:58
Eigen::Map< Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor >, 0, Eigen::OuterStride<> > matrix(T *data, int rows, int cols, int stride)
Definition: common.h:85
#define max(a, b)
Definition: datatypes.h:23
int * m
Definition: level2_cplx_impl.h:294
DRAIG: Change all instances of (SPATIAL_DIM) to (DIM-1).
Definition: AnisotropicHookean.h:10
list x
Definition: plotDoE.py:28
#define OOMPH_EXCEPTION_LOCATION
Definition: oomph_definitions.h:61
#define OOMPH_CURRENT_FUNCTION
Definition: oomph_definitions.h:86
Definition: datatypes.h:12
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2