Nektar++
library/FieldUtils/InputModules/InputNek5000.cpp
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // File: InputNek5000.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: Reads a Nek5000 checkpoint file.
32 //
33 ////////////////////////////////////////////////////////////////////////////////
34 
35 #include <iostream>
36 #include <fstream>
37 #include <string>
38 #include <vector>
39 using namespace std;
40 
41 #include <boost/core/ignore_unused.hpp>
42 #include <boost/algorithm/string.hpp>
43 
44 #include "InputNek5000.h"
45 
46 namespace Nektar
47 {
48 namespace FieldUtils
49 {
50 
51 ModuleKey InputNek5000::m_className[1] = {
53  ModuleKey(eInputModule, "fld5000"), InputNek5000::create,
54  "Reads Nek5000 field file.")
55 };
56 
57 /**
58  * @brief Set up InputNek5000 object.
59  *
60  */
61 InputNek5000::InputNek5000(FieldSharedPtr f) : InputModule(f)
62 {
63  m_allowedFiles.insert("fld5000");
64 }
65 
66 /**
67  *
68  */
70 {
71 }
72 
73 /**
74  * @brief Process Nek5000 input file.
75  *
76  * This routine reads a binary-format Nek5000 field file, loads the data into
77  * memory and populates the field definitions to match the data format. Nek5000
78  * is a classic nodal-Lagrangian spectral element code at a single polynomial
79  * order, meaning that the field data are set up according to this structure.
80  *
81  * This module is adapted from the VisIt visualisation software, which supports
82  * a number of Nek5000 inputs.
83  */
84 void InputNek5000::Process(po::variables_map &vm)
85 {
86  boost::ignore_unused(vm);
87 
88  ifstream file(m_config["infile"].as<string>().c_str(), ios::binary);
89 
90  // Header: 132 bytes for binary.
91  vector<char> data(132);
92  file.read(&data[0], 132);
93 
94  // Check header: should be the four characters #std
95  string check(&data[0], 4);
96  string header(&data[4], 128);
97 
98  ASSERTL0(check == "#std", "Unable to read file");
99 
100  // Determine whether we need to byte-swap data: 4-byte float at byte 80
101  // should be 6.54321.
102  bool byteSwap = false;
103  float test;
104 
105  file.read((char *)(&test), 4);
106  if (test > 6.5 && test < 6.6)
107  {
108  byteSwap = false;
109  }
110  else
111  {
112  swap_endian(test);
113  ASSERTL0(test > 6.5 && test < 6.6,
114  "Unable to determine endian-ness of input file");
115  byteSwap = true;
116  }
117 
118  stringstream ss;
119  ss.str(header);
120 
121  int nBytes, nBlocksXYZ[3], nBlocks, nTotBlocks, dir, nDirs, nCycle, nDim;
122  NekDouble time;
123 
124  // Read header information (this is written as ASCII)
125  string remain;
126  ss >> nBytes >> nBlocksXYZ[0] >> nBlocksXYZ[1] >> nBlocksXYZ[2]
127  >> nBlocks >> nTotBlocks >> time >> nCycle >> dir >> nDirs >> remain;
128  boost::trim(remain);
129 
130  nDim = nBlocksXYZ[2] == 1 ? 2 : 3;
131 
132  // Print some basic information for input if in verbose mode.
133  if (m_f->m_verbose)
134  {
135  cout << "Found header information:" << endl;
136  cout << " -- " << (byteSwap ? "" : "do not ") << "need to swap endian"
137  << endl;
138  cout << " -- Data byte size : " << nBytes << endl;
139  cout << " -- Number of xyz blocks : " << nBlocksXYZ[0] << "x"
140  << nBlocksXYZ[1] << "x" << nBlocksXYZ[2] << endl;
141  cout << " -- Blocks in file/total : " << nBlocks << "/" << nTotBlocks
142  << endl;
143  cout << " -- Simulation time : " << time << endl;
144  cout << " -- Number of cycles : " << nCycle << endl;
145  cout << " -- Number of dirs : " << dir << "/" << nDirs << endl;
146  cout << " -- Remaining header : " << remain << endl;
147  }
148 
149  // Major limitation: we don't read out of multiple directories
150  ASSERTL0(nDirs == 1, "Number of directories must be one");
151 
152  // We also don't read partial files.
153  ASSERTL0(nBlocks == nTotBlocks, "Partial field output not supported");
154 
155  // We don't support non-double data
156  ASSERTL0(nBytes == 8, "Data file must contain double-precision data");
157 
158  // Set up a field definition
160  LibUtilities::FieldDefinitions>::AllocateSharedPtr();
161  fielddef->m_shapeType = LibUtilities::eHexahedron;
162  fielddef->m_numHomogeneousDir = 0;
163  fielddef->m_homoStrips = false;
164  fielddef->m_pointsDef = false;
165  fielddef->m_uniOrder = true;
166  fielddef->m_numPointsDef = false;
167 
168  for (int i = 0; i < nDim; ++i)
169  {
170  fielddef->m_basis.push_back(LibUtilities::eGLL_Lagrange);
171  fielddef->m_numModes.push_back(nBlocksXYZ[i]);
172  }
173 
174  // Read element IDs
175  NekUInt32 maxID = 0, minID = numeric_limits<NekUInt32>::max();
176  for (NekUInt32 i = 0; i < nBlocks; ++i)
177  {
178  NekUInt32 blockNum;
179  file.read((char *)&blockNum, 4);
180  if (byteSwap)
181  {
182  swap_endian(blockNum);
183  }
184  fielddef->m_elementIDs.push_back(blockNum-1);
185 
186  maxID = maxID > blockNum ? maxID : blockNum;
187  minID = minID < blockNum ? minID : blockNum;
188  }
189 
190  // Determine how many fields we have to extract
191  size_t blockSize = nBlocksXYZ[0] * nBlocksXYZ[1] * nBlocksXYZ[2];
192  size_t dataSize = blockSize * nBlocks;
193 
194  for (string::size_type i = 0; i < remain.size(); ++i)
195  {
196  switch (remain[i])
197  {
198  case 'U':
199  fielddef->m_fields.push_back("u");
200  fielddef->m_fields.push_back("v");
201  if (nDim == 3)
202  {
203  fielddef->m_fields.push_back("w");
204  }
205  break;
206  case 'P':
207  fielddef->m_fields.push_back("p");
208  break;
209  case 'T':
210  fielddef->m_fields.push_back("T");
211  break;
212  case '1':
213  case '2':
214  case '3':
215  fielddef->m_fields.push_back(string("scalar") + remain[i]);
216  break;
217  case ' ':
218  continue;
219  default:
220  cerr << "Field contains unknown variable: "
221  << remain[i] << endl;
222  abort();
223  }
224  }
225 
226  m_f->m_data.resize(1);
227  m_f->m_data[0].resize(fielddef->m_fields.size() * dataSize);
228 
229  // Now reprocess since different fields need different logic
230  for (size_t i = 0, cnt = 0; i < remain.size(); ++i)
231  {
232  switch (remain[i])
233  {
234  case 'U':
235  {
236  size_t cntVel[3] = {
237  cnt, cnt + dataSize, cnt + 2*dataSize
238  };
239 
240  for (size_t j = 0; j < nBlocks; ++j)
241  {
242  for (size_t k = 0; k < nDim; ++k)
243  {
244  file.read(
245  (char *)&m_f->m_data[0][cntVel[k]],
246  blockSize * sizeof(NekDouble));
247  cntVel[k] += blockSize;
248  }
249  }
250 
251  cnt += nDim * dataSize;
252  break;
253  }
254  case 'P':
255  {
256  file.read(
257  (char *)&m_f->m_data[0][cnt],
258  dataSize * sizeof(NekDouble));
259  cnt += dataSize;
260  break;
261  }
262  case '1':
263  case '2':
264  case '3':
265  {
266  file.read(
267  (char *)&m_f->m_data[0][cnt],
268  dataSize * sizeof(NekDouble));
269  cnt += dataSize;
270  break;
271  }
272  case ' ':
273  continue;
274  }
275  }
276 
277  m_f->m_fielddef.push_back(fielddef);
278 
279  // save field names
280  m_f->m_variables = m_f->m_fielddef[0]->m_fields;
281 }
282 }
283 }
void swap_endian(T &u)
Swap endian ordering of the input variable.
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:216
std::map< std::string, ConfigOption > m_config
List of configuration values.
General purpose memory allocation routines with the ability to allocate from thread specific memory p...
STL namespace.
std::shared_ptr< Field > FieldSharedPtr
Definition: Field.hpp:762
virtual void Process(po::variables_map &vm)
Process Nek5000 input file.
std::uint32_t NekUInt32
Abstract base class for input modules.
Metadata that describes the storage properties of field output.
Definition: FieldIO.h:102
std::pair< ModuleType, std::string > ModuleKey
double NekDouble
std::set< std::string > m_allowedFiles
std::shared_ptr< FieldDefinitions > FieldDefinitionsSharedPtr
Definition: FieldIO.h:179
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:199
Lagrange for SEM basis .
Definition: BasisType.h:54
ModuleFactory & GetModuleFactory()
FieldSharedPtr m_f
Field object.