[cig-commits] r13759 - cs/cigma/trunk/src

luis at geodynamics.org luis at geodynamics.org
Wed Dec 17 02:33:21 PST 2008


Author: luis
Date: 2008-12-17 02:33:20 -0800 (Wed, 17 Dec 2008)
New Revision: 13759

Modified:
   cs/cigma/trunk/src/io_hdf5_reader.cpp
   cs/cigma/trunk/src/io_hdf5_reader.h
Log:
Conforming HDF_Reader to the FileReader interface

Modified: cs/cigma/trunk/src/io_hdf5_reader.cpp
===================================================================
--- cs/cigma/trunk/src/io_hdf5_reader.cpp	2008-12-17 10:33:19 UTC (rev 13758)
+++ cs/cigma/trunk/src/io_hdf5_reader.cpp	2008-12-17 10:33:20 UTC (rev 13759)
@@ -1,24 +1,31 @@
 #include "io_hdf5_reader.h"
-#include "io_hdf5.h"
 
+#ifdef HAVE_HDF5
+
 #include <iostream>
+#include <sstream>
+#include "io_hdf5.h"
+#include "nc_array.h"
+#include "eb_array.h"
 
 using namespace std;
+using namespace boost;
 using namespace cigma;
 
 
-#ifdef HAVE_HDF5
 
 HDF5_Reader::HDF5_Reader()
 {
     file = 0;
 }
 
+
 HDF5_Reader::~HDF5_Reader()
 {
     close();
 }
 
+
 int HDF5_Reader::open(const char *filename, const char *mode)
 {
     TRI_LOG_STR("HDF5_Reader::open()");
@@ -32,7 +39,8 @@
     string m(mode);
     if ((m == "r") || (m == "rw") || (m == "a"))
     {
-        file = h5_open_file(filename, mode);
+        this->file = h5_open_file(filename, mode);
+        this->filename = string(filename);
     }
     else
     {
@@ -42,12 +50,14 @@
 
     if (file == 0)
     {
+        this->filename = "";
         return -1;
     }
 
     return 0;
 }
 
+
 int HDF5_Reader::close()
 {
     if (file)
@@ -55,6 +65,7 @@
         delete file;
         file = 0;
     }
+    filename = "";
     return 0;
 }
 
@@ -62,143 +73,322 @@
 int HDF5_Reader::getDataset(const char *loc, double **data, int *num, int *dim)
 {
     TRI_LOG_STR("HDF5_Reader::getDataset()");
-    TRI_LOG(loc);
     return get_full_dataset<double>(file, loc, data, num, dim);
 }
 
+
 int HDF5_Reader::getCoordinates(const char *loc, double **coordinates, int *nno, int *nsd)
 {
     TRI_LOG_STR("HDF5_Reader::getCoordinates()");
-    TRI_LOG(loc);
     return get_full_dataset<double>(file, loc, coordinates, nno, nsd);
 }
 
+
 int HDF5_Reader::getConnectivity(const char *loc, int **connectivity, int *nel, int *ndofs)
 {
     TRI_LOG_STR("HDF5_Reader::getConnectivity()");
-    TRI_LOG(loc);
     return get_full_dataset<int>(file, loc, connectivity, nel, ndofs);
 }
 
-int HDF5_Reader::readAttrString(const char *loc, const char *attr_name, string& val)
-{
-    TRI_LOG_STR("HDF5_Reader::readAttrString()");
-    return read_scalar_attribute<string>(file, loc, attr_name, val);
-}
 
-int HDF5_Reader::readAttrInteger(const char *loc, const char *attr_name, int& val)
+void HDF5_Reader::getIntArray(const char *loc, cigma::array<int>& A)
 {
-    return read_scalar_attribute<int>(file, loc, attr_name, val);
+    TRI_LOG_STR("HDF5_Reader::getIntArray()");
+    int status = get_full_dataset<int>(file, loc, &(A._data), &(A.npts), &(A.ndim));
+    if (status < 0)
+    {
+        std::ostringstream stream;
+        stream << "Could not read int array from location '"
+               << loc << "' in HDF5 file '" << filename << "'"
+               << std::ends;
+        throw cigma::Exception("HDF5_Reader::getIntArray", stream.str());
+    }
 }
 
-int HDF5_Reader::readAttrLong(const char *loc, const char *attr_name, long& val)
+
+void HDF5_Reader::getLongArray(const char *loc, cigma::array<long>& A)
 {
-    return read_scalar_attribute<long>(file, loc, attr_name, val);
+    TRI_LOG_STR("HDF5_Reader::getLongArray()");
+    int status = get_full_dataset<long>(file, loc, &(A._data), &(A.npts), &(A.ndim));
+    if (status < 0)
+    {
+        std::ostringstream stream;
+        stream << "Could not read long array from location '"
+               << loc << "' in HDF5 file '" << filename << "'"
+               << std::ends;
+        throw cigma::Exception("HDF5_Reader::getLongArray", stream.str());
+    }
 }
 
-int HDF5_Reader::readAttrFloat(const char *loc, const char *attr_name, float& val)
+
+void HDF5_Reader::getFloatArray(const char *loc, cigma::array<float>& A)
 {
-    return read_scalar_attribute<float>(file, loc, attr_name, val);
+    TRI_LOG_STR("HDF5_Reader::getFloatArray()");
+    int status = get_full_dataset<float>(file, loc, &(A._data), &(A.npts), &(A.ndim));
+    if (status < 0)
+    {
+        std::ostringstream stream;
+        stream << "Could not read float array from location '"
+               << loc << "' in HDF5 file '" << filename << "'"
+               << std::ends;
+        throw cigma::Exception("HDF5_Reader::getFloatArray", stream.str());
+    }
 }
 
-int HDF5_Reader::readAttrDouble(const char *loc, const char *attr_name, double& val)
+
+void HDF5_Reader::getDoubleArray(const char *loc, cigma::array<double>& A)
 {
-    return read_scalar_attribute<double>(file, loc, attr_name, val);
+    TRI_LOG_STR("HDF5_Reader::getDoubleArray()");
+    int status = get_full_dataset<double>(file, loc, &(A._data), &(A.npts), &(A.ndim));
+    if (status < 0)
+    {
+        std::ostringstream stream;
+        stream << "Could not read double array from location '"
+               << loc << "' in HDF5 file '" << filename << "'"
+               << std::ends;
+        throw cigma::Exception("HDF5_Reader::getDoubleArray", stream.str());
+    }
 }
 
-bool HDF5_Reader::locationIsGroup(const char *loc)
+
+Cell::type HDF5_Reader::getCellType(const char *loc)
 {
-    H5::Group* group = h5_open_group(file, loc);
-    if (group != 0)
+    TRI_LOG_STR("HDF5_Reader::getCellType()");
+    if (this->locationIsGroup(loc))
     {
-        delete group;
-        return true;
+        string cellname;
+        int status = this->getStringAttr(loc, "CellType", cellname);
+        if (status == 0)
+        {
+            TRI_LOG(cellname);
+            return Cell::string2type(cellname);
+        }
     }
-    return false;
+    return Cell::NONE;
 }
 
-bool HDF5_Reader::locationIsDataset(const char *loc)
+
+shared_ptr<NodeCoordinates> HDF5_Reader::getNodeCoordinates(const char *loc)
 {
-    H5::DataSet* dataset = h5_open_dataset(file, loc);
-    if (dataset != 0)
+    TRI_LOG_STR("HDF5_Reader::getNodeCoordinates()");
+    shared_ptr<nc_array> nc(new nc_array);
+    int status = this->getCoordinates(loc, &(nc->coords), &(nc->nno), &(nc->nsd));
+    if (status < 0)
     {
-        delete dataset;
-        return true;
+        std::ostringstream stream;
+        stream << "Could not read node coordinates from location '"
+               << loc << "' in HDF5 file '" << filename << "'"
+               << std::ends;
+        throw cigma::Exception("HDF5_Reader::getNodeCoordinates", stream.str());
     }
-    return false;
+    return nc;
 }
 
 
-#else
-
-HDF5_Reader::HDF5_Reader()
+shared_ptr<ElementBlock> HDF5_Reader::getElementBlock(const char *loc)
 {
-    throw cigma::Exception("HDF5_Reader", "Can't create HDF5_Reader. Cigma is not configured with C++ HDF5 Library");
+    TRI_LOG_STR("HDF5_Reader::getElementBlock()");
+    shared_ptr<eb_array> eb(new eb_array);
+    int status = this->getConnectivity(loc, &(eb->connect), &(eb->nel), &(eb->ndofs));
+    if (status < 0)
+    {
+        std::ostringstream stream;
+        stream << "Could not read element block from location '"
+               << loc << "' in HDF5 file '" << filename << "'"
+               << std::ends;
+        throw cigma::Exception("HDF5_Reader::getElementBlock", stream.str());
+    }
+    return eb;
 }
 
-HDF5_Reader::~HDF5_Reader()
+
+shared_ptr<MeshPart> HDF5_Reader::getMeshPart(const char *loc)
 {
-}
+    TRI_LOG_STR("HDF5_Reader::getMeshPart()");
+    TRI_LOG(loc);
+    
+    shared_ptr<MeshPart> mesh;
 
-int HDF5_Reader::open(const char *filename, const char *mode)
-{
-    return -1;
+    if (this->locationIsGroup(loc))
+    {
+        string nc(loc), eb(loc);
+        
+        nc += "/coordinates";
+        eb += "/connectivity";
+
+        mesh            = shared_ptr<MeshPart>(new MeshPart);
+        mesh->coords    = this->getNodeCoordinates(nc.c_str());
+        mesh->connect   = this->getElementBlock(eb.c_str());
+        mesh->cell_type = this->getCellType(loc);
+    }
+    else
+    {
+        std::ostringstream stream;
+        stream << "Could not read mesh location '"
+               << loc << "' as an HDF5 group"
+               << std::ends;
+        throw cigma::Exception("HDF5_Reader::getMeshPart", stream.str());
+    }
+
+    return mesh;
 }
 
-int HDF5_Reader::close()
+
+shared_ptr<Quadrature> HDF5_Reader::getQuadrature(const char *loc)
 {
-    return -1;
-}
+    TRI_LOG_STR("HDF5_Reader::getQuadrature");
 
+    shared_ptr<Quadrature> Q;
 
-int HDF5_Reader::getDataset(const char *loc, double **data, int *num, int *dim)
-{
-    return -1;
+    if (this->locationIsGroup(loc))
+    {
+        int status;
+        string wts(loc), pts(loc);
+
+        wts += "/weights";
+        pts += "/points";
+
+        Q = shared_ptr<Quadrature>(new Quadrature);
+
+        int nw, nwd;
+        status = this->getDataset(wts.c_str(), &(Q->weights), &nw, &nwd);
+        if (status < 0)
+        {
+            std::ostringstream stream;
+            stream << "Failed to read quadrature weights from location '"
+                   << wts << "' in HDF5 file '" << filename << "'"
+                   << std::ends;
+            throw cigma::Exception("HDF5_Reader::getQuadrature", stream.str());
+        }
+
+        int nx, nxd;
+        status = this->getDataset(pts.c_str(), &(Q->points), &nx, &nxd);
+        if (status < 0)
+        {
+            std::ostringstream stream;
+            stream << "Failed to read quadrature points from location '"
+                   << wts << "' in HDF5 file '" << filename << "'"
+                   << std::ends;
+            throw cigma::Exception("HDF5_Reader::getQuadrature", stream.str());
+        }
+
+        if (nw != nx)
+        {
+            std::ostringstream stream;
+            stream << "Number of weights (" << nw << ") "
+                   << "does not match the number of quadrature "
+                   << "points (" << nx << ")"
+                   << std::ends;
+            throw cigma::Exception("HDF5_Reader::getQuadrature", stream.str());
+        }
+
+        Q->npts = nx;
+        Q->ndim = nxd;
+        Q->cell_type = this->getCellType(loc);
+
+    }
+    else if (this->locationIsDataset(loc))
+    {
+        cigma::array<double> wx;
+        this->getDoubleArray(loc, wx);
+
+        Q = shared_ptr<Quadrature>(new Quadrature);
+        Q->setFromCombinedData(wx);
+    }
+
+    return Q;
 }
 
-int HDF5_Reader::getCoordinates(const char *loc, double **coordinates, int *nno, int *nsd)
+
+shared_ptr<FE> HDF5_Reader::getFE(const char *loc)
 {
-    return -1;
+    shared_ptr<FE> fe;
+    return fe;
 }
 
-int HDF5_Reader::getConnectivity(const char *loc, int **connectivity, int *nel, int *ndofs)
+
+shared_ptr<DofHandler> HDF5_Reader::getDofHandler(const char *loc)
 {
-    return -1;
+    shared_ptr<DofHandler> dh(new DofHandler);
+    int status = this->getDataset(loc, &(dh->dofs), &(dh->nno), &(dh->rank));
+    if (status < 0)
+    {
+        std::ostringstream stream;
+        stream << "Could not read dofs from location '"
+               << loc << "' in HDF5 file '" << filename << "'"
+               << std::ends;
+        throw cigma::Exception("HDF5_Reader::getDofHandler", stream.str());
+    }
+    return dh;
 }
 
-int HDF5_Reader::readAttrString(const char *loc, const char *attr_name, string& val)
+
+int HDF5_Reader::getIntAttr(const char *loc, const char *attr_name, int& val)
 {
-    return -1;
+    return read_scalar_attribute<int>(file, loc, attr_name, val);
 }
 
-int HDF5_Reader::readAttrInteger(const char *loc, const char *attr_name, int& val)
+
+int HDF5_Reader::getLongAttr(const char *loc, const char *attr_name, long& val)
 {
-    return -1;
+    return read_scalar_attribute<long>(file, loc, attr_name, val);
 }
 
-int HDF5_Reader::readAttrLong(const char *loc, const char *attr_name, long& val)
+
+int HDF5_Reader::getFloatAttr(const char *loc, const char *attr_name, float& val)
 {
-    return -1;
+    return read_scalar_attribute<float>(file, loc, attr_name, val);
 }
 
-int HDF5_Reader::readAttrFloat(const char *loc, const char *attr_name, float& val)
+
+int HDF5_Reader::getDoubleAttr(const char *loc, const char *attr_name, double& val)
 {
-    return -1;
+    return read_scalar_attribute<double>(file, loc, attr_name, val);
 }
 
-int HDF5_Reader::readAttrDouble(const char *loc, const char *attr_name, double& val)
+
+int HDF5_Reader::getStringAttr(const char *loc, const char *attr_name, string& val)
 {
-    return -1;
+    TRI_LOG_STR("HDF5_Reader::getString()");
+    return read_scalar_attribute<string>(file, loc, attr_name, val);
 }
 
+
 bool HDF5_Reader::locationIsGroup(const char *loc)
 {
+    H5::Group* group = h5_open_group(file, loc);
+    if (group != 0)
+    {
+        delete group;
+        return true;
+    }
     return false;
 }
 
 bool HDF5_Reader::locationIsDataset(const char *loc)
 {
+    H5::DataSet* dataset = h5_open_dataset(file, loc);
+    if (dataset != 0)
+    {
+        delete dataset;
+        return true;
+    }
     return false;
 }
 
+
+#else
+
+
+cigma::HDF5_Reader::HDF5_Reader()
+{
+    std::string msg("Can't create HDF5_Reader. Cigma is not configured with the C++ HDF5 library");
+    throw cigma::Exception("HDF5_Reader", msg);
+}
+
+cigma::HDF5_Reader::~HDF5_Reader()
+{
+}
+
+
 #endif

Modified: cs/cigma/trunk/src/io_hdf5_reader.h
===================================================================
--- cs/cigma/trunk/src/io_hdf5_reader.h	2008-12-17 10:33:19 UTC (rev 13758)
+++ cs/cigma/trunk/src/io_hdf5_reader.h	2008-12-17 10:33:20 UTC (rev 13759)
@@ -3,15 +3,16 @@
 
 #include "io_file_reader.h"
 
-#ifdef HAVE_HDF5
-#include "H5Cpp.h"
-#endif
-
 namespace cigma
 {
     class HDF5_Reader;
 }
 
+
+#ifdef HAVE_HDF5
+
+#include "H5Cpp.h"
+
 class cigma::HDF5_Reader : public cigma::FileReader
 {
 public:
@@ -26,20 +27,57 @@
     int getDataset(const char *loc, double **data, int *num, int *dim);
     int getCoordinates(const char *loc, double **coordinates, int *nno, int *nsd);
     int getConnectivity(const char *loc, int **connectivity, int *nel, int *ndofs);
-    
-    int readAttrString(const char *loc, const char *attr_name, std::string& val);
-    int readAttrInteger(const char *loc, const char *attr_name, int& val);
-    int readAttrLong(const char *loc, const char *attr_name, long& val);
-    int readAttrFloat(const char *loc, const char *attr_name, float& val);
-    int readAttrDouble(const char *loc, const char *attr_name, double& val);
 
+    void getIntArray(const char *loc, cigma::array<int>& A);
+    void getLongArray(const char *loc, cigma::array<long>& A);
+    void getFloatArray(const char *loc, cigma::array<float>& A);
+    void getDoubleArray(const char *loc, cigma::array<double>& A);
+  
+    boost::shared_ptr<NodeCoordinates> getNodeCoordinates(const char *loc);
+    boost::shared_ptr<ElementBlock> getElementBlock(const char *loc);
+    boost::shared_ptr<MeshPart> getMeshPart(const char *loc);
+    boost::shared_ptr<Quadrature> getQuadrature(const char *loc);
+    boost::shared_ptr<FE> getFE(const char *loc);
+    boost::shared_ptr<DofHandler> getDofHandler(const char *loc);
+
+    Cell::type getCellType(const char *loc);
+
+    int getIntAttr(const char *loc, const char *attr_name, int& val);
+    int getLongAttr(const char *loc, const char *attr_name, long& val);
+    int getFloatAttr(const char *loc, const char *attr_name, float& val);
+    int getDoubleAttr(const char *loc, const char *attr_name, double& val);
+    int getStringAttr(const char *loc, const char *attr_name, std::string& val);
+
     bool locationIsGroup(const char *loc);
     bool locationIsDataset(const char *loc);
 
-#ifdef HAVE_HDF5
 public:
     H5::H5File *file;
-#endif
+    std::string filename;
 };
 
-#endif
+
+#else
+
+
+class cigma::HDF5_Reader : public cigma::NullReader
+{
+public:
+    HDF5_Reader();
+    ~HDF5_Reader();
+    ReaderType getReaderType() { return HDF5_FILE_READER; }
+    Cell::type getCellType(const char *loc) { return Cell::NONE; }
+    int getIntAttr(const char *loc, const char *attr_name, int& val) { return -1; }
+    int getLongAttr(const char *loc, const char *attr_name, long& val) { return -1; }
+    int getFloatAttr(const char *loc, const char *attr_name, float& val) { return -1; }
+    int getDoubleAttr(const char *loc, const char *attr_name, double& val) { return -1; }
+    int getStringAttr(const char *loc, const char *attr_name, std::string& val) { return -1; }
+    bool locationIsGroup(const char *loc) { return false; }
+    bool locationIsDataset(const char *loc) { return false; }
+}
+
+
+#endif /* HAVE_HDF5 */
+
+
+#endif /* __CIGMA_HDF5_READER_H__ */



More information about the CIG-COMMITS mailing list