The Conley-Morse Graphs Software
compmap.cpp
Go to the documentation of this file.
1/////////////////////////////////////////////////////////////////////////////
2///
3/// @file compmap.cpp
4///
5/// Computing the map on a given set of cubes at the final subdivision depth.
6/// This program may be useful for the computation of the index pairs
7/// and the index map for combinatorial Morse sets saved previously.
8///
9/// @author Pawel Pilarczyk
10///
11/////////////////////////////////////////////////////////////////////////////
12
13// Copyright (C) 1997-2014 by Pawel Pilarczyk.
14//
15// This file is part of my research software package. This is free software:
16// you can redistribute it and/or modify it under the terms of the GNU
17// General Public License as published by the Free Software Foundation,
18// either version 3 of the License, or (at your option) any later version.
19//
20// This software is distributed in the hope that it will be useful,
21// but WITHOUT ANY WARRANTY; without even the implied warranty of
22// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23// GNU General Public License for more details.
24//
25// You should have received a copy of the GNU General Public License
26// along with this software; see the file "license.txt". If not,
27// please, see <https://www.gnu.org/licenses/>.
28
29// Started on February 26, 2008. Last revision: March 19, 2013.
30
31
32// include some standard C++ header files
33#include <exception>
34#include <sstream>
35#include <iostream>
36#include <fstream>
37#include <algorithm>
38#include <new>
39
40// include selected header files from the CHomP library
41#include "chomp/system/config.h"
42#include "chomp/system/textfile.h"
43#include "chomp/system/timeused.h"
44#include "chomp/system/arg.h"
45
46// include local header files
47#include "config.h"
48#include "confinfo.h"
49#include "typedefs.h"
50#include "typedyns.h"
51#include "utils.h"
52#include "mapopt.h"
53
54
55// --------------------------------------------------
56// -------------------- OVERTURE --------------------
57// --------------------------------------------------
58
59/// The title of the program which is displayed every time
60/// the program is launcued.
61const char *title = "\
62This is a program which computes a combinatorial cubical multivalued map.\n\
63Ver. 0.01, March 26, 2013. Copyright (C) 2005-2012 by Pawel Pilarczyk.\n\
64This is free software. No warranty. Consult 'license.txt' for details.";
65
66/// The help information about the program which is displayed
67/// if the program is launched without command-line arguments
68/// or with incorrect arguments.
69const char *helpinfo = "\
70This is the front-end program for the computation of a combinatorial\n\
71cubical multivalued map on a cubical set at the final subdivision depth\n\
72for a single parameter box. It is a supplementary program to 'cmgraphs',\n\
73and may be used for the computation of the index pairs and the index map\n\
74for combinatorial Morse sets saved previously.\n\
75Command line arguments (the argument '-b' is mandatory):\n\
76-b x1,x2,...,xN - the integer coordinates of a parameter box to use,\n\
77file.cub - a file with the set of cubes on which to compute the map,\n\
78file.map - a file to which the computed map will be saved,\n\
79-d disjoint.cub - a set of cubes that will be removed from the images,\n\
80-t filename - file name with optimization information (read only),\n\
81--log filename - file name to save all the text output of the program to,\n\
82--quiet - suppress any output written to the terminal,\n\
83--debug - display additional information useful for debugging,\n\
84--help - make the program show this brief help information and exit.\n\
85For more information ask the author at http://www.pawelpilarczyk.com/.";
86
87
88// --------------------------------------------------
89// ---------------- Map Computation -----------------
90// --------------------------------------------------
91
92int runMapComp (const char *boxCoordinates, const char *mapOptFileName,
93 const char *cubName, const char *mapName, const char *disjointName)
94{
95 using chomp::homology::sout;
96 using chomp::homology::scon;
97 using chomp::homology::sbug;
98 using chomp::homology::ignorecomments;
99 using chomp::homology::ignoreline;
100 using chomp::homology::fileerror;
101
102 // show the global configuration information
103 std::ostringstream info;
104 showConfigInfo (info, 0, 0);
105 sout << "\n" << info. str () << "\n";
106
107 // reset the variables for computing max image diameter and volume
110
111 // decode the parameter box
112 parCube paramBox;
113 std::string boxCoordStr ("(" + std::string (boxCoordinates) + ")");
114 std::istringstream boxCoordStream (boxCoordStr);
115 boxCoordStream >> paramBox;
116
117 // prepare the offset and the width of the phase space region
118 double offset [spaceDim];
119 for (int i = 0; i < spaceDim; ++ i)
120 offset [i] = spaceOffset [i];
121 double width [spaceDim];
122 for (int i = 0; i < spaceDim; ++ i)
123 width [i] = spaceWidth [i];
124
125 // set up an array of coordinates of the box
126 parCoord curCoord [paramDim];
127 paramBox. coord (curCoord);
128
129 // prepare the actual parameters for the map
130 sout << "Parameter box: " << paramBox << ".\n";
131 double leftMapParam [paramCount];
132 double rightMapParam [paramCount];
133 computeParam (curCoord, leftMapParam, rightMapParam);
134 sout << "Parameter values:";
135 for (int i = 0; i < paramCount; ++ i)
136 {
137 sout << " [" << leftMapParam [i] << "," <<
138 rightMapParam [i] << "]";
139 }
140 sout << "\n";
141
142 // create the map object and set its parameters
143 theMapType theMap;
144 theMap. setParam (leftMapParam, rightMapParam, paramCount);
145
146 // read map optimization data and apply it to the map
147 MapOptimization mapOptimization;
148 if (mapOptFileName && *mapOptFileName)
149 {
150 std::ifstream in (mapOptFileName);
151 if (in)
152 {
153 sout << "Reading map optimization data... ";
154 mapOptimization. loadData (in);
155 sout << " Done.\n";
156
157 sout << "Optimizing the map... ";
158 std::vector<std::string> data;
159 mapOptimization. prepareData (data,
160 leftMapParam, rightMapParam, paramCount);
161 sout << data. size () << " hints... ";
162 for (std::vector<std::string>::const_iterator it =
163 data. begin (); it != data. end (); ++ it)
164 {
165 theMap. useOptInfo (*it);
166 }
167 sout << "Done.\n";
168 }
169 }
170
171 // prepare an object of the cubical map
172 // at the finest subdivision depth to be used in the computation
173 theCubMapType theCubMap (offset, width, 1 << finalDepth,
174 finalDepth, theMap);
175 theCubMap. cache = false;
176 theCubMap. cropping = false;
177
178 // read the set of cubes with which the images will be made disjoint
180 if (disjointName && *disjointName)
181 {
182 sout << "Reading '" << disjointName << "'... ";
183 std::ifstream disjointFile (disjointName);
184 if (!disjointFile)
185 fileerror (disjointName, "open");
186 disjointFile >> disjoint;
187 sout << disjoint. size () << " cubes read.\n";
188 }
189 bool usingDisjoint = !disjoint. empty ();
190
191 // open the file containing the set of cubes
192 std::ifstream cubFile (cubName);
193 if (!cubFile)
194 fileerror (cubName, "open");
195
196 // ignore the line "dimension N" if it is there
197 ignorecomments (cubFile);
198 if (cubFile. peek () == 'd')
199 {
200 ignoreline (cubFile);
201 ignorecomments (cubFile);
202 }
203
204 // create the file for saving the map
205 std::ofstream mapFile (mapName);
206 if (!mapFile)
207 fileerror (mapName, "create");
208
209 // compute the map on all the cubes and save it to the file
210 sout << "Computing the map... ";
211 int_t allImages = 0;
212 int_t removedCubes = 0;
213 int_t emptyImages = 0;
214 int_t cur = 0;
215 while (1)
216 {
217 // read a subsequent cube from the file unless EOF
218 bool cubeRead = false;
219 spcCube q;
220 if (!cubFile. eof ())
221 {
222 cubFile >> q;
223 ignorecomments (cubFile);
224 cubeRead = true;
225 ++ cur;
226 }
227
228 // prepare data for a computed image of a cube
229 spcCubes img;
230 bool imgComputed = false;
231
232 // compute the image of the cube
233 if (cubeRead)
234 {
235 theCubMap (q, &img, 0, 0, 0, false);
236 imgComputed = true;
237 }
238
239 // if the image of a cube has been computed
240 if (imgComputed)
241 {
242 // save the map assignment to the output file
243 mapFile << q << " -> {";
244 int_t imgSize = img. size ();
245 allImages += imgSize;
246 bool first = true;
247 for (int_t i = 0; i < imgSize; ++ i)
248 {
249 // if the cube is in the set with which
250 // the image should be disjoint
251 if (usingDisjoint &&
252 disjoint. check (img [i]))
253 {
254 ++ removedCubes;
255 -- allImages;
256 continue;
257 }
258
259 // write the cube; separate cubes with spaces
260 if (first)
261 first = false;
262 else
263 mapFile << ' ';
264 mapFile << img [i];
265 }
266 mapFile << "}\n";
267
268 // if there were no cubes written, take a note of it
269 if (first)
270 ++ emptyImages;
271
272 // show progress indicator if necessary
273 if (cur && !(cur % 1000))
274 {
275 scon << std::setw (8) << (cur / 1000) <<
276 "k\b\b\b\b\b\b\b\b\b";
277 }
278 }
279
280 // if there are no more cubes that must be read
281 // and no more images that must be computed then quit
282 if (cubFile. eof ())
283 break;
284 }
285
286 // show the number of processed cubes
287 sout << "Images of " << cur << " cubes computed.\n";
288 sout << "There are " << allImages << " cubes in total "
289 "in all the images.\n";
290 if (usingDisjoint)
291 {
292 sout << removedCubes << " cubes were removed "
293 "from the images.\n";
294 }
295 if (emptyImages)
296 {
297 sout << "WARNING: There are " << emptyImages <<
298 " empty images.\n";
299 }
300 else
301 {
302 sout << "All the images are nonempty.\n";
303 }
304
305 // close the files
306 cubFile. close ();
307 mapFile. close ();
308
309 // show the average image size
310 if (cur > 0)
311 sout << "Average image size: " << (allImages / cur) << ".\n";
312
313 // show computed max image diameter and volume
314 sout << "Max img diam = " << theCubMapType::maxImgDiam <<
315 ", max img vol = " << theCubMapType::maxImgVol << ".\n";
316
317 // release pointset data to make fewer valgrind complaints
318 sbug << "(Releasing the pointset data.)\n";
319 parCube::PointBase::reset ();
320 spcCube::PointBase::reset ();
321 chomp::homology::Cube::PointBase::reset ();
322
323 return 0;
324} /* runMapComp */
325
326
327// --------------------------------------------------
328// ---------------------- MAIN ----------------------
329// --------------------------------------------------
330
331/// The main procedure of the program.
332/// Returns: 0 = Ok, -1 = Error, 1 = Help displayed, 2 = Wrong arguments.
333int main (int argc, char *argv [])
334{
335 using namespace chomp::homology;
336
337 // prepare user-configurable data
338 char *boxCoordinates = 0;
339 char *cubName = 0;
340 char *mapName = 0;
341 char *disjointName = 0;
342 char *mapOptFileName = 0;
343 const int maxComments = 16;
344 int nComments = 0;
345 char *comments [maxComments];
346
347 // interprete the command-line arguments
348 arguments a;
349 arg (a, NULL, cubName);
350 arg (a, NULL, mapName);
351 arg (a, "b", boxCoordinates);
352 arg (a, "t", mapOptFileName);
353 arg (a, "d", disjointName);
354 arg (a, "-comment", comments, nComments, maxComments);
355 arghelp (a);
356
357 argstreamprepare (a);
358 int argresult = a. analyze (argc, argv);
359 argstreamset ();
360
361 // if no box coordinates defined then show the help message
362 if (!boxCoordinates || !cubName || !mapName)
363 argresult = 1;
364
365 // show the program's main title
366 if (argresult >= 0)
367 sout << title << '\n';
368
369 // if something was incorrect, show an additional message and exit
370 if (argresult < 0)
371 {
372 sout << "Call with '--help' for help.\n";
373 return 2;
374 }
375
376 // if help requested, show help information
377 if (argresult > 0)
378 {
379 sout << helpinfo << '\n';
380 return 1;
381 }
382
383 // try running the main function and catch an error message if thrown
384 try
385 {
386 // set an appropriate program time message
387 program_time = "Aborted after:";
388 program_time = 1;
389
390 // run the computations for a single parameter box
391 runMapComp (boxCoordinates, mapOptFileName,
392 cubName, mapName, disjointName);
393
394 // set an appropriate program time message
395 program_time = "Total time used:";
396
397 // finalize
398 return 0;
399 }
400 catch (const char *msg)
401 {
402 sout << "ERROR: " << msg << '\n';
403 return -1;
404 }
405 catch (const std::exception &e)
406 {
407 sout << "ERROR: " << e. what () << '\n';
408 return -1;
409 }
410 catch (...)
411 {
412 sout << "ABORT: An unknown error occurred.\n";
413 return -1;
414 }
415} /* main */
416
A generic map computation routine that computes a rigorous cubical multivalued map based on a functio...
Definition: mapcomp.h:69
static int maxImgDiam
The maximal image diameter encountered so far.
Definition: mapcomp.h:117
static int maxImgVol
The maximal image volume encountered so far.
Definition: mapcomp.h:121
This class defines a map derived from a sample difference equation.
Definition: m_differ.h:47
A class whose objects are responsible for adjusting the integration parameters such as step size and ...
Definition: mapopt.h:59
int main(int argc, char *argv[])
The main procedure of the program.
Definition: compmap.cpp:333
int runMapComp(const char *boxCoordinates, const char *mapOptFileName, const char *cubName, const char *mapName, const char *disjointName)
Definition: compmap.cpp:92
const char * helpinfo
The help information about the program which is displayed if the program is launched without command-...
Definition: compmap.cpp:69
const char * title
The title of the program which is displayed every time the program is launcued.
Definition: compmap.cpp:61
Choice of configuration settings.
Writing configuration settings to an output stream.
void showConfigInfo(std::ostream &f, const char *prefix, const parCoord *maxIter)
Writes the configuration information to the given output stream.
Definition: confinfo.h:53
bool disjoint(const pointset &p, const pointset &q)
Definition: psetjoin.cpp:139
Map optimization data saving, retrieving, and sending.
const SpaceOffsetType spaceOffset
An imitation of an array which returns the offset of the rectangular area in the phase space which co...
Definition: p_differ.h:108
const int paramDim
The dimension of the parameter space to iterate.
Definition: p_differ.h:67
const int spaceDim
The dimension of the phase space.
Definition: p_differ.h:48
const int paramCount
The number of all the parameters, both varying and fixed.
Definition: p_differ.h:82
const SpaceWidthType spaceWidth
An imitation of an array which returns the width of the rectangular area in the phase space which con...
Definition: p_differ.h:128
const int finalDepth
The final depth of subdivisions in the phase space.
Definition: p_differ.h:58
Customizable data types for the Conley-Morse graphs computation program.
Data types for the dynamical systems data structures.
chomp::homology::tCubeFix< paramDim, parCoord > parCube
The type of a cube in the set of parameters.
Definition: typeparam.h:58
short int parCoord
The type of coordinates of cubes in the set of parameters.
Definition: typeparam.h:53
chomp::homology::hashedset< spcCube > spcCubes
The type of a set of cubes in the phase space.
Definition: typespace.h:58
chomp::homology::tCubeBase< spcCoord > spcCube
The type of a cube in the phase space.
Definition: typespace.h:55
Utilites and helper functions.
void computeParam(const intType *curCoord, double *leftMapParam, double *rightMapParam)
Computes the real coordinates of the parameter cube which corresponds to the given box.
Definition: utils.h:160