def test_squeeze(self):
     x = np.linspace(0, 4 * np.pi, 101)
     h = np.sin(x)
     surface = UniformLineScan(h, 4 * np.pi).scale(2.0)
     surface2 = surface.squeeze()
     self.assertTrue(isinstance(surface2, UniformLineScan))
     np.testing.assert_allclose(surface.heights(), surface2.heights())
def test_window_line_scan():
    nx = 27
    sx = 1.3
    h = np.ones((nx, ))

    t = UniformLineScan(h, physical_sizes=(sx, ), periodic=True).window()
    np.testing.assert_almost_equal(t.heights()[0], 1.0)
    np.testing.assert_almost_equal((t.heights()**2).sum() / nx, 1.0)

    t = UniformLineScan(h, physical_sizes=(sx, ), periodic=False).window()
    np.testing.assert_almost_equal(t.heights()[0], 0.0)
    np.testing.assert_almost_equal((t.heights()**2).sum() / nx, 1.0)

    with pytest.raises(ValueError):
        UniformLineScan(h, physical_sizes=(sx, ),
                        periodic=False).window(direction='y').window_data

    with pytest.raises(ValueError):
        UniformLineScan(h, physical_sizes=(sx, ),
                        periodic=False).window(direction='radial').window_data
    def test_positions_and_heights(self):

        h = np.array((0, 1, 2, 3, 4))

        t = UniformLineScan(h, 4)

        assert_array_equal(t.heights(), h)

        expected_x = np.array((0., 0.8, 1.6, 2.4, 3.2))
        np.testing.assert_allclose(t.positions(), expected_x)

        x2, h2 = t.positions_and_heights()
        np.testing.assert_allclose(x2, expected_x)
        assert_array_equal(h2, h)
def test_lbfgsb_1D(dx, n):
    # test the 1D periodic objective is working

    s = n * dx

    Es = 1.

    substrate = PeriodicFFTElasticHalfSpace((n,), young=Es,
                                            physical_sizes=(s,), fft='serial')

    surface = UniformLineScan(np.cos(2 * np.pi * np.arange(n) / n), (s,))
    system = make_system(substrate, surface)

    offset = 0.005
    lbounds = np.zeros(n)
    bnds = system._reshape_bounds(lbounds, )
    init_gap = np.zeros(n, )  # .flatten()
    disp = init_gap + surface.heights() + offset

    res = optim.minimize(system.primal_objective(offset, gradient=True),
                         disp,
                         method='L-BFGS-B', jac=True,
                         bounds=bnds,
                         options=dict(gtol=1e-5 * Es * surface.rms_slope_from_profile()
                                      * surface.area_per_pt,
                                      ftol=0))

    forces = res.jac
    gap = res.x
    converged = res.success
    assert converged
    if hasattr(res.message, "decode"):
        decoded_message = res.message.decode()
    else:
        decoded_message = res.message

    assert decoded_message == \
        'CONVERGENCE: NORM_OF_PROJECTED_GRADIENT_<=_PGTOL'

    x = np.arange(n) * s / n
    mean_pressure = np.mean(forces) / substrate.area_per_pt

    assert mean_pressure > 0

    pth = mean_pressure * _pressure(
        x / s,
        mean_pressure=s * mean_pressure / Es)

    if False:
        import matplotlib.pyplot as plt
        plt.figure()
        # plt.plot(np.arange(n)*s/n, surface.heights())
        plt.plot(x, gap + (surface.heights()[:] + offset), 'r-')
        plt.plot(x, (surface.heights()[:] + offset), 'k-')
        plt.figure()
        plt.plot(x, forces / substrate.area_per_pt, 'k-')
        plt.plot(x, pth, 'r-')
        plt.show()

    assert (np.allclose(
        forces /
        substrate.area_per_pt, pth, atol=1e-2))