/
qEWode.py
603 lines (477 loc) · 20.8 KB
/
qEWode.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
import scipy.integrate
import numpy as np
import scipy.ndimage
import scipy.optimize
import scipy.sparse
import pylab
import pickle
#import pyfftw
from scipy.sparse.linalg import spsolve
from scipy.linalg import eig, solve
from scipy.integrate import ode, odeint
#
#
# later on: find zeros of velocity with matrix method?
#
class Subject:
def __init__(self):
self._observers = []
def attach(self, observer):
if not observer in self._observers:
self._observers.append(observer)
def detach(self, observer):
try:
self._observers.remove(observer)
except ValueError:
pass
def notify(self, exclude=[]):
for observer in self._observers:
if observer not in exclude: # provides option to exclude some observers from being notified
observer.update(self)
class qEWContinuous(Subject):
def __init__(self,m,N,gamma,sigma,seed):
Subject.__init__(self)
np.random.seed(seed)
self.randmat = sigma*np.random.randn(10*N,N)
self.us = np.zeros(N)
self.m = m
self.w = 0.0
self.length = N
self.VelocityArray = np.zeros(N)
self.lhs = np.zeros((N,N))
self.gamma = gamma
self.current_cs = 0
self.current_as = 0
self.finish_run = False
self.u_traj = 0
def buildMatrix(self):
"""
this will only be called once per simulation?
do i build this directly as a sparse matrix or as a dense matrix then convert
make a self.lhs and self.rhs?
"""
gammaRow = np.zeros(self.length)
gammaRow[1] = self.gamma
gammaRow[-1] = self.gamma
a = gammaRow
for i in range(0,self.length-1):
gammaRow = np.roll(gammaRow,1)
a=np.vstack((a,gammaRow))
c_s, a_s = self.RFSplineCoeffList()
self.current_cs = c_s
self.current_as = a_s
diag_terms = -self.m**2 - 2*self.gamma + c_s
np.fill_diagonal(a,diag_terms)
# now make the dense matrix sparse
a = scipy.sparse.csc_matrix(a) #
# set diagonal of a
self.lhs = a
self.rhs = -self.m**2*self.w - a_s
def doesAvalancheHappen(self):
"""
check dFi/duj matrix (d^2 E/ dui duj)
if any of the eigenvalues are positive
then an avalanche happens
"""
# problem with line below! it is not possible to compute all
# eigenvectors of sparse matrix... need to convert back to dense matrix
# this is probably both time and memory consuming -- think of way around it
denselhs = self.lhs.todense()
evals, evects = eig(denselhs)
self.evals = evals
self.evects = evects
if (evals >= 0).any():
return True
else:
return False
def solveForUs(self):
"""
this is just to solve the equation...
"""
us_solution = spsolve(self.lhs,self.rhs)
return us_solution
def updateAllMatrix(self):
"""
this updates the matrix once something has moved
!!! actually if we keep a list of what has been moved we can update only those diagonal terms!!!
time the two different method to see which one is faster...
"""
c_s, a_s = self.RFSplineCoeffList()
diag_terms = -self.m**2 - 2*self.gamma + c_s
self.lhs.setdiag(diag_terms)
self.rhs = -self.m**2*self.w - a_s
self.current_cs = c_s
self.current_as = a_s
def updatePartialMatrix(self,list_moved):
old_diagonal = self.lhs.diagonal()
for i in list_moved:
c, a = self.RFSplineCoeff(self.us[i],i)
old_diagonal[i] = -self.m**2 - 2*self.gamma + c
self.rhs[i] = -self.m**2*self.w - a
self.current_cs[i] = c
self.current_as[i] = a
self.lhs.setdiag(old_diagonal)
def findFirstMinPush(self):
u_next = np.fix(self.us)+1
# this is assuming that the current configuration is stable?
mwpushes = self.m**2*(u_next-self.us) + 2*self.gamma*(u_next-self.us) -self.current_cs*(u_next-self.us)
#maskedpushes = np.ma.array(mwpushes,mask=(mwpushes>0))
#print maskedpushes
print "local forces:", mwpushes
minIndex = mwpushes.argmin()
mwmin = mwpushes[minIndex]
return mwmin, minIndex
def findW(self,u0=None):
if u0 is None:
# this solves the current
u0 = self.solveForUs()
#print u0
deltau = spsolve(self.lhs,-np.ones(self.length))
#print "from find W deltau= ", deltau
deltaw = ((np.floor(u0)+1.)-u0)/(self.m**2*deltau)
#print "from find W detlaw= ", deltaw
return min(deltaw), deltaw.argmin()
def findMinPush(self):
"""
this is to find the general minimum push given a configuration
"""
mwpush = []
for i in range(0, self.length):
u_next = np.fix(self.us[i])+1
deltaF = self.localVelocity(u_next,i)-self.localVelocity(self.us[i],i)
# the above line can be simplified
mwpush.append(deltaF)
mwpush = np.array(mwpush)
# don't allow backwards motion for now...
maskedpushes = np.ma.array(mwpush,mask = (mwpush>0))
minIndex = maskedpushes.argmin()
return mwpush[minIndex], minIndex
def checkForSitesInAvalanche(self, u_next ,mwpush):
"""
should I evolve all sites with F>0 forward or mwpush?
"""
old_noise = self.RFSplineArray(self.us)
new_noise = self.RFSplineArray(u_next)
deltaF = new_noise - old_noise - self.m**2*(u_next - self.us)-2*self.gamma*(u_next-self.us)
#sitesToPush = (deltaF<=mwpush)
pushedarray = ((deltaF+mwpush)>0)
sitesToPush = np.where(pushedarray)[0]
# returns array of indices in avalanche. advance those forward?
return sitesToPush, pushedarray
def findDeltaW(self):
return -self.VelocityArray.min()/self.m**2.
def findZero(self,x):
# find next zero that will change the sign of the local Velocity
# assume that current velocity is greater than zero
Felharm = self.Felharm(self.us[x],x)
u_int = int(np.fix(self.us[x])+1)
# find next integer that makes the total velocity negative
while Felharm + self.randmat[u_int,x] >= 0:
u_int += 1
# find zero in between current height and current_u_int
print u_int, x
#print self.localVelocity(self.us[x],x)
#print self.localVelocity(u_int,x)
unextzero = scipy.optimize.brentq(self.localVelocity,self.us[x],u_int,args=(x,))
# replace current us[x] with the zero
self.us[x] = unextzero
def increaseW(self,deltaw):
self.w += deltaw
def localVelocity(self,ux,x):
# implementing for periodic boundary conditions for now...
laplacian = (self.us[np.mod(x+1,self.length)]-2*ux+self.us[np.mod(x-1,self.length)])
quenched_noise = self.RFSpline(x,ux)
return self.m**2*(self.w-ux) + self.gamma*laplacian + quenched_noise
def Felharm(self,ux,x):
laplacian = (self.us[x+1]-2*ux+self.us[x-1])
return self.m**2*(self.w-ux) + self.gamma*laplacian
def calculateVelocityArray(self, u_array, tdummy=0.0):
quenched_noise = self.RFSplineArray(u_array)
return self.calculateFharmArray(u_array)+quenched_noise
def odeFunc(self, t, u_array):
return self.calculateVelocityArray(u_array)
def calculateFharmArray(self,u_array):
laplacian = scipy.ndimage.convolve1d(u_array,[1,-2,1],mode='wrap')
return self.m**2*(self.w - u_array) + self.gamma*laplacian
def RFSplineArray(self, u_array):
"""
this returns an array of Random Fields, length of u array
"""
u_lowers = np.fix(u_array)
u_uppers = np.fix(u_array)+1
lower_fields = self.randmat.transpose()[zip(*enumerate(u_lowers))]
upper_fields = self.randmat.transpose()[zip(*enumerate(u_uppers))]
return lower_fields +(u_array-u_lowers)*(upper_fields-lower_fields)
def RFSpline(self,x,u):
"""
u is continuous, find closest two u integers
this is for single x, mainly for finding roots
"""
u_lower = int(np.fix(u))
u_upper = int(np.fix(u))+1
return self.randmat[u_lower,x] + (u-u_lower)*(self.randmat[u_upper,x]-self.randmat[u_lower,x])
def RFSplineCoeffList(self):
u_lowers = np.fix(self.us)
u_uppers = np.fix(self.us)+1 # use np.ceil
lower_fields = self.randmat.transpose()[zip(*enumerate(u_lowers))]
upper_fields = self.randmat.transpose()[zip(*enumerate(u_uppers))]
c_s = upper_fields-lower_fields
a_s = lower_fields - u_lowers*(upper_fields-lower_fields)
return c_s, a_s
def RFSplineCoeff(self,u,x):
u_lower = np.floor(u)
u_upper = np.floor(u)+1
lower_field = self.randmat[u_lower,x]
upper_field = self.randmat[u_upper,x]
c = upper_field-lower_field
a = lower_field - u_lower*(upper_field-lower_field)
return c, a
class storeVelocities:
"""
store velocities of avalanche?
"""
def __init__(self, fileName = "Velocities.txt"):
self.fileObject = open(fileName, "w")
def update(self, subject, sampling_freq = 100):
"""
calculates the average velocity of the front throughout the avalanche at various time points in the avalanche
"""
if not subject.finish_run:
duration, width = np.shape(subject.u_traj)
t_list = np.arange(0,duration,sampling_freq)
average_velocities = [np.average(subject.calculateVelocityArray(subject.u_traj[t])) for t in t_list]
self.store(average_velocities)
else:
self.fileObject.close()
def store(self,velocities):
"""
stores the velocity information
"""
self.fileObject.write(str(velocities)+"\n")
class storeHeights:
"""
store heights at the places where the front stops (so F = 0 configurations)
and where configuration is unstable...
"""
def __init__(self, fileName = "Heights.pkl"):
self.fileObject = open(fileName, "w")
self.fileName = fileName
def update(self,subject):
if not subject.finish_run:
with open(self.fileName,"a") as file:
pickle.dump(subject.us,file)
class storeSolutions:
"""
stores the solutions just before an avalanche.
and the end of avalanche (integration solution and matrix solution)
so sequence goes start, end_int, end_sol
"""
def __init__(self, fileName = "Solutions.pkl"):
self.fileName = fileName
def update(self,subject):
if not subject.finish_run:
with open(self.fileName, "a") as file:
pickle.dump(subject.us,file)
class storeCorrelations:
"""
calculate height height correlations for each solution of the model
"""
def __init__(self, skip, fileName="Correlations.pkl"):
self.fileName = fileName
self.fileObject = open(self.fileName, 'w')
self.correlations = 0
#self.old_heights = np.zeros(len(subject.us))
self.front_counter = 0
self.total_weight = 0
self.skip_fronts = skip
self.update_counter = 0
self.sum_squares = 0
def calculate(self,subject):
# let's calculate the unweighted correlation for now...
#weight = np.sum(subject.us-self.old_heights)
#self.correlations += weight*np.fft.fft(subject.us)
# below is pyfftw code to be used for speed up on ept
#shape_tuple = subject.us.shape
#a = pyfftw.n_byte_align_empty(shape_tuple,16,dtype=np.complex128)
#b = pyfftw.n_byte_align_empty(shape_tuple,16,dtype=np.complex128)
#c = pyfftw.n_byte_align_empty(shape_tuple,16,dtype=np.complex128)
#fft = pyfftw.FFTW(a,b)
#a[:] = subject.us
#fft.execute()
#ifft = pyfftw.FFTW(b*b.conjugate(),c, direction="FFTW_BACKWARD")
#ifft.execute()
us_fft = np.fft.fft(subject.us)
c = np.fft.ifft(us_fft*us_fft.conjugate())
av_corr = (c-np.average(subject.us))/subject.length
self.correlations += av_corr
# for some reason I used to do the negative of this... why???
self.sum_squares += av_corr**2.
self.front_counter += 1
#self.total_weight += weight
def update(self,subject):
self.update_counter += 1
if self.update_counter == self.skip_fronts:
self.correlations = np.zeros(subject.length)
if not subject.finish_run and self.update_counter > self.skip_fronts:
self.calculate(subject) # calculate the correlation each time...
# think about how to store this part...
if subject.finish_run:
self.correlations=self.correlations/self.front_counter
self.sum_squares = self.sum_squares/self.front_counter
error = np.sqrt((self.sum_squares-self.correlations**2.)/(self.front_counter-1.))
pickle.dump([self.correlations, error], self.fileObject)
def main(m,N,gamma,sigma,seed=1):
# establish simulation class
testModel = qEWContinuous(m,N,gamma,sigma,seed)
# attach observers
dir = 'data/'
commonName = '_VODE_m'+str(m) + '_N'+str(N)+'_g'+str(gamma)+"_s"+str(seed)
heightsFile = dir + 'qEWheights' + commonName +'.pkl'
heightsObserver = storeHeights(fileName = heightsFile)
velocitiesFile = dir + 'qEWvelocities' + commonName + '.txt'
velocitiesObserver = storeVelocities(fileName = velocitiesFile)
correlationsFile = dir + 'qEWcorr'+commonName +'.bp'
corrObserver = storeCorrelations(0,fileName = correlationsFile)
solutionsFile = dir + 'qEWsolutions' + commonName + '.pkl'
solutionsObserver = storeSolutions(fileName = solutionsFile)
testModel.attach(heightsObserver)
testModel.attach(velocitiesObserver)
testModel.attach(corrObserver)
testModel.attach(solutionsObserver)
# set initial external force equal to the most negative a_s
# increase external force slowly and solve the differential equation (monitor velocity for when avalanche stops
testModel.VelocityArray = testModel.calculateVelocityArray(testModel.us)
firstW = testModel.findDeltaW() # this finds the most negative a_s
#testModel.buildMatrix()
# put external force equal to most negative velocity
# not optimal -- try pushing less, or more intelligently...
#firstW, argchanged = testModel.findW()
#testModel.increaseW(firstW/2)
# now start integrating forward until front stops (what time points should we use)
# determine
# the integrateFront function integrates outside of the class...
# conisder logic
testModel.u_traj, topReached = integrateFront(testModel, firstW/2, 10.0,1000)
#pylab.figure()
#pylab.plot(u_traj.transpose())
#plotTrajectories(testModel.u_traj)
#plotVelocities(testModel,u_traj)
testModel.us = testModel.u_traj[-1]
testModel.buildMatrix()
#print "testModel.us from main: ", testModel.us
#print testModel.doesAvalancheHappen()
#print testModel.calculateVelocityArray(testModel.us)
#print np.dot(testModel.lhs.todense(),testModel.us) - testModel.rhs
deltaw, argchanged = testModel.findW()
testModel.increaseW(deltaw)
testModel.rhs = testModel.rhs - testModel.m**2*deltaw
# line above should be consolidated in increaseW? (does ODE solver depend on this?)
testModel.us = testModel.solveForUs()
testModel.us[argchanged] = np.ceil(testModel.us[argchanged])
testModel.updatePartialMatrix([argchanged])
#print testModel.doesAvalancheHappen()
print topReached
while not (testModel.us >= (testModel.length*10-1)).any() and topReached is False:
# repeat this portion until end of the system...
if testModel.doesAvalancheHappen():
print "avalanche happened!"
# if avalanche happens, notify observers
testModel.notify() # observers can be skipped...
# if avalanche happens, push front a little bit...
u_traj_new, topReached = integrateFront(testModel,0.001/m**2,10.0,1000)
#plotTrajectories(u_traj_new)
#plotVelocities(testModel,u_traj_new)
if not topReached:
testModel.us = u_traj_new[-1]
testModel.u_traj = u_traj_new
testModel.notify(exclude = [heightsObserver, corrObserver,velocitiesObserver])
testModel.updateAllMatrix()
testModel.us = testModel.solveForUs()
testModel.notify(exclude = [heightsObserver, corrObserver])
else:
testModel.finish_run = True
testModel.notify()
#u_traj = np.concatenate((u_traj,u_traj_new))
else:
print "front creeping"
deltaw, argchanged = testModel.findW()
testModel.increaseW(deltaw)
testModel.rhs = testModel.rhs - testModel.m**2*deltaw
testModel.us = testModel.solveForUs()
testModel.us[argchanged] = np.ceil(testModel.us[argchanged])
testModel.updatePartialMatrix([argchanged])
# bug in front creeping part? doesn't change properly?
# while not testModel.doesAvalancheHappen():
# try:
# u_traj_new = integrateFront(testModel,0.01/m**2,1.0,100)
# #pylab.figure()
# #pylab.plot(u_traj_new.transpose())
# #plotTrajectories(u_traj_new)
# #plotVelocities(testModel,u_traj_new)
# testModel.us = u_traj_new[-1]
# testModel.updateAllMatrix()
# print testModel.us
# print testModel.solveForUs()
# print testModel.doesAvalancheHappen()
# u_traj = np.concatenate((u_traj,u_traj_new))
# now increase W slowly and integrate slowly
# except:
# break
#return testModel, u_traj
def integrateFront(testModel, Wincrements, t_end,time_steps):
# modify integrator so that it integrates in steps
testModel.increaseW(Wincrements)
tarray = np.linspace(0.0,t_end, time_steps)
dt = tarray[1]-tarray[0]
u_traj, topReached = odeIntegrator(testModel.odeFunc,testModel.us,t_end,dt)
#u_traj = odeint(testModel.calculateVelocityArray,testModel.us,tarray)
# NOTE: need more elegant way to stop integration once out of range...
if not topReached:
while np.average(testModel.calculateVelocityArray(u_traj[-1])) > 10**(-5):
print "front still going"
u_traj_new, topReached = odeIntegrator(testModel.odeFunc,u_traj[-1],t_end,dt)
if topReached:
break
else:
u_traj = np.concatenate((u_traj,u_traj_new))
return u_traj, topReached
def odeIntegrator(func, u0, tend, dt):
# figure out if problem is stiff or non-stiff (this decides which method...)
r = ode(func).set_integrator('vode', method = 'bdf')
#r = ode(func).set_integrator('dopri5')
r.set_initial_value(u0, 0.0)
u_traj = u0
topReached = False
while r.successful() and r.t < tend:
try:
r.integrate(r.t+dt)
u_traj = np.vstack((u_traj,np.array(r.y)))
except:
print r.successful()
topReached = True
break
# put in option to only return part of the trajectory later
if not r.successful():
topReached = True
return u_traj, topReached
# Note: should I make another class the interacts with both subject and observer to do plotting?
def plotTrajectories(u_traj):
n = len(u_traj)
ind = np.arange(0,int(n/10)*10,10)
pylab.figure()
pylab.plot(u_traj[ind,:].transpose())
def plotVelocities(testModel, u_traj):
"""
plot a random site's local velocity
also average velocities
"""
duration, width = np.shape(u_traj)
site_to_track = np.random.randint(0,10)
t_list = np.arange(0,duration-1,10)
velocities = [testModel.calculateVelocityArray(u_traj[t])[site_to_track] for t in t_list]
average_velocities = [np.average(testModel.calculateVelocityArray(u_traj[t])) for t in t_list]
pylab.figure()
pylab.plot(velocities, 'r', label='single site velocity')
pylab.plot(average_velocities, 'b', label='average velocity of front')
pylab.legend()
# add functionality to track velocities as a function of external force