-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_applications.py
176 lines (162 loc) · 5.87 KB
/
main_applications.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
"""
Code for nonlinear localized waves identification in numerical simulations of
one-dimensional crystal lattice model
"""
import numpy as np
import time
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import joblib as jl
from param_val import comp_param
from functions.energy_E import comp_energy_E
from functions.force_onsite import comp_force_onsite
from functions.force_interaction import comp_force_interaction
from functions.density import comp_density
# Plotting properties
import matplotlib
matplotlib.rc("font", size=22)
matplotlib.rc("axes", titlesize=22)
plt.rcParams.update({
"text.usetex": True,
"font.family": "serif"
})
print("Start of the computation!")
# Start time
start = time.time()
# Parameter values
# Number of particles
Np = 64
# Simulation time
Tsim = 200
# Dictionary <param>
param = comp_param(Np, Tsim)
# Load classifier
mth = "lle"
kernel = "linear"
data_title = "X"
# Number of simulations used
Nsim = 1000
# Number of neighboring particles
Nd = 8
clf = jl.load("saved_classifiers/clf_" + mth + "_" + kernel + "_" + data_title +
"_Nsim" + str(Nsim) + "Nd" + str(Nd) + ".pkl")
dim_red = jl.load("saved_classifiers/" + mth + "_" + data_title +
"_Nsim" + str(Nsim) + "Nd" + str(Nd) + ".pkl")
scaler = jl.load("saved_classifiers/" + mth + "_scaler_" + data_title +
"_Nsim" + str(Nsim) + "Nd" + str(Nd) +".pkl")
# Initial conditions: two breathers' collision
q = param["xp"]
p = np.zeros((param["Np"], 1))
# Stationary
gamma = 0.43
p[Np//4-2] = -1*gamma
p[Np//4-1] = 2*gamma
p[Np//4] = -2*gamma
p[Np//4+1] = 1*gamma
# Moving
gamma = -0.6
p[3*Np//4-1] = -1*gamma
p[3*Np//4] = 2*gamma
p[3*Np//4+1] = -1*gamma
# Save simulation data
# Positions
Q = np.zeros((param["Np"], param["Nsteps"]+1))
Q[:,0] = q.T
# Displacements
W = np.zeros((param["Np"], param["Nsteps"]+1))
# Compute particle displacement values taking into account
# periodic boundary conditions
w = q - param["xp"]
if q[0] > param["Np"]/2:
w[0] -= param["Np"]
if q[-1] < param["Np"]/2:
w[0] += param["Np"]
W[:, 0] = w.T
# Momenta
P = np.zeros((param["Np"], param["Nsteps"]+1))
P[:,0] = p.T
# Energy density values
E = np.zeros((param["Np"], param["Nsteps"]+1))
E[:,0] = comp_energy_E(q, p, param["eps"], param["Np"]).T
# Localization density values
rho = np.zeros((param["Np"], param["Nsteps"]+1))
e = np.reshape(E[:, 0].T, q.shape)
rho[:, 0] = comp_density(w, p, E[:, 0].T, Nd, clf, dim_red, scaler, data_title)
# Time integration with the Verlet method
for n in range(param["Nsteps"]):
# Update positions
qq = q + param["tau"]/2*p
# Compute forces
G = comp_force_onsite(qq)
F = comp_force_interaction(qq, param["eps"], param["Np"])
# Update momenta
p = p + param["tau"]*(G+F)
# Update positions
q = qq + param["tau"]/2*p
# Save data
Q[:, n+1] = q.T
P[:, n+1] = p.T
E[:, n+1] = comp_energy_E(q, p, param["eps"], param["Np"]).T
e = np.reshape(E[:, n+1].T, q.shape)
# Compute particle displacement values
w = q - param["xp"]
if q[0] > param["Np"]/2:
w[0] -= param["Np"]
if q[-1] < param["Np"]/2:
w[0] += param["Np"]
# Save particle displacement values
W[:, n+1] = w.T
# Compute localization density function values at time t=(n+1)*tau
rho[:, n+1] = comp_density(w, p, e, Nd, clf, dim_red, scaler, data_title)
# Save simulation data into folder saved_applications_data
np.savetxt("saved_applications_data/Q_applications_" + mth + "_" +
kernel + "_" + data_title + "_Nsim" + str(Nsim) + "Nd" + str(Nd) +
".csv", Q, delimiter=",")
np.savetxt("saved_applications_data/W_applications_" + mth + "_" +
kernel + "_" + data_title + "_Nsim" + str(Nsim) + "Nd" + str(Nd) +
".csv", W, delimiter=",")
np.savetxt("saved_applications_data/P_applications_" + mth + "_" +
kernel + "_" + data_title + "_Nsim" + str(Nsim) + "Nd" + str(Nd) +
".csv", P, delimiter=",")
np.savetxt("saved_applications_data/E_applications_" + mth + "_" +
kernel + "_" + data_title + "_Nsim" + str(Nsim) + "Nd" + str(Nd) +
".csv", E, delimiter=",")
np.savetxt("saved_applications_data/rho_applications_" + mth + "_" +
kernel + "_" + data_title + "_Nsim" + str(Nsim) + "Nd" + str(Nd) +
".csv", rho, delimiter=",")
# Plot particle energy density function in time
fig, ax = plt.subplots(figsize=(9, 6.5))
x, y = np.meshgrid(param["xp"]+1, param["time"])
im = plt.contourf(x, y, E.T+param["eps"], 100, cmap="viridis")
plt.xlabel(r"$n$")
plt.ylabel(r"$t$")
plt.axis([1, param["Np"], 0, param["Tfinal"]])
plt.title(r"Particle energy density function")
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="2%", pad=0.1)
plt.colorbar(im, cax=cax)
# Save figure
plt.savefig("figures/particle_energy_density" +
"_Nsim" + str(Nsim) + "Nd" + str(Nd) + ".png",
dpi=300, bbox_inches="tight")
plt.show()
# Plot normalized localization density function in time
fig, ax = plt.subplots(figsize=(9, 6.5))
x, y = np.meshgrid(param["xp"]+1, param["time"])
im = plt.contourf(x, y, rho.T/Nd, Nd, cmap="plasma")
plt.xlabel(r"$n$")
plt.ylabel(r"$t$")
plt.axis([1, param["Np"], 0, param["Tfinal"]])
plt.title(r"Normalized localization density function")
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="2%", pad=0.1)
plt.colorbar(im, cax=cax, ticks=np.round(np.arange(0,2,1/Nd),2))
# Save figure
plt.savefig("figures/normalized_localization_density" +
"_Nsim" + str(Nsim) + "Nd" + str(Nd) + ".png",
dpi=300, bbox_inches="tight")
plt.show()
# End time
end = time.time()
# Total time taken
print(f"Runtime of the program was {(end - start)/60:.4f} min.")