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
        })
Esempio n. 4
0
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]
Esempio n. 6
0
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