Nektar++
Loading...
Searching...
No Matches
TestNekLinSysIterEigenvalues.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: TestNekLinSysIterEigenvalues.cpp
4//
5// For more information, please see: http://www.nektar.info
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description: Unit tests for iterative linear solver eigenvalue estimates.
32//
33///////////////////////////////////////////////////////////////////////////////
34
39
40#include <boost/test/unit_test.hpp>
41
42#include <algorithm>
43#include <cmath>
44#include <fstream>
45#include <vector>
46
48{
49using namespace LibUtilities;
50
52{
53public:
54 BandedMatrixOperator(const std::vector<NekDouble> &diagonal,
55 const std::vector<NekDouble> &offDiagonal)
56 : m_diagonal(diagonal), m_offDiagonal(offDiagonal)
57 {
58 }
59
62 [[maybe_unused]] const bool &flag)
63 {
64 std::fill(output.data(), output.data() + output.size(), 0.0);
65 for (size_t i = 0; i < m_diagonal.size(); ++i)
66 {
67 output[i] += m_diagonal[i] * input[i];
68 if (i > 0)
69 {
70 output[i] += m_offDiagonal[i - 1] * input[i - 1];
71 }
72 if (i + 1 < m_diagonal.size())
73 {
74 output[i] += m_offDiagonal[i] * input[i + 1];
75 }
76 }
77 }
78
79private:
80 std::vector<NekDouble> m_diagonal;
81 std::vector<NekDouble> m_offDiagonal;
82};
83
84// create a fake session file for the linear solver
86{
87 const std::string filename =
88 "/tmp/nektar_linsys_iter_eigenvalue_session.xml";
89 std::ofstream sessionFile(filename);
90 sessionFile << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
91 << "<NEKTAR>\n"
92 << " <CONDITIONS />\n"
93 << "</NEKTAR>\n";
94 sessionFile.close();
95
96 char arg0[] = "LinearAlgebraUnitTests";
97 char *argv[] = {arg0, nullptr};
98 std::vector<std::string> filenames = {filename};
99 CommSharedPtr comm = GetCommFactory().CreateInstance("Serial", 0, nullptr);
100 return SessionReader::CreateInstance(1, argv, filenames, comm);
101}
102
103std::vector<NekDouble> CreateBandedMatrix(
104 const std::vector<NekDouble> &diagonal,
105 const std::vector<NekDouble> &offDiagonal)
106{
107 const int n = diagonal.size();
108 std::vector<NekDouble> matrix(n * n, 0.0);
109 for (int i = 0; i < n; ++i)
110 {
111 matrix[i + i * n] = diagonal[i];
112 if (i + 1 < n)
113 {
114 matrix[i + (i + 1) * n] = offDiagonal[i];
115 matrix[i + 1 + i * n] = offDiagonal[i];
116 }
117 }
118 return matrix;
119}
120
121NekDouble MaxRealEigenvalueFromLapack(const std::vector<NekDouble> &matrix,
122 const int n)
123{
124 std::vector<NekDouble> a = matrix;
125 Array<OneD, NekDouble> wr(n, 0.0);
126 Array<OneD, NekDouble> wi(n, 0.0);
127 Array<OneD, NekDouble> work(3 * n, 0.0);
128
129 const char jobvl = 'N';
130 const char jobvr = 'N';
131 NekDouble dum = 0.0;
132 int info = 0;
133
134 Lapack::Dgeev(jobvl, jobvr, n, a.data(), n, wr.data(), wi.data(), &dum, 1,
135 &dum, 1, work.data(), work.size(), info);
136 BOOST_REQUIRE_EQUAL(info, 0);
137
138 return *std::max_element(wr.data(), wr.data() + n);
139}
140
142{
143 return *std::max_element(values.data(), values.data() + values.size());
144}
145
146BOOST_AUTO_TEST_CASE(TestCGComputesMaximumEigenvalue)
147{
148 const std::vector<NekDouble> diagonal = {4.0, 5.0, 6.0, 7.0, 30.0};
149 const std::vector<NekDouble> offDiagonal = {0.5, -0.35, 0.4, -0.2};
150 const int n = diagonal.size();
151 const NekDouble reference = MaxRealEigenvalueFromLapack(
152 CreateBandedMatrix(diagonal, offDiagonal), n);
153
154 auto session = CreateTestSession();
155
156 NekSysKey key;
157 key.m_NekLinSysMaxIterations = 10;
158 key.m_NekLinSysTolerance = 1.0e-13;
159
160 NekLinSysIterCG solver(session, session->GetComm(), n, key);
161 solver.InitObject();
162 solver.SetEigenValueFlags(true, false);
163
164 BandedMatrixOperator matrixOperator(diagonal, offDiagonal);
165 NekSysOperators operators;
167 &matrixOperator);
168 solver.SetSysOperators(operators);
169
170 Array<OneD, NekDouble> rhs(n, 1.0);
171 Array<OneD, NekDouble> solution(n, 0.0);
172 solver.SolveSystem(n, rhs, solution);
173
174 const Array<OneD, NekDouble> &computed = solver.GetEigenValues();
175 BOOST_REQUIRE_EQUAL(computed.size(), n);
176 BOOST_CHECK_SMALL(std::abs(MaxValue(computed) - reference) / reference,
177 1.0e-10);
178}
179
180BOOST_AUTO_TEST_CASE(TestGMRESComputesMaximumEigenvalue)
181{
182 const std::vector<NekDouble> diagonal = {4.0, 5.0, 6.0, 7.0, 30.0};
183 const std::vector<NekDouble> offDiagonal = {0.5, -0.35, 0.4, -0.2};
184 const int n = diagonal.size();
185 const NekDouble reference = MaxRealEigenvalueFromLapack(
186 CreateBandedMatrix(diagonal, offDiagonal), n);
187
188 auto session = CreateTestSession();
189
190 NekSysKey key;
191 key.m_NekLinSysMaxIterations = n - 1;
192 key.m_NekLinSysTolerance = 1.0e-13;
193 key.m_LinSysMaxStorage = n - 1;
194 key.m_KrylovMaxHessMatBand = n;
195
196 NekLinSysIterGMRES solver(session, session->GetComm(), n, key);
197 solver.InitObject();
198 solver.SetFlagWarnings(false);
199 solver.SetEigenValueFlags(true, false);
200
201 BandedMatrixOperator matrixOperator(diagonal, offDiagonal);
202 NekSysOperators operators;
204 &matrixOperator);
205 solver.SetSysOperators(operators);
206
207 Array<OneD, NekDouble> rhs(n, 1.0);
208 Array<OneD, NekDouble> solution(n, 0.0);
209 solver.SolveSystem(n, rhs, solution);
210
211 const Array<OneD, NekDouble> &computed = solver.GetEigenValues();
212 BOOST_REQUIRE_EQUAL(computed.size(), n);
213 BOOST_CHECK_SMALL(std::abs(MaxValue(computed) - reference) / reference,
214 1.0e-6);
215}
216} // namespace Nektar::NekLinSysIterEigenvalueUnitTests
LibUtilities::SessionReaderSharedPtr session
tBasePtr CreateInstance(tKey idKey, tParam... args)
Create an instance of the class referred to by idKey.
const Array< OneD, NekDouble > & GetEigenValues()
void SetEigenValueFlags(const bool computeEV, const bool outputEV)
void SetFlagWarnings(bool in)
Definition NekSys.h:288
void SetSysOperators(const NekSysOperators &in)
Definition NekSys.h:283
int SolveSystem(const int nGlobal, const Array< OneD, const NekDouble > &pInput, Array< OneD, NekDouble > &pOutput, const int nDir=0)
Definition NekSys.h:271
void DefineNekSysLhsEval(FuncPointerT func, ObjectPointerT obj)
Definition NekSys.h:107
static SessionReaderSharedPtr CreateInstance(int argc, char *argv[])
Creates an instance of the SessionReader class.
BandedMatrixOperator(const std::vector< NekDouble > &diagonal, const std::vector< NekDouble > &offDiagonal)
void Apply(const Array< OneD, const NekDouble > &input, Array< OneD, NekDouble > &output, const bool &flag)
static void Dgeev(const char &uplo, const char &lrev, const int &n, const double *a, const int &lda, double *wr, double *wi, double *rev, const int &ldr, double *lev, const int &ldv, double *work, const int &lwork, int &info)
Solve general real matrix eigenproblem.
Definition Lapack.hpp:357
std::shared_ptr< SessionReader > SessionReaderSharedPtr
CommFactory & GetCommFactory()
std::shared_ptr< Comm > CommSharedPtr
Pointer to a Communicator object.
Definition Comm.h:62
NekDouble MaxRealEigenvalueFromLapack(const std::vector< NekDouble > &matrix, const int n)
NekDouble MaxValue(const Array< OneD, const NekDouble > &values)
std::vector< NekDouble > CreateBandedMatrix(const std::vector< NekDouble > &diagonal, const std::vector< NekDouble > &offDiagonal)