-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMMFFsp3_loc.h
1825 lines (1602 loc) · 80.6 KB
/
MMFFsp3_loc.h
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
#ifndef MMFFsp3_loc_h
#define MMFFsp3_loc_h
/// @file MMFFsp3_loc.h @brief Classical Molecular-Mechanics force-filed for bonding interactions (i.e. topology based) using representation localized on atoms to optimize memory access especially for paralel evaluation
/// @ingroup Classical_Molecular_Mechanics
#define ANG_HALF_COS 1
#include <omp.h>
#include "fastmath.h" // fast math operations
#include "Vec2.h" // 2D vector
#include "Vec3.h" // 3D vector
#include "quaternion.h" // quaternions
#include "constants.h"
#include "Forces.h" // various physical interactions
#include "SMat3.h" // Symmetric Matrix
#include "molecular_utils.h" // various molecular utilities
#include "NBFF.h" // Non-Bonded Force Field
//#include <cstdio>
// ========================
// ==== MMFFsp3_loc ====
// ========================
/// @brief Classical Molecular-Mechanics force-filed for bonding interactions (i.e. topology based) using representation localized on atoms to optimize memory access especially for paralel evaluation
/// @details MMFFsp3_loc stores and evaluates bonds and angles by functions and datastructures localized on "node" atoms (e.g. carbon, not capping atoms like hydrogen)
/// The dihedral angles and "inproper" dihedrals are not evaluated directly, but are simulated by angular terms invoilving explicit orientation of pi-orbitals on node atom (which are stored as 3D vectors, and dynamically updated)
// Torsions (proper dihedrals) are simulated by k_pp term (pi-pi alignment interaction) and plane-inversions (inproper dihedrals) are simulated by k_sp term (pi-sigma alignment interaction)
/// This is a local version of MMFFsp3, i.e. we store all parameters per atom, and we compute energy and forces for each atom separately.
/// The recoil forces on neighbors are stored in a temporary array fneigh. Which is latter assembled into the global force array fapos.
/// This allows for efficient parallelization, since we avoid synchronization of the global force array fapos or using atomic operations.
/// The drawback is that we need to allocate additional memory for fneigh, and the overhead of assembling fneigh into fapos.
/// @todo We should test performance of of atomic writes in MMFFsp3_loc::eval_atom() versus assembling fneigh into fapos in MMFFsp3_loc::assemble_atom( ia );
class MMFFsp3_loc : public NBFF { public:
static constexpr const int nneigh_max = 4; // maximum number of neighbors
// === inherited from NBFF
// int natoms=0; // [natoms] // from Atoms
//int * atypes =0; // [natom] atom types
//Vec3d * apos =0; // [natoms] // from Atoms
//Vec3d * vapos = 0; // [natom] velocities of atoms
//Vec3d * fapos =0; // [natoms] // from NBFF
//Quat4i* neighs =0; // [natoms] // from NBFF
//Quat4i* neighCell=0; // [natoms] // from NBFF
//Quat4d* REQs =0; // [nnode] // from NBFF
//bool bPBC=false; // from NBFF
//Vec3i nPBC; // from NBFF
//Mat3d lvec; // from NBFF
//double Rdamp = 1.0; // from NBFF
// dimensions of the system
int nDOFs=0,nnode=0,ncap=0,nvecs=0,ntors=0;
double Etot,Eb,Ea, Eps,EppT,EppI; // total energy, bond energy, angle energy, pi-sigma energy, pi-pi torsion energy, pi-pi interaction energy
double * DOFs = 0; // degrees of freedom
double * fDOFs = 0; // forces
bool doBonds =true; // compute bonds
bool doNeighs =true; // compute neighbors
bool doPiPiI =true; // compute pi-pi interaction parallel
bool doPiPiT =true; // compute pi-pi torsion perpendicular
bool doPiSigma=true; // compute pi-sigma interaction
bool doAngles =true; // compute angles
//bool doEpi =true; // compute pi-electron interaction
// bool bCollisionDamping = false; // if true we use collision damping
// bool bCollisionDampingAng = false;
// bool bCollisionDampingNonBond = false; // if true we use collision damping for non-bonded interactions
// double damping_medium = 1.0; // cdamp = 1 -(damping_medium /ndampstep )
// double collisionDamping = 0.0; // col_damp = collisionDamping /(dt*ndampstep )
// double collisionDamping_ang = 0.0; // col_damp_NB = collisionDamping_NB/(dt*ndampstep )
// double collisionDamping_NB = 0.0; // col_damp_NB = collisionDamping_NB/(dt*ndampstep )
// int ndampstep = 10; // how many steps it takes to decay velocity to to 1/e of the initial value
// double col_damp_dRcut1 = -0.2; // non-covalent collision damping interaction goes between 1.0 to 0.0 on interval [ Rvdw , Rvdw+col_damp_dRcut ]
// double col_damp_dRcut2 = 0.3; // non-covalent collision damping interaction goes between 1.0 to 0.0 on interval [ Rvdw , Rvdw+col_damp_dRcut ]
// double col_damp = 0.0; // collisionDamping /(dt*ndampstep );
// double col_damp_NB = 0.0; // collisionDamping_NB/(dt*ndampstep );
// double col_dampAng = 0.0; // collisionDamping /(dt*ndampstep );
CollisionDamping colDamp;
Vec3d cvf=Vec3dZero; // <f|f>, <v|v>, <v|f>
bool bEachAngle = false; // if true we compute angle energy for each angle separately, otherwise we use common parameters for all angles
bool bTorsion = false; // if true we compute torsion energy
// c0 Kss Ksp c0_e
Quat4d default_NeighParams{ -1.0, 1.0, 1.0, -1.0 }; // default parameters for neighbors, c0 is cosine of equilibrium angle, Kss is bond stiffness, Ksp is pi-sigma stiffness, c0_e is cos of equilibrium angle for pi-electron interaction
// Dynamical Varaibles;
//Vec3d * apos=0; // [natom]
//Vec3d * fapos=0; // [natom]
Vec3d * pipos=0; // [nnode]
Vec3d * fpipos=0; // [nnode]
// Aux Dynamil
Vec3d * fneigh =0; // [nnode*4] temporary store of forces on atoms form neighbors (before assembling step)
Vec3d * fneighpi=0; // [nnode*4] temporary store of forces on pi form neighbors (before assembling step)
//Quat4i* neighs =0; // [natoms] // from NBFF
Quat4i* bkneighs=0; // [natoms] inverse neighbors
Quat4d* apars __attribute__((aligned(64))) =0; // [nnode] angle parameters
Quat4d* bLs __attribute__((aligned(64))) =0; // [nnode] bond lengths
Quat4d* bKs __attribute__((aligned(64))) =0; // [nnode] bond stiffness
Quat4d* Ksp __attribute__((aligned(64))) =0; // [nnode] stiffness of pi-alignment
Quat4d* Kpp __attribute__((aligned(64))) =0; // [nnode] stiffness of pi-planarization
Vec3d* angles=0; // [nnode*6] angles between bonds
Quat4i* tors2atom __attribute__((aligned(64))) =0; // [ntors] torsion atoms
Quat4d* torsParams __attribute__((aligned(64))) =0; // [ntors] torsion parameters
Quat4d* constr __attribute__((aligned(64))) = 0; // [natom] constraints
Quat4d* constrK __attribute__((aligned(64))) = 0; // [natom] constraints
//Vec3d * vapos __attribute__((aligned(64))) = 0; // [natom] velocities of atoms
Mat3d invLvec; // inverse lattice vectors
bool bAngleCosHalf = true; // if true we use evalAngleCosHalf() instead of evalAngleCos() to compute anglular energy
// these are defined in ForceFiled.h
//bool bSubtractAngleNonBond = false; // if true we subtract angle energy from non-bonded energy
//bool bSubtractBondNonBond = false; // if true we subtract bond energy from non-bonded energy
//int itr_DBG=0;
// =========================== Functions
// reallcoate MMFFsp3_loc
void realloc( int nnode_, int ncap_, int ntors_=0 ){
nnode=nnode_; ncap=ncap_; ntors=ntors_;
natoms= nnode + ncap;
nvecs = natoms+nnode; // each atom as also pi-orientiation (like up-vector)
nDOFs = nvecs*3;
//printf( "MMFFsp3::realloc() natom(%i,nnode=%i,ncap=%i), npi=%i, nbond=%i \n", natoms, nnode, ncap, npi, nbonds );
int ipi0=natoms;
_realloc0( DOFs , nDOFs , (double)NAN );
_realloc0( fDOFs , nDOFs , (double)NAN );
apos = (Vec3d*) DOFs ;
fapos = (Vec3d*)fDOFs;
pipos = apos + ipi0;
fpipos = fapos + ipi0;
// ---- Aux
_realloc0( fneigh , nnode*4, Vec3dNAN );
_realloc0( fneighpi, nnode*4, Vec3dNAN );
// ----- Params [natom]
_realloc0( atypes , natoms, -1 );
_realloc0( neighs , natoms, Quat4iMinusOnes );
_realloc0( neighCell , natoms, Quat4iMinusOnes );
_realloc0( bkneighs , natoms, Quat4iMinusOnes);
_realloc0( apars , nnode, Quat4dNAN );
_realloc0( bLs , nnode, Quat4dNAN );
_realloc0( bKs , nnode, Quat4dNAN );
_realloc0( Ksp , nnode, Quat4dNAN );
_realloc0( Kpp , nnode, Quat4dNAN );
// Additional:
// Angles
_realloc0( angles, nnode*6, Vec3dNAN ); // 6=4*3/2
// Torsions
_realloc0( tors2atom, ntors, Quat4iZero );
_realloc0( torsParams, ntors, Quat4dNAN );
_realloc0( constr , natoms, Quat4dOnes*-1. );
_realloc0( constrK , natoms, Quat4dOnes*-1. );
}
// clone from another MMFFsp3_loc
void clone( MMFFsp3_loc& from, bool bRealloc, bool bREQsDeep=true ){
realloc( from.nnode, from.ncap );
lvec =from.lvec;
invLvec=from.invLvec;
for(int i=0; i<nDOFs; i++){
DOFs[i]=from. DOFs[i];
//fDOFs[i]=from.fDOFs[i];
}
for(int i=0; i<natoms; i++){
atypes [i]=from.atypes [i];
neighs [i]=from.neighs [i];
neighCell[i]=from.neighCell[i];
bkneighs [i]=from.bkneighs [i];
constr [i]=from.constr [i];
}
for(int i=0; i<nnode; i++){
apars[i]=from.apars[i];
bLs [i]=from.bLs [i];
bKs [i]=from.bKs [i];
Ksp [i]=from.Ksp [i];
Kpp [i]=from.Kpp [i];
}
if(from.REQs){
if(bREQsDeep){ _realloc(REQs, natoms ); for(int i=0; i<natoms; i++){ REQs[i]=from.REQs[i]; } }
else { REQs=from.REQs; }
}
}
// deallcoate MMFFsp3_loc
void dealloc(){
_dealloc(DOFs );
_dealloc(fDOFs);
apos = 0;
fapos = 0;
pipos = 0;
fpipos = 0;
//vDOFs = 0; // to-do (not sure if we should deallocate it here ?)
vapos = 0; // to-do (not sure if we should deallocate it here ?)
//vpipos = 0; // to-do (not sure if we should deallocate it here ?)
_dealloc(atypes);
_dealloc(neighs);
_dealloc(neighCell);
_dealloc(bkneighs);
_dealloc(apars);
_dealloc(bLs);
_dealloc(bKs);
_dealloc(Ksp);
_dealloc(Kpp);
_dealloc(angles);
_dealloc(tors2atom );
_dealloc(torsParams );
nnode=0; ncap=0; ntors=0; natoms=0; nvecs =0; nDOFs =0; int ipi0=natoms;
}
// set lattice vectors
void setLvec(const Mat3d& lvec_){ lvec=lvec_; lvec.invert_T_to( invLvec ); }
// find optimal time-step for dy FIRE optimization algorithm
double optimalTimeStep(double m=1.0){
double Kmax = 1.0;
for(int i=0; i<nnode; i++){
Kmax=fmax(Kmax, bKs[i].x );
Kmax=fmax(Kmax, bKs[i].y );
Kmax=fmax(Kmax, bKs[i].z );
Kmax=fmax(Kmax, bKs[i].w );
}
return M_PI*2.0*sqrt(m/Kmax)/10.0; // dt=T/10; T = 2*pi/omega = 2*pi*sqrt(m/k)
}
// ============== Evaluation
// evaluate energy and forces for single atom (ia) depending on its neighbors
__attribute__((hot))
double eval_atom(const int ia){
//printf( "MMFFsp3_loc::eval_atom(%i) bSubtractBondNonBond=%i \n", ia, bSubtractBondNonBond );
double E=0;
const double Fmax2 = FmaxNonBonded*FmaxNonBonded;
const double R2damp = Rdamp*Rdamp;
const Vec3d pa = apos [ia];
const Vec3d hpi = pipos[ia];
//printf( "apos[%i](%g,%g,%g)\n",ia,apos[ia].x,apos[ia].y,apos[ia].z );
//return E;
//Vec3d& fa = fapos [ia];
//Vec3d& fpi = fpipos[ia];
Vec3d fa = Vec3dZero;
Vec3d fpi = Vec3dZero;
//--- array aliases
const int* ings = neighs [ia].array; // neighbors
const int* ingC = neighCell[ia].array; // neighbors cell index
const double* bK = bKs [ia].array; // bond stiffness
const double* bL = bLs [ia].array; // bond length
const double* Kspi = Ksp [ia].array; // pi-sigma stiffness
const double* Kppi = Kpp [ia].array; // pi-pi stiffness
Vec3d* fbs = fneigh +ia*4; // forces on bonds
Vec3d* fps = fneighpi +ia*4; // forces on pi vectors
const bool bColDampB = colDamp.bBond && vapos; // if true we use collision damping
const bool bColDampAng = colDamp.bAng && vapos; // if true we use collision damping for non-bonded interactions
// // --- settings
// double ssC0 = apars[ia].x;
// double ssK = apars[ia].y;
// double piC0 = apars[ia].z;
// //bool bPi = ings[3]<0; we distinguish this by Ksp, otherwise it would be difficult for electron pairs e.g. (-O-C=)
const Quat4d& apar = apars[ia]; // [c0, Kss, Ksp, c0_e] c0 is cos of equilibrium angle, Kss is bond stiffness, Ksp is pi-sigma stiffness, c0_e is cos of equilibrium angle for pi-electron interaction
const double piC0 = apar.w; // cos of equilibrium angle for pi-electron interaction
//printf( "ang0 %g cs0(%g,%g)\n", atan2(cs0_ss.y,cs0_ss.x)*180/M_PI, cs0_ss.x,cs0_ss.x );
//--- Aux Variables
Quat4d hs[4]; // bond vectors (normalized in .xyz ) and their inverse length in .w
Vec3d f1,f2; // working forces
//bool bErr=0;
//const int ia_DBG = 0;
//if(ia==ia_DBG)printf( "ffl[%i] neighs(%i,%i,%i,%i) \n", ia, ings[0],ings[1],ings[2],ings[3] );
//printf("lvec %i %i \n", ia, ia_DBG ); // printMat(lvec);
//if( ia==5 ){ printf( "ffls[%2i] atom[%2i] ng(%3i,%3i,%3i,%3i) ngC(%3i,%3i,%3i,%3i) shifts=%li bPBC=%i\n", id, ia, ings[0],ings[1],ings[2],ings[3], ingC[0],ingC[1],ingC[2],ingC[3], (long)shifts, bPBC ); };
for(int i=0; i<4; i++){ fbs[i]=Vec3dZero; fps[i]=Vec3dZero; } // we initialize it here because of the break in the loop
// double Eb=0;
// double Ea=0;
// double EppI=0;
// double Eps=0;
// --------- Bonds Step
for(int i=0; i<4; i++){ // loop over bonds
int ing = ings[i];
//printf( "bond[%i|%i=%i]\n", ia,i,ing );
//fbs[i]=Vec3dOne; fps[i]=Vec3dOne;
//fbs[i]=Vec3dZero; fps[i]=Vec3dZero; // NOTE: wee need to initialize it before, because of the break
if(ing<0) break;
//printf("ia %i ing %i \n", ia, ing );
Vec3d pi = apos[ing];
Quat4d h;
h.f.set_sub( pi, pa );
//if(idebug)printf( "bond[%i|%i=%i] l=%g pj[%i](%g,%g,%g) pi[%i](%g,%g,%g)\n", ia,i,ing, h.f.norm(), ing,apos[ing].x,apos[ing].y,apos[ing].z, ia,pa.x,pa.y,pa.z );
//Vec3d h_bak = h.f;
//shifts=0;
// Periodic Boundary Conditions
if(bPBC) [[likely]] {
if(shifts) [[likely]] { // if we have bond shifts vectors we use them
int ipbc = ingC[i];
//Vec3d sh = shifts[ipbc]; //apbc[i] = pi + sh;
h.f.add( shifts[ipbc] );
//if( (ia==0) ){ printf("ffls[%i] atom[%i,%i=%i] ipbc %i shifts(%g,%g,%g)\n", id, ia,i,ing, ipbc, shifts[ipbc].x,shifts[ipbc].y,shifts[ipbc].z); };
//if( (ipbc!=4) ){ printf("ffls[%i] atom[%i,%i=%i] ipbc %i shifts(%g,%g,%g)\n", id, ia,i,ing, ipbc, shifts[ipbc].x,shifts[ipbc].y,shifts[ipbc].z); };
}else{ // if we don't have bond shifts vectors we use lattice vectors
Vec3i g = invLvec.nearestCell( h.f );
// if(ia==ia_DBG){
// Vec3d u; invLvec.dot_to(h.f,u);
// printf( "CPU:bond[%i,%i] u(%6.3f,%6.3f,%6.3f) shi(%6.3f,%6.3f,%6.3f) \n", ia, ing, u.x,u.y,u.z, (float)g.x,(float)g.y,(float)g.z );
// }
Vec3d sh = lvec.a*g.x + lvec.b*g.y + lvec.c*g.z;
h.f.add( sh );
//apbc[i] = pi + sh;
}
}
//wrapBondVec( h.f );
//printf( "h[%i,%i] r_old %g r_new %g \n", ia, ing, h_bak.norm(), h.f.norm() );
// initial bond vectors
const double l = h.f.normalize();
h.e = 1/l;
hs [i] = h;
//bErr|=ckeckNaN( 1,4, (double*)(hs+i), [&]{ printf("atom[%i]hs[%i]",ia,i); } );
// bond length force
//continue;
//if(ia==ia_DBG) printf( "ffl:h[%i|%i=%i] l %g h(%g,%g,%g) pj(%g,%g,%g) pa(%g,%g,%g) \n", ia,i,ing, l, h.x,h.y,h.z, apos[ing].x,apos[ing].y,apos[ing].z, pa.x,pa.y,pa.z );
if(ia<ing){ // we should avoid double counting because otherwise node atoms would be computed 2x, but capping only once
if(doBonds)[[likely]]{
if(bColDampB){ //
//printf( "MMFFsp3_loc::eval_atom() bCollisionDamping=%i col_damp=%g \n", bCollisionDamping, col_damp ); exit(0);
// double invL = 1./l;
// double dv = d.dot( vel[b.y].f - vel[b.x].f )*invL;
// double mcog = pj.w + pi.w;
// double reduced_mass = pj.w*pi.w/mcog;
// double imp = collision_damping * reduced_mass * dv;
// for masses = 1.0 we have reduced_mass = 1*1/(1+1) = 0.5
double dv = h.f.dot( vapos[ing] - vapos[ia] );
double fcol = colDamp.cdampB * 0.5 * dv; // col_damp ~ collision_damping/(dt*ndampstep); f = m*a = m*dv/dt
f1.set_mul( h.f, fcol );
fbs[i].sub(f1); fa.add(f1);
// double w = damp_rate * 0.5 * smoothstep_down(sqrt(r2),R, R+dRcut ); // ToDo : we can optimize this by using some other cutoff function which depends only on r2 (no sqrt)
// //double w = damp_rate * 0.5 * R8down (r2, R, R+dRcut );
// double fcol = w * d.dot( vapos[j]-vi ); // collisionDamping ~ 1/(dt*ndampstep); f = m*a = m*dv/dt
// Vec3d fij; fij.set_mul( d, fcol/r2 ); // vII = d*d.fot(v)/|d|^2
// fx+=fij.x;
// fy+=fij.y;
// fz+=fij.z;
}
// bond length force
//printf( "bond[%i,%i] l=%g bL=%g bK=%g f=%g \n", ia, ing, l, bL[i], bK[i], f1 );
E+= evalBond( h.f, l-bL[i], bK[i], f1 );
// {
// f1=Vec3dZero; // DEBUG
// }
if(bSubtractBondNonBond) [[likely]] { // subtract non-bonded interactions between atoms which have common neighbor
Vec3d fij=Vec3dZero;
//Quat4d REQij; combineREQ( REQs[ing],REQs[jng], REQij );
Quat4d REQij = _mixREQ(REQs[ia],REQs[ing]); // combine van der Waals parameters for the pair of atoms
Vec3d dp = h.f*l;
E -= getLJQH( dp, fij, REQij, R2damp ); // subtract non-bonded interactions
if(bClampNonBonded)[[likely]] { clampForce( fij, Fmax2 ); }
//if(ia==ia_DBG)printf( "ffl:LJQ[%i|%i,%i] r=%g REQ(%g,%g,%g) fij(%g,%g,%g)\n", ia,ing,jng, dp.norm(), REQij.x,REQij.y,REQij.z, fij.x,fij.y,fij.z );
//bErr|=ckeckNaN( 1,3, (double*)&fij, [&]{ printf("atom[%i]fLJ2[%i,%i]",ia,i,j); } );
f1.sub(fij);
//printf( "ffl:SubtractBondNonBond[%i|%i] r=%g fij(%g,%g,%g) REQ(%g,%g,%g)\n", ia,ing, dp.norm(), fij.x,fij.y,fij.z, REQij.x,REQij.y,REQij.z);
}
fbs[i].sub(f1); fa.add(f1);
//double Ebi = evalBond( h.f, l-bL[i], bK[i], f1 ); fbs[i].sub(f1); fa.add(f1);
//E +=Ebi;
//Eb+=Ebi;
//printf( "ffl:bond[%i|%i=%i] kb=%g l0=%g l=%g h(%g,%g,%g) f(%g,%g,%g) \n", ia,i,ing, bK[i],bL[i], l, h.x,h.y,h.z, f1.x,f1.y,f1.z );
//if(ia==ia_DBG)printf( "ffl:bond[%i|%i=%i] kb=%g l0=%g l=%g h(%g,%g,%g) f(%g,%g,%g) \n", ia,i,ing, bK[i],bL[i], l, h.x,h.y,h.z, f1.x,f1.y,f1.z );
//bErr|=ckeckNaN( 1,3, (double*)&f1, [&]{ printf("atom[%i]fbond[%i]",ia,i); } );
}
double kpp = Kppi[i];
if( (doPiPiI) && (ing<nnode) && (kpp>1e-6) ){ // Only node atoms have pi-pi alignemnt interaction
// pi-pi interaction (make them parallel)
E += evalPiAling( hpi, pipos[ing], 1., 1., kpp, f1, f2 ); fpi.add(f1); fps[i].add(f2); // pi-alignment (konjugation)
//double EppIi = evalPiAling( hpi, pipos[ing], 1., 1., kpp, f1, f2 ); fpi.add(f1); fps[i].add(f2); // pi-alignment (konjugation)
//E +=EppIi;
//EppI+=EppIi;
//if(verbosity>0)printf( "ffl:pp[%i|%i] kpp=%g c=%g f1(%g,%g,%g) f2(%g,%g,%g), EppI=%g\n", ia,ing, kpp, hpi.dot(pipos[ing]), f1.x,f1.y,f1.z, f2.x,f2.y,f2.z, EppIi );
//printf( "ffl:pp[%i|%i] kpp=%g c=%g f1(%g,%g,%g) f2(%g,%g,%g), EppI=%g, Epp=%g\n", ia,ing, kpp, hpi.dot(pipos[ing]), f1.x,f1.y,f1.z, f2.x,f2.y,f2.z, EppIi, EppI );
//if(ia==ia_DBG)printf( "ffl:pp[%i|%i] kpp=%g c=%g f1(%g,%g,%g) f2(%g,%g,%g)\n", ia,ing, kpp, hpi.dot(pipos[ing]), f1.x,f1.y,f1.z, f2.x,f2.y,f2.z );
//bErr|=ckeckNaN( 1,3, (double*)&f1, [&]{ printf("atom[%i]fpp1[%i]",ia,i); } );
//bErr|=ckeckNaN( 1,3, (double*)&f2, [&]{ printf("atom[%i]fpp2[%i]",ia,i); } );
}
// ToDo: triple bonds ?
}
// pi-sigma
//if(bPi){
double ksp = Kspi[i];
if( doPiSigma && (ksp>1e-6) ){
// pi-sigma interaction (make them orthogonal)
E += evalAngleCos( hpi, h.f , 1., h.e, ksp, piC0, f1, f2 ); fpi.add(f1); fa.sub(f2); fbs[i].add(f2); // pi-planarization (orthogonality)
//double Epsi = evalAngleCos( hpi, h.f , 1., h.e, ksp, piC0, f1, f2 ); fpi.add(f1); fa.sub(f2); fbs[i].add(f2); // pi-planarization (orthogonality)
//E +=Epsi;
//Eps+=Epsi;
//printf( "ffl:sp[%i|%i] ksp=%g piC0=%g c=%g hp(%g,%g,%g) h(%g,%g,%g)\n", ia,ing, ksp,piC0, hpi.dot(h.f), hpi.x,hpi.y,hpi.z, h.x,h.y,h.z );
//if(kpp<-1e-6){ E += evalPiAling( hpi, pipos[ing], 1., 1., -kpp, f1, f2 ); fpi.add(f1); fps[i].add(f2); } // align pi-electron pair (e.g. in Nitrogen)
//if(ia==ia_DBG)printf( "ffl:sp[%i|%i] ksp=%g piC0=%g c=%g hp(%g,%g,%g) h(%g,%g,%g)\n", ia,ing, ksp,piC0, hpi.dot(h.f), hpi.x,hpi.y,hpi.z, h.x,h.y,h.z );
//if(ia==ia_DBG)printf( "ffl:sp[%i|%i] ksp=%g piC0=%g c=%g f1(%g,%g,%g) f2(%g,%g,%g)\n", ia,ing, ksp,piC0, hpi.dot(h.f), f1.x,f1.y,f1.z, f2.x,f2.y,f2.z );
//bErr|=ckeckNaN( 1,3, (double*)&f1, [&]{ printf("atom[%i]fsp1[%i]",ia,i); } );
//bErr|=ckeckNaN( 1,3, (double*)&f2, [&]{ printf("atom[%i]fsp2[%i]",ia,i); } );
}
//}
}
//printf( "MMFF_atom[%i] cs(%6.3f,%6.3f) ang=%g [deg]\n", ia, cs0_ss.x, cs0_ss.y, atan2(cs0_ss.y,cs0_ss.x)*180./M_PI );
// ======= Angle Step : we compute forces due to angles between bonds, and also due to angles between bonds and pi-vectors
// {
// doAngles = false; // DEBUG
// }
if(doAngles){
double ssK,ssC0;
Vec2d cs0_ss;
Vec3d* angles_i;
if(bEachAngle)[[unlikely]] { // if we compute angle energy for each angle separately, we need to store angle parameters for each angle
angles_i = angles+(ia*6);
}else{ // otherwise we use common parameters for all angles
ssK = apar.z;
cs0_ss = Vec2d{apar.x,apar.y};
ssC0 = cs0_ss.x*cs0_ss.x - cs0_ss.y*cs0_ss.y; // cos(2x) = cos(x)^2 - sin(x)^2, because we store cos(ang0/2) to use in evalAngleCosHalf
}
int iang=0;
for(int i=0; i<3; i++){
int ing = ings[i];
if(ing<0) break;
const Quat4d& hi = hs[i];
for(int j=i+1; j<4; j++){
int jng = ings[j];
if(jng<0) break;
const Quat4d& hj = hs[j];
if(bEachAngle) [[unlikely]] { //
// 0-1, 0-2, 0-3, 1-2, 1-3, 2-3
// 0 1 2 3 4 5
cs0_ss = angles_i[iang].xy();
ssK = angles_i[iang].z;
//printf( "types{%i,%i,%i} ssK %g cs0(%g,%g) \n", atypes[ing], atypes[ia], atypes[jng], ssK, cs0_ss.x, cs0_ss.y );
iang++;
};
//bAngleCosHalf = false;
//double Eai;
//printf( "bAngleCosHalf= %i\n", bAngleCosHalf);
if( bAngleCosHalf )[[likely]] {
E += evalAngleCosHalf( hi.f, hj.f, hi.e, hj.e, cs0_ss, ssK, f1, f2 );
//Eai = evalAngleCosHalf( hi.f, hj.f, hi.e, hj.e, cs0_ss, ssK, f1, f2 );
}else{
E += evalAngleCos( hi.f, hj.f, hi.e, hj.e, ssK, ssC0, f1, f2 ); // angles between sigma bonds
//Eai = evalAngleCos( hi.f, hj.f, hi.e, hj.e, ssK, ssC0, f1, f2 ); // angles between sigma bonds
}
//E +=Eai;
//Ea+=Eai;
//printf( "ffl:ang[%i|%i,%i] kss=%g cs0(%g,%g) c=%g l(%g,%g) f1(%g,%g,%g) f2(%g,%g,%g)\n", ia,ing,jng, ssK, cs0_ss.x,cs0_ss.y, hi.f.dot(hj.f),hi.w,hj.w, f1.x,f1.y,f1.z, f2.x,f2.y,f2.z );
//if(ia==ia_DBG)printf( "ffl:ang[%i|%i,%i] kss=%g cs0(%g,%g) c=%g l(%g,%g) f1(%g,%g,%g) f2(%g,%g,%g)\n", ia,ing,jng, ssK, cs0_ss.x,cs0_ss.y, hi.f.dot(hj.f),hi.w,hj.w, f1.x,f1.y,f1.z, f2.x,f2.y,f2.z );
//bErr|=ckeckNaN( 1,3, (double*)&f1, [&]{ printf("atom[%i]fss1[%i,%i]",ia,i,j); } );
//bErr|=ckeckNaN( 1,3, (double*)&f2, [&]{ printf("atom[%i]fss2[%i,%i]",ia,i,j); } );
fa .sub( f1+f2 ); // apply force on the central atom
if(bColDampAng) [[unlikely]] {
Vec3d dp; dp.set_lincomb( 1./hj.w, hj.f, -1./hi.w, hi.f );
double dv = dp.dot( vapos[ing] - vapos[ia] );
double fcol = colDamp.cdampAng * 0.5 * dv; // col_damp ~ collision_damping/(dt*ndampstep); f = m*a = m*dv/dt
dp.mul( fcol/dp.norm2() );
f1.sub(dp);
f2.add(dp);
}
if(bSubtractAngleNonBond) [[likely]] { // subtract non-bonded interactions between atoms which have common neighbor
Vec3d fij=Vec3dZero;
//Quat4d REQij; combineREQ( REQs[ing],REQs[jng], REQij );
Quat4d REQij = _mixREQ(REQs[ing],REQs[jng]); // combine van der Waals parameters for the pair of atoms
Vec3d dp; dp.set_lincomb( 1./hj.w, hj.f, -1./hi.w, hi.f ); // compute vector between neighbors i,j
//Vec3d dp = hj.f*(1./hj.w) - hi.f*(1./hi.w);
//Vec3d dp = apbc[j] - apbc[i];
E -= getLJQH( dp, fij, REQij, R2damp ); // subtract non-bonded interactions
if(bClampNonBonded)[[likely]] { clampForce( fij, Fmax2 ); }
//if(ia==ia_DBG)printf( "ffl:LJQ[%i|%i,%i] r=%g REQ(%g,%g,%g) fij(%g,%g,%g)\n", ia,ing,jng, dp.norm(), REQij.x,REQij.y,REQij.z, fij.x,fij.y,fij.z );
//bErr|=ckeckNaN( 1,3, (double*)&fij, [&]{ printf("atom[%i]fLJ2[%i,%i]",ia,i,j); } );
f1.sub(fij); //
f2.add(fij);
}
// apply forces on neighbors
fbs[i].add( f1 );
fbs[j].add( f2 );
//if(ia==ia_DBG)printf( "ffl:ANG[%i|%i,%i] fa(%g,%g,%g) fbs[%i](%g,%g,%g) fbs[%i](%g,%g,%g)\n", ia,ing,jng, fa.x,fa.y,fa.z, i,fbs[i].x,fbs[i].y,fbs[i].z, j,fbs[j].x,fbs[j].y,fbs[j].z );
// ToDo: subtract non-covalent interactions
}
}
}
//if(bErr){ printf("ERROR in ffl.eval_atom[%i] => Exit() \n", ia ); exit(0); }
/*
double Kfix = constr[ia].w;
if(Kfix>0){
//printf( "applyConstrain(i=%i,K=%g)\n", ia, Kfix );
Vec3d d = constr[ia].f-pa;
d.mul( Kfix );
double fr2 = d.norm2();
double F2max = 1.0;
if( fr2>F2max ){
d.mul( sqrt(F2max/fr2) );
}
fa.add( d );
E += d.norm()*Kfix*0.5;
}
*/
//fapos [ia].add(fa );
//fpipos[ia].add(fpi);
fapos [ia]=fa;
fpipos[ia]=fpi;
//printf( "MYOUTPUT atom %i Ebond= %g Eangle= %g Edihed= %g Eimpr = %g Etot=%g\n", ia+1, Eb, Ea, EppI, Eps, E );
//fprintf( file, "atom %i Ebond= %g Eangle= %g Edihed= %g Eimpr= %g Etot=%g \n", ia+1, Eb, Ea, EppI, Eps, E );
return E;
}
// evaluate energy and forces for single atom (ia) depending on its neighbors
template<bool _bPBC, bool _doBonds, bool _doPiPiI, bool _doPiSigma, bool _doAngles, bool _bSubtractBondNonBond, bool _bClampNonBonded, bool _bEachAngle, bool _bAngleCosHalf, bool _bSubtractAngleNonBond>
__attribute__((hot)) double eval_atom_t(const int ia){
double E=0;
const double Fmax2 = FmaxNonBonded*FmaxNonBonded;
const double R2damp = Rdamp*Rdamp;
const Vec3d pa = apos [ia];
const Vec3d hpi = pipos[ia];
Vec3d fa = Vec3dZero;
Vec3d fpi = Vec3dZero;
//--- array aliases
const int* ings = neighs [ia].array; // neighbors
const int* ingC = neighCell[ia].array; // neighbors cell index
const double* bK = bKs [ia].array; // bond stiffness
const double* bL = bLs [ia].array; // bond length
const double* Kspi = Ksp [ia].array; // pi-sigma stiffness
const double* Kppi = Kpp [ia].array; // pi-pi stiffness
Vec3d* fbs = fneigh +ia*4; // forces on bonds
Vec3d* fps = fneighpi +ia*4; // forces on pi vectors
const bool bColDampB = colDamp.bBond && vapos; // if true we use collision damping
const bool bColDampAng = colDamp.bAng && vapos; // if true we use collision damping for non-bonded interactions
const Quat4d& apar = apars[ia]; // [c0, Kss, Ksp, c0_e] c0 is cos of equilibrium angle, Kss is bond stiffness, Ksp is pi-sigma stiffness, c0_e is cos of equilibrium angle for pi-electron interaction
const double piC0 = apar.w; // cos of equilibrium angle for pi-electron interaction
//--- Aux Variables
Quat4d hs[4]; // bond vectors (normalized in .xyz ) and their inverse length in .w
Vec3d f1,f2; // working forces
for(int i=0; i<4; i++){ fbs[i]=Vec3dZero; fps[i]=Vec3dZero; } // we initialize it here because of the break in the loop
// --------- Bonds Step
for(int i=0; i<4; i++){ // loop over bonds
int ing = ings[i];
if(ing<0) break;
//printf("ia %i ing %i \n", ia, ing );
Vec3d pi = apos[ing];
Quat4d h;
h.f.set_sub( pi, pa );
// Periodic Boundary Conditions
if constexpr(_bPBC){
int ipbc = ingC[i];
h.f.add( shifts[ipbc] );
}
const double l = h.f.normalize();
h.e = 1/l;
hs [i] = h;
if(ia<ing){ // we should avoid double counting because otherwise node atoms would be computed 2x, but capping only once
if constexpr(_doBonds){
E+= evalBond( h.f, l-bL[i], bK[i], f1 );
if constexpr(_bSubtractBondNonBond) { // subtract non-bonded interactions between atoms which have common neighbor
Vec3d fij=Vec3dZero;
Quat4d REQij = _mixREQ(REQs[ia],REQs[ing]); // combine van der Waals parameters for the pair of atoms
Vec3d dp = h.f*l;
E -= getLJQH( dp, fij, REQij, R2damp ); // subtract non-bonded interactions
if constexpr(_bClampNonBonded){ clampForce( fij, Fmax2 ); }
f1.sub(fij);
}
fbs[i].sub(f1); fa.add(f1);
}
double kpp = Kppi[i];
if constexpr(_doPiPiI) if( (ing<nnode) && (kpp>1e-6) ){
E += evalPiAling( hpi, pipos[ing], 1., 1., kpp, f1, f2 ); fpi.add(f1); fps[i].add(f2);
}
}
double ksp = Kspi[i];
if constexpr(_doPiSigma) if (ksp>1e-6){
E += evalAngleCos( hpi, h.f , 1., h.e, ksp, piC0, f1, f2 ); fpi.add(f1); fa.sub(f2); fbs[i].add(f2);
}
}
if(doAngles){
double ssK,ssC0;
Vec2d cs0_ss;
Vec3d* angles_i;
if constexpr(_bEachAngle) {
angles_i = angles+(ia*6);
}else{
ssK = apar.z;
cs0_ss = Vec2d{apar.x,apar.y};
ssC0 = cs0_ss.x*cs0_ss.x - cs0_ss.y*cs0_ss.y;
}
int iang=0;
for(int i=0; i<3; i++){
int ing = ings[i];
if(ing<0) break;
const Quat4d& hi = hs[i];
for(int j=i+1; j<4; j++){
int jng = ings[j];
if(jng<0) break;
const Quat4d& hj = hs[j];
if constexpr(_bEachAngle) { //
cs0_ss = angles_i[iang].xy();
ssK = angles_i[iang].z;
iang++;
};
if constexpr(_bAngleCosHalf ){
E += evalAngleCosHalf( hi.f, hj.f, hi.e, hj.e, cs0_ss, ssK, f1, f2 );
}else{
E += evalAngleCos( hi.f, hj.f, hi.e, hj.e, ssK, ssC0, f1, f2 ); // angles between sigma bonds
}
fa .sub( f1+f2 ); // apply force on the central atom
if constexpr(_bSubtractAngleNonBond){
Vec3d fij=Vec3dZero;
Quat4d REQij = _mixREQ(REQs[ing],REQs[jng]);
Vec3d dp; dp.set_lincomb( 1./hj.w, hj.f, -1./hi.w, hi.f );
E -= getLJQH( dp, fij, REQij, R2damp );
if constexpr(_bClampNonBonded){ clampForce( fij, Fmax2 ); }
f1.sub(fij); //
f2.add(fij);
}
fbs[i].add( f1 );
fbs[j].add( f2 );
}
}
}
fapos [ia]=fa;
fpipos[ia]=fpi;
return E;
}
__attribute__((hot))
double eval_atom_opt(const int ia){
double E=0;
const Vec3d pa = apos [ia];
Vec3d fa = Vec3dZero;
const int* ings = neighs[ia].array;
Vec3d* fbs = fneigh +ia*4;
const Quat4d& apar = apars[ia];
const double piC0 = apar.w;
//--- Aux Variables
Quat4d hs[4];
Vec3d f1,f2;
// ========================= Bonds
{ // Bonds
const Vec3d hpi = pipos[ia];
Vec3d fpi = Vec3dZero;
const int* ingC = neighCell[ia].array;
const double* bK = bKs [ia].array;
const double* bL = bLs [ia].array;
const double* Kspi = Ksp [ia].array;
const double* Kppi = Kpp [ia].array;
Vec3d* fps = fneighpi +ia*4;
for(int i=0; i<4; i++){ fbs[i]=Vec3dZero; fps[i]=Vec3dZero; } // we initialize it here because of the break
for(int i=0; i<4; i++){
const int ing = ings[i];
if(ing<0) break;
const Vec3d pi = apos[ing];
Quat4d h;
h.f.set_sub( pi, pa );
const int ipbc = ingC[i];
//Vec3d sh = shifts[ipbc]; //apbc[i] = pi + sh;
h.f.add( shifts[ipbc] );
const double l = h.f.normalize();
h.e = 1/l;
hs [i] = h;
if(ia<ing){ // we should avoid double counting because otherwise node atoms would be computed 2x, but capping only once
E+= evalBond( h.f, l-bL[i], bK[i], f1 ); fbs[i].sub(f1); fa.add(f1);
const double kpp = Kppi[i];
if( (ing<nnode) && (kpp>1e-6) ){ // Only node atoms have pi-pi alignemnt interaction
E += evalPiAling( hpi, pipos[ing], 1., 1., kpp, f1, f2 ); fpi.add(f1); fps[i].add(f2); // pi-alignment (konjugation)
}
}
const double ksp = Kspi[i];
if( (ksp>1e-6) ){
E += evalAngleCos( hpi, h.f , 1., h.e, ksp, piC0, f1, f2 ); fpi.add(f1); fa.sub(f2); fbs[i].add(f2); // pi-planarization (orthogonality)
}
}
fpipos[ia]=fpi;
} // Bonds
// ========================= Angles
const double R2damp=Rdamp*Rdamp;
if(doAngles){
const double ssK = apar.z;
const Vec2d cs0_ss = Vec2d{apar.x,apar.y};
const double ssC0 = cs0_ss.x*cs0_ss.x - cs0_ss.y*cs0_ss.y; // cos(2x) = cos(x)^2 - sin(x)^2, because we store cos(ang0/2) to use in evalAngleCosHalf
int iang=0;
for(int i=0; i<3; i++){
int ing = ings[i];
if(ing<0) break;
const Quat4d& hi = hs[i];
for(int j=i+1; j<4; j++){
int jng = ings[j];
if(jng<0) break;
const Quat4d& hj = hs[j];
E += evalAngleCosHalf( hi.f, hj.f, hi.e, hj.e, cs0_ss, ssK, f1, f2 );
fa .sub( f1+f2 );
/*
// ----- Error is HERE
if(bSubtractAngleNonBond){
Vec3d fij=Vec3dZero;
//Quat4d REQij; combineREQ( REQs[ing],REQs[jng], REQij );
Quat4d REQij = _mixREQ(REQs[ing],REQs[jng]);
Vec3d dp; dp.set_lincomb( 1./hj.w, hj.f, -1./hi.w, hi.f );
//Vec3d dp = hj.f*(1./hj.w) - hi.f*(1./hi.w);
//Vec3d dp = apbc[j] - apbc[i];
E -= getLJQH( dp, fij, REQij, R2damp );
//if(ia==ia_DBG)printf( "ffl:LJQ[%i|%i,%i] r=%g REQ(%g,%g,%g) fij(%g,%g,%g)\n", ia,ing,jng, dp.norm(), REQij.x,REQij.y,REQij.z, fij.x,fij.y,fij.z );
//bErr|=ckeckNaN( 1,3, (double*)&fij, [&]{ printf("atom[%i]fLJ2[%i,%i]",ia,i,j); } );
f1.sub(fij);
f2.add(fij);
}
*/
fbs[i].add( f1 );
fbs[j].add( f2 );
}
}
} // doAngles
fapos [ia]=fa;
return E;
}
double eval_atoms( bool bDebug=false, bool bPrint=false ){
//FILE *file = fopen("out","w");
//Etot,
//Eb=0;Ea=0;Eps=0;EppT=0;EppI=0;
double E=0;
if (bDebug){ for(int ia=0; ia<nnode; ia++){ E+=eval_atom_debug(ia,bPrint); }}
else { for(int ia=0; ia<nnode; ia++){ E+=eval_atom(ia); }}
//printf( "MYOUTPUT Ebond= %g Eangle= %g Edihed= %g Eimpr = %g Etot=%g\n", Eb, Ea, EppI, Eps, E );
//fprintf( file, "Ebond= %g Eangle= %g Edihed= %g Eimpr= %g Etot=%g \n", Eb, Ea, EppI, Eps, E );
//fclose(file);
return E;
}
double evalKineticEnergy(){
// In SI units:
double mass=1;
double Ek=0;
double vabsmean = 0;
double msum = 0;
for(int ia=0; ia<natoms; ia++){
vabsmean = fabs( vapos[ia].norm()/10.180505710774743 ); // to Angstrom/fs
double mi = (1.66053906660e-27 * mass);
msum += mi;
Ek += 0.5* mi* vapos[ia].norm2()*sq( 1e-10/1.0180505710774743e-14 );
}
double Ek_eV = Ek/1.602176634e-19;
double v2mean = sqrt(2*Ek/msum);
//printf( "vabsmean %g[m/s](%g[au]) v2mean %g[m/s](%g[au]) Ek=%g[J](Ek=%g[eV]) msum=%g[kg] \n", vabsmean*1e+5,vabsmean, v2mean,v2mean/1e+5, Ek, Ek_eV, msum );
return Ek_eV;
/*
double Ek=0;
double mass=1; // ToDo: get some masses later
for(int ia=0; ia<natoms; ia++){
Ek+=0.5*mass*vapos[ia].norm2();
}
Ek * = const_fs_timeu*const_fs_timeu; // convert to eV
double v2mean = sqrt(Ek/natoms);
printf( "v2mean %g [A/fs] \n", v2mean );
// units conversion
// velocity is in Angstrom/fs 1 Angstrom = 1.0e-10 m, 1 fs = 1.0e-15 s => 1 Angstrom/fs = 1.0e+5 m/s
// mass is in amu 1 amu = 1.66053906660E-27 kg
// 1/2 m v^2 = 1/2 * 1.66053906660E-27 * (1.0e+5)^2 = 1.66053906660E-27 * 1.0e+10 = 1.66053906660E-17 J
// eV = 1.602176634E-19 J
// 1.66053906660e-17 / 1.602176634e-19 = 103.642695
// 1.602176634e-19 / 1.66053906660e-17 = 0.00964853321
Ek*=0.00964853321;
return Ek;
*/
}
// move cappping atom to equilibrium distance from the node atoms
void addjustAtomCapLenghs(int ia){
const Vec3d pa = apos [ia];
const int* ings = neighs [ia].array;
//const int* ingC = neighCell[ia].array;
const double* bL = bLs [ia].array;
for(int i=0; i<4; i++){
int ing = ings[i];
if(ing<nnode)continue;
Vec3d pi = apos[ing];
Vec3d h;
h.set_sub( pi, pa );
h.mul(bL[ing]/h.norm());
apos[ing].set_add( pa, h );
}
}
// move cappping atoms to equilibrium distance from the node atoms
void addjustCapLenghs(){
for(int ia=0; ia<nnode; ia++){
addjustAtomCapLenghs(ia);
}
}
// initialize directions of pi orbitals
void initPi( Vec3d* pbc_shifts, double Kmin=0.0001, double r2min=1e-4, bool bCheck=true ){
//printf( "MMFFsp3_loc::initPi()\n" );
// first set pi-orbitals on atoms determined by orthogonality of sigma bonds
for(int ia=0; ia<nnode; ia++){
if(vapos)vapos[natoms+ia]=Vec3dZero;
const int* ngs = neighs [ia].array;
const int* ngC = neighCell[ia].array;
const double* ks = Ksp [ia].array;
Vec3d u,v,p;
int nfound=0;
int j=0;
while(j<4){ if(ks[j]>Kmin){ u=apos[ngs[j]]+pbc_shifts[ngC[j]]; nfound++; j++;break; }; j++; }
while(j<4){ if(ks[j]>Kmin){ v=apos[ngs[j]]+pbc_shifts[ngC[j]]; nfound++; j++;break; }; j++; }
while(j<4){ if(ks[j]>Kmin){ p=apos[ngs[j]]+pbc_shifts[ngC[j]]; nfound++; j++;break; }; j++; }
//printf("...ia=%i nfound=%i\n",ia,nfound);
if ( nfound>=2 ){
if( nfound==2 ){ p=apos[ia]; }
//printf( "::[%i] u(%6.3f,%6.3f,%6.3f) v(%6.3f,%6.3f,%6.3f) p(%6.3f,%6.3f,%6.3f) nf=%1i \n", ia, u.x,u.y,u.z, v.x,v.y,v.z, p.x,p.y,p.z, nfound );
u.sub(p); v.sub(p);
Vec3d pi; pi.set_cross( u,v );
double r2 = pi.norm2();
//printf( "::pi[%i](%6.3f,%6.3f,%6.3f) u(%6.3f,%6.3f,%6.3f) v(%6.3f,%6.3f,%6.3f) nf=%1i r2 %g \n", ia, u.x,u.y,u.z, v.x,v.y,v.z, pi.x,pi.y,pi.z, nfound, sqrt(r2) );
if( r2>r2min ){
pi.mul(1/sqrt(r2));
pipos[ia]=pi;
//printf( "pi[%i]=pi(%5.3f,%5.3f,%5.3f) nfound %1i r2 %g \n", ia, pi.x,pi.y,pi.z, nfound, sqrt(r2) );
continue;
}
}
//printf( "pi[%i]=Vec3dZero nf=%1i \n", ia, nfound );
//pipos[ia]=Vec3dZ; // pi cannot be define
pipos[ia]=Vec3dZero; // pi cannot be define
}
// then set remaining pi-orbitals to be parallel to the pi-orbital bonded by strongest pi-pi interaction (Kpp)
for(int ia=0; ia<nnode; ia++){
const int* ngs = neighs [ia].array;
const double* ks = Kpp [ia].array;
int imax=-1;
double kmax=0.0;
double r2 = pipos[ia].norm2();
if( r2>0.1 )continue;
for(int i=0; i<4; i++){
double k = ks[i];
if(k>kmax){
if(bCheck){
int ing=ngs[i];
if ( ing<0 ){ printf("ERROR in MMFFsp3_loc::initPi() atom[%i] Kpp=%g but neihgbor[%i] is undefined(%i) => Exit() \n", ia, k,i, ing ); exit(0); }
else if( ing>nnode ){ printf("ERROR in MMFFsp3_loc::initPi() atom[%i] Kpp=%g but neihgbor[%i] is cap(nnode=%i) => Exit() \n", ia, k,i, nnode ); exit(0); }
}
imax=i; kmax=k;
};
}
if( kmax>Kmin ){
pipos[ia]=pipos[ ngs[imax] ];
//printf("pi[%i] set by neigh[%i==%i] with Kpp=%g\n", ia, imax, ngs[imax], kmax );
}else{
pipos[ia]=Vec3dZ; // pi cannot be define
}
}
//for(int ia=0; ia<nnode; ia++){ pipos[ia]=Vec3dZ ; } // Debug
}
// initialize directions of pi orbitals iteratively
__attribute__((hot))
int relax_pi( int niter, double dt, double Fconv, double Flim=1000.0 ){
double F2conv = Fconv*Fconv;
double E=0,F2=0;
int itr=0;
for(itr=0; itr<niter; itr++){
{E=0;F2=0;}
cleanForce();
normalizePis();
{Eb=0;Ea=0;Eps=0;EppT=0;EppI=0;}
for(int ia=0; ia<nnode; ia++){
E += eval_atom(ia);
}
for(int ia=0; ia<nnode; ia++){
assemble_atom( ia );
}
for(int i=natoms; i<nvecs; i++){
F2 +=move_atom_MD( i, dt, Flim, 0.9 ).z;
}
if(F2<F2conv){ return itr; }
}
return itr;
}
// normalize pi orbitals
__attribute__((hot))
void normalizePis(){
for(int i=0; i<nnode; i++){ pipos[i].normalize(); }
}
// constrain atom to fixed position
void constrainAtom( int ia, double Kfix=1.0 ){
printf( "constrainAtom(i=%i,K=%g)\n", ia, Kfix );
constr[ia].f=apos[ia];
constr[ia].w=Kfix;
};
// clear forces on all atoms and other DOFs
void cleanForce(){
Etot=0;
//for(int i=0; i<natoms; i++){ fapos [i].set(0.0); }
//for(int i=0; i<nnode; i++){ fpipos[i].set(0.0); }
for(int i=0; i<nDOFs; i++){ fDOFs[i]=0; }
// NOTE: We do not need clean fneigh,fneighpi because they are set in eval_atoms
}
void cleanVelocity(){ for(int i=0; i<nvecs; i++){ vapos[i]=Vec3dZero; } }
// add neighbor recoil forces to an atom (ia)
__attribute__((hot))
void assemble_atom(int ia){
Vec3d fa=Vec3dZero,fp=Vec3dZero;
const int* ings = bkneighs[ia].array;
bool bpi = ia<nnode; // if this is node atom, it has pi-orbital