/
core.py
1504 lines (1207 loc) · 53.6 KB
/
core.py
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
"""
"""
import os
import numpy as np
import warnings
import itertools
from ase.io import write
import uuid
import gzip
import io
from scipy.special import sph_harm
import copy
from pyscal.atoms import Atoms, AttrSetter
import pyscal.csystem as pc
import pyscal.traj_process as ptp
from pyscal.formats.ase import convert_snap
import pyscal.structure_creator as pcs
#import pyscal.routines as routines
#import pyscal.visualization as pv
class System:
"""
Python class for holding the properties of an atomic configuration
"""
def __init__(self, filename=None, format="lammps-dump",
compressed = False, customkeys=None):
self.initialized = True
self.neighbors_found = False
self.neighbor_method = None
self.ghosts_created = False
self.actual_box = None
#box parameters
self.rot = [[0,0,0], [0,0,0], [0,0,0]]
self.rotinv = [[0,0,0], [0,0,0], [0,0,0]]
self.boxdims = [0,0,0]
self.triclinic = 0
self._atoms = Atoms()
if filename is not None:
self.read_inputfile(filename, format=format,
compressed = compressed, customkeys=customkeys)
@classmethod
def from_structure(cls, structure, lattice_constant = 1.00, repetitions = None, ca_ratio = 1.633, noise = 0, element=None, chemical_symbol=None):
atoms, box = pcs.make_crystal(structure, lattice_constant=lattice_constant,
repetitions=repetitions, ca_ratio=ca_ratio,
noise=noise, element=element)
obj = cls()
obj.box = box
obj.atoms = atoms
obj.atoms._lattice = structure
obj.atoms._lattice_constant = lattice_constant
return obj
def iter_atoms(self):
return self.atoms.iter_atoms()
@property
def natoms(self):
return self.atoms.natoms
@property
def concentration(self):
return self.atoms.composition
@property
def composition(self):
return self.atoms.composition
@property
def box(self):
"""
Wrap for inbuilt box
"""
if self.actual_box is not None:
return self.actual_box
else:
return self._box
@box.setter
def box(self, userbox):
"""
Box setter
"""
#we should automatically check for triclinic cells here
summ = 0
for i in range(3):
box1 = np.array(userbox[i-1])
box2 = np.array(userbox[i])
summ += np.dot(box1, box2)/(np.linalg.norm(box1)*np.linalg.norm(box2))
#check if the summ is zero
if (np.abs(summ) > 1E-6):
#this is a triclinic box
rot = np.array(userbox).T
rotinv = np.linalg.inv(rot)
self.triclinic = 1
self.rot = rot
self.rotinv = rotinv
self.boxdims[0] = np.sum(np.array(userbox[0])**2)**0.5
self.boxdims[1] = np.sum(np.array(userbox[1])**2)**0.5
self.boxdims[2] = np.sum(np.array(userbox[2])**2)**0.5
self._box = userbox
@property
def volume(self):
"""
Volume of box
"""
vol = np.dot(np.cross(self._box[0], self._box[1]), self._box[2])
return vol
@property
def atoms(self):
return self._atoms
@atoms.setter
def atoms(self, atoms):
"""
Set atoms
"""
if(len(atoms['positions']) < 200):
#we need to estimate a rough idea
needed_atoms = 200 - len(atoms)
#get a rough cell
needed_cells = np.ceil(needed_atoms/len(atoms))
nx = int(needed_cells**(1/3))
nx = int(np.ceil(nx/2))
if np.sum(self.box) == 0:
raise ValueError("Simulation box should be initialized before atoms")
atoms = self.repeat((nx, nx, nx), atoms=atoms, ghost=True, scale_box=True, assign=False, return_atoms=True)
self._atoms = atoms
def add_atoms(self, atoms):
"""
Cleanly add a given list of atoms
Parameters
----------
atoms : dict
Returns
-------
None
"""
## MOVE TO ATOMS
self._atoms.add_atoms(atoms)
#def repeat(self, reps, atoms=None, ghost=False, scale_box=True, assign=True):
"""
Frac greater than one will be converted to int
"""
"""
if isinstance(reps, list):
if not len(reps)==3:
raise ValueError("repetitions should be [nx, ny, nz]")
else:
reps = [reps, reps, reps]
isfrac = np.prod([True if (0<rep<1) else False for rep in reps])
if isfrac:
atoms = self._repeat_partial_box(reps, atoms=atoms, ghost=ghost, scale_box=scale_box)
else:
atoms = self._repeat_full_box(reps, atoms=atoms, ghost=ghost, scale_box=scale_box)
if assign:
self._atoms = atoms
else:
return atoms
"""
def repeat(self, reps, atoms=None, ghost=False, scale_box=True, assign=False, return_atoms=False):
"""
"""
box = self.box
self.actual_box = box.copy()
if atoms is None:
atoms = self.atoms
newatoms = []
idstart = len(atoms) + 1
x1 = -reps[0]
x2 = reps[0]+1
y1 = -reps[1]
y2 = reps[1]+1
z1 = -reps[2]
z2 = reps[2]+1
xs = 2*reps[0] + 1
ys = 2*reps[1] + 1
zs = 2*reps[2] + 1
datadict = {key:[] for key in atoms.keys()}
del datadict['positions']
del datadict['ids']
del datadict['head']
del datadict['ghost']
positions = []
ids = []
head = []
ghosts = []
for i in range(x1, x2):
for j in range(y1, y2):
for k in range(z1, z2):
if (i==j==k==0):
continue
for count, pos in enumerate(atoms['positions']):
#we should create ghost images for only real atoms
if not atoms["ghost"][count]:
pos = (pos + i*np.array(box[0]) + j*np.array(box[1]) + k*np.array(box[2]))
positions.append(list(pos))
ids.append(idstart)
head.append(count)
ghosts.append(ghost)
idstart += 1
for key in datadict.keys():
datadict[key].append(atoms[key][count])
if scale_box:
box[0] = xs*np.array(box[0])
box[1] = ys*np.array(box[1])
box[2] = zs*np.array(box[2])
self.box = box
if ghost:
self.ghosts_created = True
atoms['positions'].extend(positions)
atoms['ids'].extend(ids)
atoms['ghost'].extend(ghosts)
atoms['head'].extend(head)
for key in datadict.keys():
atoms[key].extend(datadict[key])
if return_atoms:
return atoms
else:
self.atoms = atoms
return self
#def _repeat_partial_box(self, reps, atoms=None, ghost=False, scale_box=True):
"""
"""
"""
box = self.box
self.actual_box = box.copy()
if atoms is None:
atoms = self.atoms
idstart = len(atoms) + 1
for index, rep in enumerate(reps):
if not (0 < rep < 1):
raise ValueError("Fractional repetition should be 0<rep<1")
left = rep*self.boxdims[index]
right = (1-rep)*self.boxdims[index]
datadict = {key:[] for key in atoms.keys()}
del datadict['positions']
del datadict['ids']
del datadict['head']
del datadict['ghost']
positions = []
ids = []
head = []
ghosts = []
disc = 0
for count, pos in enumerate(atoms['positions']):
if (pos[index] <= left):
displacement = [0, 0, 0]
displacement[index] = 1.0
displace = True
elif (pos[index] > right):
displacement = [0, 0, 0]
displacement[index] = -1.0
displace = True
else:
displace = False
if displace:
disc += 1
pos = pc.remap_and_displace_atom(pos, self.triclinic,
self.rot, self.rotinv,
self.boxdims, displacement)
positions.append(list(pos))
ids.append(idstart)
head.append(count)
ghosts.append(ghost)
idstart += 1
for key in datadict.keys():
datadict[key].append(atoms[key][count])
print(len(atoms['positions']))
print("displaced %d"%disc)
atoms['positions'] = [*atoms['positions'], *positions]
atoms['ids'] = [*atoms['ids'], *ids]
atoms['ghost'] = [*atoms['ghost'], *ghosts]
atoms['head'] = [*atoms['head'], *head]
for key in datadict.keys():
atoms[key] = [*atoms[key], *datadict[key]]
if scale_box:
print(box[index])
box[index] = np.array(box[index])+2*rep*np.array(box[index])
print(box[index])
if scale_box:
print("here")
print(box)
self.box = box
print(self.box)
if ghost:
self.ghosts_created = True
return atoms
"""
def apply_mask(self, mask_type="primary", ids=None, indices=None, condition=None, selection=False):
"""
Notes
-----
Masks can be used to exclude atoms from neighbor calculations. An atom for which
mask is set to True is excluded from the calculation. There are two types of masks,
`primary` or `secondary`. For example, neighbors are being calculated for a central
atom `i`. The neighbor atom is denoted as `j`. If `primary` mask of `i` is True, no neighbor
calculation is carried out for `i`. If it is False, `i` is considered. Now if `secondary`
mask of `j` is True, it will not included in the list of neighbors of `i` even if it is within
the cutoff distance. The `primary` mask of `j` has no effect in this situation.
An example situation can be to calculate the local concentration around Ni atoms in a NiAl
structure. In this case, the `primary` mask of all Al atoms can be set to True so that
only `Ni` atoms are considered. Now, in a second case, the task is to count the number of Al
atoms around each Ni atom. For this case, the `primary` mask of all Al atoms can be set to True,
and the `secondary` mask of all Ni atoms can be set to True.
The masks for ghost atoms are copied from the corresponding mask for real atoms.
"""
#check if length of mask is equal to length of real atoms
self.atoms.apply_mask(mask_type=mask_type, ids=ids,
indices=indices, condition=condition, selection=selection)
def remove_mask(self, mask_type="primary", ids=None, indices=None, condition=None, selection=False):
"""
Remove applied masks
Parameters
----------
mask_type: string, optional
type of mask to be applied, either `primary`, `secondary` or `all`
Returns
-------
None
"""
self._atoms.remove_mask(mask_type=mask_type, ids=ids,
indices=indices, condition=condition, selection=selection)
def apply_selection(self, ids=None, indices=None, condition=None):
self._atoms.apply_selection(ids=ids, indices=indices, condition=condition)
def remove_selection(self, ids=None, indices=None, condition=None):
self._atoms.remove_selection(ids=ids, indices=indices, condition=condition)
def delete(self, ids=None, indices=None, condition=None, selection=False):
self._atoms.delete(ids=ids, indices=indices, condition=condition, selection=selection)
def embed_in_cubic_box(self, inputbox=None, return_box=False):
"""
Embedded the triclinic box in a cubic box
"""
#first task is to create a box representation
if inputbox is None:
box = self._box
backupbox = box.copy()
else:
box = inputbox
backupbox = inputbox.copy
a = np.array(box[0])
b = np.array(box[1])
c = np.array(box[2])
cosa = np.dot(b, c)/(np.linalg.norm(b)*np.linalg.norm(c))
cosb = np.dot(c, a)/(np.linalg.norm(c)*np.linalg.norm(a))
cosc = np.dot(a, b)/(np.linalg.norm(a)*np.linalg.norm(b))
lx = np.linalg.norm(a)
xy = np.linalg.norm(b)*cosc
xz = np.linalg.norm(c)*cosb
ly = np.sqrt(np.linalg.norm(b)*np.linalg.norm(b) - xy*xy)
yz = (np.linalg.norm(b)*np.linalg.norm(c)*cosa - xy*xz)/ly
lz = np.sqrt(np.linalg.norm(c)*np.linalg.norm(c) - xz*xz - yz*yz)
xlo = ylo = zlo = 0
xhi = lx
yhi = ly
zhi = lz
xlo_bound = xlo + min(0.0,xy,xz,xy+xz)
xhi_bound = xhi + max(0.0,xy,xz,xy+xz)
ylo_bound = ylo + min(0.0,yz)
yhi_bound = yhi + max(0.0,yz)
zlo_bound = zlo
zhi_bound = zhi
newbox = np.array([[xhi_bound-xlo_bound, 0, 0], [0, yhi_bound-ylo_bound, 0], [0, 0, zhi_bound-zlo_bound]])
if not return_box:
self.newbox = newbox
self.box = newbox
self.box_backup = backupbox
self.actual_box = None
else:
return newbox
def get_distance(self, pos1, pos2, vector=False):
"""
Get the distance between two atoms.
Parameters
----------
pos1 : list
first atom position
pos2 : list
second atom position
vector: bool, optional
If True, return the vector between two atoms
Returns
-------
distance : double
distance between the first and second atom.
Notes
-----
Periodic boundary conditions are assumed by default.
"""
diff = pc.get_distance_vector(pos1, pos2, self.triclinic,
self.rot, self.rotinv, self.boxdims)
dist = np.linalg.norm(diff)
if vector:
return dist, diff
else:
return dist
def get_concentration(self):
"""
Return a dict containing the concentration of the system
Parameters
----------
None
Returns
-------
condict : dict
dict of concentration values
"""
return self.concentration
def read_inputfile(self, filename, format="lammps-dump",
compressed = False, customkeys=None):
"""
Read input file that contains the information of system configuration.
Parameters
----------
filename : string
name of the input file.
format : {'lammps-dump', 'poscar', 'ase', 'mdtraj'}
format of the input file, in case of `ase` the ASE Atoms object
compressed : bool, optional
If True, force to read a `gz` compressed format, default False.
customkeys : list
A list containing names of headers of extra data that needs to be read in from the
input file.
Returns
-------
None
Notes
-----
`format` keyword specifies the format of the input file. Currently only
a `lammps-dump` and `poscar` files are supported. Additionaly, the widely
use Atomic Simulation environment (https://wiki.fysik.dtu.dk/ase/ase/ase.html).
mdtraj objects (http://mdtraj.org/1.9.3/) are also supported by using the keyword
`'mdtraj'` for format. Please note that triclinic boxes are not yet supported for
mdtraj format.
Atoms object can also be used directly. This function uses the
:func:`~pyscal.traj_process` module to process a file which is then assigned to system.
`compressed` keyword is not required if a file ends with `.gz` extension, it is
automatically treated as a compressed file.
Triclinic simulation boxes can also be read in.
If `custom_keys` are provided, this extra information is read in from input files if
available. This information is can be accessed directly as `self.atoms['customkey']`
"""
self.neighbors_found = False
self.neighbor_method = None
self.ghosts_created = False
self.actual_box = None
#box parameters
self.rot = [[0,0,0], [0,0,0], [0,0,0]]
self.rotinv = [[0,0,0], [0,0,0], [0,0,0]]
self.boxdims = [0,0,0]
self.triclinic = 0
atoms, box = ptp.read_file(filename, format=format,
compressed=compressed, customkeys=customkeys,)
self.box = box
self.atoms = atoms
def to_file(self, outfile, format='lammps-dump', customkeys=None, customvals=None,
compressed=False, timestep=0, species=None):
"""
Save the system instance to a trajectory file.
Parameters
----------
outfile : string
name of the output file
format : string, {'lammps-dump', 'lammps-data', 'poscar'}
format of the output file, default `lammps-dump`
Currently only `lammps-dump` format is supported.
customkeys : list of strings, optional
a list of extra atom wise values to be written in the output file.
customvals : list or list of lists, optional
If `customkey` is specified, `customvals` take an array of the same length
as number of atoms, which contains the values to be written out.
compressed : bool, optional
If true, the output is written as a compressed file.
timestep : int, optional
timestep to be written to file. default 0
species : None, optional
species of the atoms. Required if any format other than 'lammps-dump' is used. Required
for convertion to ase object.
Returns
-------
None
Notes
-----
`to_file` method can handle a number of file formats. The most customizable format is the
`lammps-dump` which can take a custom options using customkeys and customvals. customkeys
will be the header written to the dump file. It can be any Atom attribute, any property
stored in custom variable of the Atom, or calculated q values which can be given by `q4`,
`aq4` etc. External values can also be provided using `customvals` option. `customvals` array
should be of the same length as the number of atoms in the system.
For all other formats, ASE is used to write out the file, and hence the `species` keyword
needs to be specified. If initially, an ASE object was used to create the System, `species`
keyword will already be saved, and need not be specified. In other cases, `species` should
be a list of atomic species in the System. For example `["Cu"]` or `["Cu", "Al"]`, depending
on the number of species in the System. In the above case, atoms of type 1 will be mapped to
Cu and of type 2 will be mapped to Al. For a complete list of formats that ASE can handle,
see `here <https://wiki.fysik.dtu.dk/ase/ase/io/io.html>`_ .
"""
ptp.write_file(self, outfile, format = format,
compressed = compressed, customkeys = customkeys, customvals = customvals,
timestep = timestep, species = species)
def to_ase(self, species=None):
"""
Convert system to an ASE Atoms object
Parameters
----------
species : list of string
The chemical species
Returns
-------
None
"""
return convert_snap(self, species=species)
def reset_neighbors(self):
"""
Reset the neighbors of all atoms in the system.
Parameters
----------
None
Returns
-------
None
Notes
-----
It is used automatically when neighbors are recalculated.
"""
self.atoms["neighbors"] = []
self.atoms["neighbordist"] = []
self.atoms["temp_neighbors"] = []
self.atoms["temp_neighbordist"] = []
self.atoms["neighborweight"] = []
self.atoms["diff"] = []
self.atoms["r"] = []
self.atoms["theta"] = []
self.atoms["phi"] = []
self.atoms["cutoff"] = []
self.neighbors_found = False
mapdict = {}
mapdict["neighbors"] = {}
mapdict["neighbors"]["index"] = "neighbors"
mapdict["neighbors"]["distance"] = "neighbordist"
mapdict["neighbors"]["weight"] = "neighborweight"
mapdict["neighbors"]["displacement"] = "diff"
mapdict["neighbors"]["cutoff"] = "cutoff"
mapdict["neighbors"]["angle"] = {}
mapdict["neighbors"]["angle"]["polar"] = "theta"
mapdict["neighbors"]["angle"]["azimuthal"] = "phi"
mapdict["neighbors"]["temporary"] = {}
mapdict["neighbors"]["temporary"]["index"] = "temp_neighbors"
mapdict["neighbors"]["temporary"]["distance"] = "temp_neighbordist"
self.atoms._add_attribute(mapdict)
def _check_neighbors(self):
"""
Check if neighbors are calculated
"""
if not self.neighbors_found:
raise ValueError("This calculation needs neighbors to be calculated")
def average_over_neighbors(self, key, include_self=True):
"""
Perform a simple average over neighbor atoms
Parameters
----------
key: string
atom property
include_self: bool, optional
If True, include the host atom in the calculation
Returns
-------
"""
print(key)
self._check_neighbors()
if not key in self.atoms.keys():
raise KeyError("required property not found!")
test = self.atoms[key][0]
if isinstance(test, list):
raise TypeError("Averaging can only be done over 1D quantities")
avgarr = []
for i in range(len(self.atoms["positions"])):
arr = []
if include_self:
arr.append(self.atoms[key][i])
for j in self.atoms["neighbors"][i]:
arr.append(self.atoms[key][j])
avgarr.append(np.mean(arr))
return avgarr
def find_neighbors(self, method='cutoff', cutoff=0, threshold=2,
voroexp=1, padding=1.2, nlimit=6,
cells=None, nmax=12, assign_neighbor=True):
"""
Find neighbors of all atoms in the :class:`~pyscal.core.System`.
Parameters
----------
method : {'cutoff', 'voronoi', 'number'}
`cutoff` method finds neighbors of an atom within a specified or adaptive cutoff distance from the atom.
`voronoi` method finds atoms that share a Voronoi polyhedra face with the atom. Default, `cutoff`
`number` method finds a specified number of closest neighbors to the given atom. Number only populates
cutoff : { float, 'sann', 'adaptive'}
the cutoff distance to be used for the `cutoff` based neighbor calculation method described above.
If the value is specified as 0 or `adaptive`, adaptive method is used.
If the value is specified as `sann`, sann algorithm is used.
threshold : float, optional
only used if ``cutoff=adaptive``. A threshold which is used as safe limit for calculation of cutoff.
voroexp : int, optional
only used if ``method=voronoi``. Power of the neighbor weight used to weight the contribution of each atom towards
Steinhardt parameter values. Default 1.
padding : double, optional
only used if ``cutoff=adaptive`` or ``cutoff=number``. A safe padding value used after an adaptive cutoff is found. Default 1.2.
nlimit : int, optional
only used if ``cutoff=adaptive``. The number of particles to be considered for the calculation of adaptive cutoff.
Default 6.
cells : bool, optional
If True, always use cell lists. Default None.
nmax : int, optional
only used if ``cutoff=number``. The number of closest neighbors to be found for each atom. Default 12
Returns
-------
None
Raises
------
RuntimeWarning
raised when `threshold` value is too low. A low threshold value will lead to 'sann' algorithm not converging
when finding a neighbor. This function will try to automatically increase `threshold` and check again.
RuntimeError
raised when neighbor search was unsuccessful. This is due to a low `threshold` value.
Notes
-----
This function calculates the neighbors of each particle. There are several ways to do this. A complete description of
the methods can be `found here <https://pyscal.readthedocs.io/en/latest/nearestneighbormethods.html>`_.
Method cutoff and specifying a cutoff radius uses the traditional approach being the one in which the neighbors of an atom
are the ones that lie in the cutoff distance around it.
In order to reduce time during the distance sorting during thefind_neighbors adaptive methods, pyscal sets an initial guess for a cutoff distance.
This is calculated as,
.. math:: r_{initial} = threshold * (simulation~box~volume/ number~of~particles)^{(1/3)}
threshold is a safe multiplier used for the guess value and can be set using the `threshold` keyword.
In Method cutoff, if ``cutoff='adaptive'``, an adaptive cutoff is found during runtime for each atom [1].
Setting the cutoff radius to 0 also uses this algorithm. The cutoff for an atom i is found using,
.. math:: r_c(i) = padding * ((1/nlimit) * \sum_{j=1}^{nlimit}(r_{ij}))
padding is a safe multiplier to the cutoff distance that can be set through the keyword `padding`. `nlimit` keyword sets the
limit for the top nlimit atoms to be taken into account to calculate the cutoff radius.
In Method cutoff, if ``cutoff='sann'``, sann algorithm is used [2]. There are no parameters to tune sann algorithm.
The second approach is using Voronoi polyhedra which also assigns a weight to each neighbor in the ratio of the face area between the two atoms.
Higher powers of this weight can also be used [3]. The keyword `voroexp`
can be used to set this weight.
If method is `number`, instead of using a cutoff value for finding neighbors, a specified number of closest atoms are
found. This number can be set through the argument `nmax`.
If `cells` is None, cell lists are used if number of atoms are higher than 2500. If True, cell lists are always used.
.. warning::
Adaptive and number cutoff uses a padding over the intial guessed "neighbor distance". By default it is 2. In case
of a warning that ``threshold`` is inadequate, this parameter should be further increased. High/low value
of this parameter will correspond to the time taken for finding neighbors.
References
----------
.. [1] Stukowski, A, Model Simul Mater SC 20, 2012
.. [2] van Meel, JA, Filion, L, Valeriani, C, Frenkel, D, J Chem Phys 234107, 2012
.. [3] Haeberle, J, Sperl, M, Born, P, arxiv 2019
"""
#first reset all neighbors
self.reset_neighbors()
self.filter = 0
if threshold < 1:
raise ValueError("value of threshold should be at least 1.00")
if cells is None:
cells = (self.natoms > 250)
if method == 'cutoff':
if cutoff=='sann':
finished = False
for i in range(1, 10):
finished = pc.get_all_neighbors_sann(self.atoms, 0.0,
self.triclinic, self.rot, self.rotinv,
self.boxdims, threshold*i, cells)
if finished:
if i>1:
warnings.warn("found neighbors with higher threshold than default/user input")
break
warnings.warn("Could not find sann cutoff. trying with a higher threshold", RuntimeWarning)
else:
raise RuntimeError("sann cutoff could not be converged. This is most likely, \
due to a low threshold value. Try increasing it.")
elif cutoff=='adaptive' or cutoff==0:
finished = pc.get_all_neighbors_adaptive(self.atoms, 0.0,
self.triclinic, self.rot, self.rotinv,
self.boxdims, threshold, nlimit, padding, cells)
if not finished:
raise RuntimeError("Could not find adaptive cutoff")
else:
if cells:
pc.get_all_neighbors_cells(self.atoms, cutoff,
self.triclinic, self.rot, self.rotinv, self.boxdims)
else:
pc.get_all_neighbors_normal(self.atoms, cutoff,
self.triclinic, self.rot, self.rotinv, self.boxdims)
elif method == 'number':
finished = pc.get_all_neighbors_bynumber(self.atoms, 0.0,
self.triclinic, self.rot, self.rotinv,
self.boxdims, threshold, nmax, cells, assign_neighbor)
if not finished:
raise RuntimeError("Could not find enough neighbors - try increasing threshold")
elif method == 'voronoi':
clean_vertices = (cutoff>0)
#CLEANING is TURNED OFF
#if not clean_vertices:
#copy the simulation cell
backupbox = self._box.copy()
if self.triclinic:
if not self.ghosts_created:
atoms = self.repeat((1, 1, 1), ghost=True, scale_box=True, assign=False, return_atoms=True)
self._atoms = atoms
self.embed_in_cubic_box()
pc.get_all_neighbors_voronoi(self.atoms, 0.0,
self.triclinic, self.rot, self.rotinv,
self.boxdims, voroexp)
if self.triclinic:
self._box = backupbox
#now clean up
#else:
# real_atomdict = {"positions":copy.copy(self.atoms.positions_for_all),
# "ghost":copy.copy(self.atoms.positions_for_ghost)}
#we need to call the method
#this means alles good
# if self.actual_box is None:
# if self.triclinic:
# new_box = self.embed_in_cubic_box(inputbox=self._box, return_box=True)
# rot = np.array(new_box).T
# rotinv = np.linalg.inv(rot)
# else:
# new_box = self._box
# rot = [[0,0,0], [0,0,0], [0,0,0]]
# rotinv = [[0,0,0], [0,0,0], [0,0,0]]
# #ghosts are present
# else:
# if self.triclinic:
# new_box = self.embed_in_cubic_box(inputbox=self.actual_box, return_box=True)
# rot = np.array(new_box).T
# rotinv = np.linalg.inv(rot)
# else:
# new_box = self.actual_box
# rot = [[0,0,0], [0,0,0], [0,0,0]]
# rotinv = [[0,0,0], [0,0,0], [0,0,0]]
# boxdims = [0,0,0]
# boxdims[0] = np.sum(np.array(new_box[0])**2)**0.5
# boxdims[1] = np.sum(np.array(new_box[1])**2)**0.5
# boxdims[2] = np.sum(np.array(new_box[2])**2)**0.5
# pc.get_all_neighbors_voronoi(real_atomdict, 0.0,
# self.triclinic, rot, rotinv,
# boxdims, 1)
# pc.clean_voronoi_vertices(real_atomdict,
# self.atoms, 0.0,
# self.triclinic, rot, rotinv,
# boxdims, cutoff)
#unique_vertices = []
#for i in range(len(self.vertex_is_unique)):
# for j in range(len(self.vertex_is_unique[i])):
# if self.vertex_is_unique[i][j]:
# unique_vertices.append(self.vertex_positions[i][j])
#self.atoms["vertex_positions_unique_skipcheck"] = unique_vertices
#assign extra options
mapdict = {}
mapdict["voronoi"] = {}
mapdict["voronoi"]["volume"] = "voronoi_volume"
mapdict["voronoi"]["face"] = {}
mapdict["voronoi"]["face"]["vertices"] = "face_vertices"
mapdict["voronoi"]["face"]["perimeters"] = "face_perimeters"
mapdict["voronoi"]["vertex"] = {}
mapdict["voronoi"]["vertex"]["vectors"] = "vertex_vectors"
mapdict["voronoi"]["vertex"]["numbers"] = "vertex_numbers"
mapdict["voronoi"]["vertex"]["positions"] = "vertex_positions"
#mapdict["voronoi"]["vertex"]["unique_positions"] = "vertex_positions_unique_nofilter"
self.atoms._add_attribute(mapdict)
self.neighbors_found = True
def calculate_q(self, q, averaged=False, continuous_algorithm=False):
"""
Find the Steinhardt parameter q_l for all atoms.
Parameters
----------
q : int or list of ints
A list of all Steinhardt parameters to be found.
averaged : bool, optional
If True, return the averaged q values, default False
continuous_algorithm: bool, optional
See Notes for description.
Returns
-------
q : list of floats
calculated q values
Notes
-----
Enables calculation of the Steinhardt parameters [1] q. The type of
q values depend on the method used to calculate neighbors. See the description
:func:`~pyscal.core.System.find_neighbors` for more details.
The option `continuous_algorithm` specifies which algorithm to use for calculations. If False,
an algorithm [3] is used. The C++ algorithm is faster is a large, consecutive number of q values (> 200)
are to be calculated.
This function creates three new attributes for this class: `qx`, `qx_real` and `qx_imag`,
where `stands` for the q number.
References
----------
.. [1] Steinhardt, PJ, Nelson, DR, Ronchetti, M. Phys Rev B 28, 1983
.. [2] Lechner, W, Dellago, C, J Chem Phys, 2013
"""
if isinstance(q, int):
qq = [q]
else:
qq = q
self._check_neighbors()
if averaged:
self._calculate_aq(qq)
qvals = [self.atoms["avg_q%d"%x] for x in qq]
else:
if continuous_algorithm:
lm = max(qq)
pc.calculate_q(self.atoms, lm)
else:
self._calculate_q(qq)
qvals = [self.atoms["q%d"%x] for x in qq]
return qvals
def _calculate_q(self, qq):
"""
Private method for calculation of qvals