Nektar++
Loading...
Searching...
No Matches
TestLinearSolver.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: TestLinearSolver.hpp
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: Fixture base shared by the linear system unit tests -
32// FwdTrans, HelmSolve, PoissonSolve and LinearADRSolve. It owns the session,
33// the expansion list and the physical input / coefficient output fields they
34// all work on, and compares the computed field against an expected one.
35//
36// This carries only what those four tests use. In particular there is no
37// homogeneous (3DH1/3DH2) setup and no plain ExpList branch: every one of
38// them solves a global system on a ContField.
39//
40///////////////////////////////////////////////////////////////////////////////
41
42#pragma once
43
49
52
53#include <cmath>
54#include <cstdio>
55#include <cstdlib>
56#include <cstring>
57#include <iostream>
58#include <string>
59#include <vector>
60
61using namespace Nektar;
62using namespace Nektar::LibUtilities;
63using namespace Nektar::MultiRegions;
64
66
67/**
68 * @class TestLinearSolver
69 *
70 * A test fixture holding the session, expansion list and fields that the
71 * linear system operators are exercised on: a physical space input, a
72 * coefficient space output, and the coefficient space result the operator is
73 * expected to produce.
74 *
75 * The fixture constructor (destructor) is called before (after) each call to
76 * the BOOST_FIXTURE_TEST_CASE(<test name>, <fixture>) macro.
77 *
78 * See "Single test case fixture" on the Boost.Test documentation for more
79 * details:
80 * https://www.boost.org/doc/libs/1_82_0/libs/test/doc/html/boost_test/tests_organization/fixtures/case.html
81 */
82template <typename TData> class TestLinearSolver
83{
84public:
85 TestLinearSolver() = default;
86 virtual ~TestLinearSolver() = default;
87
88 /**
89 * @brief Read the session, build the expansion list and allocate the
90 * fields the test cases work on.
91 */
92 void Configure()
93 {
94 SetSession();
95 SetExpList();
96 SetFixture();
97 }
98
99 /**
100 * @brief Compare the computed field to the expected one, with absolute
101 * tolerance tol. The two must have the same blocks and components; their
102 * interleave widths may differ.
103 *
104 * @return bool
105 */
106 bool Compare(TData tol)
107 {
108 auto rank = m_session->GetComm()->GetRank();
109
111 {
112 std::cout << "Mismatch of number of components." << std::endl;
113 return false;
114 }
115
117 {
118 std::cout << "Mismatch of number of homogeneous modes."
119 << std::endl;
120 return false;
121 }
122
123 if (m_expected.GetBlocks().size() != m_out.GetBlocks().size())
124 {
125 std::cout << "Mismatch of block size." << std::endl;
126 return false;
127 }
128
129 // ToVector() returns every component element by element, whatever
130 // the interleave width of each block, and leaves the fields as they
131 // are.
132 const std::vector<TData> out = m_out.template ToVector<TData>();
133 const std::vector<TData> expected =
134 m_expected.template ToVector<TData>();
135 const size_t compSize =
136 out.size() / (m_out.GetNumComponents() * m_out.GetNumHomoModes());
137
138 bool isMatch = true;
139
140 if (rank == 0)
141 {
142 printf("#elm #pts output expected "
143 "difference\n");
144 }
145 size_t blkOffset = 0;
146 for (unsigned int blk = 0; blk < m_out.GetBlocks().size(); ++blk)
147 {
148 const auto &outBlock = m_out.GetBlocks()[blk];
149 const auto &expBlock = m_expected.GetBlocks()[blk];
150 if ((outBlock.GetNumElements() != expBlock.GetNumElements()) ||
151 (outBlock.GetNumData() != expBlock.GetNumData()))
152 {
153 std::cout << "Mismatch of block structure." << std::endl;
154 return false;
155 }
156 const size_t nElmts = outBlock.GetNumElements();
157 const unsigned int nPts = outBlock.GetNumData();
158
159 for (unsigned int n = 0;
161 {
162 const TData *outptr = out.data() + n * compSize + blkOffset;
163 const TData *expptr =
164 expected.data() + n * compSize + blkOffset;
165 size_t MisMatchcnt = 0, total = 0;
166
167 for (size_t el = 0, cnt = 0; el < nElmts; ++el)
168 {
169 for (unsigned int pts = 0; pts < nPts; ++pts, ++cnt)
170 {
171 if (std::isnan(outptr[cnt]) ||
172 std::isinf(outptr[cnt]) ||
173 std::abs(outptr[cnt] - expptr[cnt]) > tol)
174 {
175 printf("%04lu %04u %20.16f %20.16f %20.16f\n", el,
176 pts, outptr[cnt], expptr[cnt],
177 std::abs(outptr[cnt] - expptr[cnt]));
178 MisMatchcnt++;
179 }
180 total++;
181 }
182 }
183
184 if (MisMatchcnt)
185 {
186 std::cout << "Number of mismatches in component " << n
187 << " on block " << blk << " is " << MisMatchcnt
188 << " out of " << total << " on rank: " << rank
189 << std::endl;
190 isMatch = false;
191 }
192 }
193 blkOffset += nElmts * nPts;
194 }
195
196 return isMatch;
197 }
198
199protected:
200 std::string m_meshName = "";
206
208 {
209 std::string execStr(GlobalConfiguration::ExecStr());
210 std::string implStr(GlobalConfiguration::ImplStr());
211
212 BOOST_TEST_MESSAGE("Creating input and output fields");
213
214 // Construct a fake command-line argument array to be fed to
215 // Session::Reader::CreateInstance. The first element stands for
216 // the name of the executable which, in our case, doesn't matter.
217 int argc = 4;
218 char **argv = new char *[argc];
219 argv[0] = strdup("exe_name");
220 argv[1] = strdup(m_meshName.data());
221 argv[2] = strdup(("--opExecSpace=" + execStr).c_str());
222 argv[3] = strdup(("--opImpl=" + implStr).c_str());
223
225
226 for (int i = 0; i < argc; ++i)
227 {
228 free(argv[i]);
229 }
230 delete[] argv;
231 }
232
241
243 {
244 const unsigned int ncomp = m_session->GetVariables().size();
245 auto inBlockAttr = GetBlockAttributes<TData, FieldState::Phys>(
246 m_expList, MultiRegions::UnitInterleaveWidths(m_expList));
247 auto outBlockAttr = GetBlockAttributes<TData, FieldState::Coeff>(
248 m_expList, MultiRegions::UnitInterleaveWidths(m_expList));
249
250 m_in = Field<TData, FieldState::Phys>("f_in", inBlockAttr, ncomp, 1);
251 m_out =
252 Field<TData, FieldState::Coeff>("f_out", outBlockAttr, ncomp, 1);
253 m_expected = Field<TData, FieldState::Coeff>("f_expected", outBlockAttr,
254 ncomp, 1);
255 }
256};
257
SpatialDomains::MeshGraphSharedPtr graph
#define NEKTAR_TEST_GLOBAL_CONFIGURATION(args)
SpatialDomains::Geometry * el
A Field represents expansion data to be operated on.
std::vector< BlockAccessor< TData, TState > > & GetBlocks()
Get BlockAccessors for the field.
unsigned int GetNumHomoModes() const
Gets the number of homogeneous modes.
unsigned int GetNumComponents() const
Gets the number of components for a vector field.
static SessionReaderSharedPtr CreateInstance(int argc, char *argv[])
Creates an instance of the SessionReader class.
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
static MeshGraphSharedPtr Read(const LibUtilities::SessionReaderSharedPtr pSession, LibUtilities::DomainRangeShPtr rng=LibUtilities::NullDomainRangeShPtr, bool fillGraph=true, SpatialDomains::MeshGraphSharedPtr partitionedGraph=nullptr)
bool Compare(TData tol)
Compare the computed field to the expected one, with absolute tolerance tol. The two must have the sa...
Field< TData, FieldState::Coeff > m_out
void Configure()
Read the session, build the expansion list and allocate the fields the test cases work on.
virtual ~TestLinearSolver()=default
Field< TData, FieldState::Phys > m_in
TestLinearSolver()=default
ExpListSharedPtr m_expList
SessionReaderSharedPtr m_session
Field< TData, FieldState::Coeff > m_expected
std::shared_ptr< SessionReader > SessionReaderSharedPtr
std::shared_ptr< ExpList > ExpListSharedPtr
Shared pointer to an ExpList object.
@ ExecAndImpl
exe – ExecName ImplName