Nektar++
TestBandedMatrixStoragePolicy.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: TestBandedMatrixStoragePolicy.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:
32//
33///////////////////////////////////////////////////////////////////////////////
34
35#include <boost/test/unit_test.hpp>
36
39#include <UnitTests/util.h>
40#include <boost/test/tools/floating_point_comparison.hpp>
41#include <boost/test/unit_test.hpp>
42
43namespace Nektar
44{
45namespace BandedMatrixStoragePolicyUnitTests
46{
48
49BOOST_AUTO_TEST_CASE(TestCalculateStorageSizeAndCalculateNumberOfRows)
50{
52 BOOST_CHECK_EQUAL(16u, Policy::GetRequiredStorageSize(4, 4, 2, 1));
53 BOOST_CHECK_EQUAL(4u, Policy::CalculateNumberOfRows(4, 2, 1));
54 BOOST_CHECK_EQUAL(4u, Policy::CalculateNumberOfRows(3, 2, 1));
55}
56
57BOOST_AUTO_TEST_CASE(TestDiagonalOnlyCalculateIndex)
58{
60
61 BOOST_CHECK_EQUAL(0, Policy::CalculateIndex(3, 3, 0, 0, 0, 0));
62 BOOST_CHECK_EQUAL(1, Policy::CalculateIndex(3, 3, 1, 1, 0, 0));
63 BOOST_CHECK_EQUAL(2, Policy::CalculateIndex(3, 3, 2, 2, 0, 0));
64 BOOST_CHECK_EQUAL(0, Policy::CalculateIndex(1, 1, 0, 0, 0, 0));
65
66 BOOST_CHECK_EQUAL(std::numeric_limits<unsigned int>::max(),
67 Policy::CalculateIndex(3, 3, 0, 1, 0, 0));
68 BOOST_CHECK_EQUAL(std::numeric_limits<unsigned int>::max(),
69 Policy::CalculateIndex(3, 3, 0, 2, 0, 0));
70 BOOST_CHECK_EQUAL(std::numeric_limits<unsigned int>::max(),
71 Policy::CalculateIndex(3, 3, 1, 0, 0, 0));
72 BOOST_CHECK_EQUAL(std::numeric_limits<unsigned int>::max(),
73 Policy::CalculateIndex(3, 3, 1, 2, 0, 0));
74 BOOST_CHECK_EQUAL(std::numeric_limits<unsigned int>::max(),
75 Policy::CalculateIndex(3, 3, 2, 0, 0, 0));
76 BOOST_CHECK_EQUAL(std::numeric_limits<unsigned int>::max(),
77 Policy::CalculateIndex(3, 3, 2, 1, 0, 0));
78}
79
80BOOST_AUTO_TEST_CASE(TestSubDiagonalsOnlyCalculateIndex)
81{
83
84 BOOST_CHECK_EQUAL(0, Policy::CalculateIndex(3, 3, 0, 0, 1, 0));
85 BOOST_CHECK_EQUAL(2, Policy::CalculateIndex(3, 3, 1, 1, 1, 0));
86 BOOST_CHECK_EQUAL(4, Policy::CalculateIndex(3, 3, 2, 2, 1, 0));
87 BOOST_CHECK_EQUAL(1, Policy::CalculateIndex(3, 3, 1, 0, 1, 0));
88 BOOST_CHECK_EQUAL(3, Policy::CalculateIndex(3, 3, 2, 1, 1, 0));
89
90 BOOST_CHECK_EQUAL(std::numeric_limits<unsigned int>::max(),
91 Policy::CalculateIndex(3, 3, 0, 1, 1, 0));
92 BOOST_CHECK_EQUAL(std::numeric_limits<unsigned int>::max(),
93 Policy::CalculateIndex(3, 3, 0, 2, 1, 0));
94 BOOST_CHECK_EQUAL(std::numeric_limits<unsigned int>::max(),
95 Policy::CalculateIndex(3, 3, 2, 0, 1, 0));
96 BOOST_CHECK_EQUAL(std::numeric_limits<unsigned int>::max(),
97 Policy::CalculateIndex(3, 3, 1, 2, 1, 0));
98}
99
100BOOST_AUTO_TEST_CASE(TestSuperDiagonalsOnlyCalculateIndex)
101{
103
104 BOOST_CHECK_EQUAL(2, Policy::CalculateIndex(3, 3, 0, 0, 0, 2));
105 BOOST_CHECK_EQUAL(5, Policy::CalculateIndex(3, 3, 1, 1, 0, 2));
106 BOOST_CHECK_EQUAL(8, Policy::CalculateIndex(3, 3, 2, 2, 0, 2));
107 BOOST_CHECK_EQUAL(4, Policy::CalculateIndex(3, 3, 0, 1, 0, 2));
108 BOOST_CHECK_EQUAL(7, Policy::CalculateIndex(3, 3, 1, 2, 0, 2));
109 BOOST_CHECK_EQUAL(6, Policy::CalculateIndex(3, 3, 0, 2, 0, 2));
110
111 BOOST_CHECK_EQUAL(std::numeric_limits<unsigned int>::max(),
112 Policy::CalculateIndex(3, 3, 1, 0, 0, 2));
113 BOOST_CHECK_EQUAL(std::numeric_limits<unsigned int>::max(),
114 Policy::CalculateIndex(3, 3, 2, 0, 0, 2));
115 BOOST_CHECK_EQUAL(std::numeric_limits<unsigned int>::max(),
116 Policy::CalculateIndex(3, 3, 2, 1, 0, 2));
117}
118
119BOOST_AUTO_TEST_CASE(TestSupAndSuperDiagonalsCalculateIndex)
120{
122
123 BOOST_CHECK_EQUAL(2, Policy::CalculateIndex(4, 4, 0, 0, 1, 2));
124 BOOST_CHECK_EQUAL(5, Policy::CalculateIndex(4, 4, 0, 1, 1, 2));
125 BOOST_CHECK_EQUAL(8, Policy::CalculateIndex(4, 4, 0, 2, 1, 2));
126 BOOST_CHECK_EQUAL(std::numeric_limits<unsigned int>::max(),
127 Policy::CalculateIndex(4, 4, 0, 3, 1, 2));
128
129 BOOST_CHECK_EQUAL(3, Policy::CalculateIndex(4, 4, 1, 0, 1, 2));
130 BOOST_CHECK_EQUAL(6, Policy::CalculateIndex(4, 4, 1, 1, 1, 2));
131 BOOST_CHECK_EQUAL(9, Policy::CalculateIndex(4, 4, 1, 2, 1, 2));
132 BOOST_CHECK_EQUAL(12, Policy::CalculateIndex(4, 4, 1, 3, 1, 2));
133
134 BOOST_CHECK_EQUAL(std::numeric_limits<unsigned int>::max(),
135 Policy::CalculateIndex(4, 4, 2, 0, 1, 2));
136 BOOST_CHECK_EQUAL(7, Policy::CalculateIndex(4, 4, 2, 1, 1, 2));
137 BOOST_CHECK_EQUAL(10, Policy::CalculateIndex(4, 4, 2, 2, 1, 2));
138 BOOST_CHECK_EQUAL(13, Policy::CalculateIndex(4, 4, 2, 3, 1, 2));
139
140 BOOST_CHECK_EQUAL(std::numeric_limits<unsigned int>::max(),
141 Policy::CalculateIndex(4, 4, 3, 0, 1, 2));
142 BOOST_CHECK_EQUAL(std::numeric_limits<unsigned int>::max(),
143 Policy::CalculateIndex(4, 4, 3, 1, 1, 2));
144 BOOST_CHECK_EQUAL(11, Policy::CalculateIndex(4, 4, 3, 2, 1, 2));
145 BOOST_CHECK_EQUAL(14, Policy::CalculateIndex(4, 4, 3, 3, 1, 2));
146}
147
149{
151
152 // [ 1 2 3 0 ]
153 // [ 4 5 6 7 ]
154 // [ 0 8 9 10 ]
155 // [ 0 0 11 12 ]
156 NekDouble buf[] = {0, 0, 1, 4, 0, 2, 5, 8, 3, 6, 9, 11, 7, 10, 12, 0};
157 NekMatrix<NekDouble> m(4, 4, buf, eBANDED, 1, 2);
158
159 BOOST_CHECK_EQUAL(1, m(0, 0));
160 m.SetValue(0, 0, 19);
161 BOOST_CHECK_EQUAL(19, m(0, 0));
162 BOOST_CHECK_EQUAL(2, m(0, 1));
163 m.SetValue(0, 1, 20);
164 BOOST_CHECK_EQUAL(20, m(0, 1));
165 BOOST_CHECK_EQUAL(3, m(0, 2));
166 m.SetValue(0, 2, 21);
167 BOOST_CHECK_EQUAL(21, m(0, 2));
168 BOOST_CHECK_EQUAL(0, m(0, 3));
169 BOOST_CHECK_THROW(m.SetValue(0, 3, 21), ErrorUtil::NekError);
170
171 BOOST_CHECK_EQUAL(4, m(1, 0));
172 m.SetValue(1, 0, 22);
173 BOOST_CHECK_EQUAL(22, m(1, 0));
174 BOOST_CHECK_EQUAL(5, m(1, 1));
175 m.SetValue(1, 1, 23);
176 BOOST_CHECK_EQUAL(23, m(1, 1));
177 BOOST_CHECK_EQUAL(6, m(1, 2));
178 m.SetValue(1, 2, 24);
179 BOOST_CHECK_EQUAL(24, m(1, 2));
180 BOOST_CHECK_EQUAL(7, m(1, 3));
181 m.SetValue(1, 3, 25);
182 BOOST_CHECK_EQUAL(25, m(1, 3));
183
184 BOOST_CHECK_EQUAL(0, m(2, 0));
185 BOOST_CHECK_THROW(m.SetValue(2, 0, 21), ErrorUtil::NekError);
186 BOOST_CHECK_EQUAL(8, m(2, 1));
187 m.SetValue(2, 1, 26);
188 BOOST_CHECK_EQUAL(26, m(2, 1));
189 BOOST_CHECK_EQUAL(9, m(2, 2));
190 m.SetValue(2, 2, 27);
191 BOOST_CHECK_EQUAL(27, m(2, 2));
192 BOOST_CHECK_EQUAL(10, m(2, 3));
193 m.SetValue(2, 3, 28);
194 BOOST_CHECK_EQUAL(28, m(2, 3));
195
196 BOOST_CHECK_EQUAL(0, m(3, 0));
197 BOOST_CHECK_THROW(m.SetValue(3, 0, 21), ErrorUtil::NekError);
198 BOOST_CHECK_EQUAL(0, m(3, 1));
199 BOOST_CHECK_THROW(m.SetValue(3, 1, 21), ErrorUtil::NekError);
200 BOOST_CHECK_EQUAL(11, m(3, 2));
201 m.SetValue(3, 2, 29);
202 BOOST_CHECK_EQUAL(29, m(3, 2));
203 BOOST_CHECK_EQUAL(12, m(3, 3));
204 m.SetValue(3, 3, 30);
205 BOOST_CHECK_EQUAL(30, m(3, 3));
206}
207} // namespace BandedMatrixStoragePolicyUnitTests
208} // namespace Nektar
BOOST_AUTO_TEST_CASE(TestCalculateStorageSizeAndCalculateNumberOfRows)
void RedirectCerrIfNeeded()
Definition: util.cpp:43
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:2
double NekDouble
static unsigned int CalculateNumberOfRows(unsigned int totalRows, unsigned int subDiags, unsigned int superDiags)
Definition: MatrixFuncs.cpp:69
static unsigned int CalculateIndex(unsigned int totalRows, unsigned int totalColumns, unsigned int row, unsigned int column, unsigned int sub, unsigned int super)
Definition: MatrixFuncs.cpp:77
static unsigned int GetRequiredStorageSize(unsigned int totalRows, unsigned int totalColumns, unsigned int subDiags, unsigned int superDiags)
Calculates and returns the storage size required.
Definition: MatrixFuncs.cpp:44