00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #ifndef _CMGRAPHS_INDCACHE_H_
00036 #define _CMGRAPHS_INDCACHE_H_
00037
00038
00039
00040 #include <fstream>
00041
00042
00043 #include "chomp/system/textfile.h"
00044 #include "chomp/cubes/pointset.h"
00045
00046
00047 #include "config.h"
00048 #include "typedefs.h"
00049 #include "conindex.h"
00050 #include "morsedec.h"
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 class IndexCache
00062 {
00063 public:
00064
00065
00066 IndexCache (int nSets);
00067
00068
00069
00070
00071 int read (const std::string &fileName,
00072 theMorseDecompositionType &morseDec);
00073
00074
00075
00076
00077
00078 int write (const std::string &fileName,
00079 const theMorseDecompositionType &morseDec) const;
00080
00081
00082 ~IndexCache ();
00083
00084
00085
00086 std::vector<bool> emptyInv;
00087
00088
00089
00090 std::vector<bool> attractor;
00091
00092
00093
00094 std::vector<bool> wrongIndex;
00095
00096
00097
00098 std::vector<bool> skipped;
00099
00100 };
00101
00102
00103
00104 inline IndexCache::IndexCache (int nSets): emptyInv (nSets, false),
00105 attractor (nSets, false), wrongIndex (nSets, false),
00106 skipped (nSets, false)
00107 {
00108 return;
00109 }
00110
00111 inline IndexCache::~IndexCache ()
00112 {
00113 return;
00114 }
00115
00116 int IndexCache::read (const std::string &fileName,
00117 theMorseDecompositionType &morseDec)
00118 {
00119 using chomp::homology::ignoreline;
00120 using chomp::homology::ignorecomments;
00121 using chomp::homology::sbug;
00122
00123
00124 std::ifstream in (fileName. c_str ());
00125 if (!in)
00126 return -1;
00127 ignorecomments (in);
00128
00129
00130 int nSets = -1;
00131 in >> nSets;
00132 ignorecomments (in);
00133 if (nSets != morseDec. count ())
00134 {
00135 sbug << "Wrong number of Morse sets: " << nSets <<
00136 " instead of " << morseDec. count () << ".\n";
00137 return -1;
00138 }
00139
00140
00141 int number = -1;
00142 int empty = -1;
00143 int attr = -1;
00144 int wrong = -1;
00145 int skip = -1;
00146 int nCubes = -1;
00147 spcCube reprCube;
00148 theConleyIndexType ind;
00149 int countItems = 0;
00150
00151
00152 int countSets = 0;
00153 std::vector<bool> matched (nSets, false);
00154 bool found = false;
00155
00156
00157 while (1)
00158 {
00159
00160
00161 int character = in. get ();
00162 ignorecomments (in);
00163 if (countSets && (character == 'M'))
00164 character = 0x100;
00165
00166 switch (character)
00167 {
00168
00169 case EOF:
00170 case 0x100:
00171
00172 if (countItems < 7)
00173 {
00174 sbug << "Only " << countItems << " elements "
00175 "read, at least 7 expected.\n";
00176 return -1;
00177 }
00178
00179
00180
00181 found = false;
00182 for (int setNum = 0; setNum < nSets; ++ setNum)
00183 {
00184 if (matched [setNum])
00185 continue;
00186 if (morseDec [setNum]. size () != nCubes)
00187 continue;
00188 if (!morseDec [setNum]. check (reprCube))
00189 continue;
00190 matched [setNum] = true;
00191 emptyInv [setNum] = (empty == 1);
00192 attractor [setNum] = (attr == 1);
00193 wrongIndex [setNum] = (wrong == 1);
00194 skipped [setNum] = (skip == 1);
00195 morseDec. setindex (setNum, ind);
00196 found = true;
00197 break;
00198 }
00199
00200
00201 if (!found)
00202 {
00203 sbug << "Morse set no. " << number <<
00204 " could not be matched with any "
00205 "Morse set in the Morse dec.\n";
00206 return -1;
00207 }
00208
00209
00210 if (character == EOF)
00211 {
00212 if (countSets == nSets)
00213 return 0;
00214 sbug << "Cached information for " <<
00215 countSets << " Morse sets found, " <<
00216 nSets << " expected.\n";
00217 return -1;
00218 }
00219
00220
00221 number = -1;
00222 empty = -1;
00223 attr = -1;
00224 wrong = -1;
00225 skip = -1;
00226 nCubes = -1;
00227 countItems = 0;
00228
00229
00230
00231 case 'M':
00232 in >> number;
00233 ignorecomments (in);
00234 if (number != countSets)
00235 {
00236 sbug << "Wrong Morse set number: " <<
00237 number << " (" << countSets <<
00238 " expected).\n";
00239 return -1;
00240 }
00241 ++ countSets;
00242 ++ countItems;
00243 break;
00244
00245
00246 case 'E':
00247 in >> empty;
00248 ignorecomments (in);
00249 if ((empty != 0) && (empty != 1))
00250 {
00251 sbug << "The number 0 or 1 expected "
00252 "for 'emptyInv', " << empty <<
00253 " found.\n";
00254 return -1;
00255 }
00256 ++ countItems;
00257 break;
00258
00259
00260 case 'A':
00261 in >> attr;
00262 ignorecomments (in);
00263 if ((attr != 0) && (attr != 1))
00264 {
00265 sbug << "The number 0 or 1 expected "
00266 "for 'attractor', " << attr <<
00267 " found.\n";
00268 return -1;
00269 }
00270 ++ countItems;
00271 break;
00272
00273
00274 case 'W':
00275 in >> wrong;
00276 ignorecomments (in);
00277 if ((wrong != 0) && (wrong != 1))
00278 {
00279 sbug << "The number 0 or 1 expected "
00280 "for 'wrong index', " << wrong <<
00281 " found.\n";
00282 return -1;
00283 }
00284 ++ countItems;
00285 break;
00286
00287
00288 case 'S':
00289 in >> skip;
00290 ignorecomments (in);
00291 if ((skip != 0) && (skip != 1))
00292 {
00293 sbug << "The number 0 or 1 expected "
00294 "for 'skipped index', " << skip <<
00295 " found.\n";
00296 return -1;
00297 }
00298 ++ countItems;
00299 break;
00300
00301
00302 case 'N':
00303 in >> nCubes;
00304 ignorecomments (in);
00305 if (nCubes < 0)
00306 {
00307 sbug << "Negative number of cubes found.\n";
00308 return -1;
00309 }
00310 ++ countItems;
00311 break;
00312
00313
00314 case 'Q':
00315
00316 if (in. peek () != '(')
00317 {
00318 sbug << "An opening parenthesis of a cube "
00319 "not present.\n";
00320 return -1;
00321 }
00322
00323 in >> reprCube;
00324 ignorecomments (in);
00325 ++ countItems;
00326 break;
00327
00328
00329 case 'I':
00330 in >> ind;
00331 ignorecomments (in);
00332 ++ countItems;
00333 break;
00334
00335
00336 default:
00337 ignoreline (in);
00338 ignorecomments (in);
00339 break;
00340 }
00341 }
00342
00343 return 0;
00344 }
00345
00346 int IndexCache::write (const std::string &fileName,
00347 const theMorseDecompositionType &morseDec) const
00348 {
00349
00350 std::ofstream out (fileName. c_str ());
00351 if (!out)
00352 return -1;
00353 out << "; Cached information with Conley indices "
00354 "for a Morse decomposition.\n";
00355
00356
00357 int nSets = morseDec. count ();
00358 out << "; The number of Morse sets:\n";
00359 out << nSets << "\n";
00360
00361
00362 out << "; -----------------------------------------------\n";
00363 out << "; M - the consecutive number of a Morse set\n";
00364 out << "; E - 1 if the invariant set is empty, 0 if unknown\n";
00365 out << "; A - 1 if this is an attractor, 0 if not or unknown\n";
00366 out << "; W - 1 if the index is wrong (no isolation), 0 if good\n";
00367 out << "; S - 1 if the index computation was skipped, 0 otherwise\n";
00368 out << "; N - the number of cubes in the Morse set\n";
00369 out << "; Q - a representative cube\n";
00370 out << "; I - the Conley index encoded in numbers\n";
00371
00372
00373 for (int n = 0; n < nSets; ++ n)
00374 {
00375 out << "; -----------------------------------------------\n";
00376 out << "M " << n << "\n";
00377 out << "E " << (emptyInv [n] ? 1 : 0) << "\n";
00378 out << "A " << (attractor [n] ? 1 : 0) << "\n";
00379 out << "W " << (wrongIndex [n] ? 1 : 0) << "\n";
00380 out << "S " << (skipped [n] ? 1 : 0) << "\n";
00381 out << "N " << morseDec [n]. size () << "\n";
00382 out << "Q " << morseDec [n] [0] << "\n";
00383 out << "I " << morseDec. index (n) << "\n";
00384 }
00385
00386 return 0;
00387 }
00388
00389
00390 #endif // _CMGRAPHS_INDCACHE_H_
00391