Nektar++
Loading...
Searching...
No Matches
Classes | Functions | Variables
Nektar::FieldFileRoundTripTest Namespace Reference

Classes

struct  ScratchDirectory
 A uniquely named directory under the system temporary directory, removed with everything in it when the object goes out of scope. More...
 

Functions

static void Substitute (std::string &text, const std::string &token, const std::string &value)
 Write a coefficient vector to a field file and read it back.
 
static void RoundTrip (const std::string &expansionType)
 
static void RoundTripOrderChange (const std::string &expansionType)
 Round trip a coefficient vector through a different polynomial order and back.
 
 BOOST_AUTO_TEST_CASE (TestFieldFileRoundTripHybridMeshModified)
 The modified basis, which is what almost every session uses.
 
 BOOST_AUTO_TEST_CASE (TestFieldFileRoundTripHybridMeshOrthogonal)
 
 BOOST_AUTO_TEST_CASE (TestFieldFileOrderChangeModified)
 Restarting at a different order, which is what reaches the per element extraction, for each basis.
 
 BOOST_AUTO_TEST_CASE (TestFieldFileOrderChangeOrthogonal)
 

Variables

static const std::string sessionTemplate
 

Function Documentation

◆ BOOST_AUTO_TEST_CASE() [1/4]

Nektar::FieldFileRoundTripTest::BOOST_AUTO_TEST_CASE ( TestFieldFileOrderChangeModified  )

Restarting at a different order, which is what reaches the per element extraction, for each basis.

Definition at line 408 of file TestFieldFileRoundTrip.cpp.

409{
410 RoundTripOrderChange("MODIFIED");
411}
static void RoundTripOrderChange(const std::string &expansionType)
Round trip a coefficient vector through a different polynomial order and back.

References RoundTripOrderChange().

◆ BOOST_AUTO_TEST_CASE() [2/4]

Nektar::FieldFileRoundTripTest::BOOST_AUTO_TEST_CASE ( TestFieldFileOrderChangeOrthogonal  )

Definition at line 413 of file TestFieldFileRoundTrip.cpp.

414{
415 RoundTripOrderChange("ORTHOGONAL");
416}

References RoundTripOrderChange().

◆ BOOST_AUTO_TEST_CASE() [3/4]

Nektar::FieldFileRoundTripTest::BOOST_AUTO_TEST_CASE ( TestFieldFileRoundTripHybridMeshModified  )

The modified basis, which is what almost every session uses.

Definition at line 389 of file TestFieldFileRoundTrip.cpp.

390{
391 RoundTrip("MODIFIED");
392}
static void RoundTrip(const std::string &expansionType)

References RoundTrip().

◆ BOOST_AUTO_TEST_CASE() [4/4]

Nektar::FieldFileRoundTripTest::BOOST_AUTO_TEST_CASE ( TestFieldFileRoundTripHybridMeshOrthogonal  )

The orthogonal basis, whose second direction is eOrtho_B rather than eModified_B. The per-element extraction asserts on the basis it is given, so a basis it does not name cannot be read back at all - the failure is an assertion rather than wrong numbers, and no modified-basis session reaches it.

Definition at line 401 of file TestFieldFileRoundTrip.cpp.

402{
403 RoundTrip("ORTHOGONAL");
404}

References RoundTrip().

◆ RoundTrip()

static void Nektar::FieldFileRoundTripTest::RoundTrip ( const std::string &  expansionType)
static

Definition at line 189 of file TestFieldFileRoundTrip.cpp.

190{
191 std::string sessionFile = sessionTemplate;
192 Substitute(sessionFile, "@TYPE@", expansionType);
193 Substitute(sessionFile, "@NMODES@", "4");
194
195 // Write the session out so the reader has a file to open.
196 ScratchDirectory scratch("FieldFileRoundTrip");
197 fs::path sessionPath = scratch.m_path / fs::path("FieldFileRoundTrip" +
198 expansionType + ".xml");
199 fs::path fieldPath = scratch.m_path / fs::path("FieldFileRoundTrip" +
200 expansionType + ".fld");
201
202 std::ofstream sessionOut(sessionPath.string());
203 sessionOut << sessionFile;
204 sessionOut.close();
205
206 int argc = 2;
207 std::string exe = "FieldFileRoundTrip";
208 std::string file = sessionPath.string();
209 char *argv[] = {exe.data(), file.data(), nullptr};
210
212 LibUtilities::SessionReader::CreateInstance(argc, argv);
214 SpatialDomains::MeshGraphIO::Read(session);
217
218 // Both shapes must be present, or the test cannot see the failure it is
219 // written for.
220 std::vector<LibUtilities::FieldDefinitionsSharedPtr> fieldDefs =
221 expList->GetFieldDefinitions();
222 BOOST_REQUIRE_MESSAGE(fieldDefs.size() > 1,
223 "Expected one field definition per element shape.");
224
225 // A distinct value per coefficient, so a misplaced one is visible rather
226 // than coincidentally right.
227 const int nCoeffs = expList->GetNcoeffs();
228 std::vector<NekDouble> written(nCoeffs);
229 for (int i = 0; i < nCoeffs; ++i)
230 {
231 written[i] = 1.0 + 0.25 * i;
232 }
233
234 std::vector<std::vector<NekDouble>> fieldData(fieldDefs.size());
235 for (std::size_t i = 0; i < fieldDefs.size(); ++i)
236 {
237 fieldDefs[i]->m_fields.push_back("u");
238 expList->AppendFieldData(fieldDefs[i], fieldData[i], written);
239 }
240
241 LibUtilities::FieldIOSharedPtr fieldIO =
242 LibUtilities::FieldIO::CreateDefault(session);
243 fieldIO->Write(fieldPath.string(), fieldDefs, fieldData);
244
245 // Read it back the way a restart does.
246 std::vector<LibUtilities::FieldDefinitionsSharedPtr> readDefs;
247 std::vector<std::vector<NekDouble>> readData;
248 fieldIO->Import(fieldPath.string(), readDefs, readData);
249
250 std::string variable = "u";
251 std::vector<NekDouble> read(nCoeffs, 0.0);
252 for (std::size_t i = 0; i < readDefs.size(); ++i)
253 {
254 expList->ExtractDataToCoeffs(readDefs[i], readData[i], variable, read);
255 }
256
257 for (int i = 0; i < nCoeffs; ++i)
258 {
259 BOOST_CHECK_CLOSE(read[i], written[i], 1.0e-8);
260 }
261
262 session->Finalise();
263}
LibUtilities::SessionReaderSharedPtr session
SpatialDomains::MeshGraphSharedPtr graph
General purpose memory allocation routines with the ability to allocate from thread specific memory p...
static void Substitute(std::string &text, const std::string &token, const std::string &value)
Write a coefficient vector to a field file and read it back.
static const std::string sessionTemplate
std::shared_ptr< SessionReader > SessionReaderSharedPtr
std::shared_ptr< ExpList > ExpListSharedPtr
Shared pointer to an ExpList object.
std::shared_ptr< MeshGraph > MeshGraphSharedPtr
Definition MeshGraph.h:287
A uniquely named directory under the system temporary directory, removed with everything in it when t...

References Nektar::MemoryManager< DataType >::AllocateSharedPtr(), Nektar::LibUtilities::FieldIO::CreateDefault(), Nektar::LibUtilities::SessionReader::CreateInstance(), graph, Nektar::FieldFileRoundTripTest::ScratchDirectory::m_path, Nektar::SpatialDomains::MeshGraphIO::Read(), session, sessionTemplate, and Substitute().

Referenced by BOOST_AUTO_TEST_CASE(), and BOOST_AUTO_TEST_CASE().

◆ RoundTripOrderChange()

static void Nektar::FieldFileRoundTripTest::RoundTripOrderChange ( const std::string &  expansionType)
static

Round trip a coefficient vector through a different polynomial order and back.

Restarting at the order the file was written at is the easy case: ExpList::ExtractDataToCoeffs() sees a matching mode count and basis and copies the element's coefficients straight across, never consulting the expansion. Restarting at a different order is what sends it into the per element routine, which switches on the basis type and knows how to place mode (i,j) of one order among the modes of another. A basis that switch does not name cannot be read at all.

Writing at one order, reading at a higher one and coming back is exact for a hierarchical basis - the added modes are zero and are dropped again - so the original coefficients are the expected answer, without the test needing to know how the modes are laid out.

Definition at line 282 of file TestFieldFileRoundTrip.cpp.

283{
284 ScratchDirectory scratch("FieldOrderChange");
285
286 auto build = [&expansionType, &scratch](const std::string &numModes,
287 const std::string &tag) {
288 std::string text = sessionTemplate;
289 Substitute(text, "@TYPE@", expansionType);
290 Substitute(text, "@NMODES@", numModes);
291
292 fs::path path = scratch.m_path / fs::path("FieldOrderChange" +
293 expansionType + tag + ".xml");
294 std::ofstream out(path.string());
295 out << text;
296 out.close();
297 return path;
298 };
299
300 fs::path lowPath = build("4", "Low");
301 fs::path highPath = build("5", "High");
302 fs::path fieldLow = scratch.m_path / fs::path("FieldOrderChange" +
303 expansionType + "Low.fld");
304 fs::path fieldHigh = scratch.m_path / fs::path("FieldOrderChange" +
305 expansionType + "High.fld");
306
307 std::string exe = "FieldOrderChange";
308 std::string lowFile = lowPath.string();
309 std::string highFile = highPath.string();
310
311 int argc = 2;
312 char *argvLow[] = {exe.data(), lowFile.data(), nullptr};
313 LibUtilities::SessionReaderSharedPtr sessionLow =
314 LibUtilities::SessionReader::CreateInstance(argc, argvLow);
315 SpatialDomains::MeshGraphSharedPtr graphLow =
316 SpatialDomains::MeshGraphIO::Read(sessionLow);
317 MultiRegions::ExpListSharedPtr expLow =
318 MemoryManager<MultiRegions::ExpList>::AllocateSharedPtr(sessionLow,
319 graphLow);
320
321 char *argvHigh[] = {exe.data(), highFile.data(), nullptr};
322 LibUtilities::SessionReaderSharedPtr sessionHigh =
323 LibUtilities::SessionReader::CreateInstance(argc, argvHigh);
324 SpatialDomains::MeshGraphSharedPtr graphHigh =
325 SpatialDomains::MeshGraphIO::Read(sessionHigh);
326 MultiRegions::ExpListSharedPtr expHigh =
327 MemoryManager<MultiRegions::ExpList>::AllocateSharedPtr(sessionHigh,
328 graphHigh);
329
330 BOOST_REQUIRE(expLow->GetNcoeffs() != expHigh->GetNcoeffs());
331
332 // Writes @p coeffs from @p exp into a field file.
333 auto writeField = [](const MultiRegions::ExpListSharedPtr &exp,
334 const LibUtilities::SessionReaderSharedPtr &session,
335 std::vector<NekDouble> &coeffs, const fs::path &path) {
336 std::vector<LibUtilities::FieldDefinitionsSharedPtr> defs =
337 exp->GetFieldDefinitions();
338 std::vector<std::vector<NekDouble>> data(defs.size());
339 for (std::size_t i = 0; i < defs.size(); ++i)
340 {
341 defs[i]->m_fields.push_back("u");
342 exp->AppendFieldData(defs[i], data[i], coeffs);
343 }
344 LibUtilities::FieldIO::CreateDefault(session)->Write(path.string(),
345 defs, data);
346 };
347
348 // Reads a field file back into a coefficient vector sized for @p exp.
349 auto readField = [](const MultiRegions::ExpListSharedPtr &exp,
350 const LibUtilities::SessionReaderSharedPtr &session,
351 const fs::path &path) {
352 std::vector<LibUtilities::FieldDefinitionsSharedPtr> defs;
353 std::vector<std::vector<NekDouble>> data;
354 LibUtilities::FieldIO::CreateDefault(session)->Import(path.string(),
355 defs, data);
356 std::string variable = "u";
357 std::vector<NekDouble> coeffs(exp->GetNcoeffs(), 0.0);
358 for (std::size_t i = 0; i < defs.size(); ++i)
359 {
360 exp->ExtractDataToCoeffs(defs[i], data[i], variable, coeffs);
361 }
362 return coeffs;
363 };
364
365 std::vector<NekDouble> written(expLow->GetNcoeffs());
366 for (std::size_t i = 0; i < written.size(); ++i)
367 {
368 written[i] = 1.0 + 0.25 * static_cast<NekDouble>(i);
369 }
370
371 writeField(expLow, sessionLow, written, fieldLow);
372
373 // Up to the higher order, then back down.
374 std::vector<NekDouble> raised = readField(expHigh, sessionHigh, fieldLow);
375 writeField(expHigh, sessionHigh, raised, fieldHigh);
376 std::vector<NekDouble> read = readField(expLow, sessionLow, fieldHigh);
377
378 BOOST_REQUIRE_EQUAL(read.size(), written.size());
379 for (std::size_t i = 0; i < written.size(); ++i)
380 {
381 BOOST_CHECK_CLOSE(read[i], written[i], 1.0e-8);
382 }
383
384 sessionLow->Finalise();
385 sessionHigh->Finalise();
386}

References Nektar::MemoryManager< DataType >::AllocateSharedPtr(), Nektar::LibUtilities::FieldIO::CreateDefault(), Nektar::LibUtilities::SessionReader::CreateInstance(), Nektar::FieldFileRoundTripTest::ScratchDirectory::m_path, Nektar::SpatialDomains::MeshGraphIO::Read(), session, sessionTemplate, and Substitute().

Referenced by BOOST_AUTO_TEST_CASE(), and BOOST_AUTO_TEST_CASE().

◆ Substitute()

static void Nektar::FieldFileRoundTripTest::Substitute ( std::string &  text,
const std::string &  token,
const std::string &  value 
)
static

Write a coefficient vector to a field file and read it back.

This is the path a restart takes: the solver projects its state to coefficients, ExpList::AppendFieldData() lays them out per field definition, and on resumption ExpList::ExtractDataToCoeffs() puts them back. The coefficients are the state, so anything the round trip loses is state the resumed run does not have.

The reader is called once per field definition and accumulates into one vector, which is what makes a mesh of more than one element shape the interesting case: a reader that starts from zero each call returns only the shape it was given last and silently zeros the others. On this mesh that would leave the two quadrilaterals - the first 32 coefficients - at zero while the triangles survived. Substitute every occurrence of token in text with value.

Definition at line 148 of file TestFieldFileRoundTrip.cpp.

150{
151 for (std::size_t at = text.find(token); at != std::string::npos;
152 at = text.find(token))
153 {
154 text.replace(at, token.size(), value);
155 }
156}

Referenced by RoundTrip(), and RoundTripOrderChange().

Variable Documentation

◆ sessionTemplate

const std::string Nektar::FieldFileRoundTripTest::sessionTemplate
static

A mesh of two quadrilaterals and four triangles over the unit square. The two shapes are the point of it: ExpList::GetFieldDefinitions() emits one field definition per shape, and a reader that handles only the last of them is the failure this test exists to catch. A single shape mesh cannot see it.

Definition at line 58 of file TestFieldFileRoundTrip.cpp.

Referenced by RoundTrip(), and RoundTripOrderChange().