Nektar++
testLinearSystem.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: testLinearSystem.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: Test code for NekVector
32//
33///////////////////////////////////////////////////////////////////////////////
34
36#include <boost/test/tools/floating_point_comparison.hpp>
37#include <boost/test/unit_test.hpp>
38
40{
41BOOST_AUTO_TEST_CASE(TestLinearSystemSolveWithReturnValue)
42{
43 double matrix_buf[] = {10, 5, 2};
44
45 double b_buf[] = {20, 50, 10};
46
47 std::shared_ptr<NekMatrix<double>> A(
48 new NekMatrix<double>(3, 3, matrix_buf, eDIAGONAL));
49 NekVector<double> b1(3, b_buf);
50 std::shared_ptr<NekVector<double>> b2(new NekVector<double>(3, b_buf));
51 NekVector<double> b3(3, b_buf);
52 std::shared_ptr<NekVector<double>> b4(new NekVector<double>(3, b_buf));
53
54 LinearSystem linsys(A);
55
56 NekVector<double> result1 = linsys.Solve(b1);
57 NekVector<double> result2 = linsys.Solve(b2);
58 NekVector<double> result3 = linsys.Solve(&b1);
59 NekVector<double> result4 = linsys.Solve(b2.get());
60
61 NekVector<double> result5 = linsys.Solve(b3);
62 NekVector<double> result6 = linsys.Solve(b4);
63 NekVector<double> result7 = linsys.Solve(&b3);
64 NekVector<double> result8 = linsys.Solve(b4.get());
65
66 double expected_result_buf[] = {2, 10, 5};
67 NekVector<double> expectedResult(3, expected_result_buf);
68
69 BOOST_CHECK_EQUAL(result1, expectedResult);
70 BOOST_CHECK_EQUAL(result2, expectedResult);
71 BOOST_CHECK_EQUAL(result3, expectedResult);
72 BOOST_CHECK_EQUAL(result4, expectedResult);
73 BOOST_CHECK_EQUAL(result5, expectedResult);
74 BOOST_CHECK_EQUAL(result6, expectedResult);
75 BOOST_CHECK_EQUAL(result7, expectedResult);
76 BOOST_CHECK_EQUAL(result8, expectedResult);
77}
78
79BOOST_AUTO_TEST_CASE(TestLinearSystemSolveWithSoluationAsParameter)
80{
81 double matrix_buf[] = {10, 5, 2};
82
83 double b_buf[] = {20, 50, 10};
84
85 std::shared_ptr<NekMatrix<double>> A(
86 new NekMatrix<double>(3, 3, matrix_buf, eDIAGONAL));
87 NekVector<double> b1(3, b_buf);
88 std::shared_ptr<NekVector<double>> b2(new NekVector<double>(3, b_buf));
89 NekVector<double> b3(3, b_buf);
90 std::shared_ptr<NekVector<double>> b4(new NekVector<double>(3, b_buf));
91
92 NekVector<double> result1(3, 0.0);
93 std::shared_ptr<NekVector<double>> result2(new NekVector<double>(3, 0.0));
94 NekVector<double> result3(3, 0.0);
95 std::shared_ptr<NekVector<double>> result4(new NekVector<double>(3, 0.0));
96
97 LinearSystem linsys(A);
98 double expected_result_buf[] = {2, 10, 5};
99 NekVector<double> expectedResult(3, expected_result_buf);
100
101 linsys.Solve(b1, result1);
102 linsys.Solve(b2, result3);
103 linsys.Solve(b3, result4);
104 linsys.Solve(b4, result2);
105
106 BOOST_CHECK_EQUAL(result1, expectedResult);
107 BOOST_CHECK_EQUAL(*result2, expectedResult);
108 BOOST_CHECK_EQUAL(result3, expectedResult);
109 BOOST_CHECK_EQUAL(*result4, expectedResult);
110}
111
112BOOST_AUTO_TEST_CASE(TestDiagonalSystem)
113{
114 double matrix_buf[] = {10, 5, 2};
115
116 double result_buf[] = {20, 50, 10};
117
118 std::shared_ptr<NekMatrix<double>> A(
119 new NekMatrix<double>(3, 3, matrix_buf, eDIAGONAL));
120 std::shared_ptr<NekVector<double>> b(new NekVector<double>(3, result_buf));
121
122 LinearSystem linsys(A);
123
124 NekVector<double> result = linsys.Solve(b);
125
126 double expected_result_buf[] = {2, 10, 5};
127 NekVector<double> expectedResult(3, expected_result_buf);
128
129 BOOST_CHECK_EQUAL(result, expectedResult);
130 std::shared_ptr<NekVector<double>> b1 = b;
131 NekVector<double> result1 = linsys.Solve(b1);
132 BOOST_CHECK_EQUAL(result1, expectedResult);
133
134 NekVector<double> result2 = linsys.SolveTranspose(b);
135 BOOST_CHECK_EQUAL(expectedResult, result2);
136}
137
138BOOST_AUTO_TEST_CASE(TestDiagonalSystemAsFullSystem)
139{
140 double matrix_buf[] = {10.0, 0.0, 0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 2.0};
141
142 double result_buf[] = {20, 50, 10};
143
144 std::shared_ptr<NekMatrix<double>> A(
145 new NekMatrix<double>(3, 3, matrix_buf, eFULL));
146 std::shared_ptr<NekVector<double>> b(new NekVector<double>(3, result_buf));
147
148 LinearSystem linsys(A);
149
150 NekVector<double> result = linsys.Solve(b);
151
152 double epsilon = 1e-14;
153
154 BOOST_CHECK_CLOSE(result[0], 2.0, epsilon);
155 BOOST_CHECK_CLOSE(result[1], 10.0, epsilon);
156 BOOST_CHECK_CLOSE(result[2], 5.0, epsilon);
157}
158
159BOOST_AUTO_TEST_CASE(TestSmallFullSystem)
160{
161
162 double matrix_buf[] = {81, -5, -28, 4};
163
164 double b_buf[] = {-941, 348};
165
166 std::shared_ptr<NekMatrix<double>> A(
167 new NekMatrix<double>(2, 2, matrix_buf, eFULL));
168 std::shared_ptr<NekVector<double>> b(new NekVector<double>(2, b_buf));
169 LinearSystem linsys(A);
170
171 NekVector<double> result = linsys.Solve(b);
172 double epsilon = 1e-11;
173
174 BOOST_CHECK_CLOSE(result[0], 32.5, epsilon);
175 BOOST_CHECK_CLOSE(result[1], 127.625, epsilon);
176
177 result = linsys.SolveTranspose(b);
178 BOOST_CHECK_CLOSE(result[0], -11.0, epsilon);
179 BOOST_CHECK_CLOSE(result[1], 10.0, epsilon);
180}
181
182BOOST_AUTO_TEST_CASE(TestLargeFullSystem)
183{
184
185 // Larger matrix.
186 double matrix_buf[] = {
187 -85, -55, -37, -35, 97, 50, 79, 56, 49, 63, 57, -59, 45,
188 -8, -93, 92, 43, -62, 77, 66, 54, -5, 99, -61, -50, -12,
189 -18, 31, -26, -62, 1, -47, -91, -47, -61, 41, -58, -90, 53,
190 -1, 94, 83, -86, 23, -84, 19, -50, 88, -53, 85, 49, 78,
191 17, 72, -99, -85, -86, 30, 80, 72, 66, -29, -91, -53, -19,
192 -47, 68, -72, -87, 79, 43, -66, -53, -61, -23, -37, 31, -34,
193 -42, 88, -76, -65, 25, 28, -61, -60, 9, 29, -66, -32, 78,
194 39, 94, 68, -17, -98, -36, 40, 22, 5};
195
196 double b_buf[] = {12719, -3169, -16810, 7408, -14945,
197 -6822, 10166, 7023, 8679, -11826};
198
199 std::shared_ptr<NekMatrix<double>> A(
200 new NekMatrix<double>(10, 10, matrix_buf, eFULL));
201 std::shared_ptr<NekVector<double>> b(new NekVector<double>(10, b_buf));
202 LinearSystem linsys(A);
203
204 NekVector<double> result = linsys.SolveTranspose(b);
205 double epsilon = 1e-11;
206 BOOST_CHECK_CLOSE(result[0], -88.0, epsilon);
207 BOOST_CHECK_CLOSE(result[1], -43.0, epsilon);
208 BOOST_CHECK_CLOSE(result[2], -73.0, epsilon);
209 BOOST_CHECK_CLOSE(result[3], 25.0, epsilon);
210 BOOST_CHECK_CLOSE(result[4], 4.0, epsilon);
211 BOOST_CHECK_CLOSE(result[5], -59.0, epsilon);
212 BOOST_CHECK_CLOSE(result[6], 62.0, epsilon);
213 BOOST_CHECK_CLOSE(result[7], -55.0, epsilon);
214 BOOST_CHECK_CLOSE(result[8], 25.0, epsilon);
215 BOOST_CHECK_CLOSE(result[9], 9.0, epsilon);
216}
217
218// void testSolvingBlockDiagonalMatrices()
219// {
220// //typedef NekMatrix<double, DiagonalMatrixTag, eBlock>
221// BlockMatrix;
222// //typedef BlockMatrix::InnerMatrixType InnerMatrixType;
223// //
224// //std::shared_ptr<BlockMatrix> m1(new BlockMatrix(4, 2, 2));
225// //const double m1_buf0[] = {-91, -47, 94, 83};
226// //const double m1_buf1[] = {49, 78, 80, 72};
227// //const double m1_buf2[] = {87, 79, 31, -34};
228// //const double m1_buf3[] = {9, 29, -17, -98};
229// //
230// //m1->GetBlock(0,0) = InnerMatrixType(2, 2, m1_buf0);
231// //m1->GetBlock(1,1) = InnerMatrixType(2, 2, m1_buf1);
232// //m1->GetBlock(2,2) = InnerMatrixType(2, 2, m1_buf2);
233// //m1->GetBlock(3,3) = InnerMatrixType(2, 2, m1_buf3);
234// //
235// //double b_vals[] = {-7198, 7642, 1344, 3744, -9968, 115, -2469,
236// 8424};
237// //std::shared_ptr<NekVector<double> > b(new NekVector<double>(8,
238// b_vals));
239// //
240// //LinearSystem<BlockMatrix, NekVector<double> > linsys(m1, b);
241// //
242// //NekVector<double> result = linsys.Solve();
243// //double epsilon = 1e-11;
244// //BOOST_CHECK_CLOSE(result[0], 76.0, epsilon);
245// //BOOST_CHECK_CLOSE(result[1], 6.0, epsilon);
246// //BOOST_CHECK_CLOSE(result[2], 72.0, epsilon);
247// //BOOST_CHECK_CLOSE(result[3], -28.0, epsilon);
248// //BOOST_CHECK_CLOSE(result[4], -61.0, epsilon);
249// //BOOST_CHECK_CLOSE(result[5], -59.0, epsilon);
250// //BOOST_CHECK_CLOSE(result[6], 6.0, epsilon);
251// //BOOST_CHECK_CLOSE(result[7], -87.0, epsilon);
252// }
253
254BOOST_AUTO_TEST_CASE(TestFullSystemWithWrappedVectors)
255{
256 // Larger matrix.
257 double matrix_buf[] = {
258 -85, -55, -37, -35, 97, 50, 79, 56, 49, 63, 57, -59, 45,
259 -8, -93, 92, 43, -62, 77, 66, 54, -5, 99, -61, -50, -12,
260 -18, 31, -26, -62, 1, -47, -91, -47, -61, 41, -58, -90, 53,
261 -1, 94, 83, -86, 23, -84, 19, -50, 88, -53, 85, 49, 78,
262 17, 72, -99, -85, -86, 30, 80, 72, 66, -29, -91, -53, -19,
263 -47, 68, -72, -87, 79, 43, -66, -53, -61, -23, -37, 31, -34,
264 -42, 88, -76, -65, 25, 28, -61, -60, 9, 29, -66, -32, 78,
265 39, 94, 68, -17, -98, -36, 40, 22, 5};
266
267 double b_buf[] = {12719, -3169, -16810, 7408, -14945,
268 -6822, 10166, 7023, 8679, -11826};
269 double result_buf[10] = {0};
270
271 std::shared_ptr<NekMatrix<double>> A(
272 new NekMatrix<double>(10, 10, matrix_buf, eFULL));
273 std::shared_ptr<NekVector<double>> b(
275 NekVector<double> result(Array<OneD, double>(10, result_buf), eWrapper);
276
277 LinearSystem linsys(A);
278
279 linsys.SolveTranspose(b, result);
280 double epsilon = 1e-11;
281 BOOST_CHECK_CLOSE(result[0], -88.0, epsilon);
282 BOOST_CHECK_CLOSE(result[1], -43.0, epsilon);
283 BOOST_CHECK_CLOSE(result[2], -73.0, epsilon);
284 BOOST_CHECK_CLOSE(result[3], 25.0, epsilon);
285 BOOST_CHECK_CLOSE(result[4], 4.0, epsilon);
286 BOOST_CHECK_CLOSE(result[5], -59.0, epsilon);
287 BOOST_CHECK_CLOSE(result[6], 62.0, epsilon);
288 BOOST_CHECK_CLOSE(result[7], -55.0, epsilon);
289 BOOST_CHECK_CLOSE(result[8], 25.0, epsilon);
290 BOOST_CHECK_CLOSE(result[9], 9.0, epsilon);
291}
292
293BOOST_AUTO_TEST_CASE(TestSymmetricMatrix)
294{
296 m.SetValue(0, 0, 3.0);
297 m.SetValue(0, 1, 4.0);
298 m.SetValue(0, 2, 9);
299 m.SetValue(1, 1, 7);
300 m.SetValue(1, 2, 2);
301 m.SetValue(2, 2, 11);
302
303 BOOST_CHECK_EQUAL(4.0, m(1, 0));
304
305 double b_buf[] = {-20.0, 30.0, -20.0};
306 NekVector<double> b(3, b_buf);
307
308 LinearSystem linsys(m);
309 NekVector<double> x = linsys.Solve(b);
310 NekVector<double> xt = linsys.SolveTranspose(b);
311
312 double epsilon = 1e-11;
313 BOOST_CHECK_CLOSE(x[0], 3.0, epsilon);
314 BOOST_CHECK_CLOSE(x[1], 4.0, epsilon);
315 BOOST_CHECK_CLOSE(x[2], -5.0, epsilon);
316 BOOST_CHECK_CLOSE(xt[0], 3.0, epsilon);
317 BOOST_CHECK_CLOSE(xt[1], 4.0, epsilon);
318 BOOST_CHECK_CLOSE(xt[2], -5.0, epsilon);
319}
320} // namespace Nektar::LinearSystemUnitTests
RawType_t< VectorType > SolveTranspose(const VectorType &b)
Definition: NekLinSys.hpp:471
RawType_t< VectorType > Solve(const VectorType &b)
Definition: NekLinSys.hpp:448
BOOST_AUTO_TEST_CASE(TestLinearSystemSolveWithReturnValue)