-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.hpp
1511 lines (1227 loc) · 56.3 KB
/
core.hpp
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,2017,2018,2019,2020 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.
*/
#ifndef MYTESTCORE_HPP
#define MYTESTCORE_HPP
#include <fstream>
#include <vector>
#include <array>
#include <string>
#include <map>
#include <memory>
#include <random>
#include "KIM_SimulatorHeaders.hpp"
#include "ndarray.hpp"
#include "utils.hpp"
/** The main namespace. */
namespace mytest {
/** Class to hold simulation box and neighbor lists.
@todo Disabled neighbor list leads to segfaults because the
class does not take into account that there are no neighbor
lists and tries to access them.
@todo Way to disable rvec in the constructor.
@todo Copy constructor, move constructor, and semantics.
@todo All methods that manipulate the box (scale etc.) should
also have constant versions that return a new box that has the
given manipulation done. This can be useful in some
circumstances.
*/
class Box {
public:
// Initialization related //////////////////////////////////////////
/** General constructor.
This constructor takes unique_ptr arguments to assume
ownership of position/type arrays. This is done to save memory
and to avoid copying this data. If you need to copy the data
you must do that manually beforehand.
@param a First vector spanning the simulation box.
@param b Second vector spanning the simulation box.
@param c Third vector spanning the simulation box.
@param periodic_a Periodic boundary condition in a-direction.
@param periodic_b Periodic boundary condition in b-direction.
@param periodic_c Periodic boundary condition in c-direction.
@param coordinates Unique pointer to n×3 coordinate array.
@param types Unique pointer to n component array containing
the atom types. Must have the same length as the
first dimension of @p coordinates.
@param name A name for the box.
*/
Box(const Vec3D<double>& a, const Vec3D<double>& b, const Vec3D<double>& c,
bool periodic_a, bool periodic_b, bool periodic_c,
std::unique_ptr< Array2D<double> > coordinates,
std::unique_ptr< Array1DInit<std::string> > types,
const std::string& name);
/** Lattice constructor.
Construct a given lattice and repeat in specified way.
@param lattice A string specifying which lattice should be
produced. Supported lattices with a basis of 1
are @c sc, @c bcc, @c fcc, and @c diamond.
Supported lattices with a basis of 2 are @c
dimer, @c B1 (NaCl), @c B2 (CsCl), and @c B3
(zincblende).
@param lattice_const The lattice constant a. In case of a
dimer this is interpreted as the bond
length.
@param cubic Produce a cubic unit cell instead of the
primitive unit cell.
@param repeat_a Repeat unit cell this many times in a direction.
@param repeat_b Repeat unit cell this many times in b direction.
@param repeat_c Repeat unit cell this many times in c direction.
@param periodic_a Periodic boundary condition in a-direction.
@param periodic_b Periodic boundary condition in b-direction.
@param periodic_c Periodic boundary condition in c-direction.
@param coordinates Unique pointer to n×3 coordinate array.
@param types The types of the atoms on the lattice. The length
of this array must match the basis of the given
lattice.
@param name A name for the box.
@todo: lattices with c != a.
*/
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 std::vector<std::string>& types,
const std::string& name);
/** Copy constructor that changes the name. */
Box(const Box& other, const std::string& new_name);
/** Copy constructor. */
Box(const Box& other);
virtual ~Box() {}
/** Create a randomly filled box
@todo: document params
*/
static
std::unique_ptr<Box> random_box(double a, double b, double c,
bool periodic_a,
bool periodic_b,
bool periodic_c,
double min_dist,
const std::string& atomtype,
const std::string& name,
std::mt19937& rng);
/** Create a randomly filled box with random types from list
@todo: document params
*/
static
std::unique_ptr<Box> random_box(double a, double b, double c,
bool periodic_a,
bool periodic_b,
bool periodic_c,
double min_dist,
const std::vector<std::string>& atomtypes,
const std::string& name,
std::mt19937& rng);
/** Initialize and/or update neighbor list.
This will allocate memory for ghost atoms if needed and
includes a call to update_ghosts().
@todo should wrap atoms outside the box back into it!
@param cutoff The cutoff.
@param skin Fraction of cutoff that should be added to the
cutoff to avoid re-calculating the neighbor list
when atoms move small distances.
@param typemap Mapping of atom type strings to the model's
type codes.
@return If the return value is true, then any previous
pointers to ghost atom memory are invalid (that means
re-setting KIM data for positions and types at least).
*/
bool update_neighbor_list(double cutoff, double skin,
const std::map<std::string,int>& typemap);
/** Update ghost atom positions from central atom positions.
With a MI_OPBC_F neighbor mode, this will also enforce
periodic boundary conditions but only on the ghost positions
array, i.e. invisible from the @c positions field.
Call after manipulating atomic positions even if ghost atoms
are disabled.
@param typemap Mapping of atom type strings to the model's
type codes.
*/
void update_ghosts(const std::map<std::string,int>& typemap);
/** Return unique_ptr to new box with atom i deleted.
@param i The atom that should be deleted.
@param new_name Name for the new box.
*/
std::unique_ptr<Box> delete_atom(unsigned i, const std::string& name) const;
/** Return unique_ptr to new box with atom i deleted.
The new box will have the same name as the old one.
@param i The atom that should be deleted.
*/
std::unique_ptr<Box> delete_atom(unsigned i) const {
return delete_atom(i, name_);
}
/** Return unique_ptr to new box, repeated along the given directions.
@param repeat_a Repeat this many times in a direction.
@param repeat_b Repeat this many times in b direction.
@param repeat_c Repeat this many times in c direction.
@param new_name Name for the new box.
*/
std::unique_ptr<Box> repeat(unsigned repeat_a,
unsigned repeat_b,
unsigned repeat_c,
const std::string& name) const;
/** Return unique_ptr to new box, repeated along the given directions.
The new box will have the same name as the old one.
@param repeat_a Repeat this many times in a direction.
@param repeat_b Repeat this many times in b direction.
@param repeat_c Repeat this many times in c direction.
*/
std::unique_ptr<Box> repeat(unsigned repeat_a,
unsigned repeat_b,
unsigned repeat_c) const {
return repeat(repeat_a, repeat_b, repeat_c, name_);
}
// Public data /////////////////////////////////////////////////////
/** Constant reference to box side lengths.
This is a three-component vector of side lengths, data is
continous.
Be warned that these are just the norms of the box
vectors. Only for an orthorhombic box do they define the box
completely.
@todo Actually make sure the internal storage of Vec3D is
continuous!! Use array internally, make x/y/z references!
*/
const Vec3D<double>& box_side_lengths;
/** Constant reference to box vector a.
This is a three-component vector of a box vector, data is
continous.
@todo Actually make sure the internal storage of Vec3D is
continuous!! Use array internally, make x/y/z references!
*/
const Vec3D<double>& a;
/** Constant reference to box vector b.
This is a three-component vector of a box vector, data is
continous.
@todo Actually make sure the internal storage of Vec3D is
continuous!! Use array internally, make x/y/z references!
*/
const Vec3D<double>& b;
/** Constant reference to box vector c.
This is a three-component vector of a box vector, data is
continous.
@todo Actually make sure the internal storage of Vec3D is
continuous!! Use array internally, make x/y/z references!
*/
const Vec3D<double>& c;
/** Periodic boundary conditions.
Constant three-component vector that defines if the box is
periodic in ±a, ±b, and ±c direction.
*/
const Vec3D<bool>& periodic;
/** Number of atoms without ghost atoms. */
const unsigned& natoms;
/** Number of ghost atoms. */
const unsigned& nghosts;
/** Number of all atoms, including ghost atoms. */
const unsigned& nall;
/** Atom positions.
Users are free to manipulate the data in this array. Changes
will not be applied until calling update_ghosts().
*/
Array2D<double> positions;
/** Atom types.
Contains atom types as strings.
Users are free to manipulate the data in this array. Changes
will not be applied until calling update_ghosts(). Changing
the size produces undefined results!
*/
Array1DInit<std::string> types;
// Calculate/return values /////////////////////////////////////////
/** Calculate volume of the simulation cell.
@return Volume of the box.
*/
double calc_volume() const {
return dot(a_, cross(b_, c_));
}
/** Calculate distance between to atoms i and j.
@param i First atom.
@param j Second atom, both may be ghost atoms.
@return Distance.
*/
double calc_dist(int i, int j) const {
// Just a convenience wrapper.
double dx, dy, dz;
return calc_dist(i,j,dx,dy,dz);
};
/** Calculate distance between to atoms i and j.
@param i First atom.
@param j Second atom, both may be ghost atoms.
@param[out] dx Will be set to x<sub>j</sub> - x<sub>i</sub>.
@param[out] dy Will be set to y<sub>j</sub> - y<sub>i</sub>.
@param[out] dz Will be set to z<sub>j</sub> - z<sub>i</sub>.
@return Distance.
*/
double calc_dist(int i, int j, double& dx, double& dy, double& dz) const;
/** Return const reference to the neighbor list of an atom.
@param i The index of the central atom, must not be a ghost atom.
@return A reference to atom i's neighbor list.
*/
const std::vector<int>& get_neighbors(unsigned i) const {
return neigh_list_[i];
}
/** Return const pointer to the neighbor list of an atom.
@param i The index of the central atom, must not be a ghost atom.
@return A pointer to atom i's neighbor list.
*/
const int* get_neighbors_ptr(unsigned i) const {
return &get_neighbors(i)[0];
}
/** Return const pointer to position (including ghosts).
This is different from the @c positions field insofar as this
the data that must be used for actual calculations. It is
updated by update_neighbor_list() and update_ghosts(). The
returned pointer may lose validity when calling
update_neighbor_list(). It is also the position data that
includes ghost atom positions.
In short: If you want to change positions use the @c positions
field, if you want to calculate based on positions use this
method.
*/
const double* get_positions_ptr() const {
return &(*ghost_positions)(0,0);
}
/** Return const pointer to type codes (including ghosts).
Same arguments as for get_positions() vs @c positions
apply. Further difference compared to the @c types field is
that these are the type codes (int, not string) as the model
wants them.
@see get_positions()
*/
const int* get_types_ptr() const {
return &(*ghost_types)(0);
}
/** Return const pointer to "contributing" array.
Same arguments as for get_positions() vs @c positions
apply. Further difference compared to the @c types field is
that these are the type codes (int, not string) as the model
wants them.
@see get_positions()
*/
const int* get_contributing_ptr() const {
return &contributing_[0];
}
// IO //////////////////////////////////////////////////////////////
/** Write to file.
@param filename The name of the output file (will be silently
overwritten).
*/
void write_to(const std::string& filename) const {
std::ofstream f(filename);
write_to(f);
};
/** Write to file in extended XYZ format.
@param output An output stream.
*/
void write_to(std::ostream& output) const;
// Manipulate box //////////////////////////////////////////////////
/** Uniformly scale box.
Includes scaling of atom positions. Implicitly calls
update_ghost_rvecs().
@param factor The factor that the box lengths will be
multiplied with.
@param typemap Mapping of atom type strings to the model's
type codes.
*/
void scale(double factor,
const std::map<std::string,int>& typemap) {
deform(Voigt6<double>(factor, factor, factor, 0, 0, 0), typemap);
}
/** Scale box in a, b, and c direction.
Includes scaling of atom positions. Implicitly calls
update_ghost_rvecs().
@param factor_a Multiply box vector a by this factor.
@param factor_b Multiply box vector b by this factor.
@param factor_c Multiply box vector c by this factor.
@param typemap Mapping of atom type strings to the model's
type codes.
*/
void scale(double factor_a, double factor_b, double factor_c,
const std::map<std::string,int>& typemap);
/** Set box vector lengths.
Includes scaling of atom positions. Implicitly calls
update_ghost_rvecs().
@param len_a Scale box vector a to have this length.
@param len_b Scale box vector b to have this length.
@param len_c Scale box vector c to have this length.
@param typemap Mapping of atom type strings to the model's
type codes.
*/
void scale_to(double len_a, double len_b, double len_c,
const std::map<std::string,int>& typemap) {
scale(len_a / box_side_lengths_[0],
len_b / box_side_lengths_[1],
len_c / box_side_lengths_[2],
typemap);
}
/** Deform box by given deformation matrix.
Includes scaling of atom positions. Implicitly calls
update_ghost_rvecs().
A deformation matrix is defined is the strain tensor plus the
identity matrix: ε<sub>ij</sub> + δ<sub>ij</sub>.
@param defmatrix The deformation matrix in Voigt notation.
@param typemap Mapping of atom type strings to the model's
type codes.
*/
void deform(Voigt6<double> defmatrix,
const std::map<std::string,int>& typemap);
/** Deform box to fit the given box vectors.
Includes scaling of atom positions. Implicitly calls
update_ghost_rvecs().
@param new_a The new box vector a.
@param new_b The new box vector b.
@param new_c The new box vector c.
*/
void deform_to(const Vec3D<double>& new_a,
const Vec3D<double>& new_b,
const Vec3D<double>& new_c);
protected:
/** Return number of atoms in the unit cell of a given lattice.
See the constructor for details of the parameters.
*/
static unsigned atoms_per_unit_cell(const std::string& lattice,
bool cubic);
/** Return the number of different species in the unit cell of a
given lattice.
See the constructor for details of the parameters.
*/
static unsigned species_per_unit_cell(const std::string& lattice);
/** Calculate the number of ghost atom shells needed. */
Vec3D<unsigned> calc_number_of_ghost_shells(double cutoff) const;
private:
Vec3D<double> box_side_lengths_;
Vec3D<double> a_, b_, c_;
Vec3D<bool> periodic_;
unsigned natoms_, nghosts_, nall_;
std::string name_;
Vec3D<unsigned> ghost_shells;
std::unique_ptr< Array2D<double> > ghost_positions;
std::unique_ptr< Array1D<int> > ghost_types;
std::vector< std::vector<int> > neigh_list_;
std::vector<int> contributing_; // false if ghost
};
/** Interface to KIM model free parameters. */
class FreeParam {
public:
FreeParam(const std::string& name,
const std::string& description,
KIM::DataType data_type,
int size,
int kim_index)
: name(name), description(description),
data_type(data_type), size(size), kim_index(kim_index)
{}
const std::string name;
const std::string description;
const KIM::DataType data_type;
const unsigned size;
const int kim_index;
};
/** Parameters for the Birch-Murnaghan equation of state. */
struct BMParams {
double E0, V0, B0, dB0_dp;
};
/** Main interface class.
This class contains a simulation box and interacts with KIM.
*/
class Compute {
public:
/** General constructor.
Take a simulation box and a neighbor mode and prepare KIM.
@param box The simulation box. This class takes possession of
the pointer.
@param modelname KIM model identifier.
*/
Compute(std::unique_ptr<Box> box, const std::string& modelname,
KIM::LengthUnit length_unit = KIM::LENGTH_UNIT::A,
KIM::EnergyUnit energy_unit = KIM::ENERGY_UNIT::eV,
KIM::ChargeUnit charge_unit = KIM::CHARGE_UNIT::e,
KIM::TemperatureUnit temperature_unit = KIM::TEMPERATURE_UNIT::K,
KIM::TimeUnit time_unit = KIM::TIME_UNIT::ps);
/** Destructor.
Will deallocate all KIM memory, too.
*/
~Compute();
/** Update the neighbor list and re-register any changed arrays to
KIM.
Call after atom positions have changed more than the cutoff
skin or if the cutoff has changed (increased).
@param force_ptr_update Force re-registering pointers to KIM.
*/
void update_neighbor_list(bool force_ptr_update = false);
/** Get box vector a. */
Vec3D<double> get_a() const { return box_->a; }
/** Get box vector b. */
Vec3D<double> get_b() const { return box_->b; }
/** Get box vector c. */
Vec3D<double> get_c() const { return box_->c; }
/** Get code for a particle type.
@param name String representation (chemical symbol).
@return Type code.
*/
int get_particle_type_code(const std::string& name) {
auto it = partcl_type_codes.find(name);
if (it != partcl_type_codes.end())
return it->second;
else
throw std::runtime_error("unknown type code");
}
/** Get name for a particle type.
@param code Integer represting that particle type.
@return Type name.
*/
std::string get_particle_type_name(int code) {
auto it = partcl_type_names.find(code);
if (it != partcl_type_names.end())
return it->second;
else
throw std::runtime_error("unknown type name");
}
/** Get number of particle types in the box. */
unsigned get_number_of_particle_types() {
return partcl_type_codes.size();
}
/** Return number of atoms in the simulation box. */
unsigned get_natoms() const {
return box_->natoms;
}
/** Get position of an atom.
@param i The index of the atom.
@return The position of atom i.
*/
Vec3D<double> get_position(unsigned i) const {
const double* pos = box_->get_positions_ptr();
return Vec3D<double>(pos[i*3 + 0], pos[i*3 + 1], pos[i*3 + 2]);
}
/** Set position of an atom.
Does not update neighbor lists.
@param i The index of the atom.
@param new_pos The new position of atom i.
@param update_ghosts Update the actual positions and ghost
atom positions of the box. This must be
set to true to actually register your
change. It may be set to false when
updating the positions of several atoms
in a row. Then only the last call needs
to set this to true. Optional, default is
@c true.
*/
void set_position(unsigned i, const Vec3D<double> new_pos,
bool update_ghosts = true){
box_->positions(i,0) = new_pos[0];
box_->positions(i,1) = new_pos[1];
box_->positions(i,2) = new_pos[2];
if (update_ghosts)
box_->update_ghosts(partcl_type_codes);
}
/** Set position of an atom.
Does not update neighbor lists.
@param i The index of the atom.
@param x The new x position of atom i.
@param y The new y position of atom i.
@param z The new z position of atom i.
@param update_ghosts Update the actual positions and ghost
atom positions of the box. This must be
set to true to actually register your
change. It may be set to false when
updating the positions of several atoms
in a row. Then only the last call needs
to set this to true. Optional, default is
@c true.
*/
void set_position(unsigned i, double x, double y, double z,
bool update_ghosts = true){
box_->positions(i,0) = x;
box_->positions(i,1) = y;
box_->positions(i,2) = z;
if (update_ghosts)
box_->update_ghosts(partcl_type_codes);
}
/** Move atom by specified offset.
Does not update neighbor lists.
@param i The index of the atom.
@param offset The offset. Will be added to the position of
atom i.
@param update_ghosts Update the actual positions and ghost
atom positions of the box. This must be
set to true to actually register your
change. It may be set to false when
updating the positions of several atoms
in a row. Then only the last call needs
to set this to true. Optional, default is
@c true.
*/
void move_atom(unsigned i, const Vec3D<double> offset,
bool update_ghosts = true){
box_->positions(i,0) += offset[0];
box_->positions(i,1) += offset[1];
box_->positions(i,2) += offset[2];
if (update_ghosts)
box_->update_ghosts(partcl_type_codes);
}
/** Move atom by specified offset.
Does not update neighbor lists.
@param i The index of the atom.
@param offset_x The x offset. Will be added to the position of
atom i.
@param offset_y The y offset. Will be added to the position of
atom i.
@param offset_z The z offset. Will be added to the position of
atom i.
@param update_ghosts Update the actual positions and ghost
atom positions of the box. This must be
set to true to actually register your
change. It may be set to false when
updating the positions of several atoms
in a row. Then only the last call needs
to set this to true. Optional, default is
@c true.
*/
void move_atom(unsigned i,
double offset_x, double offset_y, double offset_z,
bool update_ghosts = true){
box_->positions(i,0) += offset_x;
box_->positions(i,1) += offset_y;
box_->positions(i,2) += offset_z;
if (update_ghosts)
box_->update_ghosts(partcl_type_codes);
}
/** Deform box by given deformation matrix.
Includes scaling of atom positions. Implicitly calls
update_ghost_rvecs().
A deformation matrix is defined is the strain tensor plus the
identity matrix: ε<sub>ij</sub> + δ<sub>ij</sub>.
@param defmatrix The deformation matrix in Voigt notation.
@todo Take care that the box stays orthorhombic if
kim_neighbor_mode is KIM_mi_opbc_f.
*/
void deform(Voigt6<double> defmatrix) {
box_->deform(defmatrix, partcl_type_codes);
}
/** Compute values using KIM.
Output values that are not supported by the model are simply
zero.
Due to the way periodic boundaries are implemented in CLUSTER
mode (namely using ghost atoms), we need particleEnergy to
actually calculate energy and particleVirial to actually
calculate virial. If a model doesn't provide those energy and
virial are simply set to zero in that case.
*/
void compute();
/** Get the computed potential energy of the box. */
double get_energy() const {
return energy;
};
/** Get the computed potential energy of atom i. */
double get_energy(unsigned i) const {
return particleEnergy[i];
};
/** Get the computed potential energy per atom of the box. */
double get_energy_per_atom() const {
return energy / box_->natoms;
};
/** Get the force of atom i, component dim */
double get_force(unsigned i, unsigned dim) const {
if (dim >= 3)
throw std::runtime_error("invalid dim");
if (i >= box_->natoms)
throw std::runtime_error("invalid atom id");
return forces[3*i + dim];
}
/** Get the computed virial tensor. */
Voigt6<double> get_virial() const {
return virial;
}
/** Get the computed virial tensor for atom i. */
Voigt6<double> get_virial(unsigned i) const {
if (i >= box_->natoms)
throw std::runtime_error("invalid atom id");
return Voigt6<double>(particleVirial[6*i + 0],
particleVirial[6*i + 1],
particleVirial[6*i + 2],
particleVirial[6*i + 3],
particleVirial[6*i + 4],
particleVirial[6*i + 5]);
}
/** Get the global virial tensor computed from forces.
This can be used to verify the global virial tensor from the
model.
*/
Voigt6<double> get_virial_from_forces() const {
return Voigt6<double>(global_virial_from_forces_xx,
global_virial_from_forces_yy,
global_virial_from_forces_zz,
global_virial_from_forces_yz,
global_virial_from_forces_xz,
global_virial_from_forces_xy);
}
/** Get the global virial tensor computed using process_dEdr.
This can be used to verify the global virial tensor from the
model and process_dEdr.
*/
Voigt6<double> get_virial_from_dEdr() const {
return virial_from_dEdr;
}
/** Change model parameter.
Neighbor lists will not be updated automatically.
@param param_name KIM name of the free parameter.
@param index Index indicating which element of the
parameter array to access.
@param value The new value (must match the type of the
parameter).
@param reinit The new parameter may not actually be used or
used correctly until this method was called with
reinit = true. Setting it to false can be used
when updating several parameters in a row to
save computation time. Optional, default is @c true.
If true, the cutoff will also be updated from KIM.
*/
void set_parameter(const std::string& param_name,
const unsigned index,
double value, bool reinit = true);
/** Change model parameter.
Neighbor lists will not be updated automatically.
@param param_name KIM name of the free parameter.
@param index Index indicating which element of the
parameter array to access.
@param value The new value (must match the type of the
parameter).
@param reinit The new parameter may not actually be used or
used correctly until this method was called with
reinit = true. Setting it to false can be used
when updating several parameters in a row to
save computation time. Optional, default is @c true.
If true, the cutoff will also be updated from KIM.
*/
void set_parameter(const std::string& param_name,
const unsigned index,
int value, bool reinit = true);
/** Change model parameter.
Neighbor lists will not be updated automatically.
This method assumes that the parameter is a N*N row-major
array, where N is the number of supported species. It also
assumes that the species code can be used to index into that
array.
@param param_name KIM name of the free parameter.
@param species1 The element name for i.
@param species2 The element name for j.
@param value The new value (must match the type of the
parameter).
@param reinit The new parameter may not actually be used or
used correctly until this method was called with
reinit = true. Setting it to false can be used
when updating several parameters in a row to
save computation time. Optional, default is @c true.
If true, the cutoff will also be updated from KIM.
*/
void set_parameter(const std::string& param_name,
const std::string& species1,
const std::string& species2,
double value, bool reinit = true);
/** Change model parameter.
Neighbor lists will not be updated automatically.
This method assumes that the parameter is a N*N*N row-major
array, where N is the number of supported species. It also
assumes that the species code can be used to index into that
array.
@param param_name KIM name of the free parameter.
@param species1 The element name for i.
@param species2 The element name for j.
@param species3 The element name for k.
@param value The new value (must match the type of the
parameter).
@param reinit The new parameter may not actually be used or
used correctly until this method was called with
reinit = true. Setting it to false can be used
when updating several parameters in a row to
save computation time. Optional, default is @c true.
If true, the cutoff will also be updated from KIM.
*/
void set_parameter(const std::string& param_name,
const std::string& species1,
const std::string& species2,
int value, bool reinit = true);
/** Change model parameter.
Neighbor lists will not be updated automatically.
This method assumes that the parameter is a N*N*N row-major
array, where N is the number of supported species. It also
assumes that the species code can be used to index into that
array.
@param param_name KIM name of the free parameter.
@param species1 The element name for i.
@param species2 The element name for j.
@param species3 The element name for k.
@param value The new value (must match the type of the
parameter).
@param reinit The new parameter may not actually be used or
used correctly until this method was called with
reinit = true. Setting it to false can be used
when updating several parameters in a row to
save computation time. Optional, default is @c true.
If true, the cutoff will also be updated from KIM.
*/
void set_parameter(const std::string& param_name,
const std::string& species1,
const std::string& species2,
const std::string& species3,
double value, bool reinit = true);
/** Change model parameter.
Neighbor lists will not be updated automatically.
This method assumes that the parameter is a N*N*N row-major
array, where N is the number of supported species. It also
assumes that the species code can be used to index into that
array.
@param param_name KIM name of the free parameter.
@param species1 The element name for i.
@param species2 The element name for j.
@param species3 The element name for k.
@param value The new value (must match the type of the
parameter).
@param reinit The new parameter may not actually be used or
used correctly until this method was called with
reinit = true. Setting it to false can be used