test.py~ 3.37 KB
Newer Older
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
import numpy as np
import matplotlib.pyplot as plt

from angular_fingerprintFeature import Angular_Fingerprint
from featureCalculators_multi.angular_fingerprintFeature_cy import Angular_Fingerprint as Angular_Fingerprint_cy
from custom_calculators import doubleLJ_calculator

from ase import Atoms
from ase.visualize import view
from ase.io import read, write

from time import time

dim = 3

L = 2
d = 1
pbc = [1,1,0]

"""
N = 2
x = np.array([0.7*L, 0.9*L, d/2,
              0.7*L, 0.5*L, d/2])
positions = x.reshape((-1, dim))
atomtypes = ['He', 'He']
a = Atoms(atomtypes,
          positions=positions,
          cell=[L,L,d],
          pbc=pbc)

N = 3
x = np.array([1.5*L, 0.2*L, d/2,
              0.5*L, 0.9*L, d/2,
              -0.5*L, 0.5*L, d/2,])
positions = x.reshape((-1,dim))
atomtypes = ['He', 'H', 'H']
a = Atoms(atomtypes,
          positions=positions,
          cell=[L,L,d],
          pbc=pbc)

x = np.array([1, 0, 0, 2, 0, 0, 3, 0, 0, 1.5, 1, 0])
positions = x.reshape((-1,dim))
a = Atoms('H4',
          positions=positions,
          cell=[4,2,1],
          pbc=[0, 0, 0])

N = 4
x = np.array([0.2*L, 0.7*L, d/2,
              0.3*L, 0.2*L, d/2,
              0.7*L, 0.9*L, d/2,
              0.7*L, 0.5*L, d/2])
positions = x.reshape((-1,dim))
atomtypes = ['H', 'H', 'He', 'He']
a = Atoms(atomtypes,
          positions=positions,
          cell=[L,L,d],
          pbc=pbc)
"""
N = 5
x = np.array([0.2*L, 0.7*L, d/2,
              0.3*L, 0.2*L, d/2,
              0.7*L, 0.9*L, d/2,
              0.7*L, 0.5*L, d/2,
              0.9*L, 0.1*L, d/2])
positions = x.reshape((-1,dim))
atomtypes = ['H', 'He', 'O', 'H', 'H']
a = Atoms(atomtypes,
          positions=positions,
          cell=[L,L,d],
          pbc=pbc)
"""
atoms = read('graphene_data/graphene_all2.traj', index=':')
a = atoms[100]
atomtypes = a.get_atomic_numbers()
N = len(a.get_atomic_numbers())
x = a.get_positions().reshape(-1)
"""

#view(a)

Rc1 = 4
binwidth1 = 0.1
sigma1 = 0.2

Rc2 = 3
Nbins2 = 50
sigma2 = 0.2

eta = 30
gamma = 2
use_angular = True


featureCalculator = Angular_Fingerprint(a, Rc1=Rc1, Rc2=Rc2, binwidth1=binwidth1, Nbins2=Nbins2, sigma1=sigma1, sigma2=sigma2,
                                        eta=eta, gamma=gamma, use_angular=use_angular)
t0 = time()
fingerprint = featureCalculator.get_feature(a)
fingerprint_grad = featureCalculator.get_featureGradient(a)
runtime = time() - t0


featureCalculator_cy = Angular_Fingerprint_cy(a, Rc1=Rc1, Rc2=Rc2, binwidth1=binwidth1, Nbins2=Nbins2, sigma1=sigma1, sigma2=sigma2,
                                              eta=eta, gamma=gamma, use_angular=use_angular)
t0_cy = time()
fingerprint_cy = featureCalculator_cy.get_feature(a)
fingerprint_grad_cy = featureCalculator_cy.get_featureGradient(a)
runtime_cy = time() - t0_cy

print(featureCalculator_cy.Nelements)


print('runtime python:', runtime)
print('runtime cython:', runtime_cy)
print('times faster:', runtime/runtime_cy)
Nelements = featureCalculator.Nelements
Nbins1 = int(np.ceil(Rc1/binwidth1))

if use_angular:
    Nbins = Nbins1 + Nbins2
else:
    Nbins = Nbins1
Nbondtypes = int(Nelements/Nbins)
r = np.linspace(0,Rc1*Nbondtypes, Nelements)

print('py:', fingerprint.shape)
print('cy:', fingerprint_cy.shape)
plt.figure(1)
plt.plot(r, fingerprint)
plt.plot(r, fingerprint_cy, linestyle=':', color='k')

plt.figure(2)
plt.plot(r, fingerprint_grad.T)
plt.plot(r, fingerprint_grad_cy.T, linestyle=':', color='k')

plt.show()