uu = []; tt = [] vspec = np.zeros(ks.xspec.shape[1], np.float) x = np.arange(N) fig, ax = plt.subplots() line, = ax.plot(x, ks.x.squeeze()) ax.set_xlim(0,N-1) ax.set_ylim(-3,3) #Init only required for blitting to give a clean slate. def init(): global line line.set_ydata(np.ma.array(x, mask=True)) return line, # spinup for n in range(nmin): ks.advance() def updatefig(n): global tt,uu,vspec ks.advance() vspec += np.abs(ks.xspec.squeeze())**2 u = ks.x.squeeze() line.set_ydata(u) print(n,u.min(),u.max()) uu.append(u); tt.append(n*dt) return line, ani = animation.FuncAnimation(fig, updatefig, np.arange(1,nmax+1), init_func=init, interval=25, blit=True, repeat=False) plt.show()
dt = 0.5; npts = 128 diffusion_truth = 1.0 # for forecast model (same as above for perfect model expt) # for simplicity, assume dt and npts stay the same. #diffusion = 0.9 diffusion = diffusion_truth rstruth = np.random.RandomState(42) # fixed seed for truth run rsens = np.random.RandomState() # varying seed for ob noise and ensemble initial conditions # model instance for truth (nature) run model = KS(N=npts,dt=dt,diffusion=diffusion_truth,rs=rstruth) # mode instance for forecast ensemble ensemble = KS(N=npts,members=nens,dt=dt,diffusion=diffusion,rs=rsens) for nt in range(ntstart): # spinup truth run model.advance() # sample obs from truth, compute climo stats for model. xx = []; tt = [] for nt in range(ntimes): model.advance() xx.append(model.x[0]) # single member tt.append(float(nt)*model.dt) xtruth = np.array(xx,np.float) timetruth = np.array(tt,np.float) xtruth_mean = xtruth.mean() xprime = xtruth - xtruth_mean xvar = np.sum(xprime**2,axis=0)/(ntimes-1) xtruth_stdev = np.sqrt(xvar.mean()) if verbose: print 'climo for truth run:'
uu = []; tt = [] vspec = np.zeros(ks.xspec.shape[1], np.float) x = np.arange(N) fig, ax = plt.subplots() line, = ax.plot(x, ks.x.squeeze()) ax.set_xlim(0,N-1) ax.set_ylim(-3,3) #Init only required for blitting to give a clean slate. def init(): global line line.set_ydata(np.ma.array(x, mask=True)) return line, # spinup for n in range(nmin): ks.advance() def updatefig(n): global tt,uu,vspec ks.advance() vspec += np.abs(ks.xspec.squeeze())**2 u = ks.x.squeeze() line.set_ydata(u) print n,u.min(),u.max() uu.append(u); tt.append(n*dt) return line, ani = animation.FuncAnimation(fig, updatefig, np.arange(1,nmax+1), init_func=init, interval=25, blit=True, repeat=False) plt.show()
from KS import KS, KSAssim import numpy as np """ Author Benjamin Pachev <*****@*****.**> 2020 """ def fourier_projector(spec, modes=21): mod_spec = spec.copy() mod_spec[:, modes:] = 0 return np.fft.irfft(mod_spec, axis=-1) if __name__ == "__main__": #See if the data assimilation works true = KS() assimilator = KSAssim(fourier_projector, mu=1, diffusion=3, update_params=True) max_n = 100 for n in range(max_n): target = fourier_projector(true.xspec) assimilator.set_target(target) assimilator.advance() true.advance() print(assimilator.error(true))
diffusion_truth = 1.0 # for forecast model (same as above for perfect model expt) # for simplicity, assume dt and npts stay the same. #diffusion = 0.9 diffusion = diffusion_truth rstruth = np.random.RandomState(42) # fixed seed for truth run rsens = np.random.RandomState( ) # varying seed for ob noise and ensemble initial conditions # model instance for truth (nature) run model = KS(N=npts, dt=dt, diffusion=diffusion_truth, rs=rstruth) # mode instance for forecast ensemble ensemble = KS(N=npts, members=nens, dt=dt, diffusion=diffusion, rs=rsens) for nt in range(ntstart): # spinup truth run model.advance() # sample obs from truth, compute climo stats for model. xx = [] tt = [] for nt in range(ntimes): model.advance() xx.append(model.x[0]) # single member tt.append(float(nt) * model.dt) xtruth = np.array(xx, np.float) timetruth = np.array(tt, np.float) xtruth_mean = xtruth.mean() xprime = xtruth - xtruth_mean xvar = np.sum(xprime**2, axis=0) / (ntimes - 1) xtruth_stdev = np.sqrt(xvar.mean()) if verbose: