Esempio n. 1
0
    def profile(self, extra=False):
        """
        Calculates the volume fraction profile

        Returns
        -------
        z, vfp : np.ndarray
            Distance from the interface, volume fraction profile
        """
        s = Structure()
        s |= SLD(0)

        m = SLD(1.)

        for i, slab in enumerate(self.left_slabs):
            layer = m(slab.thick.value, slab.rough.value)
            if not i:
                layer.rough.value = 0
            layer.vfsolv.value = slab.vfsolv.value
            s |= layer

        polymer_slabs = self.slabs()
        offset = np.sum(s.slabs()[:, 0])

        if polymer_slabs is not None:
            for i in range(np.size(polymer_slabs, 0)):
                layer = m(polymer_slabs[i, 0], polymer_slabs[i, 3])
                layer.vfsolv.value = polymer_slabs[i, -1]
                s |= layer

        for i, slab in enumerate(self.right_slabs):
            layer = m(slab.thick.value, slab.rough.value)
            layer.vfsolv.value = 1 - slab.vfsolv.value
            s |= layer

        s |= SLD(0, 0)

        # now calculate the VFP.
        total_thickness = np.sum(s.slabs()[:, 0])
        zed = np.linspace(0, total_thickness, total_thickness + 1)
        # SLD profile puts a very small roughness on the interfaces with zero
        # roughness.
        zed[0] = 0.01
        z, s = s.sld_profile(z=zed)
        s[0] = s[1]

        # perhaps you'd like to plot the knot locations
        zeds = np.cumsum(self.dz)
        if np.sum(self.dz) > 1:
            zeds /= np.sum(self.dz)
            zeds = np.clip(zeds, 0, 1)

        zed_knots = zeds * float(self.extent) + offset

        if extra:
            return z, s, zed_knots, np.array(self.vf)
        else:
            return z, s
Esempio n. 2
0
def phase_problem_plot(flag):
    if flag != "SLD" and flag != "ACF":
        raise Exception("Invalid flag to plot")

    qvals = np.linspace(0,0.2,1025)[1:]
    sld1 = SLD(0, 0)
    sld3 = SLD(2.074, 0)

    layer1 = sld1(0, 0)
    layer3 = sld3(0, 0)

    fig = plt.figure(figsize=(15, 10))
    gs = gridspec.GridSpec(2, 2)
    ax1 = plt.subplot(gs[0,0])
    ax2 = plt.subplot(gs[1,0])
    for i, style in zip([0,1.0], ['k-', 'r:']):
        sld20 = SLD(6.335 - i, 0)
        sld21 = SLD(6.335 + i, 0)
        layer20 = sld20(100, 0)
        layer21 = sld21(100, 0)
        structure = Structure(layer1 | layer20 | layer21 | layer3)
        z, rho = structure.sld_profile()
        drho = (rho[1:] - rho[:-1]) / (z[1:] - z[:-1])
        z_ = 0.5 * (z[:-1] + z[1:])
        acf = autocorr(drho)
        z_acf = z_ - z_.min()
        z_acf = np.hstack((-np.flip(z_acf)[:-1], z_acf))

        if flag == 'SLD':
            ax1.plot(z, rho, style)
            ax2.semilogy(qvals, ReflectModel(structure, dq=0.0).model(qvals), style)

        else:
            ax1.stem(z_,   drho, style, markerfmt=' ', basefmt=style, use_line_collection=True)
            ax2.stem(z_acf, acf, style, markerfmt=' ', basefmt=style, use_line_collection=True)


    if flag == 'SLD':
        ax1.set_xlabel(r'$z$/Å')
        ax1.set_ylabel(r'$\rho(z)$/Å$^{-2}$')

        ax2.set_xlabel(r'$q$/Å')
        ax2.set_ylabel(r'$R(q)$')

    else:
        ax1.set_xlabel(r'$z$/Å')
        ax1.set_ylabel(r'$\rho\'(z)$/Å$^{-3}$')

        ax2.set_xlabel(r'$z$/Å')
        ax2.set_ylabel("$ \\rm{ACF}_{\\rho'}(z)/ \\AA^{-5}$")

    plt.tight_layout()
    figfilename = f"phase_problem_{flag}.pdf"
    plt.savefig(figfilename)
    print(f"Figure saved as {figfilename}")
    plt.close()
Esempio n. 3
0
def resolution_test(slabs, data, backend):
    structure = Structure()
    for i, slab in enumerate(slabs):
        m = SLD(complex(slab[1], slab[2]))
        structure |= m(slab[0], slab[-1])

    with use_reflect_backend(backend):
        model = ReflectModel(structure, bkg=0.0)
        model.quad_order = 17
        R = model.model(data[:, 0],
                        x_err=data[:, -1] * 2 * np.sqrt(2 * np.log(2)))
        np.testing.assert_allclose(R, data[:, 1], rtol=0.03)
Esempio n. 4
0
def roughness():
    sld1 = SLD(0, 0)
    sld2 = SLD(6.335, 0)
    sld3 = SLD(2.074, 0)

    layer1 = sld1(0, 0)
    layer3 = sld3(10, 0)

    fig = plt.figure(figsize=(10, 7.5 / 2))
    gs = gridspec.GridSpec(1, 3)
    ax1 = plt.subplot(gs[0:2])
    ax2 = plt.subplot(gs[2])
    for i in range(3, 9, 2):
        layer2 = sld2(10, i)
        structure = Structure(layer1 | layer2 | layer3)

        ax1.plot(*structure.sld_profile())
        ax2.plot(*structure.sld_profile())
    ax2.set_ylim(1.6, 2.6)
    ax2.set_xlim(5, 15)
    ax1.axhline(sld3.real, color='k', alpha=0.3)
    ax2.axhline(sld3.real, color='k', alpha=0.3)
    ax1.set_xlabel(r'$z$/Å')
    ax1.set_ylabel(r'$\rho(z)$/Å$^{-2}$')
    ax1.text(0.025,
             0.95,
             '(a)',
             horizontalalignment='left',
             verticalalignment='top',
             transform=ax1.transAxes)
    ax2.set_xlabel(r'$z$/Å')
    ax2.set_ylabel(r'$\rho(z)$/Å$^{-2}$')
    ax2.text(0.025,
             0.95,
             '(b)',
             horizontalalignment='left',
             verticalalignment='top',
             transform=ax2.transAxes)
    plt.tight_layout()
    plt.savefig("roughness.pdf")
    plt.close()
Esempio n. 5
0
    def test_stack(self):
        stk = Stack()
        slabs = stk.slabs(None)
        assert (slabs is None)

        si = SLD(2.07)
        sio2 = SLD(3.47)
        polymer = SLD(1.0)
        d2o = SLD(6.36)

        # check some initial stack properties
        stk.append(sio2(55, 4))
        slabs = stk.slabs(None)
        assert (slabs.shape == (1, 5))
        assert_equal(np.sum(slabs[:, 0]), 55)
        assert_equal(slabs[0, 1], 3.47)
        stk.repeats.value = 3.2
        slabs = stk.slabs(None)
        assert (slabs.shape == (3, 5))
        assert_equal(np.sum(slabs[:, 0]), 165)

        # ior a Stack and a Component
        stk |= polymer(110, 3.5)
        assert_equal(len(stk), 2)
        assert (isinstance(stk, Stack))
        assert_almost_equal(stk.repeats, 3.2)
        slabs = stk.slabs()
        assert (slabs.shape == (6, 5))
        assert_equal(np.sum(slabs[:, 0]), 495)

        # place a stack into a structure
        s = si | d2o(10, 3) | stk | d2o
        assert (isinstance(s, Structure))
        slabs = s.slabs()
        assert_equal(slabs[:, 0], [0, 10, 55, 110, 55, 110, 55, 110, 0])
        assert_equal(slabs[:, 1],
                     [2.07, 6.36, 3.47, 1.0, 3.47, 1.0, 3.47, 1.0, 6.36])
        assert_equal(slabs[:, 3], [0, 3, 4, 3.5, 4, 3.5, 4, 3.5, 0])

        # what are the interfaces of the Stack
        assert_equal(len(stk.interfaces), len(stk.slabs()))
        assert_equal(len(list(flatten(s.interfaces))), len(s.slabs()))

        # ior a Structure and a Stack
        s = Structure(components=[si(), d2o(10, 3)])
        s |= stk
        s |= d2o
        assert (isinstance(s, Structure))

        assert_equal(s.slabs()[:, 0], [0, 10, 55, 110, 55, 110, 55, 110, 0])
        assert_equal(s.slabs()[:, 1],
                     [2.07, 6.36, 3.47, 1.0, 3.47, 1.0, 3.47, 1.0, 6.36])

        q = repr(s)
        r = eval(q)
        assert_equal(r.slabs()[:, 0], [0, 10, 55, 110, 55, 110, 55, 110, 0])
        assert_equal(r.slabs()[:, 1],
                     [2.07, 6.36, 3.47, 1.0, 3.47, 1.0, 3.47, 1.0, 6.36])

        s |= stk
        assert (isinstance(s.components[-1], Stack))
        import pytest
        with pytest.raises(ValueError):
            s.slabs()
Esempio n. 6
0
from refnx.reflect import Structure, SLD

sld1 = SLD(0, 0)
sld2 = SLD(6.335, 0)
sld3 = SLD(2.074, 0)

layer1 = sld1(0, 0)
layer3 = sld3(10, 0)

fig = plt.figure(figsize=(10, 7.5 / 2))
gs = gridspec.GridSpec(1, 3)
ax1 = plt.subplot(gs[0:2])
ax2 = plt.subplot(gs[2])
for i in range(3, 9, 2):
    layer2 = sld2(10, i)
    structure = Structure(layer1 | layer2 | layer3)

    ax1.plot(*structure.sld_profile())
    ax2.plot(*structure.sld_profile())
ax2.set_ylim(1.6, 2.6)
ax2.set_xlim(5, 15)
ax1.axhline(sld3.real, color='k', alpha=0.3)
ax2.axhline(sld3.real, color='k', alpha=0.3)
ax1.set_xlabel(r'$z$/Å')
ax1.set_ylabel(r'$\rho(z)$/Å$^{-2}$')
ax1.text(0.025,
         0.95,
         '(a)',
         horizontalalignment='left',
         verticalalignment='top',
         transform=ax1.transAxes)
def phase_problem_plot(flag):
    if flag != "SLD" and flag != "ACF":
        raise Exception("Invalid flag to plot")

    qvals = np.linspace(0, 0.2, 1025)[1:]
    sld1 = SLD(0, 0)
    sld3 = SLD(2.074, 0)

    layer1 = sld1(0, 0)
    layer3 = sld3(0, 0)

    fig = plt.figure(figsize=(10, 7.5))
    gs = gridspec.GridSpec(2, 1)
    ax1 = plt.subplot(gs[0, 0])
    ax2 = plt.subplot(gs[1, 0])
    colors = [_fig_params.colors[0], _fig_params.colors[1]]
    ls = ['-', ':']
    for i in [0, 1]:
        sld20 = SLD(6.335 - i, 0)
        sld21 = SLD(6.335 + i, 0)
        layer20 = sld20(100, 0)
        layer21 = sld21(100, 0)
        structure = Structure(layer1 | layer20 | layer21 | layer3)
        z, rho = structure.sld_profile()
        drho = (rho[1:] - rho[:-1]) / (z[1:] - z[:-1])
        z_ = 0.5 * (z[:-1] + z[1:])
        acf = autocorr(drho)
        z_acf = z_ - z_.min()
        z_acf = np.hstack((-np.flip(z_acf)[:-1], z_acf))

        if flag == 'SLD':
            ax1.plot(z, rho, color=colors[i], ls=ls[i])
            ax2.semilogy(qvals,
                         ReflectModel(structure, dq=0.0).model(qvals),
                         color=colors[i],
                         ls=ls[i])

        else:
            ax1.stem(z_,
                     drho,
                     linefmt='C{}'.format(i) + ls[i],
                     basefmt='C{}'.format(i) + ls[i],
                     markerfmt=' ',
                     use_line_collection=True)
            ax2.stem(z_acf,
                     acf,
                     linefmt='C{}'.format(i) + ls[i],
                     basefmt='C{}'.format(i) + ls[i],
                     markerfmt=' ',
                     use_line_collection=True)

    if flag == 'SLD':
        ax1.set_xlabel(r'$z$/Å')
        ax1.set_ylabel(r'$\rho(z)$/Å$^{-2}$')

        ax2.set_xlabel(r'$q$/Å')
        ax2.set_ylabel(r'$R(q)$')
        ax1.text(0.025,
                 0.95,
                 '(a)',
                 horizontalalignment='left',
                 verticalalignment='top',
                 transform=ax1.transAxes)

    else:
        ax1.set_xlabel(r'$z$/Å')
        ax1.set_ylabel(r'$\rho\'(z)$/Å$^{-3}$')

        ax2.set_xlabel(r'$z$/Å')
        ax2.set_ylabel("$ \\rm{ACF}_{\\rho'}(z)/ \\AA^{-5}$")
        ax1.text(0.975,
                 0.95,
                 '(a)',
                 horizontalalignment='right',
                 verticalalignment='top',
                 transform=ax1.transAxes)

    ax2.text(0.975,
             0.95,
             '(b)',
             horizontalalignment='right',
             verticalalignment='top',
             transform=ax2.transAxes)

    plt.tight_layout()
    figfilename = f"phase_problem_{flag}.pdf"
    plt.savefig(figfilename)
    print(f"Figure saved as {figfilename}")
    plt.close()