forked from toby1998/MoRiBS-PIMC
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mc_piqmc.cc
executable file
·2173 lines (1718 loc) · 62.8 KB
/
mc_piqmc.cc
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
// LIMITATION : only one atom type and one molecule type at this point
#include "mc_input.h"
#include "mc_confg.h"
#include "mc_const.h"
#include "mc_setup.h"
#include "mc_randg.h"
#include "mc_utils.h"
#include "mc_poten.h"
#include "mc_piqmc.h"
#include "mc_qworm.h"
#include "mc_estim.h"
//#include <mpi.h>
#include <omp.h>
#include <math.h>
#include "rngstream.h"
#include "omprng.h"
// counters
double ** MCTotal; // MC counters (total number of moves)
double ** MCAccep; // MC counters (number of accepted moves)
// counters for parallel 3d rotation move. They are supposed to be declared for all CPUs
double MCRotChunkAcp; // total number of rotational moves for one chunk loop
double MCRotChunkTot; // total accept number of rotational moves for one chunk loop
double MCRotTot; // the sum of all MCRotChunkTot from all CPUs
double MCRotAcp; // the sum of all MCRotChunkAcp from all CPUs
extern "C" void rotden_(double *Eulan1,double *Eulan2,double *Eulrel,double *rho,double *erot,double *esq,double *rhoprp, double *erotpr,double *erotsq,int *istop); // external fortran subroutine by Toby
extern "C" void vcord_(double *Eulang, double *RCOM, double *Rpt, double *vtable, int *Rgrd, int *THgrd, int *CHgrd, double *Rvmax, double *Rvmin, double *Rvstep, double *vpot3d, double *radret, double *theret, double *chiret, double *hatx, double *haty, double *hatz, int *ivcord);
extern "C" void rsrot_(double *Eulan1,double *Eulan2,double *X_Rot,double *Y_Rot,double *Z_Rot,double *tau,int *iodevn,double *eoff,double *rho,double *erot);
extern "C" void rsline_(double *X_Rot,double *p0,double *tau,double *rho,double *erot);
extern "C" void vspher_(double *r,double *vpot);
extern "C" void reflec_(double *coord,double *rcom,double *hatx,double *haty,double *hatz);
extern "C" void rflmfx_(double *RCOM,double *hatx,double *haty,double *hatz,double *Eulang);
extern "C" void rflmfy_(double *RCOM,double *hatx,double *haty,double *hatz,double *Eulang);
extern "C" void rflmfz_(double *RCOM,double *hatx,double *haty,double *hatz,double *Eulang);
// GG ---> potentiel H2O ---- H2O
extern "C" void caleng_(double *com_1, double *com_2, double *E_2H2O, double *Eulang_1, double *Eulang_2);
int PrintYrfl; // integer flag for printing reflected coordinates
int PrintXrfl; // integer flag for printing reflected coordinates
int PrintZrfl; // integer flag for printing reflected coordinates
void MCMolecularMove(int type)
{
int numb = MCAtom[type].numb;
double disp[NDIM];
for (int atom=0;atom<numb;atom++)
{
int offset = MCAtom[type].offset + NumbTimes*atom;
int gatom = offset/NumbTimes;
for (int id=0;id<NDIM;id++) // MOVE
disp[id] = MCAtom[type].mcstep*(rnd1()-0.5);
for (int id=0;id<NDIM;id++) // MOVE
{
#pragma omp parallel for
for (int it=0;it<NumbTimes;it++)
{
newcoords[id][offset+it] = MCCoords[id][offset+it];
newcoords[id][offset+it] += disp[id];
}
}
double deltav = 0.0; // ACCEPT/REJECT
deltav += (PotEnergy(gatom,newcoords)-PotEnergy(gatom,MCCoords));
bool Accepted = false;
if (deltav<0.0) Accepted = true;
else if
(exp(-deltav*MCTau)>rnd2()) Accepted = true;
MCTotal[type][MCMOLEC] += 1.0;
if (Accepted)
{
MCAccep[type][MCMOLEC] += 1.0;
for (int id=0;id<NDIM;id++) // save accepted configuration
{
#pragma omp parallel for
for (int it=0;it<NumbTimes;it++)
MCCoords[id][offset+it] = newcoords[id][offset+it];
}
}
} // END sum over atoms (fixed atom type)
}
void MCMolecularMoveExchange(int type)
// particular atom type
{
#ifdef DEBUG_PIMC
const char *_proc_ = __func__; // MCMolecularMoveExchange(int)
if (type != BSTYPE)
nrerror(_proc_,"Wrong atom type");
#endif
bool Accepted;
double disp[NDIM];
int numb = MCAtom[type].numb;
for (int atom=0;atom<numb;atom++)
_pflags[atom] = 0;
for (int atom=0;atom<numb;atom++)
if (_pflags[atom] == 0) // start a new cycle
{
int amax = 0; // number of atoms in the current
atom_list[amax] = atom; // exchange loop
amax ++;
int patom = PIndex[atom];
while (patom != atom) // count all atoms involved
{ // in current exchange loop
atom_list[amax] = patom;
amax ++;
_pflags[patom] = 1;
patom = PIndex[patom];
}
for (int id=0;id<NDIM;id++) // MOVE
disp[id] = MCAtom[type].mcstep*(rnd1()-0.5);
for (int ia=0;ia<amax;ia++) // loop over atoms in the current
{ // exchange loop
int offset = MCAtom[type].offset + NumbTimes*atom_list[ia];
int gatom = offset/NumbTimes;
for (int id=0;id<NDIM;id++) // MOVE
{
#pragma omp parallel for
for (int it=0;it<NumbTimes;it++)
{
newcoords[id][offset+it] = MCCoords[id][offset+it];
newcoords[id][offset+it] += disp[id];
}
}
double deltav = 0.0; // ACCEPT/REJECT
deltav += (PotEnergy(gatom,newcoords)-PotEnergy(gatom,MCCoords));
Accepted = false;
if (deltav<0.0) Accepted = true;
else if
(exp(-deltav*MCTau)>rnd2()) Accepted = true;
if (!Accepted) break;
} // END sum over atoms in the current exchange loop
MCTotal[type][MCMOLEC] += 1.0;
if (Accepted)
{
MCAccep[type][MCMOLEC] += 1.0;
for (int ia=0;ia<amax;ia++) // loop over atoms in the current
{ // exchange loop
int offset = MCAtom[type].offset + NumbTimes*atom_list[ia];
for (int id=0;id<NDIM;id++) // save accepted configuration
{
#pragma omp parallel for
for (int it=0;it<NumbTimes;it++)
MCCoords[id][offset+it] = newcoords[id][offset+it];
}
}
}
} // END sum over atoms (fixed atom type)
}
void MCBisectionMove(int type, int time) // multilevel Metropolis
{
int numb = MCAtom[type].numb;
double mclambda = MCAtom[type].lambda;
int mclevels = MCAtom[type].levels; // number of levels
int seg_size = MCAtom[type].mlsegm; // segmen size
for (int atom=0;atom<numb;atom++) // one atom to move only
{
int offset = MCAtom[type].offset + NumbTimes*atom;
int gatom = offset/NumbTimes;
// initialize the end points
int pit = (time+seg_size) % NumbTimes; // periodicity in time
for (int id=0;id<NDIM;id++)
{
newcoords[id][offset + time] = MCCoords[id][offset + time];
newcoords[id][offset + pit] = MCCoords[id][offset + pit];
}
double bnorm = 1.0/(mclambda*MCTau); // variance for gaussian sampling
bool Accepted;
int t0,t1,t2;
double pot0 = 0.0; // potential, current level
double pot1 = 0.0; // potential, previous level
for (int level=0;level<mclevels;level++) // loop over bisection levels
{
int level_seg_size = (int)pow(2.0,(mclevels-level));
double bkin_norm = bnorm/(double) level_seg_size;
double bpot_norm = MCTau*(double)(level_seg_size/2);
pot1 = pot0; // swap level potentials
pot0 = 0.0;
t2 = 0;
do // loop over middle points
{
t0 = t2; // left point
t2 = t0 + level_seg_size; // right point
t1 = (t0 + t2)/2; // middle point
int pt0 = (time + t0) % NumbTimes;
int pt1 = (time + t1) % NumbTimes;
int pt2 = (time + t2) % NumbTimes;
// change the offset if exchange
for (int id=0;id<NDIM;id++)
{
newcoords[id][offset+pt1] = 0.5*(newcoords[id][offset+pt0]+newcoords[id][offset+pt2]);
newcoords[id][offset+pt1] += gauss(bkin_norm);
}
//---------------------------- the end point approximation
pot0 += (PotEnergy(gatom,newcoords,pt1) - PotEnergy(gatom,MCCoords,pt1));
if (t0!=0) // skip the contributions of the end points
pot0 += (PotEnergy(gatom,newcoords,pt0) - PotEnergy(gatom,MCCoords,pt0));
}
while (t2<seg_size); // end the loop over middle points
// inefficient version
double deltav = (pot0-2.0*pot1); // rho(0,1;tau)
deltav *= bpot_norm;
Accepted = false;
if (deltav<0.0) Accepted = true;
else if (exp(-deltav)>rnd3()) Accepted = true;
if (!Accepted) break;
} // END loop over levels
MCTotal[type][MCMULTI] += 1.0;
if (Accepted)
{
MCAccep[type][MCMULTI] += 1.0;
for (int id=0;id<NDIM;id++) // save new coordinates
for (int it=time;it<=(time+seg_size);it++)
{
int pit = it % NumbTimes; // periodicity in time
MCCoords[id][offset+pit] = newcoords[id][offset+pit];
}
}
//-----------------------------------------------------------------------
// END bisection
//-----------------------------------------------------------------------
} // END loop over time slices/atoms
}
void MCBisectionMoveExchange(int type, int time0) // multilevel Metropolis
{
int numb = MCAtom[type].numb;
double mclambda = MCAtom[type].lambda;
int mclevels = MCAtom[type].levels; // number of levels
int seg_size = MCAtom[type].mlsegm; // segment size
for (int atom=0;atom<numb;atom++) // one atom to move only
{
int offset0 = MCAtom[type].offset + NumbTimes*atom;
int offset1 = offset0;
int time1 = time0 + seg_size; // the end of the segment
int timep = time1 % NumbTimes;
if (timep != time1)
offset1 = MCAtom[type].offset + NumbTimes*PIndex[atom];
for (int id=0;id<NDIM;id++)
{
newcoords[id][offset0 + time0] = MCCoords[id][offset0 + time0];
newcoords[id][offset1 + timep] = MCCoords[id][offset1 + timep];
}
double bnorm = 1.0/(mclambda*MCTau); // variance for gaussian sampling
bool Accepted;
int t0,t1,t2;
double pot0 = 0.0; // potential, current level
double pot1 = 0.0; // potential, previous level
for (int level=0;level<mclevels;level++) // loop over bisection levels
{
int level_seg_size = (int)pow(2.0,(mclevels-level));
double bkin_norm = bnorm/(double) level_seg_size;
double bpot_norm = MCTau*(double)(level_seg_size/2);
pot1 = pot0; // swap level potentials
pot0 = 0.0;
t2 = 0;
do // loop over middle points
{
t0 = t2; // left point
t2 = t0 + level_seg_size; // right point
t1 = (t0 + t2)/2; // middle point
int pt0 = (time0 + t0) % NumbTimes;
int pt1 = (time0 + t1) % NumbTimes;
int pt2 = (time0 + t2) % NumbTimes;
int off0 = offset0;
int off1 = offset0;
int off2 = offset0;
if (pt0 != (time0 + t0))
off0 = offset1;
if (pt1 != (time0 + t1))
off1 = offset1;
if (pt2 != (time0 + t2))
off2 = offset1;
// change the offset if exchange
for (int id=0;id<NDIM;id++)
{
newcoords[id][off1+pt1] = 0.5*(newcoords[id][off0+pt0] + newcoords[id][off2+pt2]);
newcoords[id][off1+pt1] += gauss(bkin_norm);
}
//---------------------------- the end point approximation
int gatom0 = off0/NumbTimes;
int gatom1 = off1/NumbTimes;
pot0 += (PotEnergy(gatom0,newcoords,pt1) - PotEnergy(gatom0,MCCoords,pt1));
if (t0!=0) // skip the contributions of the end points
pot0 += (PotEnergy(gatom0,newcoords,pt0) - PotEnergy(gatom0,MCCoords,pt0));
}
while (t2<seg_size); // end the loop over middle points
// inefficient version
double deltav = (pot0-2.0*pot1); // rho(0,1;tau)
deltav *= bpot_norm;
Accepted = false;
if (deltav<0.0) Accepted = true;
else if (exp(-deltav)>rnd3()) Accepted = true;
if (!Accepted) break;
} // END loop over levels
MCTotal[type][MCMULTI] += 1.0;
if (Accepted)
{
MCAccep[type][MCMULTI] += 1.0;
for (int id=0;id<NDIM;id++) // save new coordinates
for (int it=time0;it<=time1;it++)
{
int pit = it % NumbTimes; // periodicity in time
int offset = offset0;
if (pit != it)
offset = offset1;
MCCoords[id][offset+pit] = newcoords[id][offset+pit];
}
}
//-----------------------------------------------------------------------
// END bisection
//-----------------------------------------------------------------------
} // END loop over time slices/atoms
}
void MCRotationsMove(int type) // update all time slices for rotational degrees of freedom
{
#ifdef DEBUG_PIMC
const char *_proc_=__func__; // MCRotationsMove()
if (type != IMTYPE)
nrerror(_proc_,"Wrong impurity type");
if (NDIM != 3)
nrerror(_proc_,"Rotational sampling for 3D systems only");
#endif
double step = MCAtom[type].rtstep;
int offset = MCAtom[type].offset;
int atom0 = 0; // only one molecular impurtiy
offset += (NumbTimes*atom0); // the same offset for rotational
int gatom = offset/NumbTimes; // and translational degrees of freedom
double MCRotChunkTot = 0.0;
double MCRotChunkAcp = 0.0;
RngStream Rng[omp_get_num_procs()]; // initialize a parallel RNG named "Rng"
double rand1,rand2,rand3;
/*
for (int it1=0;it1<NumbRotTimes;it1++)
{
rand1=runif(Rng);
rand2=runif(Rng);
rand3=runif(Rng);
MCRotLinStep(it1,offset,gatom,type,step,rand1,rand2,rand3,MCRotChunkTot,MCRotChunkAcp);
}
*/
/*
#pragma omp parallel reduction(+: MCRotChunkTot,MCRotChunkAcp) private(rand1,rand2,rand3)
{
int tid=omp_get_thread_num();
int itini=chunksize*tid;
int itfnl=itini+chunksize;
for (int itrot=itini;itrot<itfnl-1;itrot++)
{
rand1=runif(Rng);
rand2=runif(Rng);
rand3=runif(Rng);
MCRotLinStep(itrot,offset,gatom,type,step,rand1,rand2,rand3,MCRotChunkTot,MCRotChunkAcp);
}
} // end omp parallel
for (int itrot=chunksize-1;itrot<NumbRotTimes;itrot=itrot+chunksize)
{
rand1=runif(Rng);
rand2=runif(Rng);
rand3=runif(Rng);
MCRotLinStep(itrot,offset,gatom,type,step,rand1,rand2,rand3,MCRotChunkTot,MCRotChunkAcp);
}
for (int itrot=NThreads*chunksize;itrot<NumbRotTimes;itrot++)
{
rand1=runif(Rng);
rand2=runif(Rng);
rand3=runif(Rng);
MCRotLinStep(itrot,offset,gatom,type,step,rand1,rand2,rand3,MCRotChunkTot,MCRotChunkAcp);
}
MCTotal[type][MCROTAT] += MCRotChunkTot;
MCAccep[type][MCROTAT] += MCRotChunkAcp;
*/
#pragma omp parallel for reduction(+: MCRotChunkTot,MCRotChunkAcp) private(rand1,rand2,rand3)
for (int itrot=0;itrot<NumbRotTimes;itrot=itrot+2)
{
rand1=runif(Rng);
rand2=runif(Rng);
rand3=runif(Rng);
MCRotLinStep(itrot,offset,gatom,type,step,rand1,rand2,rand3,MCRotChunkTot,MCRotChunkAcp);
}
MCTotal[type][MCROTAT] += MCRotChunkTot;
MCAccep[type][MCROTAT] += MCRotChunkAcp;
MCRotChunkTot = 0;
MCRotChunkAcp = 0;
#pragma omp parallel for reduction(+: MCRotChunkTot,MCRotChunkAcp) private(rand1,rand2,rand3)
for (int itrot=1;itrot<NumbRotTimes;itrot=itrot+2)
{
rand1=runif(Rng);
rand2=runif(Rng);
rand3=runif(Rng);
MCRotLinStep(itrot,offset,gatom,type,step,rand1,rand2,rand3,MCRotChunkTot,MCRotChunkAcp);
}
MCTotal[type][MCROTAT] += MCRotChunkTot;
MCAccep[type][MCROTAT] += MCRotChunkAcp;
}
/*
void MCRotationsMove(int type) // update all time slices for rotational degrees of freedom
{
#ifdef DEBUG_PIMC
const char *_proc_=__func__; // MCRotationsMove()
if (type != IMTYPE)
nrerror(_proc_,"Wrong impurity type");
if (NDIM != 3)
nrerror(_proc_,"Rotational sampling for 3D systems only");
#endif
double step = MCAtom[type].rtstep;
int offset = MCAtom[type].offset;
int atom0 = 0; // only one molecular impurtiy
offset += (NumbTimes*atom0); // the same offset for rotational
int gatom = offset/NumbTimes; // and translational degrees of freedom
for (int it1=0;it1<NumbRotTimes;it1++)
{
int it0 = (it1 - 1);
int it2 = (it1 + 1);
if (it0<0) it0 += NumbRotTimes; // NumbRotTimes - 1
if (it2>=NumbRotTimes) it2 -= NumbRotTimes; // 0
int t0 = offset + it0;
int t1 = offset + it1;
int t2 = offset + it2;
double n1[NDIM];
double cost = MCAngles[CTH][t1];
double phi = MCAngles[PHI][t1];
cost += (step*(rnd1()-0.5));
phi += (step*(rnd1()-0.5));
if (cost > 1.0)
{
cost = 2.0 - cost;
// phi = phi + M_PI;
}
if (cost < -1.0)
{
cost = -2.0 - cost;
// phi = phi + M_PI;
}
double sint = sqrt(1.0 - cost*cost);
newcoords[AXIS_X][t1] = sint*cos(phi);
newcoords[AXIS_Y][t1] = sint*sin(phi);
newcoords[AXIS_Z][t1] = cost;
//----------------------------------------------
// the old density
double p0 = 0.0;
double p1 = 0.0;
for (int id=0;id<NDIM;id++)
{
p0 += (MCCosine[id][t0]*MCCosine[id][t1]);
p1 += (MCCosine[id][t1]*MCCosine[id][t2]);
}
double dens_old;
double rho1,rho2,erot;
if(RotDenType == 0)
{
dens_old = SRotDens(p0,type)*SRotDens(p1,type);
}
else if(RotDenType == 1)
{
rsline_(&X_Rot,&p0,&MCRotTau,&rho1,&erot);
rsline_(&X_Rot,&p1,&MCRotTau,&rho2,&erot);
dens_old = rho1+rho2;
}
if (fabs(dens_old)<RZERO) dens_old = 0.0;
if (dens_old<0.0 && RotDenType == 0) nrerror("Rotational Moves: ","Negative rot density");
double pot_old = 0.0;
int itr0 = it1 * RotRatio; // interval to average over
int itr1 = itr0 + RotRatio; // translational time slices
for (int it=itr0;it<itr1;it++) // average over tr time slices
pot_old += (PotRotEnergy(gatom,MCCosine,it));
// the new density
p0 = 0.0;
p1 = 0.0;
for (int id=0;id<NDIM;id++)
{
p0 += (MCCosine [id][t0]*newcoords[id][t1]);
p1 += (newcoords[id][t1]*MCCosine [id][t2]);
}
double dens_new;
if(RotDenType == 0)
{
dens_new = SRotDens(p0,type)*SRotDens(p1,type);
}
else if(RotDenType == 1)
{
rsline_(&X_Rot,&p0,&MCRotTau,&rho1,&erot);
rsline_(&X_Rot,&p1,&MCRotTau,&rho2,&erot);
dens_new = rho1 + rho2;
}
if (fabs(dens_new)<RZERO) dens_new = 0.0;
if (dens_new<0.0 && RotDenType == 0) nrerror("Rotational Moves: ","Negative rot density");
double pot_new = 0.0;
for (int it=itr0;it<itr1;it++) // average over tr time slices
pot_new += (PotRotEnergy(gatom,newcoords,it));
double rd;
if(RotDenType == 0)
{
if (dens_old>RZERO)
rd = dens_new/dens_old;
else rd = 1.0;
rd *= exp(- MCTau*(pot_new-pot_old));
}
else if(RotDenType == 1)
{
rd = dens_new - dens_old - MCTau*(pot_new-pot_old);
// rd = exp(rd);
}
bool Accepted = false;
if(RotDenType == 0)
{
if (rd>1.0) Accepted = true;
else if (rd>rnd7()) Accepted = true;
}
else if (RotDenType == 1)
{
if (rd > 0.0) Accepted = true;
else if (rd > log(rnd7())) Accepted = true;
}
MCTotal[type][MCROTAT] += 1.0;
if (Accepted)
{
MCAccep[type][MCROTAT] += 1.0;
MCAngles[CTH][t1] = cost;
MCAngles[PHI][t1] = phi;
for (int id=0;id<NDIM;id++)
MCCosine [id][t1] = newcoords[id][t1];
}
} // end of the loop over time slices
}
*/
void MCRotations3D(int type) // update all time slices for rotational degrees of freedom
{
#ifdef DEBUG_PIMC
const char *_proc_=__func__; // MCRotationsMove()
if (type != IMTYPE)
nrerror(_proc_,"Wrong impurity type");
if (NDIM != 3)
nrerror(_proc_,"Rotational sampling for 3D systems only");
#endif
double step = MCAtom[type].rtstep;
// for(int atom0=0;atom0<MCAtom[type].numb;atom0++)
// {
// int offset = MCAtom[type].offset+(NumbTimes*atom0); // the same offset for rotational
// int gatom = offset/NumbTimes; // and translational degrees of freedom
// serial code
/*
MCRotChunkTot = 0;
MCRotChunkAcp = 0;
for (int it1=0;it1<NumbRotTimes;it1++)
{
MCRot3Dstep(it1,offset,gatom,type,step,MCRotChunkTot,MCRotChunkAcp);
}
MCTotal[type][MCROTAT] += MCRotChunkTot;
MCAccep[type][MCROTAT] += MCRotChunkAcp;
*/
// openmp code
MCRotChunkTot = 0;
MCRotChunkAcp = 0;
// randomseed(); //set seed according to clock
RngStream Rng[omp_get_num_procs()]; // initialize a parallel RNG named "Rng"
double rand1,rand2,rand3,rand4;
#pragma omp parallel for reduction(+: MCRotChunkTot,MCRotChunkAcp) private(rand1,rand2,rand3,rand4)
for (int itrot=0;itrot<NumbRotTimes;itrot=itrot+2)
{
for(int atom0=0;atom0<MCAtom[type].numb;atom0++)
{
int offset = MCAtom[type].offset+(NumbTimes*atom0); // the same offset for rotational
int gatom = offset/NumbTimes; // and translational degrees of freedom
rand1=runif(Rng);
rand2=runif(Rng);
rand3=runif(Rng);
rand4=runif(Rng);
MCRot3Dstep(itrot,offset,gatom,type,step,rand1,rand2,rand3,rand4,IROTSYM,NFOLD_ROT,MCRotChunkTot,MCRotChunkAcp);
}
}
MCTotal[type][MCROTAT] += MCRotChunkTot;
MCAccep[type][MCROTAT] += MCRotChunkAcp;
MCRotChunkTot = 0;
MCRotChunkAcp = 0;
#pragma omp parallel for reduction(+: MCRotChunkTot,MCRotChunkAcp) private(rand1,rand2,rand3,rand4)
for (int itrot=1;itrot<NumbRotTimes;itrot=itrot+2)
{
for(int atom0=0;atom0<MCAtom[type].numb;atom0++)
{
int offset = MCAtom[type].offset+(NumbTimes*atom0); // the same offset for rotational
int gatom = offset/NumbTimes; // and translational degrees of freedom
rand1=runif(Rng);
rand2=runif(Rng);
rand3=runif(Rng);
rand4=runif(Rng);
MCRot3Dstep(itrot,offset,gatom,type,step,rand1,rand2,rand3,rand4,IROTSYM,NFOLD_ROT,MCRotChunkTot,MCRotChunkAcp);
}
}
MCTotal[type][MCROTAT] += MCRotChunkTot;
MCAccep[type][MCROTAT] += MCRotChunkAcp;
// }
}
void MCRotLinStep(int it1,int offset,int gatom,int type,double step,double rand1,double rand2,double rand3,double &MCRotChunkTot,double &MCRotChunkAcp)
{
int it0 = (it1 - 1);
int it2 = (it1 + 1);
if (it0<0) it0 += NumbRotTimes; // NumbRotTimes - 1
if (it2>=NumbRotTimes) it2 -= NumbRotTimes; // 0
int t0 = offset + it0;
int t1 = offset + it1;
int t2 = offset + it2;
double n1[NDIM];
double cost = MCAngles[CTH][t1];
double phi = MCAngles[PHI][t1];
// cost += (step*(rnd1()-0.5));
// phi += (step*(rnd1()-0.5));
cost += (step*(rand1-0.5));
phi += (step*(rand2-0.5));
if (cost > 1.0)
{
cost = 2.0 - cost;
// phi = phi + M_PI;
}
if (cost < -1.0)
{
cost = -2.0 - cost;
// phi = phi + M_PI;
}
double sint = sqrt(1.0 - cost*cost);
newcoords[AXIS_X][t1] = sint*cos(phi);
newcoords[AXIS_Y][t1] = sint*sin(phi);
newcoords[AXIS_Z][t1] = cost;
//----------------------------------------------
// the old density
double p0 = 0.0;
double p1 = 0.0;
for (int id=0;id<NDIM;id++)
{
p0 += (MCCosine[id][t0]*MCCosine[id][t1]);
p1 += (MCCosine[id][t1]*MCCosine[id][t2]);
}
double dens_old;
double rho1,rho2,erot;
if(RotDenType == 0)
{
dens_old = SRotDens(p0,type)*SRotDens(p1,type);
}
else if(RotDenType == 1)
{
rsline_(&X_Rot,&p0,&MCRotTau,&rho1,&erot);
rsline_(&X_Rot,&p1,&MCRotTau,&rho2,&erot);
dens_old = rho1+rho2;
}
if (fabs(dens_old)<RZERO) dens_old = 0.0;
if (dens_old<0.0 && RotDenType == 0) nrerror("Rotational Moves: ","Negative rot density");
double pot_old = 0.0;
int itr0 = it1 * RotRatio; // interval to average over
int itr1 = itr0 + RotRatio; // translational time slices
for (int it=itr0;it<itr1;it++) // average over tr time slices
pot_old += (PotRotEnergy(gatom,MCCosine,it));
// the new density
p0 = 0.0;
p1 = 0.0;
for (int id=0;id<NDIM;id++)
{
p0 += (MCCosine [id][t0]*newcoords[id][t1]);
p1 += (newcoords[id][t1]*MCCosine [id][t2]);
}
double dens_new;
if(RotDenType == 0)
{
dens_new = SRotDens(p0,type)*SRotDens(p1,type);
}
else if(RotDenType == 1)
{
rsline_(&X_Rot,&p0,&MCRotTau,&rho1,&erot);
rsline_(&X_Rot,&p1,&MCRotTau,&rho2,&erot);
dens_new = rho1 + rho2;
}
if (fabs(dens_new)<RZERO) dens_new = 0.0;
if (dens_new<0.0 && RotDenType == 0) nrerror("Rotational Moves: ","Negative rot density");
double pot_new = 0.0;
for (int it=itr0;it<itr1;it++) // average over tr time slices
pot_new += (PotRotEnergy(gatom,newcoords,it));
double rd;
if(RotDenType == 0)
{
if (dens_old>RZERO)
rd = dens_new/dens_old;
else rd = 1.0;
rd *= exp(- MCTau*(pot_new-pot_old));
}
else if(RotDenType == 1)
{
rd = dens_new - dens_old - MCTau*(pot_new-pot_old);
// rd = exp(rd);
}
bool Accepted = false;
if(RotDenType == 0)
{
if (rd>1.0) Accepted = true;
// else if (rd>rnd7()) Accepted = true;
else if (rd>rand3) Accepted = true;
}
else if (RotDenType == 1)
{
if (rd > 0.0) Accepted = true;
// else if (rd > log(rnd7())) Accepted = true;
else if (rd > log(rand3)) Accepted = true;
}
MCRotChunkTot += 1.0;
if (Accepted)
{
MCRotChunkAcp += 1.0;
MCAngles[CTH][t1] = cost;
MCAngles[PHI][t1] = phi;
for (int id=0;id<NDIM;id++)
MCCosine [id][t1] = newcoords[id][t1];
}
}
void MCRot3Dstep(int it1, int offset, int gatom, int type, double step,double rand1,double rand2,double rand3,double rand4,int IROTSYM, int NFOLD_ROT,double &MCRotChunkTot,double &MCRotChunkAcp)
{
int it0 = (it1 - 1);
int it2 = (it1 + 1);
if (it0<0) it0 += NumbRotTimes; // NumbRotTimes - 1
if (it2>=NumbRotTimes) it2 -= NumbRotTimes; // 0
int t0 = offset + it0;
int t1 = offset + it1;
int t2 = offset + it2;
double cost = MCAngles[CTH][t1];
double phi = MCAngles[PHI][t1];
double chi = MCAngles[CHI][t1];
// cost += (step*(rnd1()-0.5));
cost += (step*(rand1-0.5));
// Toby change:
// cout<<"before random change "<<phi<<" "<<chi<<" "<<endl;
// phi += 2.0*M_PI*(step*(rnd1()-0.5));
// chi += 2.0*M_PI*(step*(rnd1()-0.5));
phi += 2.0*M_PI*(step*(rand2-0.5));
chi += 2.0*M_PI*(step*(rand3-0.5));
/*
// axial symmetry of the molecule controlled by IROTSYM and NFOLD_ROT. use rand1 to judge whether rotate or not
if( IROTSYM == 1 )
{
// if(rand1 < 1.0/3.0)
// chi += 2.0*M_PI/(double)NFOLD_ROT;
if(rand1 < 2.0/3.0 && rand1 >= 1.0/3.0)
chi += 2.0*M_PI/(double)NFOLD_ROT;
if(rand1 >= 2.0/3.0 )
chi -= 2.0*M_PI/(double)NFOLD_ROT;
}
*/
// get to the positive values of phi and chi
if(phi<0.0) phi = 2.0*M_PI + phi;
if(chi<0.0) chi = 2.0*M_PI + chi;
// Toby needs to recover the [0:2*Pi] range for phi and chi
phi = fmod(phi,2.0*M_PI);
chi = fmod(chi,2.0*M_PI);
if (cost > 1.0)
{
cost = 2.0 - cost;
// phi = phi + M_PI;
}
if (cost < -1.0)
{
cost = -2.0 - cost;
// phi = phi + M_PI;
}
double sint = sqrt(1.0 - cost*cost);
newcoords[PHI][t1] = phi;
newcoords[CHI][t1] = chi;