Nektar++
TestVarcoeffHashing.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: TestVarcoeffHashing.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 test for varcoeff hashing
32//
33///////////////////////////////////////////////////////////////////////////////
34
36
39
40#include <boost/test/unit_test.hpp>
41
43{
44using namespace MultiRegions;
45
46// Forward declare global variables for all tests
47fs::path pathGlobal;
50
53
54// Define session file for the tests
55// Ideally use a session-free approach in the future
56static const std::string sessionfile =
57 R"END(<?xml version="1.0" encoding="UTF-8"?>
58<NEKTAR>
59 <GEOMETRY DIM="2" SPACE="2">
60
61 <VERTEX>
62 <V ID="0"> 0.0 0.0 0.0 </V>
63 <V ID="1"> 0.5 0.0 0.0 </V>
64 <V ID="2"> 1.0 0.0 0.0 </V>
65 <V ID="3"> 0.0 0.5 0.0 </V>
66 <V ID="4"> 0.5 0.5 0.0 </V>
67 <V ID="5"> 1.0 0.5 0.0 </V>
68 <V ID="6"> 0.0 1.0 0.0 </V>
69 <V ID="7"> 0.5 1.0 0.0 </V>
70 <V ID="8"> 1.0 1.0 0.0 </V>
71 </VERTEX>
72
73 <EDGE>
74 <E ID="0"> 0 1 </E>
75 <E ID="1"> 1 2 </E>
76 <E ID="2"> 0 3 </E>
77 <E ID="3"> 1 4 </E>
78 <E ID="4"> 2 5 </E>
79 <E ID="5"> 3 4 </E>
80 <E ID="6"> 4 5 </E>
81 <E ID="7"> 3 6 </E>
82 <E ID="8"> 4 7 </E>
83 <E ID="9"> 5 8 </E>
84 <E ID="10"> 6 7 </E>
85 <E ID="11"> 7 8 </E>
86 </EDGE>
87
88 <ELEMENT>
89 <Q ID="0"> 0 3 5 2 </Q>
90 <Q ID="1"> 1 4 6 3 </Q>
91 <Q ID="2"> 5 8 10 7 </Q>
92 <Q ID="3"> 6 9 11 8 </Q>
93 </ELEMENT>
94
95 <COMPOSITE>
96 <C ID="0"> Q[0-3] </C>
97 <C ID="1"> E[0,1] </C> <!-- Lower wall -->
98 <C ID="2"> E[2,7] </C> <!-- Inflow -->
99 <C ID="3"> E[4,9] </C> <!-- Outflow -->
100 <C ID="4"> E[10,11] </C> <!-- Upper wall -->
101 </COMPOSITE>
102
103 <DOMAIN> C[0] </DOMAIN>
104
105 </GEOMETRY>
106
107 <EXPANSIONS>
108 <E COMPOSITE="C[0]" FIELDS="u" TYPE="MODIFIED" NUMMODES="3"/>
109 </EXPANSIONS>
110
111 <CONDITIONS>
112
113 <PARAMETERS>
114 <P> lambda = 1.0 </P>
115 </PARAMETERS>
116 <VARIABLES>
117 <V ID="0"> u </V>
118 </VARIABLES>
119
120 <BOUNDARYREGIONS>
121 <B ID="0"> C[1] </B>
122 <B ID="1"> C[2] </B>
123 <B ID="2"> C[3] </B>
124 <B ID="3"> C[4] </B>
125 </BOUNDARYREGIONS>
126
127 <BOUNDARYCONDITIONS>
128 <REGION REF="0"> <!-- Stationary -->
129 <D VAR="u" VALUE="0" />
130 </REGION>
131 <REGION REF="1"> <!-- Inflow -->
132 <D VAR="u" VALUE="y" />
133 </REGION>
134 <REGION REF="2"> <!-- Outflow -->
135 <N VAR="u" VALUE="0" />
136 </REGION>
137 <REGION REF="3"> <!-- Moving -->
138 <D VAR="u" VALUE="1" />
139 </REGION>
140 </BOUNDARYCONDITIONS>
141
142 <FUNCTION NAME="d00">
143 <E VAR="u" VALUE="0.1*y" />
144 </FUNCTION>
145
146 <FUNCTION NAME="d00B">
147 <E VAR="u" VALUE="0.5*y" />
148 </FUNCTION>
149
150 <FUNCTION NAME="Forcing">
151 <E VAR="u" VALUE="sin(y)" />
152 </FUNCTION>
153
154 <FUNCTION NAME="ExactSolution">
155 <E VAR="u" VALUE="y" />
156 </FUNCTION>
157
158 </CONDITIONS>
159
160</NEKTAR>
161)END";
162
163/*
164 * Create the session file at working directory
165 * TODO Use session-free approach to save effort on this test
166 */
167void createSessionFile(fs::path &ph)
168{
169 // Create temporary directory
170 ph = fs::temp_directory_path() / LibUtilities::UniquePath("varctest");
171 fs::create_directories(ph);
172
173 // Create file in working directory
174 ph /= "TestVarcoeffHashing.xml"; // append filename
175 std::ofstream sfile(ph);
176 sfile << sessionfile;
177 sfile.close();
178}
179
180/*
181 * Setup for generic ContField solve
182 * Read session file, setup contfield and
183 * auxilliary functions for forcing and varcoeffs
184 */
185void setupContFieldSolve(fs::path &ph,
189{
190#ifdef _WIN32
191 // Prepare input file name - convert wstring to string to support Windows
192 const std::wstring input_file_wstr(ph.c_str());
193 const std::string input_file_str(input_file_wstr.begin(),
194 input_file_wstr.end());
195 char *input_file = (char *)input_file_str.c_str();
196#else
197 char *input_file = (char *)ph.c_str();
198#endif
199
200 // Setup parameters for call to IncNavierStokesSolver
201 int argc = 2;
202 char *argv[] = {(char *)("IncNavierStokesSolver"), input_file, nullptr};
203
204 // Read Session, MeshGraph and create ContField
209 Session, Graph, Session->GetVariable(0));
211
212 // Read functons for varcoeffs and Forcing
213 LibUtilities::EquationSharedPtr d00func = Session->GetFunction("d00", 0);
214 LibUtilities::EquationSharedPtr d00Bfunc = Session->GetFunction("d00B", 0);
215 LibUtilities::EquationSharedPtr ffunc = Session->GetFunction("Forcing", 0);
216
217 // Get coordinates to evaluate functions
218 unsigned int npoints = Exp->GetNpoints();
219 unsigned int coordim = Exp->GetCoordim(0);
220 Array<OneD, NekDouble> x0(npoints), x1(npoints), x2(npoints);
221 Array<OneD, NekDouble> d00(npoints, 0.0), d00B(npoints, 0.0),
222 fcePhys(npoints);
223
224 // Define lambda
225 factors[StdRegions::eFactorLambda] = Session->GetParameter("lambda");
226 Exp->GetCoords(x0, x1, x2);
227
228 // Define 1st varcoeff
229 d00func->Evaluate(x0, x1, x2, d00);
231
232 // Define 2nd varcoeff
233 d00Bfunc->Evaluate(x0, x1, x2, d00B);
235
236 // Define Forcing
237 ffunc->Evaluate(x0, x1, x2, fcePhys);
238 Fce->SetPhys(fcePhys);
239
240 // Define Advection velocities and add to varcoeff1 or 2
244 for (int i = 0; i < coordim; i++)
245 {
246 varcoeffs1[varcoefftypes[i]] = Array<OneD, NekDouble>(npoints, 0.1);
247 varcoeffs2[varcoefftypes[i]] = Array<OneD, NekDouble>(npoints, 0.2);
248 }
249
250 // Clear solution
251 Vmath::Zero(Exp->GetNcoeffs(), Exp->UpdateCoeffs(), 1);
252}
253
254BOOST_AUTO_TEST_CASE(TestVarcoeffHashing)
255{
256 // Create session file and setup Contfield
259
260 // Check no initial GlobalLinSys i.e. count == 0
261 BOOST_CHECK_EQUAL(contfield->GetPoolCount("GlobalLinSys"), 0);
262
263 // Create GlobalLinSys(varcoeff1)
264 contfield->HelmSolve(forcefield->GetPhys(), contfield->UpdateCoeffs(),
266
267 // Check count == 1
268 BOOST_CHECK_EQUAL(contfield->GetPoolCount("GlobalLinSys"), 1);
269
270 // Create new GlobalLinSys(varcoeff2)
271 contfield->HelmSolve(forcefield->GetPhys(), contfield->UpdateCoeffs(),
273
274 // Check count == 2
275 BOOST_CHECK_EQUAL(contfield->GetPoolCount("GlobalLinSys"), 2);
276
277 // Again use 2nd GlobalLinSys(varcoeff2)
278 contfield->HelmSolve(forcefield->GetPhys(), contfield->UpdateCoeffs(),
280
281 // Check count still == 2
282 BOOST_CHECK_EQUAL(contfield->GetPoolCount("GlobalLinSys"), 2);
283
284 // Again use 1st GlobalLinSys(varcoeff1)
285 contfield->HelmSolve(forcefield->GetPhys(), contfield->UpdateCoeffs(),
287
288 // Check count still == 2
289 BOOST_CHECK_EQUAL(contfield->GetPoolCount("GlobalLinSys"), 2);
290
291 // Clear for next tests
292 contfield->ClearGlobalLinSysManager();
293}
294
295BOOST_AUTO_TEST_CASE(TestUnsetGlobalLinSys)
296{
297 // Check that the test setup is clear i.e. count == 0
298 BOOST_CHECK_EQUAL(contfield->GetPoolCount("GlobalLinSys"), 0);
299
300 // Create 1st GlobalLinSys
301 auto gkey1 = contfield->LinearAdvectionDiffusionReactionSolve(
302 forcefield->GetPhys(), contfield->UpdateCoeffs(), factors, varcoeffs1);
303
304 // Check count == 1
305 BOOST_CHECK_EQUAL(contfield->GetPoolCount("GlobalLinSys"), 1);
306
307 // Create 2nd GlobalLinSys
308 auto gkey2 = contfield->LinearAdvectionDiffusionReactionSolve(
309 forcefield->GetPhys(), contfield->UpdateCoeffs(), factors, varcoeffs2);
310
311 // Check count == 2
312 BOOST_CHECK_EQUAL(contfield->GetPoolCount("GlobalLinSys"), 2);
313
314 // Unset GlobalLinSys1
315 contfield->UnsetGlobalLinSys(gkey1, true);
316
317 // Check count == 1
318 BOOST_CHECK_EQUAL(contfield->GetPoolCount("GlobalLinSys"), 1);
319
320 // Unset GlobalLinSys2
321 contfield->UnsetGlobalLinSys(gkey2, true);
322
323 // Check count == 0
324 BOOST_CHECK_EQUAL(contfield->GetPoolCount("GlobalLinSys"), 0);
325
326 // Clear for next tests
327 contfield->ClearGlobalLinSysManager();
328}
329} // namespace Nektar::VarcoeffHashingTest
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)
Definition: MeshGraph.cpp:115
std::shared_ptr< SessionReader > SessionReaderSharedPtr
std::shared_ptr< Equation > EquationSharedPtr
Definition: Equation.h:125
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...
Definition: Filesystem.hpp:69
std::shared_ptr< ContField > ContFieldSharedPtr
Definition: ContField.h:268
std::shared_ptr< MeshGraph > MeshGraphSharedPtr
Definition: MeshGraph.h:174
std::map< ConstFactorType, NekDouble > ConstFactorMap
Definition: StdRegions.hpp:402
std::map< StdRegions::VarCoeffType, VarCoeffEntry > VarCoeffMap
Definition: StdRegions.hpp:346
MultiRegions::ContFieldSharedPtr forcefield
void setupContFieldSolve(fs::path &ph, LibUtilities::SessionReaderSharedPtr &Session, MultiRegions::ContFieldSharedPtr &Exp, MultiRegions::ContFieldSharedPtr &Fce)
static const std::string sessionfile
MultiRegions::ContFieldSharedPtr contfield
BOOST_AUTO_TEST_CASE(TestVarcoeffHashing)
LibUtilities::SessionReaderSharedPtr lsession
StdRegions::VarCoeffMap varcoeffs1
StdRegions::VarCoeffMap varcoeffs2
StdRegions::ConstFactorMap factors
void Zero(int n, T *x, const int incx)
Zero vector.
Definition: Vmath.hpp:273