Exemplo n.º 1
0
def grad(nx, ny, nchi, ncho, n, m, xdat, dydat):

    # Image size
    dt = np.float32
    x, y, ci, co = (SpaceDimension("x"), SpaceDimension("y"), Dimension("ci"),
                    Dimension("co"))
    grid = Grid((nx, ny), dtype=dt, dimensions=(x, y))

    # Image
    X = Function(name="xin", dimensions=(ci, x, y),
                 shape=(nchi, nx, ny), grid=grid, space_order=n//2)

    # Output
    dy = Function(name="dy", dimensions=(co, x, y),
                  shape=(ncho, nx, ny), grid=grid, space_order=n//2)

    # Weights
    i, j = Dimension("i"), Dimension("j")
    dW = Function(name="dW", dimensions=(co, ci, i, j),
                  shape=(ncho, nchi, n, m), grid=grid)

    # Gradient
    grad_eq = Inc(dW[co, ci, i, j], dy[co, x, y]*X[ci, x+i-n//2, y+j-m//2])
    op = Operator(grad_eq)
    op.cfunction

    X.data[:] = xdat[:]
    dy.data[:] = dydat[:]

    return op
Exemplo n.º 2
0
    def test_collapsing_v2(self):
        """
        MFE from issue #1478.
        """
        n = 8
        m = 8
        nx, ny, nchi, ncho = 12, 12, 1, 1
        x, y = SpaceDimension("x"), SpaceDimension("y")
        ci, co = Dimension("ci"), Dimension("co")
        i, j = Dimension("i"), Dimension("j")
        grid = Grid((nx, ny), dtype=np.float32, dimensions=(x, y))

        X = Function(name="xin", dimensions=(ci, x, y),
                     shape=(nchi, nx, ny), grid=grid, space_order=n//2)
        dy = Function(name="dy", dimensions=(co, x, y),
                      shape=(ncho, nx, ny), grid=grid, space_order=n//2)
        dW = Function(name="dW", dimensions=(co, ci, i, j), shape=(ncho, nchi, n, m),
                      grid=grid)

        eq = [Eq(dW[co, ci, i, j],
                 dW[co, ci, i, j] + dy[co, x, y]*X[ci, x+i-n//2, y+j-m//2])
              for i in range(n) for j in range(m)]

        op = Operator(eq, opt=('advanced', {'openmp': True}))

        iterations = FindNodes(Iteration).visit(op)
        assert len(iterations) == 4
        assert iterations[0].ncollapsed == 1
        assert iterations[1].is_Vectorized
        assert iterations[2].is_Sequential
        assert iterations[3].is_Sequential
def conv(nx, ny, nchi, ncho, n, m):

    # Image size
    dt = np.float32
    x, y, ci, co = (SpaceDimension("x"), SpaceDimension("y"), Dimension("ci"),
                    Dimension("co"))
    grid = Grid((nchi, ncho, nx, ny), dtype=dt, dimensions=(ci, co, x, y))

    # Image
    im_in = Function(name="imi",
                     dimensions=(ci, x, y),
                     shape=(nchi, nx, ny),
                     grid=grid,
                     space_order=n // 2)

    # Output
    im_out = Function(name="imo",
                      dimensions=(co, x, y),
                      shape=(ncho, nx, ny),
                      grid=grid,
                      space_order=n // 2)

    # Weights
    i, j = Dimension("i"), Dimension("j")
    W = Function(name="W",
                 dimensions=(co, ci, i, j),
                 shape=(ncho, nchi, n, m),
                 grid=grid)

    # Popuate weights with deterministic values
    for i in range(ncho):
        for j in range(nchi):
            W.data[i, j, :, :] = np.linspace(i + j, i + j + (n * m),
                                             n * m).reshape(n, m)

    # Convlution
    conv = [
        Eq(
            im_out, im_out + sum([
                W[co, ci, i2, i1] * im_in[ci, x + i1 - n // 2, y + i2 - m // 2]
                for i1 in range(n) for i2 in range(m)
            ]))
    ]

    op = Operator(conv)
    op.cfunction

    # Initialize the input/output
    input_data = np.linspace(-1, 1, nx * ny * nchi).reshape(nchi, nx, ny)
    im_in.data[:] = input_data.astype(np.float32)
    im_out.data

    return op
Exemplo n.º 4
0
def dims():
    return {'i': SpaceDimension(name='i'),
            'j': SpaceDimension(name='j'),
            'k': SpaceDimension(name='k'),
            'l': SpaceDimension(name='l'),
            's': SpaceDimension(name='s'),
            'q': SpaceDimension(name='q')}
Exemplo n.º 5
0
def default_setup_iso(npad,
                      shape,
                      dtype,
                      sigma=0,
                      qmin=0.1,
                      qmax=100.0,
                      tmin=0.0,
                      tmax=2000.0,
                      bvalue=1.0 / 1000.0,
                      vvalue=1.5,
                      space_order=8):
    """
    For isotropic propagator build default model with 10m spacing,
        and 1.5 m/msec velocity

    Return:
        dictionary of velocity, buoyancy, and wOverQ
        TimeAxis defining temporal sampling
        Source locations: one located at center of model, z = 1dz
        Receiver locations, one per interior grid intersection, z = 2dz
            2D: 1D grid of receivers center z covering interior of model
            3D: 2D grid of receivers center z covering interior of model
    """
    d = 10.0
    origin = tuple([0.0 - d * npad for s in shape])
    extent = tuple([d * (s - 1) for s in shape])

    # Define dimensions
    if len(shape) == 2:
        x = SpaceDimension(name='x', spacing=Constant(name='h_x', value=d))
        z = SpaceDimension(name='z', spacing=Constant(name='h_z', value=d))
        grid = Grid(extent=extent,
                    shape=shape,
                    origin=origin,
                    dimensions=(x, z),
                    dtype=dtype)
    else:
        x = SpaceDimension(name='x', spacing=Constant(name='h_x', value=d))
        y = SpaceDimension(name='y', spacing=Constant(name='h_y', value=d))
        z = SpaceDimension(name='z', spacing=Constant(name='h_z', value=d))
        grid = Grid(extent=extent,
                    shape=shape,
                    origin=origin,
                    dimensions=(x, y, z),
                    dtype=dtype)

    b = Function(name='b', grid=grid, space_order=space_order)
    v = Function(name='v', grid=grid, space_order=space_order)
    b.data[:] = bvalue
    v.data[:] = vvalue

    dt = dtype("%.6f" % (0.8 * compute_critical_dt(v)))
    time_axis = TimeAxis(start=tmin, stop=tmax, step=dt)

    # Define coordinates in 2D and 3D
    if len(shape) == 2:
        nr = shape[0] - 2 * npad
        src_coords = np.empty((1, len(shape)), dtype=dtype)
        rec_coords = np.empty((nr, len(shape)), dtype=dtype)
        src_coords[:, 0] = origin[0] + extent[0] / 2
        src_coords[:, 1] = 1 * d
        rec_coords[:, 0] = np.linspace(0.0, d * (nr - 1), nr)
        rec_coords[:, 1] = 2 * d
    else:
        # using numpy outer product here for array iteration speed
        xx, yy = np.ogrid[:shape[0] - 2 * npad, :shape[1] - 2 * npad]
        x1 = np.ones((shape[0] - 2 * npad, 1))
        y1 = np.ones((1, shape[1] - 2 * npad))
        xcoord = (xx * y1).reshape(-1)
        ycoord = (x1 * yy).reshape(-1)
        nr = len(xcoord)
        src_coords = np.empty((1, len(shape)), dtype=dtype)
        rec_coords = np.empty((nr, len(shape)), dtype=dtype)
        src_coords[:, 0] = origin[0] + extent[0] / 2
        src_coords[:, 1] = origin[1] + extent[1] / 2
        src_coords[:, 2] = 1 * d
        rec_coords[:, 0] = d * xcoord
        rec_coords[:, 1] = d * ycoord
        rec_coords[:, 2] = 2 * d

    return b, v, time_axis, src_coords, rec_coords
import matplotlib.pyplot as plt
from devito import TimeFunction, Grid, SpaceDimension, Operator, Constant, Eq
from sympy import solve
from numpy import sin, cos, pi, linspace, shape, clip
from scipy.integrate import quad

# Global constants
L = 10.  # Define length of domain as a global variable
k = 100  # Number of terms in the Fourier sine series
l = 4001  # Define number of points in domain

extent = (L, )  # Grid is L long with l grid points
shape = (l, )

x = SpaceDimension(name='x',
                   spacing=Constant(name='h_x',
                                    value=extent[0] / (shape[0] - 1)))
grid = Grid(extent=extent, shape=shape, dimensions=(x, ))

# Set up optimized FD parameters
so_opt = 4  # 6th order accurate in space
to_opt = 2  # 2nd order accurate in time
time = grid.time_dim
h_x = grid.spacing[0]

# dt is defined using Courant condition (c = 1)
dt = 0.5 * (L / (shape[0] - 1))  # Timestep is half critical dt (0.0025)
t_end = L  # Standing wave will cycle twice(?) in time L
ns = int(t_end / dt)  # Number of timesteps = total time/timestep size

# Set up function and stencil
Exemplo n.º 7
0
@pytest.fixture(scope="session")
def dims():
    return {
        'i': Dimension(name='i'),
        'j': Dimension(name='j'),
        'k': Dimension(name='k'),
        'l': Dimension(name='l'),
        's': Dimension(name='s'),
        'q': Dimension(name='q')
    }


# Testing dimensions for space and time
time = TimeDimension('time', spacing=Constant(name='dt'))
t = SteppingDimension('t', parent=time)
x = SpaceDimension('x', spacing=Constant(name='h_x'))
y = SpaceDimension('y', spacing=Constant(name='h_y'))
z = SpaceDimension('z', spacing=Constant(name='h_z'))


@pytest.fixture(scope="session")
def iters(dims):
    return [
        lambda ex: Iteration(ex, dims['i'], (0, 3, 1)),
        lambda ex: Iteration(ex, dims['j'], (0, 5, 1)),
        lambda ex: Iteration(ex, dims['k'], (0, 7, 1)),
        lambda ex: Iteration(ex, dims['s'], (0, 4, 1)),
        lambda ex: Iteration(ex, dims['q'], (0, 4, 1)),
        lambda ex: Iteration(ex, dims['l'], (0, 6, 1)),
        lambda ex: Iteration(ex, x, (0, 5, 1)),
        lambda ex: Iteration(ex, y, (0, 7, 1))