[cig-commits] r7343 - in short/3D/PyLith/trunk: . libsrc/meshio unittests/libtests/meshio

brad at geodynamics.org brad at geodynamics.org
Thu Jun 21 08:43:45 PDT 2007


Author: brad
Date: 2007-06-21 08:43:44 -0700 (Thu, 21 Jun 2007)
New Revision: 7343

Modified:
   short/3D/PyLith/trunk/TODO
   short/3D/PyLith/trunk/libsrc/meshio/MeshIOAscii.cc
   short/3D/PyLith/trunk/libsrc/meshio/MeshIOAscii.hh
   short/3D/PyLith/trunk/unittests/libtests/meshio/TestMeshIO.cc
Log:
Added ability to include C++ styled comments in MeshIOAscii files. Fixed bug in TestMeshIO.cc where mesh factory was not cleared (reset) after creating the mesh.

Modified: short/3D/PyLith/trunk/TODO
===================================================================
--- short/3D/PyLith/trunk/TODO	2007-06-21 15:39:19 UTC (rev 7342)
+++ short/3D/PyLith/trunk/TODO	2007-06-21 15:43:44 UTC (rev 7343)
@@ -2,14 +2,9 @@
 CURRENT ISSUES
 ======================================================================
 
-  2. Debug mesh distribution. [MATT]
+  1. Debug mesh distribution. [MATT]
 
-  3. Debug Maxwell viscoelastic model. [CHARLES]
-
-  5. Add check to make sure every material supplied by user exists in
-  the mesh.
-  
-  6. Need PETSc settings for absolute convergence (i.e., don't want to
+  3. Need PETSc settings for absolute convergence (i.e., don't want to
   max out iterations when there is no increment in displacement).
 
 ----------------------------------------------------------------------
@@ -43,9 +38,12 @@
 add check in fields manager when returning solution (solution name
 shouldn't be empty)
 
-add ability to have comments in meshio and spatial data files (if easy
-to do, would use '#' as delimiter).
+add ability to have comments in spatial data files (if easy
+to do, would use '//' as delimiter).
 
+Add check to make sure every material supplied by user exists in the
+mesh.
+  
 Create meshes for benchmarks
 
   strike-slip

Modified: short/3D/PyLith/trunk/libsrc/meshio/MeshIOAscii.cc
===================================================================
--- short/3D/PyLith/trunk/libsrc/meshio/MeshIOAscii.cc	2007-06-21 15:39:19 UTC (rev 7342)
+++ short/3D/PyLith/trunk/libsrc/meshio/MeshIOAscii.cc	2007-06-21 15:43:44 UTC (rev 7343)
@@ -15,6 +15,7 @@
 #include "MeshIOAscii.hh" // implementation of class methods
 
 #include "pylith/utils/array.hh" // USES double_array, int_array, string_vector
+#include "pylith/utils/LineParser.hh" // USES LineParser
 
 #include "journal/info.h" // USES journal::info_t
 
@@ -43,6 +44,7 @@
 { // destructor
 } // destructor
 
+#include <iostream>
 // ----------------------------------------------------------------------
 // Unpickle mesh
 void
@@ -69,71 +71,93 @@
       throw std::runtime_error(msg.str());
     } // if
 
+    utils::LineParser parser(filein, "//");
+
     std::string token;
+    std::istringstream buffer;
     const int maxIgnore = 1024;
-    filein >> token;
+
+    buffer.str(parser.next());
+    buffer >> token;
     if (0 != strcasecmp(token.c_str(), "mesh")) {
       std::ostringstream msg;
       msg << "Expected 'mesh' token but encountered '" << token << "'\n";
       throw std::runtime_error(msg.str());
-    }
+    } // if
 
     bool readDim = false;
     bool readCells = false;
     bool readVertices = false;
     bool builtMesh = false;
 
-    filein.ignore(maxIgnore, '{');
-    filein >> token;
-    while (filein.good() && token != "}") {
-      if (0 == strcasecmp(token.c_str(), "dimension")) {
-        filein.ignore(maxIgnore, '=');
-        filein >> meshDim;
-        readDim = true;
-      } else if (0 == strcasecmp(token.c_str(), "use-index-zero")) {
-        filein.ignore(maxIgnore, '=');
-        std::string flag = "";
-        filein >> flag;
-        if (0 == strcasecmp(flag.c_str(), "true"))
-          useIndexZero(true);
-        else
-          useIndexZero(false);
-      } else if (0 == strcasecmp(token.c_str(), "vertices")) {
-        filein.ignore(maxIgnore, '{');
-        _readVertices(filein, &coordinates, &numVertices, &spaceDim);
-        readVertices = true;
-      } else if (0 == strcasecmp(token.c_str(), "cells")) {
-        filein.ignore(maxIgnore, '{');
-        _readCells(filein, &cells, &materialIds, &numCells, &numCorners);
-        readCells = true;
-      } else if (0 == strcasecmp(token.c_str(), "group")) {
-        std::string name;
-        GroupPtType type;
-        int numPoints = 0;
-        int_array points;
+    try {
+      parser.eatws();
+      buffer.str(parser.next());
+      buffer.clear();
+      buffer >> token;
+      while (buffer.good() && token != "}") {
+	if (0 == strcasecmp(token.c_str(), "dimension")) {
+	  buffer.ignore(maxIgnore, '=');
+	  buffer >> meshDim;
+	  readDim = true;
+	} else if (0 == strcasecmp(token.c_str(), "use-index-zero")) {
+	  buffer.ignore(maxIgnore, '=');
+	  std::string flag = "";
+	  buffer >> flag;
+	  if (0 == strcasecmp(flag.c_str(), "true"))
+	    useIndexZero(true);
+	  else
+	    useIndexZero(false);
+	} else if (0 == strcasecmp(token.c_str(), "vertices")) {
+	  _readVertices(parser, &coordinates, &numVertices, &spaceDim);
+	  readVertices = true;
+	} else if (0 == strcasecmp(token.c_str(), "cells")) {
+	  _readCells(parser, &cells, &materialIds, &numCells, &numCorners);
+	  readCells = true;
+	} else if (0 == strcasecmp(token.c_str(), "group")) {
+	  std::string name;
+	  GroupPtType type;
+	  int numPoints = 0;
+	  int_array points;
 
-        if (!builtMesh)
-          throw std::runtime_error("Both 'vertices' and 'cells' must "
-                                   "precede any groups in mesh file.");
-        filein.ignore(maxIgnore, '{');
-        _readGroup(filein, &points, &type, &name);
-        _setGroup(name, type, points);
-      } else {
-        std::ostringstream msg;
-        msg << "Could not parse '" << token << "' into a mesh setting.";
-        throw std::runtime_error(msg.str());  
-      } // else
+	  if (!builtMesh)
+	    throw std::runtime_error("Both 'vertices' and 'cells' must "
+				     "precede any groups in mesh file.");
+	  //buffer.ignore(maxIgnore, '{');
+	  _readGroup(parser, &points, &type, &name);
+	  _setGroup(name, type, points);
+	} else {
+	  std::ostringstream msg;
+	  msg << "Could not parse '" << token << "' into a mesh setting.";
+	  throw std::runtime_error(msg.str());  
+	} // else
 
-      if (readDim && readCells && readVertices && !builtMesh) {
-        // Can now build mesh
-        _buildMesh(coordinates, numVertices, spaceDim,
-                   cells, numCells, numCorners, meshDim);
-        _setMaterials(materialIds);
-        builtMesh = true;
-      } // if
+	if (readDim && readCells && readVertices && !builtMesh) {
+	  // Can now build mesh
+	  _buildMesh(coordinates, numVertices, spaceDim,
+		     cells, numCells, numCorners, meshDim);
+	  _setMaterials(materialIds);
+	  builtMesh = true;
+	} // if
 
-      filein >> token;
-    } // while
+	parser.eatws();
+	buffer.str(parser.next());
+	buffer.clear();
+	buffer >> token;
+      } // while
+      if (token != "}")
+	throw std::runtime_error("I/O error occurred while parsing mesh " \
+				 "tokens.");
+    } catch (const std::exception& err) {
+      std::ostringstream msg;
+      msg << "Error occurred while reading PyLith mesh ASCII file '"
+	  << _filename << "'.\n"
+	  << err.what();
+    } catch (...) {
+      std::ostringstream msg;      
+      msg << "Unknown I/O error while reading PyLith mesh ASCII file '"
+	  << _filename << "'.\n";
+    } // catch
     filein.close();
   } else {
     _buildMesh(coordinates, numVertices, spaceDim,
@@ -177,7 +201,7 @@
 // ----------------------------------------------------------------------
 // Read mesh vertices.
 void
-pylith::meshio::MeshIOAscii::_readVertices(std::istream& filein,
+pylith::meshio::MeshIOAscii::_readVertices(utils::LineParser& parser,
 					   double_array* coordinates,
 					   int* numVertices, 
 					   int* numDims) const
@@ -187,15 +211,19 @@
   assert(0 != numDims);
 
   std::string token;
+  std::istringstream buffer;
   const int maxIgnore = 1024;
-  filein >> token;
-  while (filein.good() && token != "}") {
+  parser.eatws();
+  buffer.str(parser.next());
+  buffer.clear();
+  buffer >> token;
+  while (buffer.good() && token != "}") {
     if (0 == strcasecmp(token.c_str(), "dimension")) {
-      filein.ignore(maxIgnore, '=');
-      filein >> *numDims;
+      buffer.ignore(maxIgnore, '=');
+      buffer >> *numDims;
     } else if (0 == strcasecmp(token.c_str(), "count")) {
-      filein.ignore(maxIgnore, '=');
-      filein >> *numVertices;
+      buffer.ignore(maxIgnore, '=');
+      buffer >> *numVertices;
     } else if (0 == strcasecmp(token.c_str(), "coordinates")) {
       const int size = (*numVertices) * (*numDims);
       if (0 == size) {
@@ -203,25 +231,28 @@
 	  "Tokens 'dimension' and 'count' must precede 'coordinates'.";
 	throw std::runtime_error(msg);
       } // if
-      
-      filein.ignore(maxIgnore, '{');
       coordinates->resize(size);
       int label;
       for (int iVertex=0, i=0; iVertex < *numVertices; ++iVertex) {
-	filein >> label;
+	buffer.str(parser.next());
+	buffer.clear();
+	buffer >> label;
 	for (int iDim=0; iDim < *numDims; ++iDim)
-	  filein >> (*coordinates)[i++];
+	  buffer >> (*coordinates)[i++];
       } // for
-      filein.ignore(maxIgnore, '}');
+      parser.ignore('}');
     } else {
       std::ostringstream msg;
       msg << "Could not parse '" << token << "' into a vertices setting.";
       throw std::runtime_error(msg.str());
     } // else
-    filein >> token;
+    parser.eatws();
+    buffer.str(parser.next());
+    buffer.clear();
+    buffer >> token;
   } // while
-  if (!filein.good())
-    throw std::runtime_error("I/O error while parsing vertices settings.");
+  if (token != "}")
+    throw std::runtime_error("I/O error while parsing vertices.");
 } // _readVertices
 
 // ----------------------------------------------------------------------
@@ -257,7 +288,7 @@
 // ----------------------------------------------------------------------
 // Read mesh cells.
 void
-pylith::meshio::MeshIOAscii::_readCells(std::istream& filein,
+pylith::meshio::MeshIOAscii::_readCells(utils::LineParser& parser,
 					int_array* cells,
 					int_array* materialIds,
 					int* numCells, 
@@ -271,15 +302,19 @@
   int dimension = 0;
 
   std::string token;
+  std::istringstream buffer;
   const int maxIgnore = 1024;
-  filein >> token;
-  while (filein.good() && token != "}") {
+  parser.eatws();
+  buffer.str(parser.next());
+  buffer.clear();
+  buffer >> token;
+  while (buffer.good() && token != "}") {
     if (0 == strcasecmp(token.c_str(), "num-corners")) {
-      filein.ignore(maxIgnore, '=');
-      filein >> *numCorners;
+      buffer.ignore(maxIgnore, '=');
+      buffer >> *numCorners;
     } else if (0 == strcasecmp(token.c_str(), "count")) {
-      filein.ignore(maxIgnore, '=');
-      filein >> *numCells;
+      buffer.ignore(maxIgnore, '=');
+      buffer >> *numCells;
     } else if (0 == strcasecmp(token.c_str(), "simplices")) {
       const int size = (*numCells) * (*numCorners);
       if (0 == size) {
@@ -287,14 +322,14 @@
 	  "Tokens 'num-corners' and 'count' must precede 'cells'.";
 	throw std::runtime_error(msg);
       } // if
-      
-      filein.ignore(maxIgnore, '{');
       cells->resize(size);
       int label;
       for (int iCell=0, i=0; iCell < *numCells; ++iCell) {
-	filein >> label;
+	buffer.str(parser.next());
+	buffer.clear();
+	buffer >> label;
 	for (int iCorner=0; iCorner < *numCorners; ++iCorner)
-	  filein >> (*cells)[i++];
+	  buffer >> (*cells)[i++];
       } // for
       if (!useIndexZero()) {
 	// if files begins with index 1, then decrement to index 0
@@ -302,8 +337,7 @@
 	for (int i=0; i < size; ++i)
 	  --(*cells)[i];
       } // if
-      
-      filein.ignore(maxIgnore, '}');
+      parser.ignore('}');
     } else if (0 == strcasecmp(token.c_str(), "material-ids")) {
       if (0 == *numCells) {
 	const char* msg =
@@ -311,23 +345,27 @@
 	throw std::runtime_error(msg);
       } // if
       const int size = *numCells;
-      filein.ignore(maxIgnore, '{');
       materialIds->resize(size);
       int label = 0;
       for (int iCell=0; iCell < *numCells; ++iCell) {
-	filein >> label;
-	filein >> (*materialIds)[iCell];
+	buffer.str(parser.next());
+	buffer.clear();
+	buffer >> label;
+	buffer >> (*materialIds)[iCell];
       } // for
-      filein.ignore(maxIgnore, '}');
+      parser.ignore('}');
     } else {
       std::ostringstream msg;
       msg << "Could not parse '" << token << "' into an cells setting.";
       throw std::runtime_error(msg.str());
     } // else
-    filein >> token;
+    parser.eatws();
+    buffer.str(parser.next());
+    buffer.clear();
+    buffer >> token;
   } // while
-  if (!filein.good())
-    throw std::runtime_error("I/O error while parsing cells settings.");
+  if (token != "}")
+    throw std::runtime_error("I/O error while parsing cells.");
 
   // If no materials given, assign each cell material identifier of 0
   if (0 == materialIds->size() && *numCells > 0) {
@@ -379,7 +417,7 @@
 // ----------------------------------------------------------------------
 // Read mesh group.
 void
-pylith::meshio::MeshIOAscii::_readGroup(std::istream& filein,
+pylith::meshio::MeshIOAscii::_readGroup(utils::LineParser& parser,
 					int_array* points,
 					GroupPtType* type,
 					std::string* name) const
@@ -389,20 +427,24 @@
   assert(0 != name);
 
   std::string token;
+  std::istringstream buffer;
   const int maxIgnore = 1024;
   int numPoints = -1;
-  filein >> token;
-  while (filein.good() && token != "}") {
+  parser.eatws();
+  buffer.str(parser.next());
+  buffer.clear();
+  buffer >> token;
+  while (buffer.good() && token != "}") {
     if (0 == strcasecmp(token.c_str(), "name")) {
-      filein.ignore(maxIgnore, '=');
-      filein >> std::ws;
-      char buffer[maxIgnore];
-      filein.get(buffer, maxIgnore, '\n');
-      *name = buffer;
+      buffer.ignore(maxIgnore, '=');
+      buffer >> std::ws;
+      char cbuffer[maxIgnore];
+      buffer.get(cbuffer, maxIgnore, '\n');
+      *name = cbuffer;
     } else if (0 == strcasecmp(token.c_str(), "type")) {
       std::string typeName;
-      filein.ignore(maxIgnore, '=');
-      filein >> typeName;
+      buffer.ignore(maxIgnore, '=');
+      buffer >> typeName;
       if (typeName == groupTypeNames[VERTEX])
         *type = VERTEX;
       else if (typeName == groupTypeNames[CELL])
@@ -413,27 +455,32 @@
         throw std::runtime_error(msg.str());
       } // else
     } else if (0 == strcasecmp(token.c_str(), "count")) {
-      filein.ignore(maxIgnore, '=');
-      filein >> numPoints;
+      buffer.ignore(maxIgnore, '=');
+      buffer >> numPoints;
     } else if (0 == strcasecmp(token.c_str(), "indices")) {
       if (-1 == numPoints) {
         std::ostringstream msg;
         msg << "Tokens 'count' must precede 'indices'.";
         throw std::runtime_error(msg.str());
       } // if
-      filein.ignore(maxIgnore, '{');
       points->resize(numPoints);
-      for (int i=0; i < numPoints; ++i)
-        filein >> (*points)[i];
-      filein.ignore(maxIgnore, '}');
+      for (int i=0; i < numPoints; ++i) {
+	buffer.str(parser.next());
+	buffer.clear();
+        buffer >> (*points)[i];
+      } // for
+      parser.ignore('}');
     } else {
       std::ostringstream msg;
       msg << "Could not parse '" << token << "' into a group setting.";
       throw std::runtime_error(msg.str());
     } // else
-    filein >> token;
+    parser.eatws();
+    buffer.str(parser.next());
+    buffer.clear();
+    buffer >> token;
   } // while
-  if (!filein.good())
+  if (token != "}")
     throw std::runtime_error("I/O error while parsing group settings.");
 } // _readGroup
 

Modified: short/3D/PyLith/trunk/libsrc/meshio/MeshIOAscii.hh
===================================================================
--- short/3D/PyLith/trunk/libsrc/meshio/MeshIOAscii.hh	2007-06-21 15:39:19 UTC (rev 7342)
+++ short/3D/PyLith/trunk/libsrc/meshio/MeshIOAscii.hh	2007-06-21 15:43:44 UTC (rev 7343)
@@ -22,6 +22,10 @@
   namespace meshio {
     class MeshIOAscii;
   } // meshio
+
+  namespace utils {
+    class LineParser;
+  } // utils
 } // pylith
 
 class pylith::meshio::MeshIOAscii : public MeshIO
@@ -63,12 +67,12 @@
 
   /** Read mesh vertices.
    *
-   * @param filein Input stream
+   * @param parser Input parser.
    * @param coordinates Pointer to array of vertex coordinates
    * @param numVertices Pointer to number of vertices
    * @param spaceDim Pointer to dimension of coordinates vector space
    */
-  void _readVertices(std::istream& filein,
+  void _readVertices(utils::LineParser& parser,
 		     double_array* coordinates,
 		     int* numVertices,
 		     int* spaceDim) const;
@@ -81,13 +85,13 @@
   
   /** Read mesh cells.
    *
-   * @param filein Input stream
+   * @param parser Input parser.
    * @param pCells Pointer to array of indices of cell vertices
    * @param pMaterialIds Pointer to array of material identifiers
    * @param pNumCells Pointer to number of cells
    * @param pNumCorners Pointer to number of corners
    */
-  void _readCells(std::istream& filein,
+  void _readCells(utils::LineParser& parser,
 		  int_array* pCells,
 		  int_array* pMaterialIds,
 		  int* numCells,
@@ -104,10 +108,10 @@
   
   /** Read a point group.
    *
-   * @param filein Input stream
+   * @param parser Input parser.
    * @param mesh The mesh
    */
-  void _readGroup(std::istream& filein,
+  void _readGroup(utils::LineParser& parser,
 		  int_array* points,
                   GroupPtType* type,
                   std::string* name) const;

Modified: short/3D/PyLith/trunk/unittests/libtests/meshio/TestMeshIO.cc
===================================================================
--- short/3D/PyLith/trunk/unittests/libtests/meshio/TestMeshIO.cc	2007-06-21 15:39:19 UTC (rev 7342)
+++ short/3D/PyLith/trunk/unittests/libtests/meshio/TestMeshIO.cc	2007-06-21 15:43:44 UTC (rev 7343)
@@ -91,7 +91,8 @@
       throw std::logic_error("Could not parse group type.");
     (*mesh)->allocate(groupField);
   } // for
-
+  (*mesh)->getFactory()->clear();
+ 
   return mesh;
 } // _createMesh
 



More information about the cig-commits mailing list