Nektar++
FilterHistoryPoints.cpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File FilterHistoryPoints.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: Outputs values at specific points during time-stepping.
33 //
34 ///////////////////////////////////////////////////////////////////////////////
35 
37 #include <iomanip>
39 
40 namespace Nektar
41 {
42  namespace SolverUtils
43  {
45 
46  /**
47  *
48  */
51  const std::map<std::string, std::string> &pParams) :
52  Filter(pSession)
53  {
54  if (pParams.find("OutputFile") == pParams.end())
55  {
56  m_outputFile = m_session->GetSessionName();
57  }
58  else
59  {
60  ASSERTL0(!(pParams.find("OutputFile")->second.empty()),
61  "Missing parameter 'OutputFile'.");
62  m_outputFile = pParams.find("OutputFile")->second;
63  }
64  if (!(m_outputFile.length() >= 4
65  && m_outputFile.substr(m_outputFile.length() - 4) == ".his"))
66  {
67  m_outputFile += ".his";
68  }
69 
70  if (pParams.find("OutputFrequency") == pParams.end())
71  {
73  }
74  else
75  {
76  m_outputFrequency = atoi(pParams.find("OutputFrequency")->second.c_str());
77  }
78 
79 
80  m_session->MatchSolverInfo("Homogeneous","1D",m_isHomogeneous1D,false);
81 
83  {
84  if (pParams.find("OutputPlane") == pParams.end())
85  {
86  m_outputPlane = 0;
87  }
88  else
89  {
90  m_outputPlane = atoi(pParams.find("OutputPlane")->second.c_str());
91  }
92  }
93 
94  ASSERTL0(pParams.find("Points") != pParams.end(),
95  "Missing parameter 'Points'.");
96  m_historyPointStream.str(pParams.find("Points")->second);
97  m_index = 0;
98  }
99 
100 
101  /**
102  *
103  */
105  {
106 
107  }
108 
109 
110  /**
111  *
112  */
115  const NekDouble &time)
116  {
118  "No history points in stream.");
119 
120  m_index = 0;
121  m_historyList.clear();
122 
123  // Read history points
124  Array<OneD, NekDouble> gloCoord(3,0.0);
125  int dim = pFields[0]->GetGraph()->GetSpaceDimension();
126  int i = 0;
127  while (!m_historyPointStream.fail())
128  {
129  m_historyPointStream >> gloCoord[0]
130  >> gloCoord[1]
131  >> gloCoord[2];
132  if(m_isHomogeneous1D) // overwrite with plane z
133  {
134  NekDouble Z = (pFields[0]->GetHomogeneousBasis()
135  ->GetZ())[m_outputPlane];
136  if(fabs(gloCoord[2]-Z) > NekConstants::kVertexTheSameDouble)
137  {
138  cout << "Reseting History point from " << gloCoord[2]
139  << " to " << Z << endl;
140  }
141  gloCoord[2] = Z;
142  }
143 
144  if (!m_historyPointStream.fail())
145  {
148  ::AllocateSharedPtr(dim, i, gloCoord[0],
149  gloCoord[1], gloCoord[2]);
150 
151  m_historyPoints.push_back(vert);
152  ++i;
153  }
154  }
155 
156 
157  // Determine the unique process responsible for each history point
158  // For points on a partition boundary, must select a single process
159  LibUtilities::CommSharedPtr vComm = pFields[0]->GetComm();
160  int vRank = vComm->GetRank();
161  int vHP = m_historyPoints.size();
162  Array<OneD, int> procList(vHP, -1 );
163  Array<OneD, int> idList (vHP, -1 );
164  Array<OneD, NekDouble> dist (vHP, 1e16);
165  Array<OneD, NekDouble> dist_loc(vHP, 1e16);
166  std::vector<Array<OneD, NekDouble> > LocCoords;
167 
168  // Find the nearest element on this process to which the history
169  // point could belong and note down the distance from the element
170  // and the process ID.
171  for (i = 0; i < vHP; ++i)
172  {
173  Array<OneD, NekDouble> locCoords(3);
174  m_historyPoints[i]->GetCoords( gloCoord[0],
175  gloCoord[1],
176  gloCoord[2]);
177 
178  // Determine the expansion and local coordinates
179  idList[i] = pFields[0]->GetExpIndex(gloCoord,locCoords,
181 
182  // Save Local coordinates for later
183  LocCoords.push_back(locCoords);
184 
185  // For those points for which a potential nearby element exists
186  // compute the perp. distance from the point to the element and
187  // store in the distances array.
188  if (idList[i] != -1)
189  {
191  pFields[0]->GetExp(idList[i])->GetGeom();
192  StdRegions::StdExpansionSharedPtr e = g->GetXmap();
193  Array<OneD, NekDouble> coordVals(e->GetTotPoints());
194  dist_loc[i] = 0.0;
195  for (int j = 0; j < g->GetCoordim(); ++j)
196  {
197  e->BwdTrans(g->GetCoeffs(j), coordVals);
198  NekDouble x = e->PhysEvaluate(locCoords, coordVals)
199  - gloCoord[j];
200  dist_loc[i] += x*x;
201  }
202  }
203  }
204 
205  // Reduce distances of points from elements, keeping the smallest
206  // distance.
207  Vmath::Vcopy(vHP, dist_loc, 1, dist, 1);
208  vComm->AllReduce(dist, LibUtilities::ReduceMin);
209 
210  // If multiple processes find they are the nearest (e.g. point lies
211  // on a partition boundary, we will choose the process of highest
212  // rank.
213  for (i = 0; i < vHP; ++i)
214  {
215  if (dist_loc[i] == dist[i])
216  {
217  // Set element id to Vid of m_history point for later use
218  m_historyPoints[i]->SetVid(idList[i]);
219  }
220  else
221  {
222  // This history point is not handled by this process
223  idList[i] = -1;
224  }
225 
226  // If a matching element is found on this process, note the
227  // process ID
228  if (idList[i] != -1)
229  {
231  {
232  int j;
234  = pFields[0]->GetZIDs();
235  for(j = 0; j < IDs.num_elements(); ++j)
236  {
237  if(IDs[j] == m_outputPlane)
238  {
239  break;
240  }
241  }
242 
243  if(j != IDs.num_elements())
244  {
245  m_outputPlane = j;
246  procList[i] = vRank;
247  }
248  }
249  else
250  {
251  procList[i] = vRank;
252  }
253  }
254  }
255 
256  // Reduce process IDs for all history points. The process with
257  // largest rank will handle the history point in the case where the
258  // distance was the same.
259  vComm->AllReduce(procList, LibUtilities::ReduceMax);
260 
261  // Determine the element in which each history point resides.
262  // If point is not in mesh (on this process), id is -1.
263  for (i = 0; i < vHP; ++i)
264  {
265  // If point lies on partition boundary, only the proc with max
266  // rank retains possession.
267  if (procList[i] != vRank)
268  {
269  idList[i] = -1;
270  }
271 
272  // If the current process owns this history point, add it to its
273  // local list of history points.
274  if (idList[i] != -1)
275  {
277  m_historyList.push_back(
280  (m_historyPoints[i], LocCoords[i]));
281  }
282  }
283 
284  // Collate the element ID list across processes and check each
285  // history point is allocated to a process
286  vComm->AllReduce(idList, LibUtilities::ReduceMax);
287  if (vComm->GetRank() == 0)
288  {
289  for (i = 0; i < vHP; ++i)
290  {
291  m_historyPoints[i]->GetCoords( gloCoord[0],
292  gloCoord[1],
293  gloCoord[2]);
294 
295  // Write an error if no process owns history point
296  ASSERTL0(idList[i] != -1,
297  "History point "
298  + boost::lexical_cast<std::string>(gloCoord[0])
299  + ", "
300  + boost::lexical_cast<std::string>(gloCoord[1])
301  + ", "
302  + boost::lexical_cast<std::string>(gloCoord[2])
303  + " cannot be found in the mesh.");
304 
305  // Print a warning if a process owns it but it is not close
306  // enough to the element.
307  if (dist[i] > NekConstants::kGeomFactorsTol)
308  {
309  cout << "Warning: History point " << i << " at ("
310  << gloCoord[0] << "," << gloCoord[1] << ","
311  << gloCoord[2] << ") lies a distance of "
312  << sqrt(dist[i]) << " from the manifold." << endl;
313  }
314  }
315 
316  // Open output stream
317  m_outputStream.open(m_outputFile.c_str());
318  m_outputStream << "# History data for variables (:";
319 
320  for (i = 0; i < pFields.num_elements(); ++i)
321  {
322  m_outputStream << m_session->GetVariable(i) <<",";
323  }
324 
326  {
327  m_outputStream << ") at points:";
328  }
329  else
330  {
331  m_outputStream << ") at points:" << endl;
332  }
333 
334  for (i = 0; i < vHP; ++i)
335  {
336  m_historyPoints[i]->GetCoords( gloCoord[0],
337  gloCoord[1],
338  gloCoord[2]);
339 
340  m_outputStream << "# \t" << i;
341  m_outputStream.width(8);
342  m_outputStream << gloCoord[0];
343  m_outputStream.width(8);
344  m_outputStream << gloCoord[1];
345  m_outputStream.width(8);
346  m_outputStream << gloCoord[2];
347  m_outputStream << endl;
348  }
349 
351  {
352  m_outputStream << "(in Wavespace)" << endl;
353  }
354  }
355  v_Update(pFields, time);
356  }
357 
358 
359  /**
360  *
361  */
363  {
364  // Only output every m_outputFrequency.
365  if ((m_index++) % m_outputFrequency)
366  {
367  return;
368  }
369 
370  int j = 0;
371  int k = 0;
372  int numPoints = m_historyPoints.size();
373  int numFields = pFields.num_elements();
374  LibUtilities::CommSharedPtr vComm = pFields[0]->GetComm();
375  Array<OneD, NekDouble> data(numPoints*numFields, 0.0);
376  Array<OneD, NekDouble> gloCoord(3, 0.0);
377  std::list<std::pair<SpatialDomains::PointGeomSharedPtr, Array<OneD, NekDouble> > >::iterator x;
378 
379  Array<OneD, NekDouble> physvals;
380  Array<OneD, NekDouble> locCoord;
381  int expId;
382 
383  // Pull out data values field by field
384  for (j = 0; j < numFields; ++j)
385  {
387  {
388  for (k = 0, x = m_historyList.begin(); x != m_historyList.end();
389  ++x, ++k)
390  {
391  locCoord = (*x).second;
392  expId = (*x).first->GetVid();
393 
394  physvals = pFields[j]->GetPlane(m_outputPlane)->UpdatePhys() + pFields[j]->GetPhys_Offset(expId);
395 
396  // transform elemental data if required.
397  if(pFields[j]->GetPhysState() == false)
398  {
399  pFields[j]->GetPlane(m_outputPlane)->GetExp(expId)->BwdTrans(pFields[j]->GetPlane(m_outputPlane)->GetCoeffs() + pFields[j]->GetCoeff_Offset(expId),physvals);
400  }
401 
402  // interpolate point can do with zero plane methods
403  data[m_historyLocalPointMap[k]*numFields+j] = pFields[j]->GetExp(expId)->StdPhysEvaluate(locCoord,physvals);
404 
405  }
406  }
407  else
408  {
409  for (k = 0, x = m_historyList.begin(); x != m_historyList.end(); ++x, ++k)
410  {
411  locCoord = (*x).second;
412  expId = (*x).first->GetVid();
413 
414  physvals = pFields[j]->UpdatePhys() + pFields[j]->GetPhys_Offset(expId);
415 
416  // transform elemental data if required.
417  if(pFields[j]->GetPhysState() == false)
418  {
419  pFields[j]->GetExp(expId)->BwdTrans(pFields[j]->GetCoeffs() + pFields[j]->GetCoeff_Offset(expId),physvals);
420  }
421 
422  // interpolate point
423  data[m_historyLocalPointMap[k]*numFields+j] = pFields[j]->GetExp(expId)->StdPhysEvaluate(locCoord,physvals);
424  }
425  }
426  }
427 
428  // Exchange history data
429  // This could be improved to reduce communication but works for now
430  vComm->AllReduce(data, LibUtilities::ReduceSum);
431 
432  // Only the root process writes out history data
433  if (vComm->GetRank() == 0)
434  {
435 
436  // Write data values point by point
437  for (k = 0; k < m_historyPoints.size(); ++k)
438  {
439  m_outputStream.width(8);
440  m_outputStream << setprecision(6) << time;
441  for (int j = 0; j < numFields; ++j)
442  {
443  m_outputStream.width(25);
444  m_outputStream << setprecision(16) << data[k*numFields+j];
445  }
446  m_outputStream << endl;
447  }
448  }
449  }
450 
451 
452  /**
453  *
454  */
456  {
457  if (pFields[0]->GetComm()->GetRank() == 0)
458  {
459  m_outputStream.close();
460  }
461  }
462 
463 
464  /**
465  *
466  */
468  {
469  return true;
470  }
471  }
472 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:135
static boost::shared_ptr< DataType > AllocateSharedPtr()
Allocate a shared pointer from the memory pool.
static FilterSharedPtr create(const LibUtilities::SessionReaderSharedPtr &pSession, const std::map< std::string, std::string > &pParams)
Creates an instance of this class.
static const NekDouble kVertexTheSameDouble
boost::shared_ptr< SessionReader > SessionReaderSharedPtr
Definition: MeshPartition.h:50
boost::shared_ptr< Comm > CommSharedPtr
Pointer to a Communicator object.
Definition: Comm.h:53
unsigned int m_outputPlane
plane to take history point from if using a homogeneous1D expansion
SpatialDomains::PointGeomVector m_historyPoints
static std::string className
Name of the class.
double NekDouble
LibUtilities::SessionReaderSharedPtr m_session
Definition: Filter.h:76
virtual SOLVER_UTILS_EXPORT void v_Finalise(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time)
virtual SOLVER_UTILS_EXPORT void v_Initialise(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time)
StandardMatrixTag boost::call_traits< LhsDataType >::const_reference rhs typedef NekMatrix< LhsDataType, StandardMatrixTag >::iterator iterator
virtual SOLVER_UTILS_EXPORT bool v_IsTimeDependent()
SOLVER_UTILS_EXPORT FilterHistoryPoints(const LibUtilities::SessionReaderSharedPtr &pSession, const std::map< std::string, std::string > &pParams)
static const NekDouble kGeomFactorsTol
FilterFactory & GetFilterFactory()
Definition: Filter.cpp:42
boost::shared_ptr< StdExpansion > StdExpansionSharedPtr
virtual SOLVER_UTILS_EXPORT void v_Update(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time)
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.cpp:1016
boost::shared_ptr< Geometry > GeometrySharedPtr
Definition: Geometry.h:53
std::list< std::pair< SpatialDomains::PointGeomSharedPtr, Array< OneD, NekDouble > > > m_historyList
boost::shared_ptr< PointGeom > PointGeomSharedPtr
Definition: Geometry.h:60
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, tDescription pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:215