def test_eigs(par):
    """Eigenvalues and condition number estimate with ARPACK"""
    # explicit=True
    diag = np.arange(par["nx"], 0,
                     -1) + par["imag"] * np.arange(par["nx"], 0, -1)
    Op = MatrixMult(
        np.vstack((np.diag(diag), np.zeros(
            (par["ny"] - par["nx"], par["nx"])))))
    eigs = Op.eigs()
    assert_array_almost_equal(diag[:eigs.size], eigs, decimal=3)

    cond = Op.cond()
    assert_array_almost_equal(np.real(cond), par["nx"], decimal=3)

    # explicit=False
    Op = Diagonal(diag, dtype=par["dtype"])
    if par["ny"] > par["nx"]:
        Op = VStack([Op, Zero(par["ny"] - par["nx"], par["nx"])])
    eigs = Op.eigs()
    assert_array_almost_equal(diag[:eigs.size], eigs, decimal=3)

    # uselobpcg cannot be used for square non-symmetric complex matrices
    if np.iscomplex(Op):
        eigs1 = Op.eigs(uselobpcg=True)
        assert_array_almost_equal(eigs, eigs1, decimal=3)

    cond = Op.cond()
    assert_array_almost_equal(np.real(cond), par["nx"], decimal=3)

    if np.iscomplex(Op):
        cond1 = Op.cond(uselobpcg=True, niter=100)
        assert_array_almost_equal(np.real(cond), np.real(cond1), decimal=3)
def test_dense_skinny(par):
    """Dense matrix representation of skinny matrix"""
    diag = np.arange(par["nx"]) + par["imag"] * np.arange(par["nx"])
    D = np.diag(diag)
    Dop = Diagonal(diag, dtype=par["dtype"])
    Zop = Zero(par["nx"], 3, dtype=par["dtype"])
    Op = HStack([Dop, Zop])
    O = np.hstack((D, np.zeros((par["nx"], 3))))
    assert_array_equal(Op.todense(), O)
Exemple #3
0
def test_Zero(par):
    """Dot-test, forward and adjoint for Zero operator
    """
    Zop = Zero(par['ny'], par['nx'], dtype=par['dtype'])
    assert dottest(Zop, par['ny'], par['nx'])

    x = np.ones(par['nx']) + par['imag']*np.ones(par['nx'])
    y = Zop * x
    x1 = Zop.H*y

    assert_array_almost_equal(y, np.zeros(par['ny']))
    assert_array_almost_equal(x1, np.zeros(par['nx']))
def test_Zero(par):
    """Dot-test, forward and adjoint for Zero operator"""
    np.random.seed(10)
    Zop = Zero(par["ny"], par["nx"], dtype=par["dtype"])
    assert dottest(Zop, par["ny"], par["nx"])

    x = np.ones(par["nx"]) + par["imag"] * np.ones(par["nx"])
    y = Zop * x
    x1 = Zop.H * y

    assert_array_almost_equal(y, np.zeros(par["ny"]))
    assert_array_almost_equal(x1, np.zeros(par["nx"]))
Exemple #5
0
def test_eigs(par):
    """Eigenvalues and condition number estimate with ARPACK
    """
    # explicit=True
    diag = np.arange(par['nx'], 0, -1) +\
           par['imag'] * np.arange(par['nx'], 0, -1)
    Op = MatrixMult(np.vstack((np.diag(diag),
                               np.zeros((par['ny'] - par['nx'], par['nx'])))))
    eigs = Op.eigs()
    assert_array_almost_equal(diag[:eigs.size], eigs, decimal=3)

    cond = Op.cond()
    assert_array_almost_equal(np.real(cond), par['nx'], decimal=3)

    #  explicit=False
    Op = Diagonal(diag, dtype=par['dtype'])
    if par['ny'] > par['nx']:
        Op = VStack([Op, Zero(par['ny'] - par['nx'], par['nx'])])
    eigs = Op.eigs()
    assert_array_almost_equal(diag[:eigs.size], eigs, decimal=3)

    cond = Op.cond()
    assert_array_almost_equal(np.real(cond), par['nx'], decimal=3)
def focusing_wrapper(direct, toff, g0VS, iava1, Rop1, R1op1, Restrop1, iava2,
                     Rop2, R1op2, Restrop2, t):
    nsmooth = 10
    nr = direct.shape[0]
    nsava1 = iava1.shape[0]
    nsava2 = iava2.shape[0]

    nt = t.shape[0]
    dt = t[1] - t[0]

    # window
    directVS_off = direct - toff
    idirectVS_off = np.round(directVS_off / dt).astype(np.int)
    w = np.zeros((nr, nt))
    wi = np.ones((nr, nt))
    for ir in range(nr - 1):
        w[ir, :idirectVS_off[ir]] = 1
    wi = wi - w

    w = np.hstack((np.fliplr(w), w[:, 1:]))
    wi = np.hstack((np.fliplr(wi), wi[:, 1:]))

    if nsmooth > 0:
        smooth = np.ones(nsmooth) / nsmooth
        w = filtfilt(smooth, 1, w)
        wi = filtfilt(smooth, 1, wi)

    # Input focusing function
    fd_plus = np.concatenate((np.fliplr(g0VS.T), np.zeros((nr, nt - 1))),
                             axis=-1)

    # operators
    Wop = Diagonal(w.flatten())
    WSop1 = Diagonal(w[iava1].flatten())
    WSop2 = Diagonal(w[iava2].flatten())
    WiSop1 = Diagonal(wi[iava1].flatten())
    WiSop2 = Diagonal(wi[iava2].flatten())

    Mop1 = VStack([
        HStack([Restrop1, -1 * WSop1 * Rop1]),
        HStack([-1 * WSop1 * R1op1, Restrop1])
    ]) * BlockDiag([Wop, Wop])
    Mop2 = VStack([
        HStack([Restrop2, -1 * WSop2 * Rop2]),
        HStack([-1 * WSop2 * R1op2, Restrop2])
    ]) * BlockDiag([Wop, Wop])
    Mop = VStack([
        HStack([Mop1, Mop1, Zero(Mop1.shape[0], Mop1.shape[1])]),
        HStack([Mop2, Zero(Mop2.shape[0], Mop2.shape[1]), Mop2])
    ])

    Gop1 = VStack(
        [HStack([Restrop1, -1 * Rop1]),
         HStack([-1 * R1op1, Restrop1])])
    Gop2 = VStack(
        [HStack([Restrop2, -1 * Rop2]),
         HStack([-1 * R1op2, Restrop2])])

    d1 = WSop1 * Rop1 * fd_plus.flatten()
    d1 = np.concatenate(
        (d1.reshape(nsava1, 2 * nt - 1), np.zeros((nsava1, 2 * nt - 1))))
    d2 = WSop2 * Rop2 * fd_plus.flatten()
    d2 = np.concatenate(
        (d2.reshape(nsava2, 2 * nt - 1), np.zeros((nsava2, 2 * nt - 1))))

    d = np.concatenate((d1, d2))

    # solve
    comb_f = lsqr(Mop, d.flatten(), iter_lim=10, show=False)[0]
    comb_f = comb_f.reshape(6 * nr, (2 * nt - 1))
    comb_f_tot = comb_f + np.concatenate((np.zeros(
        (nr, 2 * nt - 1)), fd_plus, np.zeros((4 * nr, 2 * nt - 1))))

    f1_1 = comb_f_tot[:2 * nr] + comb_f_tot[2 * nr:4 * nr]
    f1_2 = comb_f_tot[:2 * nr] + comb_f_tot[4 * nr:]

    g_1 = BlockDiag([WiSop1, WiSop1]) * Gop1 * f1_1.flatten()
    g_1 = g_1.reshape(2 * nsava1, (2 * nt - 1))
    g_2 = BlockDiag([WiSop2, WiSop2]) * Gop2 * f1_2.flatten()
    g_2 = g_2.reshape(2 * nsava2, (2 * nt - 1))

    f1_1_minus, f1_1_plus = f1_1[:nr], f1_1[nr:]
    f1_2_minus, f1_2_plus = f1_2[:nr], f1_2[nr:]
    g_1_minus, g_1_plus = -g_1[:nsava1], np.fliplr(g_1[nsava1:])
    g_2_minus, g_2_plus = -g_2[:nsava2], np.fliplr(g_2[nsava2:])

    return f1_1_minus, f1_1_plus, f1_2_minus, f1_2_plus, g_1_minus, g_1_plus, g_2_minus, g_2_plus
def redatuming_wrapper(toff, W, wav, iava1, iava2, Rop1, Rop2, R1op1, R1op2,
                       Restrop1, Restrop2, Sparseop, vsx, vsz, x, z, z_current,
                       nt, dt, nfft, nr, ds, dvsx):

    from scipy.signal import filtfilt

    nava1 = iava1.shape[0]
    nava2 = iava2.shape[0]
    nvsx = vsx.shape[0]

    PUP1 = np.zeros(shape=(nava1, nvsx, nt))
    PDOWN1 = np.zeros(shape=(nava1, nvsx, nt))
    PUP2 = np.zeros(shape=(nava2, nvsx, nt))
    PDOWN2 = np.zeros(shape=(nava2, nvsx, nt))

    for ix in range(nvsx):
        s = '####### Point ' + str(ix + 1) + ' of ' + str(
            nvsx) + ' of current line (z = ' + str(z_current) + ', x = ' + str(
                vsx[ix]) + ')'
        print(s)

        # direct wave
        direct = np.loadtxt(path0 + 'Traveltimes/trav_x' + str(vsx[ix]) +
                            '_z' + str(z_current) + '.dat',
                            delimiter=',')
        f = 2 * np.pi * np.arange(nfft) / (dt * nfft)
        g0VS = np.zeros((nfft, nr), dtype=np.complex128)
        for it in range(len(W)):
            g0VS[it] = W[it] * f[it] * hankel2(0, f[it] * direct + 1e-10) / 4
        g0VS = np.fft.irfft(g0VS, nfft, axis=0) / dt
        g0VS = np.real(g0VS[:nt])

        nr = direct.shape[0]
        nsava1 = iava1.shape[0]
        nsava2 = iava2.shape[0]

        # window
        directVS_off = direct - toff
        idirectVS_off = np.round(directVS_off / dt).astype(np.int)
        w = np.zeros((nr, nt))
        wi = np.ones((nr, nt))
        for ir in range(nr - 1):
            w[ir, :idirectVS_off[ir]] = 1
        wi = wi - w

        w = np.hstack((np.fliplr(w), w[:, 1:]))
        wi = np.hstack((np.fliplr(wi), wi[:, 1:]))

        # smoothing
        nsmooth = 10
        if nsmooth > 0:
            smooth = np.ones(nsmooth) / nsmooth
            w = filtfilt(smooth, 1, w)
            wi = filtfilt(smooth, 1, wi)

        # Input focusing function
        fd_plus = np.concatenate((np.fliplr(g0VS.T), np.zeros((nr, nt - 1))),
                                 axis=-1)

        # operators
        Wop = Diagonal(w.flatten())
        WSop1 = Diagonal(w[iava1].flatten())
        WSop2 = Diagonal(w[iava2].flatten())
        WiSop1 = Diagonal(wi[iava1].flatten())
        WiSop2 = Diagonal(wi[iava2].flatten())

        Mop1 = VStack([
            HStack([Restrop1, -1 * WSop1 * Rop1]),
            HStack([-1 * WSop1 * R1op1, Restrop1])
        ]) * BlockDiag([Wop, Wop])
        Mop2 = VStack([
            HStack([Restrop2, -1 * WSop2 * Rop2]),
            HStack([-1 * WSop2 * R1op2, Restrop2])
        ]) * BlockDiag([Wop, Wop])
        Mop = VStack([
            HStack([Mop1, Mop1, Zero(Mop1.shape[0], Mop1.shape[1])]),
            HStack([Mop2, Zero(Mop2.shape[0], Mop2.shape[1]), Mop2])
        ])

        Mop_radon = Mop * Sparseop

        Gop1 = VStack(
            [HStack([Restrop1, -1 * Rop1]),
             HStack([-1 * R1op1, Restrop1])])
        Gop2 = VStack(
            [HStack([Restrop2, -1 * Rop2]),
             HStack([-1 * R1op2, Restrop2])])

        d1 = WSop1 * Rop1 * fd_plus.flatten()
        d1 = np.concatenate(
            (d1.reshape(nsava1, 2 * nt - 1), np.zeros((nsava1, 2 * nt - 1))))
        d2 = WSop2 * Rop2 * fd_plus.flatten()
        d2 = np.concatenate(
            (d2.reshape(nsava2, 2 * nt - 1), np.zeros((nsava2, 2 * nt - 1))))

        d = np.concatenate((d1, d2))

        # solve with SPGL1
        comb_f = SPGL1(Mop_radon,
                       d.flatten(),
                       sigma=1e-5,
                       iter_lim=30,
                       opt_tol=0.05,
                       dec_tol=0.05,
                       verbosity=1)[0]

        # alternatively solve with FISTA
        #comb_f = FISTA(Mop_radon, d.flatten(), eps=1e-1, niter=200,
        #           alpha=2.129944e-04, eigsiter=4, eigstol=1e-3,
        #           tol=1e-2, returninfo=False, show=True)[0]

        # alternatively solve with LSQR
        #comb_f = lsqr(Mop_radon, d.flatten(), iter_lim=100, show=True)[0]

        comb_f = Sparseop * comb_f
        comb_f = comb_f.reshape(6 * nr, (2 * nt - 1))
        comb_f_tot = comb_f + np.concatenate((np.zeros(
            (nr, 2 * nt - 1)), fd_plus, np.zeros((4 * nr, 2 * nt - 1))))

        f1_1 = comb_f_tot[:2 * nr] + comb_f_tot[2 * nr:4 * nr]
        f1_2 = comb_f_tot[:2 * nr] + comb_f_tot[4 * nr:]

        g_1 = BlockDiag([WiSop1, WiSop1]) * Gop1 * f1_1.flatten()
        g_1 = g_1.reshape(2 * nsava1, (2 * nt - 1))
        g_2 = BlockDiag([WiSop2, WiSop2]) * Gop2 * f1_2.flatten()
        g_2 = g_2.reshape(2 * nsava2, (2 * nt - 1))

        #f1_1_minus, f1_1_plus =  f1_1[:nr], f1_1[nr:]
        #f1_2_minus, f1_2_plus =  f1_2[:nr], f1_2[nr:]
        g_1_minus, g_1_plus = -g_1[:nsava1], np.fliplr(g_1[nsava1:])
        g_2_minus, g_2_plus = -g_2[:nsava2], np.fliplr(g_2[nsava2:])

        PUP1[:, ix, :] = g_1_minus[:, nt - 1:]
        PDOWN1[:, ix, :] = g_1_plus[:, nt - 1:]
        PUP2[:, ix, :] = g_2_minus[:, nt - 1:]
        PDOWN2[:, ix, :] = g_2_plus[:, nt - 1:]

    # calculate and save redatumed wavefields (line-by-line)
    jt = 2
    redatumed1 = MDD(PDOWN1[:, :, ::jt],
                     PUP1[:, :, ::jt],
                     dt=jt * dt,
                     dr=dvsx,
                     wav=wav[::jt],
                     twosided=True,
                     adjoint=False,
                     psf=False,
                     dtype='complex64',
                     dottest=False,
                     **dict(iter_lim=20, show=0))
    redatumed2 = MDD(PDOWN2[:, :, ::jt],
                     PUP2[:, :, ::jt],
                     dt=jt * dt,
                     dr=dvsx,
                     wav=wav[::jt],
                     twosided=True,
                     adjoint=False,
                     psf=False,
                     dtype='complex64',
                     dottest=False,
                     **dict(iter_lim=20, show=0))

    np.savetxt(path_save + 'Line1_' + str(z_current) + '.dat',
               np.diag(redatumed1[:, :, (nt + 1) // jt - 1]),
               delimiter=',')
    np.savetxt(path_save + 'Line2_' + str(z_current) + '.dat',
               np.diag(redatumed2[:, :, (nt + 1) // jt - 1]),
               delimiter=',')

    # calculate and save angle gathers (line-by-line)
    vel_sm = np.loadtxt(path0 + 'vel_sm.dat', delimiter=',')
    cp = vel_sm[find_closest(z_current, z), 751 // 2]

    irA = np.asarray([7, 15, 24, 35])
    nalpha = 201
    A1 = np.zeros((nalpha, len(irA)))
    A2 = np.zeros((nalpha, len(irA)))

    for i in np.arange(0, len(irA)):
        ir = irA[i]
        anglegath1, alpha = AngleGather(np.swapaxes(redatumed1, 0, 2), nvsx,
                                        nalpha, dt * jt, ds, ir, cp)
        anglegath2, alpha = AngleGather(np.swapaxes(redatumed2, 0, 2), nvsx,
                                        nalpha, dt * jt, ds, ir, cp)
        A1[:, i] = anglegath1
        A2[:, i] = anglegath2

    np.savetxt(path_save + 'AngleGather1_' + str(z_current) + '.dat',
               A1,
               delimiter=',')
    np.savetxt(path_save + 'AngleGather2_' + str(z_current) + '.dat',
               A2,
               delimiter=',')