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
44{
46
47BOOST_AUTO_TEST_CASE(TestCalculateStorageSizeAndCalculateNumberOfRows)
48{
50 BOOST_CHECK_EQUAL(16u, Policy::GetRequiredStorageSize(4, 4, 2, 1));
51 BOOST_CHECK_EQUAL(4u, Policy::CalculateNumberOfRows(4, 2, 1));
52 BOOST_CHECK_EQUAL(4u, Policy::CalculateNumberOfRows(3, 2, 1));
53}
54
55BOOST_AUTO_TEST_CASE(TestDiagonalOnlyCalculateIndex)
56{
58
59 BOOST_CHECK_EQUAL(0, Policy::CalculateIndex(3, 3, 0, 0, 0, 0));
60 BOOST_CHECK_EQUAL(1, Policy::CalculateIndex(3, 3, 1, 1, 0, 0));
61 BOOST_CHECK_EQUAL(2, Policy::CalculateIndex(3, 3, 2, 2, 0, 0));
62 BOOST_CHECK_EQUAL(0, Policy::CalculateIndex(1, 1, 0, 0, 0, 0));
63
64 BOOST_CHECK_EQUAL(std::numeric_limits<unsigned int>::max(),
65 Policy::CalculateIndex(3, 3, 0, 1, 0, 0));
66 BOOST_CHECK_EQUAL(std::numeric_limits<unsigned int>::max(),
67 Policy::CalculateIndex(3, 3, 0, 2, 0, 0));
68 BOOST_CHECK_EQUAL(std::numeric_limits<unsigned int>::max(),
69 Policy::CalculateIndex(3, 3, 1, 0, 0, 0));
70 BOOST_CHECK_EQUAL(std::numeric_limits<unsigned int>::max(),
71 Policy::CalculateIndex(3, 3, 1, 2, 0, 0));
72 BOOST_CHECK_EQUAL(std::numeric_limits<unsigned int>::max(),
73 Policy::CalculateIndex(3, 3, 2, 0, 0, 0));
74 BOOST_CHECK_EQUAL(std::numeric_limits<unsigned int>::max(),
75 Policy::CalculateIndex(3, 3, 2, 1, 0, 0));
76}
77
78BOOST_AUTO_TEST_CASE(TestSubDiagonalsOnlyCalculateIndex)
79{
81
82 BOOST_CHECK_EQUAL(0, Policy::CalculateIndex(3, 3, 0, 0, 1, 0));
83 BOOST_CHECK_EQUAL(2, Policy::CalculateIndex(3, 3, 1, 1, 1, 0));
84 BOOST_CHECK_EQUAL(4, Policy::CalculateIndex(3, 3, 2, 2, 1, 0));
85 BOOST_CHECK_EQUAL(1, Policy::CalculateIndex(3, 3, 1, 0, 1, 0));
86 BOOST_CHECK_EQUAL(3, Policy::CalculateIndex(3, 3, 2, 1, 1, 0));
87
88 BOOST_CHECK_EQUAL(std::numeric_limits<unsigned int>::max(),
89 Policy::CalculateIndex(3, 3, 0, 1, 1, 0));
90 BOOST_CHECK_EQUAL(std::numeric_limits<unsigned int>::max(),
91 Policy::CalculateIndex(3, 3, 0, 2, 1, 0));
92 BOOST_CHECK_EQUAL(std::numeric_limits<unsigned int>::max(),
93 Policy::CalculateIndex(3, 3, 2, 0, 1, 0));
94 BOOST_CHECK_EQUAL(std::numeric_limits<unsigned int>::max(),
95 Policy::CalculateIndex(3, 3, 1, 2, 1, 0));
96}
97
98BOOST_AUTO_TEST_CASE(TestSuperDiagonalsOnlyCalculateIndex)
99{
101
102 BOOST_CHECK_EQUAL(2, Policy::CalculateIndex(3, 3, 0, 0, 0, 2));
103 BOOST_CHECK_EQUAL(5, Policy::CalculateIndex(3, 3, 1, 1, 0, 2));
104 BOOST_CHECK_EQUAL(8, Policy::CalculateIndex(3, 3, 2, 2, 0, 2));
105 BOOST_CHECK_EQUAL(4, Policy::CalculateIndex(3, 3, 0, 1, 0, 2));
106 BOOST_CHECK_EQUAL(7, Policy::CalculateIndex(3, 3, 1, 2, 0, 2));
107 BOOST_CHECK_EQUAL(6, Policy::CalculateIndex(3, 3, 0, 2, 0, 2));
108
109 BOOST_CHECK_EQUAL(std::numeric_limits<unsigned int>::max(),
110 Policy::CalculateIndex(3, 3, 1, 0, 0, 2));
111 BOOST_CHECK_EQUAL(std::numeric_limits<unsigned int>::max(),
112 Policy::CalculateIndex(3, 3, 2, 0, 0, 2));
113 BOOST_CHECK_EQUAL(std::numeric_limits<unsigned int>::max(),
114 Policy::CalculateIndex(3, 3, 2, 1, 0, 2));
115}
116
117BOOST_AUTO_TEST_CASE(TestSupAndSuperDiagonalsCalculateIndex)
118{
120
121 BOOST_CHECK_EQUAL(2, Policy::CalculateIndex(4, 4, 0, 0, 1, 2));
122 BOOST_CHECK_EQUAL(5, Policy::CalculateIndex(4, 4, 0, 1, 1, 2));
123 BOOST_CHECK_EQUAL(8, Policy::CalculateIndex(4, 4, 0, 2, 1, 2));
124 BOOST_CHECK_EQUAL(std::numeric_limits<unsigned int>::max(),
125 Policy::CalculateIndex(4, 4, 0, 3, 1, 2));
126
127 BOOST_CHECK_EQUAL(3, Policy::CalculateIndex(4, 4, 1, 0, 1, 2));
128 BOOST_CHECK_EQUAL(6, Policy::CalculateIndex(4, 4, 1, 1, 1, 2));
129 BOOST_CHECK_EQUAL(9, Policy::CalculateIndex(4, 4, 1, 2, 1, 2));
130 BOOST_CHECK_EQUAL(12, Policy::CalculateIndex(4, 4, 1, 3, 1, 2));
131
132 BOOST_CHECK_EQUAL(std::numeric_limits<unsigned int>::max(),
133 Policy::CalculateIndex(4, 4, 2, 0, 1, 2));
134 BOOST_CHECK_EQUAL(7, Policy::CalculateIndex(4, 4, 2, 1, 1, 2));
135 BOOST_CHECK_EQUAL(10, Policy::CalculateIndex(4, 4, 2, 2, 1, 2));
136 BOOST_CHECK_EQUAL(13, Policy::CalculateIndex(4, 4, 2, 3, 1, 2));
137
138 BOOST_CHECK_EQUAL(std::numeric_limits<unsigned int>::max(),
139 Policy::CalculateIndex(4, 4, 3, 0, 1, 2));
140 BOOST_CHECK_EQUAL(std::numeric_limits<unsigned int>::max(),
141 Policy::CalculateIndex(4, 4, 3, 1, 1, 2));
142 BOOST_CHECK_EQUAL(11, Policy::CalculateIndex(4, 4, 3, 2, 1, 2));
143 BOOST_CHECK_EQUAL(14, Policy::CalculateIndex(4, 4, 3, 3, 1, 2));
144}
145
147{
149
150 // [ 1 2 3 0 ]
151 // [ 4 5 6 7 ]
152 // [ 0 8 9 10 ]
153 // [ 0 0 11 12 ]
154 NekDouble buf[] = {0, 0, 1, 4, 0, 2, 5, 8, 3, 6, 9, 11, 7, 10, 12, 0};
155 NekMatrix<NekDouble> m(4, 4, buf, eBANDED, 1, 2);
156
157 BOOST_CHECK_EQUAL(1, m(0, 0));
158 m.SetValue(0, 0, 19);
159 BOOST_CHECK_EQUAL(19, m(0, 0));
160 BOOST_CHECK_EQUAL(2, m(0, 1));
161 m.SetValue(0, 1, 20);
162 BOOST_CHECK_EQUAL(20, m(0, 1));
163 BOOST_CHECK_EQUAL(3, m(0, 2));
164 m.SetValue(0, 2, 21);
165 BOOST_CHECK_EQUAL(21, m(0, 2));
166 BOOST_CHECK_EQUAL(0, m(0, 3));
167 BOOST_CHECK_THROW(m.SetValue(0, 3, 21), ErrorUtil::NekError);
168
169 BOOST_CHECK_EQUAL(4, m(1, 0));
170 m.SetValue(1, 0, 22);
171 BOOST_CHECK_EQUAL(22, m(1, 0));
172 BOOST_CHECK_EQUAL(5, m(1, 1));
173 m.SetValue(1, 1, 23);
174 BOOST_CHECK_EQUAL(23, m(1, 1));
175 BOOST_CHECK_EQUAL(6, m(1, 2));
176 m.SetValue(1, 2, 24);
177 BOOST_CHECK_EQUAL(24, m(1, 2));
178 BOOST_CHECK_EQUAL(7, m(1, 3));
179 m.SetValue(1, 3, 25);
180 BOOST_CHECK_EQUAL(25, m(1, 3));
181
182 BOOST_CHECK_EQUAL(0, m(2, 0));
183 BOOST_CHECK_THROW(m.SetValue(2, 0, 21), ErrorUtil::NekError);
184 BOOST_CHECK_EQUAL(8, m(2, 1));
185 m.SetValue(2, 1, 26);
186 BOOST_CHECK_EQUAL(26, m(2, 1));
187 BOOST_CHECK_EQUAL(9, m(2, 2));
188 m.SetValue(2, 2, 27);
189 BOOST_CHECK_EQUAL(27, m(2, 2));
190 BOOST_CHECK_EQUAL(10, m(2, 3));
191 m.SetValue(2, 3, 28);
192 BOOST_CHECK_EQUAL(28, m(2, 3));
193
194 BOOST_CHECK_EQUAL(0, m(3, 0));
195 BOOST_CHECK_THROW(m.SetValue(3, 0, 21), ErrorUtil::NekError);
196 BOOST_CHECK_EQUAL(0, m(3, 1));
197 BOOST_CHECK_THROW(m.SetValue(3, 1, 21), ErrorUtil::NekError);
198 BOOST_CHECK_EQUAL(11, m(3, 2));
199 m.SetValue(3, 2, 29);
200 BOOST_CHECK_EQUAL(29, m(3, 2));
201 BOOST_CHECK_EQUAL(12, m(3, 3));
202 m.SetValue(3, 3, 30);
203 BOOST_CHECK_EQUAL(30, m(3, 3));
204}
205} // namespace Nektar::BandedMatrixStoragePolicyUnitTests
BOOST_AUTO_TEST_CASE(TestCalculateStorageSizeAndCalculateNumberOfRows)
void RedirectCerrIfNeeded()
Definition: util.cpp:41
double NekDouble
static unsigned int CalculateNumberOfRows(unsigned int totalRows, unsigned int subDiags, unsigned int superDiags)
Definition: MatrixFuncs.cpp:67
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:75
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:42