Nektar++
Loading...
Searching...
No Matches
TestContFieldLocalToGlobalMap.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: TestContFieldLocalToGlobalMap.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: Unit tests for the assembly map of another variable on a
32// continuous field, ContField::GetLocalToGlobalMap(variable).
33//
34///////////////////////////////////////////////////////////////////////////////
35
41
42#include <boost/test/unit_test.hpp>
43
44#include <fstream>
45#include <string>
46
48{
49namespace
50{
51/**
52 * Four quadrilaterals over the unit square, with the boundary split into its
53 * four sides: region 0 at y = 0, 1 at x = 1, 2 at y = 1 and 3 at x = 0. All
54 * variables share one expansion, so that the field of one can be copied for
55 * another.
56 *
57 * The global system is solved iteratively on the full matrix, as the
58 * redesign solvers do, so the maps number the degrees of freedom without
59 * reordering. A reordering for static condensation draws on the random state
60 * of the graph library, which earlier maps in the same process advance, and
61 * two maps built at different times could then be numbered differently.
62 */
63const std::string sessionTemplate =
64 R"END(<?xml version="1.0" encoding="UTF-8"?>
65<NEKTAR>
66 <GEOMETRY DIM="2" SPACE="2">
67 <VERTEX>
68 <V ID="0"> 0.0 0.0 0.0 </V>
69 <V ID="1"> 0.5 0.0 0.0 </V>
70 <V ID="2"> 1.0 0.0 0.0 </V>
71 <V ID="3"> 0.0 0.5 0.0 </V>
72 <V ID="4"> 0.5 0.5 0.0 </V>
73 <V ID="5"> 1.0 0.5 0.0 </V>
74 <V ID="6"> 0.0 1.0 0.0 </V>
75 <V ID="7"> 0.5 1.0 0.0 </V>
76 <V ID="8"> 1.0 1.0 0.0 </V>
77 </VERTEX>
78 <EDGE>
79 <E ID="0"> 0 1 </E>
80 <E ID="1"> 1 2 </E>
81 <E ID="2"> 0 3 </E>
82 <E ID="3"> 1 4 </E>
83 <E ID="4"> 2 5 </E>
84 <E ID="5"> 3 4 </E>
85 <E ID="6"> 4 5 </E>
86 <E ID="7"> 3 6 </E>
87 <E ID="8"> 4 7 </E>
88 <E ID="9"> 5 8 </E>
89 <E ID="10"> 6 7 </E>
90 <E ID="11"> 7 8 </E>
91 </EDGE>
92 <ELEMENT>
93 <Q ID="0"> 0 3 5 2 </Q>
94 <Q ID="1"> 1 4 6 3 </Q>
95 <Q ID="2"> 5 8 10 7 </Q>
96 <Q ID="3"> 6 9 11 8 </Q>
97 </ELEMENT>
98 <COMPOSITE>
99 <C ID="0"> Q[0-3] </C>
100 <C ID="1"> E[0-1] </C>
101 <C ID="2"> E[4,9] </C>
102 <C ID="3"> E[10-11] </C>
103 <C ID="4"> E[2,7] </C>
104 </COMPOSITE>
105 <DOMAIN> C[0] </DOMAIN>
106 </GEOMETRY>
107
108 <EXPANSIONS>
109 <E COMPOSITE="C[0]" NUMMODES="4" TYPE="MODIFIED" FIELDS="@FIELDS@" />
110 </EXPANSIONS>
111
112 <CONDITIONS>
113 <SOLVERINFO>
114 <I PROPERTY="GlobalSysSoln" VALUE="IterativeFull" />
115 </SOLVERINFO>
116 <VARIABLES>
117@VARIABLES@
118 </VARIABLES>
119 <BOUNDARYREGIONS>
120 <B ID="0"> C[1] </B>
121 <B ID="1"> C[2] </B>
122 <B ID="2"> C[3] </B>
123 <B ID="3"> C[4] </B>
124 </BOUNDARYREGIONS>
125 <BOUNDARYCONDITIONS>
126@CONDITIONS@
127 </BOUNDARYCONDITIONS>
128 </CONDITIONS>
129</NEKTAR>
130)END";
131
132/**
133 * u and v are Dirichlet everywhere; w is Dirichlet at y = 0 and y = 1 only,
134 * so its map differs from u's; p and q are Neumann everywhere, so a field
135 * that checks for a singular system pins a degree of freedom.
136 */
137const std::string variables = R"END(
138 <V ID="0"> u </V>
139 <V ID="1"> v </V>
140 <V ID="2"> w </V>
141 <V ID="3"> p </V>
142 <V ID="4"> q </V>)END";
143
144const std::string conditions = R"END(
145 <REGION REF="0">
146 <D VAR="u" VALUE="0" />
147 <D VAR="v" VALUE="0" />
148 <D VAR="w" VALUE="0" />
149 <N VAR="p" VALUE="0" />
150 <N VAR="q" VALUE="0" />
151 </REGION>
152 <REGION REF="1">
153 <D VAR="u" VALUE="0" />
154 <D VAR="v" VALUE="0" />
155 <N VAR="w" VALUE="0" />
156 <N VAR="p" VALUE="0" />
157 <N VAR="q" VALUE="0" />
158 </REGION>
159 <REGION REF="2">
160 <D VAR="u" VALUE="0" />
161 <D VAR="v" VALUE="0" />
162 <D VAR="w" VALUE="0" />
163 <N VAR="p" VALUE="0" />
164 <N VAR="q" VALUE="0" />
165 </REGION>
166 <REGION REF="3">
167 <D VAR="u" VALUE="0" />
168 <D VAR="v" VALUE="0" />
169 <N VAR="w" VALUE="0" />
170 <N VAR="p" VALUE="0" />
171 <N VAR="q" VALUE="0" />
172 </REGION>)END";
173
174/**
175 * Periodic in x. u and v are Dirichlet at y = 0 and y = 1; w is Dirichlet
176 * at y = 0 only.
177 */
178const std::string periodicVariables = R"END(
179 <V ID="0"> u </V>
180 <V ID="1"> v </V>
181 <V ID="2"> w </V>)END";
182
183const std::string periodicConditions = R"END(
184 <REGION REF="0">
185 <D VAR="u" VALUE="0" />
186 <D VAR="v" VALUE="0" />
187 <D VAR="w" VALUE="0" />
188 </REGION>
189 <REGION REF="1">
190 <P VAR="u" VALUE="[3]" />
191 <P VAR="v" VALUE="[3]" />
192 <P VAR="w" VALUE="[3]" />
193 </REGION>
194 <REGION REF="2">
195 <D VAR="u" VALUE="0" />
196 <D VAR="v" VALUE="0" />
197 <N VAR="w" VALUE="0" />
198 </REGION>
199 <REGION REF="3">
200 <P VAR="u" VALUE="[1]" />
201 <P VAR="v" VALUE="[1]" />
202 <P VAR="w" VALUE="[1]" />
203 </REGION>)END";
204
205/// Substitute every occurrence of @p token in @p text with @p value.
206void Substitute(std::string &text, const std::string &token,
207 const std::string &value)
208{
209 for (std::size_t at = text.find(token); at != std::string::npos;
210 at = text.find(token))
211 {
212 text.replace(at, token.size(), value);
213 }
214}
215
216/**
217 * Writes the session for @p fields with the given variables and boundary
218 * conditions to a directory of its own, and reads it and its mesh.
219 */
220struct Fixture
221{
222 fs::path directory;
225
226 Fixture(const std::string &fields, const std::string &variableBlock,
227 const std::string &conditionBlock)
228 {
229 std::string sessionFile = sessionTemplate;
230 Substitute(sessionFile, "@FIELDS@", fields);
231 Substitute(sessionFile, "@VARIABLES@", variableBlock);
232 Substitute(sessionFile, "@CONDITIONS@", conditionBlock);
233
234 directory = fs::temp_directory_path() /
235 LibUtilities::UniquePath("contfieldmaps");
236 fs::create_directories(directory);
237 fs::path path = directory / "TestContFieldLocalToGlobalMap.xml";
238
239 std::ofstream stream(path);
240 stream << sessionFile;
241 stream.close();
242
243 std::string pathString = path.string();
244 char executable[] = "MultiRegionsUnitTests";
245 char *argv[] = {executable, pathString.data(), nullptr};
248 }
249
250 ~Fixture()
251 {
252 fs::remove_all(directory);
253 }
254
255 /// A continuous field for @p variable, built from the session.
256 MultiRegions::ContFieldSharedPtr Field(const std::string &variable,
257 const bool checkIfSingular = false)
258 {
260 session, graph, variable, true, checkIfSingular);
261 }
262};
263
264/**
265 * Checks that @p actual holds the same numbering, boundary condition maps and
266 * Dirichlet data as @p expected, which a field built from the session for the
267 * same variable owns.
268 */
269void CheckSameMap(const MultiRegions::AssemblyMapCGSharedPtr &actual,
271{
272 BOOST_CHECK_EQUAL(actual->GetVariable(), expected->GetVariable());
273 BOOST_CHECK_EQUAL(actual->GetNumGlobalCoeffs(),
274 expected->GetNumGlobalCoeffs());
275 BOOST_CHECK_EQUAL(actual->GetNumGlobalDirBndCoeffs(),
276 expected->GetNumGlobalDirBndCoeffs());
277 BOOST_CHECK_EQUAL(actual->GetNumLocalDirBndCoeffs(),
278 expected->GetNumLocalDirBndCoeffs());
279 BOOST_CHECK_EQUAL(actual->GetSingularSystem(),
280 expected->GetSingularSystem());
281
282 const auto &l2g = actual->GetLocalToGlobalMap();
283 const auto &expectedL2g = expected->GetLocalToGlobalMap();
284 BOOST_CHECK_EQUAL_COLLECTIONS(l2g.begin(), l2g.end(), expectedL2g.begin(),
285 expectedL2g.end());
286
287 const auto &bndMap = actual->GetBndCondCoeffsToLocalCoeffsMap();
288 const auto &expectedBndMap = expected->GetBndCondCoeffsToLocalCoeffsMap();
289 BOOST_CHECK_EQUAL_COLLECTIONS(bndMap.begin(), bndMap.end(),
290 expectedBndMap.begin(), expectedBndMap.end());
291
292 const auto &bndSign = actual->GetBndCondCoeffsToLocalCoeffsSign();
293 const auto &expectedBndSign = expected->GetBndCondCoeffsToLocalCoeffsSign();
294 BOOST_CHECK_EQUAL_COLLECTIONS(bndSign.begin(), bndSign.end(),
295 expectedBndSign.begin(),
296 expectedBndSign.end());
297
298 BOOST_CHECK_EQUAL(actual->GetSignChange(), expected->GetSignChange());
299
300 BOOST_CHECK(actual->GetCopyLocalDirDofs() ==
301 expected->GetCopyLocalDirDofs());
302 BOOST_CHECK(actual->GetParallelDirBndSign() ==
303 expected->GetParallelDirBndSign());
304
305 const auto &paraDirBnd = actual->GetParaDirBnd();
306 const auto &expectedParaDirBnd = expected->GetParaDirBnd();
307 BOOST_CHECK_EQUAL_COLLECTIONS(paraDirBnd.begin(), paraDirBnd.end(),
308 expectedParaDirBnd.begin(),
309 expectedParaDirBnd.end());
310}
311} // namespace
312
313/**
314 * A variable with the same types of boundary conditions shares the field's
315 * own map, and the field's own variable returns it directly.
316 */
317BOOST_AUTO_TEST_CASE(TestContFieldLocalToGlobalMapSameTypes)
318{
319 Fixture fixture("u,v,w,p,q", variables, conditions);
320 BOOST_REQUIRE(fixture.graph->SameExpansionInfo("u", "v"));
321
322 auto field = fixture.Field("u");
324 field->GetLocalToGlobalMap();
325
326 BOOST_CHECK(field->GetLocalToGlobalMap("u") == own);
327
329 field->GetLocalToGlobalMap("v");
330 BOOST_CHECK(mapV == own);
331 BOOST_CHECK(&field->GetLocalToGlobalMap("v") == &mapV);
332}
333
334/**
335 * A variable with other types of boundary conditions gets a map of its own,
336 * kept for later calls and equal to the map of a field built for it.
337 */
338BOOST_AUTO_TEST_CASE(TestContFieldLocalToGlobalMapOtherTypes)
339{
340 Fixture fixture("u,v,w,p,q", variables, conditions);
341 BOOST_REQUIRE(fixture.graph->SameExpansionInfo("u", "w"));
342
343 auto field = fixture.Field("u");
345 field->GetLocalToGlobalMap("w");
346 BOOST_REQUIRE(mapW);
347 BOOST_CHECK(mapW != field->GetLocalToGlobalMap());
348 BOOST_CHECK(field->GetLocalToGlobalMap("w") == mapW);
349
350 // w is Neumann on two sides, so it has fewer Dirichlet dofs than u.
351 BOOST_CHECK_LT(mapW->GetNumGlobalDirBndCoeffs(),
352 field->GetLocalToGlobalMap()->GetNumGlobalDirBndCoeffs());
353
354 CheckSameMap(mapW, fixture.Field("w")->GetLocalToGlobalMap());
355}
356
357/**
358 * A map that pins a degree of freedom of a singular system is not shared,
359 * even with a variable whose boundary conditions are of the same types.
360 */
361BOOST_AUTO_TEST_CASE(TestContFieldLocalToGlobalMapSingular)
362{
363 Fixture fixture("u,v,w,p,q", variables, conditions);
364
365 auto field = fixture.Field("p", true);
367 field->GetLocalToGlobalMap();
368 BOOST_REQUIRE(own->GetSingularSystem());
369 BOOST_REQUIRE_GT(own->GetNumGlobalDirBndCoeffs(), 0);
370
372 field->GetLocalToGlobalMap("q");
373 BOOST_REQUIRE(mapQ);
374 BOOST_CHECK(mapQ != own);
375 BOOST_CHECK(!mapQ->GetSingularSystem());
376 BOOST_CHECK_EQUAL(mapQ->GetNumGlobalDirBndCoeffs(), 0);
377
378 CheckSameMap(mapQ, fixture.Field("q")->GetLocalToGlobalMap());
379}
380
381/**
382 * On a periodic mesh, a variable with the same types of boundary conditions
383 * shares the field's map and one with other types gets a map equal to that
384 * of a field built for it.
385 */
386BOOST_AUTO_TEST_CASE(TestContFieldLocalToGlobalMapPeriodic)
387{
388 Fixture fixture("u,v,w", periodicVariables, periodicConditions);
389
390 auto field = fixture.Field("u");
391 MultiRegions::PeriodicMap periodicVerts, periodicEdges;
392 field->GetPeriodicEntities(periodicVerts, periodicEdges);
393 BOOST_REQUIRE(!periodicEdges.empty());
394
395 BOOST_CHECK(field->GetLocalToGlobalMap("v") ==
396 field->GetLocalToGlobalMap());
397
399 field->GetLocalToGlobalMap("w");
400 BOOST_REQUIRE(mapW);
401 BOOST_CHECK(mapW != field->GetLocalToGlobalMap());
402
403 CheckSameMap(mapW, fixture.Field("w")->GetLocalToGlobalMap());
404}
405
406/**
407 * A copy for another variable adds its map to those the field shares with it,
408 * and the lookup then returns that map instead of building one.
409 */
410BOOST_AUTO_TEST_CASE(TestContFieldLocalToGlobalMapSharedCopy)
411{
412 Fixture fixture("u,v,w,p,q", variables, conditions);
413
414 auto field = fixture.Field("u");
416 *field, fixture.graph, "w", true, false);
417 BOOST_REQUIRE(copyW->GetLocalToGlobalMap() != field->GetLocalToGlobalMap());
418 BOOST_CHECK(field->GetLocalToGlobalMap("w") ==
419 copyW->GetLocalToGlobalMap());
420
421 // A copy sharing the field's map adds it for its own variable and leaves
422 // the field's own map in place.
424 *field, fixture.graph, "v", true, false);
425 BOOST_CHECK(field->GetLocalToGlobalMap("u") ==
426 field->GetLocalToGlobalMap());
427 BOOST_CHECK(field->GetLocalToGlobalMap("v") ==
428 field->GetLocalToGlobalMap());
429
430 // The copies look up the maps there too, also once the copy has gone.
431 BOOST_CHECK(copyV->GetLocalToGlobalMap("w") ==
432 copyW->GetLocalToGlobalMap());
434 copyW->GetLocalToGlobalMap();
435 copyW.reset();
436 BOOST_CHECK(field->GetLocalToGlobalMap("w") == mapW);
437}
438
439/**
440 * The map of a copy that pins a degree of freedom of a singular system is not
441 * shared, and the field's own lookup still returns it. A field with element
442 * expansions of its own shares nothing with the field.
443 */
444BOOST_AUTO_TEST_CASE(TestContFieldLocalToGlobalMapNotShared)
445{
446 Fixture fixture("u,v,w,p,q", variables, conditions);
447
448 auto field = fixture.Field("u");
450 *field, fixture.graph, "q", true, true);
451 BOOST_REQUIRE(singularQ->GetLocalToGlobalMap()->GetSingularSystem());
452
454 field->GetLocalToGlobalMap("q");
455 BOOST_CHECK(mapQ != singularQ->GetLocalToGlobalMap());
456 BOOST_CHECK(!mapQ->GetSingularSystem());
457 BOOST_CHECK(singularQ->GetLocalToGlobalMap("q") ==
458 singularQ->GetLocalToGlobalMap());
459
460 auto fieldW = fixture.Field("w");
461 BOOST_REQUIRE(fieldW->GetExp() != field->GetExp());
463 field->GetLocalToGlobalMap("w");
464 BOOST_CHECK(mapW != fieldW->GetLocalToGlobalMap());
465 CheckSameMap(mapW, fieldW->GetLocalToGlobalMap());
466}
467} // namespace Nektar::ContFieldLocalToGlobalMapTest
LibUtilities::SessionReaderSharedPtr session
SpatialDomains::MeshGraphSharedPtr graph
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)
BOOST_AUTO_TEST_CASE(TestContFieldLocalToGlobalMapSameTypes)
std::shared_ptr< SessionReader > SessionReaderSharedPtr
static fs::path UniquePath(std::string specFileStem)
Create a unique (random) path, based on an input stem string. The returned string is a filename or di...
std::shared_ptr< AssemblyMapCG > AssemblyMapCGSharedPtr
std::map< int, std::vector< PeriodicEntity > > PeriodicMap
std::shared_ptr< ContField > ContFieldSharedPtr
Definition ContField.h:330
std::shared_ptr< MeshGraph > MeshGraphSharedPtr
Definition MeshGraph.h:287