-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.cpp
1396 lines (1262 loc) · 53.5 KB
/
core.cpp
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
/*
Copyright (c) 2013,2014,2017,2018,2019,2020,2021 Tobias Brink
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
//#include <iostream>
//#include <cstdio>
#include <utility>
#include <cmath>
#include <iostream>
//#include <chrono>
#include <nlopt.hpp>
#include "core.hpp"
using namespace std;
using namespace mytest;
const double DELTA = 1e-10; // For double comparison.
// Box /////////////////////////////////////////////////////////////////
Box::Box(const Vec3D<double>& a_in,
const Vec3D<double>& b_in,
const Vec3D<double>& c_in,
bool periodic_a, bool periodic_b, bool periodic_c,
unique_ptr< Array2D<double> > coordinates,
unique_ptr< Array1DInit<string> > types_in,
const string& name)
: box_side_lengths(box_side_lengths_), // Public const references.
a(a_), b(b_), c(c_),
periodic(periodic_),
natoms(natoms_), nghosts(nghosts_), nall(nall_),
positions(move(*coordinates)), // Public data.
types(move(*types_in)),
box_side_lengths_(a_in.abs(), b_in.abs(), c_in.abs()), // Private data.
a_(a_in), b_(b_in), c_(c_in),
periodic_(periodic_a, periodic_b, periodic_c),
natoms_(coordinates->extent(0)), nghosts_(0), nall_(natoms_),
name_(name),
neigh_list_(natoms_),
contributing_(natoms_, 1) // init to ones for all "real" atoms
{
if (positions.extent(0) != types.extent(0))
throw runtime_error("types must have the same length as the "
"first dimension of positions.");
if (positions.extent(1) != 3)
throw runtime_error("positions must be a n*3 array.");
}
Box::Box(const std::string& lattice, double lattice_const, bool cubic,
unsigned repeat_a, unsigned repeat_b, unsigned repeat_c,
bool periodic_a, bool periodic_b, bool periodic_c,
const vector<string>& types_in,
const std::string& name)
: box_side_lengths(box_side_lengths_), // Public const references.
a(a_), b(b_), c(c_),
periodic(periodic_),
natoms(natoms_), nghosts(nghosts_), nall(nall_),
positions(atoms_per_unit_cell(lattice, cubic) // Public data.
* repeat_a * repeat_b * repeat_c, 3),
types(positions.extent(0)),
periodic_(periodic_a, periodic_b, periodic_c), // Private data.
natoms_(positions.extent(0)), nghosts_(0), nall_(natoms_),
name_(name),
contributing_(natoms_, 1) // init to ones for all "real" atoms
{
if (types_in.size() != species_per_unit_cell(lattice))
throw runtime_error("Wrong number of types for this lattice.");
// The unit cell vectors.
Vec3D<double> unit_a;
Vec3D<double> unit_b;
Vec3D<double> unit_c;
typedef pair<Vec3D<double>,string> t_atom;
vector<t_atom> atoms; // (position, type)
if (lattice == "dimer") {
// Arbitrary big box size.
unit_a = Vec3D<double>(100*lattice_const, 0.0, 0.0);
unit_b = Vec3D<double>(0.0, 100*lattice_const, 0.0);
unit_c = Vec3D<double>(0.0, 0.0, 100*lattice_const);
atoms.push_back(t_atom(Vec3D<double>(49.5*lattice_const,
50.0*lattice_const,
50.0*lattice_const),
types_in[0]));
atoms.push_back(t_atom(Vec3D<double>(50.5*lattice_const,
50.0*lattice_const,
50.0*lattice_const),
types_in[1]));
} else if (lattice == "sc") {
unit_a = Vec3D<double>(lattice_const, 0.0, 0.0);
unit_b = Vec3D<double>(0.0, lattice_const, 0.0);
unit_c = Vec3D<double>(0.0, 0.0, lattice_const);
atoms.push_back(t_atom(Vec3D<double>(0.0, 0.0, 0.0), types_in[0]));
} else if (lattice == "bcc") {
if (cubic) {
unit_a = Vec3D<double>(lattice_const, 0.0, 0.0);
unit_b = Vec3D<double>(0.0, lattice_const, 0.0);
unit_c = Vec3D<double>(0.0, 0.0, lattice_const);
atoms.push_back(t_atom(Vec3D<double>(0.0, 0.0, 0.0),
types_in[0]));
atoms.push_back(t_atom(Vec3D<double>(0.5, 0.5, 0.5) * lattice_const,
types_in[0]));
} else {
unit_a = Vec3D<double>(-0.5, 0.5, 0.5) * lattice_const;
unit_b = Vec3D<double>(0.5, -0.5, 0.5) * lattice_const;
unit_c = Vec3D<double>(0.5, 0.5, -0.5) * lattice_const;
atoms.push_back(t_atom(Vec3D<double>(0.0, 0.0, 0.0), types_in[0]));
}
} else if (lattice == "fcc") {
if (cubic) {
unit_a = Vec3D<double>(lattice_const, 0.0, 0.0);
unit_b = Vec3D<double>(0.0, lattice_const, 0.0);
unit_c = Vec3D<double>(0.0, 0.0, lattice_const);
atoms.push_back(t_atom(Vec3D<double>(0.0, 0.0, 0.0),
types_in[0]));
atoms.push_back(t_atom(Vec3D<double>(0.0, 0.5, 0.5) * lattice_const,
types_in[0]));
atoms.push_back(t_atom(Vec3D<double>(0.5, 0.0, 0.5) * lattice_const,
types_in[0]));
atoms.push_back(t_atom(Vec3D<double>(0.5, 0.5, 0.0) * lattice_const,
types_in[0]));
} else {
unit_a = Vec3D<double>(0.0, 0.5, 0.5) * lattice_const;
unit_b = Vec3D<double>(0.5, 0.0, 0.5) * lattice_const;
unit_c = Vec3D<double>(0.5, 0.5, 0.0) * lattice_const;
atoms.push_back(t_atom(Vec3D<double>(0.0, 0.0, 0.0), types_in[0]));
}
} else if (lattice == "diamond") {
if (cubic) {
unit_a = Vec3D<double>(lattice_const, 0.0, 0.0);
unit_b = Vec3D<double>(0.0, lattice_const, 0.0);
unit_c = Vec3D<double>(0.0, 0.0, lattice_const);
atoms.push_back(t_atom(Vec3D<double>(0.00, 0.00, 0.00) * lattice_const,
types_in[0]));
atoms.push_back(t_atom(Vec3D<double>(0.25, 0.25, 0.25) * lattice_const,
types_in[0]));
atoms.push_back(t_atom(Vec3D<double>(0.00, 0.50, 0.50) * lattice_const,
types_in[0]));
atoms.push_back(t_atom(Vec3D<double>(0.25, 0.75, 0.75) * lattice_const,
types_in[0]));
atoms.push_back(t_atom(Vec3D<double>(0.50, 0.00, 0.50) * lattice_const,
types_in[0]));
atoms.push_back(t_atom(Vec3D<double>(0.75, 0.25, 0.75) * lattice_const,
types_in[0]));
atoms.push_back(t_atom(Vec3D<double>(0.50, 0.50, 0.00) * lattice_const,
types_in[0]));
atoms.push_back(t_atom(Vec3D<double>(0.75, 0.75, 0.25) * lattice_const,
types_in[0]));
} else {
unit_a = Vec3D<double>(0.0, 0.5, 0.5) * lattice_const;
unit_b = Vec3D<double>(0.5, 0.0, 0.5) * lattice_const;
unit_c = Vec3D<double>(0.5, 0.5, 0.0) * lattice_const;
atoms.push_back(t_atom(Vec3D<double>(0.00, 0.00, 0.00) * lattice_const,
types_in[0]));
atoms.push_back(t_atom(Vec3D<double>(0.25, 0.25, 0.25) * lattice_const,
types_in[0]));
}
} else if (lattice == "B1") { // NaCl
if (cubic) {
unit_a = Vec3D<double>(lattice_const, 0.0, 0.0);
unit_b = Vec3D<double>(0.0, lattice_const, 0.0);
unit_c = Vec3D<double>(0.0, 0.0, lattice_const);
atoms.push_back(t_atom(Vec3D<double>(0.0, 0.0, 0.0) * lattice_const,
types_in[0]));
atoms.push_back(t_atom(Vec3D<double>(0.5, 0.0, 0.0) * lattice_const,
types_in[1]));
atoms.push_back(t_atom(Vec3D<double>(0.0, 0.5, 0.5) * lattice_const,
types_in[0]));
atoms.push_back(t_atom(Vec3D<double>(0.5, 0.5, 0.5) * lattice_const,
types_in[1]));
atoms.push_back(t_atom(Vec3D<double>(0.5, 0.0, 0.5) * lattice_const,
types_in[0]));
atoms.push_back(t_atom(Vec3D<double>(0.0, 0.0, 0.5) * lattice_const,
types_in[1]));
atoms.push_back(t_atom(Vec3D<double>(0.5, 0.5, 0.0) * lattice_const,
types_in[0]));
atoms.push_back(t_atom(Vec3D<double>(0.0, 0.5, 0.0) * lattice_const,
types_in[1]));
} else {
unit_a = Vec3D<double>(0.0, 0.5, 0.5) * lattice_const;
unit_b = Vec3D<double>(0.5, 0.0, 0.5) * lattice_const;
unit_c = Vec3D<double>(0.5, 0.5, 0.0) * lattice_const;
atoms.push_back(t_atom(Vec3D<double>(0.0, 0.0, 0.0) * lattice_const,
types_in[0]));
atoms.push_back(t_atom(Vec3D<double>(0.5, 0.0, 0.0) * lattice_const,
types_in[1]));
}
} else if (lattice == "B2") { // CsCl
unit_a = Vec3D<double>(lattice_const, 0.0, 0.0);
unit_b = Vec3D<double>(0.0, lattice_const, 0.0);
unit_c = Vec3D<double>(0.0, 0.0, lattice_const);
atoms.push_back(t_atom(Vec3D<double>(0.0, 0.0, 0.0) * lattice_const,
types_in[0]));
atoms.push_back(t_atom(Vec3D<double>(0.5, 0.5, 0.5) * lattice_const,
types_in[1]));
} else if (lattice == "B3") { // zincblende
if (cubic) {
unit_a = Vec3D<double>(lattice_const, 0.0, 0.0);
unit_b = Vec3D<double>(0.0, lattice_const, 0.0);
unit_c = Vec3D<double>(0.0, 0.0, lattice_const);
atoms.push_back(t_atom(Vec3D<double>(0.00, 0.00, 0.00) * lattice_const,
types_in[0]));
atoms.push_back(t_atom(Vec3D<double>(0.25, 0.25, 0.25) * lattice_const,
types_in[1]));
atoms.push_back(t_atom(Vec3D<double>(0.00, 0.50, 0.50) * lattice_const,
types_in[0]));
atoms.push_back(t_atom(Vec3D<double>(0.25, 0.75, 0.75) * lattice_const,
types_in[1]));
atoms.push_back(t_atom(Vec3D<double>(0.50, 0.00, 0.50) * lattice_const,
types_in[0]));
atoms.push_back(t_atom(Vec3D<double>(0.75, 0.25, 0.75) * lattice_const,
types_in[1]));
atoms.push_back(t_atom(Vec3D<double>(0.50, 0.50, 0.00) * lattice_const,
types_in[0]));
atoms.push_back(t_atom(Vec3D<double>(0.75, 0.75, 0.25) * lattice_const,
types_in[1]));
} else {
unit_a = Vec3D<double>(0.0, 0.5, 0.5) * lattice_const;
unit_b = Vec3D<double>(0.5, 0.0, 0.5) * lattice_const;
unit_c = Vec3D<double>(0.5, 0.5, 0.0) * lattice_const;
atoms.push_back(t_atom(Vec3D<double>(0.00, 0.00, 0.00) * lattice_const,
types_in[0]));
atoms.push_back(t_atom(Vec3D<double>(0.25, 0.25, 0.25) * lattice_const,
types_in[1]));
}
} else {
throw runtime_error("unknown lattice");
}
// Create box.
a_ = double(repeat_a) * unit_a; box_side_lengths_[0] = a_.abs();
b_ = double(repeat_b) * unit_b; box_side_lengths_[1] = b_.abs();
c_ = double(repeat_c) * unit_c; box_side_lengths_[2] = c_.abs();
// Fill atoms.
unsigned i = 0;
for (unsigned aa = 0; aa != repeat_a; ++aa)
for (unsigned bb = 0; bb != repeat_b; ++bb)
for (unsigned cc = 0; cc != repeat_c; ++cc) {
// Get offset.
const Vec3D<double> offset =
double(aa)*unit_a + double(bb)*unit_b + double(cc)*unit_c;
// Assign atoms.
for (const t_atom& atom : atoms) {
for (unsigned j = 0; j != 3; ++j)
positions(i, j) = atom.first[j] + offset[j];
types(i) = atom.second;
++i;
}
}
if (i != natoms_)
throw runtime_error("BUG in the implementation, not all atoms are filled.");
// Reserve neighbor list space.
neigh_list_.resize(natoms_);
}
Box::Box(const Box& other, const std::string& new_name)
: box_side_lengths(box_side_lengths_), // Public const references.
a(a_), b(b_), c(c_),
periodic(periodic_),
natoms(natoms_), nghosts(nghosts_), nall(nall_),
positions(other.positions.extent(0), // Public data.
other.positions.extent(1)),
types(other.types.extent(0)),
box_side_lengths_(other.box_side_lengths_), // Private data.
a_(other.a_), b_(other.b_), c_(other.c_),
periodic_(other.periodic_),
natoms_(other.natoms_), nghosts_(other.nghosts_), nall_(other.nall_),
name_(new_name),
ghost_shells(other.ghost_shells),
/*
ghost_positions(make_unique<Array2D<double>>(
other.ghost_positions->extent(0),
other.ghost_positions->extent(1))),
ghost_types(make_unique<Array1D<int>>(other.ghost_types->extent(0))),
*/
neigh_list_(other.neigh_list_),
contributing_(other.contributing_)
{
for (int i = 0; i != positions.extent(0); ++i)
for (int j = 0; j != positions.extent(1); ++j)
positions(i,j) = other.positions(i,j);
for (int i = 0; i != types.extent(0); ++i)
types(i) = other.types(i);
if (other.ghost_positions && other.ghost_types) {
ghost_positions = make_unique<Array2D<double>>(other.ghost_positions->extent(0),
other.ghost_positions->extent(1));
ghost_types = make_unique<Array1D<int>>(other.ghost_types->extent(0));
for (int i = 0; i != ghost_positions->extent(0); ++i)
for (int j = 0; j != ghost_positions->extent(1); ++j)
(*ghost_positions)(i,j) = (*other.ghost_positions)(i,j);
for (int i = 0; i != ghost_types->extent(0); ++i)
(*ghost_types)(i) = (*other.ghost_types)(i);
}
}
Box::Box(const Box& other)
: box_side_lengths(box_side_lengths_), // Public const references.
a(a_), b(b_), c(c_),
periodic(periodic_),
natoms(natoms_), nghosts(nghosts_), nall(nall_),
positions(other.positions.extent(0), // Public data.
other.positions.extent(1)),
types(other.types.extent(0)),
box_side_lengths_(other.box_side_lengths_), // Private data.
a_(other.a_), b_(other.b_), c_(other.c_),
periodic_(other.periodic_),
natoms_(other.natoms_), nghosts_(other.nghosts_), nall_(other.nall_),
name_(other.name_),
ghost_shells(other.ghost_shells),
/*
ghost_positions(make_unique<Array2D<double>>(
other.ghost_positions->extent(0),
other.ghost_positions->extent(1))),
ghost_types(make_unique<Array1D<int>>(other.ghost_types->extent(0))),
*/
neigh_list_(other.neigh_list_),
contributing_(other.contributing_)
{
for (int i = 0; i != positions.extent(0); ++i)
for (int j = 0; j != positions.extent(1); ++j)
positions(i,j) = other.positions(i,j);
for (int i = 0; i != types.extent(0); ++i)
types(i) = other.types(i);
if (other.ghost_positions && other.ghost_types) {
ghost_positions = make_unique<Array2D<double>>(other.ghost_positions->extent(0),
other.ghost_positions->extent(1));
ghost_types = make_unique<Array1D<int>>(other.ghost_types->extent(0));
for (int i = 0; i != ghost_positions->extent(0); ++i)
for (int j = 0; j != ghost_positions->extent(1); ++j)
(*ghost_positions)(i,j) = (*other.ghost_positions)(i,j);
for (int i = 0; i != ghost_types->extent(0); ++i)
(*ghost_types)(i) = (*other.ghost_types)(i);
}
}
bool Box::update_neighbor_list(double cutoff, double skin,
const map<string,int>& typemap) {
const double cut = (1 + skin) * cutoff;
ghost_shells = calc_number_of_ghost_shells(cut);
unsigned new_nghosts;
// We store any ghost atoms that we may need.
new_nghosts = natoms_ * ((2*ghost_shells[0]+1)
* (2*ghost_shells[1]+1)
* (2*ghost_shells[2]+1) - 1);
// If needed, resize ghost arrays. If the arrays are not yet
// allocated, allocate them (ghost_position == nullptr).
bool reallocated = false;
if (new_nghosts != nghosts_ || !ghost_positions) {
// Number of ghosts changed, re-allocate memory.
nghosts_ = new_nghosts;
nall_ = natoms_ + nghosts_;
ghost_positions = make_unique< Array2D<double> >(nall_, 3);
ghost_types = make_unique< Array1D<int> >(nall_);
contributing_.resize(nall_, 0); // Everything that the vector is
// resized to consists only of
// ghost atoms, thus init to zero.
reallocated = true;
}
// Update ghosts.
update_ghosts(typemap);
// Clear neighbor lists.
for (auto& l : neigh_list_)
l.clear();
// Fill neighbor lists again.
const double cutsq = cut * cut;
for (unsigned i = 0; i != natoms_; ++i) {
// Go over neighbors in the central cell.
for (unsigned j = i+1; j != natoms_; ++j) {
const double dx = (*ghost_positions)(j,0) - (*ghost_positions)(i,0);
const double dy = (*ghost_positions)(j,1) - (*ghost_positions)(i,1);
const double dz = (*ghost_positions)(j,2) - (*ghost_positions)(i,2);
if (dx*dx + dy*dy + dz*dz < cutsq) {
neigh_list_[i].push_back(j);
neigh_list_[j].push_back(i);
}
}
// Iterate over ghosts.
for (unsigned j = natoms_; j != nall_; ++j) {
const double dx = (*ghost_positions)(j,0) - (*ghost_positions)(i,0);
const double dy = (*ghost_positions)(j,1) - (*ghost_positions)(i,1);
const double dz = (*ghost_positions)(j,2) - (*ghost_positions)(i,2);
if (dx*dx + dy*dy + dz*dz < cutsq) {
neigh_list_[i].push_back(j);
}
}
}
return reallocated;
}
void Box::update_ghosts(const map<string,int>& typemap) {
// Copy original atoms, do not wrap for PBCs.
for (unsigned i = 0; i != natoms_; ++i) {
(*ghost_positions)(i, 0) = positions(i, 0);
(*ghost_positions)(i, 1) = positions(i, 1);
(*ghost_positions)(i, 2) = positions(i, 2);
(*ghost_types)(i) = typemap.at(types(i));
}
unsigned ii = natoms_;
const int alo = -static_cast<int>(ghost_shells[0]);
const int ahi = ghost_shells[0];
const int blo = -static_cast<int>(ghost_shells[1]);
const int bhi = ghost_shells[1];
const int clo = -static_cast<int>(ghost_shells[2]);
const int chi = ghost_shells[2];
for (int aa = alo; aa <= ahi; ++aa)
for (int bb = blo; bb <= bhi; ++bb)
for (int cc = clo; cc <= chi; ++cc) {
if (aa == 0 && bb == 0 && cc == 0) continue;
const Vec3D<double> offset =
double(aa)*a + double(bb)*b + double(cc)*c;
for (unsigned i = 0; i != natoms_; ++i) {
(*ghost_positions)(ii, 0) = (*ghost_positions)(i, 0) + offset[0];
(*ghost_positions)(ii, 1) = (*ghost_positions)(i, 1) + offset[1];
(*ghost_positions)(ii, 2) = (*ghost_positions)(i, 2) + offset[2];
(*ghost_types)(ii) = typemap.at(types(i));
++ii;
}
}
}
unique_ptr<Box> Box::delete_atom(unsigned i, const string& name) const {
if (i >= natoms_)
throw runtime_error("Deleting non-existant atom.");
unsigned new_natoms = natoms_ - 1;
unique_ptr< Array2D<double> > new_positions =
make_unique< Array2D<double> >(new_natoms, 3);
unique_ptr< Array1DInit<string> > new_types =
make_unique< Array1DInit<string> >(new_natoms);
// Copy all except atom i.
for (unsigned j = 0; j < i; ++j) {
for (unsigned dim = 0; dim < 3; ++dim)
(*new_positions)(j, dim) = positions(j, dim);
(*new_types)(j) = types(j);
}
for (unsigned j = i+1; j < natoms_; ++j) {
for (unsigned dim = 0; dim < 3; ++dim)
(*new_positions)(j-1, dim) = positions(j, dim);
(*new_types)(j-1) = types(j);
}
return make_unique<Box>(a, b, c, periodic[0], periodic[1], periodic[2],
move(new_positions), move(new_types),
name);
}
unique_ptr<Box> Box::repeat(unsigned repeat_a,
unsigned repeat_b,
unsigned repeat_c,
const std::string& name) const {
unsigned new_natoms = natoms_ * repeat_a * repeat_b * repeat_c;
unique_ptr< Array2D<double> > new_positions =
make_unique< Array2D<double> >(new_natoms, 3);
unique_ptr< Array1DInit<string> > new_types =
make_unique< Array1DInit<string> >(new_natoms);
unsigned new_idx = 0;
for (unsigned i = 0; i < repeat_a; ++i) {
for (unsigned j = 0; j < repeat_b; ++j) {
for (unsigned k = 0; k < repeat_c; ++k) {
Vec3D<double> offset(0.0, 0.0, 0.0);
offset += double(i) * a;
offset += double(j) * b;
offset += double(k) * c;
for (unsigned idx = 0; idx < natoms_; ++idx) {
for (unsigned dim = 0; dim < 3; ++dim) {
(*new_positions)(new_idx, dim) = positions(idx, dim) + offset[dim];
}
(*new_types)(new_idx) = types(idx);
++new_idx;
}
}
}
}
return make_unique<Box>(a*double(repeat_a),
b*double(repeat_b),
c*double(repeat_c),
periodic[0], periodic[1], periodic[2],
move(new_positions), move(new_types),
name);
}
double Box::calc_dist(int i, int j, double& dx, double& dy, double& dz) const {
if (!ghost_positions)
throw runtime_error("Ghosts not initialized!");
dx = (*ghost_positions)(j,0) - (*ghost_positions)(i,0);
dy = (*ghost_positions)(j,1) - (*ghost_positions)(i,1);
dz = (*ghost_positions)(j,2) - (*ghost_positions)(i,2);
return sqrt(dx*dx + dy*dy + dz*dz);
}
void Box::write_to(ostream& output) const {
// Number of atoms.
output << natoms_ << "\n";
// Extended XYZ uses the comment line for metadata like the box shape.
output << "Lattice=\""
<< a[0] << " " << a[1] << " " << a[2] << " "
<< b[0] << " " << b[1] << " " << b[2] << " "
<< c[0] << " " << c[1] << " " << c[2] << "\" "
<< "Properties=species:S:1:pos:R:3\n";
// The atoms.
for (unsigned i = 0; i != natoms_; ++i)
output << types(i) << " "
<< positions(i,0) << " "
<< positions(i,1) << " "
<< positions(i,2) << "\n";
output << flush;
}
void Box::scale(double factor_a, double factor_b, double factor_c,
const map<string,int>& typemap) {
// Get transformation matrix.
Array2D<double> T(3,3); T = 0.0;
const double vol = calc_volume();
const Vec3D<double> bxc = cross(b_, c_);
const Vec3D<double> cxa = cross(c_, a_);
const Vec3D<double> axb = cross(a_, b_);
for (unsigned i = 0; i != 3; ++i)
for (unsigned j = 0; j != 3; ++j)
T(i,j) = ( a_[i]*factor_a*bxc[j] + b_[i]*factor_b*cxa[j] + c_[i]*factor_c*axb[j] ) / vol;
// Update box.
a_ *= factor_a;
b_ *= factor_b;
c_ *= factor_c;
// Update positions
for (unsigned atom = 0; atom != natoms_; ++atom) {
Vec3D<double> pos(0.0, 0.0, 0.0);
for (unsigned i = 0; i != 3; ++i)
for (unsigned j = 0; j != 3; ++j) {
pos[i] += T(i,j)*positions(atom,j);
}
// Assign back.
for (unsigned dim = 0; dim != 3; ++dim)
positions(atom,dim) = pos[dim];
}
// Update ghosts etc.
update_ghosts(typemap);
}
void Box::deform(Voigt6<double> defmatrix,
const map<string,int>& typemap) {
// Throw away very small shear.
if (abs(defmatrix(3)) < DELTA)
defmatrix(3) = 0.0;
if (abs(defmatrix(4)) < DELTA)
defmatrix(4) = 0.0;
if (abs(defmatrix(5)) < DELTA)
defmatrix(5) = 0.0;
// Update box.
Vec3D<double> new_a(0.0, 0.0, 0.0);
Vec3D<double> new_b(0.0, 0.0, 0.0);
Vec3D<double> new_c(0.0, 0.0, 0.0);
for (unsigned i = 0; i != 3; ++i)
for (unsigned j = 0; j != 3; ++j) {
new_a[i] += defmatrix(i,j)*a_[j];
new_b[i] += defmatrix(i,j)*b_[j];
new_c[i] += defmatrix(i,j)*c_[j];
}
a_ = new_a; b_ = new_b; c_ = new_c;
box_side_lengths_[0] = a_.abs();
box_side_lengths_[1] = b_.abs();
box_side_lengths_[2] = c_.abs();
// Update positions.
for (unsigned atom = 0; atom != natoms_; ++atom) {
Vec3D<double> pos(0.0, 0.0, 0.0);
for (unsigned i = 0; i != 3; ++i)
for (unsigned j = 0; j != 3; ++j) {
pos[i] += defmatrix(i,j)*positions(atom,j);
}
// Assign back.
for (unsigned dim = 0; dim != 3; ++dim)
positions(atom,dim) = pos[dim];
}
// Update ghosts etc.
update_ghosts(typemap);
}
unsigned Box::atoms_per_unit_cell(const string& lattice, bool cubic) {
if (lattice == "dimer")
return 2;
if (lattice == "sc")
return 1;
if (lattice == "bcc")
return cubic ? 2 : 1;
if (lattice == "fcc")
return cubic ? 4 : 1;
if (lattice == "diamond")
return cubic ? 8 : 2;
if (lattice == "B1")
return cubic ? 8 : 2;
if (lattice == "B2")
return 2;
if (lattice == "B3")
return cubic ? 8 : 2;
throw runtime_error("unknown lattice");
}
unsigned Box::species_per_unit_cell(const string& lattice) {
if (lattice == "sc" || lattice == "bcc" || lattice == "fcc"
|| lattice == "diamond")
return 1;
else if (lattice == "dimer" || lattice == "B1" || lattice == "B2"
|| lattice == "B3")
return 2;
else
throw runtime_error("unknown lattice");
}
Vec3D<unsigned> Box::calc_number_of_ghost_shells(double cutoff) const {
const Vec3D<double> bc = cross(b_, c_);
const Vec3D<double> ac = cross(a_, c_);
const Vec3D<double> ab = cross(a_, b_);
const double V = abs(dot(a_, bc));
const double da = V / bc.abs();
const double db = V / ac.abs();
const double dc = V / ab.abs();
return Vec3D<unsigned>(periodic_[0] ? ceil(cutoff/da) : 0,
periodic_[1] ? ceil(cutoff/db) : 0,
periodic_[2] ? ceil(cutoff/dc) : 0);
}
// Compute /////////////////////////////////////////////////////////////
Compute::Compute(unique_ptr<Box> box, const string& modelname,
KIM::LengthUnit length_unit,
KIM::EnergyUnit energy_unit,
KIM::ChargeUnit charge_unit,
KIM::TemperatureUnit temperature_unit,
KIM::TimeUnit time_unit)
: box_(move(box)), modelname_(modelname)
{
int error;
// Create KIM model. /////////////////////////////////////////////////
int requested_units_accepted;
error = KIM::Model::Create(KIM::NUMBERING::zeroBased, // TODO: test this??
length_unit,
energy_unit,
charge_unit,
temperature_unit,
time_unit,
modelname,
&requested_units_accepted,
&model);
if (error)
throw runtime_error("KIM model creation failed.");
if (!requested_units_accepted)
throw runtime_error("KIM model does not accept my units.");
// Check routines and register support. //////////////////////////////
int n_model_routines;
KIM::MODEL_ROUTINE_NAME::GetNumberOfModelRoutineNames(&n_model_routines);
for (int i = 0; i < n_model_routines; ++i) {
KIM::ModelRoutineName model_routine_name;
KIM::MODEL_ROUTINE_NAME::GetModelRoutineName(i, &model_routine_name);
int present, required;
error = model->IsRoutinePresent(model_routine_name, &present, &required);
if (error)
throw runtime_error("KIM model->IsRoutinePresent() failed for "
+ model_routine_name.ToString() + ".");
if (model_routine_name
== KIM::MODEL_ROUTINE_NAME::Create
||
model_routine_name
== KIM::MODEL_ROUTINE_NAME::ComputeArgumentsCreate
||
model_routine_name
== KIM::MODEL_ROUTINE_NAME::Compute
||
model_routine_name
== KIM::MODEL_ROUTINE_NAME::ComputeArgumentsDestroy
||
model_routine_name
== KIM::MODEL_ROUTINE_NAME::Destroy) {
// Those are required by the API, so let's just check they are there.
if (!present)
throw runtime_error("The required routine \""
+ model_routine_name.ToString()
+ "\" is missing.");
} else if (model_routine_name
== KIM::MODEL_ROUTINE_NAME::Extension) {
// Not supported by me.
if (present && required)
throw runtime_error("The model requires an extension. "
"This is not supported.");
} else if (model_routine_name
== KIM::MODEL_ROUTINE_NAME::Refresh) {
has_reinit = present;
} else if (model_routine_name
== KIM::MODEL_ROUTINE_NAME::WriteParameterizedModel) {
has_write_params = present;
} else {
// Unknown.
if (present && required)
throw runtime_error("The unknown routine \""
+ model_routine_name.ToString()
+ "\" is required by the model.");
}
}
// TODO: We could get back the units and check, but do we need to?
// Check species. ////////////////////////////////////////////////////
for (int i = 0; i < box_->types.extent(0); ++i) {
int species_supported;
int species_code;
// Get code from model.
const KIM::SpeciesName sn(box_->types(i));
error = model->GetSpeciesSupportAndCode(sn, &species_supported,
&species_code);
if (error || !species_supported)
throw runtime_error("Species \""
+ box_->types(i)
+ "\" from box is not supported by the model.");
partcl_type_codes[box_->types(i)] = species_code;
partcl_type_names[species_code] = box_->types(i);
}
// Init KIM compute arguments. ///////////////////////////////////////
error = model->ComputeArgumentsCreate(&compute_arguments);
if (error)
throw runtime_error("KIM model->ComputeArgumentsCreate() failed.");
// Check and set the compute arguments.
int n_comp_args;
KIM::COMPUTE_ARGUMENT_NAME::GetNumberOfComputeArgumentNames(&n_comp_args);
for (int i = 0; i < n_comp_args; ++i) {
KIM::ComputeArgumentName compute_argument_name;
KIM::COMPUTE_ARGUMENT_NAME::GetComputeArgumentName(i,
&compute_argument_name);
// TODO: get its type and support/required status and check if we are compatible
// Set the arguments.
if (compute_argument_name
== KIM::COMPUTE_ARGUMENT_NAME::numberOfParticles) {
error = compute_arguments->SetArgumentPointer(compute_argument_name,
&nall_kim);
} else if (compute_argument_name
== KIM::COMPUTE_ARGUMENT_NAME::particleSpeciesCodes) {
// The length of this array is only determined after neighbor
// lists have been calculated and the argument will thus be set
// by update_neighbor_list().
} else if (compute_argument_name
== KIM::COMPUTE_ARGUMENT_NAME::particleContributing) {
// The length of this array is only determined after neighbor
// lists have been calculated and the argument will thus be set
// by update_neighbor_list().
} else if (compute_argument_name
== KIM::COMPUTE_ARGUMENT_NAME::coordinates) {
// The length of this array is only determined after neighbor
// lists have been calculated and the argument will thus be set
// by update_neighbor_list().
} else if (compute_argument_name
== KIM::COMPUTE_ARGUMENT_NAME::partialEnergy) {
error = compute_arguments->SetArgumentPointer(compute_argument_name,
&energy);
has_energy = true; // TODO: get from API
} else if (compute_argument_name
== KIM::COMPUTE_ARGUMENT_NAME::partialParticleEnergy) {
// The length of this array is only determined after neighbor
// lists have been calculated and the argument will thus be set
// by update_neighbor_list().
has_particleEnergy = true; // TODO: get from API
} else if (compute_argument_name
== KIM::COMPUTE_ARGUMENT_NAME::partialForces) {
// The length of this array is only determined after neighbor
// lists have been calculated and the argument will thus be set
// by update_neighbor_list().
has_forces = true; // TODO: get from API
} else if (compute_argument_name
== KIM::COMPUTE_ARGUMENT_NAME::partialVirial) {
error = compute_arguments->SetArgumentPointer(compute_argument_name,
&virial(0));
has_virial = true; // TODO: get from API
} else if (compute_argument_name
== KIM::COMPUTE_ARGUMENT_NAME::partialParticleVirial) {
// The length of this array is only determined after neighbor
// lists have been calculated and the argument will thus be set
// by update_neighbor_list().
has_particleVirial = true; // TODO: get from API
} else {
// TODO: complain if something else required
}
if (error)
throw runtime_error("KIM model->SetArgumentPointer() failed for "
+ compute_argument_name.ToString() + ".");
}
// Check and set the callbacks.
int n_comp_callb;
KIM::COMPUTE_CALLBACK_NAME::GetNumberOfComputeCallbackNames(&n_comp_callb);
for (int i = 0; i < n_comp_callb; ++i) {
KIM::ComputeCallbackName compute_callback_name;
KIM::COMPUTE_CALLBACK_NAME::GetComputeCallbackName(i,
&compute_callback_name);
// TODO: get its support/required status and check if we are compatible
if (compute_callback_name
== KIM::COMPUTE_CALLBACK_NAME::GetNeighborList) {
error = compute_arguments->SetCallbackPointer(compute_callback_name,
KIM::LANGUAGE_NAME::cpp,
(KIM::Function*) &get_neigh,
this);
} else if (compute_callback_name
== KIM::COMPUTE_CALLBACK_NAME::ProcessDEDrTerm) {
error =
compute_arguments->SetCallbackPointer(compute_callback_name,
KIM::LANGUAGE_NAME::cpp,
(KIM::Function*) &process_dEdr,
this);
has_process_dEdr = true;
} else {
//TODO complain if required
}
if (error)
throw runtime_error("KIM model->SetCallbackPointer() failed for "
+ compute_callback_name.ToString() + ".");
}
// Get cutoff(s) and create neighbor lists. //////////////////////////
model->GetInfluenceDistance(&cutoff);
//TODO: implement that shit, for now we just have one neighlist :-)
/*
model->GetNeighborListPointers(&NUMBER_OF_NEIGHBORLISTS,
&CUTOFFS,
&WILL_REQUEST_NEIGHBORS_OF_GHOSTS);
*/
// Now that we know the cutoff, we calculate neighbor lists and
// ghost atoms. Then we allocate memory for variable length data
// and pass it to KIM.
update_neighbor_list();
// Get parameters of the model and store 'em. ////////////////////////
if (has_reinit) {
int n_params;
model->GetNumberOfParameters(&n_params);
for (int i; i < n_params; ++i) {
KIM::DataType data_type;
const string * param_name;
const string * param_desc;
int size;
model->GetParameterMetadata(i, &data_type, &size,
¶m_name, ¶m_desc);
// Fancy way to do free_parameter_map[param_name] = fp; but
// without assignment or copy.
free_parameter_map.emplace(piecewise_construct,
forward_as_tuple(*param_name),
forward_as_tuple(*param_name,
*param_desc,
data_type,
size,
i));
}
}
}
Compute::~Compute() {
KIM::Model::Destroy(&model);
}
void Compute::compute() {
// Reset arrays to zero. Dunno if needed, but with ghost atoms I am
// unsure.
/*
if (has_forces)
for (unsigned i = 0; i < forces.size(); ++i)
forces[i] = 0;
if (has_particleEnergy)
for (unsigned i = 0; i < particleEnergy.size(); ++i)
particleEnergy[i] = 0;
if (has_particleVirial)
for (unsigned i = 0; i < particleVirial.size(); ++i)
particleVirial[i] = 0;
*/
for (unsigned dim = 0; dim < 6; ++dim)
virial_from_dEdr(dim) = 0.0;
// Compute.
const int error = model->Compute(compute_arguments);
if (error)
throw runtime_error(string("Error in KIM's compute function in line ")
+ to_string(__LINE__)
+ string(" of file ") + string(__FILE__));
// Before reducing the ghost atoms, calculate the global virial from
// forces. This can be used to verify the global virial returned by
// the model.
if (has_forces) {
global_virial_from_forces_xx = 0.0;
global_virial_from_forces_yy = 0.0;
global_virial_from_forces_zz = 0.0;
global_virial_from_forces_yz = 0.0;
global_virial_from_forces_xz = 0.0;
global_virial_from_forces_xy = 0.0;
const double* ghost_pos = box_->get_positions_ptr();
for (unsigned i = 0; i < box_->nall; ++i) {
global_virial_from_forces_xx -= ghost_pos[3*i + 0] * forces[3*i + 0];
global_virial_from_forces_yy -= ghost_pos[3*i + 1] * forces[3*i + 1];
global_virial_from_forces_zz -= ghost_pos[3*i + 2] * forces[3*i + 2];
global_virial_from_forces_yz -= ghost_pos[3*i + 1] * forces[3*i + 2];
global_virial_from_forces_xz -= ghost_pos[3*i + 0] * forces[3*i + 2];
global_virial_from_forces_xy -= ghost_pos[3*i + 0] * forces[3*i + 1];
}
}
// Tricks to fix values when ghost atoms are in the game.
if (box_->nghosts)
for (unsigned i = box_->natoms; i < box_->nall; ++i) {
const unsigned central = i % box_->natoms;
/* KIM forbids ghost atoms to have an energy :-(
if (has_particleEnergy) {
particleEnergy[central] += particleEnergy[i];
}
*/
if (has_forces) {
forces[3*central + 0] += forces[3*i + 0];
forces[3*central + 1] += forces[3*i + 1];
forces[3*central + 2] += forces[3*i + 2];
}
if (has_particleVirial) {
particleVirial[6*central + 0] += particleVirial[6*i + 0];
particleVirial[6*central + 1] += particleVirial[6*i + 1];
particleVirial[6*central + 2] += particleVirial[6*i + 2];
particleVirial[6*central + 3] += particleVirial[6*i + 3];
particleVirial[6*central + 4] += particleVirial[6*i + 4];
particleVirial[6*central + 5] += particleVirial[6*i + 5];
}
}
}
int Compute::get_neigh(void * const compute_ptr,
const int number_of_neighlists,