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