-
Notifications
You must be signed in to change notification settings - Fork 1
/
GPE_lyap.py
286 lines (253 loc) · 10.9 KB
/
GPE_lyap.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
'''
Copyright <2019> <Andrei E. Tarkhov, Skolkovo Institute of Science and Technology, https://github.com/TarkhovAndrei/DGPE>
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 2 conditions:
1) If any part of the present source code is used for any purposes with subsequent publication of obtained results,
the GitHub repository shall be cited in all publications, according to the citation rule:
"Andrei E. Tarkhov, Skolkovo Institute of Science and Technology,
source code from the GitHub repository https://github.com/TarkhovAndrei/DGPE, 2019."
2) 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.
'''
import numpy as np
from GPElib.lyapunov_generator import LyapunovGenerator
from GPElib.visualisation import Visualisation
import matplotlib
print matplotlib.matplotlib_fname()
import matplotlib.pyplot as plt
import sys
sys.stderr = sys.stdout
def init_instability(inst, traj_seed, from_backup=False, init_conds=None):
if from_backup == False:
inst.generate_init('random', traj_seed, 100.)
# delta = (2. * np.sqrt(1.0 * inst.N_part/inst.N_wells)) * np.random.rand()
delta = (2. * np.sqrt(1.0 * inst.N_part)) * np.random.rand()
x0, y0, err = inst.E_const_perturbation_XY(inst.X[:,:,:,0], inst.Y[:,:,:,0], delta, degrees_of_freedom=10)
x1, y1 = inst.constant_perturbation_XY(x0,y0)
inst.set_init_XY(x0,y0,x1,y1)
else:
inst.set_init_XY(init_conds[0], init_conds[1], init_conds[2], init_conds[3])
err = 0
return err
print len(sys.argv), sys.argv
if len(sys.argv) > 4:
seed_from = int(sys.argv[1])
seed_to = int(sys.argv[2])
my_id = int(sys.argv[3])
unique_id = sys.argv[4]
else:
seed_from = 0
seed_to = 1
needed_trajs = np.arange(seed_from, seed_to)
my_id = 0
unique_id = 'ID_not_stated13'
needed_trajs = np.arange(seed_from, seed_to)
perturb_seeds = np.arange(123,124)#(2381,2382)#(100, 110)#(106,108)#(97,98)#(97, 100)#(15, 18) #[53, 12, 20, 87]
# time = 30 * 40 * 80.
time = 10.# * 40 * 80.
time_backup = 1.
N_backups = int(np.ceil(time / time_backup)) + 1
time = N_backups * time_backup
# time = 1.
# time = 100. * 15
# step = 0.00015625
step = 0.001
N_wells = 10.
W = 0.
lyap = LyapunovGenerator(N_part_per_well=100,
W=W, disorder_seed=53,
# N_wells=(10,1,1), dimensionality=1, threshold_XY_to_polar=0.25,
# N_wells=(10,10,1), dimensionality=2, threshold_XY_to_polar=0.25,
N_wells=(10,1,1), dimensionality=1, threshold_XY_to_polar=0.25,
reset_steps_duration=5,
# reset_steps_duration=150,
time=time_backup, step=step)
grname = 'GPE_lyap_' + unique_id
# vis = Visualisation(is_local=0, HOMEDIR='/data1/andrey/data/', GROUP_NAMES=grname)
# vis_backup = Visualisation(is_local=0, HOMEDIR='/data1/andrey/data/backups/', GROUP_NAMES=grname)
vis = Visualisation(is_local=1, HOMEDIR='/Users/tarkhov/tmp/', GROUP_NAMES=grname)
vis_backup = Visualisation(is_local=1, HOMEDIR='/Users/tarkhov/tmp/backups/', GROUP_NAMES=grname)
def try_find_backup():
try:
backup_intro = np.load(vis_backup.filename('_BACKUP_PRESENT_' + str(my_id)))
backup_id = backup_intro['backup_id']
print backup_id
backup = np.load(vis_backup.filename('_BACKUP_PRESENT_' + str(my_id) + '_intermediate_' + str(backup_id)))
print 'Start from backup file'
backup_present = True
return backup, backup_id, backup_present
except:
print 'No backup file, start from 0'
backup_id = -1
backup_present = False
backup = None
return backup, backup_id, backup_present
backup, backup_id, backup_present = try_find_backup()
print "Noise ", W
print "Characteristic, full, step times, n_steps"
print lyap.tau_char, lyap.time, lyap.step, lyap.n_steps
if backup_present == False:
num_good = 0
lmbdas = []
lmbdas_no_regr = []
chosen_trajs = []
effective_nonlinearity = []
energies = []
distances = []
numb_of_part = []
next_traj = 0
next_seed = 0
backup_id = -1
curr_traj = -1
curr_seed = -1
else:
num_good = backup['num_good']
lmbdas = backup['lambdas']
lmbdas_no_regr = backup['lambdas_no_regr']
chosen_trajs = backup['chosen']
effective_nonlinearity = backup['eff_nonl']
energies = backup['energies']
distances = backup['distance']
numb_of_part = backup['numb_of_part']
curr_traj = backup['curr_traj']
perturb_seeds = backup['pert_seeds']
curr_seed = backup['curr_seed']
time_finished = backup['time_finished']
backup_id = backup['backup_id']
def save_backup(backup_id):
if backup_id == 0:
init_conds = [lyap.X[:,:,:,0], lyap.Y[:,:,:,0], lyap.X1[:,:,:,0], lyap.Y1[:,:,:,0]]
else:
init_conds = [lyap.X[:,:,:,lyap.icurr], lyap.Y[:,:,:,lyap.icurr], lyap.X1[:,:,:,lyap.icurr], lyap.Y1[:,:,:,lyap.icurr]]
np.savez_compressed(vis_backup.filename('_BACKUP_PRESENT_' + str(my_id)),
backup_id=backup_id)
np.savez_compressed(vis_backup.filename('_BACKUP_PRESENT_' + str(my_id) + '_intermediate_' + str(backup_id)),
lambdas=lmbdas, lambdas_no_regr=lmbdas_no_regr,
eff_nonl=effective_nonlinearity,
init_conds=init_conds,
numb_of_part=numb_of_part, energies=energies,
pert_seeds=perturb_seeds,
chosen=chosen_trajs, step=lyap.step, time=lyap.time, n_steps=lyap.n_steps,
my_info=[seed_from, seed_to, my_id], needed_trajs=needed_trajs,
checksum=lyap.consistency_checksum, error_code=lyap.error_code,
distance=distances,
curr_traj=i_traj, curr_seed=j_traj,
time_finished=k_traj * time_backup,
backup_id=backup_id, num_good=num_good
)
for i_traj, traj_seed in enumerate(needed_trajs):
if i_traj < curr_traj:
continue
if num_good > needed_trajs.shape[0] - 1:
print 'We really have enough trajs, exit!'
break
for j_traj, pert_seed in enumerate(perturb_seeds):
if j_traj < curr_seed:
continue
if num_good > needed_trajs.shape[0] - 1:
print 'We really have enough trajs, exit!'
break
for k_traj in xrange(N_backups):
if k_traj <= backup_id:
continue
backup, backup_id, backup_present = try_find_backup()
if backup_present:
print 'Backup found: ', backup_id, k_traj
err = init_instability(lyap, traj_seed, from_backup=True, init_conds=backup['init_conds'])
lyap.traj_seed = traj_seed
lyap.pert_seed = pert_seed
else:
print 'No backup found, going to find a track'
np.random.seed()
lyap.traj_seed = traj_seed
lyap.pert_seed = pert_seed
err = 1
while err == 1:
traj_seed = np.random.randint(100000)
print "SEED: ", traj_seed
err = init_instability(lyap, traj_seed)
if err == 1:
print 'Bad trajectory! ', i_traj
print 'Good trajectory found!'
lyap.n_steps = int(time_backup / step)
lyap.time = time_backup
save_backup(k_traj)
continue
lyap.n_steps = int(time_backup / step)
lyap.time = time_backup
lyap.run_dynamics(no_pert=True)
if backup_present:
print 'Backup present, go adding results'
num_good = backup['num_good']
lmbdas = backup['lambdas']
lmbdas_no_regr = backup['lambdas_no_regr']
chosen_trajs = backup['chosen']
effective_nonlinearity = backup['eff_nonl']
energies = backup['energies']
distances = backup['distance']
numb_of_part = backup['numb_of_part']
curr_traj = backup['curr_traj']
curr_seed = backup['curr_seed']
time_finished = backup['time_finished']
backup_id = backup['backup_id']
lmbdas = np.hstack((lmbdas, lyap.lambdas[:-1]))
lmbdas_no_regr = np.hstack((lmbdas_no_regr, lyap.lambdas_no_regr[:-1]))
# chosen_trajs.append((traj_seed, pert_seed))
effective_nonlinearity = np.hstack((effective_nonlinearity, lyap.effective_nonlinearity))
energies = np.hstack((energies, lyap.energy))
print energies
distances = np.hstack((distances, lyap.distance))
numb_of_part = np.hstack((numb_of_part, lyap.number_of_particles))
print lyap.lambdas
print lyap.lambdas_no_regr
save_backup(k_traj)
# np.savez_compressed(vis_backup.filename(my_id),
# lambdas=lmbdas, lambdas_no_regr=lmbdas_no_regr,
# eff_nonl=effective_nonlinearity,
# numb_of_part=numb_of_part, energies=energies,
# chosen=chosen_trajs, step=lyap.step, time=lyap.time, n_steps=lyap.n_steps,
# my_info=[seed_from, seed_to, my_id], needed_trajs=needed_trajs,
# checksum=lyap.consistency_checksum, error_code=lyap.error_code,
# distance=lyap.distance)
num_good += 1
# plt.semilogy(lyap.T, lyap.distance)
# np.savez_compressed(vis.filename(my_id) + '_traj_' + str(i_traj),
# step=lyap.step, time=lyap.time,
# traj_seed=lyap.traj_seed,
# pert_seed=lyap.pert_seed,
# disorder_seed=lyap.disorder_seed,
# disorder=lyap.e_disorder,
# n_steps=lyap.n_steps,
# wells_indices=lyap.wells_indices,
# beta=lyap.beta, W=lyap.W,
# J=lyap.J, N_tuple=lyap.N_tuple,
# energy=lyap.energy, number_of_particles=lyap.number_of_particles,
# eff_nonl=lyap.effective_nonlinearity,
# error_code=lyap.error_code, checksum=lyap.consistency_checksum,
# distance=lyap.distance,
# x=lyap.X, y=lyap.Y, x1=lyap.X1, y1=lyap.Y1,
# lambdas=lyap.lambdas, lambdas_no_regr=lyap.lambdas_no_regr,
# hist2d=lyap.histograms, hist1d=lyap.rho_histograms,
# hist2d1=lyap.histograms1, hist1d1=lyap.rho_histograms1)
# plt.savefig(vis.HOMEDIR + 'pics/Lyap_' + unique_id + '_' + str(my_id)+'.png', format='png', dpi=100)
print "Error code: ", lyap.error_code
print "\n\nChecksum: ", lyap.consistency_checksum
np.savez_compressed(vis.filename(my_id),
lambdas=lmbdas, lambdas_no_regr=lmbdas_no_regr,
eff_nonl=effective_nonlinearity,
numb_of_part=numb_of_part, energies=energies,
chosen=chosen_trajs, step=lyap.step, time=lyap.time, n_steps=lyap.n_steps,
my_info=[seed_from, seed_to, my_id], needed_trajs=needed_trajs,
checksum=lyap.consistency_checksum, error_code=lyap.error_code,
distance=distances)