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

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


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

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

Modified: cs/cigma/trunk/src/io_text_reader.cpp
===================================================================
--- cs/cigma/trunk/src/io_text_reader.cpp	2008-12-17 10:33:23 UTC (rev 13760)
+++ cs/cigma/trunk/src/io_text_reader.cpp	2008-12-17 10:33:24 UTC (rev 13761)
@@ -1,26 +1,28 @@
 #include "io_text_reader.h"
-#include "Common.h"
+#include "nc_array.h"
+#include "eb_array.h"
 
 #include <cassert>
 #include <cstdlib>
 #include <cstring>
+#include <sstream>
 
 using namespace std;
+using namespace boost;
 using namespace cigma;
 
-// ----------------------------------------------------------------------------
 
 TextReader::TextReader()
 {
     fp = NULL;
 }
 
+
 TextReader::~TextReader()
 {
     close();
 }
 
-// ----------------------------------------------------------------------------
 
 int TextReader::open(const char *filename, const char *mode)
 {
@@ -39,9 +41,12 @@
         return -2;
     }
 
+    this->filename = string(filename);
+
     return 0;
 }
 
+
 int TextReader::close()
 {
     if (fp != NULL)
@@ -49,127 +54,202 @@
         fclose(fp);
         fp = NULL;
     }
+    filename = "";
     return 0;
 }
 
-// ----------------------------------------------------------------------------
 
-// XXX: rethink this method (usage is awkward...see unit tests)
-/*
-static FILE *get_fp(const char *loc, FILE *default_fp)
+template <typename T>
+static const char* get_format()
 {
-    FILE *fp = default_fp;
-    if ((loc != NULL) && (strcmp(loc, "") != 0))
+    throw cigma::Exception("get_format", "no mapping available for this type");
+    return "";
+}
+
+template <> const char* get_format<int>()    { return "%d";  }
+template <> const char* get_format<long>()   { return "%ld"; }
+template <> const char* get_format<float>()  { return "%f";  }
+template <> const char* get_format<double>() { return "%lf"; }
+
+
+template <typename T>
+static int read_mat(FILE *fp, T** mat, int& rows, int& cols)
+{
+    if (fp == NULL)
     {
-        fp = fopen(loc, "r");
+        return -1;
     }
-    return fp;
-} */
 
-static bool read_dmat(FILE *fp, double **mat, int *rows, int *cols)
-{
-    if (fp == NULL) { return false; }
-
-    int nrows, ncols;
     int ret;
 
-    ret = fscanf(fp, "%d", &nrows);
+    ret = fscanf(fp, "%d", &rows);
     assert(ret == 1);
-    assert(nrows > 0);
+    assert(rows > 0);
 
-    ret = fscanf(fp, "%d", &ncols);
+    ret = fscanf(fp, "%d", &cols);
     assert(ret == 1);
-    assert(ncols > 0);
+    assert(cols > 0);
 
-    double *m = new double[nrows * ncols];
-
-    for (int i = 0; i < nrows; i++)
+    T* m = new T[rows * cols];
+    const char *fmt = get_format<T>();
+    for (int i = 0; i < rows; i++)
     {
-        for (int j = 0; j < ncols; j++)
+        for (int j = 0; j < cols; j++)
         {
-            int n = ncols*i + j;
-            ret = fscanf(fp, "%lf", &m[n]);
+            const int n = cols*i + j;
+            ret = fscanf(fp, fmt, &m[n]);
             assert(ret == 1);
         }
     }
 
     *mat = m;
-    *rows = nrows;
-    *cols = ncols;
 
-    return true;
+    return 0;
 }
 
-static bool read_imat(FILE *fp, int **mat, int *rows, int *cols)
+
+int TextReader::getDataset(const char *loc, double **data, int *num, int *dim)
 {
-    if (fp == NULL) { return false; }
+    TRI_LOG_STR("TextReader::getDataset()");
+    return read_mat<double>(fp, data, *num, *dim);
+}
 
-    int nrows, ncols;
-    int ret;
 
-    ret = fscanf(fp, "%d", &nrows);
-    assert(ret == 1);
-    assert(nrows > 0);
+int TextReader::getConnectivity(const char *loc, int **connectivity, int *nel, int *ndofs)
+{
+    TRI_LOG_STR("TextReader::getConnectivity()");
+    return read_mat<int>(fp, connectivity, *nel, *ndofs);
+}
 
-    ret = fscanf(fp, "%d", &ncols);
-    assert(ret == 1);
-    assert(ncols > 0);
 
-    int *m = new int[nrows * ncols];
+int TextReader::getCoordinates(const char *loc, double **coordinates, int *nno, int *nsd)
+{
+    TRI_LOG_STR("TextReader::getCoordinates()");
+    return read_mat<double>(fp, coordinates, *nno, *nsd);
+}
 
-    for (int i = 0; i < nrows; i++)
+
+void TextReader::getIntArray(const char *loc, cigma::array<int>& A)
+{
+    TRI_LOG_STR("TextReader::getIntArray()");
+    int status = read_mat<int>(fp, &(A._data), A.npts, A.ndim);
+    if (status < 0)
     {
-        for (int j = 0; j < ncols; j++)
-        {
-            int n = ncols*i + j;
-            ret = fscanf(fp, "%d", &m[n]);
-            assert(ret == 1);
-        }
+        std::ostringstream stream;
+        stream << "Could not read array from text file '" << filename << "'" << std::ends;
+        throw cigma::Exception("TextReader::getIntArray", stream.str());
     }
+}
 
-    *mat = m;
-    *rows = nrows;
-    *cols = ncols;
 
-    return true;
+void TextReader::getLongArray(const char *loc, cigma::array<long>& A)
+{
+    TRI_LOG_STR("TextReader::getLongArray()");
+    int status = read_mat<long>(fp, &(A._data), A.npts, A.ndim);
+    if (status < 0)
+    {
+        std::ostringstream stream;
+        stream << "Could not read array from text file '" << filename << "'" << std::ends;
+        throw cigma::Exception("TextReader::getLongArray", stream.str());
+    }
 }
 
 
-int TextReader::getDataset(const char *loc, double **data, int *num, int *dim)
+void TextReader::getFloatArray(const char *loc, cigma::array<float>& A)
 {
-    TRI_LOG_STR("TextReader::getDataset()");
-    if (fp != NULL)
+    TRI_LOG_STR("TextReader::getFloatArray()");
+    int status = read_mat<float>(fp, &(A._data), A.npts, A.ndim);
+    if (status < 0)
     {
-        read_dmat(fp, data, num, dim);
-        return 0;
+        std::ostringstream stream;
+        stream << "Could not read array from text file '" << filename << "'" << std::ends;
+        throw cigma::Exception("TextReader::getFloatArray", stream.str());
     }
-    return -1;
 }
 
 
-int TextReader::getConnectivity(const char *loc, int **connectivity, int *nel, int *ndofs)
+void TextReader::getDoubleArray(const char *loc, cigma::array<double>& A)
 {
-    TRI_LOG_STR("TextReader::getConnectivity()");
-    if (fp != NULL)
+    TRI_LOG_STR("TextReader::getDoubleArray()");
+    int status = read_mat<double>(fp, &(A._data), A.npts, A.ndim);
+    if (status < 0)
     {
-        read_imat(fp, connectivity, nel, ndofs);
-        return 0;
+        std::ostringstream stream;
+        stream << "Could not read array from text file '" << filename << "'" << std::ends;
+        throw cigma::Exception("TextReader::getDoubleArray", stream.str());
     }
-    return -1;
 }
 
-int TextReader::getCoordinates(const char *loc, double **coordinates, int *nno, int *nsd)
+
+shared_ptr<NodeCoordinates> TextReader::getNodeCoordinates(const char *loc)
 {
-    TRI_LOG_STR("TextReader::getCoordinates()");
-    if (fp != NULL)
+    TRI_LOG_STR("TextReader::getNodeCoordinates()");
+    shared_ptr<nc_array> nc(new nc_array);
+    int status = this->getCoordinates(loc, &(nc->coords), &(nc->nno), &(nc->nsd));
+    if (status < 0)
     {
-        read_dmat(fp, coordinates, nno, nsd);
-        return 0;
+        std::ostringstream stream;
+        stream << "Could not read node coordinates from text file '"
+               << filename << "'" << std::ends;
+        throw cigma::Exception("TextReader::getNodeCoordinates", stream.str());
     }
-    return -1;
+    return nc;
 }
 
 
-// ----------------------------------------------------------------------------
+shared_ptr<ElementBlock> TextReader::getElementBlock(const char *loc)
+{
+    TRI_LOG_STR("TextReader::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 text file '"
+               << filename << "'" << std::ends;
+        throw cigma::Exception("TextReader::getElementBlock", stream.str());
+    }
+    return eb;
+}
 
 
+shared_ptr<MeshPart> TextReader::getMeshPart(const char *loc)
+{
+    shared_ptr<MeshPart> mesh;
+    return mesh;
+}
+
+
+shared_ptr<Quadrature> TextReader::getQuadrature(const char *loc)
+{
+    cigma::array<double> wx;
+    this->getDoubleArray(loc, wx);
+
+    shared_ptr<Quadrature> Q(new Quadrature);
+    Q->setFromCombinedData(wx);
+    return Q;
+}
+
+
+shared_ptr<FE> TextReader::getFE(const char *loc)
+{
+    shared_ptr<FE> fe;
+    return fe;
+}
+
+
+shared_ptr<DofHandler> TextReader::getDofHandler(const char *loc)
+{
+    TRI_LOG_STR("TextReader::getDofHandler()");
+    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 text file '"
+               << filename << "'" << std::ends;
+        throw cigma::Exception("TextReader::getElementBlock", stream.str());
+    }
+    return dh;
+}
+

Modified: cs/cigma/trunk/src/io_text_reader.h
===================================================================
--- cs/cigma/trunk/src/io_text_reader.h	2008-12-17 10:33:23 UTC (rev 13760)
+++ cs/cigma/trunk/src/io_text_reader.h	2008-12-17 10:33:24 UTC (rev 13761)
@@ -25,8 +25,21 @@
     int getCoordinates(const char *loc, double **coordinates, int *nno, int *nsd);
     int getConnectivity(const char *loc, int **connectivity, int *nel, int *ndofs);
 
+    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);
+
 public:
     FILE *fp;
+    std::string filename;
 };
 
 #endif



More information about the CIG-COMMITS mailing list