00001 #include "EpetraExt_ConfigDefs.h"
00002 #ifdef HAVE_MPI
00003 #include "mpi.h"
00004 #include "Epetra_MpiComm.h"
00005 #else
00006 #include "Epetra_SerialComm.h"
00007 #endif
00008 #include <vector>
00009 #include "Epetra_Map.h"
00010 #include "Epetra_CrsMatrix.h"
00011 #include "Epetra_MultiVector.h"
00012 #include "EpetraExt_HDF5.h"
00013 #include "EpetraExt_Utils.h"
00014 #include "EpetraExt_Exception.h"
00015 #include "Teuchos_ParameterList.hpp"
00016
00017
00018
00019
00020
00021
00022
00023
00024 int main (int argc, char **argv)
00025 {
00026 #ifdef HAVE_MPI
00027 MPI_Init(&argc, &argv);
00028 Epetra_MpiComm Comm(MPI_COMM_WORLD);
00029 #else
00030 Epetra_SerialComm Comm;
00031 #endif
00032
00033 try
00034 {
00035 int n = Comm.NumProc() * 4;
00036 Epetra_Map Map(n, 0, Comm);
00037 Epetra_MultiVector x(Map, 2); x.Random();
00038 Epetra_MultiVector b(Map, 2); x.Random();
00039 Epetra_CrsMatrix Matrix(Copy, Map, 0);
00040
00041 for (int i = 0; i < Map.NumMyElements(); ++i)
00042 {
00043 int ii = Map.GID(i);
00044 double one = 1.0;
00045 Matrix.InsertGlobalValues(ii, 1, &one, &ii);
00046 }
00047 Matrix.FillComplete();
00048
00049
00050 Teuchos::ParameterList List;
00051 List.set("bool type", true);
00052 List.set("int type", 2);
00053 List.set("double type", 3.0);
00054 List.set("string type", "a string");
00055
00056
00057 EpetraExt::HDF5 HDF5(Comm);
00058
00059
00060 HDF5.Create("myfile.h5");
00061
00062
00063
00064
00065
00066 if (Comm.MyPID() == 0)
00067 cout << "Writing objects to HDF5 file myfile.h5..." << endl << endl;
00068
00069
00070 HDF5.Write("map-" + EpetraExt::toString(Comm.NumProc()), Map);
00071
00072 HDF5.Write("matrix", Matrix);
00073
00074 HDF5.Write("x", x);
00075 HDF5.Write("b", b);
00076
00077 HDF5.Write("matrix", "integration order", 1);
00078 HDF5.Write("matrix", "numerical drop", 0.1);
00079 HDF5.Write("matrix", "package name", "EpetraExt");
00080 HDF5.Write("matrix", "author", "Marzio Sala");
00081 HDF5.Write("matrix", "institution", "ETHZ/D-INFK");
00082
00083 HDF5.Write("my parameters", "latitude", 12);
00084 HDF5.Write("my parameters", "longitude", 67);
00085
00086
00087 HDF5.Write("List", List);
00088
00089
00090 vector<int> iarray(3);
00091 iarray[0] = 0, iarray[1] = 1; iarray[2] = 2;
00092 HDF5.Write("my parameters", "int array", H5T_NATIVE_INT, 3, &iarray[0]);
00093
00094 vector<double> darray(3);
00095 darray[0] = 0.1, darray[1] = 1.1; darray[2] = 2.1;
00096 HDF5.Write("my parameters", "double array", H5T_NATIVE_DOUBLE, 3, &darray[0]);
00097
00098
00099
00100
00101
00102
00103
00104
00105 if (Comm.MyPID() == 0)
00106 cout << "Reading objects from HDF5 file myfile.h5..." << endl << endl;
00107
00108 Epetra_Map* NewMap = 0;
00109 Epetra_CrsMatrix* NewMatrix = 0;
00110 Epetra_MultiVector* NewX,* NewB;
00111
00112
00113
00114 if (HDF5.IsContained("map-" + EpetraExt::toString(Comm.NumProc())))
00115 {
00116 HDF5.Read("map-" + EpetraExt::toString(Comm.NumProc()), NewMap);
00117 HDF5.Read("matrix", *NewMap, *NewMap, NewMatrix);
00118 }
00119 else
00120 {
00121 int NumGlobalRows;
00122 HDF5.Read("matrix", "NumGlobalRows", NumGlobalRows);
00123 NewMap = new Epetra_Map(NumGlobalRows, 0, Comm);
00124 HDF5.Read("matrix", *NewMap, *NewMap, NewMatrix);
00125 }
00126
00127
00128
00129 int NewNumGlobalNonzeros;
00130 HDF5.Read("matrix", "NumGlobalNonzeros", NewNumGlobalNonzeros);
00131
00132 assert (Matrix.NumGlobalNonzeros() == NewNumGlobalNonzeros);
00133
00134
00135 HDF5.Read("x", NewX);
00136 HDF5.Read("b", NewB);
00137
00138
00139 int new_latitude, new_longitude;
00140 vector<int> new_iarray(3);
00141 vector<double> new_darray(3);
00142 HDF5.Read("my parameters", "latitude", new_latitude);
00143 HDF5.Read("my parameters", "longitude", new_longitude);
00144 HDF5.Read("my parameters", "int array", H5T_NATIVE_INT, 3, &new_iarray[0]);
00145 HDF5.Read("my parameters", "double array", H5T_NATIVE_DOUBLE, 3, &new_darray[0]);
00146
00147
00148 string PackageName;
00149 HDF5.Read("matrix", "package name", PackageName);
00150
00151 Teuchos::ParameterList newList;
00152 HDF5.Read("List", newList);
00153 if (Comm.MyPID() == 0)
00154 {
00155 cout << "New list as read from file is:" << endl;
00156 cout << "bool type = " << newList.get("bool type", false) << endl;
00157 cout << "int type = " << newList.get("int type", -1) << endl;
00158 cout << "double type = " << newList.get("double type", -1.0) << endl;
00159 cout << "string type = " << newList.get("string type", "not-set") << endl;
00160 cout << endl;
00161
00162 cout << "Checking some read and written data..." << endl;
00163 for (int i = 0; i < 3; ++i)
00164 cout << "iarray[" << i << "] = " << iarray[i] << " should be " << new_iarray[i] << endl;
00165 for (int i = 0; i < 3; ++i)
00166 cout << "darray[" << i << "] = " << darray[i] << " should be " << new_darray[i] << endl;
00167 cout << endl;
00168 cout << "Try to print out the content of myfile.h5 using the command" << endl;
00169 cout << " h5dump myfile.h5" << endl;
00170 }
00171
00172
00173
00174
00175 HDF5.Close();
00176
00177
00178 if (NewMap ) delete NewMap;
00179 if (NewMatrix) delete NewMatrix;
00180 if (NewX ) delete NewX;
00181 if (NewB ) delete NewB;
00182 }
00183 catch(EpetraExt::Exception& rhs)
00184 {
00185 rhs.Print();
00186 }
00187 catch (...)
00188 {
00189 cerr << "Caught generic exception" << endl;
00190 }
00191
00192
00193 #ifdef HAVE_MPI
00194 MPI_Finalize();
00195 #endif
00196
00197 return(EXIT_SUCCESS);
00198 }