Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FldToTecplot.cpp
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // File: FldToTecplot.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 // License for the specific language governing rights and limitations under
14 // Permission is hereby granted, free of charge, to any person obtaining a
15 // copy of this software and associated documentation files (the "Software"),
16 // to deal in the Software without restriction, including without limitation
17 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 // and/or sell copies of the Software, and to permit persons to whom the
19 // Software is furnished to do so, subject to the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be included
22 // in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 // DEALINGS IN THE SOFTWARE.
31 //
32 // Description: Output Tecplot file containing field.
33 //
34 ////////////////////////////////////////////////////////////////////////////////
35 
36 #include <cstdio>
37 #include <cstdlib>
38 
39 #include <MultiRegions/ExpList.h>
40 #include <MultiRegions/ExpList0D.h>
41 #include <MultiRegions/ExpList1D.h>
42 #include <MultiRegions/ExpList2D.h>
43 #include <MultiRegions/ExpList3D.h>
48 
49 using namespace Nektar;
50 
51 #include <sys/stat.h>
52 
53 int fexist( const char *filename ) {
54  struct stat buffer ;
55  if ( stat( filename, &buffer ) ) return 0 ;
56  return 1 ;
57 }
58 
59 int main(int argc, char *argv[])
60 {
61  if(argc < 2)
62  {
63  fprintf(stderr,"Usage: %s meshfile fieldfile\n",argv[0]);
64  exit(1);
65  }
66 
67  unsigned int i, j;
68  Array<OneD,NekDouble> fce;
69  Array<OneD,NekDouble> xc0,xc1,xc2;
70 
71  bool Extrude2DWithHomogeneous = false;
72 
73  bool SingleModePlot=false;
74  bool HalfModePlot=false;
75 
76  int nExtraPoints, nExtraPlanes;
77 
80 
81  vSession->LoadParameter("OutputExtraPoints",nExtraPoints,0);
82  vSession->LoadParameter("OutputExtraPlanes",nExtraPlanes,0);
83 
84  vSession->MatchSolverInfo("Extrude2DWithHomogeneous","True",Extrude2DWithHomogeneous,false);
85  vSession->MatchSolverInfo("ModeType","SingleMode",SingleModePlot,false);
86  vSession->MatchSolverInfo("ModeType","HalfMode",HalfModePlot,false);
87 
88 
89  // Read in mesh from input file
91  //----------------------------------------------
92  for (int n = 1; n < argc; ++n)
93  {
94  string fname = std::string(argv[n]);
95  int fdot = fname.find_last_of('.');
96  if (fdot != std::string::npos)
97  {
98  string ending = fname.substr(fdot);
99 
100  // If .chk or .fld we exchange the extension in the output file.
101  // For all other files (e.g. .bse) we append the extension to avoid
102  // conflicts.
103  if (ending == ".chk" || ending == ".fld")
104  {
105  fname = fname.substr(0,fdot);
106  }
107  else if (ending == ".gz")
108  {
109  fname = fname.substr(0,fdot);
110  fdot = fname.find_last_of('.');
111  ASSERTL0(fdot != std::string::npos,
112  "Error: expected file extension before .gz.");
113  ending = fname.substr(fdot);
114  ASSERTL0(ending == ".xml",
115  "Compressed non-xml files are not supported.");
116  continue;
117  }
118  else if (ending == ".xml")
119  {
120  continue;
121  }
122  }
123 
124  fname = fname + ".dat";
125 
126  if (argc > 3)
127  {
128  if (fexist(fname.c_str()))
129  {
130  cout << "Skipping converted file: " << argv[n] << endl;
131  continue;
132  }
133  cout << "Processing " << argv[n] << endl;
134  }
135 
136  //----------------------------------------------
137  // Import field file.
138  string fieldfile(argv[n]);
139  vector<LibUtilities::FieldDefinitionsSharedPtr> fielddef;
140  vector<vector<NekDouble> > fielddata;
141  LibUtilities::Import(fieldfile,fielddef,fielddata);
142  //----------------------------------------------
143 
144  if(Extrude2DWithHomogeneous) // Set up Homogeneous information
145  {
146  NekDouble length;
147  vSession->LoadParameter("LZ",length,1);
148  fielddef[0]->m_numHomogeneousDir = 1;
149  fielddef[0]->m_numModes.push_back(2); // Have to set this to 2 as default
150  fielddef[0]->m_homogeneousZIDs.push_back(0);
151  fielddef[0]->m_homogeneousLengths.push_back(length);
152  fielddef[0]->m_basis.push_back(LibUtilities::eFourier);
153  }
154 
155  if(SingleModePlot) // Set Up printing of perturbation
156  {
157  fielddef[0]->m_numModes.push_back(4); // Have to set this to 4 as default
158  fielddef[0]->m_basis.push_back(LibUtilities::eFourier); //Initialisation of a standard Fourier Expansion
159  }
160 
161  if(HalfModePlot)
162  {
163  fielddef[0]->m_numModes.push_back(4); // Have to set this to 4 as default
164  fielddef[0]->m_basis.push_back(LibUtilities::eFourier); //Initialisation of a standard Fourier Expansion
165  fielddef[0]->m_basis.push_back(LibUtilities::eFourierHalfModeIm);//Initialisation of a HalfModeFourierIm Expansion
166 
167  }
168 
169  //----------------------------------------------
170  // Set up Expansion information
171  for(i = 0; i < fielddef.size(); ++i)
172  {
173  vector<LibUtilities::PointsType> ptype;
174  for(j = 0; j < 3; ++j)
175  {
176  ptype.push_back(LibUtilities::ePolyEvenlySpaced);
177  }
178 
179  fielddef[i]->m_pointsDef = true;
180  fielddef[i]->m_points = ptype;
181 
182  vector<unsigned int> porder;
183  if(fielddef[i]->m_numPointsDef == false)
184  {
185  for(j = 0; j < fielddef[i]->m_numModes.size(); ++j)
186  {
187  porder.push_back(fielddef[i]->m_numModes[j]+nExtraPoints);
188  }
189 
190  fielddef[i]->m_numPointsDef = true;
191  }
192  else
193  {
194  for(j = 0; j < fielddef[i]->m_numPoints.size(); ++j)
195  {
196  porder.push_back(fielddef[i]->m_numPoints[j]+nExtraPoints);
197  }
198  }
199  fielddef[i]->m_numPoints = porder;
200  }
201 
202  graphShPt->SetExpansions(fielddef);
203  bool useFFT = false;
204  bool dealiasing = false;
205  //----------------------------------------------
206 
207 
208  //----------------------------------------------
209  // Define Expansion
210  int expdim = graphShPt->GetMeshDimension();
211  int nfields = fielddef[0]->m_fields.size();
212  Array<OneD, MultiRegions::ExpListSharedPtr> Exp(nfields);
213 
214  //auxiliary expansion for plotting perturbations
215  Array<OneD, MultiRegions::ExpListSharedPtr> Exp1(nfields);
216 
217  switch(expdim)
218  {
219  case 1:
220  {
221  ASSERTL0(fielddef[0]->m_numHomogeneousDir <= 2,"NumHomogeneousDir is only set up for 1 or 2");
222 
223  if(fielddef[0]->m_numHomogeneousDir == 1)
224  {
226 
227  // Define Homogeneous expansion
228  //int nplanes = fielddef[0]->m_numModes[1];
229  int nplanes;
230  vSession->LoadParameter("HomModesZ",nplanes,fielddef[0]->m_numModes[1]);
231 
232  // choose points to be at evenly spaced points at
234  const LibUtilities::BasisKey Bkey(fielddef[0]->m_basis[1],nplanes,Pkey);
235  NekDouble ly = fielddef[0]->m_homogeneousLengths[0];
236 
237  Exp2DH1 = MemoryManager<MultiRegions::ExpList2DHomogeneous1D>::AllocateSharedPtr(vSession,Bkey,ly,useFFT,dealiasing,graphShPt);
238  Exp[0] = Exp2DH1;
239 
240  for(i = 1; i < nfields; ++i)
241  {
243  }
244  }
245  else if(fielddef[0]->m_numHomogeneousDir == 2)
246  {
248 
249  // Define Homogeneous expansion
250  //int nylines = fielddef[0]->m_numModes[1];
251  //int nzlines = fielddef[0]->m_numModes[2];
252 
253  int nylines;
254  int nzlines;
255  vSession->LoadParameter("HomModesY",nylines,fielddef[0]->m_numModes[1]);
256  vSession->LoadParameter("HomModesZ",nzlines,fielddef[0]->m_numModes[2]);
257 
258  // choose points to be at evenly spaced points at
260  const LibUtilities::BasisKey BkeyY(fielddef[0]->m_basis[1],nylines,PkeyY);
261 
263  const LibUtilities::BasisKey BkeyZ(fielddef[0]->m_basis[2],nzlines,PkeyZ);
264 
265  NekDouble ly = fielddef[0]->m_homogeneousLengths[0];
266  NekDouble lz = fielddef[0]->m_homogeneousLengths[1];
267 
268  Exp3DH2 = MemoryManager<MultiRegions::ExpList3DHomogeneous2D>::AllocateSharedPtr(vSession,BkeyY,BkeyZ,ly,lz,useFFT,dealiasing,graphShPt);
269  Exp[0] = Exp3DH2;
270 
271  for(i = 1; i < nfields; ++i)
272  {
274  }
275  }
276  else
277  {
280  Exp[0] = Exp1D;
281  for(i = 1; i < nfields; ++i)
282  {
284  }
285  }
286  }
287  break;
288  case 2:
289  {
290  ASSERTL0(fielddef[0]->m_numHomogeneousDir <= 1,"NumHomogeneousDir is only set up for 1");
291 
292  if(fielddef[0]->m_numHomogeneousDir == 1)
293  {
297 
298  if(SingleModePlot)
299  {
300  int nplanes = fielddef[0]->m_numModes[2];
301 
302  const LibUtilities::PointsKey Pkey(nplanes+nExtraPlanes,LibUtilities::eFourierSingleModeSpaced);
303  //for plotting perturbations (4planes)
304  const LibUtilities::PointsKey Pkey1(nplanes+nExtraPlanes+2+1,LibUtilities::ePolyEvenlySpaced);
305 
306  //SingleMode Basis
307  const LibUtilities::BasisKey Bkey(fielddef[0]->m_basis[2],nplanes,Pkey);
308  //Fourier expansion
309  const LibUtilities::BasisKey Bkey1(fielddef[0]->m_basis[3],nplanes+2,Pkey1);
310 
311  NekDouble lz = fielddef[0]->m_homogeneousLengths[0];
312 
313  //Fourier SingleMode Expansion with two points
314  Exp3DH1 = MemoryManager<MultiRegions::ExpList3DHomogeneous1D>::AllocateSharedPtr(vSession,Bkey,lz,useFFT,dealiasing,graphShPt,fielddef[0]->m_fields[0]);
315 
316  //Fourier 4 modes expansion
317  Exp3DH1_aux = MemoryManager<MultiRegions::ExpList3DHomogeneous1D>::AllocateSharedPtr(vSession,Bkey1,lz,useFFT,dealiasing,graphShPt,fielddef[0]->m_fields[0]);
318  Exp1[0]= Exp3DH1_aux;
319 
320 
321  //Define Homogeneous standard 4 plane Fourier Expansion
322  for(i = 1; i < nfields; ++i)
323  {
325  ::AllocateSharedPtr(*Exp3DH1_aux);
326  }
327 
328 
329  }
330  else if(HalfModePlot)
331  {
332  int nplanes = fielddef[0]->m_numModes[2];
333 
334  const LibUtilities::PointsKey Pkey(nplanes+nExtraPlanes,LibUtilities::eFourierSingleModeSpaced);
335  //for plotting perturbations (4planes)
336  const LibUtilities::PointsKey Pkey1(nplanes+nExtraPlanes+3+1,LibUtilities::ePolyEvenlySpaced);
337 
338  //FourierHalfModeRe Basis
339  const LibUtilities::BasisKey Bkey(fielddef[0]->m_basis[2],nplanes,Pkey);
340  //Fourier expansion
341  const LibUtilities::BasisKey Bkey1(fielddef[0]->m_basis[3],nplanes+3,Pkey1);
342  //FourierHalfModeIm Expansion
343  const LibUtilities::BasisKey Bkey2(fielddef[0]->m_basis[4],nplanes,Pkey);
344 
345 
346  NekDouble lz = fielddef[0]->m_homogeneousLengths[0];
347 
348  //FourierHalfModeRe Expansion
349  Exp3DH1 = MemoryManager<MultiRegions::ExpList3DHomogeneous1D>::AllocateSharedPtr(vSession,Bkey,lz,useFFT,dealiasing,graphShPt,fielddef[0]->m_fields[0]);
350  //FourierHalfModeIm Expansion
351  Exp3DH1_Im = MemoryManager<MultiRegions::ExpList3DHomogeneous1D>::AllocateSharedPtr(vSession,Bkey2,lz,useFFT,dealiasing,graphShPt,fielddef[0]->m_fields[2]);
352 
353  //Fourier 4 modes expansion
354  Exp3DH1_aux = MemoryManager<MultiRegions::ExpList3DHomogeneous1D>::AllocateSharedPtr(vSession,Bkey1,lz,useFFT,dealiasing,graphShPt,fielddef[0]->m_fields[0]);
355  Exp1[0]= Exp3DH1_aux;
356 
357 
358  //Define Homogeneous standard 4 plane Fourier Expansion
359  for(i = 1; i < nfields; ++i)
360  {
362  ::AllocateSharedPtr(*Exp3DH1_aux);
363  }
364 
365 
366  }
367  else
368  {
369  //int nplanes = fielddef[0]->m_numModes[2];
370  int nplanes;
371  vSession->LoadParameter("HomModesZ",nplanes,fielddef[0]->m_numModes[2]);
372 
373  // choose points to be at evenly spaced points at
374  // nplanes + 1 points
375  const LibUtilities::PointsKey Pkey(nplanes+nExtraPlanes+1,LibUtilities::ePolyEvenlySpaced);
376  const LibUtilities::BasisKey Bkey(fielddef[0]->m_basis[2],nplanes,Pkey);
377  NekDouble lz = fielddef[0]->m_homogeneousLengths[0];
378 
379 
380  Exp3DH1 = MemoryManager<MultiRegions::ExpList3DHomogeneous1D>::AllocateSharedPtr(vSession,Bkey,lz,useFFT,dealiasing,graphShPt,fielddef[0]->m_fields[0]);
381  }
382 
383 
384  //it is a FourierSingleMode or HalfMode in case
385  if(HalfModePlot)
386  {
387  Exp[0] = Exp3DH1;
388  for(i = 1; i < nfields; ++i)
389  {
390  //w must have imaginary basis
391  if(i==2)
392  {
394  ::AllocateSharedPtr(*Exp3DH1_Im);
395  }
396  else
397  {
399  ::AllocateSharedPtr(*Exp3DH1);
400  }
401  }
402 
403 
404  }
405  else
406  {
407  Exp[0] = Exp3DH1;
408  for(i = 1; i < nfields; ++i)
409  {
411  ::AllocateSharedPtr(*Exp3DH1);
412 
413  }
414  }
415  }
416  else
417  {
419  Exp2D = MemoryManager<MultiRegions::ExpList2D>::AllocateSharedPtr(vSession,graphShPt,true,fielddef[0]->m_fields[0]);
420  Exp[0] = Exp2D;
421 
422  for(i = 1; i < nfields; ++i)
423  {
425  ::AllocateSharedPtr(*Exp2D);
426  }
427  }
428  }
429  break;
430  case 3:
431  {
434  ::AllocateSharedPtr(vSession,graphShPt);
435  Exp[0] = Exp3D;
436 
437  for(i = 1; i < nfields; ++i)
438  {
440  ::AllocateSharedPtr(*Exp3D);
441  }
442  }
443  break;
444  default:
445  ASSERTL0(false,"Expansion dimension not recognised");
446  break;
447  }
448  //----------------------------------------------
449 
450  if(Extrude2DWithHomogeneous)
451  {
452  // Need to set this back to 1 to read 2D field
453  // Perhaps could set up Extra parameters?
454  fielddef[0]->m_numModes[2] = 1;
455  }
456 
457  //----------------------------------------------
458  // Copy data to file
459  for(j = 0; j < nfields; ++j)
460  {
461  for(int i = 0; i < fielddata.size(); ++i)
462  {
463 
464  Exp[j]->ExtractDataToCoeffs(fielddef[i],fielddata[i],
465  fielddef[i]->m_fields[j],
466  Exp[j]->UpdateCoeffs());
467  }
468 
469  if(SingleModePlot)
470  {
471  //it is on two planes for single mode
472  int dim=Exp[j]->GetNcoeffs();
473  //copy the single mode on the 4planes expansion
474  Vmath::Vcopy(dim,&Exp[j]->GetCoeffs()[0],1,&Exp1[j]->UpdateCoeffs()[dim],1);
475 
476  Exp1[j]->BwdTrans(Exp1[j]->GetCoeffs(),Exp1[j]->UpdatePhys());
477 
478  }
479  else if(HalfModePlot)
480  {
481  //it is one planes for single mode
482  int dim=Exp[j]->GetNcoeffs();
483  //copy the mode on the 4planes expansion
484  if(j==2)
485  {
486  //copy on the 4th plane
487  //Vmath::Vcopy(dim,&Exp[j]->GetCoeffs()[0],1,&Exp1[j]->UpdateCoeffs()[3*dim],1);
488  Vmath::Vcopy(dim,&Exp[j]->GetCoeffs()[0],1,&Exp1[j]->UpdateCoeffs()[2*dim],1);
489 
490  }
491  else
492  {
493  Vmath::Vcopy(dim,&Exp[j]->GetCoeffs()[0],1,&Exp1[j]->UpdateCoeffs()[2*dim],1);
494  }
495 
496  Exp1[j]->BwdTrans(Exp1[j]->GetCoeffs(),Exp1[j]->UpdatePhys());
497 
498  }
499  else
500  {
501  Exp[j]->BwdTrans(Exp[j]->GetCoeffs(),Exp[j]->UpdatePhys());
502  }
503  }
504  //----------------------------------------------
505 
506  //----------------------------------------------
507  // Write solution
508 
509  if(SingleModePlot || HalfModePlot)
510  {
511  std::string var = "";
512 
513  for(int j = 0; j < Exp1.num_elements(); ++j)
514  {
515  var = var + ", " + fielddef[0]->m_fields[j];
516  }
517 
518  ofstream outfile(fname.c_str());
519 
520  Exp1[0]->WriteTecplotHeader(outfile,var);
521  for(int i = 0; i < Exp1[0]->GetNumElmts(); ++i)
522  {
523  Exp1[0]->WriteTecplotZone(outfile,i);
524  for(int j = 0; j < Exp1.num_elements(); ++j)
525  {
526  Exp1[j]->WriteTecplotField(outfile,i);
527  }
528  Exp1[0]->WriteTecplotConnectivity(outfile,i);
529  }
530  }
531  else
532  {
533  std::string var = "";
534 
535  for(int j = 0; j < Exp.num_elements(); ++j)
536  {
537  var = var + ", " + fielddef[0]->m_fields[j];
538  }
539 
540  ofstream outfile(fname.c_str());
541  Exp[0]->WriteTecplotHeader(outfile, var);
542  Exp[0]->WriteTecplotZone(outfile);
543  for(int j = 0; j < Exp.num_elements(); ++j)
544  {
545  Exp[j]->WriteTecplotField(outfile);
546  }
547  Exp[0]->WriteTecplotConnectivity(outfile);
548  }
549  //----------------------------------------------
550  }
551  return 0;
552 }