Exemplo n.º 1
0
def frontogenesis_timestep(N, days, tstepsize, Heun=None):
    tf = 60 * 60 * 24 * days  #final time

    #initialise parameters from SG Eady model
    H = 1.e4
    L = 1.e6
    g = 10.
    f = 1.e-4
    theta0 = 300.

    #create new directory to store results
    if Heun:
        newdir = "/scratchcomp04/cvr12/Results_" + str(days) + "D_" + str(
            N) + "N_" + str(int(tstepsize)) + "_heun"
    else:
        newdir = "/scratchcomp04/cvr12/Results_" + str(days) + "D_" + str(
            N) + "N_" + str(int(tstepsize)) + "_euler"

    os.mkdir(newdir)

    #initialise source density with periodic BCs in x
    bbox = np.array([-L, 0., L, H])
    Xdens = pdx.sample_rectangle(bbox)
    f0 = np.ones(4) / (2 * H * L)
    rho = np.zeros(Xdens.shape[0])
    T = ma.delaunay_2(Xdens, rho)
    dens = pdx.Periodic_density_in_x(Xdens, f0, T, bbox)

    #initialise points in geostrophic space
    [Y, thetap] = initialise_points(N, bbox, RegularMesh=True)
    Y = dens.to_fundamental_domain(Y)

    if Heun:
        #timestep using Heun's Method
        [Y, w, t] = heun_sg(Y,
                            dens,
                            tf,
                            bbox,
                            newdir,
                            h=tstepsize,
                            add_data=True)
        t.tofile(newdir + '/time.txt', sep=" ", format="%s")

    else:
        #timestep using forward euler scheme
        [Y, w, t] = forward_euler_sg(Y,
                                     dens,
                                     tf,
                                     bbox,
                                     newdir,
                                     h=tstepsize,
                                     add_data=True)
        t.tofile(newdir + '/time.txt', sep=" ", format="%s")

    return ('complete results ' + str(days) + 'D_' + str(N) + 'N_' +
            str(int(tstepsize)))
Exemplo n.º 2
0
import matplotlib
matplotlib.use('Agg')

import numpy as np
from periodic_densities import Periodic_density_in_x, sample_rectangle, periodicinx_draw_laguerre_cells_2
import MongeAmpere as ma
import matplotlib.pyplot as plt
import matplotlib.tri as tri

# source: uniform measure on the square with sidelength 1
bbox = [0., 0., 1., 1.]
Xdens = sample_rectangle(bbox)
f0 = np.ones(4)
rho = np.zeros(Xdens.shape[0])
T = ma.delaunay_2(Xdens, rho)
dens = Periodic_density_in_x(Xdens, f0, T, bbox)
print "mass=%g" % dens.mass()

# target is a random set of points, with random weights
N = 6
Y = np.random.rand(N, 2)
nu = np.ones(N)
nu = (dens.mass() / np.sum(nu)) * nu

print "mass(nu) = %f" % sum(nu)
print "mass(mu) = %f" % dens.mass()

w = ma.optimal_transport_2(dens, Y, nu)

#x,y = periodicinx_draw_laguerre_cells_2(dens,Y,w)
Exemplo n.º 3
0
import matplotlib
matplotlib.use('Agg')

import numpy as np
import periodic_densities as pdx
import MongeAmpere as ma
import matplotlib.pyplot as plt
import matplotlib.tri as tri
from PIL import Image

N = 10000
L = 1.e6
H = 1.e4

bbox = np.array([-L, 0., L, H])
Xdens = pdx.sample_rectangle(bbox)
f0 = np.ones(4) / (2 * L * H)
rho = np.zeros(Xdens.shape[0])
T = ma.delaunay_2(Xdens, rho)
dens = pdx.Periodic_density_in_x(Xdens, f0, T, bbox)

Y = np.random.rand(N, 2)
xY[:, 0] = Y[:, 0] * 2 * L - L
Y[:, 1] *= H
#print(Y)

print(Y.shape)
nu = np.ones(Y[:, 0].size)
nu = (dens.mass() / np.sum(nu)) * nu

w = ma.optimal_transport_2(dens, Y, nu, verbose=True)
Exemplo n.º 4
0
def validity_analysis_results(N, days, tstepsize, t0=0., Heun=None):
    #initialise parameters
    H = 1.e4
    L = 1.e6
    g = 10.
    f = 1.e-4
    theta0 = 300
    C = 3e-6

    if Heun:
        print('heun')
        datadir = "/scratchcomp04/cvr12/Results_" + str(days) + "D_" + str(
            N) + "N_" + str(int(tstepsize)) + "_heun/data"
        resultdir = "/scratchcomp04/cvr12/Results_" + str(days) + "D_" + str(
            N) + "N_" + str(int(tstepsize)) + "_heun"
    else:
        print('euler')
        datadir = "/scratchcomp04/cvr12/Results_" + str(days) + "D_" + str(
            N) + "N_" + str(int(tstepsize)) + "_euler/data"
        resultdir = "/scratchcomp04/cvr12/Results_" + str(days) + "D_" + str(
            N) + "N_" + str(int(tstepsize)) + "_euler"

    os.mkdir(datadir)

    #set up uniform density for domain Gamma
    bbox = np.array([-L, 0., L, H])
    Xdens = pdx.sample_rectangle(bbox)
    f0 = np.ones(4) / 2 / L / H
    rho = np.zeros(Xdens.shape[0])
    T = ma.delaunay_2(Xdens, rho)
    dens = pdx.Periodic_density_in_x(Xdens, f0, T, bbox)

    #set final time(t), step size (h) and total number of time steps(N)
    tf = 60 * 60 * 24 * days
    h = tstepsize
    N = int(np.ceil((tf - t0) / h))

    #initialise arrays to store data values
    KEmean = np.zeros(N + 1)
    energy = np.zeros(N + 1)
    vgmax = np.zeros(N + 1)
    PE = np.zeros(N + 1)
    KE = np.zeros(N + 1)
    rmsv = np.zeros(N + 1)

    w = np.fromfile(resultdir + '/weights_results_' + str(int(h)) +
                    '/weights_0.txt',
                    sep=" ")
    ke = np.zeros((int(w.size), 2))
    vg = np.zeros((int(w.size), 2))
    t = np.array([t0 + n * h for n in range(N + 1)])

    for n in range(0, N + 1):
        Y = np.fromfile(resultdir + '/points_results_' + str(int(h)) +
                        '/points_' + str(n) + '.txt',
                        sep=" ")
        w = np.fromfile(resultdir + '/weights_results_' + str(int(h)) +
                        '/weights_' + str(n) + '.txt',
                        sep=" ")
        l = int(w.size)
        Y = Y.reshape((l, 2))

        #calculate centroids and mass of cells
        [Yc, m] = dens.lloyd(Y, w)
        mtile = np.tile(m, (2, 1)).T

        #calculate second moments to find KE and maximum of vg
        [m1, I] = dens.moments(Y, w)

        #find kinetic energy, maximum value of vg and total energy
        ke[:, 0] = 0.5 * f * f * (m * Y[:, 0]**2 - 2 * Y[:, 0] * m1[:, 0] +
                                  I[:, 0])
        vg[:, 0] = f * (Y[:, 0] - Yc[:, 0])

        #map back to fundamental domain
        ke = dens.to_fundamental_domain(ke)
        vg = dens.to_fundamental_domain(vg)

        E = ke[:,
               0] - f * f * Y[:, 1] * m1[:, 1] + 0.5 * f * f * H * Y[:, 1] * m
        pe = -f * f * Y[:, 1] * m1[:, 1] + 0.5 * f * f * H * Y[:, 1] * m

        energy[n] = np.sum(E)
        KE[n] = np.sum(ke[:, 0])
        KEmean[n] = np.sum(ke[:, 0]) * float(l)
        rmsv[n] = np.sqrt(KEmean[n])
        PE[n] = np.sum(pe)
        vgmax[n] = np.amax(vg[:, 0])

        energy.tofile(datadir + '/energy.txt', sep=" ", format="%s")
        KEmean.tofile(datadir + '/KEmean.txt', sep=" ", format="%s")
        vgmax.tofile(datadir + '/vgmax.txt', sep=" ", format="%s")
        t.tofile(datadir + '/time.txt', sep=" ", format="%s")
        PE.tofile(datadir + '/PE.txt', sep=" ", format="%s")
        KE.tofile(datadir + '/KE.txt', sep=" ", format="%s")
        rmsv.tofile(datadir + '/rmsv.txt', sep=" ", format="%s")

    return ('complete results ' + str(days) + 'D_' + str(N) + 'N_' +
            str(int(tstepsize)))
Exemplo n.º 5
0
def initialise_points(N, bbox, RegularMesh = None):
    '''Function to initialise a mesh over the domain [-L,L]x[0,H]
    and transform to geostrophic coordinates

    args:
    N: number of grid points in z direct, total number of points
      will be 2*N*N

    RegularMesh: controls whether the mesh is regular or an optimised
                 sample of random points in the domain 

    returns:
    
    A numpy array of coordinates of points in geostrophic space
    '''
    H = bbox[3]
    L = bbox[2]

    #set up regular mesh or optimised sample of random points
    if RegularMesh:
        npts = N
        dx = 1./npts
        s = np.arange(dx*0.5,1.,dx)
        s1 = np.arange(-1. + dx*0.5,1.,dx)
        x,z = np.meshgrid(s1*L,s*H)
        nx = x.size
        x = np.reshape(x,(nx,))
        z = np.reshape(z,(nx,))
        y = np.array([x,z]).T
        
    else:
        npts = 2*N*N
        bbox = np.array([0., -0.5, 2., 0.5])
        Xdens = sample_rectangle(bbox);
        f0 = np.ones(4)/2;
        w = np.zeros(Xdens.shape[0]); 
        T = ma.delaunay_2(Xdens,w);
        Sdens = Periodic_density_in_x(Xdens,f0,T,bbox)
        X = ma.optimized_sampling_2(Sdens,npts,niter=5)
        X = Sdens.to_fundamental_domain(X)
        x = X[:,0]*L - L
        z = (X[:,1]+0.5)*H
        y = np.array([x,z]).T
    
    #define thetap from CULLEN 2006
    g = 10.
    f = 1.e-4
    theta0 = 300.
    C = 3e-6
    CP = 0.1
    Nsq = 40/H*g/theta0

    buoyancy_stratification = (z-H/2)*Nsq # b = g*theta/theta_0
    thetap = buoyancy_stratification/g*theta0 + CP*np.sin(np.pi*(x/L + z/H))

    X = x
    Z = g*thetap/theta0/f/f

    Y = np.array([X,Z]).T

    return Y, thetap
Exemplo n.º 6
0
def initialise_points(N, bbox, RegularMesh=None):
    '''Function to initialise a mesh over the domain [-L,L]x[0,H]
    and transform to geostrophic coordinates

    args:
    N: number of grid points in z direct, total number of points
      will be 2*N*N

    RegularMesh: controls whether the mesh is regular or an optimised
                 sample of random points in the domain 

    returns:
    
    A numpy array of coordinates of points in geostrophic space
    '''
    H = bbox[3]
    L = bbox[2]

    #set up regular mesh or optimised sample of random points
    if RegularMesh:
        npts = N
        dx = 1. / npts
        s = np.arange(dx * 0.5, 1., dx)
        s1 = np.arange(-1. + dx * 0.5, 1., dx)
        x, z = np.meshgrid(s1 * L, s * H)
        nx = x.size
        x = np.reshape(x, (nx, ))
        z = np.reshape(z, (nx, ))
        y = np.array([x, z]).T

    else:
        npts = 2 * N * N
        bbox = np.array([0., -0.5, 2., 0.5])
        Xdens = sample_rectangle(bbox)
        f0 = np.ones(4) / 2
        w = np.zeros(Xdens.shape[0])
        T = ma.delaunay_2(Xdens, w)
        Sdens = Periodic_density_in_x(Xdens, f0, T, bbox)
        X = ma.optimized_sampling_2(Sdens, npts, niter=5)
        x = X[:, 0] * L - L
        z = (X[:, 1] + 0.5) * H
        y = np.array([x, z]).T

    #define thetap from CULLEN 2006
    Nsq = 2.5e-5
    g = 10.
    f = 1.e-4
    theta0 = 300.
    C = 3e-6
    B = 0.25

    thetap = Nsq * theta0 * z / g + B * np.sin(np.pi * (x / L + z / H))
    vg = B * g * H / L / f / theta0 * np.sin(
        np.pi *
        (x / L + z / H)) - 2 * B * g * H / np.pi / L / f / theta0 * np.cos(
            np.pi * x / L)

    X = vg / f + x
    Z = g * thetap / f / f / theta0

    Y = np.array([X, Z]).T
    thetap = f * f * theta0 * Z / g

    return Y, thetap
Exemplo n.º 7
0
def validity_analysis_results(N, days, tstepsize, t0 = 0., Heun = None):
    #initialise parameters
    H = 1.e4
    L = 1.e6
    g = 10.
    f = 1.e-4
    theta0 = 300
    C = 3e-6

    if Heun:
        print('heun')
        datadir = "Results_"+str(days)+"D_"+str(N)+"N_"+str(int(tstepsize))+"_heun/data"
        resultdir = "Results_"+str(days)+"D_"+str(N)+"N_"+str(int(tstepsize))+"_heun"
        plotsdir = "Results_"+str(days)+"D_"+str(N)+"N_"+str(int(tstepsize))+"_heun/plots"
    else:
        print('euler')
        datadir = "Results_"+str(days)+"D_"+str(N)+"N_"+str(int(tstepsize))+"_euler/data"
        resultdir = "Results_"+str(days)+"D_"+str(N)+"N_"+str(int(tstepsize))+"_euler"
        plotsdir = "Results_"+str(days)+"D_"+str(N)+"N_"+str(int(tstepsize))+"_euler/plots"
        
    os.mkdir(datadir)
    os.mkdir(plotsdir)
    
    #set up uniform density for domain Gamma
    bbox = np.array([-L, 0., L, H])
    Xdens = pdx.sample_rectangle(bbox)
    f0 = np.ones(4)/2/L/H
    rho = np.zeros(Xdens.shape[0])
    T = ma.delaunay_2(Xdens,rho)
    dens = pdx.Periodic_density_in_x(Xdens,f0,T,bbox)

    #set up plotting uniform density for domain Gamma
    bbox = np.array([-L, 0., L, H])
    Xdens = pdx.sample_rectangle(bbox)
    npts = 100
    Xrnd = 2*L*(np.random.rand(npts)-0.5)
    Zrnd = H*np.random.rand(npts)
    Xdens = np.concatenate((Xdens, np.array((Xrnd, Zrnd)).T))
    f0 = np.ones(npts+4)/2/L/H
    rho = np.zeros(Xdens.shape[0])
    T = ma.delaunay_2(Xdens,rho)
    pdens = pdx.Periodic_density_in_x(Xdens,f0,T,bbox)

    #set final time(t), step size (h) and total number of time steps(N)
    tf = 60*60*24*days
    h = tstepsize
    N = int(np.ceil((tf-t0)/h))
    
    #initialise arrays to store data values
    KEmean = np.zeros(N+1)
    energy = np.zeros(N+1)
    vgmax = np.zeros(N+1)
    PE = np.zeros(N+1)
    KE = np.zeros(N+1)
    rmsv = np.zeros(N+1)
    
    w = np.fromfile(resultdir+'/weights_results_'+str(int(h))+'/weights_0.txt', sep = " ")
    ke = np.zeros((int(w.size),2))
    vg = np.zeros((int(w.size),2))
    t = np.array([t0 + n*h for n in range(N+1)])

    Ndump = 100
    
    for n in range(0,N+1, Ndump):
        print(n,N)
        try:
            Y = np.fromfile(resultdir+'/points_results_'+str(int(h))+'/points_'+str(n)+'.txt', sep = " ")
            w = np.fromfile(resultdir+'/weights_results_'+str(int(h))+'/weights_'+str(n)+'.txt', sep = " ")
            l = int(w.size)
            Y = Y.reshape((l,2))
        except IOError:
            break
        
        #Plots
        C = Y[:,1].reshape((l,1))
        print(C.shape, w.shape)
        img = periodic_laguerre_diagram_to_image(pdens,Y,w,C,bbox,100,100)
        plt.pcolor(img)
        plt.savefig(plotsdir+'/c_'+str(n)+'.jpg')

        plt.clf()
        plt.plot(Y[:,0], Y[:,1], '.')
        plt.savefig(plotsdir+'/Y_'+str(n)+'.jpg')
        plt.clf()

        #calculate centroids and mass of cells
        [Yc, m] = dens.lloyd(Y, w)
        mtile = np.tile(m,(2,1)).T

        #calculate second moments to find KE and maximum of vg
        [m1, I] = dens.moments(Y, w)

        #find kinetic energy, maximum value of vg and total energy
        ke[:,0] = 0.5*f*f*(m*Y[:,0]**2 - 2*Y[:,0]*m1[:,0] + I[:,0])
        vg[:,0] = f*(Y[:,0] - Yc[:,0])
        
        #map back to fundamental domain
        ke = dens.to_fundamental_domain(ke)
        vg = dens.to_fundamental_domain(vg)
        
        E = ke[:,0] - f*f*Y[:,1]*m1[:,1] + 0.5*f*f*H*Y[:,1]*m
        pe = - f*f*Y[:,1]*m1[:,1] + 0.5*f*f*H*Y[:,1]*m
        
        energy[n] = np.sum(E)
        KE[n] = np.sum(ke[:,0])
        KEmean[n] = np.sum(ke[:,0])*float(l)
        rmsv[n] = np.sqrt(KEmean[n])
        PE[n] = np.sum(pe)
        vgmax[n] = np.amax(vg[:,0])
        
    energy.tofile(datadir+'/energy.txt',sep = " ",format = "%s")
    KEmean.tofile(datadir+'/KEmean.txt',sep = " ",format = "%s")
    vgmax.tofile(datadir+'/vgmax.txt',sep = " ",format = "%s")
    t.tofile(datadir+'/time.txt',sep = " ",format = "%s")
    PE.tofile(datadir+'/PE.txt',sep = " ",format = "%s")
    KE.tofile(datadir+'/KE.txt',sep = " ",format = "%s")
    rmsv.tofile(datadir+'/rmsv.txt',sep = " ",format = "%s")
        
    return('complete results '+str(days)+'D_'+str(N)+'N_'+str(int(tstepsize)))