dataconv.h

Go to the documentation of this file.
00001 /////////////////////////////////////////////////////////////////////////////
00002 ///
00003 /// @file dataconv.h
00004 ///
00005 /// Data conversion for sending/receiving.
00006 /// This file contains the definitions of the operators << and >>
00007 /// for encoding some variables to the binary data structure
00008 /// and retrieving them. This binary data structure is used
00009 /// in the communication between the coordinator and workers.
00010 ///
00011 /// @author Pawel Pilarczyk
00012 ///
00013 /////////////////////////////////////////////////////////////////////////////
00014 
00015 // Copyright (C) 1997-2008 by Pawel Pilarczyk.
00016 //
00017 // This file is part of my research software package.  This is free software;
00018 // you can redistribute it and/or modify it under the terms of the GNU
00019 // General Public License as published by the Free Software Foundation;
00020 // either version 2 of the License, or (at your option) any later version.
00021 //
00022 // This software is distributed in the hope that it will be useful,
00023 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00024 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00025 // GNU General Public License for more details.
00026 //
00027 // You should have received a copy of the GNU General Public License along
00028 // with this software; see the file "license.txt".  If not, write to the
00029 // Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00030 // MA 02111-1307, USA.
00031 
00032 // Started on February 15, 2008. Last revision: February 29, 2008.
00033 
00034 
00035 #ifndef _CMGRAPHS_DATACONV_H_
00036 #define _CMGRAPHS_DATACONV_H_
00037 
00038 
00039 // include some standard C++ header files
00040 #include <string>
00041 #include <sstream>
00042 
00043 // include selected header files from the CHomP library
00044 #include "chomp/system/config.h"
00045 #include "chomp/system/textfile.h"
00046 #include "chomp/multiwork/mw.h"
00047 
00048 // include local header files
00049 #include "config.h"
00050 #include "typedefs.h"
00051 #include "utils.h"
00052 #include "eigenval.h"
00053 
00054 
00055 // --------------------------------------------------
00056 // ------------------ Conley index ------------------
00057 // --------------------------------------------------
00058 
00059 /// Writes a Conley index to a MultiWork data structure.
00060 inline chomp::multiwork::mwData &operator << (chomp::multiwork::mwData &data,
00061         const theConleyIndexType &ind)
00062 {
00063         std::ostringstream s;
00064         s << ind;
00065         data << s. str ();
00066         return data;
00067 } /* operator << */
00068 
00069 /// Reads a Conley index from a MultiWork data structure.
00070 inline chomp::multiwork::mwData &operator >> (chomp::multiwork::mwData &data,
00071         theConleyIndexType &ind)
00072 {
00073         std::string str;
00074         data >> str;
00075         std::istringstream s (str);
00076         s >> ind;
00077         return data;
00078 } /* operator >> */
00079 
00080 
00081 // --------------------------------------------------
00082 // ------------ Conley index eigenvalues ------------
00083 // --------------------------------------------------
00084 
00085 /// Writes a set of Conley index eigenvalues to a MultiWork data structure.
00086 inline chomp::multiwork::mwData &operator << (chomp::multiwork::mwData &data,
00087         const IndexEigenValues &eigenValues)
00088 {
00089         int maxLevel = eigenValues. maxLevel ();
00090         data << maxLevel;
00091         for (int level = 0; level < maxLevel; ++ level)
00092         {
00093                 const std::vector<double> &re = eigenValues. re (level);
00094                 const std::vector<double> &im = eigenValues. im (level);
00095                 unsigned int nValues = re. size ();
00096                 if (nValues != im. size ())
00097                         throw "Different sizes of Re and Im vectors.";
00098                 data << nValues;
00099                 for (unsigned int i = 0; i < nValues; ++ i)
00100                         data << re [i] << im [i];
00101         }
00102         return data;
00103 } /* operator << */
00104 
00105 /// Reads a set of Conley index eigenvalues from a MultiWork data structure.
00106 inline chomp::multiwork::mwData &operator >> (chomp::multiwork::mwData &data,
00107         IndexEigenValues &eigenValues)
00108 {
00109         int maxLevel = 0;
00110         data >> maxLevel;
00111         for (int level = 0; level < maxLevel; ++ level)
00112         {
00113                 std::vector<double> &reVector = eigenValues. re (level);
00114                 std::vector<double> &imVector = eigenValues. im (level);
00115                 unsigned int nValues = 0;
00116                 data >> nValues;
00117                 for (unsigned int i = 0; i < nValues; ++ i)
00118                 {
00119                         double re = 0, im = 0;
00120                         data >> re >> im;
00121                         reVector. push_back (re);
00122                         imVector. push_back (im);
00123                 }
00124         }
00125         return data;
00126 } /* operator >> */
00127 
00128 
00129 // --------------------------------------------------
00130 // ----------------- parameter cube -----------------
00131 // --------------------------------------------------
00132 
00133 /// Writes a parameter cube to a MultiWork data structure.
00134 inline chomp::multiwork::mwData &operator << (chomp::multiwork::mwData &data,
00135         const parCube &q)
00136 {
00137         int dim = q. dim ();
00138         data << dim;
00139         parCube::CoordType c [parCube::MaxDim];
00140         q. coord (c);
00141         for (int i = 0; i < dim; ++ i)
00142                 data << static_cast<int> (c [i]);
00143         return data;
00144 } /* operator << */
00145 
00146 /// Reads a parameter cube from a MultiWork data structure.
00147 inline chomp::multiwork::mwData &operator >> (chomp::multiwork::mwData &data,
00148         parCube &q)
00149 {
00150         int dim = 0;
00151         data >> dim;
00152         if (dim != paramDim)
00153                 throw "Wrong cube dimension in mwData.";
00154         parCube::CoordType c [paramDim];
00155         for (int i = 0; i < dim; ++ i)
00156         {
00157                 int number = 0;
00158                 data >> number;
00159                 c [i] = static_cast<parCube::CoordType> (number);
00160         }
00161         q = parCube (c, dim);
00162 
00163         return data;
00164 } /* operator >> */
00165 
00166 
00167 // --------------------------------------------------
00168 // ------------- set of parameter cubes -------------
00169 // --------------------------------------------------
00170 
00171 /// Writes a set of parameter cubes to a MultiWork data structure.
00172 inline chomp::multiwork::mwData &operator << (chomp::multiwork::mwData &data,
00173         const parCubes &X)
00174 {
00175         int size = X. size ();
00176         data << size;
00177         for (int i = 0; i < size; ++ i)
00178                 data << X [i];
00179         return data;
00180 } /* operator << */
00181 
00182 /// Reads a set of parameter cubes from a MultiWork data structure.
00183 inline chomp::multiwork::mwData &operator >> (chomp::multiwork::mwData &data,
00184         parCubes &X)
00185 {
00186         int size = 0;
00187         data >> size;
00188         if (size < 0)
00189                 throw "Negative size of a set of cubes in mwData.";
00190         for (int i = 0; i < size; ++ i)
00191         {
00192                 parCube q;
00193                 data >> q;
00194                 X. add (q);
00195         }
00196         return data;
00197 } /* operator >> */
00198 
00199 
00200 // --------------------------------------------------
00201 // ---------------- phase space cube ----------------
00202 // --------------------------------------------------
00203 
00204 /// Writes a phase space cube to a MultiWork data structure.
00205 inline chomp::multiwork::mwData &operator << (chomp::multiwork::mwData &data,
00206         const spcCube &q)
00207 {
00208         int dim = q. dim ();
00209         data << dim;
00210         spcCube::CoordType c [spcCube::MaxDim];
00211         q. coord (c);
00212         for (int i = 0; i < dim; ++ i)
00213                 data << static_cast<int> (c [i]);
00214         return data;
00215 } /* operator << */
00216 
00217 /// Reads a phase space cube from a MultiWork data structure.
00218 inline chomp::multiwork::mwData &operator >> (chomp::multiwork::mwData &data,
00219         spcCube &q)
00220 {
00221         int dim = 0;
00222         data >> dim;
00223         if (dim != spaceDim)
00224                 throw "Wrong cube dimension in mwData.";
00225         spcCube::CoordType c [paramDim];
00226         for (int i = 0; i < dim; ++ i)
00227         {
00228                 int number = 0;
00229                 data >> number;
00230                 c [i] = static_cast<spcCube::CoordType> (number);
00231         }
00232         q = spcCube (c, dim);
00233 
00234         return data;
00235 } /* operator >> */
00236 
00237 
00238 // --------------------------------------------------
00239 // ----------------- directed graph -----------------
00240 // --------------------------------------------------
00241 
00242 /// Writes a directed graph to a MultiWork data structure.
00243 inline chomp::multiwork::mwData &operator << (chomp::multiwork::mwData &data,
00244         const chomp::homology::diGraph<> &g)
00245 {
00246         // write the number of vertices of the directed graph
00247         int nVert = g. countVertices ();
00248         data << nVert;
00249 
00250         // the numbers of edges emanating from each vertex and their targets
00251         for (int v = 0; v < nVert; ++ v)
00252         {
00253                 // write the number of edges emanating vrom this vertex
00254                 int nEdg = g. countEdges (v);
00255                 data << nEdg;
00256 
00257                 // write the target vertices of the edges
00258                 for (int e = 0; e < nEdg; ++ e)
00259                 {
00260                         int target = g. getEdge (v, e);
00261                         data << target;
00262                 }
00263         }
00264 
00265         return data;
00266 } /* operator << */
00267 
00268 /// Reads a directed graph from a MultiWork data structure.
00269 inline chomp::multiwork::mwData &operator >> (chomp::multiwork::mwData &data,
00270         chomp::homology::diGraph<> &g)
00271 {
00272         // determine the number of vertices
00273         int nVert = -1;
00274         data >> nVert;
00275         if (nVert < 0)
00276                 throw "Negative number of vertices decoded from mwData.";
00277 
00278         // read the numbers of edges emanating from each vertex
00279         // and add their targets
00280         for (int v = 0; v < nVert; ++ v)
00281         {
00282                 // add the vertex to the graph
00283                 g. addVertex ();
00284 
00285                 // read the number of edges emanating from this vertex
00286                 int nEdg = -1;
00287                 data >> nEdg;
00288 
00289                 // read the target vertices and add them to the graph
00290                 for (int e = 0; e < nEdg; ++ e)
00291                 {
00292                         int target = -1;
00293                         data >> target;
00294                         if ((target < 0) || (target >= nVert))
00295                                 throw "Wrong vertex number found in mwData.";
00296                         g. addEdge (target);
00297                 }
00298         }
00299 
00300         return data;
00301 } /* operator >> */
00302 
00303 
00304 // --------------------------------------------------
00305 // ------------- vector of any objects --------------
00306 // --------------------------------------------------
00307 
00308 /// Writes a standard vector of any objects to a MultiWork data structure.
00309 template<class AnyType>
00310 inline chomp::multiwork::mwData &operator << (chomp::multiwork::mwData &data,
00311         const std::vector<AnyType> &vect)
00312 {
00313         unsigned int size = vect. size ();
00314         data << size;
00315         for (unsigned int i = 0; i < size; ++ i)
00316                 data << vect [i];
00317         return data;
00318 } /* operator << */
00319 
00320 /// Reads a standard vector of any objects from a MultiWork data structure.
00321 template<class AnyType>
00322 inline chomp::multiwork::mwData &operator >> (chomp::multiwork::mwData &data,
00323         std::vector<AnyType> &vect)
00324 {
00325         unsigned int size = 0;
00326         data >> size;
00327         if (size > 0)
00328                 vect. resize (size);
00329         for (unsigned int i = 0; i < size; ++ i)
00330                 data >> vect [i];
00331         return data;
00332 } /* operator >> */
00333 
00334 
00335 #endif // _CMGRAPHS_DATACONV_H_
00336 

Generated on Sun Mar 28 17:47:57 2010 for The Conley-Morse Graphs Software by  doxygen 1.5.3