Dune Core Modules (unstable)

dynmatrixev.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 // SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
4 // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
5 #ifndef DUNE_DYNMATRIXEIGENVALUES_HH
6 #define DUNE_DYNMATRIXEIGENVALUES_HH
7 
8 #include <algorithm>
9 #include <memory>
10 
11 #include "dynmatrix.hh"
12 #include "fmatrixev.hh"
13 
22 namespace Dune {
23 
24  namespace DynamicMatrixHelp {
25 
26 #if HAVE_LAPACK
27  using Dune::FMatrixHelp::eigenValuesNonsymLapackCall;
28 #endif
29 
38  template <typename K, class C>
39  static void eigenValuesNonSym(const DynamicMatrix<K>& matrix,
40  DynamicVector<C>& eigenValues,
41  std::vector<DynamicVector<K>>* eigenVectors = nullptr
42  )
43  {
44 
45 #if HAVE_LAPACK
46  {
47  const long int N = matrix.rows();
48  const char jobvl = 'n';
49  const char jobvr = eigenVectors ? 'v' : 'n';
50 
51 
52  // matrix to put into dgeev
53  auto matrixVector = std::make_unique<double[]>(N*N);
54 
55  // copy matrix
56  int row = 0;
57  for(int i=0; i<N; ++i)
58  {
59  for(int j=0; j<N; ++j, ++row)
60  {
61  matrixVector[ row ] = matrix[ i ][ j ];
62  }
63  }
64 
65  // working memory
66  auto eigenR = std::make_unique<double[]>(N);
67  auto eigenI = std::make_unique<double[]>(N);
68 
69  const long int lwork = eigenVectors ? 4*N : 3*N;
70  auto work = std::make_unique<double[]>(lwork);
71  auto vr = eigenVectors ? std::make_unique<double[]>(N*N) : std::unique_ptr<double[]>{};
72 
73  // return value information
74  long int info = 0;
75 
76  // call LAPACK routine (see fmatrixev_ext.cc)
77  eigenValuesNonsymLapackCall(&jobvl, &jobvr, &N, matrixVector.get(), &N,
78  eigenR.get(), eigenI.get(), nullptr, &N, vr.get(), &N, work.get(),
79  &lwork, &info);
80 
81  if( info != 0 )
82  {
83  std::cerr << "For matrix " << matrix << " eigenvalue calculation failed! " << std::endl;
84  DUNE_THROW(InvalidStateException,"eigenValues: Eigenvalue calculation failed!");
85  }
86 
87  eigenValues.resize(N);
88  for (int i=0; i<N; ++i)
89  eigenValues[i] = std::complex<double>(eigenR[i], eigenI[i]);
90 
91  if (eigenVectors) {
92  eigenVectors->resize(N);
93  for (int i = 0; i < N; ++i) {
94  auto& v = (*eigenVectors)[i];
95  v.resize(N);
96  std::copy(vr.get() + N*i, vr.get() + N*(i+1), &v[0]);
97  }
98  }
99  }
100 #else // #if HAVE_LAPACK
101  DUNE_THROW(NotImplemented,"LAPACK not found!");
102 #endif
103  }
104  }
105 
106 }
108 #endif
constexpr size_type rows() const
number of rows
Definition: densematrix.hh:709
Construct a matrix with a dynamic size.
Definition: dynmatrix.hh:61
Construct a vector with a dynamic size.
Definition: dynvector.hh:59
Default exception if a function was called while the object is not in a valid state for that function...
Definition: exceptions.hh:281
Default exception for dummy implementations.
Definition: exceptions.hh:263
This file implements a dense matrix with dynamic numbers of rows and columns.
static void eigenValuesNonSym(const DynamicMatrix< K > &matrix, DynamicVector< C > &eigenValues, std::vector< DynamicVector< K >> *eigenVectors=nullptr)
calculates the eigenvalues of a symmetric field matrix
Definition: dynmatrixev.hh:39
Eigenvalue computations for the FieldMatrix class.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
Dune namespace.
Definition: alignedallocator.hh:13
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (May 4, 22:30, 2024)