Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
MeshGraph1D.cpp
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // File: MeshGraph1D.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:
33 //
34 //
35 ////////////////////////////////////////////////////////////////////////////////
36 
40 #include <tinyxml.h>
41 
42 using namespace std;
43 
44 namespace Nektar
45 {
46  namespace SpatialDomains
47  {
48 
49  MeshGraph1D::MeshGraph1D()
50  {
51  }
52 
53  MeshGraph1D::MeshGraph1D(const LibUtilities::SessionReaderSharedPtr &pSession,
54  const DomainRangeShPtr &rng)
55  : MeshGraph(pSession,rng)
56  {
57  ReadGeometry (pSession->GetDocument());
58  ReadExpansions(pSession->GetDocument());
59  }
60 
62  {
63  }
64 
65  // \brief Read segments (and general MeshGraph) given filename.
66  void MeshGraph1D::ReadGeometry(const std::string &infilename)
67  {
68  TiXmlDocument doc(infilename);
69 
70  bool loadOkay = doc.LoadFile();
71  std::stringstream errstr;
72  errstr << "Unable to load file: " << infilename << "\n";
73  errstr << doc.ErrorDesc() << " (Line " << doc.ErrorRow()
74  << ", Column " << doc.ErrorCol() << ")";
75  ASSERTL0(loadOkay, errstr.str());
76 
77  ReadGeometry(doc);
78  }
79 
80  // \brief Read segments (and general MeshGraph) given TiXmlDocument.
81  void MeshGraph1D::ReadGeometry(TiXmlDocument &doc)
82  {
83  // Read mesh first
85  TiXmlHandle docHandle(&doc);
86 
87  TiXmlElement* mesh = NULL;
88 
89  /// Look for all geometry related data in GEOMETRY block.
90  mesh = docHandle.FirstChildElement("NEKTAR").FirstChildElement("GEOMETRY").Element();
91 
92  ASSERTL0(mesh, "Unable to find GEOMETRY tag in file.");
93 
94  ReadCurves(doc);
95  ReadElements(doc);
96  ReadComposites(doc);
97  ReadDomain(doc);
98  }
99 
100  void MeshGraph1D::ReadElements(TiXmlDocument &doc)
101  {
102  /// We know we have it since we made it this far.
103  TiXmlHandle docHandle(&doc);
104  TiXmlElement* mesh = docHandle.FirstChildElement("NEKTAR").FirstChildElement("GEOMETRY").Element();
105  TiXmlElement* field = NULL;
106 
107  /// Look for elements in ELEMENT block.
108  field = mesh->FirstChildElement("ELEMENT");
109 
110  ASSERTL0(field, "Unable to find ELEMENT tag in file.");
111 
112  /// All elements are of the form: "<S ID = n> ... </S>", with
113  /// ? being the element type.
114 
115  TiXmlElement *segment = field->FirstChildElement("S");
117 
118  while (segment)
119  {
120  string IsCompressed;
121  segment->QueryStringAttribute("COMPRESSED",&IsCompressed);
122 
123  if(IsCompressed.size())
124  {
125  ASSERTL0(boost::iequals(IsCompressed,
127  "Compressed formats do not match. Expected :"
129  + " but got " + std::string(IsCompressed));
130 
131  // Extract the face body
132  TiXmlNode* child = segment->FirstChild();
133  ASSERTL0(child, "Unable to extract the data from "
134  "the compressed face tag.");
135 
136  std::string str;
137  if (child->Type() == TiXmlNode::TINYXML_TEXT)
138  {
139  str += child->ToText()->ValueStr();
140  }
141 
142  int indx;
143 
144  std::vector<LibUtilities::MeshEdge> data;
146  data);
147 
148  for(int i = 0; i < data.size(); ++i)
149  {
150  indx = data[i].id;
151 
152  /// See if this face has curves.
153  it = m_curvedEdges.find(indx);
154 
155  PointGeomSharedPtr vertices[2] = {
156  GetVertex(data[i].v0), GetVertex(data[i].v1)};
157  SegGeomSharedPtr seg;
158 
159  if (it == m_curvedEdges.end())
160  {
162  indx, m_spaceDimension, vertices);
163  seg->SetGlobalID(indx); // Set global mesh id
164  }
165  else
166  {
168  indx, m_spaceDimension,
169  vertices, it->second);
170  seg->SetGlobalID(indx); //Set global mesh id
171  }
172  seg->SetGlobalID(indx);
173  m_segGeoms[indx] = seg;
174  }
175  }
176  else
177  {
178 
179  int indx;
180  int err = segment->QueryIntAttribute("ID", &indx);
181  ASSERTL0(err == TIXML_SUCCESS, "Unable to read element attribute ID.");
182 
183  TiXmlNode* elementChild = segment->FirstChild();
184  while(elementChild && elementChild->Type() != TiXmlNode::TINYXML_TEXT)
185  {
186  elementChild = elementChild->NextSibling();
187  }
188 
189  ASSERTL0(elementChild, "Unable to read element description body.");
190  std::string elementStr = elementChild->ToText()->ValueStr();
191 
192  /// Parse out the element components corresponding to type of element.
193  /// Read two vertex numbers
194  int vertex1, vertex2;
195  std::istringstream elementDataStrm(elementStr.c_str());
196 
197  try
198  {
199  elementDataStrm >> vertex1;
200  elementDataStrm >> vertex2;
201 
202  ASSERTL0(!elementDataStrm.fail(), (std::string("Unable to read element data for SEGMENT: ") + elementStr).c_str());
203 
204  PointGeomSharedPtr vertices[2] = {GetVertex(vertex1), GetVertex(vertex2)};
205  SegGeomSharedPtr seg;
206  it = m_curvedEdges.find(indx);
207 
208  if (it == m_curvedEdges.end())
209  {
211  seg->SetGlobalID(indx); // Set global mesh id
212  }
213  else
214  {
215  seg = MemoryManager<SegGeom>::AllocateSharedPtr(indx, m_spaceDimension, vertices, it->second);
216  seg->SetGlobalID(indx); //Set global mesh id
217  }
218  seg->SetGlobalID(indx);
219  m_segGeoms[indx] = seg;
220  }
221  catch(...)
222  {
224  (std::string("Unable to read element data for segment: ") + elementStr).c_str());
225  }
226  }
227  /// Keep looking for additional segments
228  segment = segment->NextSiblingElement("S");
229  }
230  }
231 
232  void MeshGraph1D::ReadComposites(TiXmlDocument &doc)
233  {
234  TiXmlHandle docHandle(&doc);
235 
236  /// We know we have it since we made it this far.
237  TiXmlElement* mesh = docHandle.FirstChildElement("NEKTAR").FirstChildElement("GEOMETRY").Element();
238  TiXmlElement* field = NULL;
239 
240  ASSERTL0(mesh, "Unable to find GEOMETRY tag in file.");
241 
242  /// Look for elements in ELEMENT block.
243  field = mesh->FirstChildElement("COMPOSITE");
244 
245  ASSERTL0(field, "Unable to find COMPOSITE tag in file.");
246 
247  TiXmlElement *node = field->FirstChildElement("C");
248 
249  // Sequential counter for the composite numbers.
250  int nextCompositeNumber = -1;
251 
252  while (node)
253  {
254  /// All elements are of the form: "<? ID="#"> ... </?>", with
255  /// ? being the element type.
256 
257  nextCompositeNumber++;
258 
259  int indx;
260  int err = node->QueryIntAttribute("ID", &indx);
261  ASSERTL0(err == TIXML_SUCCESS, "Unable to read attribute ID.");
262  //ASSERTL0(indx == nextCompositeNumber, "Composite IDs must begin with zero and be sequential.");
263 
264  TiXmlNode* compositeChild = node->FirstChild();
265  // This is primarily to skip comments that may be present.
266  // Comments appear as nodes just like elements.
267  // We are specifically looking for text in the body
268  // of the definition.
269  while(compositeChild && compositeChild->Type() != TiXmlNode::TINYXML_TEXT)
270  {
271  compositeChild = compositeChild->NextSibling();
272  }
273 
274  ASSERTL0(compositeChild, "Unable to read composite definition body.");
275  std::string compositeStr = compositeChild->ToText()->ValueStr();
276 
277  /// Parse out the element components corresponding to type of element.
278 
279  std::istringstream compositeDataStrm(compositeStr.c_str());
280 
281  try
282  {
283  bool first = true;
284  std::string prevCompositeElementStr;
285 
286  while (!compositeDataStrm.fail())
287  {
288  std::string compositeElementStr;
289  compositeDataStrm >> compositeElementStr;
290 
291  if (!compositeDataStrm.fail())
292  {
293  if (first)
294  {
295  first = false;
296 
297  Composite curVector = MemoryManager<std::vector<GeometrySharedPtr> >::AllocateSharedPtr();
298  m_meshComposites[indx] = curVector;
299  }
300 
301  if (compositeElementStr.length() > 0)
302  {
303  ResolveGeomRef(prevCompositeElementStr, compositeElementStr, m_meshComposites[indx]);
304  }
305  prevCompositeElementStr = compositeElementStr;
306  }
307  }
308  }
309  catch(...)
310  {
312  (std::string("Unable to read COMPOSITE data for composite: ") + compositeStr).c_str());
313  }
314 
315  /// Keep looking for additional composite definitions.
316  node = node->NextSiblingElement("C");
317  }
318 
319  ASSERTL0(nextCompositeNumber >= 0, "At least one composite must be specified.");
320  }
321 
322 
323  // Take the string that is the composite reference and find the
324  // pointer to the Geometry object corresponding to it.
325 
326  // Only allow segments to be grouped for 1D mesh.
327  void MeshGraph1D::ResolveGeomRef(const std::string &prevToken, const std::string &token,
328  Composite& composite)
329  {
330  try
331  {
332  std::istringstream tokenStream(token);
333  std::istringstream prevTokenStream(prevToken);
334 
335  char type;
336  char prevType;
337 
338  tokenStream >> type;
339 
340  std::string::size_type indxBeg = token.find_first_of('[') + 1;
341  std::string::size_type indxEnd = token.find_last_of(']') - 1;
342 
343  ASSERTL0(indxBeg <= indxEnd, (std::string("Error reading index definition:") + token).c_str());
344 
345  std::string indxStr = token.substr(indxBeg, indxEnd - indxBeg + 1);
346 
347  typedef vector<unsigned int> SeqVectorType;
348  SeqVectorType seqVector;
349 
350  if (!ParseUtils::GenerateSeqVector(indxStr.c_str(), seqVector))
351  {
352  NEKERROR(ErrorUtil::efatal, (std::string("Ill-formed sequence definition: ") + indxStr).c_str());
353  }
354 
355  prevTokenStream >> prevType;
356 
357  // All composites must be of the same dimension.
358  bool validSequence = (prevToken.empty() || // No previous, then current is just fine.
359  (type == 'V' && prevType == 'V') ||
360  (type == 'S' && prevType == 'S'));
361 
362  ASSERTL0(validSequence, (std::string("Invalid combination of composite items: ")
363  + type + " and " + prevType + ".").c_str());
364 
365  switch(type)
366  {
367  case 'V': // Vertex
368  for (SeqVectorType::iterator iter=seqVector.begin(); iter!=seqVector.end(); ++iter)
369  {
370  if (m_vertSet.find(*iter) == m_vertSet.end())
371  {
372  char errStr[16] = "";
373  ::sprintf(errStr, "%d", *iter);
374  NEKERROR(ErrorUtil::ewarning, (std::string("Unknown vertex index: ") + errStr).c_str());
375  }
376  else
377  {
378  composite->push_back(m_vertSet[*iter]);
379  }
380  }
381  break;
382 
383  case 'S': // Segment
384  for (SeqVectorType::iterator iter=seqVector.begin(); iter!=seqVector.end(); ++iter)
385  {
386  if (m_segGeoms.find(*iter) == m_segGeoms.end())
387  {
388  char errStr[16] = "";
389  ::sprintf(errStr, "%d", *iter);
390  NEKERROR(ErrorUtil::ewarning, (std::string("Unknown segment index: ") + errStr).c_str());
391  }
392  else
393  {
394  composite->push_back(m_segGeoms[*iter]);
395  }
396  }
397  break;
398 
399  default:
400  NEKERROR(ErrorUtil::efatal, (std::string("Unrecognized composite token: ") + token).c_str());
401  }
402  }
403  catch(...)
404  {
405  NEKERROR(ErrorUtil::efatal, (std::string("Problem processing composite token: ") + token).c_str());
406  }
407 
408  return;
409  }
410 
411  }; //end of namespace
412 }; //end of namespace
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:198
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mod...
Definition: ErrorUtil.hpp:191
void ResolveGeomRef(const std::string &prevToken, const std::string &token, Composite &composite)
static boost::shared_ptr< DataType > AllocateSharedPtr()
Allocate a shared pointer from the memory pool.
void ReadCurves(TiXmlDocument &doc)
Definition: MeshGraph.cpp:1181
General purpose memory allocation routines with the ability to allocate from thread specific memory p...
PointGeomSharedPtr GetVertex(int id)
Definition: MeshGraph.h:587
STL namespace.
boost::shared_ptr< SessionReader > SessionReaderSharedPtr
Definition: MeshPartition.h:51
static bool GenerateSeqVector(const char *const str, std::vector< unsigned int > &vec)
Definition: ParseUtils.hpp:79
virtual void ReadGeometry(const std::string &infilename)
Read will read the meshgraph vertices given a filename.
Definition: MeshGraph.cpp:235
void ReadGeometry(const std::string &infilename)
Read will read the meshgraph vertices given a filename.
Definition: MeshGraph1D.cpp:66
boost::shared_ptr< SegGeom > SegGeomSharedPtr
Definition: Geometry2D.h:60
boost::shared_ptr< DomainRange > DomainRangeShPtr
Definition: MeshGraph.h:157
boost::shared_ptr< GeometryVector > Composite
Definition: MeshGraph.h:114
void ReadElements(TiXmlDocument &doc)
StandardMatrixTag boost::call_traits< LhsDataType >::const_reference rhs typedef NekMatrix< LhsDataType, StandardMatrixTag >::iterator iterator
Base class for a spectral/hp element mesh.
Definition: MeshGraph.h:186
void ReadDomain(TiXmlDocument &doc)
Definition: MeshGraph.cpp:1087
void ReadComposites(TiXmlDocument &doc)
void ReadExpansions(const std::string &infilename)
Read the expansions given the XML file path.
Definition: MeshGraph.cpp:585
boost::shared_ptr< PointGeom > PointGeomSharedPtr
Definition: Geometry.h:60
int ZlibDecodeFromBase64Str(std::string &in64, std::vector< T > &out)
Definition: CompressData.h:243