[cig-commits] r9312 - cs/benchmark/cigma/trunk/src

luis at geodynamics.org luis at geodynamics.org
Wed Feb 13 10:28:40 PST 2008


Author: luis
Date: 2008-02-13 10:28:39 -0800 (Wed, 13 Feb 2008)
New Revision: 9312

Modified:
   cs/benchmark/cigma/trunk/src/MeshIO.cpp
   cs/benchmark/cigma/trunk/src/MeshIO.h
Log:
Added extensive checks to MeshIO, since command line args can be anything


Modified: cs/benchmark/cigma/trunk/src/MeshIO.cpp
===================================================================
--- cs/benchmark/cigma/trunk/src/MeshIO.cpp	2008-02-13 18:28:37 UTC (rev 9311)
+++ cs/benchmark/cigma/trunk/src/MeshIO.cpp	2008-02-13 18:28:39 UTC (rev 9312)
@@ -66,9 +66,27 @@
     }
 }
 
+
 // ---------------------------------------------------------------------------
 
+MeshIO::MeshIO()
+{
+    meshPart = 0;
+    reader = coords_reader = connect_reader = 0;
+    writer = coords_writer = connect_writer = 0;
+}
 
+MeshIO::~MeshIO()
+{
+    if (meshPart != 0)
+    {
+        // XXX: traverse meshPart structure and delete everything
+    }
+}
+
+// ---------------------------------------------------------------------------
+
+
 bool MeshIO::has_paths()
 {
     return (mesh_path != "") || (coords_path != "") || (connect_path != "");
@@ -98,35 +116,119 @@
 
 // ---------------------------------------------------------------------------
 
-MeshIO::MeshIO()
+bool MeshIO::prepare()
 {
-    meshPart = 0;
-}
+    // This function initializes the required variables
+    // for use in MeshIO::load()
+    //
+    //  precondition: all path variables need to be set
+    //                appropriately by calling load_args()
+    //
+    //  postcondition: if successful, location & file variables
+    //                 will be ready for use in load()
+    //
 
-MeshIO::~MeshIO()
-{
-    if (meshPart != 0)
+    if (!(has_coords_path() && has_connect_path()))
     {
-        // XXX: traverse meshPart structure and delete everything
+        // not enough information available to set location variables.
+        // top level program should use validate_args() to detect this
+        // condition ahead of time and bail out
+        return false;
     }
+
+    //
+    // Note that each of coords_path and connect_path will override
+    // the corresponding component in mesh_path.
+    //
+    if (coords_path != "")
+    {
+        parse_dataset_path(coords_path, coords_loc, coords_file, coords_ext);
+    }
+    if (connect_path != "")
+    {
+        parse_dataset_path(connect_path, connect_loc, connect_file, connect_ext);
+    }
+    if (mesh_path != "")
+    {
+        parse_dataset_path(mesh_path, mesh_loc, mesh_file, mesh_ext);
+
+        if (coords_path == "")
+        {
+            // no coords path specified...
+            // read coords from mesh file
+            // connect_loc will have to be determined later in load()
+            connect_loc = "";
+            coords_file = mesh_file;
+            coords_ext  = mesh_ext;
+        }
+        else
+        {
+            if (coords_loc == "")
+            {
+                // reinterpret the meaning of --mesh-coordinates
+                coords_loc  = coords_file;
+                coords_file = mesh_file;
+                coords_ext  = mesh_ext;
+            }
+        }
+
+        if (connect_path == "")
+        {
+            // no connect path specified...
+            // read connectivity from mesh file
+            // connect_loc will have to be determined later in load()
+            connect_loc  = "";
+            connect_file = mesh_file;
+            connect_ext  = mesh_ext;
+        }
+        else
+        {
+            if (connect_loc == "")
+            {
+                // reinterpret the meaning of --mesh-connectivity
+                connect_loc  = connect_file;
+                connect_file = mesh_file;
+                connect_ext  = mesh_ext;
+            }
+        }
+    }
+
+    return true;
 }
 
-// ---------------------------------------------------------------------------
-
 void MeshIO::load()
 {
+    // 
+    // The sole purpose of this function is to initialize
+    // the meshPart member variable. Thus, failure can be detected
+    // by checking whether meshPart is 0.
+    //
+
+
     cout << "Calling MeshIO::load()" << endl;
 
-    // skip this method if paths are empty
+
+    // Skip this method if paths are empty.
+    // This behavior is necessary because the --mesh option
+    // is not required on the command line
     if (!has_paths())
     {
         return;
     }
 
-    //string mesh_loc, mesh_file, mesh_ext;
-    //string coords_loc, coords_file, coords_ext;
-    //string connect_loc, connect_file, connect_ext;
+    // bail out if prepare() fails
+    bool ready = prepare();
+    if (!ready)
+    {
+        return;
+    }
 
+
+    //
+    // Local declarations
+    //
+    int ierr;
+
     int nno, nsd;
     double *coords = 0;
 
@@ -137,13 +239,121 @@
     nel = ndofs = 0;
 
 
+    // To implement at this point:
+    //  XXX: check whether files exist if we are reading
+    //  XXX: emit warnings or bail out if we are writing an existing file?
+    //  XXX: check file's magic number and overwrite the extension if it doesn't match
     //
-    // Note that each of coords_path and connect_path will override
-    // the corresponding component of mesh_path
+
+
+    // 
+    // Instantiate readers
     //
+    if (mesh_file != "")
+    {
+        new_reader(&reader, mesh_ext);
+    }
+    if (coords_file != "")
+    {
+        if (coords_file == mesh_file)
+        {
+            coords_reader = reader;
+        }
+        else
+        {
+            new_reader(&coords_reader, coords_ext);
+        }
+    }
+    if (connect_file != "")
+    {
+        if (connect_file == mesh_file)
+        {
+            connect_reader = reader;
+        }
+        else
+        {
+            new_reader(&connect_reader, connect_ext);
+        }
+    }
+    assert(coords_reader != 0);
+    assert(connect_reader != 0);
 
 
-    // XXX: use auto_ptr for the local readers, so we can throw exceptions
+    //
+    // Open files
+    //
+    if (reader != 0)
+    {
+        ierr = reader->open(mesh_file);
+        assert(ierr == 0);
+    }
+    if (coords_reader != reader)
+    {
+        ierr = coords_reader->open(coords_file);
+        assert(ierr == 0);
+    }
+    if (connect_reader != reader)
+    {
+        ierr = connect_reader->open(connect_file);
+        assert(ierr == 0);
+    }
+
+
+    //
+    // Last chance to determine coords_loc and connect_loc
+    //  XXX: for HDF5 files, check dataset or attribute first?
+    //  XXX: for HDF5 files, if mesh_loc is empty, does it *have* to be "/"?
+    //
+    if (coords_loc == "")
+    {
+        if (coords_reader->getType() == Reader::HDF_READER)
+        {
+            coords_loc = mesh_loc + "/coordinates";
+            // XXX: if at this point the coords_loc dataset doesn't exist,
+            // determine the following before bailing out:
+            //  * assert that mesh_loc is a group
+            //  * the group mesh_loc has a CoordLocation string attribute
+            //  * load the CoordLocation string attribute into coords_loc
+        }
+        else if (coords_reader->getType() == Reader::TXT_READER)
+        {
+            // XXX: not yet implemented!
+            // XXX: bail out with a better message
+            // Here we would need support for reading multiple datasets
+            // from a single text file (low priority since we can use
+            // HDF5 for this very purpose).
+            assert(false);
+        }
+        else if (coords_reader->getType() == Reader::VTK_READER)
+        {
+            // XXX: it is not necessary to set coords_loc for VTK files
+            // since VtkReader::get_coordinates() ignores its location argument
+        }
+
+    }
+    if (connect_loc == "")
+    {
+        if (connect_reader->getType() == Reader::HDF_READER)
+        {
+            connect_loc = mesh_loc + "/connectivity";
+            // XXX: if at this point the connect_loc dataset doesn't exist,
+            // determine the following before bailing out:
+            //  * assert that mesh_loc is a group
+            //  * the group mesh_loc has a ConnectLocation string attribute
+            //  * load the ConnectLocation string attribute into coords_loc
+        }
+    }
+
+    //
+    // Read datasets
+    //  XXX: change these functions to return an error code
+    //  XXX: need to check return values for failure
+    coords_reader->get_coordinates(coords_loc.c_str(), &coords, &nno, &nsd);
+    connect_reader->get_connectivity(connect_loc.c_str(), &connect, &nel, &ndofs);
+
+
+
+    /* XXX: use auto_ptr for the local readers, so we can throw exceptions
     if (coords_path != "")
     {
         Reader *coords_reader;
@@ -187,8 +397,11 @@
             mesh_reader->get_connectivity(connect_loc.c_str(), &connect, &nel, &ndofs);
         }
         //mesh_reader->close();
-    }
+    } // */
 
+
+
+
     if ((coords != 0) && (connect != 0))
     {
         meshPart = new MeshPart();
@@ -205,13 +418,15 @@
     {
         if (coords == 0)
         {
-            cerr << "MeshIO::load() error: Could not find mesh coordinates";
-            cerr << endl;
+            cerr << "MeshIO::load() error: "
+                 << "Could not find mesh coordinates"
+                 << endl;
         }
         if (connect == 0)
         {
-            cerr << "MeshIO::load() error: Could not find mesh connectivity";
-            cerr << endl;
+            cerr << "MeshIO::load() error: "
+                 << "Could not find mesh connectivity"
+                 << endl;
         }
     }
 

Modified: cs/benchmark/cigma/trunk/src/MeshIO.h
===================================================================
--- cs/benchmark/cigma/trunk/src/MeshIO.h	2008-02-13 18:28:37 UTC (rev 9311)
+++ cs/benchmark/cigma/trunk/src/MeshIO.h	2008-02-13 18:28:39 UTC (rev 9312)
@@ -18,10 +18,15 @@
     std::string mesh_loc, mesh_file, mesh_ext;
     std::string coords_loc, coords_file, coords_ext;
     std::string connect_loc, connect_file, connect_ext;
+    bool prepare();
 
 public:
     cigma::Reader *reader;
+    cigma::Reader *coords_reader;
+    cigma::Reader *connect_reader;
     cigma::Writer *writer;
+    cigma::Writer *coords_writer;
+    cigma::Writer *connect_writer;
     cigma::MeshPart *meshPart;
 
 public:
@@ -29,7 +34,6 @@
     ~MeshIO();
 
 public:
-    void prepare();
     void load();
     void save();
 



More information about the cig-commits mailing list