-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsolver_benchmark.c
More file actions
4019 lines (3564 loc) · 175 KB
/
solver_benchmark.c
File metadata and controls
4019 lines (3564 loc) · 175 KB
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
//
////
////////
////////////////
////////////////////////////////
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// COMPANION SOFTWARE TO: Assessment of localized and randomized algorithms for electronic structure
// USAGE: <executable> <xyz structure file> <chemical potential> <temperature> <solver> <solver parameters ...>
// C99 syntax, OpenMP-based shared memory parallelism, designed to run on a single multi-core supercomputer node
// MPI parallelism is included for PEXSI functionality (solver=2) & all other solvers should use 1 MPI process
// MPI is being used w/ an underlying shared-memory (e.g. single node) system in mind (i.e. nonuniform memory distribution)
// UNITS: energies/temperatures & distances are in electronvolts/Angstroms for input/output & Rydbergs/Bohr internally
// stress tensor is in gigapascals (GPa)
// Available solvers:
// # | F-D approx. | trace approx. | O(N^p) | solver parameters
//----+-------------+-------------------+--------+-------------------
// 0 | none | none | 1 | none [pre & post processing only]
// 1 | exact | exact | 3 | none
// 2 | rational | exact (PEXSI) | 2 (3D) | <#/2 of poles>
// 3 | polynomial | exact (iterative) | 2 | <# of Cheby.> <res. tol.>
// 4 | rational | exact (iterative) | 2 | <#/2 of poles> <res. tol.>
// 5 | polynomial | local | 1 | <# of Cheby.> <res. tol.> <loc. rad.>
// 6 | rational | local | 1 | <#/2 of poles> <res. tol.> <loc. rad.>
// 7 | polynomial | random | 1 | <# of Cheby.> <res. tol.> <loc. rad.> <seed> <# of samples>
// 8 | rational | random | 1 | <#/2 of poles> <res. tol.> <loc. rad.> <seed> <# of samples>
// 9 | rational | local (infinite) | 0 | <#/2 of poles> <res. tol.> <loc. rad.>
// 10 | exact | k-grid (infinite) | 0 | <# of k-grid pts. per dimension> <loc. rad.>
// Available testers:
// -1 | none | local (infinite) | 0 | <res. tol.> <min. rad.> <max. rad.> <# rad.> [precondition test]
// INPUT KEY:
// <#/2 of poles> : number of complex-conjugate pole pairs in the rational approximation of the Fermi-Dirac function
// <# of Cheby.> : number of Chebyshev polynomials used to approximate the Fermi-Dirac function
// <res. tol.> : residual 2-norm stopping criterion for iterative linear solvers (conjugate gradient & conjugate residual)
// <loc. rad.> : localization radius that defines the sparsity pattern of local Hamiltonians (solver = 5,6)
// & the coloring scheme for uncorrelated complex rotors (solver = 7,8)
// <seed> : integer seed for the pseudo-random number generator
// <# of samples> : the number of samples drawn from the colored complex rotor multi-vector distribution
// <# of k-grid pts. per dimension> : the number of points assigned to the k-point grid per reciprocal-space dimension (3)
// <min. rad.> <max. rad.> <# rad.> : minimum/maximum/number-of radius values for a grid of preconditioner localization radii
// Structure file format (*.xyz) for monoatomic copper clusters:
// <# of atoms>
//
// Cu <x coordinate of atom #1> <y coordinate of atom #1> <z coordinate of atom #1>
// ...
// Cu <x coordinate of atom #N> <y coordinate of atom #N> <z coordinate of atom #N>
// NOTE: for solver = 9, the positions of the 2nd, 3rd, & 4th atoms relative to the 1st define the crystal lattice vectors
// OUTPUT:
// Total number of electrons, total energy, & atomic forces to standard output
// Memory & time usage to standard output (the only output for solver = 0 & -1)
// Density & response matrix elements in the Hamiltonian sparsity pattern to "debug.out"
// F-norm for off-diagonal blocks of density & response matrices in "decay.out" (solver = 9 & 10 only)
// Fermi-smeared electronic density-of-states to "dos.out" (solver = 10 only)
// RECOMMENDED OPENMP SETTINGS:
// solver = 1 : OMP_NUM_THREADS = 1 & MKL_NUM_THREADS = # of cores , we only utilize threading through LAPACK & BLAS calls
// solver = 2 : OMP_NUM_THREADS = MKL_NUM_THREADS = 1 , MPI-based parallelism only without any threading
// otherwise : OMP_NUM_THREADS = # of cores & MKL_NUM_THREADS = 1 , threading in code & BLAS calls only for small matrix blocks
// SOFTWARE ORGANIZATION:
// 1. Fermi-Dirac approximation - fit polynomials & rational functions
// 2. NRL tight-binding model - matrix elements & their derivatives
// 3. Atomic partitioning - sets up neighbors lists for atoms
// 4. Block vector & matrix operations - native linear algebra operations in this software
// 5. Matrix construction & conversion - application-specific construction & conversion to other formats
// 6. Pseudo-random number generation - a standard PRNG generator that is better than C rand()
// 7. Iterative solvers - application-specific implementations of CG & MINRES & Chebyshev recursion
// 8. Solver mains - a main specific to each solver
// 9. Main - global control flow
// EXTERNAL LIBRARIES:
// - MKL (for BLAS, LAPACK, & FFTW3)
// - PEXSI 1.0
// - symPACK post-1.1 [development version that is adapted for PEXSI compatibility]
// - PT-Scotch 6.0.0
// - SuperLU_DIST 5.2.1
// - parMETIS 4.0.3
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <complex.h>
#include <omp.h>
#include <sys/time.h>
#include <sys/resource.h>
#include "mkl.h"
#include "fftw3.h"
#include "c_pexsi_interface.h"
#define A0 0.52917721067 // Bohr radius in Angstroms
#define E0 13.60569253 // Rydberg energy in eV
#define P0 14710.5071 // Ry/Bohr^3 in GPa
#define MIN(x,y) (((x) < (y)) ? (x) : (y))
#define MAX(x,y) (((x) > (y)) ? (x) : (y))
#ifndef M_PI
#define M_PI 3.14159265358979323846264338327950288
#endif
// MKL_INT is used as a matrix/vector index for compatibility with both 32-bit & 64-bit versions of MKL
// If MKL is not being used, define MKL_INT locally as the integer type used by BLAS & LAPACK (usually 'int')
//#define MKL_INT int
// Ditto for MKL_Complex16, but changes must also be made to infinite_reciprocal_solver if this is redefined
//#define MKL_Complex16 double complex
// Convenient hard-coded path (relative or absolute) to the rational approximation table
#define RATIONAL_TABLE_PATH "../src/table.txt" // relative path used for all benchmark calculations
// All dense matrices & matrix blocks are stored in Fortran-style column-major order
// All vectors have block structure and are stored as a sequence of memory-contiguous dense blocks (in single-index arrays)
// The block sizes are set by the natural block size for atomic partitioning in our tight-binding model (9),
// which is not optimal for performance. We choose simplicity over performance here.
#define NBLOCK_MAX 9 // hard-coded maximum block size
// Compressed-column sparsity pattern
struct pattern
{
int ncol; // number of columns
int nrow; // number of rows
int *col; // index of the first element of each column & col[ncol] is the number of nonzero elements [ncol+1]
int *row; // row of each nonzero matrix element [col[ncol]]
};
// Wrap up memory deallocation for pattern structure
void free_pattern(struct pattern* mat) // sparsity pattern to be set free [1]
{
free(mat->col);
free(mat->row);
}
//==============================//
// 1. FERMI-DIRAC APPROXIMATION //
//==============================//
// RETURN: value of the Fermi-Dirac distribution at x
double fermi(double x) // argument of the function
{ return 1.0/(1.0 + exp(x)); }
// RETURN: derivative of the Fermi-Dirac distribution at x
double dfermi_dx(double x) // argument of the function
{ return -0.5/(1.0 + cosh(x)); }
// RETURN: value of the Chebyshev polynomial expansion
double chebyshev(double x, // evaluation point
int n, // number of Chebyshev polynomials
double *coeff) // coefficients of the Chebyshev polynomials [n]
{
double T_old = 1.0, T = x, ans = 0.0;
if(n > 0) { ans += T_old*coeff[0]; }
if(n > 1) { ans += T*coeff[1]; }
for(int i=2 ; i<n ; i++)
{
double T_new = 2.0*x*T - T_old;
ans += T_new*coeff[i];
T_old = T;
T = T_new;
}
return ans;
}
// Chebyshev polynomial approximation of the Fermi-Dirac function
#define CHEBYSHEV_DX 0.1 // grid spacing needed for accurate integrals of the Fermi-Dirac function
#define GOLDEN_RATIO 1.61803398875
#define EXTREMUM_TOLERANCE 1e-12
// RETURN: maximum approximation error
double polynomial_approximation(int n, // number of Chebyshev polynomials
double min_energy, // minimum orbital energy of the system
double max_energy, // maximum orbital energy of the system
double potential, // chemical potential of the system
double temperature, // temperature of the system
double *coeff) // coefficients for Chebyshev polynomials [n]
{
// set shifted & scaled domain
double xmin = (min_energy - potential)/temperature;
double xmax = (max_energy - potential)/temperature;
// set quadrature & integrand values
int npt = MAX((int)ceil((xmax-xmin)/CHEBYSHEV_DX),2*n);
double *pt = (double*)malloc(sizeof(double)*npt);
double *val = (double*)malloc(sizeof(double)*npt);
for(int i=0 ; i<npt ; i++)
{
pt[i] = 0.5*(xmin+xmax) + 0.5*(xmin-xmax)*cos(M_PI*((double)i+0.5)/(double)npt);
val[npt-i-1] = fermi(pt[i])/(double)(2.0*npt); // reversed order & rescaling for FFTW input
}
// transform & truncate Chebyshev expansion
double *coeff_big = (double*)malloc(sizeof(double)*npt);
fftw_plan p;
p = fftw_plan_r2r_1d(npt,val,coeff_big,FFTW_REDFT10,FFTW_ESTIMATE);
fftw_execute(p);
fftw_destroy_plan(p);
for(int i=0 ; i<n ; i++) { coeff[i] = 2.0*coeff_big[i]; }
for(int i=n ; i<npt ; i++) { coeff_big[i] = 0.0; }
coeff[0] *= 0.5;
// inverse transform to generate residual grid
p = fftw_plan_r2r_1d(npt,coeff_big,val,FFTW_REDFT01,FFTW_ESTIMATE);
fftw_execute(p);
fftw_destroy_plan(p);
// find grid point with largest residual error
int ierror = -1;
double error = 0.0;
for(int i=0 ; i<npt ; i++)
{
if(fabs(val[npt-i-1] - fermi(pt[i])) > error)
{
error = fabs(val[npt-i-1] - fermi(pt[i]));
ierror = i;
}
}
// refine global residual maximum with Golden section search
double xmin0 = -cos(M_PI*((double)MAX(0,ierror-1)+0.5)/(double)npt);
double xmax0 = -cos(M_PI*((double)MIN(npt-1,ierror+1)+0.5)/(double)npt);
double xmin0_new = xmin0 + (xmax0 - xmin0)/GOLDEN_RATIO;
double xmax0_new = xmax0 - (xmax0 - xmin0)/GOLDEN_RATIO;
while(fabs(xmax0_new - xmin0_new) > EXTREMUM_TOLERANCE)
{
if( fabs(fermi(0.5*(xmin+xmax) - 0.5*(xmin-xmax)*xmin0_new) - chebyshev(xmin0_new,n,coeff)) <
fabs(fermi(0.5*(xmin+xmax) - 0.5*(xmin-xmax)*xmax0_new) - chebyshev(xmax0_new,n,coeff)) )
{ xmax0 = xmin0_new; }
else
{ xmin0 = xmax0_new; }
xmin0_new = xmin0 + (xmax0 - xmin0)/GOLDEN_RATIO;
xmax0_new = xmax0 - (xmax0 - xmin0)/GOLDEN_RATIO;
}
error = fabs(fermi(0.5*(xmin+xmax) - 0.5*(xmin-xmax)*0.5*(xmin0+xmax0)) - chebyshev(0.5*(xmin0+xmax0),n,coeff));
free(coeff_big);
free(val);
free(pt);
return error;
}
// Find an appropriate rational approximation in the table file
// RETURN: maximum approximation error
double rational_approximation(int n, // number of pole pairs
double min_energy, // minimum orbital energy of the system
double potential, // chemical potential of the system
double temperature, // temperature of the system
double complex *w, // approximation residues [n]
double complex *z) // poles (ordered by decreasing magnitude of imaginary part) [n]
{
// open the table of rational approximations
// NOTE: this version of the table has no header & is ordered by increasing # of poles & increasing error
FILE *quadrature_table = fopen(RATIONAL_TABLE_PATH,"r");
if(quadrature_table == NULL)
{ printf("ERROR: rational approximation table not found at %s\n",RATIONAL_TABLE_PATH); MPI_Abort(MPI_COMM_WORLD,0); }
int num_pole;
double approximation_error, y, real_part, imag_part;
double y_target = (potential - min_energy)/temperature;
double complex *w0 = (double complex*)malloc(sizeof(double complex)*2*n);
double complex *z0 = (double complex*)malloc(sizeof(double complex)*2*n);
// loop over entries of the table
do
{
// read the next entry of the input table
fscanf(quadrature_table,"%d %lf %lf",&num_pole,&approximation_error,&y);
if(feof(quadrature_table))
{ printf("ERROR: suitable rational approximation was not found in table\n"); MPI_Abort(MPI_COMM_WORLD,0); }
for(int i=0 ; i<num_pole ; i++)
{
fscanf(quadrature_table,"%lf %lf",&real_part,&imag_part);
w0[i] = real_part + I*imag_part;
fscanf(quadrature_table,"%lf %lf",&real_part,&imag_part);
z0[i] = real_part + I*imag_part;
}
}while(num_pole != 2*n || y < y_target);
fclose(quadrature_table);
// order by magnitude (inefficient bubble sort)
for(int i=0 ; i<2*n ; i++)
{
for(int j=i+1 ; j<2*n ; j++)
{
if(cabs(z0[j]) > cabs(z0[i]))
{
double complex c;
c = w0[i]; w0[i] = w0[j]; w0[j] = c;
c = z0[i]; z0[i] = z0[j]; z0[j] = c;
}
}
}
// shift & scale the rational approximation
for(int i=0 ; i<2*n ; i++)
{
w0[i] *= temperature;
z0[i] *= temperature;
z0[i] += potential;
}
// group poles together into conjugate pairs
for(int i=0 ; i<2*n ; i+=2)
{
for(int j=i+2 ; j<2*n ; j++)
{
if(cabs(z0[i]-conj(z0[j])) < cabs(z0[i]-conj(z0[i+1])))
{
double complex c;
c = w0[i+1]; w0[i+1] = w0[j]; w0[j] = c;
c = z0[i+1]; z0[i+1] = z0[j]; z0[j] = c;
}
// order positive imaginary part first
if(cimag(z0[i]) < cimag(z0[i+1]))
{
double complex c;
c = w0[i+1]; w0[i+1] = w0[i]; w0[i] = c;
c = z0[i+1]; z0[i+1] = z0[i]; z0[i] = c;
}
}
}
// save only one residue & pole from each pair
for(int i=0 ; i<n ; i++)
{
w[i] = w0[2*i];
z[i] = z0[2*i];
}
free(z0);
free(w0);
return approximation_error;
}
//============================//
// 2. NRL TIGHT-BINDING MODEL //
//============================//
// NRL tight-binding model parameters
struct nrl_tb
{
double Rcut, R0, Rs, lambda; // numerical cutoff radius, screening radius, screening length, & environment decay
double hs[4], hp[4], hd[4]; // onsite parameters
double hsss[4], hsps[4], hpps[4], hppp[4], hsds[4], hpds[4], hpdp[4], hdds[4], hddp[4], hddd[4]; // hopping parameters
double osss[4], osps[4], opps[4], oppp[4], osds[4], opds[4], opdp[4], odds[4], oddp[4], oddd[4]; // overlap parameters
};
// hard-coded parameters for copper
// RETURN: structure filled with copper parameters
struct nrl_tb define_copper()
{
// C99 syntax for "designated initializers"
struct nrl_tb cu = {
.Rcut = 12.5,
.R0 = 11.25, // RCUT - 5*SCREENL, for some reason not the bare parameter
.Rs = 0.25,
.lambda = .145617816949E+01,
// a, b, c, d
.hs = { .291179442078E-01, .608612040825E+02, -.580815805783E+04, .225817615341E+06 },
.hp = { .344716987246E+00, .888191059298E+02, -.627796769797E+04, .175924743450E+06 },
.hd = { -.290980998425E-02, -.280134504507E+01, .439691173572E+03, -.133435774471E+05 },
// e, f, fbar, g
.hsss = { -.597191735504E+01, .157276992857E+01, -.447299469804E+00, .968392496859E+00 },
.hsps = { .142228825776E+01, .111328503057E+00, .209048736613E-01, .816193556611E+00 },
.hpps = { -.699924962951E+00, .685983943326E+00, -.283976143863E-01, .766161691504E+00 },
.hppp = { -.194951465694E-01, -.157553504153E+01, .301142535846E+00, .943349455985E+00 },
.hsds = { -.487019125256E+00, -.122729421901E+00, -.282606250674E-01, .925507793241E+00 },
.hpds = { -.290425374224E+00, -.715797951782E-01, .137648233927E-02, .743208041114E+00 },
.hpdp = { -.186619297102E+01, .827909641955E+00, .129381300114E+00, .105155367074E+01 },
.hdds = { -.264216452809E+01, .612527278745E+00, -.411141233432E-01, .811325004989E+00 },
.hddp = { .697425666621E+01, -.173638099984E+01, .168047875555E+00, .101445807107E+01 },
.hddd = { -.122136143098E+00, -.106786813791E+00, -.573634877781E-01, .114358651642E+01 },
.osss = { -.187763110058E+01, .999745133711E+00, .294871103015E+00, .963163153997E+00 },
.osps = { .349830122695E+02, -.130114254052E+02, .607050297159E+00, .986803443924E+00 },
.opps = { .469831980051E+02, -.150210237460E+02, .423592218489E+00, .103136127318E+01 },
.oppp = { -.452858187471E+02, .212940485258E+02, -.222119065584E+01, .973686678526E+00 },
.osds = { .185975554048E+01, -.101721693929E+01, .361939123784E-01, .113738864025E+01 },
.opds = { .151404237752E+01, -.648815291269E+00, -.301781892056E+00, .107714476838E+01 },
.opdp = { -.824947586413E+01, .737040055222E+00, .202806401480E-01, .102268934886E+01 },
.odds = { .552906497058E+01, .859731091202E-01, -.303881382425E+00, .101972266315E+01 },
.oddp = { -.856025085531E+01, .413682082679E+00, .561269698491E+00, .119817640580E+01 },
.oddd = { .836929253859E-01, -.307737391082E+00, .754080691966E-01, .983776299155E+00 } };
return cu;
}
// screening function, F_c(R)
// RETURN: function value
double screen(double R, // distance between a pair of atoms
struct nrl_tb *param) // tight-binding parameters [1]
{
if(R > param->Rcut) { return 0.0; }
else { return 1.0/(1.0 + exp((R-param->R0)/param->Rs)); }
}
// RETURN: function derivative
double dscreen_dR(double R, // distance between a pair of atoms
struct nrl_tb *param) // tight-binding parameters [1]
{
if(R > param->Rcut) { return 0.0; }
else { return -0.5/((1.0 + cosh((R-param->R0)/param->Rs))*param->Rs); }
}
// local environment parameter for on-site Hamiltonian, which must be summed over neighbors
// RETURN: function value
double rho(double R, // distance between a pair of atoms
struct nrl_tb *param) // tight-binding parameters [1]
{
return exp(-pow(param->lambda,2)*R)*screen(R,param);
}
// RETURN: function derivative
double drho_dR(double R, // distance between a pair of atoms
struct nrl_tb *param) // tight-binding parameters [1]
{
return exp(-pow(param->lambda,2)*R)*(dscreen_dR(R,param) - pow(param->lambda,2)*screen(R,param));
}
// on-site tight-binding matrix element
#define RHO0 1e-16 // regularization factor
// RETURN: function value
double onsite(double rho, // total rho value summed over neighbors
double *abcd) // a, b, c, d from the NRL parameters [4]
{
return abcd[0] + abcd[1]*pow(RHO0+rho,2.0/3.0) + abcd[2]*pow(RHO0+rho,4.0/3.0) + abcd[3]*pow(RHO0+rho,2);
}
// RETURN: derivative value
double donsite_drho(double rho, // total rho value summed over neighbors
double *abcd) // a, b, c, d from the NRL parameters [4]
{
return (2.0/3.0)*abcd[1]*pow(RHO0+rho,-1.0/3.0) + (4.0/3.0)*abcd[2]*pow(RHO0+rho,1.0/3.0) + 2.0*abcd[3]*(RHO0+rho);
}
// bonding functions used to define hopping matrix elements
// RETURN: function value
double bond(double R, // distance between a pair of atoms
double *effg, // e, f, fbar, g from the NRL parameters [4]
struct nrl_tb *param) // tight-binding parameters [1]
{
return (effg[0] + effg[1]*R + effg[2]*R*R)*exp(-effg[3]*effg[3]*R)*screen(R,param);
}
// RETURN: derivative value
double dbond_dR(double R, // distance between a pair of atoms
double *effg, // e, f, fbar, g from the NRL parameters [4]
struct nrl_tb *param) // tight-binding parameters [1]
{
return (effg[1] + 2.0*effg[2]*R)*exp(-effg[3]*effg[3]*R)*screen(R,param)
- effg[3]*effg[3]*(effg[0] + effg[1]*R + effg[2]*R*R)*exp(-effg[3]*effg[3]*R)*screen(R,param)
+ (effg[0] + effg[1]*R + effg[2]*R*R)*exp(-effg[3]*effg[3]*R)*dscreen_dR(R,param);
}
// symmetrically fill in a matrix block of an s/p/d Slater-Koster tight-binding model
// NOTE: using notation consistent with [Phys. Rev. 94, 1498 (1954)]
// & orbitals ordered as: s, p_x, p_y, p_z, d_xy, d_yz, d_zx, d_{x^2-y^2}, d_{3z^2-r^2}
void fill_mat(double l, // x directional cosine
double m, // y directional cosine
double n, // z directional cosine
double sss, // s s sigma term
double sps, // s p sigma term
double pps, // p p sigma term
double ppp, // p p pi term
double sds, // s d sigma term
double pds, // p d sigma term
double pdp, // p d pi term
double dds, // d d sigma term
double ddp, // d d pi term
double ddd, // d d delta term
double *mat) // 9-by-9 matrix block [81]
{
// ss terms
mat[0+0*9] = sss;
// sp terms
mat[1+0*9] = -(mat[0+1*9] = l*sps);
mat[2+0*9] = -(mat[0+2*9] = m*sps);
mat[3+0*9] = -(mat[0+3*9] = n*sps);
// pp terms
mat[1+1*9] = l*l*pps + (1.0 - l*l)*ppp;
mat[2+2*9] = m*m*pps + (1.0 - m*m)*ppp;
mat[3+3*9] = n*n*pps + (1.0 - n*n)*ppp;
mat[2+1*9] = mat[1+2*9] = l*m*pps - l*m*ppp;
mat[3+1*9] = mat[1+3*9] = l*n*pps - l*n*ppp;
mat[3+2*9] = mat[2+3*9] = m*n*pps - m*n*ppp;
// sd terms
mat[4+0*9] = mat[0+4*9] = sqrt(3.0)*l*m*sds;
mat[5+0*9] = mat[0+5*9] = sqrt(3.0)*m*n*sds;
mat[6+0*9] = mat[0+6*9] = sqrt(3.0)*n*l*sds;
mat[7+0*9] = mat[0+7*9] = 0.5*sqrt(3.0)*(l*l - m*m)*sds;
mat[8+0*9] = mat[0+8*9] = (n*n - 0.5*(l*l + m*m))*sds;
// pd terms
mat[4+1*9] = -(mat[1+4*9] = sqrt(3.0)*l*l*m*pds + m*(1.0 - 2.0*l*l)*pdp);
mat[5+2*9] = -(mat[2+5*9] = sqrt(3.0)*m*m*n*pds + n*(1.0 - 2.0*m*m)*pdp);
mat[6+3*9] = -(mat[3+6*9] = sqrt(3.0)*n*n*l*pds + l*(1.0 - 2.0*n*n)*pdp);
mat[4+3*9] = mat[6+2*9] = mat[5+1*9] = -(mat[1+5*9] = mat[2+6*9] = mat[3+4*9] = sqrt(3.0)*l*m*n*pds - 2.0*l*m*n*pdp);
mat[6+1*9] = -(mat[1+6*9] = sqrt(3.0)*l*l*n*pds + n*(1.0 - 2.0*l*l)*pdp);
mat[4+2*9] = -(mat[2+4*9] = sqrt(3.0)*m*m*l*pds + l*(1.0 - 2.0*m*m)*pdp);
mat[5+3*9] = -(mat[3+5*9] = sqrt(3.0)*n*n*m*pds + m*(1.0 - 2.0*n*n)*pdp);
mat[7+1*9] = -(mat[1+7*9] = 0.5*sqrt(3.0)*l*(l*l - m*m)*pds + l*(1.0 - l*l + m*m)*pdp);
mat[7+2*9] = -(mat[2+7*9] = 0.5*sqrt(3.0)*m*(l*l - m*m)*pds - m*(1.0 + l*l - m*m)*pdp);
mat[7+3*9] = -(mat[3+7*9] = 0.5*sqrt(3.0)*n*(l*l - m*m)*pds - n*(l*l - m*m)*pdp);
mat[8+1*9] = -(mat[1+8*9] = l*(n*n - 0.5*(l*l + m*m))*pds - sqrt(3.0)*l*n*n*pdp);
mat[8+2*9] = -(mat[2+8*9] = m*(n*n - 0.5*(l*l + m*m))*pds - sqrt(3.0)*m*n*n*pdp);
mat[8+3*9] = -(mat[3+8*9] = n*(n*n - 0.5*(l*l + m*m))*pds + sqrt(3.0)*n*(l*l + m*m)*pdp);
// dd terms
mat[4+4*9] = 3.0*l*l*m*m*dds + (l*l + m*m - 4.0*l*l*m*m)*ddp + (n*n + l*l*m*m)*ddd;
mat[5+5*9] = 3.0*m*m*n*n*dds + (m*m + n*n - 4.0*m*m*n*n)*ddp + (l*l + m*m*n*n)*ddd;
mat[6+6*9] = 3.0*n*n*l*l*dds + (n*n + l*l - 4.0*n*n*l*l)*ddp + (m*m + n*n*l*l)*ddd;
mat[5+4*9] = mat[4+5*9] = 3.0*l*m*m*n*dds + l*n*(1.0 - 4.0*m*m)*ddp + l*n*(m*m - 1.0)*ddd;
mat[6+5*9] = mat[5+6*9] = 3.0*m*n*n*l*dds + m*l*(1.0 - 4.0*n*n)*ddp + m*l*(n*n - 1.0)*ddd;
mat[6+4*9] = mat[4+6*9] = 3.0*n*l*l*m*dds + n*m*(1.0 - 4.0*l*l)*ddp + n*m*(l*l - 1.0)*ddd;
mat[7+4*9] = mat[4+7*9] = 1.5*l*m*(l*l - m*m)*dds + 2.0*l*m*(m*m - l*l)*ddp + 0.5*l*m*(l*l - m*m)*ddd;
mat[7+5*9] = mat[5+7*9] = 1.5*m*n*(l*l - m*m)*dds - m*n*(1.0 + 2.0*(l*l - m*m))*ddp + m*n*(1.0 + 0.5*(l*l - m*m))*ddd;
mat[7+6*9] = mat[6+7*9] = 1.5*n*l*(l*l - m*m)*dds + n*l*(1.0 - 2.0*(l*l - m*m))*ddp - n*l*(1.0 - 0.5*(l*l - m*m))*ddd;
mat[8+4*9] = mat[4+8*9] = sqrt(3.0)*(l*m*(n*n - 0.5*(l*l + m*m))*dds - 2.0*l*m*n*n*ddp + 0.5*l*m*(1.0 + n*n)*ddd);
mat[8+5*9] = mat[5+8*9] = sqrt(3.0)*(m*n*(n*n - 0.5*(l*l + m*m))*dds + m*n*(l*l + m*m - n*n)*ddp - 0.5*m*n*(l*l + m*m)*ddd);
mat[8+6*9] = mat[6+8*9] = sqrt(3.0)*(n*l*(n*n - 0.5*(l*l + m*m))*dds + n*l*(l*l + m*m - n*n)*ddp - 0.5*n*l*(l*l + m*m)*ddd);
mat[7+7*9] = 0.75*pow(l*l - m*m,2)*dds + (l*l + m*m - pow(l*l - m*m,2))*ddp + (n*n + 0.25*pow(l*l - m*m,2))*ddd;
mat[8+7*9] = mat[7+8*9] = sqrt(3.0)*(0.5*(l*l - m*m)*(n*n - 0.5*(l*l + m*m))*dds + n*n*(m*m - l*l)*ddp
+ 0.25*(1.0 + n*n)*(l*l - m*m)*ddd);
mat[8+8*9] = pow(n*n - 0.5*(l*l + m*m),2)*dds + 3.0*n*n*(l*l + m*m)*ddp + 0.75*pow(l*l + m*m,2)*ddd;
}
// derivative of the Slater-Koster matrices w.r.t. l/m/n
void fill_dmat(double l, // x directional cosine
double m, // y directional cosine
double n, // z directional cosine
double sss, // s s sigma term
double sps, // s p sigma term
double pps, // p p sigma term
double ppp, // p p pi term
double sds, // s d sigma term
double pds, // p d sigma term
double pdp, // p d pi term
double dds, // d d sigma term
double ddp, // d d pi term
double ddd, // d d delta term
double *dmat) // array of 3 9-by-9 matrix blocks [243]
{
// ss terms
dmat[0+0*9+0*81] = 0.0;
dmat[0+0*9+1*81] = 0.0;
dmat[0+0*9+2*81] = 0.0;
// sp terms
dmat[1+0*9+0*81] = -(dmat[0+1*9+0*81] = sps);
dmat[1+0*9+1*81] = -(dmat[0+1*9+1*81] = 0.0);
dmat[1+0*9+2*81] = -(dmat[0+1*9+2*81] = 0.0);
dmat[2+0*9+0*81] = -(dmat[0+2*9+0*81] = 0.0);
dmat[2+0*9+1*81] = -(dmat[0+2*9+1*81] = sps);
dmat[2+0*9+2*81] = -(dmat[0+2*9+2*81] = 0.0);
dmat[3+0*9+0*81] = -(dmat[0+3*9+0*81] = 0.0);
dmat[3+0*9+1*81] = -(dmat[0+3*9+1*81] = 0.0);
dmat[3+0*9+2*81] = -(dmat[0+3*9+2*81] = sps);
// pp terms
dmat[1+1*9+0*81] = 2.0*l*pps - 2.0*l*ppp;
dmat[1+1*9+1*81] = 0.0;
dmat[1+1*9+2*81] = 0.0;
dmat[2+2*9+0*81] = 0.0;
dmat[2+2*9+1*81] = 2.0*m*pps - 2.0*m*ppp;
dmat[2+2*9+2*81] = 0.0;
dmat[3+3*9+0*81] = 0.0;
dmat[3+3*9+1*81] = 0.0;
dmat[3+3*9+2*81] = 2.0*n*pps - 2.0*n*ppp;
dmat[2+1*9+0*81] = dmat[1+2*9+0*81] = m*pps - m*ppp;
dmat[2+1*9+1*81] = dmat[1+2*9+1*81] = l*pps - l*ppp;
dmat[2+1*9+2*81] = dmat[1+2*9+2*81] = 0.0;
dmat[3+1*9+0*81] = dmat[1+3*9+0*81] = n*pps - n*ppp;
dmat[3+1*9+1*81] = dmat[1+3*9+1*81] = 0.0;
dmat[3+1*9+2*81] = dmat[1+3*9+2*81] = l*pps - l*ppp;
dmat[3+2*9+0*81] = dmat[2+3*9+0*81] = 0.0;
dmat[3+2*9+1*81] = dmat[2+3*9+1*81] = n*pps - n*ppp;
dmat[3+2*9+2*81] = dmat[2+3*9+2*81] = m*pps - m*ppp;
// sd terms
dmat[4+0*9+0*81] = dmat[0+4*9+0*81] = sqrt(3.0)*m*sds;
dmat[4+0*9+1*81] = dmat[0+4*9+1*81] = sqrt(3.0)*l*sds;
dmat[4+0*9+2*81] = dmat[0+4*9+2*81] = 0.0;
dmat[5+0*9+0*81] = dmat[0+5*9+0*81] = 0.0;
dmat[5+0*9+1*81] = dmat[0+5*9+1*81] = sqrt(3.0)*n*sds;
dmat[5+0*9+2*81] = dmat[0+5*9+2*81] = sqrt(3.0)*m*sds;
dmat[6+0*9+0*81] = dmat[0+6*9+0*81] = sqrt(3.0)*n*sds;
dmat[6+0*9+1*81] = dmat[0+6*9+1*81] = 0.0;
dmat[6+0*9+2*81] = dmat[0+6*9+2*81] = sqrt(3.0)*l*sds;
dmat[7+0*9+0*81] = dmat[0+7*9+0*81] = sqrt(3.0)*l*sds;
dmat[7+0*9+1*81] = dmat[0+7*9+1*81] = -sqrt(3.0)*m*sds;
dmat[7+0*9+2*81] = dmat[0+7*9+2*81] = 0.0;
dmat[8+0*9+0*81] = dmat[0+8*9+0*81] = -l*sds;
dmat[8+0*9+1*81] = dmat[0+8*9+1*81] = -m*sds;
dmat[8+0*9+2*81] = dmat[0+8*9+2*81] = 2.0*n*sds;
// pd terms
dmat[4+1*9+0*81] = -(dmat[1+4*9+0*81] = 2.0*sqrt(3.0)*l*m*pds - 4.0*m*l*pdp);
dmat[4+1*9+1*81] = -(dmat[1+4*9+1*81] = sqrt(3.0)*l*l*pds + (1.0 - 2.0*l*l)*pdp);
dmat[4+1*9+2*81] = -(dmat[1+4*9+2*81] = 0.0);
dmat[5+2*9+0*81] = -(dmat[2+5*9+0*81] = 0.0);
dmat[5+2*9+1*81] = -(dmat[2+5*9+1*81] = 2.0*sqrt(3.0)*m*n*pds - 4.0*n*m*pdp);
dmat[5+2*9+2*81] = -(dmat[2+5*9+2*81] = sqrt(3.0)*m*m*pds + (1.0 - 2.0*m*m)*pdp);
dmat[6+3*9+0*81] = -(dmat[3+6*9+0*81] = sqrt(3.0)*n*n*pds + (1.0 - 2.0*n*n)*pdp);
dmat[6+3*9+1*81] = -(dmat[3+6*9+1*81] = 0.0);
dmat[6+3*9+2*81] = -(dmat[3+6*9+2*81] = 2.0*sqrt(3.0)*n*l*pds - 4.0*l*n*pdp);
dmat[4+3*9+0*81] = dmat[6+2*9+0*81] = dmat[5+1*9+0*81]
= -(dmat[1+5*9+0*81] = dmat[2+6*9+0*81] = dmat[3+4*9+0*81] = sqrt(3.0)*m*n*pds - 2.0*m*n*pdp);
dmat[4+3*9+1*81] = dmat[6+2*9+1*81] = dmat[5+1*9+1*81]
= -(dmat[1+5*9+1*81] = dmat[2+6*9+1*81] = dmat[3+4*9+1*81] = sqrt(3.0)*l*n*pds - 2.0*l*n*pdp);
dmat[4+3*9+2*81] = dmat[6+2*9+2*81] = dmat[5+1*9+2*81]
= -(dmat[1+5*9+2*81] = dmat[2+6*9+2*81] = dmat[3+4*9+2*81] = sqrt(3.0)*l*m*pds - 2.0*l*m*pdp);
dmat[6+1*9+0*81] = -(dmat[1+6*9+0*81] = 2.0*sqrt(3.0)*l*n*pds - 4.0*n*l*pdp);
dmat[6+1*9+1*81] = -(dmat[1+6*9+1*81] = 0.0);
dmat[6+1*9+2*81] = -(dmat[1+6*9+2*81] = sqrt(3.0)*l*l*pds + (1.0 - 2.0*l*l)*pdp);
dmat[4+2*9+0*81] = -(dmat[2+4*9+0*81] = sqrt(3.0)*m*m*pds + (1.0 - 2.0*m*m)*pdp);
dmat[4+2*9+1*81] = -(dmat[2+4*9+1*81] = 2.0*sqrt(3.0)*m*l*pds - 4.0*l*m*pdp);
dmat[4+2*9+2*81] = -(dmat[2+4*9+2*81] = 0.0);
dmat[5+3*9+0*81] = -(dmat[3+5*9+0*81] = 0.0);
dmat[5+3*9+1*81] = -(dmat[3+5*9+1*81] = sqrt(3.0)*n*n*pds + (1.0 - 2.0*n*n)*pdp);
dmat[5+3*9+2*81] = -(dmat[3+5*9+2*81] = 2.0*sqrt(3.0)*n*m*pds - 4.0*m*n*pdp);
dmat[7+1*9+0*81] = -(dmat[1+7*9+0*81] = 0.5*sqrt(3.0)*(3.0*l*l - m*m)*pds + (1.0 - 3.0*l*l + m*m)*pdp);
dmat[7+1*9+1*81] = -(dmat[1+7*9+1*81] = -sqrt(3.0)*l*m*pds + 2.0*l*m*pdp);
dmat[7+1*9+2*81] = -(dmat[1+7*9+2*81] = 0.0);
dmat[7+2*9+0*81] = -(dmat[2+7*9+0*81] = sqrt(3.0)*m*l*pds - 2.0*m*l*pdp);
dmat[7+2*9+1*81] = -(dmat[2+7*9+1*81] = 0.5*sqrt(3.0)*(l*l - 3.0*m*m)*pds - (1.0 + l*l - 3.0*m*m)*pdp);
dmat[7+2*9+2*81] = -(dmat[2+7*9+2*81] = 0.0);
dmat[7+3*9+0*81] = -(dmat[3+7*9+0*81] = sqrt(3.0)*n*l*pds - 2.0*n*l*pdp);
dmat[7+3*9+1*81] = -(dmat[3+7*9+1*81] = -sqrt(3.0)*n*m*pds + 2.0*n*m*pdp);
dmat[7+3*9+2*81] = -(dmat[3+7*9+2*81] = 0.5*sqrt(3.0)*(l*l - m*m)*pds - (l*l - m*m)*pdp);
dmat[8+1*9+0*81] = -(dmat[1+8*9+0*81] = (n*n - 0.5*(3.0*l*l + m*m))*pds - sqrt(3.0)*n*n*pdp);
dmat[8+1*9+1*81] = -(dmat[1+8*9+1*81] = -l*m*pds);
dmat[8+1*9+2*81] = -(dmat[1+8*9+2*81] = 2.0*l*n*pds - 2.0*sqrt(3.0)*l*n*pdp);
dmat[8+2*9+0*81] = -(dmat[2+8*9+0*81] = -m*l*pds);
dmat[8+2*9+1*81] = -(dmat[2+8*9+1*81] = (n*n - 0.5*(l*l + 3.0*m*m))*pds - sqrt(3.0)*n*n*pdp);
dmat[8+2*9+2*81] = -(dmat[2+8*9+2*81] = 2.0*m*n*pds - 2.0*sqrt(3.0)*m*n*pdp);
dmat[8+3*9+0*81] = -(dmat[3+8*9+0*81] = -n*l*pds + 2.0*sqrt(3.0)*n*l*pdp);
dmat[8+3*9+1*81] = -(dmat[3+8*9+1*81] = -n*m*pds + 2.0*sqrt(3.0)*n*m*pdp);
dmat[8+3*9+2*81] = -(dmat[3+8*9+2*81] = (3.0*n*n - 0.5*(l*l + m*m))*pds + sqrt(3.0)*(l*l + m*m)*pdp);
// dd terms
dmat[4+4*9+0*81] = 6.0*l*m*m*dds + (2.0*l - 8.0*l*m*m)*ddp + 2.0*l*m*m*ddd;
dmat[4+4*9+1*81] = 6.0*l*l*m*dds + (2.0*m - 8.0*l*l*m)*ddp + 2.0*l*l*m*ddd;
dmat[4+4*9+2*81] = 2.0*n*ddd;
dmat[5+5*9+0*81] = 2.0*l*ddd;
dmat[5+5*9+1*81] = 6.0*m*n*n*dds + (2.0*m - 8.0*m*n*n)*ddp + 2.0*m*n*n*ddd;
dmat[5+5*9+2*81] = 6.0*m*m*n*dds + (2.0*n - 8.0*m*m*n)*ddp + 2.0*m*m*n*ddd;
dmat[6+6*9+0*81] = 6.0*n*n*l*dds + (2.0*l - 8.0*n*n*l)*ddp + 2.0*n*n*l*ddd;
dmat[6+6*9+1*81] = 2.0*m*ddd;
dmat[6+6*9+2*81] = 6.0*n*l*l*dds + (2.0*n - 8.0*n*l*l)*ddp + 2.0*n*l*l*ddd;
dmat[5+4*9+0*81] = dmat[4+5*9+0*81] = 3.0*m*m*n*dds + n*(1.0 - 4.0*m*m)*ddp + n*(m*m - 1.0)*ddd;
dmat[5+4*9+1*81] = dmat[4+5*9+1*81] = 6.0*l*m*n*dds - 8.0*l*n*m*ddp + 2.0*l*n*m*ddd;
dmat[5+4*9+2*81] = dmat[4+5*9+2*81] = 3.0*l*m*m*dds + l*(1.0 - 4.0*m*m)*ddp + l*(m*m - 1.0)*ddd;
dmat[6+5*9+0*81] = dmat[5+6*9+0*81] = 3.0*m*n*n*dds + m*(1.0 - 4.0*n*n)*ddp + m*(n*n - 1.0)*ddd;
dmat[6+5*9+1*81] = dmat[5+6*9+1*81] = 3.0*n*n*l*dds + l*(1.0 - 4.0*n*n)*ddp + l*(n*n - 1.0)*ddd;
dmat[6+5*9+2*81] = dmat[5+6*9+2*81] = 6.0*m*n*l*dds - 8.0*m*l*n*ddp + 2.0*m*l*n*ddd;
dmat[6+4*9+0*81] = dmat[4+6*9+0*81] = 6.0*n*l*m*dds - 8.0*n*m*l*ddp + 2.0*n*m*l*ddd;
dmat[6+4*9+1*81] = dmat[4+6*9+1*81] = 3.0*n*l*l*dds + n*(1.0 - 4.0*l*l)*ddp + n*(l*l - 1.0)*ddd;
dmat[6+4*9+2*81] = dmat[4+6*9+2*81] = 3.0*l*l*m*dds + m*(1.0 - 4.0*l*l)*ddp + m*(l*l - 1.0)*ddd;
dmat[7+4*9+0*81] = dmat[4+7*9+0*81] = 1.5*m*(3.0*l*l - m*m)*dds + 2.0*m*(m*m - 3.0*l*l)*ddp + 0.5*m*(3.0*l*l - m*m)*ddd;
dmat[7+4*9+1*81] = dmat[4+7*9+1*81] = 1.5*l*(l*l - 3.0*m*m)*dds + 2.0*l*(3.0*m*m - l*l)*ddp + 0.5*l*(l*l - 3.0*m*m)*ddd;
dmat[7+4*9+2*81] = dmat[4+7*9+2*81] = 0.0;
dmat[7+5*9+0*81] = dmat[5+7*9+0*81] = 3.0*m*n*l*dds - 4.0*m*n*l*ddp + m*n*l*ddd;
dmat[7+5*9+1*81] = dmat[5+7*9+1*81] = 1.5*n*(l*l - 3.0*m*m)*dds - n*(1.0 + 2.0*(l*l - 3.0*m*m))*ddp
+ n*(1.0 + 0.5*(l*l - 3.0*m*m))*ddd;
dmat[7+5*9+2*81] = dmat[5+7*9+2*81] = 1.5*m*(l*l - m*m)*dds - m*(1.0 + 2.0*(l*l - m*m))*ddp + m*(1.0 + 0.5*(l*l - m*m))*ddd;
dmat[7+6*9+0*81] = dmat[6+7*9+0*81] = 1.5*n*(3.0*l*l - m*m)*dds + n*(1.0 - 2.0*(3.0*l*l - m*m))*ddp
- n*(1.0 - 0.5*(3.0*l*l - m*m))*ddd;
dmat[7+6*9+1*81] = dmat[6+7*9+1*81] = -3.0*n*l*m*dds + 4.0*n*l*m*ddp - n*l*m*ddd;
dmat[7+6*9+2*81] = dmat[6+7*9+2*81] = 1.5*l*(l*l - m*m)*dds + l*(1.0 - 2.0*(l*l - m*m))*ddp - l*(1.0 - 0.5*(l*l - m*m))*ddd;
dmat[8+4*9+0*81] = dmat[4+8*9+0*81] = sqrt(3.0)*(m*(n*n - 0.5*(3.0*l*l + m*m))*dds - 2.0*m*n*n*ddp + 0.5*m*(1.0 + n*n)*ddd);
dmat[8+4*9+1*81] = dmat[4+8*9+1*81] = sqrt(3.0)*(l*(n*n - 0.5*(l*l + 3.0*m*m))*dds - 2.0*l*n*n*ddp + 0.5*l*(1.0 + n*n)*ddd);
dmat[8+4*9+2*81] = dmat[4+8*9+2*81] = sqrt(3.0)*(2.0*l*m*n*dds - 4.0*l*m*n*ddp + l*m*n*ddd);
dmat[8+5*9+0*81] = dmat[5+8*9+0*81] = sqrt(3.0)*(-m*n*l*dds + 2.0*m*n*l*ddp - m*n*l*ddd);
dmat[8+5*9+1*81] = dmat[5+8*9+1*81] = sqrt(3.0)*(n*(n*n - 0.5*(l*l + 3.0*m*m))*dds + n*(l*l + 3.0*m*m - n*n)*ddp
- 0.5*n*(l*l + 3.0*m*m)*ddd);
dmat[8+5*9+2*81] = dmat[5+8*9+2*81] = sqrt(3.0)*(m*(3.0*n*n - 0.5*(l*l + m*m))*dds + m*(l*l + m*m - 3.0*n*n)*ddp
- 0.5*m*(l*l + m*m)*ddd);
dmat[8+6*9+0*81] = dmat[6+8*9+0*81] = sqrt(3.0)*(n*(n*n - 0.5*(3.0*l*l + m*m))*dds + n*(3.0*l*l + m*m - n*n)*ddp
- 0.5*n*(3.0*l*l + m*m)*ddd);
dmat[8+6*9+1*81] = dmat[6+8*9+1*81] = sqrt(3.0)*(-n*l*m*dds + 2.0*n*l*m*ddp - n*l*m*ddd);
dmat[8+6*9+2*81] = dmat[6+8*9+2*81] = sqrt(3.0)*(l*(3.0*n*n - 0.5*(l*l + m*m))*dds + l*(l*l + m*m - 3.0*n*n)*ddp
- 0.5*l*(l*l + m*m)*ddd);
dmat[7+7*9+0*81] = 3.0*l*(l*l - m*m)*dds + (2.0*l - 4.0*l*(l*l - m*m))*ddp + l*(l*l - m*m)*ddd;
dmat[7+7*9+1*81] = -3.0*m*(l*l - m*m)*dds + (2.0*m + 4.0*m*(l*l - m*m))*ddp - m*(l*l - m*m)*ddd;
dmat[7+7*9+2*81] = 2.0*n*ddd;
dmat[8+7*9+0*81] = dmat[7+8*9+0*81] = sqrt(3.0)*(l*(n*n - 0.5*(l*l + m*m))*dds - 0.5*(l*l - m*m)*l*dds - 2.0*n*n*l*ddp
+ 0.5*(1.0 + n*n)*l*ddd);
dmat[8+7*9+1*81] = dmat[7+8*9+1*81] = sqrt(3.0)*(-m*(n*n - 0.5*(l*l + m*m))*dds - 0.5*(l*l - m*m)*m*dds + 2.0*n*n*m*ddp
- 0.5*(1.0 + n*n)*m*ddd);
dmat[8+7*9+2*81] = dmat[7+8*9+2*81] = sqrt(3.0)*((l*l - m*m)*n*dds + 2.0*n*(m*m - l*l)*ddp + 0.5*n*(l*l - m*m)*ddd);
dmat[8+8*9+0*81] = -2.0*l*(n*n - 0.5*(l*l + m*m))*dds + 6.0*n*n*l*ddp + 3.0*l*(l*l + m*m)*ddd;
dmat[8+8*9+1*81] = -2.0*m*(n*n - 0.5*(l*l + m*m))*dds + 6.0*n*n*m*ddp + 3.0*m*(l*l + m*m)*ddd;
dmat[8+8*9+2*81] = 4.0*n*(n*n - 0.5*(l*l + m*m))*dds + 6.0*n*(l*l + m*m)*ddp;
}
// distance between a pair of atoms
double distance(double *atom1, // coordinate of 1st atom [3]
double *atom2) // coordinate of 2nd atom [3]
{
double d2 = 0.0;
for(size_t i=0 ; i<3 ; i++) { d2 += pow(atom1[i] - atom2[i],2); }
return sqrt(d2);
}
// calculate a diagonal atomic matrix block of the tight-binding model
void tb_diagonal(int iatom, // atom index
int natom, // number of atoms
double *atom, // atomic coordinates [3*natom]
int nneighbor, // number of neighbors coupled to iatom
int *neighbor, // neighbor list of iatom [nneighbor]
struct nrl_tb *param, // tight-binding parameters [1]
double *hblock, // Hamiltonian matrix elements [81]
double *oblock) // overlap matrix elements [81]
{
// calculate rho for iatom
double rho0 = 0.0;
for(int i=0 ; i<nneighbor ; i++)
{
if(iatom != neighbor[i])
{ rho0 += rho(distance(&(atom[3*iatom]),&(atom[3*neighbor[i]])),param); }
}
// calculate the matrix elements
for(int i=0 ; i<81 ; i++) { hblock[i] = oblock[i] = 0.0; }
hblock[0+0*9] = onsite(rho0,param->hs);
hblock[1+1*9] = hblock[2+2*9] = hblock[3+3*9] = onsite(rho0,param->hp);
hblock[4+4*9] = hblock[5+5*9] = hblock[6+6*9] = hblock[7+7*9] = hblock[8+8*9] = onsite(rho0,param->hd);
for(int i=0 ; i<9 ; i++) { oblock[i+i*9] = 1.0; }
}
// calculate atomic response of a diagonal atomic matrix block of the tight-binding model
void tb_diagonal_force(int iatom, // atom index of matrix elements
int jatom, // atom index of perturbed atom
int natom, // number of atoms
double *atom, // atomic coordinates [3*natom]
int nneighbor, // number of neighbors coupled to iatom
int *neighbor, // neighbor list of iatom [nneighbor]
struct nrl_tb *param, // tight-binding parameters [1]
double *hblock_force) // array of 3 Hamiltonian matrix elements [243]
{
// calculate rho for iatom
double rho0 = 0.0, rho0_force[3] = { 0.0, 0.0, 0.0 };
for(int i=0 ; i<nneighbor ; i++)
{
if(iatom != neighbor[i])
{ rho0 += rho(distance(&(atom[3*iatom]),&(atom[3*neighbor[i]])),param); }
}
// when iatom == jatom, the entire sum over neighbors contributes
if(iatom == jatom)
{
for(int i=0 ; i<nneighbor ; i++)
{
if(iatom == neighbor[i]) { continue; }
double R = distance(&(atom[3*iatom]),&(atom[3*neighbor[i]]));
double drho_dR0 = drho_dR(R,param);
for(int j=0 ; j<3 ; j++)
{ rho0_force[j] += drho_dR0*(atom[j+iatom*3]-atom[j+neighbor[i]*3])/R; }
}
}
else // when iatom != jatom, only a single term in the rho sum is perturbed
{
double R = distance(&(atom[3*iatom]),&(atom[3*jatom]));
double drho_dR0 = drho_dR(R,param);
for(int j=0 ; j<3 ; j++)
{ rho0_force[j] += drho_dR0*(atom[j+jatom*3]-atom[j+iatom*3])/R; }
}
for(int i=0 ; i<243 ; i++) { hblock_force[i] = 0.0; }
for(int i=0 ; i<3 ; i++)
{
hblock_force[0+0*9+i*81] = -donsite_drho(rho0,param->hs)*rho0_force[i];
hblock_force[1+1*9+i*81] = hblock_force[2+2*9+i*81] = hblock_force[3+3*9+i*81]
= -donsite_drho(rho0,param->hp)*rho0_force[i];
hblock_force[4+4*9+i*81] = hblock_force[5+5*9+i*81] = hblock_force[6+6*9+i*81] = hblock_force[7+7*9+i*81]
= hblock_force[8+8*9+i*81] = -donsite_drho(rho0,param->hd)*rho0_force[i];
}
}
// calculate an offdiagonal atomic matrix block of the tight-binding model
void tb_offdiagonal(int iatom, // 1st atom index
int jatom, // 2nd atom index
int natom, // number of atoms
double *atom, // atomic coordinates [3*natom]
struct nrl_tb *param, // tight-binding parameters [1]
double *hblock, // Hamiltonian matrix elements [81]
double *oblock) // overlap matrix elements [81]
{
// calculate distance between atoms and directional cosines
double R = distance(&(atom[3*iatom]),&(atom[3*jatom]));
double l = (atom[0+iatom*3]-atom[0+jatom*3])/R;
double m = (atom[1+iatom*3]-atom[1+jatom*3])/R;
double n = (atom[2+iatom*3]-atom[2+jatom*3])/R;
fill_mat(l,m,n,bond(R,param->hsss,param),bond(R,param->hsps,param),bond(R,param->hpps,param),bond(R,param->hppp,param),
bond(R,param->hsds,param),bond(R,param->hpds,param),bond(R,param->hpdp,param),bond(R,param->hdds,param),
bond(R,param->hddp,param),bond(R,param->hddd,param),hblock);
fill_mat(l,m,n,bond(R,param->osss,param),bond(R,param->osps,param),bond(R,param->opps,param),bond(R,param->oppp,param),
bond(R,param->osds,param),bond(R,param->opds,param),bond(R,param->opdp,param),bond(R,param->odds,param),
bond(R,param->oddp,param),bond(R,param->oddd,param),oblock);
}
// calculate atomic response of an offdiagonal atomic matrix block of the tight-binding model
void tb_offdiagonal_force(int iatom, // 1st atom index & perturbed atom
int jatom, // 2nd atom index
int natom, // number of atoms
double *atom, // atomic coordinates [3*natom]
struct nrl_tb *param, // tight-binding parameters [1]
double *hblock_force, // array of 3 Hamiltonian matrix elements [243]
double *oblock_force) // array of 3 overlap matrix elements [243]
{
// calculate distance between atoms and directional cosines
double R = distance(&(atom[3*iatom]),&(atom[3*jatom]));
double l = (atom[0+iatom*3] - atom[0+jatom*3])/R;
double m = (atom[1+iatom*3] - atom[1+jatom*3])/R;
double n = (atom[2+iatom*3] - atom[2+jatom*3])/R;
// derivative of the bond functions
double dhblock_dR[81], doblock_dR[81];
fill_mat(l,m,n,dbond_dR(R,param->hsss,param),dbond_dR(R,param->hsps,param),dbond_dR(R,param->hpps,param),
dbond_dR(R,param->hppp,param),dbond_dR(R,param->hsds,param),dbond_dR(R,param->hpds,param),
dbond_dR(R,param->hpdp,param),dbond_dR(R,param->hdds,param),dbond_dR(R,param->hddp,param),
dbond_dR(R,param->hddd,param),dhblock_dR);
fill_mat(l,m,n,dbond_dR(R,param->osss,param),dbond_dR(R,param->osps,param),dbond_dR(R,param->opps,param),
dbond_dR(R,param->oppp,param),dbond_dR(R,param->osds,param),dbond_dR(R,param->opds,param),
dbond_dR(R,param->opdp,param),dbond_dR(R,param->odds,param),dbond_dR(R,param->oddp,param),
dbond_dR(R,param->oddd,param),doblock_dR);
// derivative of l/m/n
double dhblock_dlmn[243], doblock_dlmn[243];
fill_dmat(l,m,n,bond(R,param->hsss,param),bond(R,param->hsps,param),bond(R,param->hpps,param),bond(R,param->hppp,param),
bond(R,param->hsds,param),bond(R,param->hpds,param),bond(R,param->hpdp,param),bond(R,param->hdds,param),
bond(R,param->hddp,param),bond(R,param->hddd,param),dhblock_dlmn);
fill_dmat(l,m,n,bond(R,param->osss,param),bond(R,param->osps,param),bond(R,param->opps,param),bond(R,param->oppp,param),
bond(R,param->osds,param),bond(R,param->opds,param),bond(R,param->opdp,param),bond(R,param->odds,param),
bond(R,param->oddp,param),bond(R,param->oddd,param),doblock_dlmn);
for(int i=0 ; i<81 ; i++)
{
double dhblock0 = dhblock_dlmn[i+0*81]*l + dhblock_dlmn[i+1*81]*m + dhblock_dlmn[i+2*81]*n;
hblock_force[i+0*81] = -dhblock_dR[i]*l - dhblock_dlmn[i+0*81]/R + dhblock0*l/R;
hblock_force[i+1*81] = -dhblock_dR[i]*m - dhblock_dlmn[i+1*81]/R + dhblock0*m/R;
hblock_force[i+2*81] = -dhblock_dR[i]*n - dhblock_dlmn[i+2*81]/R + dhblock0*n/R;
double doblock0 = doblock_dlmn[i+0*81]*l + doblock_dlmn[i+1*81]*m + doblock_dlmn[i+2*81]*n;
oblock_force[i+0*81] = -doblock_dR[i]*l - doblock_dlmn[i+0*81]/R + doblock0*l/R;
oblock_force[i+1*81] = -doblock_dR[i]*m - doblock_dlmn[i+1*81]/R + doblock0*m/R;
oblock_force[i+2*81] = -doblock_dR[i]*n - doblock_dlmn[i+2*81]/R + doblock0*n/R;
}
}
//========================//
// 3. ATOMIC PARTITIONING //
//========================//
// NOTE: This version does not support additional blocking of atoms, which would improve performance but complicate the code
// The atoms would be reordered so that atoms within a block are contiguous & a neighbor list of blocks would be computed
// in addition to the neighbor list of atoms to define the block-sparse density matrix structure
// grid of boxes structure that partition the atoms
struct grid
{
int nx[3]; // number of boxes in each direction
double x0[3]; // minimum coordinate in each direction
double dx[3]; // width of boxes in each direction
int *to_atom; // index of locations in atom_index for the first atom in each box [nx[0]*nx[1]*nx[2]+1]
// NOTE: this list is ordered & to_atom[nx[0]*nx[1]*nx[2]] is the number of atoms
int *atom_index; // list of atom indices contained in each box [to_atom[nx*ny*nz]]
};
// find the box that contains a given atom
void box_index(double *atom, // target atom [3]
struct grid *partition, // specification of the grid for partitioning atoms [1]
int *box) // output box index [3]
{
for(int i=0 ; i<3 ; i++)
{ box[i] = (int)((atom[i] - partition->x0[i])/partition->dx[i]); }
}
// Find the grid index of a box
int grid_index(int* box, // target box [3]
struct grid* partition) // specification of the grid for partitioning atoms [1]
{
return (box[0] + partition->nx[0]*(box[1] + partition->nx[1]*box[2]));
}
// comparison function for sorting atoms by box using the C qsort function in stdlib.h
// RETURN: 1 if a goes after b, -1 if a goes before b, 0 if they are equal
int list_compare(const void *a, const void *b)
{
// sort by grid index first ...
if( ((int*)a)[1] > ((int*)b)[1] ) return 1;
if( ((int*)a)[1] < ((int*)b)[1] ) return -1;
// ... and atom index second
if( ((int*)a)[0] > ((int*)b)[0] ) return 1;
if( ((int*)a)[0] < ((int*)b)[0] ) return -1;
return 0;
}
// construct the grid structure for a list of atoms and a box width
// RETURN: grid structure with allocated memory
struct grid construct_grid(int natom, // number of atoms
double *atom, // atomic coordinates [3*natom]
double width) // box width that defines the uniform grid of boxes
{
struct grid partition;
// define the grid coordinates
for(int i=0 ; i<3 ; i++)
{
double xmin = atom[i], xmax = atom[i];
for(int j=1 ; j<natom ; j++)
{
if(atom[i+j*3] < xmin) { xmin = atom[i+j*3]; }
if(atom[i+j*3] > xmax) { xmax = atom[i+j*3]; }
}
partition.dx[i] = width; // uniform boxes
partition.nx[i] = (int)ceil((xmax - xmin)/width) + 1; // pad to prevent atoms near grid boundaries
partition.x0[i] = 0.5*(xmin + xmax - partition.nx[i]*width);
}
// memory allocation
int ngrid = partition.nx[0]*partition.nx[1]*partition.nx[2];
int *sort_list = (int*)malloc(sizeof(int)*2*natom);
partition.atom_index = (int*)malloc(sizeof(int)*natom); // not locally deallocated
partition.to_atom = (int*)malloc(sizeof(int)*(ngrid+1)); // not locally deallocated
// assign each atom to a box in the grid
for(int i=0 ; i<natom ; i++)
{
sort_list[2*i] = i;
int box[3];
box_index(&(atom[3*i]),&partition,box);
sort_list[1+2*i] = grid_index(box,&partition);
}
// sort atoms by box
qsort(sort_list,natom,sizeof(int)*2,list_compare);
// move sorted list into atom_index & construct to_atom
for(int i=0 ; i<ngrid ; i++)
{ partition.to_atom[i] = natom + 1; } // (natom + 1) indicates that a box has not been set yet
partition.to_atom[ngrid] = natom; // last entry is the number of atoms
for(int i=0 ; i<natom ; i++)
{
partition.atom_index[i] = sort_list[2*i];
if(partition.to_atom[sort_list[1+2*i]] == (natom + 1))
{ partition.to_atom[sort_list[1+2*i]] = i; }
}
for(int i=ngrid ; i>=1 ; i--)
{
if(partition.to_atom[i-1] == (natom + 1))
{ partition.to_atom[i-1] = partition.to_atom[i]; }
}
// memory deallocation
free(sort_list);
return partition;
}
// comparison function for sorting neighbor lists using the C qsort function in stdlib.h
// RETURN: 1 if a goes after b, -1 if a goes before b, 0 if they are equal
int neighbor_compare(const void *a, const void *b)
{
if( *((int*)a) > *((int*)b) ) return 1;
if( *((int*)a) < *((int*)b) ) return -1;
return 0;
}
// create a list of neighboring atoms for each atom (including self) as a sparsity pattern in CRS format
void neighbor_list(int natom, // number of atoms
double *atom, // atomic coordinates [3*natom]
double radius, // cutoff radius used to define the neighbor list
struct pattern *neighbor) // neighbor list defined by matrix sparsity pattern (no matrix elements) [1]
{
// determine a minimum radius value to avoid memory problems
double xmin[3], xmax[3];
for(int i=0 ; i<3 ; i++)
{
xmin[i] = xmax[i] = atom[i];
for(int j=1 ; j<natom ; j++)
{
if(atom[i+j*3] < xmin[i]) { xmin[i] = atom[i+j*3]; }
if(atom[i+j*3] > xmax[i]) { xmax[i] = atom[i+j*3]; }
}
}
double radius0 = pow((xmax[0] - xmin[0])*(xmax[1] - xmin[1])*(xmax[2] - xmin[2])/(double)(natom*pow(NBLOCK_MAX,2)),1.0/3.0);
// create a grid with allocated memory
struct grid partition;
if(radius > radius0) { partition = construct_grid(natom,atom,radius); }
else { partition = construct_grid(natom,atom,radius0); }
// allocate column list in neighbor matrix to store # of nearest neighbors
neighbor->ncol = neighbor->nrow = natom;
neighbor->col = (int*)malloc(sizeof(int)*(natom+1));