[cig-commits] r18405 - short/3D/PyLith/trunk/libsrc/meshio

brad at geodynamics.org brad at geodynamics.org
Fri May 20 17:07:27 PDT 2011


Author: brad
Date: 2011-05-20 17:07:27 -0700 (Fri, 20 May 2011)
New Revision: 18405

Modified:
   short/3D/PyLith/trunk/libsrc/meshio/HDF5.cc
   short/3D/PyLith/trunk/libsrc/meshio/HDF5.hh
   short/3D/PyLith/trunk/libsrc/meshio/Xdmf.cc
   short/3D/PyLith/trunk/libsrc/meshio/Xdmf.hh
Log:
More work on Xdmf.

Modified: short/3D/PyLith/trunk/libsrc/meshio/HDF5.cc
===================================================================
--- short/3D/PyLith/trunk/libsrc/meshio/HDF5.cc	2011-05-20 23:11:14 UTC (rev 18404)
+++ short/3D/PyLith/trunk/libsrc/meshio/HDF5.cc	2011-05-21 00:07:27 UTC (rev 18405)
@@ -164,6 +164,30 @@
 } // hasDataset
 
 // ----------------------------------------------------------------------
+// Get topology metadata.
+void
+pylith::meshio::HDF5::getTopologyMetadata(int* numCells,
+					  int* numCorners,
+					  std::string* cellType)
+{ // getTopologyMetadata
+} // getTopologyMetadata
+
+// ----------------------------------------------------------------------
+// Get geometry metadata.
+void
+pylith::meshio::HDF5::getGeometryMetadata(int* numVertices,
+					  int* spaceDim)
+{ // getGeometryMetadata
+} // getGeometryMetadata
+
+// ----------------------------------------------------------------------
+// Get metadata for fields.
+void
+pylith::meshio::HDF5::getFieldsMetadata(std::vector<FieldMetadata>* metadata)
+{ // getFieldsMetadata
+} // getFieldsMetadata
+
+// ----------------------------------------------------------------------
 // Create group.
 void
 pylith::meshio::HDF5::createGroup(const char* name)

Modified: short/3D/PyLith/trunk/libsrc/meshio/HDF5.hh
===================================================================
--- short/3D/PyLith/trunk/libsrc/meshio/HDF5.hh	2011-05-20 23:11:14 UTC (rev 18404)
+++ short/3D/PyLith/trunk/libsrc/meshio/HDF5.hh	2011-05-21 00:07:27 UTC (rev 18405)
@@ -24,6 +24,7 @@
 
 #include <hdf5.h> // USES hid_t
 
+#include <vector> // USES std::vector
 #include <string> // USES std::string
 
 // HDF5 -----------------------------------------------------------------
@@ -32,6 +33,19 @@
 { // HDF5
   friend class TestHDF5; // Unit testing
 
+// PUBLIC STRUCTS -------------------------------------------------------
+public :
+
+  /// Metadata associated with fields.
+  struct FieldMetadata {
+    std::string name; ///< Name of field.
+    std::string vectorFieldType; ///< Type of field.
+    std::string domain; ///< Domain over which field is given.
+    int numPoints; ///< Number of points in field.
+    int fiberDim; ///< Number of values in field at each point, time.
+    int numTimeSteps; ///< Number of time steps for field.
+  }; // FieldMetadata
+
 // PUBLIC METHODS -------------------------------------------------------
 public :
 
@@ -80,6 +94,30 @@
    */
   bool hasDataset(const char* name);
 
+  /** Get topology metadata.
+   *
+   * @param numCells Number of cells [output]
+   * @param numCorners Number of corners [output]
+   * @param cellType Type of cell [output]
+   */
+  void getTopologyMetadata(int* numCells,
+			   int* numCorners,
+			   std::string* cellType);
+
+  /** Get geometry metadata.
+   *
+   * @param numVertices Number of vertices [output].
+   * @param spaceDim Spatial dimension [output].
+   */
+  void getGeometryMetadata(int* numVertices,
+			   int* spaceDim);
+
+  /** Get metadata for fields.
+   *
+   * @param metadata Array of metadata for fields.
+   */
+  void getFieldsMetadata(std::vector<FieldMetadata>* metadata);
+
   /** Create group.
    *
    * Create group and leave group open for further operations.

Modified: short/3D/PyLith/trunk/libsrc/meshio/Xdmf.cc
===================================================================
--- short/3D/PyLith/trunk/libsrc/meshio/Xdmf.cc	2011-05-20 23:11:14 UTC (rev 18404)
+++ short/3D/PyLith/trunk/libsrc/meshio/Xdmf.cc	2011-05-21 00:07:27 UTC (rev 18405)
@@ -47,20 +47,33 @@
   assert(filenameXdmf);
   assert(filenameHDF5);
 
-  std::string cellType;
   int numCells = 0;
   int numCorners = 0;
+  std::string cellType;
   int numVertices = 0;
   int spaceDim = 0;
   int numTimeSteps = 0;
   double* timeStamps = 0;
-  int numFields = 0;
-  string_vector fieldName;
-  string_vector fieldVectorFieldType;
-  string_vector fieldType;
-  int_array fieldNumPoints;
-  int_array fieldFiberDim;
+  std::vector<HDF5::FieldMetadata> fieldsMetadata;
 
+  HDF5 h5(filenameHDF5, H5F_ACC_RDONLY);
+  h5.getTopologyMetadata(&numCells, &numCorners, &cellType);
+  h5.getGeometryMetadata(&numVertices, &spaceDim);
+  h5.getFieldsMetadata(&fieldsMetadata);
+  const int numFields = fieldsMetadata.size();
+  for (int iField=0; iField < numFields; ++iField)
+    if ("vertex_field" == fieldsMetadata[iField].domain) {
+      fieldsMetadata[iField].domain = "Node";
+    } else if ("cell_field" == fieldsMetadata[iField].domain) {
+      fieldsMetadata[iField].domain = "Cell";
+    } else {
+      std::ostringstream msg;
+      msg << "Unknown field type '" << fieldsMetadata[iField].domain
+	  << "' for field '" 
+	  << fieldsMetadata[iField].domain << "'" << std::endl;
+      throw std::runtime_error(msg.str());
+    } // if/else
+
   if (1 == spaceDim) {
     std::cout
       << "WARNING: Xdmf grids not defined for 1-D domains.\n"
@@ -102,19 +115,12 @@
     _writeGridGeometry(spaceDim);
     for (int iField=0; iField < numFields; ++iField) {
       if (2 == spaceDim && 
-	  std::string("vector") == fieldVectorFieldType[iField]) {
+	  std::string("vector") == fieldsMetadata[iField].vectorFieldType) {
 	for (int component=0; component < spaceDim; ++component)
-	  _writeGridAttributeComponent(fieldName[iField].c_str(),
-				       fieldVectorFieldType[iField].c_str(),
-				       fieldType[iField].c_str(),
-				       numTimeSteps, fieldNumPoints[iField],
-				       fieldFiberDim[iField],
+	  _writeGridAttributeComponent(fieldsMetadata[iField],
 				       iTimeStep, component);
       } else {
-	_writeGridAttribute(fieldName[iField].c_str(),
-			    fieldVectorFieldType[iField].c_str(),
-			    fieldType[iField].c_str(), numTimeSteps, 
-			    fieldNumPoints[iField], fieldFiberDim[iField],
+	_writeGridAttribute(fieldsMetadata[iField],
 			    iTimeStep);
       } // if/else
     } // for
@@ -214,58 +220,46 @@
 // ----------------------------------------------------------------------
 // Write grid attribute.
 void
-pylith::meshio::Xdmf::_writeGridAttributeComponent(const char* name,
-						   const char* vectorFieldType,
-						   const char* center,
-						   const int numTimeSteps,
-						   const int numPoints,
-						   const int fiberDim,
-						   const int iTime,
-						   const int component)
+pylith::meshio::Xdmf::_writeGridAttribute(const HDF5::FieldMetadata& metadata,
+					  const int iTime)
 { // _writeGridAttribute
   assert(_file.is_open() && _file.good());
 
-  std::string componentName = "unknown";
-  switch (component) {
-  case 0:
-    componentName = std::string("x-") + std::string(name);
-    break;
-  case 1:
-    componentName = std::string("y-") + std::string(name);
-    break;
-  case 2:
-    componentName = std::string("z-") + std::string(name);
-    break;
-  default:
-    { // default
-      std::ostringstream msg;
-      msg << "Unknown component " << component << " while writing Xdmf file.";
-      std::cerr << msg.str() << std::endl;
-      assert(0);
-    throw std::logic_error(msg.str());
-    } // default
-  } // switch
+  std::string h5FullName = "";
+  if (std::string("Vertex") == metadata.domain) {
+    h5FullName = std::string("/vertex_fields/") + metadata.name;
+  } else if (std::string("Cell") == metadata.domain) {
+    h5FullName = std::string("/cell_fields/") + metadata.name;
+  } else {
+    std::ostringstream msg;
+    msg << "Unknown domain '" << metadata.domain << "' for field '"
+	<< metadata.name << "'." << std::endl;
+    throw std::runtime_error(msg.str());
+  } // if/else
+
   _file
     << "	<Attribute\n"
-    << "	   Name=\"" << componentName << "\"\n"
-    << "	   Type=\""<< vectorFieldType << "\"\n"
-    << "	   Center=\"" << center << "\">\n"
+    << "	   Name=\"" << metadata.name << "\"\n"
+    << "	   Type=\"" << metadata.vectorFieldType << "\"\n"
+    << "	   Center=\"" << metadata.domain << "\">\n"
     << "          <DataItem ItemType=\"HyperSlab\"\n"
-    << "		    Dimensions=\"1 " << numPoints << " 1\"\n"
+    << "		    Dimensions=\"1 " << metadata.numPoints << " 1\"\n"
     << "		    Type=\"HyperSlab\">\n"
     << "            <DataItem\n"
     << "	       Dimensions=\"3 3\"\n"
     << "	       Format=\"XML\">\n"
-    << "              0 0 "<< component << "\n"
+    << "              0 0 0\n"
     << "              1 1 1\n"
-    << "              1 " << numPoints << " 1\n"
+    << "              1 " << metadata.numPoints << " 1\n"
     << "	    </DataItem>\n"
     << "	    <DataItem\n"
     << "	       DataType=\"Float\" Precision=\"8\"\n"
     << "	       Dimensions=\""
-    << numTimeSteps << " " << numPoints << " " << fiberDim << "\"\n"
+    << metadata.numTimeSteps
+    << " " << metadata.numPoints
+    << " " << metadata.fiberDim << "\"\n"
     << "	       Format=\"HDF\">\n"
-    << "	      &HeavyData;:/vertex_fields/" << name << "\n"
+    << "	      &HeavyData;:" << h5FullName << "\n"
     << "	    </DataItem>\n"
     << "	  </DataItem>\n"
     << "	</Attribute>\n";
@@ -274,37 +268,68 @@
 // ----------------------------------------------------------------------
 // Write grid attribute.
 void
-pylith::meshio::Xdmf::_writeGridAttribute(const char* name,
-					  const char* vectorFieldType,
-					  const char* center,
-					  const int numTimeSteps,
-					  const int numPoints,
-					  const int fiberDim,
-					  const int iTime)
+pylith::meshio::Xdmf::_writeGridAttributeComponent(const HDF5::FieldMetadata& metadata,
+						   const int iTime,
+						   const int component)
 { // _writeGridAttribute
   assert(_file.is_open() && _file.good());
 
+  std::string h5FullName = "";
+  if (std::string("Vertex") == metadata.domain) {
+    h5FullName = std::string("/vertex_fields/") + metadata.name;
+  } else if (std::string("Cell") == metadata.domain) {
+    h5FullName = std::string("/cell_fields/") + metadata.name;
+  } else {
+    std::ostringstream msg;
+    msg << "Unknown domain '" << metadata.domain << "' for field '"
+	<< metadata.name << "'." << std::endl;
+    throw std::runtime_error(msg.str());
+  } // if/else
+
+  std::string componentName = "unknown";
+  switch (component) {
+  case 0:
+    componentName = std::string("x-") + std::string(metadata.name);
+    break;
+  case 1:
+    componentName = std::string("y-") + std::string(metadata.name);
+    break;
+  case 2:
+    componentName = std::string("z-") + std::string(metadata.name);
+    break;
+  default:
+    { // default
+      std::ostringstream msg;
+      msg << "Unknown component " << component << " while writing Xdmf file.";
+      std::cerr << msg.str() << std::endl;
+      assert(0);
+    throw std::logic_error(msg.str());
+    } // default
+  } // switch
+
   _file
     << "	<Attribute\n"
-    << "	   Name=\"" << name << "\"\n"
-    << "	   Type=\"" << vectorFieldType << "\"\n"
-    << "	   Center=\"" << center << "\">\n"
+    << "	   Name=\"" << componentName << "\"\n"
+    << "	   Type=\""<< metadata.vectorFieldType << "\"\n"
+    << "	   Center=\"" << metadata.domain << "\">\n"
     << "          <DataItem ItemType=\"HyperSlab\"\n"
-    << "		    Dimensions=\"1 " << numPoints << " 1\"\n"
+    << "		    Dimensions=\"1 " << metadata.numPoints << " 1\"\n"
     << "		    Type=\"HyperSlab\">\n"
     << "            <DataItem\n"
     << "	       Dimensions=\"3 3\"\n"
     << "	       Format=\"XML\">\n"
-    << "              0 0 0\n"
+    << "              0 0 "<< component << "\n"
     << "              1 1 1\n"
-    << "              1 " << numPoints << " 1\n"
+    << "              1 " << metadata.numPoints << " 1\n"
     << "	    </DataItem>\n"
     << "	    <DataItem\n"
     << "	       DataType=\"Float\" Precision=\"8\"\n"
     << "	       Dimensions=\""
-    << numTimeSteps << " " << numPoints << " " << fiberDim << "\"\n"
+    << metadata.numTimeSteps
+    << " " << metadata.numPoints
+    << " " << metadata.fiberDim << "\"\n"
     << "	       Format=\"HDF\">\n"
-    << "	      &HeavyData;:/vertex_fields/" << name << "\n"
+    << "	      &HeavyData;:" << h5FullName << "\n"
     << "	    </DataItem>\n"
     << "	  </DataItem>\n"
     << "	</Attribute>\n";

Modified: short/3D/PyLith/trunk/libsrc/meshio/Xdmf.hh
===================================================================
--- short/3D/PyLith/trunk/libsrc/meshio/Xdmf.hh	2011-05-20 23:11:14 UTC (rev 18404)
+++ short/3D/PyLith/trunk/libsrc/meshio/Xdmf.hh	2011-05-21 00:07:27 UTC (rev 18405)
@@ -38,6 +38,9 @@
 // Include directives ---------------------------------------------------
 #include "meshiofwd.hh" // forward declarations
 
+#include "HDF5.hh" // USES HDF5::FieldMetadata
+
+#include <vector> // USES std::vector
 #include <fstream> // HASA std::ofstream
 #include <string> // USES std::string
 
@@ -107,39 +110,19 @@
 
   /** Write grid attribute.
    * 
-   * @param name Name of attribute.
-   * @param vectorFieldType Type of vector field.
-   * @param center Vertex/Cell center.
-   * @param numTimeSteps Number of time steps.
-   * @param numPoints Number of vertices or cells.
-   * @param fiberDim Fiber dimension for attribute.
+   * @param metadata Metadata for field.
    * @param iTime Index of time step.
    */
-  void _writeGridAttribute(const char* name,
-			   const char* vectorFieldType,
-			   const char* center,
-			   const int numTimeSteps,
-			   const int numPoints,
-			   const int fiberDim,
+  void _writeGridAttribute(const HDF5::FieldMetadata& metadata,
 			   const int iTime);
 
   /** Write grid attribute as single component (for 2-D vector).
    * 
-   * @param name Name of attribute.
-   * @param vectorFieldType Type of vector field.
-   * @param center Vertex/Cell center.
-   * @param numTimeSteps Number of time steps.
-   * @param numPoints Number of vertices or cells.
-   * @param fiberDim Fiber dimension for attribute.
+   * @param metadata Metadata for field.
    * @param iTime Index of time step.
    * @param component Index of component.
    */
-  void _writeGridAttributeComponent(const char* name,
-				    const char* vectorFieldType,
-				    const char* center,
-				    const int numTimeSteps,
-				    const int numPoints,
-				    const int fiberDim,
+  void _writeGridAttributeComponent(const HDF5::FieldMetadata& metadata,
 				    const int iTime,
 				    const int component);
 



More information about the CIG-COMMITS mailing list