-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc.c
1841 lines (1617 loc) · 49.3 KB
/
calc.c
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
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*=================================================================*/
/*= calc.c =*/
/*= main calculation and iteration routines for treekin =*/
/*= --------------------------------------------------------- =*/
/*= Last changed Time-stamp: <2012-01-14 19:04:10 mtw> =*/
/*= $Id: calc.c,v 1.41 2006/11/27 23:01:45 mtw Exp $ =*/
/*= --------------------------------------------------------- =*/
/*= (c) Michael Thomas Wolfinger, W. Andreas Svrcek-Seiler =*/
/*= {mtw,svrci}@tbi.univie.ac.at =*/
/*= treekin =*/
/*=================================================================*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <assert.h>
#include "exp_matrix.h" /* functions for matrix-exponent stuff */
#include "mxccm.h" /* functions for eigen-problems stolen from ccmath */
#include "barparser.h" /* functions for input */
#include "calc.h" /* does all matrix stuff for markov process */
#include "globals.h" /* contains getopt-stuff */
#include "calcpp.h"
#define FEPS 1.0e-15
#define SQ(X) ((X)*(X))
/* private function(s) */
static double *MxMethodeA (BarData *Data);
static double *MxMethodeFULL(double *);
static double *MxMethodeINPUT (BarData *Data, double *);
static double *MxMethodeLoc(double *);
static double max_saddle(int i, int j, BarData *Data);
static void print_settings(void);
static char *time_stamp(void);
static void MxDoDegeneracyStuff(void);
static void MxBinWrite(double *Mx, char what[], char T);
static int MxBinRead(double** Mx, char what[], char T);
static void MxASCIIWrite(double *Mx, char *asciifile);
static void MxASCIIWriteV(double *Mx, char *asciifile);
static void MxKotzOutMathematica(double *Mx);
static void MxSortEig(double *evals, double *evecs);
static void MxEVLapackSym(double *U);
static void MxEVLapackNonSym(double *U);
static void MxFixevecs(double *, double *);
static void MxDiagHelper(double *P8);
void MxFPrintD(double *mx, char *name, int dim1, int dim2, FILE *out);
/* private vars and arrays */
static int dim = 0;
static double _kT = 1.;
static double *evals = NULL;
static double *evecs = NULL;
static double *_sqrPI = NULL; /* left constant array */
static double *sqrPI_ = NULL; /* right constant array */
static double *D = NULL; /* matrix with degree of degeneracy */
static char Aname[30];
static TypeDegSaddle *saddle = NULL;
/*==*/
void
MxInit (int d)
{
_kT = 0.00198717*(273.15 + opt.T);
if (d > 0 ) dim = d;
else {
fprintf(stderr, "dim <= 0\n");
exit(EXIT_FAILURE);
}
}
/*==*/
double*
MxBar2Matrix ( BarData *Data, double *R)
{
double *U=NULL;
if(opt.want_degenerate) MxDoDegeneracyStuff();
switch (opt.method) {
case 'A':
U = MxMethodeA(Data);
break;
case 'F':
U = MxMethodeFULL(R);
break;
case 'I':
U = MxMethodeINPUT(Data, R);
break;
case 'L':
U = MxMethodeLoc(R);
break;
default:
fprintf (stderr,
"ERROR in MxBar2Matrix(): No handler 4 method %c\n", opt.method);
exit(EXIT_FAILURE);
}
if (opt.dumpU) {
MxASCIIWrite(U, "U.txt");
MxBinWrite(U, "U", 'm');
}
return (U);
}
/*==*/
void
MxGetSpace (double **p8)
{
*p8 = (double *) MxNew (dim*sizeof(double));
evals = (double *) MxNew (dim*sizeof(double));
evecs = (double *) MxNew (dim*dim*sizeof(double));
assert(evals!=NULL);
assert(evecs!=NULL);
assert(p8!=NULL);
if(!opt.absrb) {
_sqrPI = (double *) MxNew (dim*dim*sizeof(double));
sqrPI_ = (double *) MxNew (dim*dim*sizeof(double));
}
}
/*==*/
void
MxStartVec (double **p0)
{
int i;
double *pzero = NULL;
pzero = (double *) MxNew(dim*sizeof(double));
if (opt.pini) {
for (i = 1; i < (int) *opt.pini; i+=2)
pzero[(int)opt.pini[i]-1] = (double)opt.pini[i+1];
/* -1 because our lmins start with 1, not with 0 (as Data does ) */
} else {
// all into first state...
pzero[0]=1.0;
}
if (opt.want_verbose) MxPrint (pzero, "p0", 'v');
*p0=pzero;
}
/*==*/
/* calculate equilibrium distribution */
void
MxEqDistr ( BarData *Data, double **p8 )
{
int i;
double Z = 0.;
if(opt.absrb) {
for(i = 0; i < dim; i++)
(*p8)[i] = 0.;
(*p8)[dim-1] = 1.0; /* last entry is the 'new' absorbing state */
}
else {
for(i = 0; i < dim; i++) Z += exp(-((double)Data[i].energy/_kT));
for(i = 0; i < dim; i++) (*p8)[i] = exp(-((double) Data[i].energy/_kT));
}
/* now normalize the new p8 */
double sumsq=0.0;
for (i=0; i<dim; i++)
sumsq += SQ((*p8)[i]);
if(sumsq > 0.0)
sumsq=1./sqrtl(sumsq);
for (i=0; i<dim; i++)
*(*p8+i) *= sumsq;
if(opt.want_verbose) MxPrint (*p8, "p8", 'v');
return;
}
/*==*/
void
MxEqDistrFULL (SubInfo *E, double *p8 )
{
int i;
double Z = 0.;
if(opt.absrb) {
for(i = 0; i < dim; i++)
p8[i] = 0.;
p8[opt.absrb-1] = 1.;
}
else {
for(i = 0; i < dim; i++) Z += exp(-E[i].energy/_kT);
for(i = 0; i < dim; i++) p8[i] = exp(-E[i].energy/_kT)/Z;
}
if(opt.want_verbose) MxPrint (p8, "p8", 'v');
return;
}
void
MxEqDistrFromLinSys( double *U, double **p8 )
{
extern void dgesv_(int *N,int *NRHS,double *A,int *LDA,int *IPIV,
double *B,int *LDB,int *INFO);
int i,j,n, nrhs, nfo, *ipiv=NULL;
double *A=NULL, *B=NULL;
if(opt.absrb) {
for(i = 0; i < dim; i++)
*(*p8+i) = 0.;
*(*p8+(dim-1)) = 1.0; /* last entry is the 'new' absorbing state */
}
else {
n=dim-1;
A = (double *)malloc(n*n*sizeof(double));
B = (double *)malloc(n*sizeof(double));
ipiv = (int *)malloc(n*sizeof(int));
nrhs=1;
if (opt.want_verbose) MxPrint(U, "U", 'm' );
for(i=1; i<=n; i++) /* all except first row */
for(j=1; j<=n; j++)
A[n*(i-1)+(j-1)]=U[dim*i+j] - (i==j ? 1.0 : 0.0); //U-I=Q
for(n=0,i=1; i<dim; i++,n++)
B[n]=-U[dim*i];
dim=n;
trnm(A,n);
if (opt.want_verbose) MxPrint(A, "A in MxEqDistrFromLinSys", 'm' );
if (opt.want_verbose) MxPrint(B, "B in MxEqDistrFromLinSys", 'v' );
//DGESV computes the solution to a real system of linear equations A * X = B
dgesv_(&n, &nrhs, A, &n, ipiv, B, &n, &nfo);
if(nfo != 0) {
fprintf(stderr, "dgesv exited with value %d (do rates have good dimension?)\n", nfo);
exit(EXIT_FAILURE);
}
if (opt.want_verbose) MxPrint(B, "X in MxEqDistrFromLinSys", 'v' );
dim=n+1;
*p8[0]=1.;
for(i=1; i<dim; i++) *(*p8+i)=B[i-1];
if (opt.want_verbose) MxPrint(*p8, "p8 in MxEqDistrFromLinSys before norm", 'v' );
/* now make the vector stochastic (sum = 1.0) */
long double sum=0.0;
for (i=0; i<dim; i++) {
sum += (*p8)[i];
}
for (i=0; i<dim; i++) {
(*p8)[i] /= sum;
}
// now normalize the new p8
/*long double sumsq=0.0;
for (i=0; i<dim; i++)
sumsq += SQ((*p8)[i]);
if(sumsq > 0.0)
sumsq=1./sqrtl(sumsq);
for (i=0; i<dim; i++)
*(*p8+i) *= sumsq;
*/
free(A);
free(B);
free(ipiv);
}
if(opt.want_verbose)
MxPrint(*p8, "p8", 'v' );
}
/*==*/
void
MxDiagonalize ( double *U, double **_S, double *P8)
{
int i,j;
double *tmpMx=NULL;
if(opt.dumpMathematica == 1) MxKotzOutMathematica(U);
if(!opt.absrb) {
if (opt.want_verbose) MxPrint (U, "input U", 'm');
tmpMx = (double *) MxNew (dim*dim*sizeof(double));
MxDiagHelper(P8);
mmul(tmpMx, _sqrPI, U, dim);
if (opt.want_verbose) MxPrint (_sqrPI, "_sqrPI", 'm');
if (opt.want_verbose) MxPrint (tmpMx, "tmpMx = _sqrPI*U", 'm');
if (opt.want_verbose) MxPrint (sqrPI_, "sqrPI_", 'm');
memset(U,0,dim*dim*sizeof(double));
mmul(U, tmpMx, sqrPI_, dim);
if (opt.want_verbose) MxPrint (U, "U = _sqrPI*U*sqrPI_", 'm');
free(tmpMx);
if (opt.want_verbose) MxPrint (U, "force symmetrized U(uncorrected)", 'm');
/* correct for numerical errors */
long double err = 0.0; // acumulated error
for (i = 0; i < dim; i++) {
for (j = 0; j < dim; j++) {
err += fabs((U[dim*i+j]+U[dim*j+i])/2 - U[dim*i+j]);
U[dim*i+j] = (U[dim*i+j]+U[dim*j+i])/2;
U[dim*j+i] = U[dim*i+j];
}
}
if (isnan(err)) {
fprintf(stderr, "Matrix is not ergodic! Exiting...");
exit(-1);
}
if (!opt.quiet) fprintf(stderr, "Corrected numerical error: %e (%e per number)\n", (double)err, (double)(err/(long double)(dim*dim)));
if (opt.want_verbose) MxPrint (U, "force symmetrized U", 'm');
}
// get eigenv*
if(opt.absrb) {
MxEVLapackNonSym(U);
} else {
MxEVLapackSym(U);
}
MxSortEig(evals, evecs);
if (opt.want_verbose) {
MxPrint(evecs, "Eigenvectors of U (LAPACK)", 'm');
MxPrint(evals, "Eigenvalues of U (LAPACK)", 'v');
}
for (i=0; i<dim; i++)
if (evals[i]>(1.0-FEPS)) evals[i]=1.0;
if(opt.absrb)
MxFixevecs(evecs,evals);
*_S=evecs;
if(opt.wrecover) {
MxBinWrite(evals, "evals", 'v');
MxBinWrite(evecs, "evecs", 'm');
MxASCIIWriteV(evals, "evals.txt");
MxASCIIWrite(evecs, "evecs.txt");
}
}
/*==*/
static void
MxDiagHelper(double *P8)
{
int i,j;
for(i = 0; i < dim; i++)
for(j = 0; j < dim; j++)
if( i == j) {
sqrPI_[dim*i+j] = sqrt(P8[i]); /* pos right */
_sqrPI[dim*i+j] = 1/(sqrPI_[dim*i+j]); /* neg left */
}
}
/*==*/
void
MxRecover (double **_S, double *P8)
{
MxDiagHelper(P8);
free(evecs);
free(evals);
if(MxBinRead(&evecs, "evecs", 'm') != dim ) {
fprintf(stderr, "ERROR: MxBinRead() returns wrong dimension for evecs\n");
exit(EXIT_FAILURE);
}
if(MxBinRead(&evals, "evals", 'v') != dim) {
fprintf(stderr, "ERROR: MxBinRead() returns wrong dimension for evals\n");
exit(EXIT_FAILURE);
}
*_S=evecs;
if(opt.want_verbose) {
MxPrint(evals, "MxRecover: Eigenvalues", 'v');
MxPrint(evecs, "MxRecover: Eigenvectors", 'm');
}
return;
}
/*==*/
/* S which comes into this function contains the (right) eigenvectors
calculated by LAPACK */
void
MxIterate (double *p0, double *p8, double *S)
{
/* solve following equation 4 various times
***** NON-ABSORBING CASE: ******
p(t) = sqrPI_ * S * exp(time * EV) * St * _sqrPI * p(0)
CL = sqrPI_ * S
CR = St * _sqrPI
tmpVec = CR * p(0)
tmpVec2 = exp(time * EV) * tmpVec
p(t) = CL * tmpVec2
******* ABSORBING CASE: *******
p(t) = S * exp(time * EV) * S_inv * p(0)
tmpVec = S_inv * p(0)
tmpVec2 = exp(time * EV) *tmpVec
p(t) = S * tmpVec2
*/
int i, count = 0;
double time, check = 0.;
double *CL = NULL, *CR, *exptL, *tmpVec, *tmpVec2, *pt, *St, *pdiff;
double *ptFULL = NULL; /* prob dist 4 of the effective lmins of the tree at time t */
double *p8FULL = NULL; /* equ dist 4 gradient basins, full process */
tmpVec = (double *) MxNew (dim*sizeof(double));
tmpVec2 = (double *) MxNew (dim*sizeof(double));
pt = (double *) MxNew (dim*sizeof(double));
exptL = (double *) MxNew (dim*dim*sizeof(double));
if(opt.method=='F') {
ptFULL = (double *) MxNew ((lmins+1)*sizeof(double));
p8FULL = (double *) MxNew ((lmins+1)*sizeof(double));
pdiff = (double *) MxNew ((lmins+1)*sizeof(double));
}
else pdiff = (double *) MxNew (dim*sizeof(double));
if(! opt.absrb) { /* NON-absorbing case */
CL = (double *) MxNew (dim*dim*sizeof(double));
CR = (double *) MxNew (dim*dim*sizeof(double));
St = (double *) MxNew (dim*dim*sizeof(double));
mcopy(St, S, dim*dim);
trnm(St, dim); /* transpose S (same as invert(S) ('cause of symmetry) minv(St, dim); )*/
mmul (CL, sqrPI_, S, dim);
mmul (CR, St, _sqrPI, dim);
vmul (tmpVec, CR, p0, dim);
free(St);
free(CR);
}
else { /* absorbing case */
double *S_inv;
S_inv = (double *) MxNew (dim*dim*sizeof(double));
mcopy(S_inv, S, dim*dim);
minv(S_inv,dim);
if(opt.want_verbose) MxPrint(evals, "evals in MxIterate", 'v');
vmul (tmpVec, S_inv, p0, dim);
free(S_inv);
}
if(opt.method=='F') { /* calculate equilibrium distribution once */
for (i = 0; i < dim; i++) p8FULL[E[i].ag] += p8[i];
for (i = 0; i < lmins; i++) check += fabs(p8FULL[i]);
if ( ((check-1) < -0.1) || ((check-1) > 0.1) ) {
fprintf(stderr, "overall equilibrium probability is %e != 1. !\n", check);
if (opt.num_err == 'H') exit(EXIT_FAILURE);
else if (opt.num_err == 'R') {
for (i=0; i<dim; i++) p8[i] /= check;
check = 1.0;
}
}
}
check = 0.;
for(i = 0; i < dim; i++) evals[i] -= 1; /* compensate 4 translation of matrix U */
/* solve fundamental equation */
print_settings();
if (opt.t0 == 0.0) {
if (opt.method=='F') PrintProbFull(p0, dim, 0.0, lmins);
else PrintProb(p0, dim, 0.0);
opt.t0 = TZERO;
}
// iterate
for (time = opt.t0; time <= opt.t8; time *= opt.tinc) {
for (i = 0; i < dim; i++)
exptL[dim*i+i] = exp(time*evals[i]);
vmul(tmpVec2, exptL, tmpVec, dim);
if(!opt.absrb) vmul(pt, CL, tmpVec2, dim);
else vmul(pt, S, tmpVec2, dim);
count++; /* # of iterations */
if(opt.method=='F') {
memset(ptFULL, 0, (lmins+1)*sizeof(double));
for (i = 0; i < dim; i++) {
ptFULL[E[i].ag] += pt[i];
}
}
// print probabilities with respect to corrected ergodicity
if (opt.method=='F') check = PrintProbFull(pt, dim, time, lmins);
else check = PrintProb(pt, dim, time);
//PrintProbNR(p8FULL, lmins, -1);
int reached;
if (opt.method=='F') reached = ConvergenceReached(p8FULL, ptFULL, lmins, 1);
else reached = ConvergenceReached(p8, pt, dim, 0);
fflush(stdout);
if (reached) break;
}
if (time < opt.t8) {
if (opt.method=='F') PrintProbFull(pt, dim, opt.t8, lmins);
else PrintProb(pt, dim, opt.t8);
}
printf("# of iterations: %d\n", count);
/*** end solve fundamental equation ***/
if(opt.method=='F') {
free(ptFULL);
free(p8FULL);
free(E);
}
free(evals);
free(exptL);
free(CL);
free(tmpVec);
free(tmpVec2);
free(pdiff);
if(pt != NULL) free(pt);
}
/*==*/
static double*
MxMethodeA (BarData *Data)
{
/***************************************/
/* | E_s | */
/* \ ____________ / */
/* \ | /\ | / */
/* \ | / \ | / */
/* \| / \ | / */
/* \/ \ | / */
/* i \ | / */
/* \/ */
/* j */
/* -beta(E_s-E_j) */
/* rate: j->i: prop e */
/* -beta(E_s-E_i) */
/* i->j prop e */
/****************************************/
int i,j,real_abs = 0;
double m_saddle, Zabs, abs_rate, *T, *U;
U = (double *) MxNew (dim*dim*sizeof(double));
for( i = 0; i < dim; i++) {
for( j = i+1; j < dim; j++) {
m_saddle = max_saddle(i, j, Data);
/* rate j -> i */
U[dim*i+j] = 1.0*exp(-(m_saddle-Data[j].energy)/_kT);
/* rate i -> j */
U[dim*j+i] = 1.0*exp(-(m_saddle-Data[i].energy)/_kT);
}
}
if(opt.absrb) { /*==== absorbing states ====*/
dim++;
fprintf(stderr, "dim increased to %i\n", dim);
T = (double *) MxNew(dim*dim*sizeof(double));
real_abs = opt.absrb; /* the original absorbing lmin */
real_abs--;
opt.absrb = dim; /* the 'new' abs state = last row/column of rate matrix */
fprintf(stderr, "new absorbing lmin is: %i\n", opt.absrb);
Zabs = exp((-Data[real_abs].energy)/_kT);
abs_rate = exp((-Data[real_abs].energy)/_kT)/Zabs;
for(i = 0; i < (dim-1); i++) { /* all except the last row */
for(j = 0; j < (dim-1); j++)
T[dim*i+j] = U[(dim-1)*i+j];
T[(dim-1)*j+(dim-1)] = 0.;
}
for(j = 0; j < dim; j++) /* last row */
T[dim*(dim-1)+j] = 0.;
T[dim*(dim-1)+real_abs] = abs_rate;
free(U);
U = T;
if(opt.want_verbose) MxPrint(U, "aufgeblasene Matrix", 'm');
} /*== end absorbing states ==*/
/* set diagonal elements to 0 */
for (i = 0; i < dim; i++) U[dim*i+i] = 0;
for (j = 0; j < dim; j++) {
double tmp = 0.00;
/* calculate column sum */
for(i = 0; i < dim; i++) tmp += U[dim*i+j];
U[dim*j+j] = -tmp+1; /* make U a stochastic matrix */
}
if (opt.want_verbose) MxPrint (U,"U with Methode A", 'm');
return (U);
}
/*==*/
static double*
MxMethodeFULL (double *R)
{
int i, j;
free(D);
if(opt.absrb) { /*==== absorbing states ====*/
for(i = 0; i < dim; i++)
R[dim*i+(opt.absrb-1)] = 0. ;
} /*== end absorbing states ==*/
/* set diagonal elements to 0 */
for (i = 0; i < dim; i++) R[dim*i+i] = 0;
for (j = 0; j < dim; j++) {
double tmp = 0.00;
/* calculate column sum */
for(i = 0; i < dim; i++) tmp += R[dim*i+j];
R[dim*j+j] = -tmp+1; /* make U a stochastic matrix */
}
if (opt.want_verbose) MxPrint (R, "R with Methode F", 'm');
return R;
}
/*==*/
static double*
MxMethodeINPUT (BarData *Data, double *Input)
{
int i, j;
double *U=NULL, Zabs, abs_rate;
if (opt.want_verbose) MxPrint(Input, "Input Matrix", 'm');
if (opt.absrb) { /*==== absorbing states ====*/
dim++;
fprintf(stderr, "dim increased to %i\n", dim);
U = (double *) MxNew(dim*dim*sizeof(double));
opt.real_abs = opt.absrb; /* the original absorbing lmin */
opt.real_abs--;
opt.absrb = dim; /* the 'new' abs state = last row/column of rate matrix */
fprintf(stderr, "new absorbing lmin is: %i\n", opt.absrb);
Zabs = exp((-Data[opt.real_abs].FGr)/_kT);
abs_rate = exp((-Data[opt.real_abs].energy)/_kT)/Zabs;
for(i = 0; i < (dim-1); i++) { /* all except the last row */
for(j = 0; j < (dim-1); j++)
U[dim*i+j] = Input[(dim-1)*i+j];
U[(dim-1)*j+(dim-1)] = 0.;
}
for(j = 0; j < dim; j++) /* last row */
U[dim*(dim-1)+j] = 0.;
U[dim*(dim-1)+opt.real_abs] = abs_rate;
/* if(opt.want_verbose) MxPrint(U, "aufgeblasene Matrix", 'm'); */
} /*== end absorbing states ==*/
else { /*== non-absorbing states ==*/
U = (double *) MxNew(dim*dim*sizeof(double));
memcpy(U, Input, dim*dim*sizeof(double));
} /*== end non-absorbing states ==*/
/* diagonal elements */
for (i = 0; i < dim; i++) U[dim*i+i] = 0;
//fprintf(stderr, "dim is %i\n", dim);
for (i = 0; i < dim; i++) {
double tmp = 0.00;
// calculate column sum
for(j = 0; j < dim; j++) tmp += U[dim*j+i];
U[dim*i+i] = -tmp+1.0; // make U a stochastic matrix U = Q+I ??
}
/* // normalize each column to sum=1
for (i = 0; i < dim; i++) {
double tmp = 0.00;
for(j = 0; j < dim; j++) tmp += U[dim*j+i];
for(j = 0; j < dim; j++) U[dim*j+i] /= tmp;
}*/
if(opt.want_verbose) MxPrint (U,"U with Methode I" , 'm');
free(Input);
return U;
}
static double *MxMethodeLoc(double *arr)
{
int i, j;
double *R = MxNew(dim*dim*sizeof(double));
if(opt.absrb) { /*==== absorbing states ====*/
for(i = 0; i < dim; i++)
R[dim*i+(opt.absrb-1)] = 0. ;
} /*== end absorbing states ==*/
// we assume that saddle energies are "saddle" above the higher energy
// does not matter what exactly in stable distribution cause its only multiplication with exp(-saddle/_kT)
float mult = 0.001; // multiplicator
float saddle = _kT*(log(dim-1)-log(1)); // rate[dim*(dim-1) + (dim-1)] = 0.0
// set rates according to energies
// rate i->j: exp(e(i)>e(j) ? saddle : saddle + e(i)-e(j))
for (i=0; i<dim; i++)
for (j=0; j<dim; j++) {
if (arr[i]<arr[j]) {
R[dim*j+i] = mult*exp(-(arr[j]-arr[i]+saddle)/_kT);
} else {
R[dim*j+i] = mult*exp(-(saddle)/_kT);
}
}
/* set diagonal elements */
for (i=0; i<dim; i++) R[dim*i+i] = 0;
for (i = 0; i<dim; i++) {
double tmp = 0.00;
// calculate column sum
for(j = 0; j < dim; j++) tmp += R[dim*j+i];
R[dim*i+i] = -tmp+1.0; // make R a stochastic matrix R = Q+I ??
}
if (opt.want_verbose) MxPrint (R, "R with Methode L", 'm');
free(arr);
return R;
}
/*==*/
static double
max_saddle(int i, int j, BarData *Data)
{
int tmp;
if(Data[i].number > Data[j].father) { /* exchange i & j */
tmp = i;
i = j;
j = tmp;
}
if(Data[i].number == Data[j].father) return Data[j].energy + Data[j].ediff;
else {
if((Data[j].energy + Data[j].ediff) > max_saddle((Data[j].father - 1), (Data[i].number - 1), Data)) return (Data[j].energy + Data[j].ediff);
else return max_saddle((Data[j].father - 1), (Data[i].number - 1), Data);
}
}
/*==*/
void*
MxNew ( size_t size )
{
void *mx = NULL;
if ( (mx = (void *) calloc (1, size)) == NULL )
fprintf (stderr, "ERROR: new_martix() allocation failed\n");
return mx;
}
void MxFPrintD(double *mx, char *name, int dim1, int dim2, FILE *out)
{
int k, l;
fprintf(out,"%s:{\n", name);
for (k = 0; k < dim1; k++) {
if (k!=0) fprintf(out, ",");
fprintf(out, "{");
for (l=0; l< dim2; l++) {
if (l!=0) fprintf(out, ",");
fprintf(out,"%15.7g (%4d) ", mx[dim2*k+l], dim2*k+l);
}
fprintf(out,"}\n");
}
fprintf(out,"}-----------\n");
}
void MxFPrint(double *mx, char *name, char T, FILE *out, int pure)
{
int k, l;
switch (T) {
case 'm': /* square matrix */
if (!pure) fprintf(out,"%s:\n", name);
for (k = 0; k < dim; k++) {
for (l=0; l< dim; l++) {
fprintf(out,"%15.7f ", mx[dim*k+l]);
}
fprintf(out,"\n");
}
if (!pure) fprintf(out,"---\n");
break;
case 'v':
if (!pure) fprintf(out,"%s:\n", name);
for (k = 0; k < dim; k++) fprintf(out,"%15.7f ", mx[k]);
if (!pure) fprintf(out,"\n---\n");
else fprintf(out, "\n");
break;
default:
fprintf(out,"ERROR MxPrint(): no handler 4 type %c\n", T);
}
}
/*==*/
/* print matrix stored in ccmath-format */
void
MxPrint(double *mx, char *name, char T)
{
MxFPrint(mx, name, T, stderr, 0);
}
void PrintDummy(double *line)
{
print_settings();
PrintProb(line, 1, opt.t0);
PrintProb(line, 1, opt.t8);
}
/*==*/
static void
print_settings(void)
{
printf(
"# Date: %s"
"# Sequence: %s\n"
"# Method: %c Start Time: %.2f Stop Time: %.2f Temperature: %.2f\n",
time_stamp(),
opt.sequence,
opt.method,
opt.t0,
opt.t8,
opt.T
);
if(opt.basename != NULL) printf("# basename: %s\n",opt.basename);
else printf("# basename: <stdin>\n");
if (opt.tinc) printf("# time increment: %.2f\n", opt.tinc);
else printf("# time increment: %.2f \n", opt.tinc);
if(opt.want_degenerate == 1)printf("# degeneracy: on\n");
else printf("# degeneracy: off\n");
if (opt.absrb < 1) printf("# absorbing lmin: none\n");
else printf("# absorbing lmin: %d\n", opt.absrb);
if (opt.n > 0) printf("# nlmins: %d\n", opt.n);
else printf("# nlmins: till EOF\n");
}
/*==*/
static char*
time_stamp(void)
{
time_t cal_time;
cal_time = time(NULL);
return ( ctime(&cal_time) );
}
/*==*/
void
MxMemoryCleanUp (void)
{
if(_sqrPI) free(_sqrPI);
if(sqrPI_) free(sqrPI_);
if(opt.sequence) free(opt.sequence);
if(opt.basename) free(opt.basename);
if(opt.fpt_file) free(opt.fpt_file);
free_gengetopt();
}
/*==*/
static void
MxDoDegeneracyStuff(void)
{
int i, j, b, nr, current, numsad = 1, count = 0;
numsad = ParseSaddleFile(&saddle);
/* loop over all elements of structure-array saddle: */
/* first we fill the upper triangle */
for (count = 0; count < numsad; count++) {
current = 1;
nr = saddle[count].list[0];
/* only for saddles with a cc >= 1 AND those which connect at least 2 lmins */
if(saddle[count].cc >= 1 && nr >= 2 && !(saddle[count].cc == 1 && nr == 2)) {
while(current < nr) {
for(b = current+1; b <= nr; b++) {
/* skip in case a lmin which we don't see is connected by */
/* the saddle (because we can only see --max x lmins) */
if(saddle[count].list[current] > dim || saddle[count].list[b] > dim) {
current++;
continue;
}
/* FIRST: we consider the size of the cc the saddle belongs to */
if(saddle[count].cc > 1) {
D[dim * (saddle[count].list[current]-1) + (saddle[count].list[b]-1)] += (saddle[count].cc - 1);
fprintf(stderr, "transition between %3d - %3d: adding %2d cc\n",
saddle[count].list[current], saddle[count].list[b], saddle[count].cc-1);
/* -1 because if the size of cc == 1 there's nothing special */
/* about it, i.e. there is just one saddle */
}
/* SECOND: we consider that the saddle connects several lmins */
if(nr > 2) {
D[dim * (saddle[count].list[current]-1) + (saddle[count].list[b]-1)]++;
fprintf(stderr, "transition betweed %3d - %3d: adding 1 deg_saddle\n",
saddle[count].list[current], saddle[count].list[b] );
/* -1 because matrix starts with0, NOT with 1 */
}
}
current++;
}
}
}
for(i = 0; i < dim; i++) /* make matrix symmetric */
for(j = 0; j < dim; j++)
if (i != j)
D[dim*j+i] = D[dim*i+j];
if (opt.want_verbose) {
sprintf (Aname, "%s", "D (degeneracies)");
MxPrint (D, Aname, 'm');
}
free(saddle);
}
/*==*/
static void
norm2(double *mx)
{
/* normalize columns of matrix mx */
/* (to euclidean norm 1)*/
int i,j;
long double sumsq;
for (j=0; j<dim; j++) {
sumsq=0.0;
for (i=0; i<dim; i++)
sumsq += SQ(mx[dim*i+j]);
if(sumsq > 0.0)
sumsq=1./sqrtl(sumsq);
for (i=0; i<dim; i++)
mx[dim*i+j] *= sumsq;
}
return;
}
#define abs(x) ((x)>0.0?(x):(-x))
/*==*/
static void
MxFixevecs(double *evecs, double *evals)
/* evecs: eigenvectors columns, dimension N */
/* since the sum over each non-absorbing */
/* column must vanish, replace the */
/* problematic evecs with linear combinations */
/* satisfying that criterion */
/* rationale: with 1^T a row vector of 1s */
/* Q=S*exp(L)*inv(S) */
/* 1^T*Q = 1^T (probabilities sum up to 1) */
/* 1^T*S*exp(L) = 1^T*S */
{
int i,j,maxind,abscount;
double maxent,csum;
/* assume sorted evals */
/* and an absorbing state */
/* im the 1st evec=1st column */
/* take care of possibly more than one abs. state*/
/* all abs. states have been *assigned* eigenvalue one */
/* for each of them, set the largest component of the */
/* eigenvectors to one, all others to zero */
abscount=0;
for (i=0; i<dim; i++) {
if (evals[i]==1.0) {
abscount++;
maxent=0.;
maxind=0;
for (j=0; j<dim; j++) {
if (abs(evecs[dim*j+i]) > maxent) {
maxent=abs(evecs[dim*j+i]);
maxind=j;
}
}
evecs[dim*maxind+i]=1.0;
for (j=0; j<dim; j++) {
if (j==maxind) continue;
evecs[dim*j+i]=0.0;
}
}
}
/* repair messed non abs. eigenvectors */
/* using all abs. states equally */
/* using all abs. states equally */
for (j=abscount; j< dim; j++) {
double mu=0.;
for(i=0; i<dim; i++)
mu += evecs[dim*i+j];
for (i=0; i<abscount; i++)
evecs[dim*i+j] -= mu/(double)abscount;
}
if (opt.want_verbose) {
MxPrint (evals, "evals_complete", 'v');
MxPrint (evecs, "evecs_complete", 'm');
fflush(stdout);
fflush(stderr);
}
norm2(evecs);
if (opt.want_verbose) {
MxPrint (evals, "evals_complete", 'v');
MxPrint (evecs, "evecs_complete", 'm');
fflush(stdout);
fflush(stderr);
}
if (opt.want_verbose) {
fprintf(stderr,"colsums: ");
for (i=0; i<dim; i++) {
csum=0.0;
for (j=0; j<dim; j++)
csum += evecs[dim*j+i];
fprintf(stderr,"%g ", csum);
}
fprintf(stderr,"\n");
}
if (opt.absrb && (abscount > 1)) {
int i,j;
if (!opt.quiet) fprintf(stderr, "\nWARNING: found %d additional absorbing state(s): ", abscount-1);