File: ComputeTriangs.cc

package info (click to toggle)
topcom 1.2.0~beta%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 148,596 kB
  • sloc: cpp: 40,956; sh: 4,663; makefile: 679; ansic: 55
file content (443 lines) | stat: -rw-r--r-- 17,683 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
////////////////////////////////////////////////////////////////////////////////
// 
// ComputeTriangs.cc
//
//    produced: 25 Nov 1999 jr
// 
////////////////////////////////////////////////////////////////////////////////

#include "Signal.hh"
#include "CommandlineOptions.hh"

#include "Permutation.hh"
#include "Volumes.hh"
#include "Circuits.hh"
#include "Facets.hh"
#include "Admissibles.hh"
#include "InteriorFacets.hh"
#include "PartialTriang.hh"
#include "CheckTriang.hh"
#include "Flip.hh"
#include "MarkedFlips.hh"
#include "TriangNode.hh"
#include "TriangFlips.hh"
#include "FineTriang.hh"
#include "SymmetricFlipGraph.hh"
#include "SymmetricExtensionGraphMaster.hh"

#include "ComputeTriangs.hh"

namespace topcom {
  
  Message& ComputeTriangs::write_header(Message& msg) const {
    msg << std::endl;
    msg << "------------------------------------------------------------------\n";
    msg << " computing triangulations of a point configuration up to symmetry \n";
    msg << " TOPCOM client: " << CommandlineOptions::client() << '\n';
    msg << "------------------------------------------------------------------\n";
    msg << std::endl;
    return msg;
  }

  std::istream& ComputeTriangs::read_input(std::istream& ist) {
    if (input_chiro) {
      _chiroptr = new Chirotope();
      if (CommandlineOptions::output_asy()) {
	MessageStreams::forced() << "graphics output cancelled because of chirotope input." << std::endl;
      }
      if (!_chiroptr->read_string(ist)) {
	MessageStreams::forced() << "error while reading chirotope - exiting" << std::endl;
	exit(1);
      }
      MessageStreams::verbose() << "read chirotope with " << _chiroptr->no()
				<< " elements in rank " << _chiroptr->rank() << std::endl;
    }
    else {
      _pointsptr = new PointConfiguration();
      if (!_pointsptr->read(ist)) {
	MessageStreams::forced() << "error while reading point configuration - exiting" << std::endl;
	exit(1);
      }
      if (_pointsptr->rank() < _pointsptr->rowdim()) {
	MessageStreams::forced() << "point configuration has " << _pointsptr->rowdim() << " rows of rank " << _pointsptr->rank() << std::endl;
	_pointsptr->transform_to_full_rank();
	MessageStreams::forced() << "resulting no of rows after transformation: " << _pointsptr->rank() << std::endl;
	_pointsptr->pretty_print(MessageStreams::forced());
      }
      if ((_pointsptr->no() < 1) || (_pointsptr->rank() < 1)) {
	std::cerr << "no of points and rank must be at least one - exiting" << std::endl;
	exit(1);
      }
      if (_pointsptr->rank() > _pointsptr->no()) {
	std::cerr << "rank must not be larger than no of points - exiting" << std::endl;
	exit(1);
      }
      MessageStreams::verbose()  << "read point configuration with " << _pointsptr->no()
				 << " points in rank " << _pointsptr->rank() << std::endl;
      _chiroptr = new Chirotope(*_pointsptr, preprocess);
    }
    _no = _chiroptr->no();
    _rank = _chiroptr->rank();
    MessageStreams::verbose() << "checking for symmetries ..." << std::endl;
    _symmetriesptr = new SymmetryGroup(_no); // all configurations have a symmetry group
    if (!CommandlineOptions::ignore_symmetries()) {
      if (CommandlineOptions::use_switch_tables()
	  && !CommandlineOptions::require_point()
	  && (CommandlineOptions::required_point() < _no)
	  && (CommandlineOptions::required_point() >= 0)) {
	if (_symmetriesptr->read_generators(ist)) {
	  std::cerr << "read " << _symmetriesptr->generators().size()
		    << " generators" << std::endl;
	  if (CommandlineOptions::debug()) {
	    std::cerr << "_symmetriesptr->generators():" << std::endl;
	    std::cerr << _symmetriesptr->generators() << std::endl;
	  }
	}
      }
      else {
	if (_symmetriesptr->read(ist)) {
	  MessageStreams::verbose() << "read symmetry group with " << _symmetriesptr->generators().size()
				    << " generators of order " << _symmetriesptr->size() + 1 << std::endl;
	  MessageStreams::debug() << "symmetries:" << '\n'
				  << *_symmetriesptr << std::endl;
	  if (CommandlineOptions::require_point()
	      && (CommandlineOptions::required_point() < _no)
	      && (CommandlineOptions::required_point() >= 0)) {
	    
	    // if an existing point is required to be in any simplex, only point-stabilizing symmetries are compatible
	    // because otherwise we need that lex-minimal triangulations requiring a point need to remain
	    // lex-minimal in their orbits:
	    MessageStreams::verbose() << "reducing symmetry group to stabilizer of " << CommandlineOptions::required_point() << " ..." << std::endl;
	    SymmetryGroup compatible_symmetries(_symmetriesptr->stabilizer(IntegerSet(CommandlineOptions::required_point())));
	    *_symmetriesptr = compatible_symmetries;
	    MessageStreams::verbose() << "... order of compatible symmetries: " << _symmetriesptr->size() + 1 << std::endl;
	  }
	}
      }
    }
    else {
      
      // read symmetries anyway into dummy structure to be able to read seed later:
      SymmetryGroup dummy_symmetries(_no);
      dummy_symmetries.read(ist);
      MessageStreams::verbose() << "symmetry generators ignored." << std::endl;
    }
    MessageStreams::verbose() << "... done." << std::endl;
    MessageStreams::verbose() << "checking for seed triangulation ..." << std::endl;
    SimplicialComplex seed;
    if (!seed.read(ist) || seed.empty()) { // empty seed encodes no seed
      MessageStreams::verbose() << "... no non-empty seed given ..." << std::endl;
    }
    else {
      MessageStreams::verbose() << "... found seed " << seed << std::endl;
      _seedptr = new SimplicialComplex(seed); // only here, a seed is given in the input
    }
    MessageStreams::verbose() << "... done." << std::endl;
    MessageStreams::verbose() << "checking for required symmetries for triangulations ..." << std::endl;
    _required_symmetriesptr = new SymmetryGroup(_no); // required symmetries always exist (trivial group for non-symmetric configurations)
    if (CommandlineOptions::observe_required_symmetries()) {
      if (_required_symmetriesptr->read(ist)) {
	MessageStreams::verbose() << "read " << _required_symmetriesptr->generators().size() << " required symmetries beyond the identity" << std::endl;
      }
      else {
	MessageStreams::verbose() << "no valid required symmetry generators found." << std::endl;
      }
    }
    else {
      MessageStreams::verbose() << "required symmetry generators ignored." << std::endl;
    }
    MessageStreams::verbose() << "... done." << std::endl;
    return ist;
  }
  
  const std::pair<size_type, SimplicialComplex> ComputeTriangs::findmin_by_extension(const parameter_type      no,
										     const parameter_type      rank,
										     const PointConfiguration* pointsptr,
										     const Chirotope*          chiroptr,
										     const SymmetryGroup*      symmetriesptr,
										     const SymmetryGroup*      required_symmetriesptr,
										     const bool                output_triangs,
										     const bool                only_fine_triangs) {
    SymmetricExtensionGraphMaster segm(no,
				       rank,
				       pointsptr,
				       chiroptr,
				       symmetriesptr,
				       required_symmetriesptr,
				       output_triangs,
				       only_fine_triangs,
				       true);
    MessageStreams::verbose() << segm.nodecount() << " partial triangulations visited in total." << std::endl;
    MessageStreams::verbose() << segm.deadendcount() << " branching deadends." << std::endl;
    MessageStreams::verbose() << segm.earlydeadendcount() << " early detected deadends." << std::endl;
    MessageStreams::verbose() << segm.mintriang() << " is a minimal triangulation." <<  std::endl;
    MessageStreams::verbose() << segm.mincard() << " simplices in a minimal triangulation." << std::endl;
    return std::pair<size_type, SimplicialComplex>(segm.mincard(), segm.mintriang());
  }

  const size_type ComputeTriangs::enumerate_by_extension(const parameter_type      no,
							 const parameter_type      rank,
							 const PointConfiguration* pointsptr,
							 const Chirotope*          chiroptr,
							 const SymmetryGroup*      symmetriesptr,
							 const SymmetryGroup*      required_symmetriesptr,
							 const bool                output_triangs,
							 const bool                only_fine_triangs) {
    SymmetricExtensionGraphMaster segm(no,
				       rank,
				       pointsptr,
				       chiroptr,
				       symmetriesptr,
				       required_symmetriesptr,
				       output_triangs,
				       only_fine_triangs,
				       false);
    if (Signal::signal_received()) {
      MessageStreams::verbose().print_dumpseparator();
      MessageStreams::verbose() << "### intermediate results at checkpoint forced by signal:" << std::endl;
      MessageStreams::verbose().print_dumpseparator();
    }
    MessageStreams::verbose() << segm.nodecount() << " partial triangulations visited in total." << std::endl;
    MessageStreams::verbose() << segm.deadendcount() << " branching deadends." << std::endl;
    MessageStreams::verbose() << segm.earlydeadendcount() << " early detected deadends." << std::endl;
    MessageStreams::verbose() << segm.maxiter_coversimptighten() << " max iterations in coversimp tightening." << std::endl;
    MessageStreams::verbose() << segm.symcount() << " symmetry classes";
    if (!CommandlineOptions::skip_orbitcount()) {
      MessageStreams::verbose() << " --- " << segm.totalcount() << " triangulations in total";
    }
    MessageStreams::verbose() << "." << std::endl;
    return segm.symcount();
  }

  const size_type ComputeTriangs::enumerate_by_flips(const parameter_type        no,
						     const parameter_type        rank,
						     const PointConfiguration*   pointsptr,
						     const Chirotope*            chiroptr,
						     const SymmetryGroup*        symmetriesptr,
						     const SymmetryGroup*        required_symmetriesptr,
						     const SimplicialComplex*    seedptr,
						     const symmetryptr_datapair* seed_symmetryptrs,
						     const Volumes*              voltableptr,
						     const bool                  output_triangs,
						     const bool                  fine_only) {
    SymmetricFlipGraph sfg(no,
			   rank,
			   pointsptr,
			   chiroptr, 
			   symmetriesptr,
			   required_symmetriesptr,
			   seedptr,
			   seed_symmetryptrs,
			   voltableptr,
			   output_triangs,
			   fine_only);
    MessageStreams::verbose() << sfg.symcount() << " symmetry classes";
    if (!CommandlineOptions::skip_orbitcount()) {
      MessageStreams::verbose() << " --- " << sfg.totalcount() << " triangulations in total";
    }
    MessageStreams::verbose() << "." << std::endl;
    return sfg.symcount();
  }

  int ComputeTriangs::run() {

    // // write TOPCOM header with version information:
    // global::write_topcom_header(std::cerr);

    // write header for computations concerning triangulations:
    write_header(MessageStreams::forced());

    // terminate if non-consistent options have been chosen:
    if (input_chiro && CommandlineOptions::check_regular()) {
      MessageStreams::forced() << "regularity check not possible if only chirotope is given - exiting." << std::endl;
      exit(1);
    }
    if (input_chiro && CommandlineOptions::use_volumes()) {
      MessageStreams::forced() << "volumes not exploitable if only chirotope is given - exiting." << std::endl;
      exit(1);
    }

    // read input from stdin or file (depending on commandline options):
    read_input(std::cin);
    
    // compute according to flags:
    if (compute_all) {
      if (findmin) {
	const std::pair<size_type, SimplicialComplex> mintriang_pair = findmin_by_extension(_no,
											    _rank,
											    _pointsptr,
											    _chiroptr,
											    _symmetriesptr,
											    _required_symmetriesptr,
											    output_triangs,
											    fine_only);
	if (CommandlineOptions::verbose()) {
	  MessageStreams::verbose() << "... done." << std::endl;
	}
	if (mintriang_pair.second.empty()) {
	  MessageStreams::result() << "[-, -]" << std::endl;
	}
	else {
	  MessageStreams::result() << mintriang_pair << std::endl;
	}
	return Signal::exit_value();
      }
      else {
	const size_type N = enumerate_by_extension(_no,
						   _rank,
						   _pointsptr,
						   _chiroptr,
						   _symmetriesptr,
						   _required_symmetriesptr,
						   output_triangs,
						   fine_only);
	if (CommandlineOptions::verbose()) {
	  MessageStreams::verbose() << N << " symmetry classes of triangulations in total." << std::endl;
	  MessageStreams::verbose() << "... done." << std::endl;
	}
	if (!output_triangs) {
	  MessageStreams::result() << N << std::endl;
	}
	return Signal::exit_value();
      }
    }
    else {
      if (CommandlineOptions::use_gkz()
	  && CommandlineOptions::symmetries_are_isometric()
	  && _pointsptr) {
	if (CommandlineOptions::verbose()) {
	  MessageStreams::verbose() << "computing volumes table ..." << std::endl;
	}
	_voltableptr = new Volumes(*_pointsptr);
	if (CommandlineOptions::verbose()) {
	  MessageStreams::verbose() << "... done." << std::endl;
	}
      }
      
      if (CommandlineOptions::memopt()) {
	if (CommandlineOptions::verbose()) {
	  MessageStreams::verbose() << "no preprocessing of simplex table to save memory" << std::endl;
	}
      }
      else {
	const parameter_type pprank = std::min<parameter_type>(_rank + 1, _no);
	if (CommandlineOptions::verbose()) {
	  MessageStreams::verbose() << "preprocessing simplex table for all simplices up to rank "
				    << pprank << " ..." << std::endl;
	}
	if (_voltableptr) {
	  SimplicialComplex::preprocess_index_table(_no, 0, pprank, *_voltableptr, true);
	}
	else {
	  SimplicialComplex::preprocess_index_table(_no, 0, pprank, *_chiroptr, true);
	}
	if (CommandlineOptions::verbose()) {
	  MessageStreams::verbose() << "... done: "
				    << SimplicialComplex::no_of_simplices(_rank)
				    << " full-dimensional simplices in rank " << _rank << std::endl;
	}
      }
      
      // for a flipping algorithm we need a seed anyway:
      if (!_seedptr) {
	if (CommandlineOptions::verbose()) {
	  MessageStreams::verbose() << "no valid seed triangulation found" << std::endl;
	  MessageStreams::verbose() << "computing seed triangulation via placing and pushing ..." 
				    << std::endl;
	}
	if (fine_only) {
	  _seedptr = new FineTriang(*_chiroptr);
	}
	else {
	  _seedptr = new PlacingTriang(*_chiroptr);
	}
      }
      if (CommandlineOptions::verbose()) {
	MessageStreams::verbose() << "seed: " << *_seedptr << std::endl;
	MessageStreams::verbose() << "containing " << _seedptr->card() << " simplices" << std::endl;
	MessageStreams::verbose() << "using the following " << _seedptr->support().card() << " vertices: " 
				  << _seedptr->support() << std::endl;
	MessageStreams::verbose() << "... done." << std::endl;
      }
      
      if (CommandlineOptions::verbose()) {
	MessageStreams::verbose() << "computing symmetries of seed ..." << std::endl;
      }
      if (!_symmetriesptr->complete()) {
	_symmetriesptr->closure();
      }
      _seed_symmetryptrsptr = new symmetryptr_datapair(_symmetriesptr->stabilizer_ptrs(*_seedptr));
      if (CommandlineOptions::verbose()) {
	MessageStreams::verbose() << "... done." << std::endl;
	MessageStreams::verbose() << _seed_symmetryptrsptr->first.size() << " symmetries in total in seed." << std::endl;
	if (CommandlineOptions::debug()) {
	  MessageStreams::debug() << "symmetries:" << '\n'
				  << *_symmetriesptr << std::endl;
	}
	if (CommandlineOptions::verbose()) {
	  MessageStreams::verbose() << "... done." << std::endl;;
	}
      }
      if (CommandlineOptions::check()) {
	if (CommandlineOptions::verbose()) {
	  MessageStreams::verbose() << "checking seed triangulation ..." << std::endl;
	}
	if (!(CheckTriang(*_seedptr, *_seed_symmetryptrsptr, *_chiroptr, *_symmetriesptr, *_required_symmetriesptr, fine_only))()) {
	  if (CommandlineOptions::verbose()) {
	    MessageStreams::verbose() << "seed triangulation " << std::endl
				      << *_seedptr << std::endl
				      << "not valid." << std::endl;
	  }
	  return 1;
	}
	if (CommandlineOptions::verbose()) {
	  MessageStreams::verbose() << "... done." << std::endl;;
	}
      }
      
      if (findflips) {
	if (CommandlineOptions::verbose()) {
	  MessageStreams::verbose() << "count all flips of seed ..." << std::endl;
	}
	const TriangNode tn(0, _no, _rank, *_seedptr);
	const TriangFlips tf(*_chiroptr, nullptr, tn, *_seed_symmetryptrsptr, fine_only);
	if (CommandlineOptions::verbose()) {
	  MessageStreams::verbose() << tf.flips().size() << " flips in total." << std::endl;
	  MessageStreams::verbose() << "... done." << std::endl;
	}
	if (output_triangs) {
	  MessageStreams::result() << tf << std::endl;
	}
	else {
	  MessageStreams::result() << tf.flips().size() << std::endl;
	}
	return Signal::exit_value();
      }
      if (CommandlineOptions::verbose()) {
	MessageStreams::verbose() << "exploring all seed-connected symmetry classes of triangulations by flipping ..." << std::endl;
      }
      const size_type N = enumerate_by_flips(_no,
					     _rank,
					     _pointsptr,
					     _chiroptr, 
					     _symmetriesptr,
					     _required_symmetriesptr,
					     _seedptr,
					     _seed_symmetryptrsptr,
					     _voltableptr,
					     output_triangs,
					     fine_only);
      if (CommandlineOptions::verbose()) {
	MessageStreams::verbose() << N << " symmetry classes of triangulations in total." << std::endl;
	MessageStreams::verbose() << "... done." << std::endl;
      }
      if (!output_triangs) {
	MessageStreams::result() << N << std::endl;
      }
      return Signal::exit_value();
    }
  }

}; // namespace topcom

// eof ComputeTriangs.cc