Nektar++
Loading...
Searching...
No Matches
library
UnitTests
SolverCore
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
44
#include <
LibUtilities/Backends/Backends.hpp
>
45
#include <
LibUtilities/BasicUtils/Field/Field.hpp
>
46
#include <
LibUtilities/BasicUtils/SessionReader.h
>
47
#include <
MultiRegions/ContField.h
>
48
#include <
SpatialDomains/MeshGraphIO.h
>
49
50
#include <
UnitTests/TestBoostSetup.hpp
>
51
#include <
UnitTests/TestGlobalConfiguration.hpp
>
52
53
#include <cmath>
54
#include <cstdio>
55
#include <cstdlib>
56
#include <cstring>
57
#include <iostream>
58
#include <string>
59
#include <vector>
60
61
using namespace
Nektar
;
62
using namespace
Nektar::LibUtilities
;
63
using namespace
Nektar::MultiRegions
;
64
65
NEKTAR_TEST_GLOBAL_CONFIGURATION
(
Nektar::UnitTests::TestArgs::ExecAndImpl
);
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
*/
82
template
<
typename
TData>
class
TestLinearSolver
83
{
84
public
:
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
110
if
(
m_expected
.
GetNumComponents
() !=
m_out
.
GetNumComponents
())
111
{
112
std::cout <<
"Mismatch of number of components."
<< std::endl;
113
return
false
;
114
}
115
116
if
(
m_expected
.
GetNumHomoModes
() !=
m_out
.
GetNumHomoModes
())
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;
160
n <
m_out
.
GetNumComponents
() *
m_out
.
GetNumHomoModes
(); ++n)
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
199
protected
:
200
std::string
m_meshName
=
""
;
201
SessionReaderSharedPtr
m_session
;
202
ExpListSharedPtr
m_expList
;
203
Field<TData, FieldState::Phys>
m_in
;
204
Field<TData, FieldState::Coeff>
m_out
;
205
Field<TData, FieldState::Coeff>
m_expected
;
206
207
void
SetSession
()
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
224
m_session
=
SessionReader::CreateInstance
(argc, argv);
225
226
for
(
int
i = 0; i < argc; ++i)
227
{
228
free(argv[i]);
229
}
230
delete
[] argv;
231
}
232
233
void
SetExpList
()
234
{
235
auto
graph
=
SpatialDomains::MeshGraphIO::Read
(
m_session
);
236
237
m_expList
=
MemoryManager<ContField>::AllocateSharedPtr
(
238
m_session
,
graph
,
"u"
,
true
,
false
,
Collections::eNoCollection
);
239
m_expList
->SetDataWarehouse();
240
}
241
242
void
SetFixture
()
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
258
#include <
UnitTests/TestBoostTeardown.hpp
>
Backends.hpp
ContField.h
Field.hpp
MeshGraphIO.h
SessionReader.h
TestBoostSetup.hpp
TestBoostTeardown.hpp
graph
SpatialDomains::MeshGraphSharedPtr graph
Definition
TestContFieldLocalToGlobalMap.cpp:224
TestGlobalConfiguration.hpp
NEKTAR_TEST_GLOBAL_CONFIGURATION
#define NEKTAR_TEST_GLOBAL_CONFIGURATION(args)
Definition
TestGlobalConfiguration.hpp:227
rank
int rank
Definition
TestSharedIdResolvers.cpp:326
el
SpatialDomains::Geometry * el
Definition
TestVarOptiDerivatives.cpp:96
Nektar::LibUtilities::Field
A Field represents expansion data to be operated on.
Definition
LibUtilities/BasicUtils/Field/Field.hpp:54
Nektar::LibUtilities::Field::GetBlocks
std::vector< BlockAccessor< TData, TState > > & GetBlocks()
Get BlockAccessors for the field.
Definition
LibUtilities/BasicUtils/Field/Field.hpp:603
Nektar::LibUtilities::Field::GetNumHomoModes
unsigned int GetNumHomoModes() const
Gets the number of homogeneous modes.
Definition
LibUtilities/BasicUtils/Field/Field.hpp:693
Nektar::LibUtilities::Field::GetNumComponents
unsigned int GetNumComponents() const
Gets the number of components for a vector field.
Definition
LibUtilities/BasicUtils/Field/Field.hpp:659
Nektar::LibUtilities::SessionReader::CreateInstance
static SessionReaderSharedPtr CreateInstance(int argc, char *argv[])
Creates an instance of the SessionReader class.
Definition
SessionReader.h:146
Nektar::MemoryManager::AllocateSharedPtr
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
Definition
NekMemoryManager.hpp:167
Nektar::SpatialDomains::MeshGraphIO::Read
static MeshGraphSharedPtr Read(const LibUtilities::SessionReaderSharedPtr pSession, LibUtilities::DomainRangeShPtr rng=LibUtilities::NullDomainRangeShPtr, bool fillGraph=true, SpatialDomains::MeshGraphSharedPtr partitionedGraph=nullptr)
Definition
MeshGraphIO.cpp:65
TestLinearSolver
Definition
TestLinearSolver.hpp:83
TestLinearSolver::Compare
bool Compare(TData tol)
Compare the computed field to the expected one, with absolute tolerance tol. The two must have the sa...
Definition
TestLinearSolver.hpp:106
TestLinearSolver::m_out
Field< TData, FieldState::Coeff > m_out
Definition
TestLinearSolver.hpp:204
TestLinearSolver::SetExpList
void SetExpList()
Definition
TestLinearSolver.hpp:233
TestLinearSolver::Configure
void Configure()
Read the session, build the expansion list and allocate the fields the test cases work on.
Definition
TestLinearSolver.hpp:92
TestLinearSolver::~TestLinearSolver
virtual ~TestLinearSolver()=default
TestLinearSolver::m_in
Field< TData, FieldState::Phys > m_in
Definition
TestLinearSolver.hpp:203
TestLinearSolver::SetSession
void SetSession()
Definition
TestLinearSolver.hpp:207
TestLinearSolver::TestLinearSolver
TestLinearSolver()=default
TestLinearSolver::m_meshName
std::string m_meshName
Definition
TestLinearSolver.hpp:200
TestLinearSolver::SetFixture
void SetFixture()
Definition
TestLinearSolver.hpp:242
TestLinearSolver::m_expList
ExpListSharedPtr m_expList
Definition
TestLinearSolver.hpp:202
TestLinearSolver::m_session
SessionReaderSharedPtr m_session
Definition
TestLinearSolver.hpp:201
TestLinearSolver::m_expected
Field< TData, FieldState::Coeff > m_expected
Definition
TestLinearSolver.hpp:205
Nektar::Collections::eNoCollection
@ eNoCollection
Definition
Operator.h:89
Nektar::LibUtilities
Definition
GitRevision.h:54
Nektar::LibUtilities::SessionReaderSharedPtr
std::shared_ptr< SessionReader > SessionReaderSharedPtr
Definition
SessionReader.h:127
Nektar::MultiRegions
Definition
BlockOpShapeBlock.cpp.in:78
Nektar::MultiRegions::ExpListSharedPtr
std::shared_ptr< ExpList > ExpListSharedPtr
Shared pointer to an ExpList object.
Definition
LocTraceToTraceMap.h:56
Nektar::UnitTests::TestArgs::ExecAndImpl
@ ExecAndImpl
exe – ExecName ImplName
Nektar
Definition
CoupledSolver.h:2
Generated on Tue Dec 1 2026 10:16:14 for Nektar++ by
1.9.8