[cig-commits] [commit] master: Finished moving event HDF5 output into QuakeLib (4d00835)

cig_noreply at geodynamics.org cig_noreply at geodynamics.org
Wed Oct 8 17:05:59 PDT 2014


Repository : https://github.com/geodynamics/vc

On branch  : master
Link       : https://github.com/geodynamics/vc/compare/23464fca3efa2b6ad7ee0ce8f60c225b18b49741...e4325192ad1118379f46ba66899cb98143d09e04

>---------------------------------------------------------------

commit 4d00835a8d15ec9d5d640202c3af12a051bcbe36
Author: Eric Heien <emheien at ucdavis.edu>
Date:   Thu Sep 25 13:58:44 2014 -0700

    Finished moving event HDF5 output into QuakeLib


>---------------------------------------------------------------

4d00835a8d15ec9d5d640202c3af12a051bcbe36
 quakelib/src/QuakeLibIO.cpp |  42 ++++++------
 quakelib/src/QuakeLibIO.h   |  10 +++
 src/io/EventOutput.cpp      |  74 ++++++++++++++++++---
 src/io/EventOutput.h        |   6 +-
 src/io/HDF5Data.cpp         | 152 --------------------------------------------
 src/io/HDF5Data.h           |  31 ---------
 6 files changed, 99 insertions(+), 216 deletions(-)

diff --git a/quakelib/src/QuakeLibIO.cpp b/quakelib/src/QuakeLibIO.cpp
index 486a064..87e5ad7 100644
--- a/quakelib/src/QuakeLibIO.cpp
+++ b/quakelib/src/QuakeLibIO.cpp
@@ -1257,7 +1257,7 @@ void quakelib::ModelWorld::write_section_hdf5(const hid_t &data_file) const {
     // Create the section table
     res = H5TBmake_table("Fault Sections",
                          data_file,
-                         "sections",
+                         ModelSection::hdf5_table_name().c_str(),
                          num_fields,
                          num_sections,
                          sizeof(SectionData),
@@ -1275,7 +1275,10 @@ void quakelib::ModelWorld::write_section_hdf5(const hid_t &data_file) const {
     for (i=0; i<num_fields; ++i) {
         std::stringstream   ss;
         ss << "FIELD_" << i << "_DETAILS";
-        res = H5LTset_attribute_string(data_file, "sections", ss.str().c_str(), field_details[i]);
+        res = H5LTset_attribute_string(data_file,
+                                       ModelSection::hdf5_table_name().c_str(),
+                                       ss.str().c_str(),
+                                       field_details[i]);
 
         if (res < 0) exit(-1);
     }
@@ -2394,9 +2397,6 @@ void quakelib::ModelSweeps::write_ascii_header(std::ostream &out_stream) {
     out_stream << "\n";
 }
 
-#define EVENT_TABLE_HDF5            "event_table"
-#define SWEEP_TABLE_HDF5            "event_sweep_table"
-
 void quakelib::ModelSweeps::setup_sweeps_hdf5(const hid_t &data_file) {
     std::vector<FieldDesc>  descs;
     size_t                  num_fields;
@@ -2433,7 +2433,7 @@ void quakelib::ModelSweeps::setup_sweeps_hdf5(const hid_t &data_file) {
     // Create the sweep table
     res = H5TBmake_table("Sweeps Table",
                          data_file,
-                         SWEEP_TABLE_HDF5,
+                         ModelSweeps::hdf5_table_name().c_str(),
                          num_fields,
                          0,
                          sizeof(SweepData),
@@ -2451,7 +2451,7 @@ void quakelib::ModelSweeps::setup_sweeps_hdf5(const hid_t &data_file) {
     for (i=0; i<num_fields; ++i) {
         std::stringstream   ss;
         ss << "FIELD_" << i << "_DETAILS";
-        res = H5LTset_attribute_string(data_file, "sections", ss.str().c_str(), field_details[i]);
+        res = H5LTset_attribute_string(data_file, ModelSweeps::hdf5_table_name().c_str(), ss.str().c_str(), field_details[i]);
 
         if (res < 0) exit(-1);
     }
@@ -2501,7 +2501,7 @@ void quakelib::ModelSweeps::append_sweeps_hdf5(const hid_t &data_file) const {
 
     // Create the section table
     res = H5TBappend_records(data_file,
-                             SWEEP_TABLE_HDF5,
+                             ModelSweeps::hdf5_table_name().c_str(),
                              num_sweeps,
                              sizeof(SweepData),
                              field_offsets,
@@ -2664,10 +2664,10 @@ void quakelib::ModelEvent::setup_event_hdf5(const hid_t &data_file) {
         field_sizes[i] = descs[i].size;
     }
 
-    // Create the sweep table
+    // Create the event table
     res = H5TBmake_table("Event Table",
                          data_file,
-                         EVENT_TABLE_HDF5,
+                         ModelEvent::hdf5_table_name().c_str(),
                          num_fields,
                          0,
                          sizeof(EventData),
@@ -2685,7 +2685,10 @@ void quakelib::ModelEvent::setup_event_hdf5(const hid_t &data_file) {
     for (i=0; i<num_fields; ++i) {
         std::stringstream   ss;
         ss << "FIELD_" << i << "_DETAILS";
-        res = H5LTset_attribute_string(data_file, "sections", ss.str().c_str(), field_details[i]);
+        res = H5LTset_attribute_string(data_file,
+                                       ModelEvent::hdf5_table_name().c_str(),
+                                       ss.str().c_str(),
+                                       field_details[i]);
 
         if (res < 0) exit(-1);
     }
@@ -2704,17 +2707,16 @@ void quakelib::ModelEvent::setup_event_hdf5(const hid_t &data_file) {
 }
 
 void quakelib::ModelEvent::append_event_hdf5(const hid_t &data_file) const {
-    std::vector<FieldDesc>                  descs;
-    std::vector<EventData>::const_iterator  it;
-    size_t                                  num_fields;
-    unsigned int                i;
-    size_t                      *field_offsets;
-    size_t                      *field_sizes;
-    herr_t                      res;
+    std::vector<FieldDesc>  descs;
+    size_t                  num_fields;
+    unsigned int            i;
+    size_t                  *field_offsets;
+    size_t                  *field_sizes;
+    herr_t                  res;
 
     // Set up the section table definition
     descs.clear();
-    ModelSweeps::get_field_descs(descs);
+    ModelEvent::get_field_descs(descs);
     num_fields = descs.size();
     field_offsets = new size_t[num_fields];
     field_sizes = new size_t[num_fields];
@@ -2726,7 +2728,7 @@ void quakelib::ModelEvent::append_event_hdf5(const hid_t &data_file) const {
 
     // Append the event record
     res = H5TBappend_records(data_file,
-                             EVENT_TABLE_HDF5,
+                             ModelEvent::hdf5_table_name().c_str(),
                              1,
                              sizeof(EventData),
                              field_offsets,
diff --git a/quakelib/src/QuakeLibIO.h b/quakelib/src/QuakeLibIO.h
index bce7f99..fd01f72 100644
--- a/quakelib/src/QuakeLibIO.h
+++ b/quakelib/src/QuakeLibIO.h
@@ -693,6 +693,11 @@ namespace quakelib {
                 return _sweeps.size();
             };
 
+#ifdef HDF5_FOUND
+        static std::string hdf5_table_name(void) {
+            return "sweeps";
+        };
+#endif
             static void get_field_descs(std::vector<FieldDesc> &descs);
             static void write_ascii_header(std::ostream &out_stream);
             static void setup_sweeps_hdf5(const hid_t &data_file);
@@ -914,6 +919,11 @@ namespace quakelib {
                 return _event_sweeps.end();
             };*/
 
+#ifdef HDF5_FOUND
+        static std::string hdf5_table_name(void) {
+            return "events";
+        };
+#endif
             static void get_field_descs(std::vector<FieldDesc> &descs);
             static void write_ascii_header(std::ostream &out_stream);
             static void setup_event_hdf5(const hid_t &data_file);
diff --git a/src/io/EventOutput.cpp b/src/io/EventOutput.cpp
index 07d11f8..ac54e32 100644
--- a/src/io/EventOutput.cpp
+++ b/src/io/EventOutput.cpp
@@ -40,22 +40,74 @@ void EventOutput::initDesc(const SimFramework *_sim) const {
     sim->console() << "# Writing events in format " << sim->getEventOutfileType() << " to file " << sim->getEventOutfile() << std::endl;
 }
 
+/*!
+ Initialize the HDF5 writer using the specified model dimensions.
+ */
+#ifdef HDF5_FOUND
+void EventOutput::open_hdf5_file(const std::string &hdf5_file_name, const double &start_year, const double &end_year) {
+    hid_t   plist_id;
+    herr_t  status;
+    double  tmp[2];
+    hid_t   sim_years_set;
+    hid_t   pair_val_dataspace;
+    hsize_t dimsf[2];
+    
+    plist_id = H5Pcreate(H5P_FILE_ACCESS);
+    
+    if (plist_id < 0) exit(-1);
+    
+#ifdef MPI_C_FOUND
+#ifdef H5_HAVE_PARALLEL
+    //H5Pset_fapl_mpio(plist_id, MPI_COMM_WORLD, MPI_INFO_NULL);
+#endif
+#endif
+    // Create the data file, overwriting any old files
+    data_file = H5Fcreate(hdf5_file_name.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, plist_id);
+    
+    if (data_file < 0) exit(-1);
+    
+    // Create dataspace for pairs of values
+    dimsf[0] = 2;
+    pair_val_dataspace = H5Screate_simple(1, dimsf, NULL);
+    
+    // Create entries for the simulation start/stop years and base longitude/latitude
+    sim_years_set = H5Dcreate2(data_file, "sim_years", H5T_NATIVE_DOUBLE, pair_val_dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
+    
+    if (sim_years_set < 0) exit(-1);
+    
+    // Create the event table
+    quakelib::ModelEvent::setup_event_hdf5(data_file);
+    
+    // Create the event sweeps table
+    quakelib::ModelSweeps::setup_sweeps_hdf5(data_file);
+    
+    // Record the simulation start/end years
+    tmp[0] = start_year;
+    tmp[1] = end_year;
+    status = H5Dwrite(sim_years_set, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, &tmp);
+    herr_t      res;
+    
+    // Close the handles we've used
+    res = H5Dclose(sim_years_set);
+    if (res < 0) exit(-1);
+    
+    H5Pclose(plist_id);
+}
+#endif
+
 void EventOutput::init(SimFramework *_sim) {
     VCSimulation                *sim = static_cast<VCSimulation *>(_sim);
     BlockList::const_iterator   it;
 
-    h5_data = NULL;
     sweep_count = 0;
+    data_file = 0;
     next_pause_check = sim->itersPerSecond();
 
     // Only the root node writes to the output file
     if (sim->isRootNode()) {
         if (sim->getEventOutfileType() == "hdf5") {
 #ifdef HDF5_FOUND
-            h5_data = new HDF5DataWriter(sim->getEventOutfile());
-
-            // Set the start and end years of the simulation
-            h5_data->setStartEndYears(sim->getYear(), sim->getSimDuration());
+            open_hdf5_file(sim->getEventOutfile(), sim->getYear(), sim->getSimDuration());
 #else
             std::cerr << "ERROR: HDF5 library not linked, cannot use HDF5 output files." << std::endl;
             exit(-1);
@@ -86,7 +138,7 @@ SimRequest EventOutput::run(SimFramework *_sim) {
 
     if (sim->getEventOutfileType() == "hdf5") {
 #ifdef HDF5_FOUND
-        h5_data->writeEvent(sim->getCurrentEvent());
+        sim->getCurrentEvent().append_event_hdf5(data_file);
 #endif
     } else if (sim->getEventOutfileType() == "text") {
         unsigned int num_sweeps = sim->getCurrentEvent().getSweeps().size();
@@ -108,7 +160,7 @@ SimRequest EventOutput::run(SimFramework *_sim) {
         if (sim->isRootNode() && pauseFileExists()) {
             // Flush out the HDF5 data
 #ifdef HDF5_FOUND
-            h5_data->flush();
+            H5Fflush(data_file, H5F_SCOPE_GLOBAL);
 #endif
             sim->console() << "# Pausing simulation due to presence of file " << PAUSE_FILE_NAME << std::endl;
 
@@ -135,9 +187,11 @@ void EventOutput::finish(SimFramework *_sim) {
     VCSimulation        *sim = static_cast<VCSimulation *>(_sim);
 
 #ifdef HDF5_FOUND
-
-    if (h5_data) delete h5_data;
-
+    if (data_file) {
+        herr_t      res;
+        res = H5Fclose(data_file);
+        if (res < 0) exit(-1);
+    }
 #endif
 
     if (sim->getEventOutfileType() == "text") {
diff --git a/src/io/EventOutput.h b/src/io/EventOutput.h
index f633690..bca50ae 100644
--- a/src/io/EventOutput.h
+++ b/src/io/EventOutput.h
@@ -36,9 +36,9 @@
 class EventOutput : public SimPlugin {
     private:
 #ifdef HDF5_FOUND
-        HDF5DataWriter      *h5_data;
-#else
-        void                *h5_data;
+        // HDF5 handle to data file
+        hid_t               data_file;
+        void open_hdf5_file(const std::string &hdf5_file_name, const double &start_year, const double &end_year);
 #endif
         unsigned int        next_pause_check;
         unsigned int        sweep_count;
diff --git a/src/io/HDF5Data.cpp b/src/io/HDF5Data.cpp
index 923d591..054d461 100644
--- a/src/io/HDF5Data.cpp
+++ b/src/io/HDF5Data.cpp
@@ -339,156 +339,4 @@ void HDF5GreensDataReader::getGreensVals(const int &bid, double *shear_vals, dou
     H5Sclose(mem_select);
 }
 
-HDF5Data::~HDF5Data(void) {
-    herr_t      res;
-
-    // Close the handles we've used
-    res = H5Dclose(sim_years_set);
-
-    if (res < 0) exit(-1);
-
-    res = H5Fclose(data_file);
-
-    if (res < 0) exit(-1);
-}
-
-/*!
- Initialize the HDF5 writer using the specified model dimensions.
- */
-HDF5DataWriter::HDF5DataWriter(const std::string &hdf5_file_name) : HDF5Data() {
-    hid_t               plist_id;
-
-    plist_id = H5Pcreate(H5P_FILE_ACCESS);
-
-    if (plist_id < 0) exit(-1);
-
-#ifdef MPI_C_FOUND
-#ifdef H5_HAVE_PARALLEL
-    //H5Pset_fapl_mpio(plist_id, MPI_COMM_WORLD, MPI_INFO_NULL);
-#endif
-#endif
-    // Create the data file, overwriting any old files
-    data_file = H5Fcreate(hdf5_file_name.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, plist_id);
-
-    if (data_file < 0) exit(-1);
-
-    createH5Handles();
-
-    // Create entries for the simulation start/stop years and base longitude/latitude
-    sim_years_set = H5Dcreate2(data_file, SIM_YEARS_HDF5, H5T_NATIVE_DOUBLE, pair_val_dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
-
-    if (sim_years_set < 0) exit(-1);
-
-    // Create the event table
-    quakelib::ModelEvent::setup_event_hdf5(data_file);
-
-    // Create the event sweeps table
-    quakelib::ModelSweeps::setup_sweeps_hdf5(data_file);
-
-    H5Pclose(plist_id);
-}
-
-void HDF5Data::createH5Handles(void) {
-    hsize_t     dimsf[2];
-
-    // Create dataspace for pairs of values
-    dimsf[0] = 2;
-    pair_val_dataspace = H5Screate_simple(1, dimsf, NULL);
-}
-
-/*!
- Set the start and end years of the simulation.
- */
-void HDF5DataWriter::setStartEndYears(const double &new_start_year, const double &new_end_year) {
-    herr_t      status;
-    double      tmp[2];
-
-    tmp[0] = new_start_year;
-    tmp[1] = new_end_year;
-    status = H5Dwrite(sim_years_set, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, &tmp);
-}
-
-/*!
- Write the information for an event to the HDF5 file.
- */
-void HDF5DataWriter::writeEvent(void) {
-    VCEventSweep::iterator      eit;
-    VCGeneralEventSet::iterator git;
-    quakelib::ElementIDSet      involved_blocks;
-    EventInfo                   e_info;
-    EventSweepInfo              *s_info_array;
-    herr_t                      status;
-    unsigned int                i, sweep_num, rec_num, event_num;
-    hsize_t                     start_fields, end_fields, start_recs, end_recs;
-
-    event_num = event.getEventNumber();
-
-    // Count the number of sweep records for preallocation
-    for (it=event.sweepBegin(),rec_num=0; it!=event.sweepEnd(); ++it) {
-        for (eit=it->begin(); eit!=it->end(); ++eit) {
-            rec_num++;
-        }
-    }
-
-    // Check the number of records before appending to the table
-    status = H5TBget_table_info(data_file, SWEEP_TABLE_HDF5, &start_fields, &start_recs);
-
-    if (status < 0) exit(-1);
-
-    if (rec_num > 0) {
-        s_info_array = new EventSweepInfo[rec_num];
-
-        // Write the event sweep information
-        for (it=event.sweepBegin(),i=0,sweep_num=0; it!=event.sweepEnd(); ++it,++sweep_num) {
-            for (eit=it->begin(); eit!=it->end(); ++eit,++i) {
-                s_info_array[i].event_number = event_num;
-                s_info_array[i].sweep_number = sweep_num;
-                s_info_array[i].block_id = eit->first;
-                s_info_array[i].block_slip = eit->second.slip;
-                s_info_array[i].block_area = eit->second.area;
-                s_info_array[i].block_mu = eit->second.mu;
-                s_info_array[i].shear_init = eit->second.shear_init;
-                s_info_array[i].shear_final = eit->second.shear_final;
-                s_info_array[i].normal_init = eit->second.normal_init;
-                s_info_array[i].normal_final = eit->second.normal_final;
-            }
-        }
-
-        H5TBappend_records(data_file,
-                           SWEEP_TABLE_HDF5,
-                           rec_num,
-                           sizeof(EventSweepInfo),
-                           sweep_field_offsets,
-                           sweep_field_sizes,
-                           s_info_array);
-        delete s_info_array;
-    }
-
-    // Get the number of records after appending to the table
-    status = H5TBget_table_info(data_file, SWEEP_TABLE_HDF5, &end_fields, &end_recs);
-
-    if (status < 0) exit(-1);
-
-    e_info.start_sweep_rec = start_recs;
-    e_info.end_sweep_rec = end_recs;
-
-    event.getInvolvedElements(involved_blocks);
-
-    e_info.event_number = event.getEventNumber();
-    e_info.event_year = event.getEventYear();
-    e_info.event_trigger = event.getEventTrigger();
-    e_info.event_magnitude = event.getMagnitude(involved_blocks);
-
-    e_info.init_shear = event.getShearStressInit();
-    e_info.init_normal = event.getNormalStressInit();
-    e_info.final_shear = event.getShearStressFinal();
-    e_info.final_normal = event.getNormalStressFinal();
-
-    H5TBappend_records(data_file, EVENT_TABLE_HDF5, 1, sizeof(EventInfo), event_field_offsets, event_field_sizes, &e_info);
-}
-
-void HDF5DataWriter::flush(void) {
-    H5Fflush(data_file, H5F_SCOPE_GLOBAL);
-}
-
 #endif
diff --git a/src/io/HDF5Data.h b/src/io/HDF5Data.h
index a84bb16..4202716 100644
--- a/src/io/HDF5Data.h
+++ b/src/io/HDF5Data.h
@@ -42,10 +42,6 @@
 #define GREEN_SHEAR_HDF5            "greens_shear"
 #define GREEN_NORMAL_HDF5           "greens_normal"
 
-// HDF5 file data definitions
-#define SIM_YEARS_HDF5              "sim_years"
-#define BASE_LAT_LON_HDF5           "base_lat_lon"
-
 // State checkpoint table definitions
 #define CHECKPOINT_STATE_HDF5       "checkpoint_state"
 #define CHECKPOINT_YEAR_HDF5        "checkpoint_year"
@@ -126,33 +122,6 @@ class HDF5GreensDataWriter : public HDF5GreensData {
         void setGreensVals(const int &bid, const double *shear_vals, const double *norm_vals);
 };
 
-class HDF5Data {
-    protected:
-        // HDF5 handle to data file
-        hid_t               data_file;
-
-        // Handles to data in the file
-        hid_t               sim_years_set;
-
-        // Handles to data space specifications
-        hid_t               pair_val_dataspace;
-
-        void createH5Handles(void);
-
-    public:
-        HDF5Data(void) {};
-        ~HDF5Data(void);
-};
-
-class HDF5DataWriter : public HDF5Data {
-    public:
-        HDF5DataWriter(const std::string &hdf5_file_name);
-        void setStartEndYears(const double &new_start_year, const double &new_end_year);
-        void flush(void);
-
-        void writeEvent(void);
-};
-
 #endif
 
 #endif



More information about the CIG-COMMITS mailing list