Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FldQCriterion.cpp
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // File: FldQCriterion.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: Add Q criterion to field.
33 //
34 ////////////////////////////////////////////////////////////////////////////////
35 
36 #include <cstdio>
37 #include <cstdlib>
38 
39 #include <MultiRegions/ExpList.h>
40 #include <MultiRegions/ExpList1D.h>
41 #include <MultiRegions/ExpList2D.h>
42 #include <MultiRegions/ExpList3D.h>
46 
47 using namespace Nektar;
48 
49 int main(int argc, char *argv[])
50 {
51  int i, j;
52 
53  if (argc != 4)
54  {
55  fprintf(stderr, "Usage: FldQCriterion meshfile infld outfld\n");
56  exit(1);
57  }
58 
61 
62 
63  //----------------------------------------------
64  // Read in mesh from input file
65  string meshfile(argv[argc - 3]);
68  //----------------------------------------------
69 
70  //----------------------------------------------
71  // Import field file.
72  string fieldfile(argv[argc - 2]);
73  vector<LibUtilities::FieldDefinitionsSharedPtr> fielddef;
74  vector<vector<NekDouble> > fielddata;
75  LibUtilities::Import(fieldfile, fielddef, fielddata);
76  bool useFFT = false;
77  bool dealiasing = false;
78  //----------------------------------------------
79 
80  //----------------------------------------------
81  // Define Expansion
82  int expdim = graphShPt->GetMeshDimension();
83  int nfields = fielddef[0]->m_fields.size();
84  int addfields = 1;
85  Array<OneD, MultiRegions::ExpListSharedPtr> Exp(nfields + addfields);
86 
87  switch (expdim)
88  {
89  case 1:
90  {
91  ASSERTL0(fielddef[0]->m_numHomogeneousDir <= 2,
92  "Quasi-3D approach is only set up for 1 or 2 homogeneous "
93  "directions");
94 
95  if (fielddef[0]->m_numHomogeneousDir == 1)
96  {
98 
99  // Define Homogeneous expansion
100  int nplanes;
101  vSession->LoadParameter(
102  "HomModesZ", nplanes, fielddef[0]->m_numModes[1]);
103 
104  // choose points to be at evenly spaced points at
105  // nplanes points
106  const LibUtilities::PointsKey Pkey(
108  const LibUtilities::BasisKey Bkey(
109  fielddef[0]->m_basis[1], nplanes, Pkey);
110  NekDouble ly = fielddef[0]->m_homogeneousLengths[0];
111 
114  vSession, Bkey, ly, useFFT, dealiasing, graphShPt);
115  Exp[0] = Exp2DH1;
116 
117  for (i = 1; i < nfields; ++i)
118  {
120  ::AllocateSharedPtr(*Exp2DH1);
121  }
122  }
123  else if (fielddef[0]->m_numHomogeneousDir == 2)
124  {
126 
127  // Define Homogeneous expansion
128  //int nylines = fielddef[0]->m_numModes[1];
129  //int nzlines = fielddef[0]->m_numModes[2];
130 
131  int nylines;
132  int nzlines;
133  vSession->LoadParameter(
134  "HomModesY", nylines, fielddef[0]->m_numModes[1]);
135  vSession->LoadParameter(
136  "HomModesZ", nzlines, fielddef[0]->m_numModes[2]);
137 
138  // choose points to be at evenly spaced points at
139  // nplanes points
140  const LibUtilities::PointsKey PkeyY(
142  const LibUtilities::BasisKey BkeyY(
143  fielddef[0]->m_basis[1], nylines, PkeyY);
144 
145  const LibUtilities::PointsKey PkeyZ(
147  const LibUtilities::BasisKey BkeyZ(
148  fielddef[0]->m_basis[2], nzlines, PkeyZ);
149 
150  NekDouble ly = fielddef[0]->m_homogeneousLengths[0];
151  NekDouble lz = fielddef[0]->m_homogeneousLengths[1];
152 
154  ::AllocateSharedPtr(vSession, BkeyY, BkeyZ, ly, lz,
155  useFFT, dealiasing, graphShPt);
156  Exp[0] = Exp3DH2;
157  for (i = 1; i < nfields; ++i)
158  {
160  ::AllocateSharedPtr(*Exp3DH2);
161  }
162  }
163  else
164  {
167  ::AllocateSharedPtr(vSession, graphShPt);
168  Exp[0] = Exp1D;
169  for (i = 1; i < nfields + addfields; ++i)
170  {
172  ::AllocateSharedPtr(*Exp1D);
173  }
174  }
175  }
176  break;
177  case 2:
178  {
179 
180  ASSERTL0(fielddef[0]->m_numHomogeneousDir <= 1,
181  "NumHomogeneousDir is only set up for 1");
182 
183  if (fielddef[0]->m_numHomogeneousDir == 1)
184  {
186 
187  // Define Homogeneous expansion
188  //int nplanes = fielddef[0]->m_numModes[2];
189 
190  int nplanes;
191  vSession->LoadParameter(
192  "HomModesZ", nplanes, fielddef[0]->m_numModes[2]);
193 
194  // choose points to be at evenly spaced points at
195  // nplanes points
196  const LibUtilities::PointsKey Pkey(
198  const LibUtilities::BasisKey Bkey(
199  fielddef[0]->m_basis[2], nplanes, Pkey);
200  NekDouble lz = fielddef[0]->m_homogeneousLengths[0];
201 
203  ::AllocateSharedPtr(vSession, Bkey, lz, useFFT,
204  dealiasing, graphShPt);
205  Exp[0] = Exp3DH1;
206 
207  for (i = 1; i < nfields + addfields; ++i)
208  {
210  ::AllocateSharedPtr(*Exp3DH1);
211  }
212  }
213  else
214  {
217  ::AllocateSharedPtr(vSession, graphShPt);
218  Exp[0] = Exp2D;
219 
220  for (i = 1; i < nfields + addfields; ++i)
221  {
223  ::AllocateSharedPtr(*Exp2D);
224  }
225  }
226  }
227  break;
228  case 3:
229  {
230 
233  ::AllocateSharedPtr(vSession, graphShPt);
234  Exp[0] = Exp3D;
235 
236  for (i = 1; i < nfields + addfields; ++i)
237  {
239  ::AllocateSharedPtr(*Exp3D);
240  }
241  }
242  break;
243  default:
244  ASSERTL0(false, "Expansion dimension not recognised");
245  break;
246  }
247  //----------------------------------------------
248 
249  //----------------------------------------------
250  // Copy data from field file
251  for (j = 0; j < nfields; ++j)
252  {
253  for (int i = 0; i < fielddata.size(); ++i)
254  {
255  Exp[j]->ExtractDataToCoeffs(fielddef [i],
256  fielddata[i],
257  fielddef [i]->m_fields[j],
258  Exp[j]->UpdateCoeffs());
259  }
260  Exp[j]->BwdTrans(Exp[j]->GetCoeffs(), Exp[j]->UpdatePhys());
261  }
262  //----------------------------------------------
263 
264  //----------------------------------------------
265  // Compute gradients of fields
266  int nq = Exp[0]->GetNpoints();
267  Array<OneD, Array<OneD, NekDouble> > grad(nfields * nfields);
268 
269  Array<OneD, Array<OneD, NekDouble> > omega(nfields * nfields);
270  Array<OneD, Array<OneD, NekDouble> > S (nfields * nfields);
271  Array<OneD, Array<OneD, NekDouble> > Q (nfields * nfields);
272 
273  Array<OneD, Array<OneD, NekDouble> > outfield (addfields);
274  Array<OneD, Array<OneD, NekDouble> > outfield1(addfields);
275  Array<OneD, Array<OneD, NekDouble> > outfield2(addfields);
276  Array<OneD, Array<OneD, NekDouble> > outfield3(addfields);
277 
278  for (i = 0; i < nfields * nfields; ++i)
279  {
280  grad[i] = Array<OneD, NekDouble>(nq);
281  }
282 
283  for (i = 0; i < addfields; ++i)
284  {
285  outfield [i] = Array<OneD, NekDouble>(nq);
286  outfield1[i] = Array<OneD, NekDouble>(nq);
287  outfield2[i] = Array<OneD, NekDouble>(nq);
288  outfield3[i] = Array<OneD, NekDouble>(nq);
289 
290  omega[i] = Array<OneD, NekDouble>(nq);
291  S[i] = Array<OneD, NekDouble>(nq);
292  Q[i] = Array<OneD, NekDouble>(nq);
293  }
294 
295  // Calculate Gradient & Vorticity
296  for (i = 0; i < nfields; ++i)
297  {
298  Exp[i]->PhysDeriv(
299  Exp[i]->GetPhys(), grad[i * nfields], grad[i * nfields + 1],
300  grad[i * nfields + 2]);
301  }
302 
303  // W_x = Wy - Vz
304  Vmath::Vsub(nq, grad[2 * nfields + 1], 1, grad[1 * nfields + 2], 1,
305  outfield1[0], 1);
306  // W_x^2
307  Vmath::Vmul(nq, outfield1[0], 1, outfield1[0], 1, outfield1[0], 1);
308 
309  // W_y = Uz - Wx
310  Vmath::Vsub(nq, grad[0 * nfields + 2], 1, grad[2 * nfields + 0], 1,
311  outfield2[0], 1);
312  // W_y^2
313  Vmath::Vmul(nq, outfield2[0], 1, outfield2[0], 1, outfield2[0], 1);
314 
315  // W_z = Vx - Uy
316  Vmath::Vsub(nq, grad[1 * nfields + 0], 1, grad[0 * nfields + 1], 1,
317  outfield3[0], 1);
318  // W_z^2
319  Vmath::Vmul(nq, outfield3[0], 1, outfield3[0], 1, outfield3[0], 1);
320 
321  // add fields omega = 0.5*(W_x^2 + W_y^2 + W_z^2)
322 
323  NekDouble fac = 0.5;
324  Vmath::Vadd(nq, &outfield1[0][0], 1, &outfield2[0][0], 1, &omega[0][0], 1);
325  Vmath::Vadd(nq, &omega[0][0], 1, &outfield3[0][0], 1, &omega[0][0], 1);
326 
327  for (int k = 0; k < addfields; ++k)
328  {
329  Vmath::Smul(nq, fac, &omega[k][0], 1, &omega[k][0], 1);
330  }
331 
332  Vmath::Zero(nq, &outfield1[0][0], 1);
333  Vmath::Zero(nq, &outfield2[0][0], 1);
334  Vmath::Zero(nq, &outfield3[0][0], 1);
335 
336  Vmath::Vmul(nq, grad[0 * nfields + 0], 1, grad[0 * nfields + 0], 1,
337  outfield1[0], 1);
338  Vmath::Vmul(nq, grad[1 * nfields + 1], 1, grad[1 * nfields + 1], 1,
339  outfield2[0], 1);
340  Vmath::Vmul(nq, grad[2 * nfields + 2], 1, grad[2 * nfields + 2], 1,
341  outfield3[0], 1);
342 
343  Vmath::Vadd(nq, &outfield1[0][0], 1, &outfield2[0][0], 1, &S[0][0], 1);
344  Vmath::Vadd(nq, &S[0][0], 1, &outfield3[0][0], 1, &S[0][0], 1);
345 
346  // W_y + V_z
347  Vmath::Vadd(nq, grad[2 * nfields + 1], 1, grad[1 * nfields + 2], 1,
348  outfield1[0], 1);
349  Vmath::Vmul(nq, &outfield1[0][0], 1, &outfield1[0][0], 1,
350  &outfield1[0][0], 1);
351 
352  // U_z + W_x
353  Vmath::Vadd(nq, grad[0 * nfields + 2], 1, grad[2 * nfields + 0], 1,
354  outfield2[0], 1);
355  Vmath::Vmul(nq, &outfield2[0][0], 1, &outfield2[0][0], 1,
356  &outfield2[0][0], 1);
357 
358  // V_x + U_y
359  Vmath::Vadd(nq, grad[1 * nfields + 0], 1, grad[0 * nfields + 1], 1,
360  outfield3[0], 1);
361  Vmath::Vmul(nq, &outfield3[0][0], 1, &outfield3[0][0], 1,
362  &outfield3[0][0], 1);
363 
364  Vmath::Vadd(nq, &outfield1[0][0], 1, &outfield2[0][0], 1,
365  &outfield2[0][0], 1);
366  Vmath::Vadd(nq, &outfield2[0][0], 1, &outfield3[0][0], 1,
367  &outfield3[0][0], 1);
368 
369  for (int k = 0; k < addfields; ++k)
370  {
371  Vmath::Smul(nq, fac, &outfield3[k][0], 1, &outfield3[k][0], 1);
372  }
373 
374  Vmath::Vadd(nq, &outfield3[0][0], 1, &S[0][0], 1, &S[0][0], 1);
375  Vmath::Vsub(nq, omega[0], 1, S[0], 1, Q[0], 1);
376 
377  for (int k = 0; k < addfields; ++k)
378  {
379  Vmath::Smul(nq, fac, &Q[k][0], 1, &Q[k][0], 1);
380  }
381 
382  for (i = 0; i < addfields; ++i)
383  {
384  Exp[nfields + i]->FwdTrans(Q[i], Exp[nfields + i]->UpdateCoeffs());
385  }
386 
387  //-----------------------------------------------
388  // Write solution to file with additional computed fields
389  string out(argv[argc - 1]);
390  std::vector<LibUtilities::FieldDefinitionsSharedPtr> FieldDef
391  = Exp[0]->GetFieldDefinitions();
392  std::vector<std::vector<NekDouble> > FieldData(FieldDef.size());
393 
394  vector<string> outname;
395  outname.push_back("Q");
396 
397  for (j = 0; j < nfields + addfields; ++j)
398  {
399  for (i = 0; i < FieldDef.size(); ++i)
400  {
401  if (j >= nfields)
402  {
403  FieldDef[i]->m_fields.push_back(outname[j - nfields]);
404  }
405  else
406  {
407  FieldDef[i]->m_fields.push_back(fielddef[i]->m_fields[j]);
408  }
409  Exp[j]->AppendFieldData(FieldDef[i], FieldData[i]);
410  }
411  }
412  LibUtilities::Write(out, FieldDef, FieldData);
413  //-----------------------------------------------
414 
415  return 0;
416 }
417