-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrigid-press.c
1645 lines (1488 loc) · 68.7 KB
/
rigid-press.c
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
// Geometry optimization of molecular crystals in a regularized rigid-body approximation
// METHODOLOGY COMMENTS ON THIS OPTIMIZER:
// - It would be more efficient & robust to work with only the symmetry-inequivalent structural degrees of freedom,
// but we don't have a convenient way of expressing those degrees of freedom, especially for Wyckoff positions.
//
// - It would be a little more efficient & stable to only consider 3 explicit degrees of freedom when perturbing
// the orientational quaternions. For example, rather than an additive perturbation, a multiplicative perturbation
// of the form (1, a, b, c) could be considered for infinitesimal a, b, & c.
//
// - There is nothing particularly special about the regularized rigid-body approximation that was chosen here,
// it could be replaced by a more physical & accurate intermolecular interaction, which might improve the overlap
// between the set of structures that are local minima here versus the true local minima of an accurate energy surface.
//
// - Space-group symmetries are likely to break if the initial structure is too loosely packed relative to the cutoff
// distance of the regularized interatomic interaction. This can be mitigated by increasing the cutoff distance.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "crystal.h"
#include "cocrystal.h"
#include "rigid-press.h"
// prototypes for external dependencies (BLAS & LAPACK)
void dgemv_(char*, int*, int*, double*, double*, int*, double*, int*, double*, double*, int*);
void dgemm_(char*, char*, int*, int*, int*, double*, double*, int*, double*, int*, double*, double*, int*);
void dsyev_(char*, char*, int*, double*, int*, double*, double*, int*, int*);
void dgeqrf_(int*, int*, double*, int*, double*, double*, int*, int*);
void dormqr_(char*, char*, int*, int*, int*, double*, int*, double*, double*, int*, double*, int*, int*);
void dgesvd_(char*, char*, int*, int*, double*, int*, double*, double*, int*, double*, int*, double*, int* ,int*);
// cutoff distance of the interaction kernel
#define INTERACTION_CUTOFF 10.0
// parameters defining the regularized interatomic contact interaction
#define INTERACTION_WEIGHT 0.1
// number of steps to take for each Golden-section line search
#define GOLDEN_STEPS 20
// optimization tolerance on energy change in 1 iteration relative to minimum energy
#define OPTIMIZATION_TOLERANCE 1e-6
// step size for numerical tests of analytical derivatives
#define STEP 1e-4
// the state vector of a crystal's geometry has size 6+7*nmol containing the following information:
// state[0] : 1st lattice vector (x)
// state[1-2] : 2nd lattice vector (x-y)
// state[3-5] : 3rd lattice vector (x-y-z)
// state[6-8] : center of 1st molecule (x-y-z)
// state[9-12] : orientational quaternion of 1st molecule
// ...
// information about a molecular crystal to define the structural relaxation problem
struct molecular_crystal
{
// size information
int ntype; // number of types of molecules in the crystal
int nmol; // number of molecules per unit cell
// information about each type of molecule
int *natom; // number of atoms in a molecule type [ntype]
double **geometry; // centered geometry of a molecule type (interleaved x-y-z format) [ntype][3*natom[i]]
double ***collide; // collision distances for pairs of atoms between 2 molecules [ntype][ntype][natom[i]*natom[j]]
// information about the crystal
int *type; // type of each molecule in the unit cell [nmol]
int *invert; // inversion of each molecule in the unit cell (+1 for standard, -1 for inverted) [nmol]
};
// deallocate memory in the molecular_crystal structure
void free_molecular_crystal(struct molecular_crystal *xtl)
{
free(xtl->type);
free(xtl->invert);
for(int i=0 ; i<xtl->ntype ; i++)
{ free(xtl->geometry[i]); }
free(xtl->geometry);
for(int i=0 ; i<xtl->ntype ; i++)
{
for(int j=0 ; j<xtl->ntype ; j++)
{ free(xtl->collide[i][j]); }
free(xtl->collide[i]);
}
free(xtl->collide);
free(xtl->natom);
}
// interatomic interaction kernel & its 1st & 2nd distance derivatives
double kernel(double distance, // interatomic distance
// kernel parameters: (add/replace parameters for more physical interactions)
double distance0, // collision distance
double wt) // kernel weight
{
if(distance > INTERACTION_CUTOFF)
{ return 0.0; }
if(distance < distance0)
{ return INFINITY; }
return wt*(INTERACTION_CUTOFF - distance)/(distance - distance0);
}
double dkernel(double distance, // interatomic distance
// kernel parameters: (add/replace parameters for more physical interactions)
double distance0, // collision distance
double wt) // kernel weight
{
if(distance > INTERACTION_CUTOFF)
{ return 0.0; }
if(distance < distance0)
{ return NAN; }
double recip = 1.0/(distance - distance0);
return -wt*((INTERACTION_CUTOFF - distance)*recip*recip + recip);
}
double d2kernel(double distance, // interatomic distance
// kernel parameters: (add/replace parameters for more physical interactions)
double distance0, // collision distance
double wt) // kernel weight
{
if(distance > INTERACTION_CUTOFF)
{ return 0.0; }
if(distance < distance0)
{ return NAN; }
double recip = 1.0/(distance - distance0);
return 2.0*wt*((INTERACTION_CUTOFF - distance)*recip*recip*recip + recip*recip);
}
// position of atom in a translated & rotated molecule
void position(double *local, // local coordinate of atom in the molecule [3]
double *state, // state vector of the molecule (x-y-z & orientational quaternion) [7]
double *global) // global coordinate of the atom in the crystal [3]
{
double wt = 2.0/(state[3]*state[3] + state[4]*state[4] + state[5]*state[5] + state[6]*state[6]);
global[0] = state[0] + local[0] + wt*(-(state[5]*state[5] + state[6]*state[6])*local[0]
+ (state[4]*state[5] - state[3]*state[6])*local[1]
+ (state[4]*state[6] + state[3]*state[5])*local[2]);
global[1] = state[1] + local[1] + wt*( (state[4]*state[5] + state[3]*state[6])*local[0]
- (state[4]*state[4] + state[6]*state[6])*local[1]
+ (state[5]*state[6] - state[3]*state[4])*local[2]);
global[2] = state[2] + local[2] + wt*( (state[4]*state[6] - state[3]*state[5])*local[0]
+ (state[5]*state[6] + state[3]*state[4])*local[1]
- (state[4]*state[4] + state[5]*state[5])*local[2]);
}
// NOTE: simple derivatives w.r.t. state[0], state[1], & state[2] are ignored here
void position_derivative(double *local, // local coordinate of atom in the molecule [3]
double *state, // state vector of the molecule (x-y-z & orientational quaternion) [7]
double *global1, // global coordinate 1st derivatives of the atom in the crystal [12]
double *global2) // global coordinate 2nd derivatives of the atom in the crystal [48]
{
double wt = 2.0/(state[3]*state[3] + state[4]*state[4] + state[5]*state[5] + state[6]*state[6]);
double dwt = -wt*wt, dglobal[3], dglobal2[12];
dglobal[0] = dwt*(-(state[5]*state[5] + state[6]*state[6])*local[0]
+ (state[4]*state[5] - state[3]*state[6])*local[1]
+ (state[4]*state[6] + state[3]*state[5])*local[2]);
dglobal[1] = dwt*( (state[4]*state[5] + state[3]*state[6])*local[0]
- (state[4]*state[4] + state[6]*state[6])*local[1]
+ (state[5]*state[6] - state[3]*state[4])*local[2]);
dglobal[2] = dwt*( (state[4]*state[6] - state[3]*state[5])*local[0]
+ (state[5]*state[6] + state[3]*state[4])*local[1]
- (state[4]*state[4] + state[5]*state[5])*local[2]);
dglobal2[0 + 0*3] = dwt*(-state[6]*local[1] + state[5]*local[2]);
dglobal2[0 + 1*3] = dwt*( state[5]*local[1] + state[6]*local[2]);
dglobal2[0 + 2*3] = dwt*(-2.0*state[5]*local[0] + state[4]*local[1] + state[3]*local[2]);
dglobal2[0 + 3*3] = dwt*(-2.0*state[6]*local[0] - state[3]*local[1] + state[4]*local[2]);
dglobal2[1 + 0*3] = dwt*(state[6]*local[0] - state[4]*local[2]);
dglobal2[1 + 1*3] = dwt*(state[5]*local[0] - 2.0*state[4]*local[1] - state[3]*local[2]);
dglobal2[1 + 2*3] = dwt*(state[4]*local[0] + state[6]*local[2]);
dglobal2[1 + 3*3] = dwt*(state[3]*local[0] - 2.0*state[6]*local[1] + state[5]*local[2]);
dglobal2[2 + 0*3] = dwt*(-state[5]*local[0] + state[4]*local[1]);
dglobal2[2 + 1*3] = dwt*( state[6]*local[0] + state[3]*local[1] - 2.0*state[4]*local[2]);
dglobal2[2 + 2*3] = dwt*(-state[3]*local[0] + state[6]*local[1] - 2.0*state[5]*local[2]);
dglobal2[2 + 3*3] = dwt*( state[4]*local[0] + state[5]*local[1]);
for(int i=0 ; i<4 ; i++)
for(int j=0 ; j<3 ; j++)
{ global1[j + i*3] = state[i+3]*dglobal[j]; }
global1[0 + 0*3] += wt*(-state[6]*local[1] + state[5]*local[2]);
global1[0 + 1*3] += wt*( state[5]*local[1] + state[6]*local[2]);
global1[0 + 2*3] += wt*(-2.0*state[5]*local[0] + state[4]*local[1] + state[3]*local[2]);
global1[0 + 3*3] += wt*(-2.0*state[6]*local[0] - state[3]*local[1] + state[4]*local[2]);
global1[1 + 0*3] += wt*(state[6]*local[0] - state[4]*local[2]);
global1[1 + 1*3] += wt*(state[5]*local[0] - 2.0*state[4]*local[1] - state[3]*local[2]);
global1[1 + 2*3] += wt*(state[4]*local[0] + state[6]*local[2]);
global1[1 + 3*3] += wt*(state[3]*local[0] - 2.0*state[6]*local[1] + state[5]*local[2]);
global1[2 + 0*3] += wt*(-state[5]*local[0] + state[4]*local[1]);
global1[2 + 1*3] += wt*( state[6]*local[0] + state[3]*local[1] - 2.0*state[4]*local[2]);
global1[2 + 2*3] += wt*(-state[3]*local[0] + state[6]*local[1] - 2.0*state[5]*local[2]);
global1[2 + 3*3] += wt*( state[4]*local[0] + state[5]*local[1]);
for(int i=0 ; i<4 ; i++)
for(int j=0 ; j<4 ; j++)
for(int k=0 ; k<3 ; k++)
{
global2[k + j*3 + i*12] = -2.0*wt*state[i+3]*state[j+3]*dglobal[k]
+ state[i+3]*dglobal2[k + j*3] + state[j+3]*dglobal2[k + i*3];
}
for(int i=0 ; i<4 ; i++)
for(int j=0 ; j<3 ; j++)
{ global2[j + i*3 + i*12] += dglobal[j]; }
global2[0 + 0*3 + 2*12] += wt*local[2];
global2[0 + 0*3 + 3*12] += -wt*local[1];
global2[0 + 1*3 + 2*12] += wt*local[1];
global2[0 + 1*3 + 3*12] += wt*local[2];
global2[0 + 2*3 + 0*12] += wt*local[2];
global2[0 + 2*3 + 1*12] += wt*local[1];
global2[0 + 2*3 + 2*12] += -2.0*wt*local[0];
global2[0 + 3*3 + 0*12] += -wt*local[1];
global2[0 + 3*3 + 1*12] += wt*local[2];
global2[0 + 3*3 + 3*12] += -2.0*wt*local[0];
global2[1 + 0*3 + 1*12] += -wt*local[2];
global2[1 + 0*3 + 3*12] += wt*local[0];
global2[1 + 1*3 + 0*12] += -wt*local[2];
global2[1 + 1*3 + 1*12] += -2.0*wt*local[1];
global2[1 + 1*3 + 2*12] += wt*local[0];
global2[1 + 2*3 + 1*12] += wt*local[0];
global2[1 + 2*3 + 3*12] += wt*local[2];
global2[1 + 3*3 + 0*12] += wt*local[0];
global2[1 + 3*3 + 2*12] += wt*local[2];
global2[1 + 3*3 + 3*12] += -2.0*wt*local[1];
global2[2 + 0*3 + 1*12] += wt*local[1];
global2[2 + 0*3 + 2*12] += -wt*local[0];
global2[2 + 1*3 + 0*12] += wt*local[1];
global2[2 + 1*3 + 1*12] += -2.0*wt*local[2];
global2[2 + 1*3 + 3*12] += wt*local[0];
global2[2 + 2*3 + 0*12] += -wt*local[0];
global2[2 + 2*3 + 2*12] += -2.0*wt*local[2];
global2[2 + 2*3 + 3*12] += wt*local[1];
global2[2 + 3*3 + 1*12] += wt*local[0];
global2[2 + 3*3 + 2*12] += wt*local[1];
}
void position_derivative_test(double *local, // local coordinate of atom in the molecule [3]
double *state, // state vector of the molecule [7]
double *global1, // global coordinate 1st derivatives of the atom in the crystal [12]
double *global2) // global coordinate 2nd derivatives of the atom in the crystal [48]
{
for(int i=0 ; i<4 ; i++)
{
double state0 = state[i+3], global_plus[3], global_minus[3], global1_plus[12], global1_minus[12], dummy[48];
state[i+3] += STEP;
position(local, state, global_plus);
position_derivative(local, state, global1_plus, dummy);
state[i+3] -= 2.0*STEP;
position(local, state, global_minus);
position_derivative(local, state, global1_minus, dummy);
state[i+3] = state0;
for(int j=0 ; j<3 ; j++)
{ global1[j + i*3] = (global_plus[j] - global_minus[j])/(2.0*STEP); }
for(int j=0 ; j<12 ; j++)
{ global2[j + i*12] = (global1_plus[j] - global1_minus[j])/(2.0*STEP); }
}
}
// interaction energy between a pair of molecules
double pair_energy(int natom1, // number of atoms in the 1st molecule
int natom2, // number of atoms in the 2nd molecule
int invert1, // inversion flag for 1st molecule {+1, -1}
int invert2, // inversion flag for 2nd molecule {+1, -1}
double *geo1, // geometry of the 1st molecule [3*natom1]
double *geo2, // geometry of the 2nd molecule [3*natom2]
double *collide, // collision matrix [natom1*natom2]
double *state1, // state vector of the 1st molecule [7]
double *state2) // state vector of the 2nd molecule [7]
{
double energy = 0.0, wt = INTERACTION_WEIGHT/(double)(natom1*natom2);
for(int i=0 ; i<natom1 ; i++)
{
double local1[3], coord1[3];
local1[0] = invert1*geo1[3*i]; local1[1] = invert1*geo1[1+3*i]; local1[2] = invert1*geo1[2+3*i];
position(local1, state1, coord1);
for(int j=0 ; j<natom2 ; j++)
{
double local2[3], coord2[3];
local2[0] = invert2*geo2[3*j]; local2[1] = invert2*geo2[1+3*j]; local2[2] = invert2*geo2[2+3*j];
position(local2, state2, coord2);
// regularized contact interaction
double dist = sqrt((coord1[0] - coord2[0])*(coord1[0] - coord2[0])
+(coord1[1] - coord2[1])*(coord1[1] - coord2[1])
+(coord1[2] - coord2[2])*(coord1[2] - coord2[2]));
energy += kernel(dist, collide[i+j*natom1], wt);
}
}
return energy;
}
void pair_energy_derivative(int natom1, // number of atoms in the 1st molecule
int natom2, // number of atoms in the 2nd molecule
int invert1, // inversion flag for 1st molecule {+1, -1}
int invert2, // inversion flag for 2nd molecule {+1, -1}
double *geo1, // geometry of the 1st molecule [3*natom1]
double *geo2, // geometry of the 2nd molecule [3*natom2]
double *collide, // collision matrix [natom1*natom2]
double *state1, // state vector of the 1st molecule [7]
double *state2, // state vector of the 2nd molecule [7]
double *grad1, // 1st derivatives w.r.t. 1st state vector [7]
double *grad2, // 1st derivatives w.r.t. 2nd state vector [7]
double *hess11, // 2nd derivatives w.r.t. 1st state vector [49]
double *hess22, // 2nd derivatives w.r.t. 2nd state vector [49]
double *hess12) // mixed 2nd derivatives [49]
{
for(int i=0 ; i<7 ; i++)
{ grad1[i] = grad2[i] = 0.0; }
for(int i=0 ; i<49 ; i++)
{ hess11[i] = hess12[i] = hess22[i] = 0.0; }
double wt = INTERACTION_WEIGHT/(double)(natom1*natom2);
for(int i=0 ; i<natom1 ; i++)
{
double local1[3], coord1[3], coord1_deriv1[12], coord1_deriv2[48];
local1[0] = invert1*geo1[3*i]; local1[1] = invert1*geo1[1+3*i]; local1[2] = invert1*geo1[2+3*i];
position(local1, state1, coord1);
position_derivative(local1, state1, coord1_deriv1, coord1_deriv2);
for(int j=0 ; j<natom2 ; j++)
{
double local2[3], coord2[3], coord2_deriv1[12], coord2_deriv2[48];
local2[0] = invert2*geo2[3*j]; local2[1] = invert2*geo2[1+3*j]; local2[2] = invert2*geo2[2+3*j];
position(local2, state2, coord2);
position_derivative(local2, state2, coord2_deriv1, coord2_deriv2);
double dist = sqrt((coord1[0] - coord2[0])*(coord1[0] - coord2[0])
+(coord1[1] - coord2[1])*(coord1[1] - coord2[1])
+(coord1[2] - coord2[2])*(coord1[2] - coord2[2]));
double denergy = dkernel(dist, collide[i+j*natom1], wt)/dist;
double d2energy = (d2kernel(dist, collide[i+j*natom1], wt) - denergy)/(dist*dist);
// simple derivatives w.r.t. state1[0], state1[1], state1[2], state2[0], state2[1], & state2[2]
for(int k=0 ; k<3 ; k++)
{
double delta = coord1[k] - coord2[k];
grad1[k] += denergy*delta;
grad2[k] -= denergy*delta;
hess11[k + k*7] += denergy;
hess22[k + k*7] += denergy;
hess12[k + k*7] -= denergy;
for(int l=0 ; l<3 ; l++)
{
double delta2 = coord1[l] - coord2[l];
hess11[l + k*7] += d2energy*delta*delta2;
hess22[l + k*7] += d2energy*delta*delta2;
hess12[l + k*7] -= d2energy*delta*delta2;
}
}
// gradients & mixed quaternion-translation hessian
for(int k=0 ; k<4 ; k++)
for(int l=0 ; l<3 ; l++)
{
double delta = coord1[l] - coord2[l];
grad1[k+3] += denergy*delta*coord1_deriv1[l+k*3];
grad2[k+3] -= denergy*delta*coord2_deriv1[l+k*3];
hess11[k+3 + l*7] += denergy*coord1_deriv1[l+k*3];
hess22[k+3 + l*7] += denergy*coord2_deriv1[l+k*3];
hess12[k+3 + l*7] -= denergy*coord1_deriv1[l+k*3];
hess11[l + (k+3)*7] += denergy*coord1_deriv1[l+k*3];
hess22[l + (k+3)*7] += denergy*coord2_deriv1[l+k*3];
hess12[l + (k+3)*7] -= denergy*coord2_deriv1[l+k*3];
for(int m=0 ; m<3 ; m++)
{
double delta2 = coord1[m] - coord2[m];
hess11[k+3 + m*7] += d2energy*delta*delta2*coord1_deriv1[l+k*3];
hess22[k+3 + m*7] += d2energy*delta*delta2*coord2_deriv1[l+k*3];
hess12[k+3 + m*7] -= d2energy*delta*delta2*coord1_deriv1[l+k*3];
hess11[m + (k+3)*7] += d2energy*delta*delta2*coord1_deriv1[l+k*3];
hess22[m + (k+3)*7] += d2energy*delta*delta2*coord2_deriv1[l+k*3];
hess12[m + (k+3)*7] -= d2energy*delta*delta2*coord2_deriv1[l+k*3];
}
}
// quaternion components of hessian
for(int k=0 ; k<4 ; k++)
for(int l=0 ; l<4 ; l++)
for(int m=0 ; m<3 ; m++)
{
double delta = coord1[m] - coord2[m];
hess11[k+3 + (l+3)*7] += denergy*coord1_deriv1[m+k*3]*coord1_deriv1[m+l*3];
hess22[k+3 + (l+3)*7] += denergy*coord2_deriv1[m+k*3]*coord2_deriv1[m+l*3];
hess12[k+3 + (l+3)*7] -= denergy*coord1_deriv1[m+k*3]*coord2_deriv1[m+l*3];
hess11[k+3 + (l+3)*7] += denergy*delta*coord1_deriv2[m+k*3+l*12];
hess22[k+3 + (l+3)*7] -= denergy*delta*coord2_deriv2[m+k*3+l*12];
for(int n=0 ; n<3 ; n++)
{
double delta2 = coord1[n] - coord2[n];
hess11[k+3 + (l+3)*7] += d2energy*delta*delta2*coord1_deriv1[m+k*3]*coord1_deriv1[n+l*3];
hess22[k+3 + (l+3)*7] += d2energy*delta*delta2*coord2_deriv1[m+k*3]*coord2_deriv1[n+l*3];
hess12[k+3 + (l+3)*7] -= d2energy*delta*delta2*coord1_deriv1[m+k*3]*coord2_deriv1[n+l*3];
}
}
}
}
}
void pair_energy_derivative_test(int natom1, // number of atoms in the 1st molecule
int natom2, // number of atoms in the 2nd molecule
int invert1, // inversion flag for 1st molecule {+1, -1}
int invert2, // inversion flag for 2nd molecule {+1, -1}
double *geo1, // geometry of the 1st molecule [3*natom1]
double *geo2, // geometry of the 2nd molecule [3*natom2]
double *collide, // collision matrix [natom1*natom2]
double *state1, // state vector of the 1st molecule [7]
double *state2, // state vector of the 2nd molecule [7]
double *grad1, // 1st derivatives w.r.t. 1st state vector [7]
double *grad2, // 1st derivatives w.r.t. 2nd state vector [7]
double *hess11, // 2nd derivatives w.r.t. 1st state vector [49]
double *hess22, // 2nd derivatives w.r.t. 2nd state vector [49]
double *hess12) // mixed 2nd derivatives [49]
{
for(int i=0 ; i<7; i++)
{
double state0 = state1[i], grad1_plus[7], grad1_minus[7], grad2_plus[7], grad2_minus[7], dummy[49];
state1[i] += STEP;
double energy_plus = pair_energy(natom1,natom2,invert1,invert2,geo1,geo2,collide,state1,state2);
pair_energy_derivative(natom1,natom2,invert1,invert2,geo1,geo2,collide,state1,state2,grad1_plus,grad2_plus,dummy,dummy,dummy);
state1[i] -= 2.0*STEP;
double energy_minus = pair_energy(natom1,natom2,invert1,invert2,geo1,geo2,collide,state1,state2);
pair_energy_derivative(natom1,natom2,invert1,invert2,geo1,geo2,collide,state1,state2,grad1_minus,grad2_minus,dummy,dummy,dummy);
state1[i] = state0;
grad1[i] = (energy_plus - energy_minus)/(2.0*STEP);
for(int j=0 ; j<7 ; j++)
{
hess11[i+j*7] = (grad1_plus[j] - grad1_minus[j])/(2.0*STEP);
hess12[i+j*7] = (grad2_plus[j] - grad2_minus[j])/(2.0*STEP);
}
state0 = state2[i];
state2[i] += STEP;
energy_plus = pair_energy(natom1,natom2,invert1,invert2,geo1,geo2,collide,state1,state2);
pair_energy_derivative(natom1,natom2,invert1,invert2,geo1,geo2,collide,state1,state2,grad1_plus,grad2_plus,dummy,dummy,dummy);
state2[i] -= 2.0*STEP;
energy_minus = pair_energy(natom1,natom2,invert1,invert2,geo1,geo2,collide,state1,state2);
pair_energy_derivative(natom1,natom2,invert1,invert2,geo1,geo2,collide,state1,state2,grad1_minus,grad2_minus,dummy,dummy,dummy);
state2[i] = state0;
grad2[i] = (energy_plus - energy_minus)/(2.0*STEP);
for(int j=0 ; j<7 ; j++)
{ hess22[i+j*7] = (grad2_plus[j] - grad2_minus[j])/(2.0*STEP); }
}
}
// calculate a lattice-aligned bounding box for a molecule
void bound_box(int natom, // number of atoms in the molecule
int invert, // inversion of the molecule {+1, -1}
double *geo, // atomic coordinates of the molecule [3*natom]
double *state, // state of the molecule [7]
double *reclat, // reciprocal lattice vectors [6]
double *box) // bounding box [6]
{
box[0] = box[2] = box[4] = INFINITY;
box[1] = box[3] = box[5] = -INFINITY;
for(int i=0 ; i<natom ; i++)
{
double local[3], global[3], latpos[3];
local[0] = invert*geo[3*i]; local[1] = invert*geo[1+3*i]; local[2] = invert*geo[2+3*i];
position(local, state, global);
latpos[0] = reclat[0]*global[0] + reclat[1]*global[1] + reclat[2]*global[2];
latpos[1] = reclat[3]*global[1] + reclat[4]*global[2];
latpos[2] = reclat[5]*global[2];
if(latpos[0] < box[0]) { box[0] = latpos[0]; }
if(latpos[0] > box[1]) { box[1] = latpos[0]; }
if(latpos[1] < box[2]) { box[2] = latpos[1]; }
if(latpos[1] > box[3]) { box[3] = latpos[1]; }
if(latpos[2] < box[4]) { box[4] = latpos[2]; }
if(latpos[2] > box[5]) { box[5] = latpos[2]; }
}
}
// energy function that we are minimizing to relax the molecular crystal
double total_energy(struct molecular_crystal *xtl, // description of the crystal being optimized
double *state) // the crystal's state vector [6+7*xtl->nmol]
{
double energy = fabs(state[0]*state[2]*state[5]);
// construct reciprocal lattice vectors (1st: x-y-z, 2nd: y-z, 3rd: z)
double wt = 1.0/(state[0]*state[2]*state[5]), reclat[6];
reclat[0] = wt*state[2]*state[5];
reclat[1] = -wt*state[1]*state[5];
reclat[2] = wt*(state[1]*state[4] - state[2]*state[3]);
reclat[3] = wt*state[5]*state[0];
reclat[4] = -wt*state[4]*state[0];
reclat[5] = wt*state[0]*state[2];
// calculate buffer (lattice-aligned bounding box for the interaction sphere)
double buffer[3];
buffer[0] = INTERACTION_CUTOFF/sqrt(state[0]*state[0]);
buffer[1] = INTERACTION_CUTOFF/sqrt(state[1]*state[1] + state[2]*state[2]);
buffer[2] = INTERACTION_CUTOFF/sqrt(state[3]*state[3] + state[4]*state[4] + state[5]*state[5]);
// loop over molecules in the central unit cell
for(int i=0 ; i<xtl->nmol ; i++)
{
double *state1 = state + 6 + 7*i;
// calculate lattice-aligned bounding box for molecule 1
double box1[6];
bound_box(xtl->natom[xtl->type[i]], xtl->invert[i], xtl->geometry[xtl->type[i]], state1, reclat, box1);
// loop over molecules in 2nd unit cell
for(int j=0 ; j<xtl->nmol ; j++)
{
// calculate lattice-aligned bounding box for molecule 2
double box2[6];
bound_box(xtl->natom[xtl->type[j]], xtl->invert[j], xtl->geometry[xtl->type[j]], state + 6 + 7*j, reclat, box2);
// adjust lattice vector summation for pair of molecules
int latmin1, latmin2, latmin3, latmax1, latmax2, latmax3;
latmin1 = floor(box1[0] - box2[1] - buffer[0]);
latmin2 = floor(box1[2] - box2[3] - buffer[1]);
latmin3 = floor(box1[4] - box2[5] - buffer[2]);
latmax1 = ceil(box1[1] - box2[0] + buffer[0]);
latmax2 = ceil(box1[3] - box2[2] + buffer[1]);
latmax3 = ceil(box1[5] - box2[4] + buffer[2]);
// loop over interacting unit cells
for(int k=latmin1 ; k<=latmax1 ; k++)
for(int l=latmin2 ; l<=latmax2 ; l++)
for(int m=latmin3 ; m<=latmax3 ; m++)
{
// molecules don't interact with themselves & only consider unique pairs in central cell
if(i >= j && k == 0 && l == 0 && m == 0)
{ continue; }
// shift center of the 2nd molecule
double state2[7];
for(int n=0 ; n<7 ; n++)
{ state2[n] = state[n+6+7*j]; }
state2[0] += k*state[0] + l*state[1] + m*state[3];
state2[1] += l*state[2] + m*state[4];
state2[2] += m*state[5];
energy += pair_energy(xtl->natom[xtl->type[i]], xtl->natom[xtl->type[j]],
xtl->invert[i], xtl->invert[j],
xtl->geometry[xtl->type[i]], xtl->geometry[xtl->type[j]],
xtl->collide[xtl->type[i]][xtl->type[j]],
state1, state2);
if(energy == INFINITY)
{ return energy; }
}
}
}
return energy;
}
void total_energy_derivative(struct molecular_crystal *xtl, // description of the crystal being optimized
double *state, // the crystal's state vector [6+7*xtl->nmol]
double *grad, // 1st derivative of the total energy [6+7*xtl->nmol]
double *hess) // 2nd derivative of the total energy [(6+7*xtl->nmol)*(6+7*xtl->nmol)]
{
int size = 6+7*xtl->nmol;
double sign0 = 1.0, sign2 = 1.0, sign5 = 1.0;
if(state[0] < 0.0)
{ sign0 = -1.0; }
if(state[2] < 0.0)
{ sign2 = -1.0; }
if(state[5] < 0.0)
{ sign5 = -1.0; }
for(int i=0 ; i<size ; i++)
{ grad[i] = 0.0; }
grad[0] = fabs(state[2]*state[5])*sign0;
grad[2] = fabs(state[0]*state[5])*sign2;
grad[5] = fabs(state[0]*state[2])*sign5;
for(int i=0 ; i<size*size ; i++)
{ hess[i] = 0.0; }
hess[0 + 2*size] = fabs(state[5])*sign0*sign2;
hess[2 + 0*size] = fabs(state[5])*sign0*sign2;
hess[0 + 5*size] = fabs(state[2])*sign0*sign5;
hess[5 + 0*size] = fabs(state[2])*sign0*sign5;
hess[2 + 5*size] = fabs(state[0])*sign2*sign5;
hess[5 + 2*size] = fabs(state[0])*sign2*sign5;
// construct reciprocal lattice vectors (1st: x-y-z, 2nd: y-z, 3rd: z)
double wt = 1.0/(state[0]*state[2]*state[5]), reclat[6];
reclat[0] = wt*state[2]*state[5];
reclat[1] = -wt*state[1]*state[5];
reclat[2] = wt*(state[1]*state[4] - state[2]*state[3]);
reclat[3] = wt*state[5]*state[0];
reclat[4] = -wt*state[4]*state[0];
reclat[5] = wt*state[0]*state[2];
// calculate buffer (lattice-aligned bounding box for the interaction sphere)
double buffer[3];
buffer[0] = INTERACTION_CUTOFF/sqrt(state[0]*state[0]);
buffer[1] = INTERACTION_CUTOFF/sqrt(state[1]*state[1] + state[2]*state[2]);
buffer[2] = INTERACTION_CUTOFF/sqrt(state[3]*state[3] + state[4]*state[4] + state[5]*state[5]);
// loop over molecules in the central unit cell
for(int i=0 ; i<xtl->nmol ; i++)
{
double *state1 = state + 6 + 7*i;
// calculate lattice-aligned bounding box for molecule 1
double box1[6];
bound_box(xtl->natom[xtl->type[i]], xtl->invert[i], xtl->geometry[xtl->type[i]], state1, reclat, box1);
// loop over molecules in 2nd unit cell
for(int j=0 ; j<xtl->nmol ; j++)
{
// calculate lattice-aligned bounding box for molecule 2
double box2[6];
bound_box(xtl->natom[xtl->type[j]], xtl->invert[j], xtl->geometry[xtl->type[j]], state + 6 + 7*j, reclat, box2);
// adjust lattice vector summation for pair of molecules
int latmin1, latmin2, latmin3, latmax1, latmax2, latmax3;
latmin1 = floor(box1[0] - box2[1] - buffer[0]);
latmin2 = floor(box1[2] - box2[3] - buffer[1]);
latmin3 = floor(box1[4] - box2[5] - buffer[2]);
latmax1 = ceil(box1[1] - box2[0] + buffer[0]);
latmax2 = ceil(box1[3] - box2[2] + buffer[1]);
latmax3 = ceil(box1[5] - box2[4] + buffer[2]);
// loop over interacting unit cells
for(int k=latmin1 ; k<=latmax1 ; k++)
for(int l=latmin2 ; l<=latmax2 ; l++)
for(int m=latmin3 ; m<=latmax3 ; m++)
{
// molecules don't interact with themselves & only consider unique pairs in central cell
if(i >= j && k == 0 && l == 0 && m == 0)
{ continue; }
// shift center of the 2nd molecule
double state2[7];
for(int n=0 ; n<7 ; n++)
{ state2[n] = state[n+6+7*j]; }
state2[0] += k*state[0] + l*state[1] + m*state[3];
state2[1] += l*state[2] + m*state[4];
state2[2] += m*state[5];
double grad1[7], grad2[7], hess11[49], hess22[49], hess12[49];
pair_energy_derivative(xtl->natom[xtl->type[i]], xtl->natom[xtl->type[j]],
xtl->invert[i], xtl->invert[j],
xtl->geometry[xtl->type[i]], xtl->geometry[xtl->type[j]],
xtl->collide[xtl->type[i]][xtl->type[j]],
state1, state2, grad1, grad2, hess11, hess22, hess12);
for(int n=0 ; n<7 ; n++)
{
grad[n+6+i*7] += grad1[n];
grad[n+6+j*7] += grad2[n];
}
grad[0] += k*grad2[0];
grad[1] += l*grad2[0];
grad[2] += l*grad2[1];
grad[3] += m*grad2[0];
grad[4] += m*grad2[1];
grad[5] += m*grad2[2];
for(int n=0 ; n<7 ; n++)
for(int o=0 ; o<7 ; o++)
{
hess[n+6+i*7 + (o+6+i*7)*size] += hess11[n + o*7];
hess[n+6+j*7 + (o+6+j*7)*size] += hess22[n + o*7];
hess[n+6+i*7 + (o+6+j*7)*size] += hess12[n + o*7];
hess[n+6+j*7 + (o+6+i*7)*size] += hess12[o + n*7];
}
for(int n=0 ; n<7 ; n++)
{
hess[0 + (n+6+i*7)*size] += k*hess12[n + 0*7];
hess[1 + (n+6+i*7)*size] += l*hess12[n + 0*7];
hess[2 + (n+6+i*7)*size] += l*hess12[n + 1*7];
hess[3 + (n+6+i*7)*size] += m*hess12[n + 0*7];
hess[4 + (n+6+i*7)*size] += m*hess12[n + 1*7];
hess[5 + (n+6+i*7)*size] += m*hess12[n + 2*7];
hess[0 + (n+6+j*7)*size] += k*hess22[n + 0*7];
hess[1 + (n+6+j*7)*size] += l*hess22[n + 0*7];
hess[2 + (n+6+j*7)*size] += l*hess22[n + 1*7];
hess[3 + (n+6+j*7)*size] += m*hess22[n + 0*7];
hess[4 + (n+6+j*7)*size] += m*hess22[n + 1*7];
hess[5 + (n+6+j*7)*size] += m*hess22[n + 2*7];
hess[n+6+i*7 + 0*size] += k*hess12[n + 0*7];
hess[n+6+i*7 + 1*size] += l*hess12[n + 0*7];
hess[n+6+i*7 + 2*size] += l*hess12[n + 1*7];
hess[n+6+i*7 + 3*size] += m*hess12[n + 0*7];
hess[n+6+i*7 + 4*size] += m*hess12[n + 1*7];
hess[n+6+i*7 + 5*size] += m*hess12[n + 2*7];
hess[n+6+j*7 + 0*size] += k*hess22[n + 0*7];
hess[n+6+j*7 + 1*size] += l*hess22[n + 0*7];
hess[n+6+j*7 + 2*size] += l*hess22[n + 1*7];
hess[n+6+j*7 + 3*size] += m*hess22[n + 0*7];
hess[n+6+j*7 + 4*size] += m*hess22[n + 1*7];
hess[n+6+j*7 + 5*size] += m*hess22[n + 2*7];
}
hess[0 + 0*size] += k*k*hess22[0 + 0*7];
hess[1 + 0*size] += k*l*hess22[0 + 0*7];
hess[2 + 0*size] += k*l*hess22[1 + 0*7];
hess[3 + 0*size] += k*m*hess22[0 + 0*7];
hess[4 + 0*size] += k*m*hess22[1 + 0*7];
hess[5 + 0*size] += k*m*hess22[2 + 0*7];
hess[0 + 1*size] += l*k*hess22[0 + 0*7];
hess[1 + 1*size] += l*l*hess22[0 + 0*7];
hess[2 + 1*size] += l*l*hess22[1 + 0*7];
hess[3 + 1*size] += l*m*hess22[0 + 0*7];
hess[4 + 1*size] += l*m*hess22[1 + 0*7];
hess[5 + 1*size] += l*m*hess22[2 + 0*7];
hess[0 + 2*size] += l*k*hess22[0 + 1*7];
hess[1 + 2*size] += l*l*hess22[0 + 1*7];
hess[2 + 2*size] += l*l*hess22[1 + 1*7];
hess[3 + 2*size] += l*m*hess22[0 + 1*7];
hess[4 + 2*size] += l*m*hess22[1 + 1*7];
hess[5 + 2*size] += l*m*hess22[2 + 1*7];
hess[0 + 3*size] += m*k*hess22[0 + 0*7];
hess[1 + 3*size] += m*l*hess22[0 + 0*7];
hess[2 + 3*size] += m*l*hess22[1 + 0*7];
hess[3 + 3*size] += m*m*hess22[0 + 0*7];
hess[4 + 3*size] += m*m*hess22[1 + 0*7];
hess[5 + 3*size] += m*m*hess22[2 + 0*7];
hess[0 + 4*size] += m*k*hess22[0 + 1*7];
hess[1 + 4*size] += m*l*hess22[0 + 1*7];
hess[2 + 4*size] += m*l*hess22[1 + 1*7];
hess[3 + 4*size] += m*m*hess22[0 + 1*7];
hess[4 + 4*size] += m*m*hess22[1 + 1*7];
hess[5 + 4*size] += m*m*hess22[2 + 1*7];
hess[0 + 5*size] += m*k*hess22[0 + 2*7];
hess[1 + 5*size] += m*l*hess22[0 + 2*7];
hess[2 + 5*size] += m*l*hess22[1 + 2*7];
hess[3 + 5*size] += m*m*hess22[0 + 2*7];
hess[4 + 5*size] += m*m*hess22[1 + 2*7];
hess[5 + 5*size] += m*m*hess22[2 + 2*7];
if(isnan(grad1[0]))
{
for(int n=0 ; n<size ; n++)
{ grad[n] = NAN; }
return;
}
}
}
}
}
// numerical derivatives for debugging purposes
void total_energy_derivative_test(struct molecular_crystal *xtl, // description of the crystal being optimized
double *state, // the crystal's state vector [6+7*xtl->nmol]
double *grad, // 1st derivative of the total energy [6+7*xtl->nmol]
double *hess) // 2nd derivative of the total energy [(6+7*xtl->nmol)*(6+7*xtl->nmol)]
{
int num = 6+7*xtl->nmol;
double *grad_plus = (double*)malloc(sizeof(double)*num);
double *grad_minus = (double*)malloc(sizeof(double)*num);
double *dummy = (double*)malloc(sizeof(double)*num*num);
for(int i=0 ; i<num ; i++)
{
double state0 = state[i];
state[i] += STEP;
double energy_plus = total_energy(xtl,state);
total_energy_derivative(xtl,state,grad_plus,dummy);
state[i] -= 2.0*STEP;
double energy_minus = total_energy(xtl,state);
total_energy_derivative(xtl,state,grad_minus,dummy);
state[i] = state0;
grad[i] = (energy_plus - energy_minus)/(2.0*STEP);
for(int j=0 ; j<num ; j++)
{ hess[i+j*num] = (grad_plus[j] - grad_minus[j])/(2.0*STEP); }
}
free(grad_plus);
free(grad_minus);
free(dummy);
}
// renormalize quaternions
void renormalize(int nmol, // number of molecules in the state vector
double *state) // state vector [6+7*nmol]
{
for(int i=0 ; i<nmol ; i++)
{
double renorm = 1.0/sqrt(state[9+7*i]*state[9+7*i] + state[10+7*i]*state[10+7*i]
+ state[11+7*i]*state[11+7*i] + state[12+7*i]*state[12+7*i]);
for(int j=0 ; j<4 ; j++)
{ state[9+j+7*i] *= renorm; }
}
}
// objective function to optimize the volume of the molecular crystal
double volume_search(double x, // optimization variable [0,1]
struct molecular_crystal *xtl, // description of the crystal being optimized
double *state, // the crystal's state vector [6+7*xtl->nmol]
double *min, // smallest scale factor to be considered [1]
double *max, // largest scale factor to be considered [1]
double *dummy, // dummy variable to match argument list w/ other objective function
double *work) // work vector [6+7*xtl->nmol]
{
int size = 6+7*xtl->nmol;
double scale0 = (1.0-x)*min[0] + x*max[0];
for(int i=0 ; i<size ; i++)
{ work[i] = scale0*state[i]; }
return total_energy(xtl, work);
}
// objective function for the Tikhonov-regularized line search
double quad_search(double x, // optimization variable [0,1]
struct molecular_crystal *xtl, // description of the crystal being optimized
double *state, // the crystal's state vector in normal coordinates [6+7*xtl->nmol]
double *grad, // gradient in normal coordinates [6+7*xtl->nmol]
double *eval, // eigenvalues of the Hessian matrix [6+7*xtl->nmol]
double *evec, // eigenvectors of the Hessian matrix [(6+7*xtl->nmol)*(6+7*xtl->nmol)]
double *work) // work vector [13+14*xtl->nmol]
{
int size = 6+7*xtl->nmol;
x *= work[2*size]; // hack to tune the search interval
for(int i=0 ; i<size ; i++)
{
work[i] = state[i];
work[i+size] = -x*grad[i]/(fabs(x*eval[i]) + (1-x)*fabs(eval[size-1]));
}
char notrans = 'N';
int inc = 1;
double one = 1.0;
dgemv_(¬rans, &size, &size, &one, evec, &size, work+size, &inc, &one, work, &inc);
return total_energy(xtl, work);
}
// line optimizer for both objective functions (Golden-section search)
// code adapted from a Python implementation at https://en.wikipedia.org/wiki/Golden-section_search
double line_optimize(struct molecular_crystal *xtl, // description of the crystal being optimized
int nstep, // number of optimization steps
double *state, // the crystal's state vector in normal coordinates [6+7*xtl->nmol]
double (*fptr)(double, struct molecular_crystal*, double*, double*, double*, double*, double*),
double *vec1,
double *vec2,
double *vec3,
double *vec4)
{
double invphi = (sqrt(5.0) - 1.0)*0.5, invphi2 = (3.0 - sqrt(5.0))*0.5;
double a = 0.0, b = 1.0, c = invphi2, d = invphi, h = 1.0;
double yc = fptr(c, xtl, state, vec1, vec2, vec3, vec4);
double yd = fptr(d, xtl, state, vec1, vec2, vec3, vec4);
for(int i=0 ; i<nstep ; i++)
{
if(yc < yd)
{
b = d;
d = c;
yd = yc;
h *= invphi;
c = a + invphi2*h;
yc = fptr(c, xtl, state, vec1, vec2, vec3, vec4);
}
else
{
a = c;
c = d;
yc = yd;
h *= invphi;
d = a + invphi*h;
yd = fptr(d, xtl, state, vec1, vec2, vec3, vec4);
}
}
// crappy hack to load the minimizer into the workspace (extra computation of objective function)
double min = c, ymin = yc;
if(yd < yc) { min = d; ymin = yd; }
fptr(min, xtl, state, vec1, vec2, vec3, vec4);
// extract minimizer from the workspace (a hack) & renormalize quaternions
int size = 6+7*xtl->nmol;
for(int i=0 ; i<size ; i++)
{ state[i] = vec4[i]; }
renormalize(xtl->nmol, state);
return ymin;
}
// main loop of crystal optimization
void optimize(struct molecular_crystal *xtl, // description of the crystal being optimized
double *state, // the crystal's state vector to be updated [6+7*xtl->nmol]
int family) // crystal family (see key in rigid-press.h)
{
int size = 6+7*xtl->nmol;
double *workspace = (double*)malloc(sizeof(double)*size*2);
// construct a constraint matrix for high-symmetry lattice vectors
double constraint_mat[36];
for(int i=0 ; i<36 ; i++)
{ constraint_mat[i] = 0.0; }
switch(family)
{
case 1:
constraint_mat[0 + 0*6] = constraint_mat[2 + 2*6] = constraint_mat[3 + 3*6] = constraint_mat[5 + 5*6] = 1.0;
break;
case 2:
constraint_mat[0 + 0*6] = constraint_mat[2 + 2*6] = constraint_mat[5 + 5*6] = 1.0;
break;
case 3:
constraint_mat[5 + 5*6] = 1.0;
constraint_mat[0 + 0*6] = constraint_mat[2 + 0*6] = constraint_mat[0 + 2*6] = constraint_mat[2 + 2*6] = 0.5;
break;
case 4:
constraint_mat[5 + 5*6] = 1.0;
constraint_mat[0 + 0*6] = 0.5;
constraint_mat[1 + 1*6] = 0.125;
constraint_mat[2 + 2*6] = 0.375;
constraint_mat[0 + 1*6] = constraint_mat[1 + 0*6] = -0.25;
constraint_mat[0 + 2*6] = constraint_mat[2 + 0*6] = sqrt(3.0)*0.25;
constraint_mat[1 + 2*6] = constraint_mat[2 + 1*6] = -sqrt(3.0)*0.125;
break;
case 5:
constraint_mat[0 + 0*6] = constraint_mat[2 + 0*6] = constraint_mat[5 + 0*6] = 1.0/3.0;
constraint_mat[0 + 2*6] = constraint_mat[2 + 2*6] = constraint_mat[5 + 2*6] = 1.0/3.0;
constraint_mat[0 + 5*6] = constraint_mat[2 + 5*6] = constraint_mat[5 + 5*6] = 1.0/3.0;
break;
}
// find an overpacked volume
double scale_min = 1.0, scale_max = 1.0;
double energy = total_energy(xtl, state), energy_min = energy, energy_max = energy;
while(energy_min != INFINITY)
{
scale_min *= 0.5;
energy_min = volume_search(0.0, xtl, state, &scale_min, &scale_max, NULL, workspace);
}
// find an underpacked volume
while(energy_max == INFINITY)
{
scale_max *= 2.0;
energy_max = volume_search(1.0, xtl, state, &scale_min, &scale_max, NULL, workspace);
if(energy_max == INFINITY)
{ scale_min = scale_max; }
}
do
{
energy = energy_max;
scale_max *= 2.0;
energy_max = volume_search(1.0, xtl, state, &scale_min, &scale_max, NULL, workspace);
} while(energy > energy_max);
// preliminary volume optimization
energy = line_optimize(xtl, GOLDEN_STEPS, state, volume_search, &scale_min, &scale_max, NULL, workspace);
// main optimization loop
int iter = 0, lwork = -1, info, inc = 1, progress = 1, six = 6;
char jobz = 'V', uplo = 'U', notrans = 'N', trans = 'T';
double work0, one = 1.0, zero = 0.0;
double *grad = (double*)malloc(sizeof(double)*size);
double *hess = (double*)malloc(sizeof(double)*size*size);
double *ev = (double*)malloc(sizeof(double)*size);
dsyev_(&jobz, &uplo, &size, NULL, &size, NULL, &work0, &lwork, &info);
lwork = (int)work0;
if(lwork < 6*size)
{ lwork = 6*size; }
double *work = (double*)malloc(sizeof(double)*lwork);
double new_energy = energy;
do
{
// save previous energy
energy = new_energy;
// expand the total energy to 2nd order
total_energy_derivative(xtl, state, grad, hess);
// apply constraints to gradient & Hessian
if(family != 0)
{
dgemv_(¬rans, &six, &six, &one, constraint_mat, &six, grad, &inc, &zero, work, &inc);
for(int i=0 ; i<6 ; i++)
{ grad[i] = work[i]; }
dgemm_(¬rans, ¬rans, &six, &size, &six, &one, constraint_mat, &six, hess, &size, &zero, work, &six);
for(int i=0 ; i<6 ; i++)
for(int j=0 ; j<size ; j++)
{ hess[i+j*size] = work[i+j*6]; }
dgemm_(¬rans, ¬rans, &size, &six, &six, &one, hess, &size, constraint_mat, &six, &zero, work, &size);
for(int i=0 ; i<size ; i++)
for(int j=0 ; j<6 ; j++)
{ hess[i+j*size] = work[i+j*size]; }
}
// transform into normal modes of the quadratic approximant
dsyev_(&jobz, &uplo, &size, hess, &size, ev, work, &lwork, &info);
for(int i=0 ; i<size ; i++)
{ work[i] = grad[i]; }
dgemv_(&trans, &size, &size, &one, hess, &size, work, &inc, &zero, grad, &inc);
// identify a reasonable search interval