-
Notifications
You must be signed in to change notification settings - Fork 1
/
cfp.cpp
776 lines (688 loc) · 26.7 KB
/
cfp.cpp
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
/// @file cfp.cpp
/// Driver program for the CrystalFp library.
///
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <climits>
#include <algorithm>
#include <vector>
#include "CmdLine.h"
#include "CrystalFp.h"
#include "ReadPoscar.h"
#include "AtomSymbols.h"
#include "CrystalFpAnalysis.h"
#include "CrystalFpScatterplot.h"
#ifdef _OPENMP
#include <omp.h>
#endif
#ifdef _MSC_VER
#define strncasecmp _strnicmp
#else
#include <strings.h>
#endif
using namespace cfp;
/// Main program for CrystalFp.
///
/// @author Mario Valle - Swiss National Supercomputing Centre (CSCS)
/// @date 2011-02-09 (initial version)
/// @version 1.0
///
/// @param[in] ac Number of command line parameters
/// @param[in] av Command line parameters
///
int main(int ac, char **av)
{
try
{
// Parse the command line
CmdLine cmd(ac, av, true);
// Write out command line parameters
if(cmd.mVerboseLevel > 0)
{
std::cerr << std::endl;
std::cerr << "Poscar file: " << cmd.mPoscarFile << std::endl;
if(cmd.mEnergyFile) std::cerr << "Energy file: " << cmd.mEnergyFile << std::endl;
if(cmd.mVerboseLevel > 0) std::cerr << "Verbose level: " << cmd.mVerboseLevel << std::endl;
if(!cmd.mAtomZ.empty())
{
std::cerr << "Elements: ";
std::vector<unsigned int>::const_iterator iaz;
for(iaz=cmd.mAtomZ.begin(); iaz != cmd.mAtomZ.end(); ++iaz) std::cerr << elementZToSymbol(*iaz) << ' ';
std::cerr << std::endl;
}
if(cmd.mStartStep > 1) std::cerr << "Start step: " << cmd.mStartStep << std::endl;
if(cmd.mEndStep > 0) std::cerr << "End step: " << cmd.mEndStep << std::endl;
if(cmd.mCutoffDistance > 0) std::cerr << "Cutoff at: " << cmd.mCutoffDistance << std::endl;
std::cerr << "Energy: ";
if(cmd.mEnergyIsPerAtom) std::cerr << "per atom" << std::endl;
else std::cerr << "per structure" << std::endl;
std::cerr << "Structures: ";
if(cmd.mIsNanocluster) std::cerr << "nanoclusters" << std::endl;
else std::cerr << "crystals" << std::endl;
std::cerr << "Bin size: " << cmd.mDiffrBinSize << std::endl;
std::cerr << "Peak size: " << cmd.mDiffrPeakSize << std::endl;
if(cmd.mForcedFpLen > 0) std::cerr << "Forced fp len: " << cmd.mForcedFpLen << std::endl;
if(cmd.mCheckpointDir) std::cerr << "Checkpt dir: " << cmd.mCheckpointDir << std::endl;
if(cmd.mOverwriteChkptDir && cmd.mCheckpointDir) std::cerr << "Prev checkpt: overwrite" << std::endl;
#ifdef _OPENMP
std::cerr << "Max threads: " << omp_get_max_threads() << std::endl;
#endif
std::cerr << std::endl;
}
// Construct the classifier object
CrystalFp cfp(cmd.mVerboseLevel);
// List methods if requested
if(cmd.mListFingerprintingMethods || cmd.mListDistanceMethods || cmd.mListGroupingMethods || cmd.mListAnalysisMethods)
{
if(cmd.mListFingerprintingMethods)
{
std::cerr << std::endl << "Available fingerprinting methods:" << std::endl;
std::vector<std::string> fnames = cfp.getFingerprintMethodsNames();
std::vector<std::string>::const_iterator ifn;
int n=0;
for(ifn=fnames.begin(); ifn != fnames.end(); ++ifn, ++n) std::cerr << " " << n << " " << *ifn << std::endl;
}
if(cmd.mListDistanceMethods)
{
std::cerr << std::endl << "Available distance measures:" << std::endl;
std::vector<std::string> fnames = cfp.getDistanceMethodsNames();
std::vector<std::string>::const_iterator ifn;
int n=0;
for(ifn=fnames.begin(); ifn != fnames.end(); ++ifn, ++n) std::cerr << " " << n << " " << *ifn << std::endl;
}
if(cmd.mListGroupingMethods)
{
std::cerr << std::endl << "Available grouping methods:" << std::endl;
std::vector<std::string> fnames = cfp.getGroupingMethodsNames();
std::vector<std::string>::const_iterator ifn;
int n=0;
for(ifn=fnames.begin(); ifn != fnames.end(); ++ifn, ++n) std::cerr << " " << n << " " << *ifn << std::endl;
}
if(cmd.mListAnalysisMethods)
{
// Start the analysis method
CrystalFpAnalysis analysis(&cfp);
std::vector<std::string>::const_iterator ifn;
int n=0;
std::cerr << "\nSimple analysis methods (method_x,method_y)" << std::endl;
const std::vector<std::string> n1 = analysis.getAnalysisMethodsNames(CrystalFpAnalysis::CATEGORY_SIMPLE);
for(ifn=n1.begin(); ifn != n1.end(); ++ifn, ++n) std::cerr << " " << n << " " << *ifn << std::endl;
std::cerr << "\nHistogram analysis methods (method)" << std::endl;
const std::vector<std::string> n2 = analysis.getAnalysisMethodsNames(CrystalFpAnalysis::CATEGORY_HIST);
for(ifn=n2.begin(); ifn != n2.end(); ++ifn, ++n) std::cerr << " " << n << " " << *ifn << std::endl;
std::cerr << "\nSpecial analysis methods (method)" << std::endl;
const std::vector<std::string> n3 = analysis.getAnalysisMethodsNames(CrystalFpAnalysis::CATEGORY_SPECIAL);
for(ifn=n3.begin(); ifn != n3.end(); ++ifn, ++n) std::cerr << " " << n << " " << *ifn << std::endl;
std::cerr << "\nAll per structure analysis methods (method)" << std::endl;
const std::vector<std::string> n4 = analysis.getAnalysisMethodsNames(CrystalFpAnalysis::CATEGORY_ALL);
for(ifn=n4.begin(); ifn != n4.end(); ++ifn, ++n) std::cerr << " " << n << " " << *ifn << std::endl;
}
std::cerr << std::endl;
return 0;
}
// Check and set methods
if(cmd.mFingerprintingMethod != CmdLine::NO_METHOD_SELECTED) cfp.setFingerprintMethod(cmd.mFingerprintingMethod);
if(cmd.mDistanceMethod != CmdLine::NO_METHOD_SELECTED) cfp.setDistanceMethod(cmd.mDistanceMethod);
if(cmd.mGroupingMethod != CmdLine::NO_METHOD_SELECTED) cfp.setGroupingMethod(cmd.mGroupingMethod);
// Read requested steps
if(cmd.mVerboseLevel >= 1) std::cerr << "Start file loading" << std::endl;
readPoscarAndEnergies(cmd.mPoscarFile, cmd.mEnergyFile, cmd.mEnergyIsPerAtom, cmd.mStartStep, cmd.mEndStep, cmd.mAtomZ, cfp);
// Select structures based on energies
if(cfp.hasEnergies())
{
if(cmd.mVerboseLevel >= 2) std::cerr << "Min energy: " << cfp.getMinEnergy() << std::endl;
// Select based on energies
if(cmd.mHasEnergyThreshold)
{
if(cmd.mVerboseLevel >= 2) std::cerr << "Threshold energy: " << cmd.mEnergyThreshold << std::endl;
cfp.energyThreshold(cmd.mEnergyThreshold);
}
else if(cmd.mHasReverseEnergyThreshold)
{
cmd.mEnergyThreshold += cfp.getMinEnergy();
if(cmd.mVerboseLevel >= 2) std::cerr << "Threshold energy: " << cmd.mEnergyThreshold << std::endl;
cfp.energyThreshold(cmd.mEnergyThreshold);
}
}
// Show the number of loaded structures
if(cmd.mVerboseLevel >= 1) std::cerr << "Loaded " << cfp.getNumActiveStructures() << " of " << cfp.getNumTotalStructures() << std::endl;
// Compute the cutoff distance to use and set it
if(cmd.mCutoffDistance > 0)
{
float computed_cutoff_distance = cfp.computeCutoffDistance();
if(cmd.mVerboseLevel >= 2) std::cerr << "Forced cutoff distance: " << std::fixed << std::setprecision(4) << cmd.mCutoffDistance
<< " (computed: " << std::fixed << std::setprecision(4) << computed_cutoff_distance << ')' << std::endl;
}
else
{
cmd.mCutoffDistance = cfp.computeCutoffDistance();
if(cmd.mVerboseLevel >= 2) std::cerr << "Computed cutoff distance: " << std::fixed << std::setprecision(4) << cmd.mCutoffDistance << std::endl;
}
// Force the new dimensionality for the fingerprints (should be called before loadCheckpoint())
cfp.forceFpLength(cmd.mForcedFpLen);
// Set checkpointing
cfp.setCheckpointDir(cmd.mCheckpointDir);
// Load previous checkpoint if needed
if(cmd.mCheckpointDir && !cmd.mOverwriteChkptDir)
{
if(cmd.mVerboseLevel >= 1) std::cerr << "Reload checkpoint" << std::endl;
cfp.loadCheckpoint();
}
// Compute fingerprints
if(cmd.mFingerprintingMethod != CmdLine::NO_METHOD_SELECTED)
{
if(cmd.mVerboseLevel >= 1)
{
std::cerr << std::endl << "Fingerprint computation using method: " << cfp.getFingerprintMethod();
if(cfp.isDiffractionLike()) std::cerr << " (diffraction like)";
std::cerr << std::endl;
}
// Set the kind of structure under analysis
if(cmd.mIsNanocluster) cfp.setNanoclusterStructureType();
// Set fingerprinting parameters
cfp.setCutoffDistance(cmd.mCutoffDistance);
cfp.setDiffrBinSize(cmd.mDiffrBinSize);
cfp.setDiffrPeakSize(cmd.mDiffrPeakSize);
// Compute fingerprints (not already loaded from the checkpoint dir)
cfp.computeFingerprints();
if(cmd.mVerboseLevel >= 1)
{
std::cerr << "Done" << std::endl;
if(!cfp.hasFingerprints()) std::cerr << "Some fingerprints missing!" << std::endl;
std::cerr << "Dimensionality: " << cfp.getFingerprintNumSections() * cfp.getFingerprintSectionLen() << std::endl;
}
}
// Compute distances matrix
if(cmd.mDistanceMethod != CmdLine::NO_METHOD_SELECTED)
{
if(cmd.mVerboseLevel >= 1) std::cerr << std::endl << "Distances computation using method: " << cfp.getDistanceMethod() << std::endl;
cfp.computeDistanceMatrix();
if(cmd.mVerboseLevel >= 1) std::cerr << "Done" << std::endl;
}
// Classify the results
if(cmd.mGroupingMethod != CmdLine::NO_METHOD_SELECTED)
{
if(cmd.mVerboseLevel >= 1)
{
std::cerr << std::endl << "Start grouping using method: " << cfp.getGroupingMethod();
if(cfp.groupingNeedsK()) std::cerr << " (needs K)";
std::cerr << std::endl;
}
// Set parameters
cfp.setMaxGroupingDistance(cmd.mMaxDistanceForGrouping);
cfp.setK(cmd.mK);
// Group the results
cfp.groupResults();
if(cmd.mVerboseLevel >= 1)
{
std::cerr << "Done" << std::endl;
std::cerr << "Num grouped entries: " << cfp.getNgroups() << std::endl;
std::cerr << "Num single entries: " << cfp.getNsingle() << std::endl;
}
}
// Remove duplicated structures
if(cmd.mRemoveDuplicates && cfp.hasFingerprints() && cfp.hasDistanceMatrix() && cfp.getNgroups() > 0)
{
if(cmd.mVerboseLevel >= 1) std::cerr << std::endl << "Remove duplicates" << std::endl;
std::vector<unsigned int> new_idx;
int new_cnt = cfp.reduceDuplicatesToRepresentative(new_idx);
// Save the map to a file if a filename has been specified and if mapping should be done
if(cmd.mMapFile && new_cnt > 0)
{
std::ofstream out(cmd.mMapFile, std::ios_base::trunc | std::ios_base::out);
if(!out.good())
{
std::cerr << "Cannot create duplicates map file <" << cmd.mMapFile << ">" << std::endl;
}
else
{
for(std::vector<unsigned int>::const_iterator ii= new_idx.begin(); ii != new_idx.end(); ++ii) out << *ii << std::endl;
out.close();
}
}
if(cmd.mVerboseLevel >= 1)
{
if(new_cnt > 0) std::cerr << "New count: " << new_cnt << std::endl;
else std::cerr << "Nothing done" << std::endl;
}
}
// Output fingerprints in AVS FLD format
if(cmd.mFldOutFp && cfp.hasFingerprints())
{
// Prepare the names of the fld and dat files
std::string fld_out_fp = cmd.mFldOutFp;
std::string ffld;
std::string fdat;
size_t ext = fld_out_fp.find_last_of('.');
if(ext == std::string::npos)
{
ffld = fld_out_fp + ".fld";
fdat = fld_out_fp + ".dat";
}
else
{
ffld = fld_out_fp.substr(0, ext) + ".fld";
fdat = fld_out_fp.substr(0, ext) + ".dat";
}
size_t slash = fdat.find_last_of("/\\");
std::string fdat_base = (slash != std::string::npos) ? fdat.substr(slash+1) : fdat;
// Create the description file
std::ofstream fld(ffld.c_str(), std::ios_base::trunc | std::ios_base::out);
if(!fld.good())
{
std::cerr << "Cannot create fingerprints fld file <" << ffld << ">" << std::endl;
}
else
{
unsigned int fplen = cfp.getFingerprintNumSections() * cfp.getFingerprintSectionLen();
fld << "# AVS field file" << std::endl;
fld << "ndim=2 # number of dimensions in the field" << std::endl;
fld << "dim1=" << std::left << std::setw(9) << fplen << "# dimension of axis 1 (fp length)" << std::endl;
fld << "dim2=" << std::left << std::setw(9) << cfp.getNumActiveStructures() << "# dimension of axis 2 (num. structures)" << std::endl;
fld << "nspace=2 # number of physical coordinates per point" << std::endl;
fld << "veclen=1 # number of components at each point" << std::endl;
fld << "data=float # portable data format" << std::endl;
fld << "field=uniform # field type (uniform, rectilinear, irregular)" << std::endl;
fld << "variable 1 file=" << fdat_base << " filetype=binary skip=" << 3*sizeof(int) << std::endl;
fld.close();
}
// Create the binary file (first two integers contain the dim1 and dim2 values)
std::ofstream dat(fdat.c_str(), std::ios_base::binary | std::ios_base::trunc | std::ios_base::out);
if(!dat.good())
{
std::cerr << "Cannot create fingerprints dat file <" << fdat << ">" << std::endl;
}
else
{
int fplen = (int)(cfp.getFingerprintNumSections() * cfp.getFingerprintSectionLen());
dat.write((const char *)&fplen, sizeof(int));
int numfp = (int)cfp.getNumActiveStructures();
dat.write((const char *)&numfp, sizeof(int));
int nparts = (int)cfp.getFingerprintNumSections();
dat.write((const char *)&nparts, sizeof(int));
for(size_t s=0; s < (size_t)numfp; ++s)
{
const float* f = cfp.getFingerprint(s);
dat.write((const char *)f, sizeof(float)*fplen);
}
dat.close();
}
}
// Output distances in AVS FLD format
if(cmd.mFldOutDist && cfp.hasDistanceMatrix())
{
// Prepare the names of the fld and dat files
std::string fld_out_dist = cmd.mFldOutDist;
std::string ffld;
std::string fdat;
size_t ext = fld_out_dist.find_last_of('.');
if(ext == std::string::npos)
{
ffld = fld_out_dist + ".fld";
fdat = fld_out_dist + ".dat";
}
else
{
ffld = fld_out_dist.substr(0, ext) + ".fld";
fdat = fld_out_dist.substr(0, ext) + ".dat";
}
size_t slash = fdat.find_last_of("/\\");
std::string fdat_base = (slash != std::string::npos) ? fdat.substr(slash+1) : fdat;
// Create the description file
std::ofstream fld(ffld.c_str(), std::ios_base::trunc | std::ios_base::out);
if(!fld.good())
{
std::cerr << "Cannot create distances fld file <" << ffld << ">" << std::endl;
}
else
{
size_t nstruct = cfp.getNumActiveStructures();
fld << "# AVS field file" << std::endl;
fld << "ndim=2 # number of dimensions in the field" << std::endl;
fld << "dim1=" << std::left << std::setw(9) << nstruct << "# dimension of axis 1 (num. structures)" << std::endl;
fld << "dim2=" << std::left << std::setw(9) << nstruct << "# dimension of axis 2 (num. structures)" << std::endl;
fld << "nspace=2 # number of physical coordinates per point" << std::endl;
fld << "veclen=1 # number of components at each point" << std::endl;
fld << "data=float # portable data format" << std::endl;
fld << "field=uniform # field type (uniform, rectilinear, irregular)" << std::endl;
fld << "variable 1 file=" << fdat_base << " filetype=binary skip=" << 2*sizeof(int) << std::endl;
fld.close();
}
// Create the binary file (first two integers contain the dim1 and dim2 values)
std::ofstream dat(fdat.c_str(), std::ios_base::binary | std::ios_base::trunc | std::ios_base::out);
if(!dat.good())
{
std::cerr << "Cannot create distances dat file <" << fdat << ">" << std::endl;
}
else
{
int nstruct = static_cast<int>(cfp.getNumActiveStructures());
dat.write((const char *)&nstruct, sizeof(int));
dat.write((const char *)&nstruct, sizeof(int));
size_t s, v;
size_t ns = cfp.getNumActiveStructures();
for(s=0; s < ns; ++s)
{
for(v=0; v < ns; ++v)
{
float d = cfp.getDistance(s, v);
dat.write((const char *)&d, sizeof(float));
}
}
dat.close();
}
}
// Output sorted distances
if(cmd.mSortedDistFile && cfp.hasDistanceMatrix())
{
if(cmd.mVerboseLevel >= 1) std::cerr << std::endl << "Start sorting distances" << std::endl;
std::ofstream outsorted(cmd.mSortedDistFile, std::ios_base::binary | std::ios_base::trunc | std::ios_base::out);
if(!outsorted.good())
{
std::cerr << "Cannot create sorted distances file <" << cmd.mSortedDistFile << ">" << std::endl;
}
else
{
size_t ns = cfp.getNumActiveStructures();
size_t s, v;
std::vector<float> dist;
dist.reserve((ns*(ns-1))/2);
for(s=0; s < ns-1; ++s)
{
for(v=s+1; v < ns; ++v)
{
float d = cfp.getDistance(s, v);
dist.push_back(d);
}
}
std::sort(dist.begin(), dist.end());
int ndist = static_cast<int>(dist.size());
outsorted.write((const char *)&ndist, sizeof(int));
std::vector<float>::const_iterator idist;
for(idist = dist.begin(); idist != dist.end(); ++idist)
{
float d = *idist;
outsorted.write((const char *)&d, sizeof(float));
}
outsorted.close();
}
if(cmd.mVerboseLevel >= 1) std::cerr << "End sorting distances" << std::endl;
}
// Output a summary of the computed quantities
if(cmd.mSummaryFile)
{
std::ofstream outsummary(cmd.mSummaryFile, std::ios_base::trunc | std::ios_base::out);
if(!outsummary.good())
{
std::cerr << "Cannot create summary <" << cmd.mSummaryFile << ">" << std::endl;
}
else
{
outsummary << "Loaded:" << cfp.getNumActiveStructures() << std::endl;
outsummary << "Computed cutoff distance:" << std::fixed << std::setprecision(4) << cfp.computeCutoffDistance() << std::endl;
outsummary << "Actual cutoff distance:" << std::fixed << std::setprecision(4) << cfp.getCutoffDistance() << std::endl;
outsummary << "Dimension:" << cfp.getFingerprintNumSections() * cfp.getFingerprintSectionLen() << std::endl;
outsummary.close();
}
}
// Compute the required analysis
if(cmd.mAnalysisMethod != CmdLine::NO_METHOD_SELECTED)
{
// Start the analysis method
CrystalFpAnalysis analysis(&cfp);
// Set the analysis (and verify valididy)
if(!analysis.setAnalysisMethod(cmd.mAnalysisMethod, cmd.mAnalysisMethod2)) throw CrystalFpFatal("Invalid analysis requested");
#if 0
// Enable caching (useful only for testing here because makes sense only in interactive usage)
analysis.enableCaching();
#endif
// Set parameters
for(std::map<std::string,std::string>::const_iterator im=cmd.mAnalysisParams.begin(); im != cmd.mAnalysisParams.end(); ++im)
{
analysis.setNamedParam(im->first, im->second);
}
// Compute number of values (first element is the number of X values following)
std::vector<size_t> num_results = analysis.numValues();
// Output the values on file (well does not make much sense not to have this set)
if(cmd.mAnalysisFile)
{
// Create the analysis results file
std::ofstream fa(cmd.mAnalysisFile, std::ios_base::trunc | std::ios_base::out);
if(!fa.good())
{
std::cerr << "Cannot create analysis file <" << cmd.mAnalysisFile << ">" << std::endl;
}
else
{
// Output header
std::vector<std::string> lbl = analysis.getLabels();
std::vector<std::string>::const_iterator ilbl;
bool first = true;
for(ilbl=lbl.begin(); ilbl != lbl.end(); ++ilbl)
{
if(first) first = false; else fa << ',';
fa << '"' << *ilbl << '"';
}
fa << std::endl;
// Output values
unsigned int i, j;
float **out = new float*[num_results.size()-1];
for(i=0; i < num_results.size()-1; ++i) out[i] = new float[num_results[i+1]];
analysis.getValues(out);
for(i=0; i < num_results[1]; ++i)
{
first = true;
for(j=0; j < num_results.size()-1; ++j)
{
if(first) first = false; else fa << ',';
fa << std::scientific << std::setprecision(6) << out[j][i];
}
fa << std::endl;
}
// Close file and release memory
fa.close();
for(i=0; i < num_results.size()-1; ++i) delete [] out[i];
delete [] out;
}
}
}
// Creaste the corresponding scatterplot
if(cmd.mCreateScatterplot)
{
if(cmd.mVerboseLevel >= 1) std::cerr << std::endl << "Start creating scatterplot" << std::endl;
// Create the scatterplot object and set few default values
CrystalFpScatterplot sp;
unsigned int num_retries = 1;
float min_energy = 1e-6F;
unsigned int max_iterations = 600;
CrystalFpScatterplot::ValueType value_type = CrystalFpScatterplot::VAL_STRESS;
CrystalFpScatterplot::DiagnosticType diagnostic_type = CrystalFpScatterplot::DIAG_BINNED_DISTANCES;
float timestep = 0.02F;
// Set parameters from command line
for(std::map<std::string,std::string>::const_iterator im=cmd.mScatterplotParams.begin(); im != cmd.mScatterplotParams.end(); ++im)
{
if(!strncasecmp(im->first.c_str(), "retry", 1))
{
num_retries = atoi(im->second.c_str());
sp.setNamedParam(im->first, im->second);
}
else if(!strncasecmp(im->first.c_str(), "energy", 1))
{
min_energy = (float)atof(im->second.c_str());
}
else if(!strncasecmp(im->first.c_str(), "iterations", 1))
{
max_iterations = atoi(im->second.c_str());
}
else if(!strncasecmp(im->first.c_str(), "kind", 1))
{
value_type = (CrystalFpScatterplot::ValueType)atoi(im->second.c_str());
}
else if(!strncasecmp(im->first.c_str(), "diagnostic", 1))
{
diagnostic_type = (CrystalFpScatterplot::DiagnosticType)atoi(im->second.c_str());
}
else if(!strncasecmp(im->first.c_str(), "timestep", 1))
{
timestep = (float)atof(im->second.c_str());
}
}
// Recap the scatterplot values
if(cmd.mVerboseLevel >= 1)
{
sp.dumpParams();
std::cerr << "Num. retries: " << std::setw(12) << num_retries << std::endl;
std::cerr << "Min energy: " << std::setw(12) << min_energy << std::endl;
std::cerr << "Max iterations: " << std::setw(12) << max_iterations << std::endl;
std::cerr << "Value type: " << std::setw(12) << value_type << std::endl;
std::cerr << "Diagnostic: " << std::setw(12) << diagnostic_type << std::endl;
std::cerr << "Timestep: " << std::setw(12) << timestep << std::endl;
}
// Initialize the scatterplot and run it the required number of retries
size_t npoints = sp.initScatterplot(&cfp);
unsigned int i, j;
for(i=0; i < num_retries; ++i)
{
if(cmd.mVerboseLevel >= 1) std::cerr << std::endl << "Start retry " << i << std::endl;
// Iterate and stop on energy or num. iterations criterias
for(j=0; j < max_iterations; ++j)
{
float energy = sp.stepScatterplot(timestep);
if(energy < min_energy) break;
}
// Perturb the point position to run another retry
if(num_retries > 1) sp.perturbPositions();
}
// Output the values on file (well, does not make much sense not to have this set)
if(cmd.mScatterplotFile)
{
// Create the analysis results file
std::ofstream fa(cmd.mScatterplotFile, std::ios_base::trunc | std::ios_base::out);
if(!fa.good())
{
std::cerr << "Cannot create scatterplot file <" << cmd.mScatterplotFile << ">" << std::endl;
}
else
{
float* coords = new float[2*npoints];
float* vals = new float[npoints];
// Get the values
sp.getPoints(coords);
sp.getValues(vals, value_type);
// Output header
fa << "x,y,value" << std::endl;
// Output values
for(i=0; i < npoints; ++i)
{
fa << std::scientific << std::setprecision(6) << coords[2*i+0] << ','
<< std::scientific << std::setprecision(6) << coords[2*i+1] << ','
<< std::scientific << std::setprecision(6) << vals[i] << std::endl;
}
// Close file and release memory
fa.close();
delete [] coords;
delete [] vals;
}
}
if(cmd.mDiagnosticFile && diagnostic_type != CrystalFpScatterplot::DIAG_DO_NOTHING)
{
// Create the analysis results file
std::ofstream fa(cmd.mDiagnosticFile, std::ios_base::trunc | std::ios_base::out);
if(!fa.good())
{
std::cerr << "Cannot create scatterplot diagnostic file <" << cmd.mDiagnosticFile << ">" << std::endl;
}
else
{
npoints = sp.initDiagnostic(diagnostic_type);
float* coords = new float[2*npoints];
float* vals = new float[npoints];
// Get the values
sp.getDiagnosticValues(coords, vals);
// Output header
fa << "x,y,value" << std::endl;
// Output values (for binned, only the not empty bins)
for(i=0; i < npoints; ++i)
{
if(diagnostic_type != CrystalFpScatterplot::DIAG_BINNED_DISTANCES || vals[i] > 0)
{
fa << std::scientific << std::setprecision(6) << coords[2*i+0] << ','
<< std::scientific << std::setprecision(6) << coords[2*i+1] << ','
<< std::scientific << std::setprecision(6) << vals[i] << std::endl;
}
}
// Close file and release memory
fa.close();
delete [] coords;
delete [] vals;
}
}
}
// Serialize the CrystalFp class to file
if(cmd.mSerializeFile)
{
if(cmd.mVerboseLevel >= 1) std::cerr << std::endl << "Start serializing content" << std::endl;
std::ofstream serialized(cmd.mSerializeFile, std::ios_base::binary | std::ios_base::trunc | std::ios_base::out);
if(!serialized.good())
{
std::cerr << "Cannot create serialized file <" << cmd.mSerializeFile << ">" << std::endl;
}
else
{
cfp.serialize(serialized);
serialized.close();
}
if(cmd.mVerboseLevel >= 1) std::cerr << "End serializing content" << std::endl;
// TEST
if(cmd.mVerboseLevel >= 1) std::cerr << "Try to deserialize content" << std::endl;
CrystalFp xcfp(cmd.mVerboseLevel);
std::ifstream unserialized(cmd.mSerializeFile, std::ios_base::binary | std::ios_base::in);
if(!unserialized.good())
{
std::cerr << "Cannot open serialized file <" << cmd.mSerializeFile << ">" << std::endl;
}
else
{
xcfp.unserialize(unserialized);
unserialized.close();
}
if(cmd.mVerboseLevel >= 1) std::cerr << "End deserializing content" << std::endl;
std::cerr << std::endl << "**** Original" << std::endl;
cfp.dump();
std::cerr << std::endl << "**** Reloaded" << std::endl;
xcfp.dump();
}
////////////////////////////////////////////////////////////////////////////////////////////////
// Catch all exceptions
}
catch(CmdLineSuccess&)
{
return 0;
}
catch(CrystalFpFatal& e)
{
// Don't print throw messages for which the message has been already printed
const char* p = e.what();
if(*p) std::cerr << std::endl << "Error: " << p << std::endl;
return 1;
}
catch(CmdLineFatal& e)
{
// Don't print throw messages for which the message has been already printed
const char* p = e.what();
if(*p) std::cerr << std::endl << "Error: " << p << std::endl;
return 1;
}
catch (std::exception& e)
{
std::cerr << std::endl << "Error: Std exception: " << e.what() << std::endl;
}
catch(...)
{
std::cerr << std::endl << "Error: Catch-all exception" << std::endl;
return 1;
}
}