-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrace_irc.c
1914 lines (1732 loc) · 67.1 KB
/
trace_irc.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
#include "trace_irc.h"
// -------------------------------------------------------------
/* print help */
void print_help(void){
printf("\n\n======================================================================\n\n");
printf("This program traces the intrinsic reaction coordinate from\n");
printf("the NVAR-dimensional free energy surface generate with a\n");
printf("CPMD or PLUMED metadynamics simulation, or a 2D potential on a grid\n\n");
printf(" Requires:\n");
printf(" 1) colvar_val and dcolvar_val files generaterd with CPMD or\n");
printf(" HILLS file generaterd with PLUMED or potential on a grid\n");
printf(" 2) trace_irc.in input file\n");
printf(" Generates:\n");
printf(" 1) trace_coursegrid.out, contains the two minima and some points between\n");
printf(" 2) umbrella.pot, contains all points along the IRC, the free\n");
printf(" energy and the forces to use in an CPMD \n");
printf(" umbrella sampling run\n");
printf(" See also the headerfile trace_irc.h for more files and print options\n");
printf("");
printf("");
printf("\n Keywords required in the inputfile trace_irc:\n");
printf(" INPUT <TYPE>: Read filenames with potential data from next line(s)\n");
printf(" TYPE is optional and can be CPMD, PLUMED, or POTENTIAL (default=CPMD)\n");
printf(" default=colvar_val and dcolvar_val. Old and New CPMD files\n");
printf(" are recognized. For PLUMED the HILLS file is expected.\n");
printf(" For POTENTIAL a 2D potential on a regular grid is expected.\n");
printf(" NVAR: number of collective variables is read from the next line\n");
printf(" NGRID: two integers are read from the next line for the number of\n");
printf(" grid points. Required if INPUT TYPE is a 2D potential (=POT)\n");
printf(" MINIMA: next two lines should contain an initial guess for the\n");
printf(" two minima");
printf("\n Additional Keywords recognized in the inputfile trace_irc:\n");
printf(" SPHERICAL: Spherical hills are being used \n");
printf(" GAUSSCUT: the gaussian cut-off parameter is read from the next\n");
printf(" line (default: 10^8)\n");
printf(" BRACKETSIZE: Respectively the bracketsize and the stepsize are\n");
printf(" read from the next line (used to find the course grid\n");
printf(" points) (default values: 0.2 0.2). The first number\n");
printf(" sets a distance right and left from an initial guess\n");
printf(" between which the energy is minimized for localizing\n");
printf(" the minima and the irc points. The second number sets\n");
printf(" the distance between the initial course irc points.\n");
printf(" NSMALLSTEPS: Number of points to generate in between the course\n");
printf(" grid that is traced first (default 10 points). The minimum\n");
printf(" is 1 small step per segment, taking just the coarse grid points.\n");
printf(" NSIDEPOINTS: Number of points to generate outside the two minima\n");
printf(" with the fine grid spacing (default 10 points)\n");
printf(" (NB this spacing equals the stepsize (see BRACKETSIZE) or the\n");
printf(" distance between the minima (which ever smaller) divided by \n");
printf(" NSMALLSTEPS, but the final distance between the found points\n");
printf(" is NOT fixed (only their projection on the CG vectors is)).\n");
printf(" TEDGECONST: if set then the NSIDEPOINTS have the same energy as the\n");
printf(" minima so that the force will vanish outside the minima\n");
printf(" SMOOTHFACTOR: 2 gaussian widths are read for smoothing respec-\n");
printf(" tively the points along the IRC and the energy (required\n");
printf(" to compute smooth forces (default no smoothing)\n");
printf(" TNOMINOPT: if set then no optimization of the minima \n");
printf(" TNOTRACE: if set then locate minima only\n");
printf(" TMONTECARLO: performs the integration of the free energy surface\n");
printf(" over the nvar-1 directions perpendicular to the IRC\n");
printf(" and an effective umbrella potential is computed instead\n");
printf(" of just the potential at the minimum path (IRC). Reads 1\n");
printf(" integer and 1 float from the next line for the number of \n");
printf(" accepted MC moves and the maximum stepsize\n");
printf(" FES_MOVIE: Interval in number of hills to write the evolution of the\n");
printf(" free energy profile along the final path\n");
printf("");
printf("");
printf("");
printf("\n\n======================================================================\n\n");
}
// -------------------------------------------------------------
double myrandom(idumm){
double random;
const double d2p31m=2147483647.0, d2p31=2147483648.0, d7p5=16807.0;
dseed=fmod(d7p5*dseed,d2p31m);
random=dseed/d2p31;
return random;
}
// -------------------------------------------------------------
/* radius */
double radius(double *x, double *y, int ndim){
int i;
double r=0.0, dx;
for(i=0;i<ndim;i++){
dx=x[i]-y[i];
r=r+dx*dx;
}
return sqrt(r);
}
// -------------------------------------------------------------
/* read input */
int read_input(HILLSPATH *hills, double **points, double *bracketsize, double *stepsize, int *nsteps, double *gausswidth, int *nside, int *tedgeconst, int *tminopt, int *ttrace, int *tmc, int *maxmcmoves, double *maxstep, double *drmax, double *mcminetot, char *colvarfile, char *hillfile,int *inputtype,int *nprint_fesmovie){
FILE *fpin;
char keyword[50], line[MAXLINE];
int i, nvar;
nvar=hills->nvar;
if( (fpin = fopen(INPUTFILE,"r")) == NULL){
printf("Cannot open %s \nStopped\n",INPUTFILE);
print_help();
return EXIT_FAILURE;
}
while( fgets(line,MAXLINE,fpin) != NULL ){
sscanf(strtok(line," "),"%s",keyword);
// printf("keyword: %s\n",keyword);
if( strcasecmp(keyword,"NVAR") == 0 ){
fscanf(fpin,"%i",&nvar);
printf(" Number of CVs: %d\n",nvar);
fgets(line,MAXLINE,fpin);
}
else if( strcasecmp(keyword,"MINIMA") == 0 ){
if(nvar==-1){
printf(" ERROR: Give NVAR before MINIMA in %s\n Stopped\n",INPUTFILE);
return EXIT_FAILURE;
}
fgets(line,MAXLINE,fpin);
sscanf(strtok(line," "),"%lf",&points[0][0]);
for(i=1;i<nvar;i++) sscanf(strtok(NULL," "),"%lf",&points[0][i]);
printf(" Guess minimim 1: %lg",points[0][0]);
for(i=1;i<nvar;i++) printf(", %lg",points[0][i]); printf("\n");
fgets(line,MAXLINE,fpin);
sscanf(strtok(line," "),"%lf",&points[1][0]);
for(i=1;i<nvar;i++) sscanf(strtok(NULL," "),"%lf",&points[1][i]);
printf(" Guess minimim 2: %lg",points[1][0]);
for(i=1;i<nvar;i++) printf(", %lg",points[1][i]); printf("\n");
}
else if( strcasecmp(keyword,"INPUT") == 0 ){
if(sscanf(strtok(NULL," "),"%s",keyword) != 1){
*inputtype=0; /* default is CPMD input */
fscanf(fpin,"%s%s",colvarfile,hillfile);
printf(" Using CPMD input from %s and %s\n",colvarfile,hillfile);
}else{
if(strcasecmp(keyword,"CPMD") == 0){
*inputtype=0;
fscanf(fpin,"%s%s",colvarfile,hillfile);
printf(" Using CPMD input from %s and %s\n",colvarfile,hillfile);
}else if(strcasecmp(keyword,"PLUMED") == 0){
*inputtype=1;
fscanf(fpin,"%s",hillfile);
printf(" Using PLUMED input from %s\n",hillfile);
}else if(strcasecmp(keyword,"POTENTIAL") == 0){
*inputtype=2;
fscanf(fpin,"%s",hillfile);
printf(" Using potential input from %s\n",hillfile);
}else{
printf(" ERROR: INPUT %s unknown (use CPMD, PLUMED, or POTENTIAL)\n",keyword);
return EXIT_FAILURE;
}
}
fgets(line,MAXLINE,fpin);
}
else if( strcasecmp(keyword,"GAUSSCUT") == 0 ){
fscanf(fpin,"%lf",&hills->cutgauss);
fgets(line,MAXLINE,fpin);
}
else if( strcasecmp(keyword,"SPHERICAL") == 0 ){
hills->tspherical=1;
}
else if( strcasecmp(keyword,"SYMMETRY") == 0 ){
hills->tsym=1;
if( sscanf(&line[9],"%d",&hills->isym) != 1 ){
hills->isym=-1;
printf(" All symmetry equivalent hills are used\n");
}else{
printf(" Only hills of symmetry point %d are used\n",hills->isym);
}
}
else if( strcasecmp(keyword,"BRACKETSIZE") == 0 ){
fscanf(fpin,"%lf%lf",bracketsize,stepsize);
printf(" Bracket and step size: %lf %lf\n",*bracketsize,*stepsize);
fgets(line,MAXLINE,fpin);
}
else if( strcasecmp(keyword,"NSMALLSTEPS") == 0 ){
fscanf(fpin,"%d",nsteps);
if( *nsteps < 1) *nsteps=1;
printf(" Number of small steps: %d\n",*nsteps);
fgets(line,MAXLINE,fpin);
}
else if( strcasecmp(keyword,"NSIDEPOINTS") == 0 ){
fscanf(fpin,"%d",nside);
printf(" Number of flanking points: %d\n",*nside);
fgets(line,MAXLINE,fpin);
}
else if( strcasecmp(keyword,"TEDGECONST") == 0 ){
*tedgeconst=1;
}
else if( strcasecmp(keyword,"SMOOTHFACTOR") == 0 ){
fscanf(fpin,"%lf%lf",&gausswidth[0],&gausswidth[1]);
printf(" Smooth factors: %lf %lf\n",gausswidth[0],gausswidth[1]);
fgets(line,MAXLINE,fpin);
}
else if( strcasecmp(keyword,"TNOMINOPT") == 0 ){
*tminopt=0;
}
else if( strcasecmp(keyword,"TNOTRACE") == 0 ){
*ttrace=0;
}
else if( strcasecmp(keyword,"TMONTECARLO") == 0 ){
*tmc=1;
fscanf(fpin,"%d%lf",maxmcmoves,maxstep);
fgets(line,MAXLINE,fpin);
}
else if( strcasecmp(keyword,"MCMAXVALLEYWIDTH") == 0 ){
fscanf(fpin,"%lf",drmax);
fgets(line,MAXLINE,fpin);
}
else if( strcasecmp(keyword,"MCMINETOT") == 0 ){
fscanf(fpin,"%lf",mcminetot);
fgets(line,MAXLINE,fpin);
}
else if( strcasecmp(keyword,"NEWCODE") == 0 ){
hills->tnewcode=1;
}
else if( strcasecmp(keyword,"FES_MOVIE") == 0 ){
fscanf(fpin,"%d",nprint_fesmovie);
fgets(line,MAXLINE,fpin);
}
else if( strcasecmp(keyword,"NGRID") == 0 ){
fgets(line,MAXLINE,fpin);
if( sscanf(line,"%d%d",&hills->nx1,&hills->nx2) != 2){
printf("\n ERROR: failed to read two integers after the NGRID keyword.\n\n");
exit(1);
}
printf(" Potential grid size: %d x %d\n",hills->nx1,hills->nx2);
}
else{
printf(" Non-fatal warning: Keyword not recognized: %s \n",keyword);
// return EXIT_FAILURE;
}
}
hills->nvar = nvar;
fclose(fpin);
/* Sanity checks */
if( (*inputtype==2) && (hills->nx2==-1) ){
printf("\n ERROR: input type POTENTIAL selected but no NGRID specified.\n\n");
exit(1);
}
/* Some more feedback */
if(hills->tspherical){
printf(" Spherical hills: yes\n");
}else{
printf(" Spherical hills: no\n");
}
if(*tedgeconst){
printf(" Sides constant energy: yes\n");
}else{
printf(" Sides constant energy: no\n");
}
if(gausswidth[0]==-999.){
printf(" Smooting of path: no\n");
}
if(gausswidth[1]==-999.){
printf(" Smooting of energy: no\n");
}
if(*tminopt){
printf(" Optimize minima: yes\n");
}else{
printf(" Optimize minima: no\n");
}
if(*ttrace){
printf(" Trace path: yes\n");
}else{
printf(" Trace path: no\n");
}
if(*nprint_fesmovie > 0){
printf(" Write FES with interval of %d HILLS\n",*nprint_fesmovie);
}
if(*tmc){
printf(" MC integration: yes\n");
printf(" Max moves and stepsize: %d, %lf",*maxmcmoves,*maxstep);
}else{
printf(" MC integration: no\n");
}
return EXIT_SUCCESS;
}
// -------------------------------------------------------------
/* open colvar_val and dcolvar_val to read the hills */
int read_hills_from_CPMDfile(HILLSPATH *hills, char *colvarfile,char *hillfile){
FILE *fpcolvar, *fpdcolvar;
char line[MAXLINE];
int i, ihill, istep, nvar, ndummy,isym;
double dummy;
nvar=hills->nvar;
if( (fpcolvar = fopen(colvarfile,"r")) == NULL){
printf("\n ERROR: Failed to open colvar file %s \nStopped\n\n",colvarfile);
return EXIT_FAILURE;
}
if( (fpdcolvar = fopen(hillfile,"r")) == NULL){
printf("\n ERROR: Failed to open hills file %s \nStopped\n\n",hillfile);
return EXIT_FAILURE;
}
/* allocate memory for the colvar trajectory and scale factors and read them in */
ihill=0;
hills->cvpath = (double **) malloc(nvar*sizeof(double *));
for(i=0;i<nvar;i++) hills->cvpath[i] = (double *) malloc(NHILLSMAX*sizeof(double));
hills->scale = (double **) malloc(nvar*sizeof(double *));
for(i=0;i<nvar;i++) hills->scale[i] = (double *) malloc(NHILLSMAX*sizeof(double));
while( fgets(line,MAXLINE,fpcolvar) != NULL ){
sscanf(strtok(line," "),"%d",&istep);
if(hills->tsym) sscanf(strtok(NULL," "),"%d",&isym);
if( (hills->isym==-1) || (hills->isym==isym) ){
for(i=0;i<nvar;i++){
sscanf(strtok(NULL," "),"%lg",&hills->cvpath[i][ihill]);
}
for(i=0;i<nvar;i++){
sscanf(strtok(NULL," "),"%lg",&hills->scale[i][ihill]);
}
ihill++;
if(ihill >= NHILLSMAX){
printf("Number of hills to read in more than %d\n",NHILLSMAX);
printf("Increase MAXNHILLS\nProgram stopped\n");
return EXIT_FAILURE;
}
}
}
hills->nhills=ihill;
printf(" Number of hills: %d\n",hills->nhills);
/* allocate memory for the hill height and width and read them in */
if(hills->tnewcode){
ndummy=1;
} else{
ndummy=nvar+1;
}
hills->hllw = (double *) malloc(hills->nhills*sizeof(double));
hills->hllh = (double *) malloc(hills->nhills*sizeof(double));
for(ihill=0;ihill<hills->nhills;){
fgets(line,MAXLINE,fpdcolvar);
sscanf(strtok(line," "),"%d",&istep);
if(hills->tsym) sscanf(strtok(NULL," "),"%d",&isym);
if( (hills->isym==-1) || (hills->isym==isym) ){
for(i=0;i<ndummy;++i) sscanf(strtok(NULL," "),"%lg",&dummy);
sscanf(strtok(NULL," "),"%lg",&hills->hllw[ihill]);
sscanf(strtok(NULL," "),"%lg",&hills->hllh[ihill]);
ihill++;
}
}
fclose(fpdcolvar);
fclose(fpcolvar);
printf(" Hill width and heigth of last hill are %lf, %lf\n",hills->hllw[ihill-1],hills->hllh[ihill-1]);
return EXIT_SUCCESS;
}
// -------------------------------------------------------------
/* open PLUMED HILLS file to read the hills */
int read_hills_from_PLUMEDfile(HILLSPATH *hills,char *hillfile){
FILE *fphills;
char line[MAXLINE], *pt;
int i, ihill, nvar, ndummy,isym, nhills;
double dummy, time;
nvar=hills->nvar;
if( (fphills = fopen(hillfile,"r")) == NULL){
printf("\n ERROR: Failed to open hills file %s \nStopped\n\n",hillfile);
return EXIT_FAILURE;
}
/* count number of hills */
nhills=0;
while( fgets(line,MAXLINE,fphills) != NULL ){
pt=strtok(line," "); /* skip comment lines starting with # */
if( pt[0] !='#' ) nhills++;
}
// printf(" Number of hills: %d\n",nhills);
rewind(fphills);
/* allocate memory for the hills trajectory and scale factors */
ihill=0;
hills->cvpath = (double **) malloc(nvar*sizeof(double *));
for(i=0;i<nvar;i++) hills->cvpath[i] = (double *) malloc(nhills*sizeof(double));
hills->scale = (double **) malloc(nvar*sizeof(double *));
for(i=0;i<nvar;i++) hills->scale[i] = (double *) malloc(nhills*sizeof(double));
hills->hllw = (double *) malloc(nhills*sizeof(double));
hills->hllh = (double *) malloc(nhills*sizeof(double));
/* read hills */
while( fgets(line,MAXLINE,fphills) != NULL ){
pt=strtok(line," ");
if( pt[0] !='#' ){ /* skip comment lines starting with # */
sscanf(pt,"%lg",&time);
if(hills->tsym) sscanf(strtok(NULL," "),"%d",&isym);
if( (hills->isym==-1) || (hills->isym==isym) ){
for(i=0;i<nvar;i++){
sscanf(strtok(NULL," "),"%lg",&hills->cvpath[i][ihill]);
}
for(i=0;i<nvar;i++){
sscanf(strtok(NULL," "),"%lg",&hills->scale[i][ihill]);
}
sscanf(strtok(NULL," "),"%lg",&hills->hllh[ihill]);
hills->hllw[ihill] = 1.0;
ihill++;
}
}
}
hills->nhills=ihill;
printf(" Number of hills: %d\n",hills->nhills);
fclose(fphills);
printf(" Hill heigth and widths of last hill are %lf and %lf",hills->hllh[ihill-1],hills->scale[0][ihill-1]);
for(i=1;i<nvar;i++) printf(", %lf",hills->scale[i][ihill-1]);
printf("\n");
return EXIT_SUCCESS;
}
// -------------------------------------------------------------
/* open POTENTIAL file and read input data */
int read_2Dpotential(HILLSPATH *hills,char *potfile){
int i, ix, iy, nx1, nx2;
double dx1, dx2;
FILE *fpin;
/* allocate memory */
nx1=hills->nx1;
nx2=hills->nx2;
hills->x1pot = (double *)malloc(nx1*sizeof(double));
hills->x2pot = (double *)malloc(nx2*sizeof(double));
hills->extpot = (double **)malloc(nx1*sizeof(double *));
hills->extdvxx = (double **)malloc(nx1*sizeof(double *));
hills->extdvx1 = (double **)malloc(nx1*sizeof(double *));
hills->extdvx2 = (double **)malloc(nx1*sizeof(double *));
hills->extpot[0] = (double *)malloc(nx1*nx2*sizeof(double));
hills->extdvxx[0] = (double *)malloc(nx1*nx2*sizeof(double));
hills->extdvx1[0] = (double *)malloc(nx1*nx2*sizeof(double));
hills->extdvx2[0] = (double *)malloc(nx1*nx2*sizeof(double));
for(i=1;i<nx1;i++){
hills->extpot[i] = hills->extpot[i-1] + nx2;
hills->extdvxx[i] = hills->extdvxx[i-1] + nx2;
hills->extdvx1[i] = hills->extdvx1[i-1] + nx2;
hills->extdvx2[i] = hills->extdvx2[i-1] + nx2;
}
/* open file */
if( (fpin = fopen(potfile,"r")) == NULL){
printf("\n ERROR: Failed to open potential file %s \nStopped\n\n",potfile);
return EXIT_FAILURE;
}
/* read first line and get grid origin */
if( fscanf(fpin,"%lf%lf%lf",&hills->x1pot[0],&hills->x2pot[0],&hills->extpot[0][0]) != 3){
printf("\n ERROR: Failed to read first line from potential file %s\n\n",potfile);
exit(1);
}
rewind(fpin);
/* read file */
for(ix=0;ix<nx1;ix++){
for(iy=0;iy<nx2;iy++){
if( fscanf(fpin,"%lf%lf%lf",&hills->x1pot[ix],&hills->x2pot[iy],&hills->extpot[ix][iy]) != 3){
printf("\n ERROR: Failed while reading line %d from potential file %s\n\n",ix*iy,potfile);
exit(1);
}
}
}
/* negate the potential (contrary to metadynamics Gaussians, external potentials are typically negative valued) */
for(ix=0;ix<nx1;ix++){
for(iy=0;iy<nx2;iy++){
hills->extpot[ix][iy] *= -1.0;
}
}
/* some feedback */
dx1 = (hills->x1pot[nx1-1]-hills->x1pot[0])/(nx1-1);
dx2 = (hills->x2pot[nx2-1]-hills->x2pot[0])/(nx2-1);
printf(" Potential grid origin: %lf %lf\n",hills->x1pot[0],hills->x2pot[0]);
printf(" Potential grid interval: %lf %lf\n",dx1,dx2);
fclose(fpin);
return EXIT_SUCCESS;
}
// -------------------------------------------------------------
/* allocate a double matrix with subscript range m[nrl..nrh][ncl..nch] */
double **dmatrix(int nrl,int nrh,int ncl,int nch)
{
int i,nrow,ncol;
double **m;
nrow = nrh-nrl+1; ncol = nch-ncl+1;
if(nrow*ncol==0) return (double **)NULL;
/* allocate pointers to rows */
m = (double **)malloc(nrow*sizeof(double*));
if(!m){
fprintf(stderr,"ERROR: can't allocate memory for matrix (%lu bytes)\n",
nrow*sizeof(double*));
exit(1);
}
m -= nrl;
/* allocate rows */
m[nrl] = (double *)calloc(nrow*ncol,sizeof(double));
if(!m[nrl]){
fprintf(stderr,
"ERROR: can't allocate memory for arrays for matrix (%lu bytes)\n",
nrow*ncol*sizeof(double));
exit(1);
}
m[nrl] -= ncl;
/* set pointers to rows */
for(i=nrl+1;i<=nrh;i++) m[i] = m[i-1]+ncol;
return m;
}
// -------------------------------------------------------------
void free_dmatrix(double **m,int nrl,int nrh,int ncl,int nch)
{
free((char *)(m[nrl]+ncl)); free((char *)(m+nrl));
}
// -------------------------------------------------------------
void bcucof(double y[], double y1[], double y2[], double y12[], double d1, double d2,
double **c){
static int wt[16][16]=
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,
-3,0,0,3,0,0,0,0,-2,0,0,-1,0,0,0,0,
2,0,0,-2,0,0,0,0,1,0,0,1,0,0,0,0,
0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,
0,0,0,0,-3,0,0,3,0,0,0,0,-2,0,0,-1,
0,0,0,0,2,0,0,-2,0,0,0,0,1,0,0,1,
-3,3,0,0,-2,-1,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,-3,3,0,0,-2,-1,0,0,
9,-9,9,-9,6,3,-3,-6,6,-6,-3,3,4,2,1,2,
-6,6,-6,6,-4,-2,2,4,-3,3,3,-3,-2,-1,-1,-2,
2,-2,0,0,1,1,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,2,-2,0,0,1,1,0,0,
-6,6,-6,6,-3,-3,3,3,-4,4,2,-2,-2,-2,-1,-1,
4,-4,4,-4,2,2,-2,-2,2,-2,-2,2,1,1,1,1};
int l,k,j,i;
double xx,d1d2,cl[16],x[16];
d1d2=d1*d2;
for (i=1;i<=4;i++) {
x[i-1]=y[i];
x[i+3]=y1[i]*d1;
x[i+7]=y2[i]*d2;
x[i+11]=y12[i]*d1d2;
}
for (i=0;i<=15;i++) {
xx=0.0;
for (k=0;k<=15;k++) xx += wt[i][k]*x[k];
cl[i]=xx;
}
l=0;
for (i=1;i<=4;i++)
for (j=1;j<=4;j++) c[i][j]=cl[l++];
}
// -------------------------------------------------------------
void bcuint(double y[], double y1[], double y2[], double y12[], double x1l,
double x1u, double x2l, double x2u, double x1, double x2, double *ansy,
double *ansy1, double *ansy2){
void bcucof(double y[], double y1[], double y2[], double y12[], double d1,
double d2, double **c);
int i;
double t,u,d1,d2,**c;
c=dmatrix(1,4,1,4);
d1=x1u-x1l;
d2=x2u-x2l;
bcucof(y,y1,y2,y12,d1,d2,c);
if (x1u == x1l || x2u == x2l) {
fprintf(stderr,"Bad input in routine bcuint");
exit(1);
}
t=(x1-x1l)/d1;
u=(x2-x2l)/d2;
*ansy=(*ansy2)=(*ansy1)=0.0;
for (i=4;i>=1;i--) {
*ansy=t*(*ansy)+((c[i][4]*u+c[i][3])*u+c[i][2])*u+c[i][1];
*ansy2=t*(*ansy2)+(3.0*c[i][4]*u+2.0*c[i][3])*u+c[i][2];
*ansy1=u*(*ansy1)+(3.0*c[4][i]*t+2.0*c[3][i])*t+c[2][i];
}
*ansy1 /= d1;
*ansy2 /= d2;
free_dmatrix(c,1,4,1,4);
}
/*--------------------------------------------------------------*/
void get_2Dextpot_derivatives(HILLSPATH *hills){
int i, j;
double **extdvx1 = hills->extdvx1;
double **extdvx2 = hills->extdvx2;
double **extdvxx = hills->extdvxx;
double **extpot = hills->extpot;
double *x1pot = hills->x1pot;
double *x2pot = hills->x2pot;
for(i=0;i<hills->nx1;i++){
extdvx1[i][0] = extdvx1[i][hills->nx2-1] = 0.;
extdvx2[i][0] = extdvx2[i][hills->nx2-1] = 0.;
extdvxx[i][0] = extdvxx[i][hills->nx2-1] = 0.;
}
for(j=0;j<hills->nx2;j++){
extdvx1[0][j] = extdvx1[hills->nx1-1][j] = 0.;
extdvx2[0][j] = extdvx2[hills->nx1-1][j] = 0.;
extdvxx[0][j] = extdvxx[hills->nx1-1][j] = 0.;
}
for(i=1;i<hills->nx1-1;i++){
for(j=1;j<hills->nx2-1;j++){
extdvx1[i][j] = (extpot[i+1][j] - extpot[i-1][j]) / (x1pot[i+1] - x1pot[i-1]);
extdvx2[i][j] = (extpot[i][j+1] - extpot[i][j-1]) / (x2pot[j+1] - x2pot[j-1]);
extdvxx[i][j] = (extpot[i+1][j+1] - extpot[i+1][j-1] - extpot[i-1][j+1] + extpot[i-1][j-1])
/ ((x1pot[i+1] - x1pot[i-1]) * (x2pot[j+1] - x2pot[j-1]));
}
}
}
// -------------------------------------------------------------
double energy_pot(double *point, HILLSPATH *hills){
int i, j;
double y[5], y1[5], y2[5], y3[5], v, vdx1, vdx2;
double *x1pot = hills->x1pot;
double *x2pot = hills->x2pot;
double **extpot = hills->extpot;
double **extdvx1 = hills->extdvx1;
double **extdvx2 = hills->extdvx2;
double **extdvxx = hills->extdvxx;
/* find in which grid square the point is */
i = j = 0;
if((point[0] < x1pot[0]) || (point[0] > x1pot[hills->nx1-1]) || (point[1] < x2pot[0]) || (point[1] > x2pot[hills->nx2-1])){
/* pcolvar is off the potential grid where the external force is zero */
return 0.0;
}
i = j = 1;
while(point[0] > x1pot[i]) i++;
while(point[1] > x2pot[j]) j++;
i--;
j--;
y[1] = extpot[i][j];
y[2] = extpot[i+1][j];
y[3] = extpot[i+1][j+1];
y[4] = extpot[i][j+1];
y1[1] = extdvx1[i][j];
y1[2] = extdvx1[i+1][j];
y1[3] = extdvx1[i+1][j+1];
y1[4] = extdvx1[i][j+1];
y2[1] = extdvx2[i][j];
y2[2] = extdvx2[i+1][j];
y2[3] = extdvx2[i+1][j+1];
y2[4] = extdvx2[i][j+1];
y3[1] = extdvxx[i][j];
y3[2] = extdvxx[i+1][j];
y3[3] = extdvxx[i+1][j+1];
y3[4] = extdvxx[i][j+1];
bcuint(y,y1,y2,y3,x1pot[i],x1pot[i+1],x2pot[j],x2pot[j+1],
point[0],point[1],&v,&vdx1,&vdx2);
#ifdef DEBUG
printf("punt %d (%d,%d): x=%lf y=%lf E=%lf (%lf %lf %lf)\n",1,i,j,x1pot[i],x2pot[j],y[1],y1[1],y2[1],y3[1]);
printf("punt %d (%d,%d): x=%lf y=%lf E=%lf (%lf %lf %lf)\n",2,i+1,j,x1pot[i+1],x2pot[j],y[2],y1[2],y2[2],y3[2]);
printf("punt %d (%d,%d): x=%lf y=%lf E=%lf (%lf %lf %lf)\n",3,i+1,j+1,x1pot[i+1],x2pot[j+1],y[3],y1[3],y2[3],y3[3]);
printf("punt %d (%d,%d): x=%lf y=%lf E=%lf (%lf %lf %lf)\n",4,i,j+1,x1pot[i],x2pot[j+1],y[4],y1[4],y2[4],y3[4]);
printf("Potential at (%lf,%lf) = %g; Forces are %g, %g\n",point[0],point[1],v,vdx1,vdx2);
#endif
// colvar->fcolvar[0] -= vdx1;
// colvar->fcolvar[1] -= vdx2;
return v;
}
// -------------------------------------------------------------
double energy_meta(double *coord, HILLSPATH *hills){
double gperp, gperp_cut, gpar, expn, expn2, sigma2;
double diffpos[NVARMAX], deltacv[NVARMAX], stepgauss;
int i, ihill, nhills;
nhills=hills->nhills;
stepgauss=0.0;
if(hills->tspherical==1){
for(ihill=0;ihill<nhills-1;ihill++){
expn=0.0;
for(i=0;i<hills->nvar;i++){
diffpos[i]=(coord[i]-hills->cvpath[i][ihill])/hills->scale[i][ihill];
expn=expn+diffpos[i]*diffpos[i];
}
expn=sqrt(expn);
if(expn < hills->hllw[i]*hills->cutgauss){
gperp=exp(-0.5*pow(expn/hills->hllw[ihill],2.0));
gperp_cut=exp(-0.5*pow(hills->cutgauss,2.0));
stepgauss += hills->hllh[ihill]*(gperp-gperp_cut);
}
}
} else{
for(ihill=0;ihill<nhills-1;ihill++){
expn=0.0;
expn2=0.0;
sigma2=0.0;
for(i=0;i<hills->nvar;i++){
diffpos[i]=(coord[i]-hills->cvpath[i][ihill])/hills->scale[i][ihill];
deltacv[i]=(hills->cvpath[i][ihill+1]-hills->cvpath[i][ihill])/hills->scale[i][ihill];
expn=expn+diffpos[i]*diffpos[i];
expn2=expn2+diffpos[i]*deltacv[i];
sigma2=sigma2+deltacv[i]*deltacv[i];
}
expn=sqrt(expn);
sigma2=sigma2*sigma2;
if(sigma2 < 1.0e-6) sigma2=1.0e-6;
if(expn < hills->hllw[i]*hills->cutgauss){
gperp=exp(-0.5*pow(expn/hills->hllw[ihill],2.0));
gperp_cut=exp(-0.5*pow(hills->cutgauss,2.0));
gpar=hills->hllh[ihill]*exp(-0.5*(expn2*expn2/sigma2));
stepgauss += gpar*(gperp-gperp_cut);
}
}
}
return stepgauss;
}
// -------------------------------------------------------------
void print_point(FILE *fpout, HILLSPATH *hills, double *p){
int i;
fprintf(fpout,PRINTFORMAT,p[0]);
for(i=1;i<hills->nvar;i++) fprintf(fpout,PRINTFORMAT,p[i]);
fprintf(fpout,PRINTFORMAT,energy(p,hills));
fprintf(fpout,"\n");
fflush(fpout);
}
// -------------------------------------------------------------
void print_movie(HILLSPATH *hills, double **irc_traj,int totpoints, int nprint){
int i, ivar, nvar, ipoint, nhillstot;
char filename[MAXLINE];
FILE *fpout;
nhillstot = hills->nhills; /* temporary store the actual total number of hills */
hills->nhills = nprint;
nvar = hills->nvar;
while(hills->nhills <= nhillstot){
sprintf(filename,"fes_%d.out",hills->nhills);
printf(" Writing energy profile along path to file %s. Number of hills: %d\n",filename,hills->nhills);
fpout = fopen(filename,"w");
for(ipoint=1;ipoint<totpoints-1;ipoint++){ /* force in points between minima */
fprintf(fpout," %d",ipoint+1);
for(ivar=0;ivar<nvar;ivar++) fprintf(fpout," %lf",irc_traj[ipoint][ivar]);
fprintf(fpout," %lf\n",energy(irc_traj[ipoint],hills));
}
fclose(fpout);
hills->nhills += nprint;
}
hills->nhills = nhillstot; /* reset to the actual total number of hills */
return;
}
// -------------------------------------------------------------
int bracket_minimum(HILLSPATH *hills, double *min, double bracketsize){
double pointright[NVARMAX], pointleft[NVARMAX];
double Eright, Eleft, Emiddle;
int i, ivar, nvar, iconsistency;
nvar=hills->nvar;
iconsistency=1;
printf("============= Bracketing minimum ===============================\n");
printf("ivar left bracket E minimum Energy right bracket E \n");
while(iconsistency){
iconsistency=0;
ivar=0;
while(ivar<nvar){
for(i=0;i<nvar;i++)pointleft[i]=min[i] ;
for(i=0;i<nvar;i++)pointright[i]=min[i] ;
pointright[ivar] = pointright[ivar] + bracketsize;
pointleft[ivar] = pointleft[ivar] - bracketsize;
Eleft=energy(pointleft,hills);
Eright=energy(pointright,hills);
Emiddle=energy(min,hills);
printf(" %d %lf %lf %lf %lf %lf %lf\n",ivar,pointleft[ivar],Eleft,min[ivar],Emiddle,pointright[ivar],Eright);
if((Eleft>Emiddle)&&(Eright>Emiddle)){
printf("Bracketing failed.\nAdjust bracketsize.\nProgram stopped.\n");
return EXIT_FAILURE;
}
if(Eleft>Emiddle){
min[ivar] = pointleft[ivar];
iconsistency=1;
}
else if(Eright>Emiddle){
min[ivar] = pointright[ivar];
iconsistency=1;
}
else{
ivar++;
}
}
}
return EXIT_SUCCESS;
}
// -------------------------------------------------------------
void minimize_energy(HILLSPATH *hills, double *min, double bracketsize){
double pointright[NVARMAX], pointleft[NVARMAX], point[NVARMAX];
double step, Eright, Eleft, Emiddle, Eprevious;
int i, ihill, ivar, icycle, icycle2, nvar;
nvar=hills->nvar;
icycle2=0;
printf("============= Minimizing =======================================\n");
printf("cycle var.1 ");
for(ivar=1;ivar<nvar;ivar++) printf(" var.%d ",ivar+1);
printf(" Energy \n");
printf("> %d %lf",icycle2,min[0]);
for(i=1;i<nvar;i++) printf(" %lf",min[i]);
printf(" %lg\n",energy(min,hills));
Emiddle=0.0;
Eprevious=Emiddle-2.0*ETOL;
while( ((Emiddle-Eprevious) > ETOL) && (icycle2<100)){
Eprevious=Emiddle;
for(ivar=0;ivar<nvar;ivar++){
for(i=0;i<nvar;i++) pointleft[i] = min[i] ;
for(i=0;i<nvar;i++) pointright[i] = min[i] ;
for(i=0;i<nvar;i++) point[i] = min[i] ;
pointright[ivar] = pointright[ivar] + bracketsize;
pointleft[ivar] = pointleft[ivar] - bracketsize;
icycle=0;
while( (pointright[ivar]-pointleft[ivar]) > TOL){
Eleft=energy(pointleft,hills);
Eright=energy(pointright,hills);
Emiddle=energy(min,hills);
// printf("%d %d %d %lf %lf %lf %lf %lf %lf\n",icycle2,ivar,icycle,pointleft[ivar],Eleft,min[ivar],Emiddle,pointright[ivar],Eright);
if(Eleft>Eright){
step=0.5*(pointright[ivar] - min[ivar]);
point[ivar] = pointright[ivar] - step;
if(energy(point,hills)<Emiddle){
pointright[ivar]=point[ivar];
}
else{
pointleft[ivar]=min[ivar];
min[ivar]=point[ivar];
}
}
else{
step=0.5*(min[ivar]-pointleft[ivar]);
point[ivar] = pointleft[ivar] + step;
if(energy(point,hills)<Emiddle){
pointleft[ivar]=point[ivar];
}
else{
pointright[ivar]=min[ivar];
min[ivar]=point[ivar];
}
}
icycle++;
}
/* printf("%d %lf",icycle,point[0]);
for(i=1;i<nvar;i++) printf(" %lf",point[i]);
printf(" %lg\n",energy(point,hills)); */
}
printf("> %d %lf",icycle2+1,min[0]);
for(i=1;i<nvar;i++) printf(" %lf",min[i]);
printf(" %lg\n",energy(min,hills));
icycle2++;
}
}
// -------------------------------------------------------------
int bracket_minimum_onsphere(HILLSPATH *hills, double *min, double* center, double stepsize, double bracketsize, int ivar ,FILE *fpout){
double pright[NVARMAX], pleft[NVARMAX];
double Eright, Eleft, Emiddle, fact;
int i, nvar, icycle;
nvar=hills->nvar;
icycle=0;
// printf("============= Bracketing minimum on sphere =====================\n");
// printf("ivar left bracket E minimum Energy right bracket E \n");
while(icycle<MAXCYCLE){
icycle++;
/* make right bracket point */
for(i=0;i<nvar;i++) pright[i]=min[i] ;
pright[ivar] = pright[ivar] + bracketsize;
fact=stepsize/radius(pright,center,nvar);
for(i=0;i<nvar;i++) pright[i]= center[i]+(pright[i]-center[i])*fact;
// print_point(fpout,hills,pright);
/* make left bracket point */
for(i=0;i<nvar;i++) pleft[i]=min[i] ;
pleft[ivar] = pleft[ivar] - bracketsize;
// print_point(fpout,hills,pleft);
fact=stepsize/radius(pleft,center,nvar);
for(i=0;i<nvar;i++) pleft[i]= center[i]+(pleft[i]-center[i])*fact;
// print_point(fpout,hills,pleft);
Eleft=energy(pleft,hills);
Eright=energy(pright,hills);
Emiddle=energy(min,hills);
// printf(" %d %lf %lf %lf %lf %lf %lf\n",ivar,pleft[ivar],Eleft,min[ivar],Emiddle,pright[ivar],Eright);
if((Eleft<Emiddle)&&(Eright<Emiddle)){
return EXIT_SUCCESS;
}
else{
if(Eleft>Eright){
for(i=0;i<nvar;i++) min[i] = pleft[i];
}
else {
for(i=0;i<nvar;i++) min[i] = pright[i];
}
}
}
printf("Needed more than %d cycles to find bracket on sphere\n",MAXCYCLE);
printf("Leaving ivar=%d for now and continuing with next dimension..\n",ivar);
return EXIT_FAILURE;
}
// -------------------------------------------------------------
void minimize_energy_onsphere(HILLSPATH *hills, double *min, double *center, double stepsize, double bracketsize, int ivar, FILE *fpout){
double pright[NVARMAX], pleft[NVARMAX], point[NVARMAX], Epoint;
double step, Eright, Eleft, Emiddle, fact;
int i, ihill, icycle, nvar;
nvar=hills->nvar;
Emiddle=0.0;
for(i=0;i<nvar;i++) {
pleft[i] = min[i] ; pright[i] = min[i] ; point[i] = min[i] ;
}
/* make right bracket point on sphere */
pright[ivar] = pright[ivar] + bracketsize;
// print_point(fpout,hills,pright);
fact=stepsize/radius(pright,center,nvar);
for(i=0;i<nvar;i++) pright[i]= center[i]+(pright[i]-center[i])*fact;
// if(ivar==0) print_point(fpout,hills,pright);
/* make left bracket point on sphere */
pleft[ivar] = pleft[ivar] - bracketsize;
// print_point(fpout,hills,pleft);
fact=stepsize/radius(pleft,center,nvar);
for(i=0;i<nvar;i++) pleft[i]= center[i]+(pleft[i]-center[i])*fact;
// if(ivar==0) print_point(fpout,hills,pleft);
Eleft=energy(pleft,hills);
Eright=energy(pright,hills);
Emiddle=energy(min,hills);
icycle=0;
while( ( fabs(pright[ivar]-pleft[ivar]) > TOL) && (icycle < 10000) ){
// printf("%d %d %lf %lf %lf %lf %lf %lf\n",ivar,icycle,pleft[ivar],Eleft,min[ivar],Emiddle,pright[ivar],Eright);
if(Eleft>Eright){ /* pick new point between pleft and min */
step=0.5*(pright[ivar] - min[ivar]);
point[ivar] = pright[ivar] - step;
fact=stepsize/radius(point,center,nvar);
for(i=0;i<nvar;i++) point[i]= center[i]+(point[i]-center[i])*fact;
// if(ivar==0)print_point(fpout,hills,point);
Epoint = energy(point,hills);
if(Epoint<Emiddle){
for(i=0;i<nvar;i++) pright[i]=point[i];
Eright=Epoint;
}
else{
for(i=0;i<nvar;i++){
pleft[i]=min[i];
min[i]=point[i];