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