Nektar++
ExtractCriticalLayer.cpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File: ExtractCriticalLayer.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: Extract location of critical layer from streak file
32 //
33 ///////////////////////////////////////////////////////////////////////////////
34 
35 #include <cstdio>
36 #include <cstdlib>
37 
38 #include <MultiRegions/ExpList.h>
39 #include <MultiRegions/ExpList2D.h>
40 
42  Array<OneD, NekDouble> &xc,
43  Array<OneD, NekDouble> &yc, NekDouble cr,
44  NekDouble trans);
45 
46 int main(int argc, char *argv[])
47 {
48  int i, j;
49  NekDouble cr = 0;
50 
51  if (argc != 3)
52  {
53  fprintf(stderr,
54  "Usage: ./ExtractCriticalLayer meshfile fieldfile \n");
55  exit(1);
56  }
57 
58  //------------------------------------------------------------
59  // Create Session file.
61  LibUtilities::SessionReader::CreateInstance(argc, argv);
62  //-----------------------------------------------------------
63 
64  //-------------------------------------------------------------
65  // Read in mesh from input file
67  SpatialDomains::MeshGraph::Read(vSession);
68  //------------------------------------------------------------
69 
70  //-------------------------------------------------------------
71  // Define Streak Expansion
73 
74  streak = MemoryManager<MultiRegions::ExpList2D>::AllocateSharedPtr(
75  vSession, graphShPt);
76 
77  //---------------------------------------------------------------
78 
79  //----------------------------------------------
80  // Import field file.
81  string fieldfile(argv[argc - 1]);
82  vector<SpatialDomains::FieldDefinitionsSharedPtr> fielddef;
83  vector<vector<NekDouble>> fielddata;
84  graphShPt->Import(fieldfile, fielddef, fielddata);
85  //----------------------------------------------
86 
87  //----------------------------------------------
88  // Copy data from field file
89  string streak_field("w");
90  for (unsigned int i = 0; i < fielddata.size(); ++i)
91  {
92  streak->ExtractDataToCoeffs(fielddef[i], fielddata[i], streak_field);
93  }
94  //----------------------------------------------
95 
96  int npts;
97  vSession->LoadParameter("NumCriticalLayerPts", npts, 30);
98  Array<OneD, NekDouble> x_c(npts);
99  Array<OneD, NekDouble> y_c(npts);
100  NekDouble xc, yc;
101 
102  NekDouble trans;
103  vSession->LoadParameter("WidthOfLayers", trans, 0.1);
104 
105  Computestreakpositions(streak, x_c, y_c, cr, trans);
106 
107  cout << "# x_c y_c" << endl;
108  for (i = 0; i < npts; ++i)
109  {
110  fprintf(stdout, "%12.10lf %12.10lf \n", x_c[i], y_c[i]);
111  // cout << x_c[i] << " " << y_c[i] << endl;
112  }
113 }
114 
116  Array<OneD, NekDouble> &xc,
117  Array<OneD, NekDouble> &yc, NekDouble cr,
118  NekDouble trans)
119 {
120  int i;
121  int npts = xc.size();
122 
123  int nq = streak->GetTotPoints();
124  Array<OneD, NekDouble> derstreak(nq);
125  Array<OneD, NekDouble> x(nq);
126  Array<OneD, NekDouble> y(nq);
127  streak->GetCoords(x, y);
128 
129  streak->BwdTrans(streak->GetCoeffs(), streak->UpdatePhys());
130  streak->PhysDeriv(MultiRegions::eY, streak->GetPhys(), derstreak);
131 
132  // set intiial xc to be equispaced over mesh and yc to be zero
133  NekDouble x_max = Vmath::Vmax(nq, x, 1);
134  NekDouble x_min = Vmath::Vmin(nq, x, 1);
135 
136  for (i = 0; i < npts; ++i)
137  {
138  xc[i] = x_min + (x_max - x_min) * i / ((NekDouble)(npts - 1));
139  yc[i] = 0.0;
140  }
141 
142  int elmtid, offset, cnt;
143  NekDouble U, dU;
144  NekDouble F;
145  NekDouble ConvTol = 1e-9;
146  NekDouble CoordTol = 1e-5;
147  int maxiter = 100;
148  Array<OneD, NekDouble> coord(2);
149  NekDouble ytmp;
150  // Do Newton iteration on y direction
151  cerr << "[";
152  for (int e = 0; e < npts; e++)
153  {
154  coord[0] = xc[e];
155  coord[1] = yc[e];
156  cout << e << endl;
157  if (!(e % 10))
158  {
159  cerr << ".";
160  }
161 
162  F = 1000;
163  cnt = 0;
164  int its = 0;
165  int attempt = 0;
166  // cout<<"start guess: x="<<xc[e]<<" y="<<yc[e]<<endl;
167  while (abs(F) > 0.000000001)
168  {
169  ytmp = coord[1];
170  elmtid = streak->GetExpIndex(coord, 0.00001);
171  offset = streak->GetPhys_Offset(elmtid);
172  U = streak->GetExp(elmtid)->PhysEvaluate(coord, streak->GetPhys() +
173  offset);
174  dU =
175  streak->GetExp(elmtid)->PhysEvaluate(coord, derstreak + offset);
176  coord[1] = coord[1] - (U - cr) / dU;
177  F = U - cr;
178  ASSERTL0(coord[0] == xc[e], " x coordinate must remain the same");
179  // stvalues[e] = streak->GetExp(elmtid)->PhysEvaluate(coord,
180  // streak->GetPhys() +offset );
181  // cout<<"elmtid="<<elmtid<<" x="<<coord[0]<<" y="<<coord[1]<<"
182  // stvalue="<<U<<" dU="<<dU<<endl;
183  if (abs(coord[1]) > 1)
184  {
185  coord[1] = ytmp + 0.01;
186  elmtid = streak->GetExpIndex(coord, 0.00001);
187  offset = streak->GetPhys_Offset(elmtid);
188  NekDouble Utmp = streak->GetExp(elmtid)->PhysEvaluate(
189  coord, streak->GetPhys() + offset);
190  NekDouble dUtmp = streak->GetExp(elmtid)->PhysEvaluate(
191  coord, derstreak + offset);
192  coord[1] = coord[1] - (Utmp - cr) / dUtmp;
193 
194  if ((abs(Utmp - cr) > abs(F)) || (abs(coord[1]) > 1))
195  {
196  coord[1] = ytmp - 0.01;
197  }
198 
199  attempt++;
200  }
201  else
202  {
203  ASSERTL0(abs(coord[1]) <= 1, " y value out of bound +/-1");
204  }
205 
206  its++;
207  if (its > 1000 && abs(F) < 0.0001)
208  {
209  cout << "warning streak position obtained with precision:" << F
210  << endl;
211  break;
212  }
213  else if (its > 1000)
214  {
215  ASSERTL0(false, "no convergence after 1000 iterations");
216  }
217  }
218 
219  yc[e] = coord[1];
220  }
221  cerr << "]" << endl;
222 
223  // output to interface file
224  FILE *fp = fopen("interfacedat.geo", "w");
225 
226  NekDouble y_max = Vmath::Vmax(nq, y, 1);
227  NekDouble y_min = Vmath::Vmin(nq, y, 1);
228 
229  cnt = 1;
230  fprintf(fp, "Point(%d)={%12.10lf,%12.10lf,0,1.0}; \n", cnt++, x_min, y_min);
231  fprintf(fp, "Point(%d)={%12.10lf,%12.10lf,0,1.0}; \n", cnt++, x_max, y_min);
232  fprintf(fp, "Point(%d)={%12.10lf,%12.10lf,0,1.0}; \n", cnt++, x_max, y_max);
233  fprintf(fp, "Point(%d)={%12.10lf,%12.10lf,0,1.0}; \n", cnt++, x_min, y_max);
234 
235  for (i = 0; i < npts; ++i)
236  {
237  fprintf(fp, "Point(%d)={%12.10lf,%12.10lf,0,1.0}; \n", cnt++, xc[i],
238  yc[i]);
239  }
240 
241  fclose(fp);
242 
243  // output to interface_up file as bend of vertical shift and 45 degrees
244  // shift
245  fp = fopen("interfacedat_up.geo", "w");
246 
247  NekDouble nx, ny, norm;
248 
249  fprintf(fp, "Point(%d)={%12.10lf,%12.10lf,0,1.0}; \n", cnt++, xc[0],
250  yc[0] + trans);
251 
252  for (i = 1; i < npts - 1; ++i)
253  {
254  norm = sqrt((xc[i + 1] - xc[i - 1]) * (xc[i + 1] - xc[i - 1]) +
255  (yc[i + 1] - yc[i - 1]) * (yc[i + 1] - yc[i - 1]));
256  nx = (yc[i - 1] - yc[i + 1]) / norm;
257  ny = (xc[i + 1] - xc[i - 1]) / norm;
258 
259  fprintf(fp, "Point(%d)={%12.10lf,%12.10lf,0,1.0}; \n", cnt++,
260  xc[i] + nx * trans, yc[i] + ny * trans);
261  }
262 
263  fprintf(fp, "Point(%d)={%12.10lf,%12.10lf,0,1.0}; \n", cnt++, xc[npts - 1],
264  yc[npts - 1] + trans);
265 
266  // output to interface_up file as bend of vertical shift and 45 degrees
267  // shift
268  fp = fopen("interfacedat_dn.geo", "w");
269 
270  trans = -trans;
271 
272  fprintf(fp, "Point(%d)={%12.10lf,%12.10lf,0,1.0}; \n", cnt++, xc[0],
273  yc[0] + trans);
274 
275  for (i = 1; i < npts - 1; ++i)
276  {
277  norm = sqrt((xc[i + 1] - xc[i - 1]) * (xc[i + 1] - xc[i - 1]) +
278  (yc[i + 1] - yc[i - 1]) * (yc[i + 1] - yc[i - 1]));
279  nx = (yc[i - 1] - yc[i + 1]) / norm;
280  ny = (xc[i + 1] - xc[i - 1]) / norm;
281 
282  fprintf(fp, "Point(%d)={%12.10lf,%12.10lf,0,1.0}; \n", cnt++,
283  xc[i] + nx * trans, yc[i] + ny * trans);
284  }
285 
286  fprintf(fp, "Point(%d)={%12.10lf,%12.10lf,0,1.0}; \n", cnt++, xc[npts - 1],
287  yc[npts - 1] + trans);
288 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:215
int main(int argc, char *argv[])
void Computestreakpositions(MultiRegions::ExpListSharedPtr &streak, Array< OneD, NekDouble > &xc, Array< OneD, NekDouble > &yc, NekDouble cr, NekDouble trans)
std::shared_ptr< SessionReader > SessionReaderSharedPtr
std::shared_ptr< ExpList > ExpListSharedPtr
Shared pointer to an ExpList object.
std::shared_ptr< MeshGraph > MeshGraphSharedPtr
Definition: MeshGraph.h:172
double NekDouble
T Vmin(int n, const T *x, const int incx)
Return the minimum element in x - called vmin to avoid conflict with min.
Definition: Vmath.cpp:1050
T Vmax(int n, const T *x, const int incx)
Return the maximum element in x – called vmax to avoid conflict with max.
Definition: Vmath.cpp:945
scalarT< T > abs(scalarT< T > in)
Definition: scalar.hpp:298
scalarT< T > sqrt(scalarT< T > in)
Definition: scalar.hpp:294