Nektar++
Loading...
Searching...
No Matches
OutputFileBase.cpp
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////////////
2//
3// File: OutputFileBase.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: Base class for outputting to a file
32//
33////////////////////////////////////////////////////////////////////////////////
34
35#include <set>
36#include <string>
37using namespace std;
38
39#include "OutputFileBase.h"
42#include <boost/format.hpp>
43#include <iomanip>
44
45namespace Nektar::FieldUtils
46{
47
49{
50 m_requireEquiSpaced = false;
51 m_prohibitWrite = false;
52 m_config["writemultiplefiles"] = ConfigOption(
53 true, "0",
54 "Write multiple files in parallel or when using nparts option");
55}
56
60
61void OutputFileBase::v_Process(po::variables_map &vm)
62{
63 m_f->SetUpExp(vm);
64
65 string filename = m_config["outfile"].as<string>();
66
67 if (filename == "")
68 {
69 m_prohibitWrite = true;
70 }
71
72 if (m_f->m_fieldPts != LibUtilities::NullPtsField)
73 {
74 ASSERTL0(!m_f->m_writeBndFld, "Boundary can't be obtained from pts.");
75 if (WriteFile(filename, vm))
76 {
78
79 if (vm.count("error"))
80 {
82 }
83 }
84 }
85 else if (m_f->m_exp.size())
86 {
87 // reset expansion definition to use equispaced points if required.
88 if (m_requireEquiSpaced && (vm.count("no-equispaced") == 0) &&
89 m_f->m_exp[0]->GetNumElmts() != 0 && !m_equispacedSetup)
90 {
92 }
93 if (m_f->m_writeBndFld)
94 {
95 if (m_f->m_verbose &&
96 m_f->m_comm->GetSpaceComm()->TreatAsRankZero())
97 {
98 cout << "\t" << GetModuleName()
99 << ": Writing boundary file(s): ";
100 for (int i = 0; i < m_f->m_bndRegionsToWrite.size(); ++i)
101 {
102 cout << m_f->m_bndRegionsToWrite[i];
103 if (i < m_f->m_bndRegionsToWrite.size() - 1)
104 {
105 cout << ", ";
106 }
107 }
108 cout << endl;
109 }
110
111 int nfields = m_f->m_variables.size();
112 int normdim = m_f->m_graph->GetMeshDimension();
113
114 // Prepare for normals output
115 if (m_f->m_addNormals)
116 {
117 // Prepare for creating expansions for normals
118 m_f->m_exp.resize(nfields + normdim);
119
120 // Include normal name in m_variables
121 string normstr[3] = {"Norm_x", "Norm_y", "Norm_z"};
122 for (int j = 0; j < normdim; ++j)
123 {
124 m_f->m_exp[nfields + j] =
125 m_f->AppendExpList(m_f->m_numHomogeneousDir);
126 m_f->m_variables.push_back(normstr[j]);
127 }
128 }
129
130 // Move m_exp to a new expansion vector
131 vector<MultiRegions::ExpListSharedPtr> exp(m_f->m_exp.size());
132 exp.swap(m_f->m_exp);
133
135 BndExp(exp.size());
136 for (int i = 0; i < exp.size(); ++i)
137 {
138 BndExp[i] = exp[i]->GetBndCondExpansions();
139 }
140
141 // get hold of partition boundary regions so we can match it to
142 // desired region extraction
144 exp[0]->GetGraph());
146 bcs.GetBoundaryRegions();
147 map<int, int> BndRegionMap;
148 map<int, LibUtilities::CommSharedPtr> BndRegionComm;
149
150 // when using extract we may not have defined all boundary regions
151 // so identify that here
152 std::set<int> ProcessBnd;
153 if (m_f->m_session->DefinesTag("CreateBndRegions"))
154 {
155 // evaluate bnd regions to be generated
156 vector<unsigned int> bndRegions;
157 ASSERTL0(
159 m_f->m_session->GetTag("CreateBndRegions"), bndRegions),
160 "Failed to interpret bnd values string");
161
162 for (auto &bnd : bndRegions)
163 {
164 if (bregions.count(bnd) == 1)
165 {
166 ProcessBnd.insert(bnd);
167 }
168 }
169 }
170 else
171 {
172 for (auto &it : bregions)
173 {
174 ProcessBnd.insert(it.first);
175 }
176 }
177
178 int cnt = 0;
179 for (auto &breg_it : bregions)
180 {
181 if (ProcessBnd.count(breg_it.first))
182 {
183 BndRegionMap[breg_it.first] = cnt++;
184 BndRegionComm[breg_it.first] =
185 bcs.GetBoundaryCommunicators()[breg_it.first];
186 }
187 }
188
189 // find ending of output file and insert _b1, _b2
190 int dot = filename.find_last_of('.') + 1;
191 string ext = filename.substr(dot, filename.length() - dot);
192 string name = filename.substr(0, dot - 1);
193
194 // Store temporary communicator
195 LibUtilities::CommSharedPtr tmpComm = m_f->m_comm;
196
197 for (int i = 0; i < m_f->m_bndRegionsToWrite.size(); ++i)
198 {
199 string outname = name + "_b" +
200 std::to_string(m_f->m_bndRegionsToWrite[i]) +
201 "." + ext;
202
203 if (!WriteFile(outname, vm))
204 {
205 continue;
206 }
207 RegisterConfig("outfile", outname);
208
209 if (BndRegionMap.count(m_f->m_bndRegionsToWrite[i]) == 1)
210 {
211 m_f->m_comm = BndRegionComm[m_f->m_bndRegionsToWrite[i]];
212
213 int Border = BndRegionMap[m_f->m_bndRegionsToWrite[i]];
214
215 // set up m_exp to point to boundary expansion
216 for (int j = 0; j < exp.size(); ++j)
217 {
218 m_f->m_exp[j] = BndExp[j][Border];
219 }
220
221 for (int j = 0; j < exp.size(); ++j)
222 {
223 m_f->m_exp[j] = BndExp[j][Border];
224 m_f->m_exp[j]->BwdTrans(m_f->m_exp[j]->GetCoeffs(),
225 m_f->m_exp[j]->UpdatePhys());
226 }
227
228 if (m_f->m_addNormals)
229 {
230 // Get normals
232 exp[0]->GetBoundaryNormals(Border, NormPhys);
233
234 // add normal coefficients to expansions
235 for (int j = 0; j < normdim; ++j)
236 {
237 m_f->m_exp[nfields + j] =
238 BndExp[nfields + j][Border];
240 m_f->m_exp[nfields + j]->GetTotPoints(),
241 NormPhys[j], 1,
242 m_f->m_exp[nfields + j]->UpdatePhys(), 1);
243 m_f->m_exp[nfields + j]->FwdTransLocalElmt(
244 m_f->m_exp[nfields + j]->GetPhys(),
245 m_f->m_exp[nfields + j]->UpdateCoeffs());
246 }
247 }
248 v_OutputFromExp(vm);
249 // output error for regression checking.
250 if (vm.count("error"))
251 {
253 }
254
255 // Reset communicator
256 m_f->m_comm = tmpComm;
257 }
258
259 // put outfile back to filename in case of nparts option
260 RegisterConfig("outfile", filename);
261 }
262 // Restore m_exp
263 exp.swap(m_f->m_exp);
264 }
265 else
266 {
267 if (WriteFile(filename, vm))
268 {
269 v_OutputFromExp(vm);
270 // output error for regression checking.
271 if (vm.count("error"))
272 {
274 }
275 }
276 }
277 }
278 else if (m_f->m_data.size())
279 {
280 ASSERTL0(!m_f->m_writeBndFld, "Boundary extraction requires xml file.");
281 if (WriteFile(filename, vm))
282 {
284 }
285 }
286}
287
288bool OutputFileBase::WriteFile(std::string &filename, po::variables_map &vm)
289{
290 if (m_prohibitWrite)
291 {
292 return true;
293 }
294
295 // Get path to file. If procid was defined, get the full name to avoid
296 // checking files from other partitions
297 fs::path outFile;
298 if (vm.count("nparts"))
299 {
300 outFile = GetFullOutName(filename, vm);
301 }
302 else
303 {
304 outFile = GetPath(filename, vm);
305 }
306
308 if (m_f->m_comm)
309 {
310 comm = m_f->m_comm;
311 }
312 else
313 {
314 comm = LibUtilities::GetCommFactory().CreateInstance("Serial", 0, 0);
315 }
316
317 int count = fs::exists(outFile) ? 1 : 0;
318 comm->GetSpaceComm()->AllReduce(count, LibUtilities::ReduceSum);
319
320 int writeFile = 1;
321 if (count && (vm.count("force-output") == 0))
322 {
323 if (vm.count("nparts") == 0) // do not do check if --nparts is enabled.
324 {
325 writeFile = 0; // set to zero for reduce all to be correct.
326
327 if (comm->GetSpaceComm()->TreatAsRankZero())
328 {
329 string answer;
330 cout << "Did you wish to overwrite " << outFile << " (y/n)? ";
331 getline(cin, answer);
332 if (answer.compare("y") == 0)
333 {
334 writeFile = 1;
335 }
336 else
337 {
338 cout << "Not writing file '" << filename
339 << "' because it already exists" << endl;
340 }
341 }
342 comm->GetSpaceComm()->AllReduce(writeFile, LibUtilities::ReduceSum);
343 }
344 }
345 return (writeFile == 0) ? false : true;
346}
347
348void OutputFileBase::ConvertExpToEquispaced(po::variables_map &vm)
349{
350 // Information to create new expansion
351 int numFields = m_f->m_exp.size();
352 m_f->m_fielddef = m_f->m_exp[0]->GetFieldDefinitions();
353
354 // Set points to equispaced
355 int nPointsNew = 0;
356 if (vm.count("output-points"))
357 {
358 nPointsNew = vm["output-points"].as<int>();
359 }
360 m_f->m_graph->SetExpansionInfoToEvenlySpacedPoints(nPointsNew);
361
362 // Save original expansion
363 vector<MultiRegions::ExpListSharedPtr> expOld = m_f->m_exp;
364
365 // Create new expansion
366 m_f->m_exp[0] = m_f->SetUpFirstExpList(m_f->m_numHomogeneousDir, true);
367 for (int i = 1; i < numFields; ++i)
368 {
369 m_f->m_exp[i] = m_f->AppendExpList(m_f->m_numHomogeneousDir);
370 }
371
372 // Extract result to new expansion
373 for (int i = 0; i < numFields; ++i)
374 {
375 m_f->m_exp[i]->ExtractCoeffsToCoeffs(expOld[i], expOld[i]->GetCoeffs(),
376 m_f->m_exp[i]->UpdateCoeffs());
377 m_f->m_exp[i]->BwdTrans(m_f->m_exp[i]->GetCoeffs(),
378 m_f->m_exp[i]->UpdatePhys());
379 }
380
381 // Extract boundary expansion if needed
382 if (m_f->m_writeBndFld)
383 {
386
387 for (int i = 0; i < numFields; ++i)
388 {
389 BndExpOld = expOld[i]->GetBndCondExpansions();
390 for (int j = 0; j < BndExpOld.size(); ++j)
391 {
392 BndExp = m_f->m_exp[i]->UpdateBndCondExpansion(j);
393 BndExp->ExtractCoeffsToCoeffs(BndExpOld[j],
394 BndExpOld[j]->GetCoeffs(),
395 BndExp->UpdateCoeffs());
396 }
397 }
398 }
399
400 m_f->m_fielddef = std::vector<LibUtilities::FieldDefinitionsSharedPtr>();
401
402 // Make a note so that we know that the fielddefs have now been setup.
403 m_equispacedSetup = true;
404}
405
407{
408 int coordim = m_f->m_fieldPts->GetDim();
409 std::string coordVars[] = {"x", "y", "z"};
410
411 vector<string> variables = m_f->m_variables;
412 variables.insert(variables.begin(), coordVars, coordVars + coordim);
413 // Get fields and coordinates
414 Array<OneD, Array<OneD, NekDouble>> fields(variables.size());
415
416 // We can just grab everything from points. This should be a
417 // reference, not a copy.
418 m_f->m_fieldPts->GetPts(fields);
419 for (int i = 0; i < fields.size(); ++i)
420 {
421 // calculate L2 and Linf value
422 int npts = fields[i].size();
423
424 NekDouble l2err = 0.0;
425 NekDouble linferr = 0.0;
426 for (int j = 0; j < npts; ++j)
427 {
428 l2err += fields[i][j] * fields[i][j];
429 linferr = max(linferr, fabs(fields[i][j]));
430 }
431
432 m_f->m_comm->GetSpaceComm()->AllReduce(l2err, LibUtilities::ReduceSum);
433 m_f->m_comm->GetSpaceComm()->AllReduce(npts, LibUtilities::ReduceSum);
434 m_f->m_comm->GetSpaceComm()->AllReduce(linferr,
436
437 l2err /= npts;
438 l2err = sqrt(l2err);
439
440 if (m_f->m_comm->GetSpaceComm()->TreatAsRankZero())
441 {
442 cout << "L 2 error (variable " << variables[i] << ") : " << l2err
443 << endl;
444
445 cout << "L inf error (variable " << variables[i]
446 << ") : " << linferr << endl;
447 }
448 }
449}
450
452{
453 int coordim =
454 m_f->m_exp[0]->GetExp(0)->GetCoordim() + m_f->m_numHomogeneousDir;
455 int totpoints = m_f->m_exp[0]->GetTotPoints();
456 std::string coordVars[] = {"x", "y", "z"};
457
458 // Set up storage for coordinates
459 Array<OneD, Array<OneD, NekDouble>> coords(coordim);
460 for (int i = 0; i < coordim; ++i)
461 {
462 coords[i] = Array<OneD, NekDouble>(totpoints);
463 }
464
465 // Get coordinates
466 if (coordim == 1)
467 {
468 m_f->m_exp[0]->GetCoords(coords[0]);
469 }
470 else if (coordim == 2)
471 {
472 m_f->m_exp[0]->GetCoords(coords[0], coords[1]);
473 }
474 else
475 {
476 m_f->m_exp[0]->GetCoords(coords[0], coords[1], coords[2]);
477 }
478
479 for (int j = 0; j < coordim; ++j)
480 {
481 NekDouble l2err = m_f->m_exp[0]->L2(coords[j]);
482 NekDouble linferr = m_f->m_exp[0]->Linf(coords[j]);
483
484 if (m_f->m_comm->GetSpaceComm()->TreatAsRankZero())
485 {
486 cout << "L 2 error (variable " << coordVars[j] << ") : " << l2err
487 << endl;
488
489 cout << "L inf error (variable " << coordVars[j]
490 << ") : " << linferr << endl;
491 }
492 }
493
494 for (int j = 0; j < m_f->m_exp.size(); ++j)
495 {
496 NekDouble l2err = m_f->m_exp[j]->L2(m_f->m_exp[j]->GetPhys());
497 NekDouble linferr = m_f->m_exp[j]->Linf(m_f->m_exp[j]->GetPhys());
498
499 if (m_f->m_comm->GetSpaceComm()->TreatAsRankZero() &&
500 m_f->m_variables.size() > 0)
501 {
502 cout << "L 2 error (variable " << m_f->m_variables[j]
503 << ") : " << l2err << endl;
504
505 cout << "L inf error (variable " << m_f->m_variables[j]
506 << ") : " << linferr << endl;
507 }
508 }
509}
510
511} // namespace Nektar::FieldUtils
#define ASSERTL0(condition, msg)
FIELD_UTILS_EXPORT void RegisterConfig(std::string key, std::string value="")
Register a configuration option with a module.
Definition Module.cpp:100
std::string GetModuleName()
Definition Module.h:200
FieldSharedPtr m_f
Field object.
Definition Module.h:239
std::map< std::string, ConfigOption > m_config
List of configuration values.
Definition Module.h:272
virtual void v_OutputFromExp(po::variables_map &vm)=0
Write from m_exp to output file.
bool WriteFile(std::string &filename, po::variables_map &vm)
fs::path GetFullOutName(std::string &filename, po::variables_map &vm)
void ConvertExpToEquispaced(po::variables_map &vm)
virtual void v_OutputFromData(po::variables_map &vm)=0
Write from data to output file.
virtual void v_OutputFromPts(po::variables_map &vm)=0
Write from pts to output file.
fs::path GetPath(std::string &filename, po::variables_map &vm)
void v_Process(po::variables_map &vm) override
Write fld to output file.
Abstract base class for output modules.
Definition Module.h:316
tBaseSharedPtr CreateInstance(tKey idKey, tParam... args)
Create an instance of the class referred to by idKey.
static bool GenerateVector(const std::string &str, std::vector< T > &out)
Takes a comma-separated string and converts it to entries in a vector.
std::map< int, LibUtilities::CommSharedPtr > GetBoundaryCommunicators() const
Definition Conditions.h:299
const BoundaryRegionCollection & GetBoundaryRegions(void) const
Definition Conditions.h:273
std::shared_ptr< Field > FieldSharedPtr
Definition Field.hpp:1026
static PtsFieldSharedPtr NullPtsField
Definition PtsField.h:185
CommFactory & GetCommFactory()
std::shared_ptr< Comm > CommSharedPtr
Pointer to a Communicator object.
Definition Comm.h:55
std::shared_ptr< ExpList > ExpListSharedPtr
Shared pointer to an ExpList object.
std::map< int, BoundaryRegionShPtr > BoundaryRegionCollection
Definition Conditions.h:249
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition Vmath.hpp:825
STL namespace.
scalarT< T > max(scalarT< T > lhs, scalarT< T > rhs)
Definition scalar.hpp:305
scalarT< T > sqrt(scalarT< T > in)
Definition scalar.hpp:290
Represents a command-line configuration option.
Definition Module.h:129