Nektar++
TestNekMatrixOperations.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: TestNekMatrixOperations.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: Tests NekMatrix functionality.
32//
33///////////////////////////////////////////////////////////////////////////////
34
37#include <boost/test/tools/floating_point_comparison.hpp>
38#include <boost/test/unit_test.hpp>
39#include <functional>
40#include <iostream>
41#include <memory>
42
43namespace Nektar
44{
46 double values[], double scale,
47 std::shared_ptr<NekMatrix<NekDouble, StandardMatrixTag>> &m1,
48 std::shared_ptr<NekMatrix<NekMatrix<NekDouble>, ScaledMatrixTag>> &m2,
49 std::shared_ptr<NekMatrix<NekMatrix<NekDouble>, BlockMatrixTag>> &m3)
50{
51 m1 =
52 std::make_shared<NekMatrix<NekDouble, StandardMatrixTag>>(4, 4, values);
53
54 double inner_values[16];
55 std::transform(
56 values, values + 16, inner_values,
57 std::bind(std::divides<NekDouble>(), std::placeholders::_1, scale));
58
59 std::shared_ptr<NekMatrix<NekDouble>> inner(
60 new NekMatrix<NekDouble>(4, 4, inner_values));
61 m2 = std::make_shared<NekMatrix<NekMatrix<NekDouble>, ScaledMatrixTag>>(
62 scale, inner);
63
64 double block_1_values[] = {values[0], values[1], values[4], values[5]};
65 double block_2_values[] = {values[2], values[3], values[6], values[7]};
66 double block_3_values[] = {values[8], values[9], values[12], values[13]};
67 double block_4_values[] = {values[10], values[11], values[14], values[15]};
68 std::shared_ptr<NekMatrix<NekDouble>> block1(
69 new NekMatrix<NekDouble>(2, 2, block_1_values));
70 std::shared_ptr<NekMatrix<NekDouble>> block2(
71 new NekMatrix<NekDouble>(2, 2, block_2_values));
72 std::shared_ptr<NekMatrix<NekDouble>> block3(
73 new NekMatrix<NekDouble>(2, 2, block_3_values));
74 std::shared_ptr<NekMatrix<NekDouble>> block4(
75 new NekMatrix<NekDouble>(2, 2, block_4_values));
76
77 m3 = std::make_shared<NekMatrix<NekMatrix<NekDouble>, BlockMatrixTag>>(
78 2, 2, 2, 2);
79 m3->SetBlock(0, 0, block1);
80 m3->SetBlock(1, 0, block2);
81 m3->SetBlock(0, 1, block3);
82 m3->SetBlock(1, 1, block4);
83}
84
86 NekDouble values[], NekDouble scale,
87 std::shared_ptr<NekMatrix<NekDouble, StandardMatrixTag>> &m1,
88 std::shared_ptr<NekMatrix<NekMatrix<NekDouble>, ScaledMatrixTag>> &m2,
89 std::shared_ptr<NekMatrix<NekMatrix<NekDouble>, BlockMatrixTag>> &m3)
90{
91 m1 = std::make_shared<NekMatrix<NekDouble, StandardMatrixTag>>(
92 4, 4, values, eUPPER_TRIANGULAR);
93
94 double inner_values[10];
95 std::transform(
96 values, values + 10, inner_values,
97 std::bind(std::divides<NekDouble>(), std::placeholders::_1, scale));
98
99 std::shared_ptr<NekMatrix<NekDouble>> inner(
100 new NekMatrix<NekDouble>(4, 4, inner_values, eUPPER_TRIANGULAR));
101 m2 = std::make_shared<NekMatrix<NekMatrix<NekDouble>, ScaledMatrixTag>>(
102 scale, inner);
103
104 double block_1_values[] = {values[0], 0.0, values[1], values[2]};
105 double block_2_values[] = {values[3], values[4], values[6], values[7]};
106 double block_4_values[] = {values[5], 0.0, values[8], values[9]};
107 std::shared_ptr<NekMatrix<NekDouble>> block1(
108 new NekMatrix<NekDouble>(2, 2, block_1_values));
109 std::shared_ptr<NekMatrix<NekDouble>> block2(
110 new NekMatrix<NekDouble>(2, 2, block_2_values));
111 std::shared_ptr<NekMatrix<NekDouble>> block4(
112 new NekMatrix<NekDouble>(2, 2, block_4_values));
113
114 m3 = std::make_shared<NekMatrix<NekMatrix<NekDouble>, BlockMatrixTag>>(
115 2, 2, 2, 2);
116 m3->SetBlock(0, 0, block1);
117 m3->SetBlock(0, 1, block2);
118 m3->SetBlock(1, 1, block4);
119}
120
121namespace MatrixOperationTests
122{
123BOOST_AUTO_TEST_CASE(TestLhsFullRhsFull)
124{
125 // double lhs_values[] = {2, 4, 6, 8,
126 // 10, 12, 14, 16,
127 // 18, 20, 22, 24,
128 // 26, 28, 30, 32};
129 double lhs_values[] = {2, 10, 18, 26, 4, 12, 20, 28,
130 6, 14, 22, 30, 8, 16, 24, 32};
131
132 std::shared_ptr<NekMatrix<NekDouble, StandardMatrixTag>> lhs1;
133 std::shared_ptr<NekMatrix<NekMatrix<NekDouble>, ScaledMatrixTag>> lhs2;
134 std::shared_ptr<NekMatrix<NekMatrix<NekDouble>, BlockMatrixTag>> lhs3;
135
136 GenerateFullMatrices(lhs_values, 2.0, lhs1, lhs2, lhs3);
137 // double rhs_values[] = {4, 8, 12, 16,
138 // 20, 24, 28, 32,
139 // 36, 40, 44, 48,
140 // 52, 56, 60, 64};
141 double rhs_values[] = {4, 20, 36, 52, 8, 24, 40, 56,
142 12, 28, 44, 60, 16, 32, 48, 64};
143 std::shared_ptr<NekMatrix<NekDouble, StandardMatrixTag>> rhs1;
144 std::shared_ptr<NekMatrix<NekMatrix<NekDouble>, ScaledMatrixTag>> rhs2;
145 std::shared_ptr<NekMatrix<NekMatrix<NekDouble>, BlockMatrixTag>> rhs3;
146 GenerateFullMatrices(rhs_values, 2.0, rhs1, rhs2, rhs3);
147
148 double result_values[16];
149 std::transform(lhs_values, lhs_values + 16, rhs_values, result_values,
150 std::plus<NekDouble>());
151 NekMatrix<NekDouble> result(4, 4, result_values);
152
153 RunAllAddCombinations(*lhs1, *lhs2, *lhs3, *rhs1, *rhs2, *rhs3, result);
154}
155
156BOOST_AUTO_TEST_CASE(TestComboExpression)
157{
158 {
159 // double buf[] = {1.0, 2.0, 3.0, 4.0};
160 double buf[] = {1.0, 3.0, 2.0, 4.0};
161 SharedNekMatrixPtr inner1(new NekMatrix<NekDouble>(2, 2, buf));
162 SharedNekMatrixPtr inner2(new NekMatrix<NekDouble>(2, 2, buf));
163
164 DNekScalMat m1(2.0, inner1);
165 DNekScalMat m2(3.0, inner2);
166
167 (*inner1) * 2.0;
168 2.0 * (*inner1);
169 // DNekScalMat m3 = m1*2.0;
170 NekMatrix<NekDouble> result = m1 + 1.0 / 6.0 * m2;
171
172 BOOST_CHECK_EQUAL(result(0, 0), 2.5);
173 BOOST_CHECK_EQUAL(result(0, 1), 5.0);
174 BOOST_CHECK_EQUAL(result(1, 0), 7.5);
175 BOOST_CHECK_EQUAL(result(1, 1), 10.0);
176 }
177
178 {
179 double buf[] = {1.0, 2.0, 3.0, 4.0};
180 SharedNekMatrixPtr inner1(new NekMatrix<NekDouble>(2, 2, buf));
181 SharedNekMatrixPtr inner2(new NekMatrix<NekDouble>(2, 2, buf));
182
183 std::shared_ptr<DNekScalMat> m1(new DNekScalMat(2.0, inner1));
184 std::shared_ptr<DNekScalMat> m2(new DNekScalMat(3.0, inner2));
185
186 (*m1) * 2.0;
187
188 // BOOST_CHECK_EQUAL(result->GetValue(0,0), 2.0);
189 // BOOST_CHECK_EQUAL(result->GetValue(0,1), 4.0);
190 // BOOST_CHECK_EQUAL(result->GetValue(1,0), 6.0);
191 // BOOST_CHECK_EQUAL(result->GetValue(1,1), 8.0);
192 }
193}
194
195BOOST_AUTO_TEST_CASE(TestLhsUpperTriangularRhsUpperTriangular)
196{
197 // double lhs_values[] = {2, 4, 6, 8,
198 // 12, 14, 16,
199 // 22, 24,
200 // 32};
201 double lhs_values[] = {2, 4, 12, 6, 14, 22, 8, 16, 24, 32};
202
203 std::shared_ptr<NekMatrix<NekDouble, StandardMatrixTag>> lhs1;
204 std::shared_ptr<NekMatrix<NekMatrix<NekDouble>, ScaledMatrixTag>> lhs2;
205 std::shared_ptr<NekMatrix<NekMatrix<NekDouble>, BlockMatrixTag>> lhs3;
206 GenerateUpperTriangularMatrices(lhs_values, 2.0, lhs1, lhs2, lhs3);
207
208 // double rhs_values[] = {4, 8, 12, 16,
209 // 24, 28, 32,
210 // 44, 48,
211 // 64};
212 double rhs_values[] = {4, 8, 24, 12, 28, 44, 16, 32, 48, 64};
213 std::shared_ptr<NekMatrix<NekDouble, StandardMatrixTag>> rhs1;
214 std::shared_ptr<NekMatrix<NekMatrix<NekDouble>, ScaledMatrixTag>> rhs2;
215 std::shared_ptr<NekMatrix<NekMatrix<NekDouble>, BlockMatrixTag>> rhs3;
216 GenerateUpperTriangularMatrices(rhs_values, 2.0, rhs1, rhs2, rhs3);
217
218 double result_values[10];
219 std::transform(lhs_values, lhs_values + 10, rhs_values, result_values,
220 std::plus<NekDouble>());
221 NekMatrix<NekDouble, StandardMatrixTag> result(4, 4, result_values,
223}
224
225BOOST_AUTO_TEST_CASE(TestLhsLowerTriangularRhsLowerTriangular)
226{
227}
228} // namespace MatrixOperationTests
229
230namespace MatrixSubtractionTests
231{
232BOOST_AUTO_TEST_CASE(TestLhsFullRhsFullSubtraction)
233{
234 // double lhs_values[] = {2, 4, 6, 8,
235 // 10, 12, 14, 16,
236 // 18, 20, 22, 24,
237 // 26, 28, 30, 32};
238
239 double lhs_values[] = {2, 10, 18, 26, 4, 12, 20, 28,
240 6, 14, 22, 30, 8, 16, 24, 32};
241
242 std::shared_ptr<NekMatrix<NekDouble, StandardMatrixTag>> lhs1;
243 std::shared_ptr<NekMatrix<NekMatrix<NekDouble>, ScaledMatrixTag>> lhs2;
244 std::shared_ptr<NekMatrix<NekMatrix<NekDouble>, BlockMatrixTag>> lhs3;
245
246 GenerateFullMatrices(lhs_values, 2.0, lhs1, lhs2, lhs3);
247 // double rhs_values[] = {4, 8, 12, 16,
248 // 20, 24, 28, 32,
249 // 36, 40, 44, 48,
250 // 52, 56, 60, 64};
251 double rhs_values[] = {4, 20, 36, 52, 8, 24, 40, 56,
252 12, 28, 44, 60, 16, 32, 48, 64};
253 std::shared_ptr<NekMatrix<NekDouble, StandardMatrixTag>> rhs1;
254 std::shared_ptr<NekMatrix<NekMatrix<NekDouble>, ScaledMatrixTag>> rhs2;
255 std::shared_ptr<NekMatrix<NekMatrix<NekDouble>, BlockMatrixTag>> rhs3;
256 GenerateFullMatrices(rhs_values, 2.0, rhs1, rhs2, rhs3);
257
258 double result_values[16];
259 std::transform(lhs_values, lhs_values + 16, rhs_values, result_values,
260 std::minus<NekDouble>());
261 NekMatrix<NekDouble> result(4, 4, result_values);
262 RunAllSubCombinations(*lhs1, *lhs2, *lhs3, *rhs1, *rhs2, *rhs3, result);
263}
264} // namespace MatrixSubtractionTests
265} // namespace Nektar
BOOST_AUTO_TEST_CASE(TestLhsFullRhsFullSubtraction)
NekMatrix< NekMatrix< NekDouble, StandardMatrixTag >, ScaledMatrixTag > DNekScalMat
void GenerateFullMatrices(double values[], double scale, std::shared_ptr< NekMatrix< NekDouble, StandardMatrixTag > > &m1, std::shared_ptr< NekMatrix< NekMatrix< NekDouble >, ScaledMatrixTag > > &m2, std::shared_ptr< NekMatrix< NekMatrix< NekDouble >, BlockMatrixTag > > &m3)
void RunAllAddCombinations(const NekMatrix< NekDouble, StandardMatrixTag > &l1, const NekMatrix< NekMatrix< NekDouble, LhsScaledInnerMatrixType >, ScaledMatrixTag > &l2, const NekMatrix< NekMatrix< NekDouble, LhsBlockInnerMatrixType >, BlockMatrixTag > &l3, const NekMatrix< NekDouble, StandardMatrixTag > &r1, const NekMatrix< NekMatrix< NekDouble, RhsScaledInnerMatrixType >, ScaledMatrixTag > &r2, const NekMatrix< NekMatrix< NekDouble, RhsBlockInnerMatrixType >, BlockMatrixTag > &r3, const NekMatrix< NekDouble, StandardMatrixTag > &result)
std::shared_ptr< NekMatrix< NekDouble, StandardMatrixTag > > SharedNekMatrixPtr
void GenerateUpperTriangularMatrices(NekDouble values[], NekDouble scale, std::shared_ptr< NekMatrix< NekDouble, StandardMatrixTag > > &m1, std::shared_ptr< NekMatrix< NekMatrix< NekDouble >, ScaledMatrixTag > > &m2, std::shared_ptr< NekMatrix< NekMatrix< NekDouble >, BlockMatrixTag > > &m3)
void RunAllSubCombinations(const NekMatrix< NekDouble, StandardMatrixTag > &l1, const NekMatrix< NekMatrix< NekDouble, LhsScaledInnerMatrixType >, ScaledMatrixTag > &l2, const NekMatrix< NekMatrix< NekDouble, LhsBlockInnerMatrixType >, BlockMatrixTag > &l3, const NekMatrix< NekDouble, StandardMatrixTag > &r1, const NekMatrix< NekMatrix< NekDouble, RhsScaledInnerMatrixType >, ScaledMatrixTag > &r2, const NekMatrix< NekMatrix< NekDouble, RhsBlockInnerMatrixType >, BlockMatrixTag > &r3, const NekMatrix< NekDouble, StandardMatrixTag > &result)
double NekDouble