-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrelax_multi.cl
4192 lines (3566 loc) · 199 KB
/
relax_multi.cl
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
/*
// ============= For automatic generation of interfaces
int nnode
int natom
int nvec = nnode+natom
//------- Dynamical
_RW float4* apos [ nSys* nvec ]
_RW float4* aforce [ nSys* nvec ]
_RW float4* avel [ nSys* nvec ]
_RW float4* fneigh [ nSys* nnode*2*4 ]
//------- parameters
_R int4* neighs [ nSys* natom ]
_R int4* bkNeighs [ nSys* natom ]
_R int4* neighCell [ nSys* natom ]
_R float4* REQKs [ nSys* natom ]
_R float4* apars, [ nSys* nnode ]
_R float4* bLs, [ nSys* nnode ]
_R float4* bKs, [ nSys* nnode ]
_R float4* Ksp, [ nSys* nnode ]
_R float4* Kpp, [ nSys* nnode ]
_R cl_Mat3* lvecs, [ nSys ]
_R cl_Mat3* ilvecs [ nSys ]
_R float4* apos_surf [ natom_surf ]
_R float4* aforce_surf [ natom_surf ]
_R float4* ps, [ ndipol ]
_R float4* dipols, [ ndipol ]
_RW image3d_t FE_Paul[ ng.x, ng.y, ng.z ]
_RW image3d_t FE_Lond[ ng.x, ng.y, ng.z ]
_RW image3d_t FE_Coul[ ng.x, ng.y, ng.z ]
const float4 MDpars
const int4 nGrid
const cl_Mat3 dGrid
const float4 grid_p0
*/
/*
relax_multi.cl - OpenCL kernel source code for multi-system relaxation
The file contains various OpenCL kernels icluding:
This file is part of FireCode project.
*/
// ======================================================================
// ======================================================================
// FUNCTIONS
// ======================================================================
// ======================================================================
#pragma OPENCL EXTENSION cl_khr_3d_image_writes : enable
typedef struct __attribute__ ((packed)){
float4 a;
float4 b;
float4 c;
} cl_Mat3;
#define float4Zero (float4){0.f,0.f,0.f,0.f}
#define float3Zero (float3){0.f,0.f,0.f}
#define float2Zero (float3){0.f,0.f,0.f}
#define R2SAFE 1e-4f
#define COULOMB_CONST 14.3996448915f // [ eV*Ang/e^2 ]
#define const_kB 8.617333262145e-5f // [ eV/K ]
inline float2 udiv_cmplx( float2 a, float2 b ){ return (float2){ a.x*b.x + a.y*b.y, a.y*b.x - a.x*b.y }; } // divison of unitary complex numbers (i.e. rotation backwards)
//inline void udiv_cmplx( const VEC& b ){ T x_ = x*b.x + y*b.y; y = y*b.x - x*b.y; x=x_; }
inline float3 rotMat ( float3 v, float3 a, float3 b, float3 c ){ return (float3)(dot(v,a),dot(v,b),dot(v,c)); } // rotate vector v by matrix (a,b,c)
inline float3 rotMatT( float3 v, float3 a, float3 b, float3 c ){ return a*v.x + b*v.y + c*v.z; } // rotate vector v by matrix (a,b,c) transposed
// evaluate angular force and energy using cos(angle) formulation, - faster, but not good for angles > 90 deg
inline float evalAngCos( const float4 hr1, const float4 hr2, float K, float c0, __private float3* f1, __private float3* f2 ){
float c = dot(hr1.xyz,hr2.xyz);
float3 hf1,hf2;
hf1 = hr2.xyz - hr1.xyz*c;
hf2 = hr1.xyz - hr2.xyz*c;
float c_ = c-c0;
float E = K*c_*c_;
float fang = -K*c_*2;
hf1 *= fang*hr1.w;
hf2 *= fang*hr2.w;
*f1=hf1;
*f2=hf2;
return E;
}
// evaluate angular force and energy using cos(angle/2) formulation - a bit slower, but not good for angles > 90 deg
inline float evalAngleCosHalf( const float4 hr1, const float4 hr2, const float2 cs0, float k, __private float3* f1, __private float3* f2 ){
// This is much better angular function than evalAngleCos() with just a little higher computational cost ( 2x sqrt )
// the main advantage is that it is quasi-harmonic beyond angles > 90 deg
float3 h = hr1.xyz + hr2.xyz; // h = a+b
float c2 = dot(h,h)*0.25f; // cos(a/2) = |ha+hb| (after normalization)
float s2 = 1.f-c2 + 1e-7; // sin(a/2) = sqrt(1-cos(a/2)^2) ; s^2 must be positive (otherwise we get NaNs)
float2 cso = (float2){ sqrt(c2), sqrt(s2) }; // cso = cos(a/2) + i*sin(a/2)
float2 cs = udiv_cmplx( cs0, cso ); // rotate back by equilibrium angle
float E = k*( 1 - cs.x ); // E = k*( 1 - cos(a/2) ) ; Do we need Energy? Just for debugging ?
float fr = -k*( cs.y ); // fr = k*( sin(a/2) ) ; force magnitude
c2 *= -2.f;
fr /= 4.f*cso.x*cso.y; // |h - 2*c2*a| = 1/(2*s*c) = 1/sin(a)
float fr1 = fr*hr1.w; // magnitude of force on atom a
float fr2 = fr*hr2.w; // magnitude of force on atom b
*f1 = h*fr1 + hr1.xyz*(fr1*c2); //fa = (h - 2*c2*a)*fr / ( la* |h - 2*c2*a| ); force on atom a
*f2 = h*fr2 + hr2.xyz*(fr2*c2); //fb = (h - 2*c2*b)*fr / ( lb* |h - 2*c2*b| ); force on atom b
return E;
}
// evaluate angular force and energy for pi-pi alignment interaction
inline float evalPiAling( const float3 h1, const float3 h2, float K, __private float3* f1, __private float3* f2 ){ // interaction between two pi-bonds
float c = dot(h1,h2); // cos(a) (assumes that h1 and h2 are normalized)
float3 hf1,hf2; // working forces or direction vectors
hf1 = h2 - h1*c; // component of h2 perpendicular to h1
hf2 = h1 - h2*c; // component of h1 perpendicular to h2
bool sign = c<0; if(sign) c=-c; // if angle is > 90 deg we need to flip the sign of force
float E = -K*c; // energy is -K*cos(a)
float fang = K; // force magnitude
if(sign)fang=-fang; // flip the sign of force if angle is > 90 deg
hf1 *= fang; // force on atom a
hf2 *= fang; // force on atom b
*f1=hf1;
*f2=hf2;
return E;
}
// evaluate bond force and energy for harmonic bond stretching
inline float evalBond( float3 h, float dl, float k, __private float3* f ){
float fr = dl*k; // force magnitude
*f = h * fr; // force on atom a
return fr*dl*0.5; // energy
}
// evaluate non-covalent interaction force and energy for Lennard-Jones (Q) and Coulomb interactions of charges (Q) and hydrogen bond correction (pseudo-charges H), damping R2damp is used to avoid singularity at r=0
inline float4 getLJQH( float3 dp, float4 REQ, float R2damp ){
// ---- Electrostatic (damped Coulomb potential)
float r2 = dot(dp,dp);
float ir2_ = 1.f/( r2 + R2damp); // inverse distance squared and damped
float Ec = COULOMB_CONST*REQ.z*sqrt( ir2_ ); // Ec = Q1*Q2/sqrt(r^2+R2damp)
// --- Lennard-Jones and Hydrogen bond correction
float ir2 = 1.f/r2; // inverse distance squared
float u2 = REQ.x*REQ.x*ir2; // u2 = (R0/r)^2
float u6 = u2*u2*u2; // u6 = (R0/r)^6
float vdW = u6*REQ.y; // vdW = E0*(R0/r)^6
float E = (u6-2.f)*vdW + Ec ; // E = E0*(R0/r)^6 - E0*(R0/r)^12 + Q1*Q2/sqrt(r^2+R2damp)
float fr = -12.f*(u6-1.f)*vdW*ir2 - Ec*ir2_; // fr = -12*E0*( (R0/r)^8/r + 12*E0*(R0/r)^14) - Q1*Q2/(r^2+R2damp)^1.5
return (float4){ dp*fr, E };
}
inline float4 getMorseQH( float3 dp, float4 REQH, float K, float R2damp ){
float r2 = dot(dp,dp);
float ir2_ = 1/(r2+R2damp);
float r = sqrt( r2 );
float ir_ = sqrt( ir2_ ); // ToDo: we can save some cost if we approximate r^2 = r^2 + R2damp;
float e = exp ( K*(r-REQH.x));
//double e2 = e*e;
//double fMors = E0* 2*K*( e2 - e ); // Morse
//double EMors = E0* ( e2 - 2*e );
float Ae = REQH.y*e;
float fMors = Ae* 2*K*(e - 1); // Morse
float EMors = Ae* (e - 2);
float Eel = COULOMB_CONST*REQH.z*ir_;
float fr = fMors/r - Eel*ir2_ ;
return (float4){ dp*fr, EMors+Eel };
}
// evaluate damped Coulomb potential and force
inline float4 getCoulomb( float3 dp, float R2damp ){
// ---- Electrostatic
float r2 = dot(dp,dp);
float ir2_ = 1.f/( r2 + R2damp);
float E = COULOMB_CONST*sqrt( ir2_ );
return (float4){ dp*-E*ir2_, E };
}
// limit force magnitude to fmax
float3 limnitForce( float3 f, float fmax ){
float fr2 = dot(f,f); // force magnitude squared
if( fr2>(fmax*fmax) ){ f*=(fmax/sqrt(fr2)); } // if force magnitude is larger than fmax we scale it down to fmax
return f;
}
float4 getR4repulsion( float3 d, float R, float Rcut, float A ){
// we use R4blob(r) = A * (1-r^2)^2
// such that at distance r=R we have force f = fmax
// f = -dR4blob/dr = 4*A*r*(1-r^2) = fmax
// A = fmax/(4*R*(1-R^2))
float R2 = R*R;
float R2cut = Rcut*Rcut;
float r2 = dot(d,d);
if( r2>R2cut ){
return (float4){0.0f,0.0f,0.0f,0.0f};
}else if( r2>R2 ){
float mr2 = R2cut-r2;
float fr = A*mr2;
return (float4){ d*(-4*fr), fr*mr2 };
}else{
float mr2 = R2cut-R2;
float fr = A*mr2;
float r = sqrt(r2);
float fmax = 4*R*fr;
return (float4){ d* (-fmax/r), fmax*(R-r) + fr*mr2 };
}
}
// ======================================================================
// ======================================================================
// MMFF kernells
// ======================================================================
// ======================================================================
// ======================================================================
// getMMFFf4()
// ======================================================================
// 1. getMMFFf4() - computes bonding interactions between atoms and nodes and its neighbors (max. 4 neighbors allowed), the resulting forces on atoms are stored "fapos" array and recoil forces on neighbors are stored in "fneigh" array
// kernel run over all atoms and all systems in parallel to exploit GPU parallelism
//__attribute__((reqd_work_group_size(1,1,1)))
__kernel void getMMFFf4(
const int4 nDOFs, // 1 (nAtoms,nnode) dimensions of the system
// Dynamical
__global float4* apos, // 2 [natoms] positions of atoms (including node atoms [0:nnode] and capping atoms [nnode:natoms] and pi-orbitals [natoms:natoms+nnode] )
__global float4* fapos, // 3 [natoms] forces on atoms (just node atoms are evaluated)
__global float4* fneigh, // 4 [nnode*4*2] recoil forces on neighbors (and pi-orbitals)
// parameters
__global int4* neighs, // 5 [nnode] neighboring atoms
__global int4* neighCell, // 5 [nnode] neighboring atom cell index
__global float4* REQKs, // 6 [natoms] non-boding parametes {R0,E0,Q} i.e. R0: van der Waals radii, E0: well depth and partial charge, Q: partial charge
__global float4* apars, // 7 [nnode] per atom forcefield parametrs {c0ss,Kss,c0sp}, i.e. c0ss: cos(equlibrium angle/2) for sigma-sigma; Kss: stiffness of sigma-sigma angle; c0sp: is cos(equlibrium angle) for sigma-pi
__global float4* bLs, // 8 [nnode] bond length between node and each neighbor
__global float4* bKs, // 9 [nnode] bond stiffness between node and each neighbor
__global float4* Ksp, // 10 [nnode] stiffness of pi-alignment for each neighbor (only node atoms have pi-pi alignemnt interaction)
__global float4* Kpp, // 11 [nnode] stiffness of pi-planarization for each neighbor (only node atoms have pi-pi alignemnt interaction)
__global cl_Mat3* lvecs, // 12 lattice vectors for each system
__global cl_Mat3* ilvecs, // 13 inverse lattice vectors for each system
__global float4* pbc_shifts,
const int npbc,
const int bSubtractVdW
){
const int iG = get_global_id (0); // intex of atom (iG<nAtoms)
const int iS = get_global_id (1); // index of system (iS<nS)
//const int nG = get_global_size(0);
//const int nS = get_global_size(1); // number of systems
//const int iL = get_local_id (0);
//const int nL = get_local_size(0);
const int nAtoms=nDOFs.x; // number of atoms in the system
const int nnode =nDOFs.y; // number of nodes in the system
//const int nvec = nAtoms+nnode;
if(iG>=nnode) return;
const int i0a = iS*nAtoms; // index of first atom in the system
const int i0n = iS*nnode; // index of first node atom in the system
const int i0v = iS*(nAtoms+nnode); // index of first vector in the system ( either atom or pi-orbital )
const int iaa = iG + i0a; // index of current atom (either node or capping atom)
const int ian = iG + i0n; // index of current node atom
const int iav = iG + i0v; // index of current vector ( either atom or pi-orbital )
#define NNEIGH 4
// ---- Dynamical
float4 hs [4]; // direction vectors of bonds (h.xyz) and inverse bond lengths (h.w)
float3 fbs[4]; // force on neighbor sigma (fbs[i] is sigma recoil force on i-th neighbor)
float3 fps[4]; // force on neighbor pi (fps[i] is pi recoil force on i-th neighbor)
float3 fa = float3Zero; // force on center atom positon
float E=0; // Total Energy of this atom
// ---- Params
const int4 ng = neighs[iaa]; // neighboring atoms
const float3 pa = apos[iav].xyz; // position of current atom
const float4 par = apars[ian]; // (xy=s0_ss,z=ssK,w=piC0 ) forcefield parameters for current atom
// Temp Arrays
const int* ings = (int* )&ng; // neighboring atoms, we cast it to int[] to be index it in for loop
const float ssC0 = par.x*par.x - par.y*par.y; // cos(2) = cos(x)^2 - sin(x)^2, because we store cos(ang0/2) to use in evalAngleCosHalf , where ang0 is equilibrium angle
for(int i=0; i<NNEIGH; i++){ fbs[i]=float3Zero; fps[i]=float3Zero; } // clear recoil forces on neighbors
float3 f1,f2; // working forces
{ // ========= BONDS - here we evaluate pairwise interactions of node atoms with its 4 neighbors
float3 fpi = float3Zero; // force on pi-orbital
const int4 ngC = neighCell[iaa]; // neighboring atom cell index
const float3 hpi = apos[iav+nAtoms].xyz; // direction of pi-orbital
const float4 vbL = bLs[ian]; // bond lengths
const float4 vbK = bKs[ian]; // bond stiffness
const float4 vKs = Ksp[ian]; // stiffness of sigma-pi othogonalization
const float4 vKp = Kpp[ian]; // stiffness of pi-pi alignment
const int* ingC = (int* )&ngC; // neighboring atom cell index (we cast it to int[] to be index it in for loop)
const float* bL = (float*)&vbL; // bond lengths
const float* bK = (float*)&vbK; // bond stiffness
const float* Kspi = (float*)&vKs; // stiffness of sigma-pi othogonalization
const float* Kppi = (float*)&vKp; // stiffness of pi-pi alignment
const int ipbc0 = iS*npbc; // index of first PBC shift for current system
for(int i=0; i<NNEIGH; i++){ // loop over 4 neighbors
float4 h; // direction vector of bond
const int ing = ings[i]; // index of i-th neighbor node atom
const int ingv = ing+i0v; // index of i-th neighbor vector
const int inga = ing+i0a; // index of i-th neighbor atom
if(ing<0) break;
// --- Compute bond direction vector and inverse bond length
h.xyz = apos[ingv].xyz - pa; // direction vector of bond
{ // shift bond to the proper PBC cell
int ic = ingC[i]; // index of i-th neighbor cell
h.xyz += pbc_shifts[ipbc0+ic].xyz; // shift bond to the proper PBC cell
}
float l = length(h.xyz); // compute bond length
h.w = 1./l; // store ivnerse bond length
h.xyz *= h.w; // normalize bond direction vector
hs[i] = h; // store bond direction vector and inverse bond length
float epp = 0; // pi-pi energy
float esp = 0; // pi-sigma energy
// --- Evaluate bond-length stretching energy and forces
if(iG<ing){
E+= evalBond( h.xyz, l-bL[i], bK[i], &f1 ); fbs[i]-=f1; fa+=f1; // harmonic bond stretching, fa is force on center atom, fbs[i] is recoil force on i-th neighbor,
// pi-pi alignment interaction
float kpp = Kppi[i];
if( (ing<nnode) && (kpp>1.e-6) ){ // Only node atoms have pi-pi alignemnt interaction
epp += evalPiAling( hpi, apos[ingv+nAtoms].xyz, kpp, &f1, &f2 ); fpi+=f1; fps[i]+=f2; // pi-alignment(konjugation), fpi is force on pi-orbital, fps[i] is recoil force on i-th neighbor's pi-orbital
E+=epp;
}
}
// pi-sigma othogonalization interaction
float ksp = Kspi[i];
if(ksp>1.e-6){
esp += evalAngCos( (float4){hpi,1.}, h, ksp, par.w, &f1, &f2 ); fpi+=f1; fa-=f2; fbs[i]+=f2; // pi-planarization (orthogonality), fpi is force on pi-orbital, fbs[i] is recoil force on i-th neighbor
E+=epp;
}
}
// --- Store Pi-forces we store pi-forces here because we don't use them in the angular force evaluation
const int i4p=(iG + iS*nnode*2 )*4 + nnode*4; // index of first pi-force for current atom
for(int i=0; i<NNEIGH; i++){
fneigh[i4p+i] = (float4){fps[i],0}; // store recoil pi-force on i-th neighbor
}
fapos[iav+nAtoms] = (float4){fpi,0}; // store pi-force on pi-orbital of current atom
}
{ // ============== Angles - here we evaluate angular interactions between pair of sigma-bonds of node atoms with its 4 neighbors
for(int i=0; i<NNEIGH; i++){ // loop over first bond
int ing = ings[i];
if(ing<0) break; // if there is no i-th neighbor we break the loop
const float4 hi = hs[i];
const int ingv = ing+i0v;
const int inga = ing+i0a;
for(int j=i+1; j<NNEIGH; j++){ // loop over second bond
int jng = ings[j];
if(jng<0) break; // if there is no j-th neighbor we break the loop
const int jngv = jng+i0v;
const int jnga = jng+i0a;
const float4 hj = hs[j];
E += evalAngleCosHalf( hi, hj, par.xy, par.z, &f1, &f2 ); // evaluate angular force and energy using cos(angle/2) formulation
fa -= f1+f2;
//if(bSubtractVdW)
{ // Remove non-bonded interactions from atoms that are bonded to common neighbor
float4 REQi=REQKs[inga]; // non-bonding parameters of i-th neighbor
float4 REQj=REQKs[jnga]; // non-bonding parameters of j-th neighbor
// combine non-bonding parameters of i-th and j-th neighbors using mixing rules
float4 REQij;
REQij.x = REQi.x + REQj.x;
REQij.yz = REQi.yz * REQj.yz;
float3 dp = (hj.xyz/hj.w) - (hi.xyz/hi.w); // recover vector between i-th and j-th neighbors using stored vectos and inverse bond lengths, this should be faster than dp=apos[jngv].xyz-apos[ingv].xyz; from global memory
float4 fij = getLJQH( dp, REQij, 1.0f ); // compute non-bonded interaction between i-th and j-th neighbors using Lennard-Jones and Coulomb interactions and Hydrogen bond correction
f1 -= fij.xyz;
f2 += fij.xyz;
}
fbs[i]+= f1;
fbs[j]+= f2;
}
}
}
// ========= Save results - store forces on atoms and recoil on its neighbors (pi-forces are already done)
const int i4 =(iG + iS*nnode*2 )*4;
//const int i4p=i4+nnode*4;
for(int i=0; i<NNEIGH; i++){
fneigh[i4 +i] = (float4){fbs[i],0};
//fneigh[i4p+i] = (float4){fps[i],0};
}
//fapos[iav ] = (float4){fa ,0}; // If we do run it as first forcefield
fapos[iav ] += (float4){fa ,0}; // If we not run it as first forcefield
//fapos[iav+nAtoms] = (float4){fpi,0};
}
//__attribute__((reqd_work_group_size(1,1,1)))
__kernel void getMMFFf4_bak(
const int4 nDOFs, // 1 (nAtoms,nnode) dimensions of the system
// Dynamical
__global float4* apos, // 2 [natoms] positions of atoms (including node atoms [0:nnode] and capping atoms [nnode:natoms] and pi-orbitals [natoms:natoms+nnode] )
__global float4* fapos, // 3 [natoms] forces on atoms (just node atoms are evaluated)
__global float4* fneigh, // 4 [nnode*4*2] recoil forces on neighbors (and pi-orbitals)
// parameters
__global int4* neighs, // 5 [nnode] neighboring atoms
__global int4* neighCell, // 5 [nnode] neighboring atoms
__global float4* REQKs, // 6 [natoms] non-boding parametes {R0,E0,Q}
__global float4* apars, // 7 [nnode] per atom forcefield parametrs {c0ss,Kss,c0sp}
__global float4* bLs, // 8 [nnode] bond lengths for each neighbor
__global float4* bKs, // 9 [nnode] bond stiffness for each neighbor
__global float4* Ksp, // 10 [nnode] stiffness of pi-alignment for each neighbor
__global float4* Kpp, // 11 [nnode] stiffness of pi-planarization for each neighbor
__global cl_Mat3* lvecs, // 12 lattice vectors for each system
__global cl_Mat3* ilvecs, // 13 inverse lattice vectors for each system
__global float4* pbc_shifts,
const int npbc,
const int bSubtractVdW
){
const int iG = get_global_id (0); // intex of atom
const int iS = get_global_id (1); // index of system
const int nG = get_global_size(0);
const int nS = get_global_size(1); // number of systems
//const int iL = get_local_id (0);
//const int nL = get_local_size(0);
const int nAtoms=nDOFs.x;
const int nnode =nDOFs.y;
const int nvec = nAtoms+nnode;
const int i0a = iS*nAtoms;
const int i0n = iS*nnode;
const int i0v = iS*nvec;
const int ipbc0 = iS*npbc;
const int iaa = iG + i0a;
const int ian = iG + i0n;
const int iav = iG + i0v;
#define NNEIGH 4
// if(iG==0){
// printf( "GPU::getMMFFf4() npbc=%i \n", npbc );
// for(int i=0; i<npbc; i++){
// printf( "pbcshift[%i](%6.3f,%6.3f,%6.3f)\n", i, pbc_shifts[i].x,pbc_shifts[i].y,pbc_shifts[i].z );
// }
// }
// if(iav==0){ printf( "GPU::getMMFFf4() nnode=%i nAtoms=%i iS %i nG %i nS %i \n", nnode, nAtoms, iS, nG, nS ); }
// if(ia==0)for(int i=0; i<nnode; i++){
// printf( "GPU[%i] ", i );
// printf( "ngs{%2i,%2i,%2i,%2i} ", neighs[i].x, neighs[i].y, neighs[i].z, neighs[i].w );
// printf( "apar{%6.3f,%6.3f,%6.3f,%6.3f} ", apars[i].x, apars[i].y, apars[i].z, apars[i].w );
// printf( "BL{%6.3f,%6.3f,%6.3f,%6.3f} ", bLs[i].x, bLs[i].y, bLs[i].z, bLs[i].w );
// printf( "BK{%6.3f,%6.3f,%6.3f,%6.3f} ", bKs[i].x, bKs[i].y, bKs[i].z, bKs[i].w );
// printf( "Ksp{%6.3f,%6.3f,%6.3f,%6.3f} ", Ksp[i].x, Ksp[i].y, Ksp[i].z, Ksp[i].w );
// printf( "Kpp{%6.3f,%6.3f,%6.3f,%6.3f} ", Kpp[i].x, Kpp[i].y, Kpp[i].z, Kpp[i].w );
// printf( "\n" );
// }
// ========= Private Memory
//const cl_Mat3 lvec = lvecs [iS];
//const cl_Mat3 invLvec = ilvecs[iS];
// ---- Dynamical
float4 hs [4]; // direction vectors of bonds
float3 fbs[4]; // force on neighbor sigma
float3 fps[4]; // force on neighbor pi
float3 fa = float3Zero; // force on center position
float3 fpi = float3Zero; // force on pi orbital
float E=0;
// ---- Params
const int4 ng = neighs[iaa];
const int4 ngC = neighCell[iaa];
const float3 pa = apos[iav].xyz;
const float3 hpi = apos[iav+nAtoms].xyz;
const float4 par = apars[ian]; // (xy=s0_ss,z=ssK,w=piC0 )
const float4 vbL = bLs[ian];
const float4 vbK = bKs[ian];
const float4 vKs = Ksp[ian];
const float4 vKp = Kpp[ian];
// Temp Arrays
const int* ings = (int* )&ng;
const int* ingC = (int* )&ngC;
const float* bL = (float*)&vbL;
const float* bK = (float*)&vbK;
const float* Kspi = (float*)&vKs;
const float* Kppi = (float*)&vKp;
const float ssC0 = par.x*par.x - par.y*par.y; // cos(2x) = cos(x)^2 - sin(x)^2, because we store cos(ang0/2) to use in evalAngleCosHalf
//const int iS_DBG = 5;
// //const int iG_DBG = 0;
//const int iG_DBG = 0;
// if((iG==iG_DBG)&&(iS==iS_DBG)){
// //for(int i=0; i<nAtoms; i++){ int4 ng_ = neighs[i+iS*nAtoms]; printf( "GPU[%i|%i] neighs(%i,%i,%i,%i) \n", i, iS, ng_.x,ng_.y,ng_.z,ng_.w ); };
// //for(int i=0; i<nAtoms*nS; i++){ int iv=i%nAtoms; int4 ng_ = neighs[i]; if(iv==0)printf("----\n"); printf( "%3i [%2i,%2i,%i] neighs(%i,%i,%i,%i) \n", i, i/nAtoms, iv, iv<=nAtoms, ng_.x,ng_.y,ng_.z,ng_.w ); };
// //for(int i=0; i<nvec*nS; i++){ int iv=i%nvec; float4 p = apos[i]; if(iv==0)printf("----\n"); printf( "%3i [%2i,%2i,%i,%i] p(%10.5f,%10.5f,%10.5f) \n", i, i/nvec, iv,iv<=nnode,iv<=nAtoms, p.x,p.y,p.z ); };
// }
//if((iG==iG_DBG)&&(iS==iS_DBG))printf( "GPU[%i=%i|%i] neighs(%i,%i,%i,%i) \n", iG, iaa, iS, ings[0],ings[1],ings[2],ings[3] );
for(int i=0; i<NNEIGH; i++){ fbs[i]=float3Zero; fps[i]=float3Zero; }
// ========= Evaluate Bonds
float3 f1,f2; // temporary forces
for(int i=0; i<NNEIGH; i++){
float4 h;
const int ing = ings[i];
const int ingv = ing+i0v;
const int inga = ing+i0a;
if(ing<0) break;
h.xyz = apos[ingv].xyz - pa; //printf( "[%i|%i] ing=%i h(%g,%g,%g) pj(%g,%g,%g) pa(%g,%g,%g) \n", ia,i,ing, h.x,h.y,h.z, apos[ing].x,apos[ing].y,apos[ing].z, pa.x,pa.y,pa.z );
// { // PBC bond vector correction
// float3 u = (float3){ dot( invLvec.a.xyz, h.xyz ), dot( invLvec.b.xyz, h.xyz ), dot( invLvec.c.xyz, h.xyz ) };
// h.xyz += lvec.a.xyz*(1.f-(int)(u.x+1.5f))
// + lvec.b.xyz*(1.f-(int)(u.y+1.5f))
// + lvec.c.xyz*(1.f-(int)(u.z+1.5f));
// // if((iG==iG_DBG)&&(iS==iS_DBG)){
// // float3 shi = (float3){(1.f-(int)(u.x+1.5f)), (1.f-(int)(u.y+1.5f)), (1.f-(int)(u.z+1.5f)) };
// // printf( "GPU:bond[%i,%i] u(%6.3f,%6.3f,%6.3f) shi(%6.3f,%6.3f,%6.3f) \n", iG, ing, u.x,u.y,u.z, shi.x,shi.y,shi.z );
// // }
// }
{ // PBC shifts
int ic = ingC[i];
h.xyz += pbc_shifts[ipbc0+ic].xyz;
}
float l = length(h.xyz);
h.w = 1./l;
h.xyz *= h.w;
hs[i] = h;
float epp = 0;
float esp = 0;
//if((iG==iG_DBG)&&(iS==iS_DBG)) printf( "GPU:h[%i|%i=%i] l %g h(%g,%g,%g) pj(%g,%g,%g) pa(%g,%g,%g) \n", iaa,i,ing, l, h.x,h.y,h.z, apos[ingv].x,apos[ingv].y,apos[ingv].z, pa.x,pa.y,pa.z );
if(iG<ing){ // we should avoid 2-counting because otherwise node atoms would be computed 2x, but capping only once
E+= evalBond( h.xyz, l-bL[i], bK[i], &f1 ); fbs[i]-=f1; fa+=f1;
//if((iG==iG_DBG)&&(iS==iS_DBG))printf( "GPU bond[%i=%i|%i] kb=%g l0=%g l=%g h(%g,%g,%g) f(%g,%g,%g) \n", iG,iaa,ing, bK[i],bL[i], l, h.x,h.y,h.z, f1.x,f1.y,f1.z );
float kpp = Kppi[i];
if( (ing<nnode) && (kpp>1.e-6) ){ // Only node atoms have pi-pi alignemnt interaction
//E += evalPiAling( hpi, pipos[ing].xyz, kpp, &f1, &f2 ); fpi+=f1; fps[i]+=f2; // pi-alignment (konjugation)
epp += evalPiAling( hpi, apos[ingv+nAtoms].xyz, kpp, &f1, &f2 ); fpi+=f1; fps[i]+=f2; // pi-alignment (konjugation)
//if((iG==iG_DBG)&&(iS==iS_DBG))printf( "GPU:pp[%i|%i] kpp=%g c=%g f1(%g,%g,%g) f2(%g,%g,%g)\n", iaa,ing, kpp, dot(hpi,apos[ingv+nAtoms].xyz), f1.x,f1.y,f1.z, f2.x,f2.y,f2.z );
//printf( "GPU:pipi[%i|%i] kpp=%g c=%g f1(%g,%g,%g) f2(%g,%g,%g)\n", ia,ing, kpp, dot(hpi,apos[ing+nAtoms].xyz), f1.x,f1.y,f1.z, f2.x,f2.y,f2.z );
E+=epp;
//printf( "GPU[%i|%i] hpi(%g,%g,%g) hpj(%g,%g,%g) \n", ia,ing, hpi.x,hpi.y,hpi.z, apos[ing+nAtoms].x,apos[ing+nAtoms].y,apos[ing+nAtoms].z );
}
// ToDo: triple bonds ?
}
// DEBUG: ERROR: uncomenting this couse drift
// pi-sigma
float ksp = Kspi[i];
if(ksp>1.e-6){
esp += evalAngCos( (float4){hpi,1.}, h, ksp, par.w, &f1, &f2 ); fpi+=f1; fa-=f2; fbs[i]+=f2; // pi-planarization (orthogonality)
//if((iG==iG_DBG)&&(iS==iS_DBG))printf( "GPU:sp:h[%i|%i] ksp=%g piC0=%g c=%g hp(%g,%g,%g) h(%g,%g,%g)\n", iaa,ing, ksp,par.z, dot(hpi,h.xyz), hpi.x,hpi.y,hpi.z, h.x,h.y,h.z );
//if((iG==iG_DBG)&&(iS==iS_DBG))printf( "GPU:sp[%i|%i] ksp=%g piC0=%g c=%g f1(%g,%g,%g) f2(%g,%g,%g)\n", iaa,ing, ksp,par.z, dot(hpi,h.xyz), f1.x,f1.y,f1.z, f2.x,f2.y,f2.z );
E+=epp;
}
//printf( "GPU[%i|%i] esp=%g epp=%g \n", esp, epp );
}
// ============== Angles
for(int i=0; i<NNEIGH; i++){
int ing = ings[i];
if(ing<0) break;
const float4 hi = hs[i];
const int ingv = ing+i0v;
const int inga = ing+i0a;
for(int j=i+1; j<NNEIGH; j++){
int jng = ings[j];
if(jng<0) break;
const int jngv = jng+i0v;
const int jnga = jng+i0a;
const float4 hj = hs[j];
//printf( "[%i|%i,%i] hi(%g,%g,%g) hj(%g,%g,%g)\n", ia, i,j, hi.x,hi.y,hi.z, hj.x,hj.y,hj.z );
E += evalAngleCosHalf( hi, hj, par.xy, par.z, &f1, &f2 );
//E += evalAngCos ( hi, hj, par.z, ssC0, &f1, &f2 ); // angles between sigma bonds
//if((iG==iG_DBG)&&(iS==iS_DBG))printf( "GPU:ang[%i|%i,%i] kss=%g cs0(%g,%g) c=%g l(%g,%g) f1(%g,%g,%g) f2(%g,%g,%g)\n", iG,ing,jng, par.z, par.x,par.y, dot(hi.xyz,hj.xyz),hi.w,hj.w, f1.x,f1.y,f1.z, f2.x,f2.y,f2.z );
fa -= f1+f2;
//if(bSubtractVdW)
{ // Remove vdW
float4 REQi=REQKs[inga]; // ToDo: can be optimized
float4 REQj=REQKs[jnga];
float4 REQij;
REQij.x = REQi.x + REQj.x;
REQij.yz = REQi.yz * REQj.yz;
float3 dp = (hj.xyz/hj.w) - (hi.xyz/hi.w);
float4 fij = getLJQH( dp, REQij, 1.0f );
//float4 fij = getLJQH( apos[ingv].xyz-apos[jngv].xyz, REQKij, 1.0f );
f1 -= fij.xyz;
f2 += fij.xyz;
//if((iG==iG_DBG)&&(iS==iS_DBG))printf( "GPU:LJQ[%i|%i,%i] r=%g REQ(%g,%g,%g) fij(%g,%g,%g)\n", iG,ing,jng, length(dp), REQij.x,REQij.y,REQij.z, fij.x,fij.y,fij.z );
}
fbs[i]+= f1;
fbs[j]+= f2;
//if((iG==iG_DBG)&&(iS==iS_DBG))printf( "GPU:ANG[%i|%i,%i] fa(%g,%g,%g) fbs[%i](%g,%g,%g) fbs[%i](%g,%g,%g)\n", iG,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 );
//if(ia==0)printf( "GPU:fa[%i](%g,%g,%g)\n", ia, fa.x,fa.y,fa.z );
// ToDo: subtract non-covalent interactions
}
}
// ========= Save results
const int i4 =(iG + iS*nnode*2 )*4;
const int i4p=i4+nnode*4;
//if((iG==iG_DBG)&&(iS==iS_DBG))printf( "GPU i4_max %i %i \n", ( (nG-1) + (nS-1)*nnode*2 )*4, ( (nG-1) + (nS-1)*nnode*2 )*4 + nnode*4+4 );
//printf( "[%i,%i] i4 %i i4p %i \n", iG,iS, i4, i4p );
for(int i=0; i<NNEIGH; i++){
fneigh[i4 +i] = (float4){fbs[i],0};
fneigh[i4p+i] = (float4){fps[i],0};
}
//fapos[iav ] = (float4){fa ,0}; // If we do run it as first forcefield
fapos[iav ] += (float4){fa ,0}; // If we not run it as first forcefield
fapos[iav+nAtoms] = (float4){fpi,0};
//printf( "GPU[%i] fa(%g,%g,%g) fpi(%g,%g,%g)\n", ia, fa.x,fa.y,fa.z, fpi.x,fpi.y,fpi.z );
//fapos[ia]=(float4){1,2,3,ia};
//if(ia==0){ printf( "GPU::getMMFFf4() DONE\n" ); }
}
// ======================================================================
// updateGroups()
// ======================================================================
//__attribute__((reqd_work_group_size(1,1,1)))
__kernel void updateGroups(
int ngroup, // 1 // number of groups (total, for all systems)
__global int2* granges, // 2 // (i0,n) range of indexes specifying the group
__global int* g2a, // 3 // indexes of atoms corresponding to groups defined by granges
__global float4* apos, // 4 // positions of atoms (including node atoms [0:nnode] and capping atoms [nnode:natoms] and pi-orbitals [natoms:natoms+nnode] )
__global float4* gcenters, // 5 // centers of each groups (CoGs)
__global float4* gfws, // 6 // forwad orietantian vector for each group
__global float4* gups, // 7 // up orietantian vector for each group
__global float4* gweights // 8 // up orietantian vector for each group
){
const int iG = get_global_id (0); // index of atom
if(iG>=ngroup) return; // make sure we are not out of bounds of current system
// if(iG==0){
// printf( "GPU ngroup=%i \n", ngroup );
// for(int i=0; i<ngroup; i++){
// const int2 grange = granges[i];
// printf("GPU granges[%i] i0=%i n=%i \n", i, grange.x, grange.y );
// for(int j=0; j<grange.y; j++){
// int ia = g2a[ grange.x + j ];
// //printf( "[%i] %i \n", j, ia );
// printf( "GPU gweights[%i](%g,%g,%g,%g)\n", ia, gweights[ia].x,gweights[ia].y,gweights[ia].z,gweights[ia].w );
// }
// printf("\n");
// }
// }
const int2 grange = granges[iG];
float3 cog = (float3){0.0f,0.0f,0.0f};
float wsum = 0.f;
for(int i=0; i<grange.y; i++){
int ia = g2a[ grange.x + i ];
//const float4 pe = apos[ia];
const float4 w = gweights[ia];
cog += apos[ia].xyz * w.x;
wsum += w.x;
}
cog *= ( 1.f/wsum );
gcenters[iG] = (float4){cog,0.0f};
float3 up = (float3){0.0f,0.0f,0.0f};
float3 fw = (float3){0.0f,0.0f,0.0f};
for(int i=0; i<grange.y; i++){
int ia = g2a[ grange.x + i ];
//const float4 pe = apos[ia];
const float4 w = gweights[ia];
const float3 d = apos[ia].xyz - cog.xyz;
fw.xyz += d * w.y;
up.xyz += d * w.z;
}
{ // Orthonormalize
fw = normalize( fw );
up += fw * -dot( fw, up );
up = normalize( up );
}
//printf( "GPU[iG=%i] cog(%g,%g,%g) fw(%g,%g,%g) up(%g,%g,%g) \n", iG, cog.x,cog.y,cog.z, fw.x,fw.y,fw.z, up.x,up.y,up.z );
gfws[iG] = (float4){fw,0.0f};
gups[iG] = (float4){up,0.0f};
}
// ======================================================================
// groupForce()
// ======================================================================
//__attribute__((reqd_work_group_size(1,1,1)))
__kernel void groupForce(
const int4 n, // 1 // (natoms,nnode) dimensions of the system
__global float4* apos, // 2 // positions of atoms (including node atoms [0:nnode] and capping atoms [nnode:natoms] and pi-orbitals [natoms:natoms+nnode] )
__global float4* aforce, // 3 // forces on atoms
__global int* a2g, // 4 // atom to group maping (index)
__global float4* gforces, // 5 // linar forces appliaed to atoms of the group
__global float4* gtorqs, // 6 // {hx,hy,hz,t} torques applied to atoms of the group
__global float4* gcenters, // 7 // centers of rotation (for evaluation of the torque
__global float4* gfws, // 8 // forward vector of group orientation
__global float4* gups, // 9 // up vector of group orientation
__global float2* gfweights // 10 // weights for application of forces on atoms
){
const int natoms = n.x; // number of atoms
const int nnode = n.y; // number of node atoms
const int nGrpup = n.w; // number of node atoms
const int nvec = natoms+nnode; // number of vectors (atoms+node atoms)
const int iG = get_global_id (0); // index of atom
if(iG>=natoms) return; // make sure we are not out of bounds of current system
const int iS = get_global_id (1); // index of system
const int nG = get_global_size(0); // number of atoms
const int nS = get_global_size(1); // number of systems
// if( (iG==0) && (iS==0) ){
// printf( "GPU::groupForce() natom=%i nnode=%i nvec=%i \n", natoms, nnode, nvec );
// // int ig_sel = 0;
// // int is = 0;
// // // for(int ia=0; ia<natoms; ia++){
// // // int iav = ia + is*nvec;
// // // printf( "%i ", a2g[iav] );
// // // }
// // // printf("\n");
// for(int is=0; is<nS; is++){
// // printf( "sys[%i] ", is );
// // for(int ia=0; ia<natoms; ia++){
// // int iav = ia + is*nvec;
// // printf( "%i ", a2g[iav] );
// // }
// // printf("\n");
// for(int ia=0; ia<natoms; ia++){
// int iav = ia + is*nvec;
// const int ig = a2g[iav];
// if(ig>=0){
// //printf( "GPU:atom[%i|%i,%i] ig=%i(%i/%i) gforces(%10.6f,%10.6f,%10.6f)\n", is, ia, iav, ig, ig-is*nGrpup,nGrpup, gforces[ig].x, gforces[ig].y, gforces[ig].z );
// printf( "GPU:atom[isys=%i|ia=%i] gfweights[iav=%i](%10.6f,%10.6f) gtorqs[ig=%i](%10.6f,%10.6f,%10.6f,%10.6f)\n", is, ia, iav, gfweights[iav].x,gfweights[iav].y, ig, gtorqs[ig].x, gtorqs[ig].y, gtorqs[ig].z, gtorqs[ig].w );
// }
// }
// }
// }
//const int ian = iG + iS*nnode;
const int iaa = iG + iS*natoms; // index of atom in atoms array
const int iav = iG + iS*nvec; // index of atom in vectors array
float4 fe = aforce[iav]; // position of atom or pi-orbital
const int ig = a2g[iav]; // index of the group to which this atom belongs
float2 w = gfweights[ig];
// --- apply linear forece from the group
fe.xyz += gforces[ig].xyz * w.x;
// ToDo: group vectors may be stored in Local Memory ?
const float3 torq = gtorqs[ig].xyz;
const float3 fw = gfws [ig].xyz;
const float3 up = gups [ig].xyz;
const float3 lf = normalize( cross(fw,up) );
const float3 tq = fw * torq.x + up * torq.y + lf * torq.z;
// --- apply torque from the group
const float3 dp = apos[iav].xyz - gcenters[ig].xyz;
fe.xyz += cross( dp, tq.xyz ) * w.x;
// --- store results
aforce[iav] = fe;
}
// ======================================================================
// updateAtomsMMFFf4()
// ======================================================================
/*
float2 KvaziFIREdamp( float c, float2 damp_lims, float2 clim ){
float2 cvf;
if (c < clim.x ){ //-- force against veloctiy
cvf.x = damp_lims.x; // v // like 0.5 (strong damping)
cvf.y = 0; // f
}else if(c > clim.y ){ //-- force alingned to velocity
cvf.x = 1-damping; // v // like 0.99 (weak dampong damping)
cvf.y = damping; // f
}else{ // -- force ~ perpendicular to velocity
float f = (c-clim.x )/( clim.y - clim.x );
cvf.x = (1.-damping)*f;
cvf.y = damping *f;
}
return cvf;
}
*/
/*
def KvaziFIREdamp( c, clim, damps ):
# ----- velocity & force ~ perpendicular
t = (c-clim[0] )/( clim[1] - clim[0] )
cv = damps[0] + (damps[1]-damps[0])*t
#cf = damps[1] *t*(1-t)*4
cf = damps[1]*t*(1-t)*2
# ----- velocity & force ~ against each other
mask_lo = c < clim[0]
cv[mask_lo] = damps[0] # v // like 0.5 (strong damping)
cf[mask_lo] = 0 # f
# ----- velocity & force ~ alligned
mask_hi = c > clim[1]
cv[mask_hi] = damps[1] # v // like 0.99 (weak dampong damping)
cf[mask_hi] = 0 # f
return cv,cf
*/
// Damping function for FIRE algorithm, modified to reduction of forece and velocity arrays to make it more suitable for parallelization
float2 KvaziFIREdamp( float c, float2 clim, float2 damps ){
float2 cvf;
if (c < clim.x ){ //-- force against veloctiy
cvf.x = damps.x; // v // like 0.5 (strong damping)
cvf.y = 0; // f
}else if(c > clim.y ){ //-- force alingned to velocity
cvf.x = damps.y; // v // like 0.99 (weak dampong damping)
cvf.y = 0; // f
}else{ // -- force ~ perpendicular to velocity
float t = (c-clim.x )/( clim.y - clim.x );
cvf.x = damps.x + (damps.y-damps.x)*t;
cvf.y = damps.y*t*(1.f-t)*2.f;
}
return cvf;
}
unsigned int hash_wang(unsigned int bits) {
//unsigned int bits = __float_as_int(value);
bits = (bits ^ 61) ^ (bits >> 16);
bits *= 9;
bits = bits ^ (bits >> 4);
bits *= 0x27d4eb2d;
bits = bits ^ (bits >> 15);
return bits;
}
float hashf_wang( float val, float xmin, float xmax) {
//return ( (float)(bits)*(2147483647.0f );
return (((float)( hash_wang( __float_as_int(val) ) )) * 4.6566129e-10 ) *(xmax-xmin)+ xmin;
}
// Assemble recoil forces from neighbors and update atoms positions and velocities
//__attribute__((reqd_work_group_size(1,1,1)))
__kernel void updateAtomsMMFFf4(
const int4 n, // 1 // (natoms,nnode) dimensions of the system
__global float4* apos, // 2 // positions of atoms (including node atoms [0:nnode] and capping atoms [nnode:natoms] and pi-orbitals [natoms:natoms+nnode] )
__global float4* avel, // 3 // velocities of atoms
__global float4* aforce, // 4 // forces on atoms
__global float4* cvf, // 5 // damping coefficients for velocity and force
__global float4* fneigh, // 6 // recoil forces on neighbors (and pi-orbitals)
__global int4* bkNeighs, // 7 // back neighbors indices (for recoil forces)
__global float4* constr, // 8 // constraints (x,y,z,K) for each atom
__global float4* constrK, // 9 // constraints stiffness (kx,ky,kz,?) for each atom
__global float4* MDparams, // 10 // MD parameters (dt,damp,Flimit)
__global float4* TDrives, // 11 // Thermal driving (T,gamma_damp,seed,?)
__global cl_Mat3* bboxes, // 12 // bounding box (xmin,ymin,zmin)(xmax,ymax,zmax)(kx,ky,kz)
__global int* sysneighs, // 13 // // for each system contains array int[nMaxSysNeighs] of nearby other systems
__global float4* sysbonds // 14 // // contains parameters of bonds (constrains) with neighbor systems {Lmin,Lmax,Kpres,Ktens}
){
const int natoms=n.x; // number of atoms
const int nnode =n.y; // number of node atoms
const int nMaxSysNeighs = n.w; // max number of inter-system interactions; if <0 shwitch inter system interactions off
const int nvec = natoms+nnode; // number of vectors (atoms+node atoms)
const int iG = get_global_id (0); // index of atom
if(iG>=nvec) return;
const int iS = get_global_id (1); // index of system
const int nG = get_global_size(0); // number of atoms
const int nS = get_global_size(1); // number of systems
//const int ian = iG + iS*nnode;
const int iaa = iG + iS*natoms; // index of atom in atoms array
const int iav = iG + iS*nvec; // index of atom in vectors array
const float4 MDpars = MDparams[iS]; // (dt,damp,Flimit)
const float4 TDrive = TDrives[iS];
// if((iS==0)&&(iG==0)){
// //printf("MDpars[%i] (%g,%g,%g,%g) \n", iS, MDpars.x,MDpars.y,MDpars.z,MDpars.w);
// for(int is=0; is<nS; is++){
// //printf( "GPU::TDrives[%i](%g,%g,%g,%g)\n", i, TDrives[i].x,TDrives[i].y,TDrives[i].z,TDrives[i].w );
// //printf( "GPU::bboxes[%i](%g,%g,%g)(%g,%g,%g)(%g,%g,%g)\n", is, bboxes[is].a.x,bboxes[is].a.y,bboxes[is].a.z, bboxes[is].b.x,bboxes[is].b.y,bboxes[is].b.z, bboxes[is].c.x,bboxes[is].c.y,bboxes[is].c.z );
// for(int ia=0; ia<natoms; ia++){
// int ic = ia+is*natoms;
// if(constr[ia+is*natoms].w>0) printf( "GPU:sys[%i]atom[%i] constr(%g,%g,%g|%g) constrK(%g,%g,%g|%g)\n", is, ia, constr[ic].x,constr[ic].y,constr[ic].z,constr[ic].w, constrK[ic].x,constrK[ic].y,constrK[ic].z,constrK[ic].w );
// }
// }
// }
const int iS_DBG = 5; // debug system
//const int iG_DBG = 0;
const int iG_DBG = 1; // debug atom
//if((iG==iG_DBG)&&(iS==iS_DBG))printf( "updateAtomsMMFFf4() natoms=%i nnode=%i nvec=%i nG %i iS %i/%i dt=%g damp=%g Flimit=%g \n", natoms,nnode, nvec, iS, nG, nS, MDpars.x, MDpars.y, MDpars.z );
// if((iG==iG_DBG)&&(iS==iS_DBG)){
// int i0a = iS*natoms;
// for(int i=0; i<natoms; i++){
// printf( "GPU:constr[%i](%7.3f,%7.3f,%7.3f |K= %7.3f) \n", i, constr[i0a+i].x,constr[i0a+i].y,constr[i0a+i].z, constr[i0a+i].w );
// }
// }
if(iG>=(natoms+nnode)) return; // make sure we are not out of bounds of current system
//aforce[iav] = float4Zero;
float4 fe = aforce[iav]; // force on atom or pi-orbital
const bool bPi = iG>=natoms; // is it pi-orbital ?
// ------ Gather Forces from back-neighbors
int4 ngs = bkNeighs[ iav ]; // back neighbors indices
//if(iS==5)printf( "iG,iS %i %i ngs %i,%i,%i,%i \n", iG, iS, ngs.x,ngs.y,ngs.z,ngs.w );
//if( (iS==0)&&(iG==0) ){ printf( "GPU:fe.1[iS=%i,iG=%i](%g,%g,%g,%g) \n", fe.x,fe.y,fe.z,fe.w ); }
// sum all recoil forces from back neighbors - WARRNING : bkNeighs must be properly shifted on CPU by adding offset of system iS*nvec*4
if(ngs.x>=0){ fe += fneigh[ngs.x]; } // if neighbor index is negative it means that there is no neighbor, so we skip it
if(ngs.y>=0){ fe += fneigh[ngs.y]; }
if(ngs.z>=0){ fe += fneigh[ngs.z]; }
if(ngs.w>=0){ fe += fneigh[ngs.w]; }
// ---- Limit Forces - WARRNING : Github_Copilot says: this is not the best way to limit forces, because it can lead to drift, better is to limit forces in the first forcefield run (best is NBFF)
float Flimit = 10.0;
float fr2 = dot(fe.xyz,fe.xyz); // squared force
if( fr2 > (Flimit*Flimit) ){ fe.xyz*=(Flimit/sqrt(fr2)); } // if force is too big, we scale it down to Flimit
// =============== FORCE DONE
aforce[iav] = fe; // store force before limit
//aforce[iav] = float4Zero; // clean force : This can be done in the first forcefield run (best is NBFF)
// =============== DYNAMICS
float4 ve = avel[iav]; // velocity of atom or pi-orbital
float4 pe = apos[iav]; // position of atom or pi-orbital
// -------- Fixed Atoms and Bounding Box
if(iG<natoms){ // only atoms have constraints, not pi-orbitals
// ------- bboxes
const cl_Mat3 B = bboxes[iS];
// if(B.c.x>0.0f){ if(pe.x<B.a.x){ fe.x+=(B.a.x-pe.x)*B.c.x; }else if(pe.x>B.b.x){ fe.x+=(B.b.x-pe.x)*B.c.x; }; }
// if(B.c.y>0.0f){ if(pe.y<B.a.y){ fe.y+=(B.a.y-pe.y)*B.c.y; }else if(pe.y>B.b.y){ fe.y+=(B.b.y-pe.y)*B.c.y; }; }
if(B.c.z>0.0f){ if(pe.z<B.a.z){ fe.z+=(B.a.z-pe.z)*B.c.z; }else if(pe.z>B.b.z){ fe.z+=(B.b.z-pe.z)*B.c.z; }; }
// ------- constrains