-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGridFF.h
1739 lines (1570 loc) · 85 KB
/
GridFF.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 GridFF_h
#define GridFF_h
#include "fastmath.h"
#include "Vec2.h"
#include "Vec3.h"
#include "quaternion.h"
#include "Grid.h"
#include "Forces.h"
#include "MMFFparams.h"
#include "Multipoles.h"
#include "InterpolateTricubic.h"
#include "InterpolateTrilinear.h"
#include "Bspline.h"
#include "Bspline_fit.h"
#include "Bspline_fit_2D.h"
#include "Bspline_fit_3D.h"
#include "VecN.h"
#include "NBFF.h"
#include "EwaldGrid.h"
#include "IO_utils.h"
static bool bDebug__ = 0;
template<typename T>
T sum( int n, T* data, T t ){
for(int i=0; i<n; i++){ t.add(data[i]); }
return t;
};
/**
* Automatically calculates the number of periodic boundary conditions (nPBC) based on minimum length.
*
* @param cell The cell dimensions represented by a 3x3 matrix (Mat3d).
* @param nPBC The number of periodic boundary conditions in each direction (x, y, z). This parameter will be modified by the function.
* @param Lmin The minimum length for calculating the number of periodic boundary conditions. Default value is 30.0.
*/
inline void autoNPBC( const Mat3d& cell, Vec3i& nPBC, double Lmin=30.0 ){
if(nPBC.x!=0){ nPBC.x=(int)Lmin/cell.a.norm(); }
if(nPBC.y!=0){ nPBC.y=(int)Lmin/cell.b.norm(); }
if(nPBC.z!=0){ nPBC.z=(int)Lmin/cell.c.norm(); }
//printf("autoNPBC(): (%i,%i,%i) \n", nPBC.x, nPBC.y, nPBC.z );
}
inline double evalDipole( int n, Vec3d* ps, Quat4d* REQs, Vec3d& Dout, Vec3d& p0out ){
// we want to approximate charge distribution by charge `Q` and dipole `D` around center `C`
// we choose center `c` so that it minimizes size of dipole `|d|^2`
// D = sum_i{ ( p_i - C ) * q_i }
// |D|^2 = Dx^2 + Dy^2 + Dz^2
// minimize |D|^2 with respocet of position of center C
// d|D|^2/dCx = 2*Dx * d{sum_i{(x_i-Cx)*qi}}/dCx = 0
// = -2*Dx * sum_i{ qi } = 0
// = -2*Dx * Q = 0 ( if Q=0 it does not matter)
// if Q is not =0
// sum_i{ ( p_i - C ) * q_i } = 0
// sum_i{ p_i*q_i } = Q*C
// C = sum_i{ p_i * q_i } / Q
Vec3d D = Vec3dZero;
Vec3d p0 = Vec3dZero;
double Q = 0;
double W = 0;
double Qsafe = 1e-4;
double Q2safe = Qsafe*Qsafe;
double Wscale = 1e-4;
for(int i=0; i<n; i++){
const Vec3d& p = ps [i];
double qi = REQs[i].z;
//double wi = (Q2safe + qi*qi)*Wscale; // Q2safe is just for case when all charges are zero
double wi = Q2safe;
Q += qi;
W += wi;
D .add_mul( p, qi ); // dipome
p0.add_mul( p, wi ); // this p0 is used only if Q~=0
//printf( "evalDipole[%i] q %g p(%g,%g,%g)\n", i, qi, p.x,p.y,p.z );
}
double denom = 1./( W*W + Q*Q ); // if
p0.mul ( W*denom ); // if Q~=0 this term dominates
//printf( "denom %g Q %g W %g \n", denom, Q, W );
//printf( "p0W(%g,%g,%g)\n", p0.x,p0.y,p0.z );
p0.add_mul( D, Q*denom ); // if Q!=0 this term dominates
D .add_mul( p0, -Q );
//printf( "p0Q(%g,%g,%g)\n", D.x*Q*denom, D.y*Q*denom, D.z*Q*denom );
//printf( "p0 (%g,%g,%g)\n", p0.x,p0.y,p0.z );
Dout = D;
p0out = p0;
return Q;
}
enum class GridFFmod{ Direct=0, LinearFloat=1, LinearDouble=2, HermiteFloat=3, HermiteDouble=4, BsplineFloat=5, BsplineDouble=6 };
class GridFF : public NBFF{ public:
// ----- From NBFF
// int natoms = 0;
// int * atypes = 0;
// Vec3d * apos = 0; // atomic position
// Quat4d * REQs = 0;
//Quat4d * PLQ = 0;
//Vec3d * fapos =0;
//Quat4d * REQs =0;
//Quat4i * neighs =0;
//Quat4i * neighCell=0;
//double Rdamp = 1.0;
//Mat3d lvec;
//Vec3i nPBC;
//bool bPBC=false;
//int npbc=0;
//Vec3d* shifts=0;
//Quat4f *PLQs =0; // used only in combination with GridFF
// -----------------------
GridShape grid;
Quat4f *FFPaul = 0;
Quat4f *FFLond = 0;
Quat4f *FFelec = 0;
Quat4d *FFPaul_d = 0;
Quat4d *FFLond_d = 0;
Quat4d *FFelec_d = 0;
//Quat4f *FFtot = 0; // total FF is not used since each atom-type has different linear combination
Quat4d FEscale{0.0,0.0,0.0,1.0};
Vec3i gridN{0,0,0};
Quat4d *VPLQH = 0;
double *V_debug = 0;
double *HHermite_d = 0;
double *Bspline_Pauli = 0;
double *Bspline_London = 0;
double *Bspline_Coulomb = 0;
bool bUseEwald=false;
EwaldGrid* ewald = 0;
Vec3d *Bspline_PLQ = 0;
Quat4f *Bspline_PLQf = 0;
Quat4i cubic_yqis[4];
Quat4i cubic_xqis[4];
int iDBG=-1;
//GridFFmod mode = GridFFmod::LinearFloat;
GridFFmod mode = GridFFmod::BsplineDouble;
int perVoxel = 4;
// ------ ToDo: this should be put inside NBFF
std::vector<Vec3d> apos_ ;
std::vector<Quat4d> REQs_ ;
std::vector<int> atypes_;
//Vec3i nPBC{1,1,0};
// dipole approximation
Vec3d dip_p0;
Vec3d dip;
double Q;
double Mpol[10];
//double Rdamp = 1.0;
int iDebugEvalR = 0;
bool bCellSet = false;
bool bSymetrized = false;
double findTop(){ double zmax=-1e+300; for(int i=0;i<natoms; i++){ double z=apos[i].z; if(z>zmax)zmax=z; }; return zmax; }
void bindSystem(int natoms_, int* atypes_, Vec3d* apos_, Quat4d* REQs_ ){
natoms=natoms_; atypes=atypes_; apos=apos_; REQs=REQs_;
}
void allocateFFs( bool bDouble=false ){
int ntot = grid.getNtot();
_realloc( FFPaul, ntot );
_realloc( FFLond, ntot );
_realloc( FFelec, ntot );
if(bDouble){
//printf( "GridFF::allocateFFs bDouble=%i \n", bDouble );
_realloc( FFPaul_d, ntot );
_realloc( FFLond_d, ntot );
_realloc( FFelec_d, ntot );
}
}
void clear(){
_dealloc( FFPaul );
_dealloc( FFLond );
_dealloc( FFelec );
_dealloc( FFPaul_d );
_dealloc( FFLond_d );
_dealloc( FFelec_d );
_dealloc( VPLQH );
_dealloc( V_debug );
apos_ .clear();
REQs_ .clear();
atypes_.clear();
//NBFF::clear();
}
void allocateAtoms(int natoms_){
natoms = natoms_;
//_realloc( atypes = new int[natoms];
_realloc(apos ,natoms);
_realloc(REQs,natoms);
}
int loadCell(const char * fname ){ return grid.loadCell(fname ); }
void evalCellDipole(){
Q = evalDipole( natoms, apos, REQs, dip, dip_p0 );
printf( "GridFF::evalDipole(na=%i): D(%g,%g,%g|%g) p0(%g,%g,%g) \n", natoms, dip.x,dip.y,dip.z,Q, dip_p0.x,dip_p0.y,dip_p0.z );
}
// =========================================================
// ================ Sampling ============
// =========================================================
__attribute__((hot))
inline Quat4f getForce( Vec3d p, const Quat4f& PLQ, bool bSurf=true ) const {
//printf( "getForce() p(%g,%g,%g) PLQ(%g,%g,%g,%g) bSurf=%i @FFPaul=%li @FFLond=%li @FFelec=%li \n", p.x,p.y,p.z, PLQ.x,PLQ.y,PLQ.z,PLQ.w, (long)FFPaul,(long)FFLond,(long)FFelec );
Vec3d u;
//p.sub(shift0);
p.sub(grid.pos0);
grid.iCell.dot_to( p, u );
Vec3i n = grid.n;
//printf( "pos(%g,%g,%g) u(%g,%g,%g) p0(%g,%g,%g) \n", p.x,p.y,p.z, u.x,u.y,u.z, grid.pos0.x,grid.pos0.y,grid.pos0.z );
if( bSurf ){ double ivnz=1./n.z; double uzmax=1-ivnz*1.5; double uzmin=ivnz*0.5; if(u.z<uzmin){ u.z=uzmin; }else if(u.z>uzmax){u.z=uzmax; } } // "clamp" boundrary condition in z-direction
// ---- PBC condiction ( within outer cell bboundaries ? )
//u.x=(u.x-(int)u.x)*n.x;
//u.y=(u.y-(int)u.y)*n.y;
//u.z=(u.z-(int)u.z)*n.z;
u.x=(u.x-((int)(u.x+10)-10))*n.x;
u.y=(u.y-((int)(u.y+10)-10))*n.y;
u.z=(u.z-((int)(u.z+10)-10))*n.z;
return Trilinear::fe3f_fe3( (Vec3f)u, n, PLQ.f, FFPaul, FFLond, FFelec );
}
__attribute__((hot))
inline float addForce( const Vec3d& p, const Quat4f& PLQ, Vec3d& f, bool bSurf=true )const{
Quat4f fe = getForce( p, PLQ, bSurf );
f.add( (Vec3d)fe.f );
return fe.e;
}
__attribute__((hot))
inline Quat4d getForce_d( Vec3d p, const Quat4d& PLQ, bool bSurf=true ) const {
Vec3d u;
//p.sub(shift0);
p.sub(grid.pos0);
grid.iCell.dot_to( p, u );
Vec3i n = grid.n;
//printf( "pos(%g,%g,%g) u(%g,%g,%g) p0(%g,%g,%g) \n", p.x,p.y,p.z, u.x,u.y,u.z, grid.pos0.x,grid.pos0.y,grid.pos0.z );
if( bSurf ){ double ivnz=1./n.z; double uzmax=1-ivnz*1.5; double uzmin=ivnz*0.5; if(u.z<uzmin){ u.z=uzmin; }else if(u.z>uzmax){u.z=uzmax; } } // "clamp" boundrary condition in z-direction
// ---- PBC condiction ( within outer cell bboundaries ? )
//u.x=(u.x-(int)u.x)*n.x;
//u.y=(u.y-(int)u.y)*n.y;
//u.z=(u.z-(int)u.z)*n.z;
u.x=(u.x-((int)(u.x+10)-10))*n.x;
u.y=(u.y-((int)(u.y+10)-10))*n.y;
u.z=(u.z-((int)(u.z+10)-10))*n.z;
return Trilinear::fe3d_fe3( u, n, PLQ.f, FFPaul_d, FFLond_d, FFelec_d );
}
__attribute__((hot))
inline double addForce_d( const Vec3d& p, const Quat4d& PLQ, Vec3d& f, bool bSurf=true )const{
Quat4d fe = getForce_d( p, PLQ, bSurf );
f.add( fe.f );
return fe.e;
}
__attribute__((hot))
double addForces_d( int natoms, Vec3d* apos, Quat4d* PLQs, Vec3d* fpos, bool bSurf=true )const{
double E=0;
for(int ia=0; ia<natoms; ia++){ E+=addForce_d( apos[ia], PLQs[ia], fpos[ia], bSurf ); };
return E;
}
__attribute__((hot))
inline Quat4d getForce_Tricubic( Vec3d p, const Quat4d& PLQH, bool bSurf=true ) const {
Vec3d u;
//p.sub(shift0);
p.sub(grid.pos0);
grid.iCell.dot_to( p, u );
u.x=(u.x-((int)(u.x+10)-10))*grid.n.x-1;
u.y=(u.y-((int)(u.y+10)-10))*grid.n.y-1;
if(u.z<0.0){ u.z=0.0; }else if(u.z>1.0){ u.z=1.0; }; u.z=u.z*grid.n.z-1;
return Spline_Hermite::fe3d_v4( PLQH, u, gridN, VPLQH ) * FEscale;
}
__attribute__((hot))
inline float addForce_Tricubic( const Vec3d& p, const Quat4d& PLQ, Vec3d& f, bool bSurf=true )const{
Quat4d fe = getForce_Tricubic( p, PLQ, bSurf );
f.add( (Vec3d)fe.f );
return fe.e;
}
inline int fold_cubic2( int i, int n )const{
//i+=n*100;
return (i+(n<<8) )%n;
//if( i==0 )[[unlikely]]{ return n-1; } else if (i>n)[[unlikely]]{ return i-n; } else [[likely]] { return i; };
}
__attribute__((hot))
inline Quat4d getForce_HHermit( Vec3d p, const Quat4d& PLQH, bool bSurf=true ) const {
//printf( "GridFF::getForce_HHermit() p(%g,%g,%g)\n", p.x,p.y,p.z );
Vec3d t;
//p.sub(shift0);
p.sub(grid.pos0);
grid.diCell.dot_to( p, t );
Vec3d inv_dg2{ -grid.diCell.xx, -grid.diCell.yy, -grid.diCell.zz };
int ix=(int)t.x;
int iy=(int)t.y;
int iz=(int)t.z;
if(t.x<0){ ix=ix-1; };
if(t.y<0){ iy=iy-1; };
const int ix_ = fold_cubic2( ix, grid.n.x );
const int iy_ = fold_cubic2( iy, grid.n.y );
//double dx=t.x-ix;
//double dy=t.y-iy;
//printf( "t.x %g dx %g ix %i \n", t.x, dx, ix );
//if(dx<0){ dx=dx-1; };
//if(dy<0){ dy=dy-1; };
//Quat4d fe = Spline_Hermite::fe3d_comb3( Vec3d{t.x-ix,t.y-iy,t.z-iz}, Vec3i{ix_,iy_,iz}, grid.n, (Vec2d*)HHermite_d, PLQH.f );
Quat4d fe = Spline_Hermite::fe3d_comb3( Vec3d{t.x-ix,t.y-iy,t.z-iz}, Vec3i{ix_,iy_,iz}, gridN, (Vec2d*)HHermite_d, PLQH.f );
//{ const int i0 = (iz+grid.n.z*(iy+grid.n.y*ix))*6; printf( "GridFF::getForce_HHermit() p(%g,%g,%g) i(%i,%i,%i) VPLQ(%g,%g,%g) PLQ(%g,%g,%g) \n", p.x,p.y,p.z, ix,iy,iz, HHermite_d[i0+0], HHermite_d[i0+2], HHermite_d[i0+4], PLQH.x,PLQH.y,PLQH.z ); }
fe.f.mul(inv_dg2);
return fe;
}
__attribute__((hot))
inline float addForce_HHermit( const Vec3d& p, const Quat4d& PLQ, Vec3d& f, bool bSurf=true )const{
Quat4d fe = getForce_HHermit( p, PLQ, bSurf );
f.add( (Vec3d)fe.f );
return fe.e;
}
__attribute__((hot))
inline Quat4d getForce_Bspline( Vec3d p, const Quat4d& PLQH, bool bSurf=true ) const {
//printf( "GridFF::getForce_Bspline() p(%8.4f,%8.4f,%8.4f) PLQH(%8.4f,%8.4f,%8.4f,%8.4f)\n", p.x,p.y,p.z, PLQH.x,PLQH.y,PLQH.z,PLQH.w );
Vec3d t;
//p.sub(shift0);
p.sub(grid.pos0);
grid.diCell.dot_to( p, t );
Vec3d inv_dg2{ -grid.diCell.xx, -grid.diCell.yy, -grid.diCell.zz };
//Quat4d fe = Quat4dZero;
Quat4d fe = Bspline::fe3d_pbc_comb3( t, grid.n, Bspline_PLQ, PLQH.f, cubic_xqis, cubic_yqis );
//printf( "GridFF::getForce_Bspline() p(%8.4f,%8.4f,%8.4f) g0(%8.4f,%8.4f,%8.4f) PLQH(%8.4f,%8.4f,%8.4f,%8.4f) fe(%g,%g,%g,%g)\n", p.x,p.y,p.z, grid.pos0.x,grid.pos0.y,grid.pos0.z, PLQH.x,PLQH.y,PLQH.z,PLQH.w, fe.x,fe.w,fe.z,fe.w );
// Quat4d fe = Bspline::fe3d( t, grid.n, Bspline_Pauli )*PLQH.x
// + Bspline::fe3d( t, grid.n, Bspline_London )*PLQH.y
// + Bspline::fe3d( t, grid.n, Bspline_Coulomb )*PLQH.z;
//Quat4d fe = Bspline::fe3d( t, gridN, Bspline_Coulomb );
//printf( "GridFF::getForce_Bspline() p(%g,%g,%g) fe(%g,%g,%g,%g)\n", p.x,p.y,p.z, fe.x,fe.y,fe.z,fe.w );
//Quat4d fe = Bspline::fe3d( t, gridN, Bspline_Coulomb );
//Quat4d fe = Bspline::fe3d( Vec3d{t.z,t.y,t.x}, Vec3i{gridN.z,gridN.y,gridN.x}, Bspline_Coulomb );
//int i = ((int)t.z) + grid.n.z*( ((int)t.y) + ((int)t.x)*grid.n.y );
//fe.w = Bspline_PLQ[i].dot( PLQH.f );
//printf( "GridFF::getForce_Bspline() p(%8.4f,%8.4f,%8.4f) ixyz(%4i,%4i,%4i,%8i) BsplinePLQ(%g,%g,%g)\n", p.x,p.y,p.z, ((int)t.z), ((int)t.y), ((int)t.x), i, Bspline_PLQ[i].x,Bspline_PLQ[i].y,Bspline_PLQ[i].z );
//fe.w = Bspline_Coulomb[i];
//printf( "GridFF::getForce_Bspline() p(%g,%g,%g) t(%g,%g,%g) Gs[%i]=%g \n", p.x,p.y,p.z, t.x,t.y,t.z, i, fe.w );
fe.f.mul(inv_dg2);
return fe;
}
__attribute__((hot))
inline float addForce_Bspline( const Vec3d& p, const Quat4d& PLQ, Vec3d& f, bool bSurf=true )const{
Quat4d fe = getForce_Bspline( p, PLQ, bSurf );
f.add( (Vec3d)fe.f );
return fe.e;
}
__attribute__((hot))
double addForces( int natoms, Vec3d* apos, Quat4f* PLQs, Vec3d* fpos, bool bSurf=true )const{
double E=0;
for(int ia=0; ia<natoms; ia++){ E+=addForce( apos[ia], PLQs[ia], fpos[ia], bSurf ); };
return E;
}
__attribute__((hot))
inline void addForce( const Vec3d& pos, const Quat4f& PLQ, Quat4f& fe ) const {
//printf( "GridFF::addForce() \n" );
//printf( "GridFF::addForce() pointers(%li,%li,%li)\n", FFPaul, FFLond, FFelec );
Vec3f gpos; grid.cartesian2grid(pos, gpos);
//printf( "pos: (%g,%g,%g) PLQ: (%g,%g,%g) pointers(%li,%li,%li)\n", pos.x, pos.y, pos.z, PLQ.x, PLQ.y, PLQ.z, FFPaul, FFLond, FFelec );
Quat4f fp=interpolate3DvecWrap( FFPaul, grid.n, gpos ); fe.add_mul( fp, PLQ.x );
Quat4f fl=interpolate3DvecWrap( FFLond, grid.n, gpos ); fe.add_mul( fl, PLQ.y );
Quat4f fq=interpolate3DvecWrap( FFelec, grid.n, gpos ); fe.add_mul( fq, PLQ.z );
//Quat4f fq=interpolate3DvecWrap( FFelec, grid.n, gpos ); fe.add_mul( fq, 1.0 );
//if(bDebug__)printf( "CPU[0] apos(%g,%g,%g) PLQ(%g,%g,%g)\n", pos.x,pos.y,pos.z, fp.w,fl.w,fq.w );
//printf( "fp(%g,%g,%g|%g)*(%g) + fl((%g,%g,%g|%g)*(%g) + fq(%g,%g,%g|%g)*(%g) \n", fp.x,fp.y,fp.z,fp.e, PLQ.x, fl.x,fl.y,fl.z,fl.e, PLQ.y, fq.x,fq.y,fq.z,fq.e, PLQ.z );
//printf( "E(%g,%g,%g) PLQ(%g,%g,%g)\n", fp.e,fl.e,fq.e, PLQ.x,PLQ.y,PLQ.z );
//fe.add_mul( interpolate3DvecWrap( FFPaul, grid.n, gpos ) , PLQ.x );
//fe.add_mul( interpolate3DvecWrap( FFLond, grid.n, gpos ) , PLQ.y );
//fe.add_mul( interpolate3DvecWrap( FFelec, grid.n, gpos ) , PLQ.z );
//f = interpolate3DvecWrap( FFLond, grid.n, gpos );
//printf( "p(%5.5e,%5.5e,%5.5e) g(%5.5e,%5.5e,%5.5e) f(%5.5e,%5.5e,%5.5e) \n", pos.x, pos.y, pos.z, gpos.x, gpos.y, gpos.z, f.x,f.y,f.z );
}
__attribute__((hot))
inline void addForce_surf( Vec3d pos, const Quat4f PLQ, Quat4f& f ) const {
//pos.add( shift0 );
if ( pos.z > grid.cell.c.z ){ pos.z = grid.dCell.c.z*-0.1 + grid.cell.c.z; }
else if( pos.z < 0 ){ pos.z = grid.dCell.c.z* 0.1; }
return addForce( pos, PLQ, f );
}
__attribute__((hot))
inline double eval( int n, const Vec3d* ps, const Quat4f* PLQs, Vec3d* fs, bool bSurf=false ) const {
double E=0;
//printf("GridFF::eval() n %i ps %li PLQs %li \n", n, (long)ps, (long)PLQs );
if(bSurf){ for(int i=0; i<n; i++){ Quat4f fe=Quat4fZero; addForce_surf( ps[i], (Quat4f)PLQs[i], fe ); fs[i].add( (Vec3d)fe.f ); E+=fe.e; } }
else { for(int i=0; i<n; i++){ Quat4f fe=Quat4fZero; bDebug__=(i==0); addForce ( ps[i], (Quat4f)PLQs[i], fe ); fs[i].add( (Vec3d)fe.f ); E+=fe.e;
//if(i==0)printf("CPU[0] apos(%g,%g,%g) PLQs[0](%g,%g,%g|%g) \n", n, ps[i].x,ps[i].y,ps[i].z, PLQs[i].x,PLQs[i].y,PLQs[i].z,alpha );
} }
return E;
}
inline double addAtom( const Vec3d& pos, const Quat4d& PLQ, Vec3d& fout )const{
Quat4d fed;
switch( mode ){
// void evalGridFFPoint( int natoms_, const Vec3d * apos_, const Quat4d * REQs_, Vec3d pos, Quat4d& qp, Quat4d& ql, Quat4d& qe )const{
case GridFFmod::Direct :{ Quat4d qp,ql,qe; evalGridFFPoint( apos_.size(), apos_.data(), REQs_.data(), pos, qp, ql, qe ); fed = qp*PLQ.x + ql*PLQ.y + qe*PLQ.z;
//printf( "GridFF::addAtom( E(%g,%g,%g|%g) PLQ(%g,%g,%g) pos(%g,%g,%g) \n", qp.e, ql.e, qe.e, fed.e, PLQ.x,PLQ.y,PLQ.z, pos.x,pos.y,pos.z );
} break;
case GridFFmod::LinearFloat :{ fed=(Quat4d)getForce( pos, (Quat4f)PLQ ); }break;
case GridFFmod::LinearDouble :{ fed=getForce_d ( pos, PLQ); }break;
case GridFFmod::HermiteDouble:{ fed=getForce_HHermit( pos, PLQ ); }break;
case GridFFmod::BsplineDouble:{ fed=getForce_Bspline( pos, PLQ ); }break;
//case GridFFmod::HermiteFloat: { }break;
//cast GridFFmod::BSplineFloat: { }break;
default: { printf("ERROR GridFF::eval() mode=%i NOT IMPLEMENTED !!! \n", mode); exit(0); }
}
//printf("GridFF::addAtom() FE(%10.5f,%10.5f,%10.5f|%10.5f) pos(%7.5f,%7.5f,%7.5f) PLQ(%7.5f,%10.6f,%7.3f) \n", fed.x,fed.y,fed.z,fed.w, pos.x,pos.y,pos.z, PLQ.x,PLQ.y,PLQ.z );
fout.add( fed.f );
//fout.sub( fed.f );
return fed.e;
}
Vec3d findIso(double isoval, Vec3d p0, Vec3d p1, const Quat4d PLQ, double xtol = 0.01, bool bPrintError=true )const{
//printf( "GridFF::findIso() iso=%g p0(%6.3f,%6.3f,%6.3f) p1(%6.3f,%6.3f,%6.3f) PLQ(%g,%g,%g,%g) xtol=%g \n", isoval, p0.x,p0.y,p0.z, p1.x,p1.y,p1.z, PLQ.x,PLQ.y,PLQ.z,PLQ.w, xtol );
Vec3d fout; // Force output vector (unused in binary search)
// Evaluate energy at the endpoints
double f0 = addAtom(p0, PLQ, fout)-isoval;
double f1 = addAtom(p1, PLQ, fout)-isoval;
//printf( "GridFF::findIso() iso=%g p0(%6.3f,%6.3f,%6.3f)f0=%fg p1(%6.3f,%6.3f,%6.3f)f1=%g PLQ(%g,%g,%g,%g) xtol=%g \n", isoval, p0.x,p0.y,p0.z,f0, p1.x,p1.y,p1.z,f1, PLQ.x,PLQ.y,PLQ.z,PLQ.w, xtol );
if( f0*f1 > 0.0) {
if(bPrintError) printf("ERROR GridFF::findIso() E1(%10.5e)*E2(%10.5e) > 0.0 | p0(%8.4f,%8.4f,%8.4f) p1(%8.4f,%8.4f,%8.4f) PLQ(%10.5e,%10.5e,%10.5e,%10.5e) \n", f0,f1, p0.x,p0.y,p0.z, p1.x,p1.y,p1.z, PLQ.x,PLQ.y,PLQ.z,PLQ.w );
//exit(0);
//return p0;
return Vec3dNAN;
}
double sgn = (f0 > 0.0) ? 1.0 : -1.0;
double r2tol = xtol*xtol;
Vec3d pmid;
int iter=0;
while ( (p0-p1).norm2() > r2tol ) {
pmid = (p0 + p1) * 0.5;
double fmid = addAtom(pmid, PLQ, fout);
if ( (fmid-isoval)*sgn < 0.0 ) { p1 = pmid; }
else { p0 = pmid; }
//printf( "p0.z=%6.3f p1.z=%6.3f fmid=%g \n", p0.z, p1.z, fmid );
iter++;
}
return pmid;
}
// =========================================================
// ================ Grid Preparation ============
// =========================================================
void init( Vec3i n, Mat3d cell, Vec3d pos0, bool bDouble=false ){
grid.n = n;
grid.setCell(cell);
grid.pos0 = pos0;
//zmax = cell.c.z;
allocateFFs( bDouble );
}
void setAtoms( int natoms_, Vec3d * apos_, Quat4d * REQs_ ){
natoms = natoms_;
//atypes = new int [natoms];
apos = apos_;
REQs = REQs_;
}
__attribute__((hot))
void evalGridFFel(int natoms, const Vec3d * apos, const Quat4d * REQs, Vec3d * FF )const{
//interateGrid3D( Vec3d{0.0,0.0,0.0}, grid.n, grid.dCell, [=](int ibuff, Vec3d p)->void{
interateGrid3D( grid, [=](int ibuff, Vec3d p)->void{
Vec3d f = Vec3dZero;
for(int ia=0; ia<natoms; ia++){ addAtomicForceQ( p-apos[ia], f, REQs[ia].z ); }
FF[ibuff]=f;
});
}
//__attribute__((pure))
__attribute__((hot))
void evalGridFFPoint( int natoms_, const Vec3d * apos_, const Quat4d * REQs_, Vec3d pos, Quat4d& qp, Quat4d& ql, Quat4d& qe )const{
//const Vec3d pos = grid.pos0 + grid.dCell.c*iz + grid.dCell.b*iy + grid.dCell.a*ix;
const double R2damp=Rdamp*Rdamp;
const double K=-alphaMorse;
qp = Quat4dZero;
ql = Quat4dZero;
qe = Quat4dZero;
//#pragma omp for simd
for(int ia=0; ia<natoms_; ia++){
const Vec3d dp0 = pos - apos_[ia];
const Quat4d REQi = REQs_[ia];
//if( (ibuff==0) ){ printf( "DEBUG a[%i] p(%g,%g,%g) Q %g \n", ia,apos_[ia].x, apos_[ia].y, apos[ia].z, REQi.z ); }
for(int ipbc=0; ipbc<npbc; ipbc++ ){
const Vec3d dp = dp0 + shifts[ipbc];
//Vec3d dp = dp0;
const double r2 = dp.norm2();
const double r = sqrt(r2 + 1e-32);
// ---- Coulomb
const double ir2 = 1/(r2+R2damp);
const double eQ = COULOMB_CONST*REQi.z*sqrt(ir2);
// ----- Morse
const double e = exp( K*(r-REQi.x) );
const double eM = e*REQi.y;
const double de = 2*K*eM/r;
// --- store
qp.e+=eM*e; qp.f.add_mul( dp, -de*e ); // repulsive part of Morse
ql.e+=eM*-2.; ql.f.add_mul( dp, de ); // attractive part of Morse
qe.e+=eQ; qe.f.add_mul( dp, eQ*ir2 ); // Coulomb
//qp.e += exp( -r2/0.16 ); // Debug
}
}
//printf( "evalGridFFPoint() E_PLQ(%g,%g,%g) pos(%g,%g,%g) \n", qp.e, ql.e, qe.e, pos.x, pos.y, pos.z );
//const int ibuff = ix + grid.n.x*( iy + grid.n.y * iz );
//FFPaul[ibuff]=(Quat4f)qp;
//FFLond[ibuff]=(Quat4f)ql;
//FFelec[ibuff]=(Quat4f)qe;
}
__attribute__((hot))
void evalGridFFPoint_Mors( int npbc, const Vec3d* shifts, int natoms_, const Vec3d * apos_, const Quat4d * REQs_, Vec3d pos, Quat4d& qp, Quat4d& ql )const{
//const Vec3d pos = grid.pos0 + grid.dCell.c*iz + grid.dCell.b*iy + grid.dCell.a*ix;
//const double R2damp=Rdamp*Rdamp;
const double K=-alphaMorse;
qp = Quat4dZero;
ql = Quat4dZero;
//#pragma omp for simd
for(int ia=0; ia<natoms_; ia++){
const Vec3d dp0 = pos - apos_[ia];
const Quat4d REQi = REQs_[ia];
//if( (ibuff==0) ){ printf( "DEBUG a[%i] p(%g,%g,%g) Q %g \n", ia,apos_[ia].x, apos_[ia].y, apos[ia].z, REQi.z ); }
for(int ipbc=0; ipbc<npbc; ipbc++ ){
const Vec3d dp = dp0 + shifts[ipbc];
//Vec3d dp = dp0;
const double r2 = dp.norm2();
const double r = sqrt(r2 + 1e-32);
// ----- Morse
const double e = exp( K*(r-REQi.x) );
const double eM = e*REQi.y;
const double de = 2*K*eM/r;
//if( (iDBG>=0) && (ia==0) && (ipbc==npbc/2) ){ printf( "evalGridFFPoint_Mors() dp(%8.4f,%8.4f,%8.4f|r=%8.4f) e=%g EPaul=%g ELond=%g alphaMorse=%g R0=%g E0=%g shift(%8.4f,%8.4f,%8.4f) apos(%8.4f,%8.4f,%8.4f) \n", dp.x,dp.y,dp.z, r, e, eM*e, eM*-2., alphaMorse, REQi.x, REQi.y, shifts[ipbc].x,shifts[ipbc].y,shifts[ipbc].z, apos_[ia].x,apos_[ia].y,apos_[ia].z ); };
// --- store
qp.e+=eM*e; qp.f.add_mul( dp, -de*e ); // repulsive part of Morse
ql.e+=eM*-2.; ql.f.add_mul( dp, de ); // attractive part of Morse
}
}
}
__attribute__((pure))
__attribute__((hot))
Quat4d evalGridFFPoint_Coul( int npbc, const Vec3d* shifts, int natoms_, const Vec3d * apos_, const Quat4d * REQs_, Vec3d pos ) const {
//const double R2damp=Rdamp*Rdamp;
const double R2damp=1e-32;
//const double K=-alphaMorse;
Quat4d qe = Quat4dZero;
//#pragma omp for simd
for(int ia=0; ia<natoms_; ia++){
const Vec3d dp0 = pos - apos_[ia];
const Quat4d REQi = REQs_[ia];
//if( (ibuff==0) ){ printf( "DEBUG a[%i] p(%g,%g,%g) Q %g \n", ia,apos_[ia].x, apos_[ia].y, apos[ia].z, REQi.z ); }
for(int ipbc=0; ipbc<npbc; ipbc++ ){
const Vec3d dp = dp0 + shifts[ipbc];
const double r2 = dp.norm2();
const double ir2 = 1/(r2+R2damp);
const double eQ = COULOMB_CONST*REQi.z*sqrt(ir2);
qe.e+=eQ; qe.f.add_mul( dp, eQ*ir2 ); // Coulomb
}
}
return qe;
}
__attribute__((pure))
__attribute__((hot))
Quat4d evalGridFFPoint_REQ( Quat4d REQ, int npbc, const Vec3d* shifts, int natoms_, const Vec3d * apos_, const Quat4d * REQs_, Vec3d pos ) const {
//const double R2damp=Rdamp*Rdamp;
const double R2damp=1e-32;
const double K=-alphaMorse;
Quat4d qe = Quat4dZero;
//#pragma omp for simd
for(int ia=0; ia<natoms_; ia++){
const Vec3d dp0 = pos - apos_[ia];
Quat4d REQ; combineREQ( REQ, REQs_[ia], REQ );
//if( (ibuff==0) ){ printf( "DEBUG a[%i] p(%g,%g,%g) Q %g \n", ia,apos_[ia].x, apos_[ia].y, apos[ia].z, REQi.z ); }
for(int ipbc=0; ipbc<npbc; ipbc++ ){
const Vec3d dp = dp0 + shifts[ipbc];
const double r2 = dp.norm2();
const double ir2 = 1/(r2+R2damp);
const double r = sqrt( r2 );
const double eQ = COULOMB_CONST*REQ.z*sqrt(ir2);
// Morse:
const double e = exp( K*(r-REQ.x) );
const double Ae = REQ.y*e;
//const double He = e * ( (REQ.w<0) ? REQH.w*REQH.y : 0.0 ); // H-bond correction
const double E = Ae*(e - 2) + eQ;
const double F = (Ae*(e - 1)*2 )*-K/r - eQ*ir2;
qe.f.set_mul( dp, F );
qe.e = E;
}
}
return qe;
}
Quat4d evalMorsePBC_PLQ( Vec3d pi, Quat4d PLQH, int natoms, const Vec3d * apos, const Quat4d * REQs )const{
//printf( "GridFF::evalMorsePBC() debug fi(%g,%g,%g) REQi(%g,%g,%g)\n", fi.x,fi.y,fi.z, REQi.x,REQi.y,REQi.z,REQi.w );
Quat4d qp,ql,qe;
evalGridFFPoint( natoms, apos, REQs, pi, qp, ql, qe );
return qp*PLQH.x + ql*PLQH.y + qe*PLQH.z;
}
Quat4d evalMorsePBC_PLQ_sym( Vec3d pi, Quat4d PLQH )const{ return evalMorsePBC_PLQ( pi, PLQH, apos_.size(), &apos_[0], &REQs_[0] ); }
// Quat4d evalMorsePBCatoms_PLQ_sym( int na, Vec3d* ps, Quat4d* REQs, Vec3d* forces ){
// double E = 0;
// for(int ia=0; ia<na; ia++){ evalMorsePBC_PLQ_sym( ps[ia], REQs[ia], forces[ia] ); };
// return E;
// }
void evalAtPoints_REQ( int n, Vec3d* ps, Quat4d* FFout, Quat4d REQ, int natoms_, Vec3d * apos_, Quat4d * REQs_, Vec3i* nPBC_=0 ){
Vec3i nPBC__ = nPBC;
if(nPBC_) nPBC__=*nPBC_;
//printf( "GridFF::evalAtPoints_Split() n=%i natoms_=%i bCoul=%i bMors=%i nPBC_Coul(%i,%i,%i) nPBC_Coul(%i,%i,%i)\n", n, natoms_, bCoul,bMors, nPBC_coul.x,nPBC_coul.y,nPBC_coul.z, nPBC_mors.x,nPBC_mors.y,nPBC_mors.z );
int i=0;
Vec3d* shifts=0;
int npbc=makePBCshifts_( nPBC__, lvec, shifts );;
printf( "GridFF::evalAtPoints_REQ() n=%i natoms_=%i REQ(%g,%g,%g,%g) nPBC(%i,%i,%i|%i)\n", n, natoms_, REQ.x,REQ.y,REQ.z,REQ.w, nPBC.x,nPBC.y,nPBC.z, npbc );
#pragma omp parallel for shared(i)
for(int i=0; i<n; i++){
FFout[i] = evalGridFFPoint_REQ( REQ, npbc, shifts, natoms_, apos_, REQs_, ps[i] );
}
//double T = (getCPUticks()-t0); printf( "evalAtPoints_Split(n=%i) time=%g[MTick] %g[kTick/point]\n", n, T*(1.e-6), (T*1e-3)/n );
delete[] shifts;
}
void evalAtPoints_REQ( int n, Vec3d* ps, Quat4d* FFout, Quat4d REQH, Vec3i* nPBC=0 ){ evalAtPoints_REQ( n, ps, FFout, REQH, natoms, apos, REQs, nPBC ); };
void evalAtPoints( int n, const Vec3d* ps, Quat4d* FFout, Quat4d PLQH, int natoms_, const Vec3d * apos_, const Quat4d * REQs_, Vec3i* nPBC=0 ){
//printf( "GridFF::evalAtPoints() n=%i natoms_=%i \n", n, natoms_ );
int i=0;
// backup
Vec3d* shifts_=shifts;
int npbc_ = npbc;
bool bUserPBC = (nPBC!=0);
if( bUserPBC ){
shifts=0;
npbc = makePBCshifts_( *nPBC, lvec, shifts );
}
#pragma omp parallel for shared(i)
for(int i=0; i<n; i++){
Quat4d qp,ql,qe;
//printf( "ps[%i](%7.3f,%7.3f,%7.3f)\n", i, ps[i].x,ps[i].y,ps[i].z );
evalGridFFPoint( natoms_, apos_, REQs_, ps[i], qp, ql, qe );
FFout[i] = qp*PLQH.x + ql*PLQH.y + qe*PLQH.z;
}
// restore
if( bUserPBC ){
npbc = npbc_;
shifts = shifts_;
}
}
//void evalAtPoints( int n, Vec3d* ps, Quat4d* FFout, Quat4d PLQH ){ evalAtPoints( n, ps, FFout, PLQH, natoms, apos, REQs ); };
void evalAtPoints( int n, const Vec3d* ps, Quat4d* FFout, Quat4d PLQH, Vec3i* nPBC=0 ){ evalAtPoints( n, ps, FFout, PLQH, apos_.size(), apos_.data(), REQs_.data(), nPBC ); };
void evalAtPoints_Split( int n, Vec3d* ps, Quat4d* FFout, Quat4d PLQH, int natoms_, Vec3d * apos_, Quat4d * REQs_, Vec3i* nPBC=0 ){
Vec3i nPBC_mors{4,4,0};
Vec3i nPBC_coul{25,25,0}; if(nPBC) nPBC_coul=*nPBC;
bool bCoul = fabs(PLQH.z) > 1e-32;
bool bMors = fmax( fabs(PLQH.x), fabs(PLQH.y) ) > 1e-32;
//printf( "GridFF::evalAtPoints_Split() n=%i natoms_=%i bCoul=%i bMors=%i nPBC_Coul(%i,%i,%i) nPBC_Coul(%i,%i,%i)\n", n, natoms_, bCoul,bMors, nPBC_coul.x,nPBC_coul.y,nPBC_coul.z, nPBC_mors.x,nPBC_mors.y,nPBC_mors.z );
int i=0;
Vec3d* shift_coul=0;
Vec3d* shift_mors=0;
int npbc_coul=0;
int npbc_mors=0;
if(bCoul){ npbc_coul = makePBCshifts_( nPBC_coul, lvec, shift_coul ); } // Coulomb converge much more slowly with nPBC
if(bMors){ npbc_mors = makePBCshifts_( nPBC_mors, lvec, shift_mors ); }
//#pragma omp parallel for shared(i)
//long t0 = getCPUticks();
//printf( "GridFF::evalAtPoints_Split() npbc_coul=%i npbc_mors=%i \n", npbc_coul, npbc_mors );
printf( "GridFF::evalAtPoints_Split() n=%i natoms_=%i PLQH(%g,%g,%g,%g) bCoul=%i bMors=%i nPBC_Coul(%i,%i,%i|%i) nPBC_mors(%i,%i,%i|%i)\n", n, natoms_, bCoul,bMors, PLQH.x,PLQH.y,PLQH.z,PLQH.w, nPBC_coul.x,nPBC_coul.y,nPBC_coul.z,npbc_coul, nPBC_mors.x,nPBC_mors.y,nPBC_mors.z,npbc_mors );
#pragma omp parallel for shared(i)
for(int i=0; i<n; i++){
Quat4d qp=Quat4dZero,ql=Quat4dZero,qe=Quat4dZero;
if(bMors){ evalGridFFPoint_Mors( npbc_mors, shift_mors, natoms_, apos_, REQs_, ps[i], qp, ql ); }
if(bCoul){ qe=evalGridFFPoint_Coul( npbc_coul, shift_coul, natoms_, apos_, REQs_, ps[i] ); }
FFout[i] = qp*PLQH.x + ql*PLQH.y + qe*PLQH.z;
}
//double T = (getCPUticks()-t0); printf( "evalAtPoints_Split(n=%i) time=%g[MTick] %g[kTick/point]\n", n, T*(1.e-6), (T*1e-3)/n );
delete[] shift_coul;
delete[] shift_mors;
}
//void evalAtPoints_Split( int n, Vec3d* ps, Quat4d* FFout, Quat4d PLQH, Vec3i* nPBC=0 ){ evalAtPoints_Split( n, ps, FFout, PLQH, apos_.size(), apos_.data(), REQs_.data(), nPBC ); };
void evalAtPoints_Split( int n, Vec3d* ps, Quat4d* FFout, Quat4d PLQH, Vec3i* nPBC=0 ){ evalAtPoints_Split( n, ps, FFout, PLQH, natoms, apos, REQs, nPBC ); };
__attribute__((hot))
void makeGridFF_omp(int natoms_, Vec3d * apos_, Quat4d * REQs_ ){
printf( "GridFF::makeGridFF_omp() nPBC(%i,%i,%i) npbc=%i natoms_=%i pos0(%g,%g,%g)\n", nPBC.x,nPBC.y,nPBC.z, npbc, natoms_, grid.pos0.x,grid.pos0.y,grid.pos0.z );
if(shifts==0)makePBCshifts( nPBC, lvec );
const double R2damp=Rdamp*Rdamp;
const double K=-alphaMorse;
//for(int i=0; i<npbc; i++ ){ printf( "shifts[%i](%g,%g,%g) \n", i, shifts[i].x, shifts[i].y, shifts[i].z ); };
//for(int ia=0; ia<natoms_; ia++) { printf( "apos[%i] pos(%g,%g,%g) REQ(%g,%g,%g,%g) \n", ia, apos_[ia].x, apos_[ia].y, apos_[ia].z, REQs_[ia].x, REQs_[ia].y, REQs_[ia].z, REQs_[ia].w ); };
int ix=0,iy=0,iz=0;
#pragma omp parallel for shared(ix,iy,iz,FFPaul,FFLond,FFelec) collapse(3)
for ( iz=0; iz<grid.n.z; iz++ ){
for ( iy=0; iy<grid.n.y; iy++ ){
for ( ix=0; ix<grid.n.x; ix++ ){
Quat4d qp,ql,qe;
const Vec3d pos = grid.pos0 + grid.dCell.c*iz + grid.dCell.b*iy + grid.dCell.a*ix;
evalGridFFPoint( natoms_, apos_, REQs_, pos, qp, ql, qe );
const int ibuff = ix + grid.n.x*( iy + grid.n.y * iz );
FFPaul[ibuff]=(Quat4f)qp;
FFLond[ibuff]=(Quat4f)ql;
FFelec[ibuff]=(Quat4f)qe;
}
}
}
}
void makeGridFF(){ makeGridFF_omp(natoms,apos,REQs); }
__attribute__((hot))
void makeGridFF_omp_d(int natoms_, Vec3d * apos_, Quat4d * REQs_ ){
printf( "GridFF::makeGridFF_omp_d() nPBC(%i,%i,%i) pos0(%g,%g,%g)\n", nPBC.x,nPBC.y,nPBC.z, grid.pos0.x,grid.pos0.y,grid.pos0.z );
if(shifts==0)makePBCshifts( nPBC, lvec );
const double R2damp=Rdamp*Rdamp;
const double K=-alphaMorse;
int ix=0,iy=0,iz=0;
#pragma omp parallel for shared(ix,iy,iz,FFPaul,FFLond,FFelec) collapse(3)
for ( iz=0; iz<grid.n.z; iz++ ){
for ( iy=0; iy<grid.n.y; iy++ ){
for ( ix=0; ix<grid.n.x; ix++ ){
Quat4d qp,ql,qe;
const Vec3d pos = grid.pos0 + grid.dCell.c*iz + grid.dCell.b*iy + grid.dCell.a*ix;
evalGridFFPoint( natoms_, apos_, REQs_, pos, qp, ql, qe );
const int ibuff = ix + grid.n.x*( iy + grid.n.y * iz );
FFPaul_d[ibuff]=qp;
FFLond_d[ibuff]=ql;
FFelec_d[ibuff]=qe;
}
}
}
}
void makeGridFF_d(){ makeGridFF_omp_d(natoms,apos,REQs); }
__attribute__((hot))
void makeGridFF_Hherm_d(int natoms_, Vec3d * apos_, Quat4d * REQs_ ){
//printf( "GridFF::makeGridFF_Hherm_d() grid.n(%i,%i,%i) nPBC(%i,%i,%i) npbc=%i natoms=%i pos0(%g,%g,%g) K=%g Rdamp=%g \n", grid.n.x,grid.n.y,grid.n.z, nPBC.x,nPBC.y,nPBC.z, npbc, natoms_, grid.pos0.x,grid.pos0.y,grid.pos0.z, alphaMorse, Rdamp );
//printf( "GridFF::makeGridFF_Hherm_d() grid.n(%i,%i,%i) gridN(%i,%i,%i) \n", grid.n.x,grid.n.y,grid.n.z, gridN.x,gridN.y,gridN.z );
//grid.printCell();
//if(HHermite_d==0) _realloc( HHermite_d, grid.n.totprod()*6 );
if(HHermite_d==0) _realloc( HHermite_d, gridN.totprod()*6 );
if(shifts==0)makePBCshifts( nPBC, lvec );
double dz = -grid.dCell.c.z;
//const int ntot = grid.n.totprod();
//const int nzy = grid.n.z*grid.n.y;
const int ntot = gridN.totprod();
const int nzy = gridN.z*gridN.y;
int i=0;
#pragma omp parallel for shared(i,HHermite_d)
for(int i=0; i<ntot; i++){
//int iz = i%grid.n.z;
//int iy = (i/grid.n.z)%grid.n.y;
int iz = i%gridN.z;
int iy = (i/gridN.z)%gridN.y;
int ix = i/nzy;
Quat4d qp,ql,qe;
//const Vec3d pos = grid.pos0 + grid.dCell.c*iz + grid.dCell.b*iy + grid.dCell.a*ix;
const Vec3d pos = grid.pos0 + grid.dCell.c*iz + grid.dCell.b*(iy-1) + grid.dCell.a*(ix-1);
evalGridFFPoint( natoms_, apos_, REQs_, pos, qp, ql, qe );
//const int ibuff = ix + grid.n.x*( iy + grid.n.y * iz );
const int ibuff = iz + gridN.z*( iy + gridN.y * ix );
int i6 = ibuff*6;
HHermite_d[i6+0] = qp.w; HHermite_d[i6+1] = qp.z*dz;
HHermite_d[i6+2] = ql.w; HHermite_d[i6+3] = ql.z*dz;
HHermite_d[i6+4] = qe.w; HHermite_d[i6+5] = qe.z*dz;
}
printf( "GridFF::makeGridFF_Hherm_d() DONE \n" );
}
void makeGridFF_Hherm_d(){ makeGridFF_Hherm_d(natoms,apos,REQs); }
__attribute__((hot))
void evalBsplineRef(int natoms_, Vec3d * apos_, Quat4d * REQs_, double* VPaul, double* VLond, double* VCoul ){
Vec3i nPBC_coul=Vec3i{25,25,0};
Vec3i nBPC_mors=nPBC;
bool bEvalCoulomb = !bUseEwald;
printf( "GridFF::evalBsplineRef() natoms=%i Rdamp=%g alphaMorse=%g bEvalCoulomb=%i nBPC_mors(%i,%i,%i) nPBC_coul(%i,%i,%i) @VPaul=%li @VLond=%li @VCoul=%li \n", natoms_, Rdamp, alphaMorse, bEvalCoulomb, nBPC_mors.x,nBPC_mors.y,nBPC_mors.z, nPBC_coul.x,nPBC_coul.y,nPBC_coul.z, (long)VPaul, (long)VLond, (long)VCoul );
Vec3d* shift_coul=0; int npbc_coul=0;
Vec3d* shift_mors=0;
if(bEvalCoulomb) npbc_coul = makePBCshifts_( nPBC_coul, lvec, shift_coul ); // Coulomb converge much more slowly with nPBC
int npbc_mors = makePBCshifts_( nBPC_mors, lvec, shift_mors );
double dz = -grid.dCell.c.z;
Vec3i ns = grid.n;
//Vec3i ns = gridN;
const int ntot = ns.totprod();
const int nxy = ns.x*ns.y;
int i=0;
long t0=getCPUticks();
//#pragma omp parallel for shared(i,VPaul,VLond,VCoul)
for(int i=0; i<ntot; i++){
//int iz = i%grid.n.z;
//int iy = (i/grid.n.z)%grid.n.y;
int iz = i/nxy;
int ixy = i - iz*nxy;
int iy = ixy/ns.x;
int ix = ixy - iy*ns.x;
const Vec3d pos = grid.pos0 + grid.dCell.c*iz + grid.dCell.b*iy + grid.dCell.a*ix;
// if((ix==0)&&(iy==0)){ iDBG=iz;
// printf( "CPU pos(%8.4f,%8.4f,%8.4f) iz=%i ", pos.x,pos.y,pos.z );
// }else{ iDBG=-1; }
//printf( "GridFF::evalBsplineRef()[%8i] ixyz(%3i,%3i,%3i) pos(%8.4f,%8.4f,%8.4f)\n", i, ix,iy,iz, pos.x,pos.y,pos.z );
Quat4d qp,ql;evalGridFFPoint_Mors( npbc_mors, shift_mors, natoms_, apos_, REQs_, pos, qp, ql );
const int ibuff = ix + ns.x*( iy + ns.y * iz );
VPaul[i] = qp.w;
VLond[i] = ql.w;
if(bEvalCoulomb){
Quat4d qe = evalGridFFPoint_Coul( npbc_coul, shift_coul, natoms_, apos_, REQs_, pos );
VCoul[i] = qe.w;
}
}
double T=getCPUticks()-t0;
if(bEvalCoulomb){ delete [] shift_coul; }
delete [] shift_mors;
printf( "GridFF::evalBsplineRef(ntot=%i,natom=%i,nPBC(%i,%i,%i)(coul=%i,morse=%i) DONE in %g [GTick] %g [kTick/point] \n", ntot, natoms_, nPBC.x,nPBC.y,nPBC.z, npbc_coul, shift_mors, T*1e-9, (T*1e-3)/ntot );
}
void makeVPLQHeval( int natoms_, Vec3d * apos_, Quat4d * REQs_ ){
printf( "GridFF::makeVPLQHeval() \n" );
gridN.x = grid.n.x+3;
gridN.y = grid.n.y+3;
gridN.z = grid.n.z+3;
FEscale.set( -grid.diCell.xx, -grid.diCell.yy, -grid.diCell.zz, 1.0 ); // NOTE: this will not work for non-orthogonal grids
_realloc0( VPLQH, gridN.totprod(), Quat4dNAN );
//_realloc0( V_debug, gridN.totprod(), 0.0/0.0 );
for ( int iz=0; iz<gridN.z; iz++ ){
int iz_ = fold_cubic( iz, grid.n.z );
for ( int iy=0; iy<gridN.y; iy++ ){
int iy_ = fold_cubic( iy, grid.n.y );
for ( int ix=0; ix<gridN.x; ix++ ){
int ix_ = fold_cubic( ix, grid.n.x );
Quat4d qp,ql,qe;
const Vec3d pos = grid.pos0 + grid.dCell.c*iz_ + grid.dCell.b*iy_ + grid.dCell.a*ix_;
evalGridFFPoint( natoms_, apos_, REQs_, pos, qp, ql, qe );
const int i = ix + gridN.x* ( iy + gridN.y * iz );
VPLQH[ i ] = Quat4d{ qp.w, ql.w, qe.w, 0.0 };
//V_debug[ i ] = FFelec_d[j].w; // debug
}
}
}
printf( "GridFF::makeVPLQHeval() DONE\n" );
}
template<typename T>
void copyPitch( int n, T* dst, int i0dst, int mdst, const T* src, int i0src, int msrc ){
printf( "copyPitch() n=%i @src=%li i0src=%i msrc=%i @dst=%li i0dst=%i mdst=%i \n", n, (long)src, i0src, msrc, (long)dst, i0dst, mdst );
for ( int i=0; i<n; i++ ){
//if((i%1000)==0){ printf("copyPitch()[%i/%i]\n", i, n); }
dst[ i*mdst+i0dst ] = src[ i*msrc+i0src ];
}
}
template<typename T>
void copyPitchTransp( Vec3i ndst, Vec3i transp, T* dst, int i0dst, int mdst, const T* src, int i0src, int msrc ){
Vec3i nsrc = Vec3i{ ndst.array[transp.x], ndst.array[transp.y], ndst.array[transp.z] };
printf( "copyPitchTransp() ndst(%i,%i,%i) nsrc(%i,%i,%i) @src=%li i0src=%i msrc=%i @dst=%li i0dst=%i mdst=%i \n", ndst.x,ndst.y,ndst.z, nsrc.x,nsrc.y,nsrc.z, (long)src, i0src, msrc, (long)dst, i0dst, mdst );
Vec3i i;
for( i.z=0; i.z<ndst.z; i.z++ ){ for( i.y=0; i.y<ndst.y; i.y++ ){ for( i.x=0; i.x<ndst.x; i.x++ ){
const Vec3i i_ = Vec3i{ i.array[transp.x], i.array[transp.y], i.array[transp.z] };
const int idst = i.x + ndst.x*( i.y + ndst.y * i.z );
const int isrc = i_.x + nsrc.x*( i_.y + nsrc.y * i_.z );
dst[ idst*mdst+i0dst ] = src[ isrc*msrc+i0src ];
}}}
}
template<typename T>
void copyPBC( Vec3i nsrc, T* src, int i0src, int msrc, Vec3i ndst, T* dst, int i0dst, int mdst ){
for ( int iz=0; iz<ndst.z; iz++ ){
int iz_ = fold_cubic( iz, nsrc.z );
for ( int iy=0; iy<ndst.y; iy++ ){
int iy_ = fold_cubic( iy, nsrc.y );
for ( int ix=0; ix<ndst.x; ix++ ){
int ix_ = fold_cubic( ix, nsrc.x );
const int i = ix + ndst.x*( iy + ndst.y * iz );
const int j = ix_ + nsrc.x*( iy_ + nsrc.y * iz_ );
src[ i*mdst+i0dst ] = dst[ j*msrc+i0src ];
//V_debug[ i ] = FFelec_d[j].w; // debug
}
}
}
}
void makeVPLQH(){
printf( "GridFF::makeVPLQH() \n" );
gridN.x = grid.n.x+3;
gridN.y = grid.n.y+3;
gridN.z = grid.n.z+3;
FEscale.set( -grid.diCell.xx, -grid.diCell.yy, -grid.diCell.zz, 1.0 ); // NOTE: this will not work for non-orthogonal grids
_realloc0( VPLQH, gridN.totprod(), Quat4dNAN );
//_realloc0( V_debug, gridN.totprod(), 0.0/0.0 );
for ( int iz=0; iz<gridN.z; iz++ ){
int iz_ = fold_cubic( iz, grid.n.z );
for ( int iy=0; iy<gridN.y; iy++ ){
int iy_ = fold_cubic( iy, grid.n.y );
for ( int ix=0; ix<gridN.x; ix++ ){
int ix_ = fold_cubic( ix, grid.n.x );
const int i = ix + gridN.x* ( iy + gridN.y * iz );
const int j = ix_ + grid.n.x*( iy_ + grid.n.y * iz_ );
//if( (iz==0)&&(iy==0) ){ printf( "GridFF::makeVPLQH() ix(%i) -> ix(%i)\n", ix, ix_ ); }
VPLQH[ i ] = Quat4d{ FFPaul_d[j].w, FFLond_d[j].w, FFelec_d[j].w, 0.0 };
//V_debug[ i ] = FFelec_d[j].w; // debug
}
}
}
printf( "GridFF::makeVPLQH() DONE\n" );
}
void FitBsplines( double Ftol=1e-8, int nmaxiter=1000, double dt=0.1 ){
printf( "GridFF::FitBsplines() \n" );
int n = gridN.totprod();
double* Vtemp = new double[ n ];
double* Ws = new double[ n ]; for(int i=0; i<n; i++){ Ws[i] = 1.0; }
copyPBC<double>( gridN, Vtemp,0,1, grid.n, (double*)FFPaul_d, 3,4 ); Bspline::fit3D( gridN, Bspline_Pauli, Vtemp, Ws, Ftol, nmaxiter, dt );
copyPBC<double>( gridN, Vtemp,0,1, grid.n, (double*)FFLond_d, 3,4 ); Bspline::fit3D( gridN, Bspline_London, Vtemp, Ws, Ftol, nmaxiter, dt );
copyPBC<double>( gridN, Vtemp,0,1, grid.n, (double*)FFelec_d, 3,4 ); Bspline::fit3D( gridN, Bspline_Coulomb, Vtemp, Ws, Ftol, nmaxiter, dt );
delete [] Vtemp;
printf( "GridFF::FitBsplines() DONE\n" );
}
[[deprecated]]
void makeGridFF_Bspline_HH_d( double Ftol=1e-8, int nmaxiter=1000, double dt=0.3 ){
int n = gridN.totprod();
printf( "GridFF::makeGridFF_Bspline_d() n=%i nmaxiter=%i Ftol=%g dt=%g \n", n );
_realloc( Bspline_Pauli, n );
_realloc( Bspline_London, n );
_realloc( Bspline_Coulomb, n );
double* Vtemp = new double[ n ];
//double* Ws = new double[ n ]; for(int i=0; i<n; i++){ Ws[i] = 1.0; }
printf( "GridFF::makeGridFF_Bspline_d() START Fitting @Vtemp=%li @HHermite_d=%li \n", (long)Vtemp, (long)HHermite_d );
//Vec3i nsrc{gridN.z,gridN.y,gridN.x};
Vec3i transp{2,1,0};
Vec3i nsrc=gridN; nsrc.swap( transp );
printf( "GridFF::makeGridFF_Bspline_d() nsrc(%i,%i,%i) transp(%i,%i,%i) \n", nsrc.x, nsrc.y, nsrc.z, transp.x, transp.y, transp.z );
copyPitchTransp<double>( gridN, transp, Vtemp,0,1, (double*)HHermite_d, 0,6 ); Bspline::fit3D_omp( gridN, Bspline_Pauli, Vtemp, 0, Ftol, nmaxiter, dt, true ); // printf( "GridFF::makeGridFF_Bspline_d() Fit(Bspline_Pauli) DONE \n" ); //gBS.saveXSF( "debug_VtempPaul.xsf", Vtemp, 1,0 ); gBS.saveXSF( "debug_BsplinePaul.xsf", Bspline_Pauli, 1,0 );
copyPitchTransp<double>( gridN, transp, Vtemp,0,1, (double*)HHermite_d, 2,6 ); Bspline::fit3D_omp( gridN, Bspline_London, Vtemp, 0, Ftol, nmaxiter, dt, true ); // printf( "GridFF::makeGridFF_Bspline_d() Fit(Bspline_London) DONE \n" ); //gBS.saveXSF( "debug_VtempLond.xsf", Vtemp, 1,0 ); gBS.saveXSF( "debug_BsplineLond.xsf", Bspline_London, 1,0 );
copyPitchTransp<double>( gridN, transp, Vtemp,0,1, (double*)HHermite_d, 4,6 ); Bspline::fit3D_omp( gridN, Bspline_Coulomb, Vtemp, 0, Ftol, nmaxiter, dt, true ); // printf( "GridFF::makeGridFF_Bspline_d() Fit(Bspline_Coulomb) DONE \n" ); //gBS.saveXSF( "debug_VtempCoul.xsf", Vtemp, 1,0 ); gBS.saveXSF( "debug_BsplineCoul.xsf", Bspline_Coulomb, 1,0 );
printf( "GridFF::makeGridFF_Bspline_d() FINISHED Fitting \n" );
delete [] Vtemp;
//delete [] Ws;
printf( "GridFF::makeGridFF_Bspline_d() DONE\n" );
}