def mesh(geometry, element):

    Lx = 100.
    Ly = 100.
    H = 100.

    deltax = Lx / 5.
    deltay = Ly / 5.
    deltaz = H / 5.
    ncolumnsy = int(Ly/deltay)
    ncolumnsx = int(Lx/deltax)
    nlayers = int(H/deltaz)

    quadrilateral = True if element == "quadrilateral" else False

    if geometry == "periodic-in-both":
        m = PeriodicRectangleMesh(ncolumnsx, ncolumnsy, Lx, Ly,
                                  direction='both', quadrilateral=quadrilateral)
    elif geometry == "periodic-in-x":
        m = PeriodicRectangleMesh(ncolumnsx, ncolumnsy, Lx, Ly,
                                  direction='x', quadrilateral=quadrilateral)
    elif geometry == "periodic-in-y":
        m = PeriodicRectangleMesh(ncolumnsx, ncolumnsy, Lx, Ly,
                                  direction='y', quadrilateral=quadrilateral)
    elif geometry == "non-periodic":
        m = RectangleMesh(ncolumnsx, ncolumnsy, Lx, Ly, quadrilateral=quadrilateral)

    extruded_mesh = ExtrudedMesh(m, layers=nlayers, layer_height=deltaz)

    return extruded_mesh
Example #2
0
def setup_2d_recovery(dirname):

    L = 100.
    W = 100.

    deltax = L / 5.
    deltay = W / 5.
    ncolumnsx = int(L / deltax)
    ncolumnsy = int(W / deltay)

    mesh = PeriodicRectangleMesh(ncolumnsx,
                                 ncolumnsy,
                                 L,
                                 W,
                                 direction='y',
                                 quadrilateral=True)
    x, y = SpatialCoordinate(mesh)

    # spaces
    VDG0 = FunctionSpace(mesh, "DG", 0)
    VCG1 = FunctionSpace(mesh, "CG", 1)
    VDG1 = FunctionSpace(mesh, "DG", 1)
    Vu = FunctionSpace(mesh, "RTCF", 1)
    VuCG1 = VectorFunctionSpace(mesh, "CG", 1)
    VuDG1 = VectorFunctionSpace(mesh, "DG", 1)

    # set up initial conditions
    np.random.seed(0)
    expr = np.random.randn() + np.random.randn() * x

    # our actual theta and rho and v
    rho_CG1_true = Function(VCG1).interpolate(expr)
    v_CG1_true = Function(VuCG1).interpolate(as_vector([expr, expr]))

    # make the initial fields by projecting expressions into the lowest order spaces
    rho_DG0 = Function(VDG0).interpolate(expr)
    rho_CG1 = Function(VCG1)
    v_Vu = Function(Vu).project(as_vector([expr, expr]))
    v_CG1 = Function(VuCG1)

    # make the recoverers and do the recovery
    rho_recoverer = Recoverer(rho_DG0,
                              rho_CG1,
                              VDG=VDG1,
                              boundary_method=Boundary_Method.dynamics)
    v_recoverer = Recoverer(v_Vu,
                            v_CG1,
                            VDG=VuDG1,
                            boundary_method=Boundary_Method.dynamics)

    rho_recoverer.project()
    v_recoverer.project()

    rho_diff = errornorm(rho_CG1, rho_CG1_true) / norm(rho_CG1_true)
    v_diff = errornorm(v_CG1, v_CG1_true) / norm(v_CG1_true)

    return (rho_diff, v_diff)
Example #3
0
def mesh(geometry, element):

    Lx = 100.
    Ly = 100.

    deltax = Lx / 5.
    deltay = Ly / 5.
    ncolumnsy = int(Ly / deltay)
    ncolumnsx = int(Lx / deltax)

    quadrilateral = True if element == "quadrilateral" else False

    if geometry == "periodic-in-both":
        m = PeriodicRectangleMesh(ncolumnsx,
                                  ncolumnsy,
                                  Lx,
                                  Ly,
                                  direction='both',
                                  quadrilateral=quadrilateral)
    elif geometry == "periodic-in-x":
        m = PeriodicRectangleMesh(ncolumnsx,
                                  ncolumnsy,
                                  Lx,
                                  Ly,
                                  direction='x',
                                  quadrilateral=quadrilateral)
    elif geometry == "periodic-in-y":
        m = PeriodicRectangleMesh(ncolumnsx,
                                  ncolumnsy,
                                  Lx,
                                  Ly,
                                  direction='y',
                                  quadrilateral=quadrilateral)
    elif geometry == "non-periodic":
        m = RectangleMesh(ncolumnsx,
                          ncolumnsy,
                          Lx,
                          Ly,
                          quadrilateral=quadrilateral)

    return m
Example #4
0
def setup_gw(dirname):
    nlayers = 10  # horizontal layers
    columns = 30  # number of columns
    L = 1.e5
    m = PeriodicRectangleMesh(columns, 1, L, 1.e4, quadrilateral=True)
    dt = 6.0

    # build volume mesh
    H = 1.0e4  # Height position of the model top
    mesh = ExtrudedMesh(m, layers=nlayers, layer_height=H / nlayers)

    fieldlist = ['u', 'p', 'b']
    timestepping = TimesteppingParameters(dt=dt)
    output = OutputParameters(dirname=dirname + "/gw_incompressible",
                              dumplist=['u'],
                              dumpfreq=5)
    parameters = CompressibleParameters()

    state = State(mesh,
                  vertical_degree=1,
                  horizontal_degree=1,
                  family="RTCF",
                  timestepping=timestepping,
                  output=output,
                  parameters=parameters,
                  fieldlist=fieldlist)

    # Initial conditions
    u0 = state.fields("u")
    p0 = state.fields("p")
    b0 = state.fields("b")

    # z.grad(bref) = N**2
    x, y, z = SpatialCoordinate(mesh)
    N = parameters.N
    bref = z * (N**2)

    b_b = Function(b0.function_space()).interpolate(bref)
    b0.interpolate(b_b)
    incompressible_hydrostatic_balance(state, b0, p0)
    state.initialise([('u', u0), ('p', p0), ('b', b0)])

    # Set up forcing
    forcing = IncompressibleForcing(state)

    return state, forcing
Example #5
0
                       ExtrudedMesh, exp, sin, Function)
import numpy as np
import sys

dt = 25.
if '--running-tests' in sys.argv:
    nlayers = 5  # horizontal layers
    columns = 50  # number of columns
    tmax = dt
else:
    nlayers = 10  # horizontal layers
    columns = 150  # number of columns
    tmax = 60000.0

L = 6.0e6
m = PeriodicRectangleMesh(columns, 1, L, 1.e4, quadrilateral=True)

# build volume mesh
H = 1.0e4  # Height position of the model top
mesh = ExtrudedMesh(m, layers=nlayers, layer_height=H / nlayers)

fieldlist = ['u', 'rho', 'theta']
timestepping = TimesteppingParameters(dt=dt)

dirname = 'sk_hydrostatic'

output = OutputParameters(dirname=dirname,
                          dumpfreq=50,
                          dumplist=['u'],
                          perturbation_fields=['theta', 'rho'],
                          log_level='INFO')
def setup_3d_recovery(dirname):

    L = 3.
    H = 3.
    W = 3.

    deltax = L / 3.
    deltay = W / 3.
    deltaz = H / 3.
    nlayers = int(H / deltaz)
    ncolumnsx = int(L / deltax)
    ncolumnsy = int(W / deltay)

    m = PeriodicRectangleMesh(ncolumnsx,
                              ncolumnsy,
                              L,
                              W,
                              direction='y',
                              quadrilateral=True)
    mesh = ExtrudedMesh(m, layers=nlayers, layer_height=H / nlayers)
    x, y, z = SpatialCoordinate(mesh)

    # horizontal base spaces
    cell = mesh._base_mesh.ufl_cell().cellname()
    u_hori = FiniteElement("RTCF", cell, 1)
    w_hori = FiniteElement("DG", cell, 0)

    # vertical base spaces
    u_vert = FiniteElement("DG", interval, 0)
    w_vert = FiniteElement("CG", interval, 1)

    # build elements
    u_element = HDiv(TensorProductElement(u_hori, u_vert))
    w_element = HDiv(TensorProductElement(w_hori, w_vert))
    theta_element = TensorProductElement(w_hori, w_vert)
    v_element = u_element + w_element

    # spaces
    VDG0 = FunctionSpace(mesh, "DG", 0)
    VCG1 = FunctionSpace(mesh, "CG", 1)
    VDG1 = FunctionSpace(mesh, "DG", 1)
    Vt = FunctionSpace(mesh, theta_element)
    Vt_brok = FunctionSpace(mesh, BrokenElement(theta_element))
    Vu = FunctionSpace(mesh, v_element)
    VuCG1 = VectorFunctionSpace(mesh, "CG", 1)
    VuDG1 = VectorFunctionSpace(mesh, "DG", 1)

    # set up initial conditions
    np.random.seed(0)
    expr = np.random.randn() + np.random.randn() * x + np.random.randn(
    ) * z + np.random.randn() * x * z

    # our actual theta and rho and v
    rho_CG1_true = Function(VCG1).interpolate(expr)
    theta_CG1_true = Function(VCG1).interpolate(expr)
    v_CG1_true = Function(VuCG1).interpolate(as_vector([expr, expr, expr]))
    rho_Vt_true = Function(Vt).interpolate(expr)

    # make the initial fields by projecting expressions into the lowest order spaces
    rho_DG0 = Function(VDG0).interpolate(expr)
    rho_CG1 = Function(VCG1)
    theta_Vt = Function(Vt).interpolate(expr)
    theta_CG1 = Function(VCG1)
    v_Vu = Function(Vu).project(as_vector([expr, expr, expr]))
    v_CG1 = Function(VuCG1)
    rho_Vt = Function(Vt)

    # make the recoverers and do the recovery
    rho_recoverer = Recoverer(rho_DG0,
                              rho_CG1,
                              VDG=VDG1,
                              boundary_method=Boundary_Method.dynamics)
    theta_recoverer = Recoverer(theta_Vt,
                                theta_CG1,
                                VDG=VDG1,
                                boundary_method=Boundary_Method.dynamics)
    v_recoverer = Recoverer(v_Vu,
                            v_CG1,
                            VDG=VuDG1,
                            boundary_method=Boundary_Method.dynamics)
    rho_Vt_recoverer = Recoverer(rho_DG0,
                                 rho_Vt,
                                 VDG=Vt_brok,
                                 boundary_method=Boundary_Method.physics)

    rho_recoverer.project()
    theta_recoverer.project()
    v_recoverer.project()
    rho_Vt_recoverer.project()

    rho_diff = errornorm(rho_CG1, rho_CG1_true) / norm(rho_CG1_true)
    theta_diff = errornorm(theta_CG1, theta_CG1_true) / norm(theta_CG1_true)
    v_diff = errornorm(v_CG1, v_CG1_true) / norm(v_CG1_true)
    rho_Vt_diff = errornorm(rho_Vt, rho_Vt_true) / norm(rho_Vt_true)

    return (rho_diff, theta_diff, v_diff, rho_Vt_diff)
plt.savefig("plots/SquareMesh.png")


# -> UnitSquareMesh
# -----------------
# possible use of quadrilateral elements
mesh = UnitSquareMesh(nx, ny, quadrilateral=not quad)
triplot(mesh)
plt.legend()
plt.savefig("plots/UnitSquareMesh.png")


# -> PeriodicRectangleMesh
# ------------------------
# possible use of quadrilateral elements
mesh = PeriodicRectangleMesh(nx+1, ny, Lx, Ly)
triplot(mesh)
plt.legend()
plt.savefig("plots/PeriodicRectangleMesh.png")


# -> PeriodicSquareMesh
# ------------------------
# possible use of quadrilateral elements
mesh = PeriodicSquareMesh(nx, ny, Lx, direction="x")
triplot(mesh)
plt.legend()
plt.savefig("plots/PeriodicSquareMesh.png")


# -> PeriodicUnitSquareMesh