def Lorenz96_SDEmodel(Dim=1, n_samp=1, t_max=1000, poly_order=3, basetag='', SavePath='./results/'): """Gives a generative model of a state variable of Lorenz using optimal transport. Args: Dim: number of state variables to be modelled n_samp: sampling interval, original dt=0.1 t_max: the length of time series used for training, max possible is 10000 poly_order: order of polynomial used in transport Returns: saves data from the generative model """ Tag = basetag + 'r' + str(poly_order) + '_t' + str(t_max) + '_ns' + str( n_samp) if not os.path.exists(SavePath): print('create path ...') os.makedirs(SavePath) # We have already computed the time-series # here we only load and subsample the data LorenzData = sio.loadmat('./thehood/Lorenz96Data.mat') x0, t0 = LorenzData['y'], LorenzData['t'] dt = t0[1] - t0[0] n_max = int(t_max / dt) + 1 x0 = x0[:n_max:n_samp, 0:Dim] t0 = t0[:n_max:n_samp] T = tm.compute_transport_map(x0, polynomial_order=poly_order) TMCoeffs(T, polynomial_order=poly_order ) # extract and plot the coefficients (just for display) q0 = T(x0) Oscillator_Params = sm.SystemID_spec_match(q0, dt=dt) t_model, q_model = sm.draw_trajectory_SDEsys(Oscillator_Params, T=10000, dt=0.1) x_model = tm.TMinverse(q_model, T) # trnasform back to the y-space sio.savemat( SavePath + 'LorenzModel_' + Tag[:], { 'x_model': x_model, 'x_train': x0, 't_model': t_model, 'q': q0, 'q_model': q_model, 't_train': t0 })
def Lorenz96_SDEmodel(): """Models a state variable of Lorenz using optimal transport. The code 1- loads the data, 2- computes the transport map to standard normal distiburion, 3- identifies linear stochastic oscillators with same spectra 4- computes some data from oscillators and pull them back under the transport map. """ Dim = 1 n_samp = 1 # sampling interval (dt=0.1) t_max = 1000 poly_order = 3 # degree of polynomial used for transport map Tag = 'r' + str(poly_order) + '_t' + str(t_max) + '_ns' + str(n_samp) SavePath = './results/' if not os.path.exists(SavePath): print('create path ...') os.makedirs(SavePath) # We have already computed the time-series # here we only load and subsample the data LorenzData = sio.loadmat('./thehood/Lorenz96Data.mat') x0, t0 = LorenzData['y'], LorenzData['t'] dt = t0[1] - t0[0] n_max = int(t_max / dt) + 1 x0 = x0[:n_max:n_samp, 0:Dim] t0 = t0[:n_max:n_samp] T = tm.compute_transport_map(x0, polynomial_order=poly_order) TMCoeffs(T, polynomial_order=poly_order ) # extract and plot the coefficients (just for display) q0 = T(x0) Oscillator_Params = sm.SystemID_spec_match(q0, dt=dt) t_model, q_model = sm.draw_trajectory_SDEsys(Oscillator_Params, T=10000, dt=0.1) x_model = tm.TMinverse(q_model, T) # trnasform back to the y-space sio.savemat( SavePath + 'LorenzModel_' + Tag[:], { 'x_model': x_model, 'x_train': x0, 't_model': t_model, 'q': q0, 'q_model': q_model, 't_train': t0 })
def Lorenz96_SDEmodel(): # modeling Lorenz 96 model as a SDE Dim = 1 # how many state variables we use as output n_samp = 1 # sampling interval (dt=0.1) t_max = 1000 # length of data poly_order = 3 # degree of polynomial used for transport map # create path for figures Tag = 'r' + str(poly_order) + '_t' + str(t_max) + '_ns' + str(n_samp) SavePath = './results/' if not os.path.exists(SavePath): print('create path ...') os.makedirs(SavePath) # load and subsample the data LorenzData = sio.loadmat('./thehood/Lorenz96Data.mat') x0, t0 = LorenzData['y'], LorenzData['t'] dt = t0[1] - t0[0] n_max = int(t_max / dt) + 1 x0 = x0[:n_max:n_samp, 0:Dim] t0 = t0[:n_max:n_samp] # compute the transport map T = tm.compute_transport_map(x0, polynomial_order=poly_order) # extract the coefficients (just for display) TMCoeffs(T, polynomial_order=poly_order) # transform the data and find the SDEs q0 = T(x0) Oscillator_Params = sm.SystemID_spec_match(q0, dt=dt) # generate trajectory of those SDEs t_model, q_model = sm.draw_trajectory_SDEsys(Oscillator_Params, T=10000, dt=0.1) # trnasform back to the y-space x_model = tm.TMinverse(q_model, T) # save data sio.savemat( SavePath + 'LorenzModel_' + Tag[:], { 'x_model': x_model, 'x_train': x0, 't_model': t_model, 'q': q0, 'q_model': q_model, 't_train': t0 })
def test_Welch_vs_WKrelation(): # verifying that PSD computed by Welch matches # the analytical formula of Wiener Khinchin relationship # form a linear system (damper and spring) k,b,D = 2.91,1.58,4.60 MySys = sm.Linear_Stochastic_Oscillator(k,b,D) T=10000 N=int(T*100) t4,X4=MySys.GenerateTrajectory(np.array([.1,.1]),T=T,N=N) # subsample and spit out njump=10 t,y = t4[::njump],X4[0,::njump] fs = 1.0/(t[1]-t[0]) # compute psd M,L = 512,1024 omega,phi = Welch_estimator(y,fs=fs,M=M,L=L) plt.figure() plt.plot(omega,phi,label=r'$f_s=$'+str(fs)) # analytic solution plt.plot(omega,Oscillator_Spectrum(k,b,D,omega),'--k',label=r'$2D/|H(\omega)|^2$') plt.legend() plt.xlim(0,5) plt.title(r'Wiener-Khinchin relation $k=$'+str(k)+', $b=$'+str(b)+', $D=$'+str(D)) plt.savefig('spectest.png',dpi=400)
def Cavity_SDEmodeling(): Dim = 10 n_samp, t_max = 10, 2500 poly_order = 2 # data tag Tag = 'production_T' + str(t_max) + '/' print('data tag= ' + Tag) print('Dim=' + str(Dim) + ' n_samp=' + str(n_samp) + ' t_max=' + str(t_max)) ## save path SavePath = './results/' if not os.path.exists(SavePath): os.makedirs(SavePath) # loading the data CavityData = sio.loadmat('./thehood/Cavity_SPOD_small.mat') SPOD_coords, t_coords = CavityData['y'], CavityData['t'] SPOD_coords = np.real(SPOD_coords[0:Dim, :]) SPOD_coords = np.swapaxes(SPOD_coords, 0, 1) n_max = int(t_max / (t_coords[1] - t_coords[0])) y_train = SPOD_coords[:n_max:n_samp, :] # training data t_train = t_coords[:n_max:n_samp] dt = t_train[1] - t_train[0] print('train data size=' + str(y_train.shape)) # form the map and transform data T = tm.compute_transport_map(y_train, polynomial_order=poly_order, MPIsetup=None) q_train = T(y_train) # training data for q # # save the transport map # file_h = open(SavePath+'CTM.dll', 'wb') # dill.dump(T,file_h) # file_h.close() # model the q dynamics Oscillator_Params = sm.SystemID_spec_match(q_train, dt=dt) # generate trajectory of those SDEs t_model, q_model = sm.draw_trajectory_SDEsys(Oscillator_Params, T=10000, dt=dt) # # model data # np.savez(SavePath+'Cavity_Oscillator_Params',Params=Oscillator_Params) # compute inverse print('computing the exact inverse ...') y_model = tm.TMinverse(q_model, T, num_core=20) # save model data for MATLAB sio.savemat( SavePath + 'Cavity_modal_' + Tag[:-1], { 'y_model': y_model, 'y_train': y_train, 't_model': t_model, 't_train': t_train, 'q_model': q_model, 'q_train': q_train }) # compute the poitwsie stat in truth and model PointwiseStats4cavity(y_train, y_model, SPOD_coords, Tag) return Tag[:-1]
def Cavity_SDEmodeling(): """Models a state variable of Lorenz using optimal transport. The code 1- loads the SPOD mode and coordiante data, 2- computes the transport map to standard normal distiburion, 3- identifies linear stochastic oscillators with same spectra 4- computes a trajectory from oscillators and pull them back under the transport map. 5- computes pointwise stats for cavity """ Dim = 10 n_samp, t_max = 10, 2500 poly_order = 2 # data tag Tag = 'production_T' + str(t_max) + '/' print('data tag= ' + Tag) print('Dim=' + str(Dim) + ' n_samp=' + str(n_samp) + ' t_max=' + str(t_max)) ## save path SavePath = './results/' if not os.path.exists(SavePath): os.makedirs(SavePath) # We have already computed the SPOD of cavity # here we only load and subsample the data CavityData = sio.loadmat('./thehood/Cavity_SPOD_small.mat') SPOD_coords, t_coords = CavityData['y'], CavityData['t'] SPOD_coords = np.real(SPOD_coords[0:Dim, :]) SPOD_coords = np.swapaxes(SPOD_coords, 0, 1) n_max = int(t_max / (t_coords[1] - t_coords[0])) y_train = SPOD_coords[:n_max:n_samp, :] # training data t_train = t_coords[:n_max:n_samp] dt = t_train[1] - t_train[0] print('train data size=' + str(y_train.shape)) # form the map and transform data T = tm.compute_transport_map(y_train, polynomial_order=poly_order, MPIsetup=None) q_train = T(y_train) # training data for q # # save the transport map # file_h = open(SavePath+'CTM.dll', 'wb') # dill.dump(T,file_h) # file_h.close() Oscillator_Params = sm.SystemID_spec_match(q_train, dt=dt) # model the q dynamics # generate trajectory of those SDEs t_model, q_model = sm.draw_trajectory_SDEsys(Oscillator_Params, T=10000, dt=dt) # # model data # np.savez(SavePath+'Cavity_Oscillator_Params',Params=Oscillator_Params) # compute inverse print('computing the exact inverse ...') y_model = tm.TMinverse(q_model, T, num_core=20) # save model data for MATLAB sio.savemat( SavePath + 'Cavity_modal_' + Tag[:-1], { 'y_model': y_model, 'y_train': y_train, 't_model': t_model, 't_train': t_train, 'q_model': q_model, 'q_train': q_train }) # compute the poitwsie stat in truth and model PointwiseStats4cavity(y_train, y_model, SPOD_coords, Tag) return Tag[:-1]
def Cavity_SDEmodeling(n_samp: int = 10, t_max: float = 2500, poly_order: int = 2, Dim=10, basetag='production', SavePath='./results/'): """Models a cavity SPOD dynamics using optimal transport and spectral matching. Args: n_samp: sampling rate of time series, original dt =.01 t_max: length of time series, max is 10000 poly_order: order of polynomial used in the transport Dim: how many SPOD coordiantes are modeled basetag: tag describing the putpose, e.g. production, analysis, etc. SavePath: folder path for saving the model from data Returns: the saved-data tag """ # data tag Tag = basetag + '_T' + str(t_max) + '_Dim=' + str(Dim) + '_ns' + str( n_samp) + '_po' + str(poly_order) print('data tag= ' + Tag) print('Dim=' + str(Dim) + ' n_samp=' + str(n_samp) + ' t_max=' + str(t_max)) ## save path if not os.path.exists(SavePath): os.makedirs(SavePath) # We have already computed the SPOD of cavity # here we only load and subsample the data CavityData = sio.loadmat('./thehood/Cavity_SPOD_small.mat') SPOD_coords, t_coords = CavityData['y'], CavityData['t'] SPOD_coords = np.real(SPOD_coords[0:Dim, :]) SPOD_coords = np.swapaxes(SPOD_coords, 0, 1) n_max = int(t_max / (t_coords[1] - t_coords[0])) y_train = SPOD_coords[:n_max:n_samp, :] # training data t_train = t_coords[:n_max:n_samp] dt = t_train[1] - t_train[0] print('training data size=' + str(y_train.shape)) # form the map and transform data T = tm.compute_transport_map(y_train, polynomial_order=poly_order, MPIsetup=None) q_train = T(y_train) # training data for q # # save the transport map # file_h = open(SavePath+'CTM.dll', 'wb') # dill.dump(T,file_h) # file_h.close() # model the q dynamics Oscillator_Params = sm.SystemID_spec_match(q_train, dt=dt) # generate trajectory of those SDEs t_model, q_model = sm.draw_trajectory_SDEsys(Oscillator_Params, T=10000, dt=dt) # # model data # np.savez(SavePath+'Cavity_Oscillator_Params',Params=Oscillator_Params) # compute inverse print('computing the exact inverse ...') y_model = tm.TMinverse(q_model, T, num_core=20) # save model data for MATLAB sio.savemat( SavePath + 'Cavity_modal_' + Tag, { 'y_model': y_model, 'y_train': y_train, 't_model': t_model, 't_train': t_train, 'q_model': q_model, 'q_train': q_train }) # compute the poitwsie stat in truth and model PointwiseStats4cavity(y_train, y_model, SPOD_coords, Tag, SavePath=SavePath) return Tag