Nektar++
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  int indx;
118  int err = segment->QueryIntAttribute("ID", &indx);
119  ASSERTL0(err == TIXML_SUCCESS, "Unable to read element attribute ID.");
120 
121  TiXmlNode* elementChild = segment->FirstChild();
122  while(elementChild && elementChild->Type() != TiXmlNode::TINYXML_TEXT)
123  {
124  elementChild = elementChild->NextSibling();
125  }
126 
127  ASSERTL0(elementChild, "Unable to read element description body.");
128  std::string elementStr = elementChild->ToText()->ValueStr();
129 
130  /// Parse out the element components corresponding to type of element.
131  /// Read two vertex numbers
132  int vertex1, vertex2;
133  std::istringstream elementDataStrm(elementStr.c_str());
134 
135  try
136  {
137  elementDataStrm >> vertex1;
138  elementDataStrm >> vertex2;
139 
140  ASSERTL0(!elementDataStrm.fail(), (std::string("Unable to read element data for SEGMENT: ") + elementStr).c_str());
141 
142  PointGeomSharedPtr vertices[2] = {GetVertex(vertex1), GetVertex(vertex2)};
143  SegGeomSharedPtr seg;
144  it = m_curvedEdges.find(indx);
145 
146  if (it == m_curvedEdges.end())
147  {
149  seg->SetGlobalID(indx); // Set global mesh id
150  }
151  else
152  {
153  seg = MemoryManager<SegGeom>::AllocateSharedPtr(indx, m_spaceDimension, vertices, it->second);
154  seg->SetGlobalID(indx); //Set global mesh id
155  }
156  seg->SetGlobalID(indx);
157  m_segGeoms[indx] = seg;
158  }
159  catch(...)
160  {
162  (std::string("Unable to read element data for segment: ") + elementStr).c_str());
163  }
164 
165  /// Keep looking for additional segments
166  segment = segment->NextSiblingElement("S");
167  }
168  }
169 
170  void MeshGraph1D::ReadComposites(TiXmlDocument &doc)
171  {
172  TiXmlHandle docHandle(&doc);
173 
174  /// We know we have it since we made it this far.
175  TiXmlElement* mesh = docHandle.FirstChildElement("NEKTAR").FirstChildElement("GEOMETRY").Element();
176  TiXmlElement* field = NULL;
177 
178  ASSERTL0(mesh, "Unable to find GEOMETRY tag in file.");
179 
180  /// Look for elements in ELEMENT block.
181  field = mesh->FirstChildElement("COMPOSITE");
182 
183  ASSERTL0(field, "Unable to find COMPOSITE tag in file.");
184 
185  TiXmlElement *node = field->FirstChildElement("C");
186 
187  // Sequential counter for the composite numbers.
188  int nextCompositeNumber = -1;
189 
190  while (node)
191  {
192  /// All elements are of the form: "<? ID="#"> ... </?>", with
193  /// ? being the element type.
194 
195  nextCompositeNumber++;
196 
197  int indx;
198  int err = node->QueryIntAttribute("ID", &indx);
199  ASSERTL0(err == TIXML_SUCCESS, "Unable to read attribute ID.");
200  //ASSERTL0(indx == nextCompositeNumber, "Composite IDs must begin with zero and be sequential.");
201 
202  TiXmlNode* compositeChild = node->FirstChild();
203  // This is primarily to skip comments that may be present.
204  // Comments appear as nodes just like elements.
205  // We are specifically looking for text in the body
206  // of the definition.
207  while(compositeChild && compositeChild->Type() != TiXmlNode::TINYXML_TEXT)
208  {
209  compositeChild = compositeChild->NextSibling();
210  }
211 
212  ASSERTL0(compositeChild, "Unable to read composite definition body.");
213  std::string compositeStr = compositeChild->ToText()->ValueStr();
214 
215  /// Parse out the element components corresponding to type of element.
216 
217  std::istringstream compositeDataStrm(compositeStr.c_str());
218 
219  try
220  {
221  bool first = true;
222  std::string prevCompositeElementStr;
223 
224  while (!compositeDataStrm.fail())
225  {
226  std::string compositeElementStr;
227  compositeDataStrm >> compositeElementStr;
228 
229  if (!compositeDataStrm.fail())
230  {
231  if (first)
232  {
233  first = false;
234 
235  Composite curVector = MemoryManager<std::vector<GeometrySharedPtr> >::AllocateSharedPtr();
236  m_meshComposites[indx] = curVector;
237  }
238 
239  if (compositeElementStr.length() > 0)
240  {
241  ResolveGeomRef(prevCompositeElementStr, compositeElementStr, m_meshComposites[indx]);
242  }
243  prevCompositeElementStr = compositeElementStr;
244  }
245  }
246  }
247  catch(...)
248  {
250  (std::string("Unable to read COMPOSITE data for composite: ") + compositeStr).c_str());
251  }
252 
253  /// Keep looking for additional composite definitions.
254  node = node->NextSiblingElement("C");
255  }
256 
257  ASSERTL0(nextCompositeNumber >= 0, "At least one composite must be specified.");
258  }
259 
260 
261  // Take the string that is the composite reference and find the
262  // pointer to the Geometry object corresponding to it.
263 
264  // Only allow segments to be grouped for 1D mesh.
265  void MeshGraph1D::ResolveGeomRef(const std::string &prevToken, const std::string &token,
266  Composite& composite)
267  {
268  try
269  {
270  std::istringstream tokenStream(token);
271  std::istringstream prevTokenStream(prevToken);
272 
273  char type;
274  char prevType;
275 
276  tokenStream >> type;
277 
278  std::string::size_type indxBeg = token.find_first_of('[') + 1;
279  std::string::size_type indxEnd = token.find_last_of(']') - 1;
280 
281  ASSERTL0(indxBeg <= indxEnd, (std::string("Error reading index definition:") + token).c_str());
282 
283  std::string indxStr = token.substr(indxBeg, indxEnd - indxBeg + 1);
284 
285  typedef vector<unsigned int> SeqVectorType;
286  SeqVectorType seqVector;
287 
288  if (!ParseUtils::GenerateSeqVector(indxStr.c_str(), seqVector))
289  {
290  NEKERROR(ErrorUtil::efatal, (std::string("Ill-formed sequence definition: ") + indxStr).c_str());
291  }
292 
293  prevTokenStream >> prevType;
294 
295  // All composites must be of the same dimension.
296  bool validSequence = (prevToken.empty() || // No previous, then current is just fine.
297  (type == 'V' && prevType == 'V') ||
298  (type == 'S' && prevType == 'S'));
299 
300  ASSERTL0(validSequence, (std::string("Invalid combination of composite items: ")
301  + type + " and " + prevType + ".").c_str());
302 
303  switch(type)
304  {
305  case 'V': // Vertex
306  for (SeqVectorType::iterator iter=seqVector.begin(); iter!=seqVector.end(); ++iter)
307  {
308  if (m_vertSet.find(*iter) == m_vertSet.end())
309  {
310  char errStr[16] = "";
311  ::sprintf(errStr, "%d", *iter);
312  NEKERROR(ErrorUtil::ewarning, (std::string("Unknown vertex index: ") + errStr).c_str());
313  }
314  else
315  {
316  composite->push_back(m_vertSet[*iter]);
317  }
318  }
319  break;
320 
321  case 'S': // Segment
322  for (SeqVectorType::iterator iter=seqVector.begin(); iter!=seqVector.end(); ++iter)
323  {
324  if (m_segGeoms.find(*iter) == m_segGeoms.end())
325  {
326  char errStr[16] = "";
327  ::sprintf(errStr, "%d", *iter);
328  NEKERROR(ErrorUtil::ewarning, (std::string("Unknown segment index: ") + errStr).c_str());
329  }
330  else
331  {
332  composite->push_back(m_segGeoms[*iter]);
333  }
334  }
335  break;
336 
337  default:
338  NEKERROR(ErrorUtil::efatal, (std::string("Unrecognized composite token: ") + token).c_str());
339  }
340  }
341  catch(...)
342  {
343  NEKERROR(ErrorUtil::efatal, (std::string("Problem processing composite token: ") + token).c_str());
344  }
345 
346  return;
347  }
348 
349  }; //end of namespace
350 }; //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:1103
General purpose memory allocation routines with the ability to allocate from thread specific memory p...
PointGeomSharedPtr GetVertex(int id)
Definition: MeshGraph.h:567
boost::shared_ptr< SessionReader > SessionReaderSharedPtr
Definition: MeshPartition.h:50
static bool GenerateSeqVector(const char *const str, std::vector< unsigned int > &vec)
Definition: ParseUtils.hpp:78
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:154
boost::shared_ptr< GeometryVector > Composite
Definition: MeshGraph.h:111
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:183
void ReadDomain(TiXmlDocument &doc)
Definition: MeshGraph.cpp:1009
void ReadComposites(TiXmlDocument &doc)
void ReadExpansions(const std::string &infilename)
Read the expansions given the XML file path.
Definition: MeshGraph.cpp:525
boost::shared_ptr< PointGeom > PointGeomSharedPtr
Definition: Geometry.h:60