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

brad at geodynamics.org brad at geodynamics.org
Fri May 20 16:10:51 PDT 2011


Author: brad
Date: 2011-05-20 16:10:51 -0700 (Fri, 20 May 2011)
New Revision: 18403

Added:
   short/3D/PyLith/trunk/unittests/libtests/meshio/TestXdmf.cc
   short/3D/PyLith/trunk/unittests/libtests/meshio/TestXdmf.hh
Modified:
   short/3D/PyLith/trunk/libsrc/meshio/Xdmf.cc
   short/3D/PyLith/trunk/libsrc/meshio/Xdmf.hh
   short/3D/PyLith/trunk/unittests/libtests/meshio/Makefile.am
Log:
More work on Xdmf.

Modified: short/3D/PyLith/trunk/libsrc/meshio/Xdmf.cc
===================================================================
--- short/3D/PyLith/trunk/libsrc/meshio/Xdmf.cc	2011-05-20 23:10:35 UTC (rev 18402)
+++ short/3D/PyLith/trunk/libsrc/meshio/Xdmf.cc	2011-05-20 23:10:51 UTC (rev 18403)
@@ -41,10 +41,13 @@
 // ----------------------------------------------------------------------
 // Write Xdmf file associated with HDF5 file.
 void
-pylith::meshio::Xdmf::write(const char* filenameXdfm,
+pylith::meshio::Xdmf::write(const char* filenameXdmf,
 			    const char* filenameHDF5)
 { // write
-  std::string cellType = 0;
+  assert(filenameXdmf);
+  assert(filenameHDF5);
+
+  std::string cellType;
   int numCells = 0;
   int numCorners = 0;
   int numVertices = 0;
@@ -58,7 +61,7 @@
   int_array fieldNumPoints;
   int_array fieldFiberDim;
 
-  if (spaceDim == 1) {
+  if (1 == spaceDim) {
     std::cout
       << "WARNING: Xdmf grids not defined for 1-D domains.\n"
       << "Skipping creation of Xdmf file associated with HDF5 file '"
@@ -66,6 +69,15 @@
     return;
   } // if
 
+  _file.open(filenameXdmf);
+  if (!_file.is_open() || !_file.good()) {
+    std::ostringstream msg;
+    msg << "Could not open Xdmf file '" << filenameXdmf
+	<< "' for writing metadata forHDF5 file '"
+	<< filenameHDF5 << "'.\n";
+    throw std::runtime_error(msg.str());
+  } // if
+
   _file
     << "<?xml version=\"1.0\" ?>\n"
     << "<!DOCTYPE Xdmf SYSTEM \"Xdmf.dtd\" [\n"
@@ -84,7 +96,7 @@
   _writeTimeStamps(timeStamps, numTimeSteps);
 
   _file
-    << "<Grid Name=\"domain\" GridType=\"Uniform\">\n";
+    << "    <Grid Name=\"domain\" GridType=\"Uniform\">\n";
   for (int iTimeStep=0; iTimeStep < numTimeSteps; ++iTimeStep) {
     _writeGridTopology(cellType.c_str(), numCells);
     _writeGridGeometry(spaceDim);
@@ -106,10 +118,10 @@
 			    iTimeStep);
       } // if/else
     } // for
+    _file << "      </Grid>\n";
   } // for
-
+  
   _file
-    << "      </Grid>\n"
     << "    </Grid>\n"
     << "  </Domain>\n"
     << "</Xdmf>\n";
@@ -122,6 +134,7 @@
 pylith::meshio::Xdmf::_writeDomainCells(const int numCells,
 					const int numCorners)
 { // _writeDomainCells
+  assert(_file.is_open() && _file.good());
   _file
     << "    <DataItem Name=\"cells\"\n"
     << "	      ItemType=\"Uniform\"\n"
@@ -139,6 +152,8 @@
 pylith::meshio::Xdmf::_writeDomainVertices(const int numVertices,
 					   const int spaceDim)
 { // _writeDomainVertices
+  assert(_file.is_open() && _file.good());
+
   _file
     << "    <DataItem Name=\"vertices\"\n"
     << "	      Format=\"HDF\"\n"
@@ -153,6 +168,8 @@
 pylith::meshio::Xdmf::_writeTimeStamps(const double* timeStamps,
 				       const int numTimeSteps)
 { // _writeTimeStamps
+  assert(_file.is_open() && _file.good());
+
   if (numTimeSteps > 0) {
     assert(timeStamps);
   } // if
@@ -164,6 +181,8 @@
 pylith::meshio::Xdmf::_writeGridTopology(const char* cellType,
 					 const int numCells)
 { // _writeGridTopology
+  assert(_file.is_open() && _file.good());
+
   _file
     << "	<Topology\n"
     << "	   TopologyType=\"Triangle\"\n"
@@ -179,6 +198,7 @@
 void
 pylith::meshio::Xdmf::_writeGridGeometry(const int spaceDim)
 { // _writeGridGeometry
+  assert(_file.is_open() && _file.good());
   assert(2 == spaceDim || 3 == spaceDim);
 
   const char* geomType = (spaceDim == 3) ? "XYZ" : "XY";
@@ -203,6 +223,8 @@
 						   const int iTime,
 						   const int component)
 { // _writeGridAttribute
+  assert(_file.is_open() && _file.good());
+
   std::string componentName = "unknown";
   switch (component) {
   case 0:
@@ -260,6 +282,8 @@
 					  const int fiberDim,
 					  const int iTime)
 { // _writeGridAttribute
+  assert(_file.is_open() && _file.good());
+
   _file
     << "	<Attribute\n"
     << "	   Name=\"" << name << "\"\n"

Modified: short/3D/PyLith/trunk/libsrc/meshio/Xdmf.hh
===================================================================
--- short/3D/PyLith/trunk/libsrc/meshio/Xdmf.hh	2011-05-20 23:10:35 UTC (rev 18402)
+++ short/3D/PyLith/trunk/libsrc/meshio/Xdmf.hh	2011-05-20 23:10:51 UTC (rev 18403)
@@ -61,7 +61,7 @@
    * @param filenameXdmf Name of Xdmf file.
    * @param filenameHDF5 Name of HDF5 file.
    */
-  void write(const char* filenameXdfm,
+  void write(const char* filenameXdmf,
 	     const char* filenameHDF5);
 
 // PRIVATE METHODS ------------------------------------------------------

Modified: short/3D/PyLith/trunk/unittests/libtests/meshio/Makefile.am
===================================================================
--- short/3D/PyLith/trunk/unittests/libtests/meshio/Makefile.am	2011-05-20 23:10:35 UTC (rev 18402)
+++ short/3D/PyLith/trunk/unittests/libtests/meshio/Makefile.am	2011-05-20 23:10:51 UTC (rev 18403)
@@ -242,7 +242,8 @@
 	TestDataWriterHDF5ExtBCMesh.cc \
 	TestDataWriterHDF5ExtBCMeshCases.cc \
 	TestDataWriterHDF5ExtFaultMesh.cc \
-	TestDataWriterHDF5ExtFaultMeshCases.cc
+	TestDataWriterHDF5ExtFaultMeshCases.cc \
+	TestXdmf.cc
 
 
   noinst_HEADERS += \
@@ -264,7 +265,8 @@
 	TestDataWriterHDF5ExtBCMesh.hh \
 	TestDataWriterHDF5ExtBCMeshCases.hh \
 	TestDataWriterHDF5ExtFaultMesh.hh \
-	TestDataWriterHDF5ExtFaultMeshCases.hh
+	TestDataWriterHDF5ExtFaultMeshCases.hh \
+	TestXdmf.hh
 
   testmeshio_LDADD += -lhdf5
 endif

Added: short/3D/PyLith/trunk/unittests/libtests/meshio/TestXdmf.cc
===================================================================
--- short/3D/PyLith/trunk/unittests/libtests/meshio/TestXdmf.cc	                        (rev 0)
+++ short/3D/PyLith/trunk/unittests/libtests/meshio/TestXdmf.cc	2011-05-20 23:10:51 UTC (rev 18403)
@@ -0,0 +1,106 @@
+// -*- C++ -*-
+//
+// ----------------------------------------------------------------------
+//
+// Brad T. Aagaard, U.S. Geological Survey
+// Charles A. Williams, GNS Science
+// Matthew G. Knepley, University of Chicago
+//
+// This code was developed as part of the Computational Infrastructure
+// for Geodynamics (http://geodynamics.org).
+//
+// Copyright (c) 2010 University of California, Davis
+//
+// See COPYING for license information.
+//
+// ----------------------------------------------------------------------
+//
+
+#include <portinfo>
+
+#include "TestXdmf.hh" // Implementation of class methods
+
+#include "pylith/meshio/Xdmf.hh" // USES Xdmf
+
+// ----------------------------------------------------------------------
+CPPUNIT_TEST_SUITE_REGISTRATION( pylith::meshio::TestXdmf );
+
+// ----------------------------------------------------------------------
+// Test constructor.
+void
+pylith::meshio::TestXdmf::testConstructor(void)
+{ // testConstructor
+  Xdmf one;
+} // testConstructor
+
+// ----------------------------------------------------------------------
+// Test write() with 2D mesh and vertex data.
+void
+pylith::meshio::TestXdmf::testWrite2DVertex(void)
+{ // testWrite2DVertex
+
+  const char* filenameHDF5 = "data/tri3_vertex.h5";
+  const char* filenameXdmf = "tri3_vertex.xmf";
+
+  Xdmf metafile;
+  metafile.write(filenameXdmf, filenameHDF5);
+
+  //_checkFile(filenameXdmf);
+} // testWrite2DVertex
+
+// ----------------------------------------------------------------------
+// Test write() with 2D mesh and cell data.
+void
+pylith::meshio::TestXdmf::testWrite2DCell(void)
+{ // testWrite2DCell
+} // testWrite2DCell
+
+// ----------------------------------------------------------------------
+// Test write() with 3D mesh and vertex data.
+void
+pylith::meshio::TestXdmf::testWrite3DVertex(void)
+{ // testWrite3DVertex
+} // testWrite3DVertex
+
+// ----------------------------------------------------------------------
+// Test write() with 3D mesh and cell data.
+void
+pylith::meshio::TestXdmf::testWrite3DCell(void)
+{ // testWrite3DCell
+} // testWrite3DCell
+
+// ----------------------------------------------------------------------
+// Check VTK file against archived file.
+void
+pylith::meshio::TestXdmf::_checkFile(const char* filename)
+{ // _checkFile
+  const std::string filenameE = std::string("data/") + std::string(filename);
+
+  std::ifstream fileInE(filenameE.c_str());
+  CPPUNIT_ASSERT(fileInE.is_open());
+
+  std::ifstream fileIn(filename);
+  CPPUNIT_ASSERT(fileIn.is_open());
+
+  const int maxLen = 256;
+  char line[maxLen];
+  char lineE[maxLen];
+
+  int i = 1;
+  while(!fileInE.eof()) {
+    fileInE.getline(lineE, maxLen);
+    fileIn.getline(line, maxLen);
+    if (0 != strcmp(line, lineE)) {
+      std::cerr << "Line " << i << " of file '" << filename << "' is incorrect."
+		<< std::endl;
+      CPPUNIT_ASSERT(false);
+    } // if
+    ++i;
+  } // while
+
+  fileInE.close();
+  fileIn.close();
+} // _checkFile
+
+
+// End of file 

Added: short/3D/PyLith/trunk/unittests/libtests/meshio/TestXdmf.hh
===================================================================
--- short/3D/PyLith/trunk/unittests/libtests/meshio/TestXdmf.hh	                        (rev 0)
+++ short/3D/PyLith/trunk/unittests/libtests/meshio/TestXdmf.hh	2011-05-20 23:10:51 UTC (rev 18403)
@@ -0,0 +1,88 @@
+// -*- C++ -*-
+//
+// ----------------------------------------------------------------------
+//
+// Brad T. Aagaard, U.S. Geological Survey
+// Charles A. Williams, GNS Science
+// Matthew G. Knepley, University of Chicago
+//
+// This code was developed as part of the Computational Infrastructure
+// for Geodynamics (http://geodynamics.org).
+//
+// Copyright (c) 2010 University of California, Davis
+//
+// See COPYING for license information.
+//
+// ----------------------------------------------------------------------
+//
+
+/**
+ * @file unittests/libtests/meshio/TestXdmf.hh
+ *
+ * @brief C++ TestXdmf object
+ *
+ * C++ unit testing for Xdmf.
+ */
+
+#if !defined(pylith_meshio_testxdmf_hh)
+#define pylith_meshio_testxdmf_hh
+
+#include <cppunit/extensions/HelperMacros.h>
+
+/// Namespace for pylith package
+namespace pylith {
+  namespace meshio {
+    class TestXdmf;
+  } // meshio
+} // pylith
+
+/// C++ unit testing for Xdmf
+class pylith::meshio::TestXdmf : public CppUnit::TestFixture
+{ // class TestXdmf
+
+  // CPPUNIT TEST SUITE /////////////////////////////////////////////////
+  CPPUNIT_TEST_SUITE( TestXdmf );
+
+  CPPUNIT_TEST( testConstructor );
+  CPPUNIT_TEST( testWrite2DVertex );
+#if 0
+  CPPUNIT_TEST( testWrite2DCell );
+  CPPUNIT_TEST( testWrite3DVertex );
+  CPPUNIT_TEST( testWrite3DCell );
+#endif
+
+  CPPUNIT_TEST_SUITE_END();
+
+  // PUBLIC METHODS /////////////////////////////////////////////////////
+public :
+
+  /// Test constructor.
+  void testConstructor(void);
+
+  /// Test write() with 2D mesh and vertex data.
+  void testWrite2DVertex(void);
+
+  /// Test write() with 2D mesh and cell data.
+  void testWrite2DCell(void);
+
+  /// Test write() with 3D mesh and vertex data.
+  void testWrite3DVertex(void);
+
+  /// Test write() with 3D mesh and cell data.
+  void testWrite3DCell(void);
+
+  // PRIVATE METHODS ////////////////////////////////////////////////////
+private :
+
+  /** Check Xmdf file against archived file.
+   *
+   * @param filename Name of file to check.
+   */
+  static
+  void _checkFile(const char* filename);
+  
+}; // class TestXdmf
+
+#endif // pylith_meshio_testxdmf_hh
+
+// End of file 



More information about the CIG-COMMITS mailing list