Beispiel #1
0
def test_h5():
    try:
        import h5py
        dct1 = \
            {'/a': 'abcgs',
             '/b/c/x1': 3,
             '/b/c/x2': rand(2,3),
             }
        # writing a dct w/o leading slash will always be read back in *with*
        # leading slash
        dct2 = \
            {'a': 'abciqo4iki',
             'b/c/x1': 3,
             'b/c/x2': rand(2,3),
             }
        for idx, dct in enumerate([dct1, dct2]):
            h5fn = os.path.join(testdir, 'test_%i.h5' % idx)
            io.write_h5(h5fn, dct)
            read_dct = io.read_h5(h5fn)
            for kk in list(read_dct.keys()):
                assert kk.startswith('/')
            for kk in list(dct.keys()):
                key = '/' + kk if not kk.startswith('/') else kk
                tools.assert_all_types_equal(dct[kk], read_dct[key])

        # write mode='a', test appending
        h5fn = os.path.join(testdir, 'test_append.h5')
        io.write_h5(h5fn, {'/a': 1.0})
        read_dct = io.read_h5(h5fn)
        assert list(read_dct.keys()) == ['/a']
        assert read_dct['/a'] == 1.0
        # append '/b', using {'/a': 1.0, '/b': 2.0} would be an error since /a
        # already exists, use mode='w' then, but this overwrites all!
        io.write_h5(h5fn, {'/b': 2.0}, mode='a')
        read_dct2 = io.read_h5(h5fn)
        # sort(...): sort possible [/b, /a] -> [/a, /b]
        assert np.sort(np.array(list(
            read_dct2.keys()))).tolist() == ['/a', '/b']
        assert read_dct2['/a'] == 1.0
        assert read_dct2['/b'] == 2.0
        # overwrite
        io.write_h5(h5fn, {'/b': 22.0, '/c': 33.0}, mode='w')
        read_dct3 = io.read_h5(h5fn)
        assert np.sort(np.array(list(
            read_dct3.keys()))).tolist() == ['/b', '/c']

    except ImportError:
        tools.skip("skipping test_h5, no h5py importable")
Beispiel #2
0
def test_h5():
    try:
        import h5py
        dct1 = \
            {'/a': 'abcgs',
             '/b/c/x1': 3,
             '/b/c/x2': rand(2,3),
             }
        # writing a dct w/o leading slash will always be read back in *with*
        # leading slash             
        dct2 = \
            {'a': 'abciqo4iki',
             'b/c/x1': 3,
             'b/c/x2': rand(2,3),
             }
        for idx,dct in enumerate([dct1, dct2]):             
            h5fn = os.path.join(testdir, 'test_%i.h5' %idx)
            io.write_h5(h5fn, dct)
            read_dct = io.read_h5(h5fn)
            for kk in read_dct.keys():
                assert kk.startswith('/')
            for kk in dct.keys():
                key = '/'+kk if not kk.startswith('/') else kk
                tools.assert_all_types_equal(dct[kk], read_dct[key])
        
        # write mode='a', test appending
        h5fn = os.path.join(testdir, 'test_append.h5')
        io.write_h5(h5fn, {'/a': 1.0})
        read_dct = io.read_h5(h5fn)
        assert read_dct.keys() == ['/a']
        assert read_dct['/a'] == 1.0
        # append '/b', using {'/a': 1.0, '/b': 2.0} would be an error since /a
        # already exists, use mode='w' then, but this overwrites all!
        io.write_h5(h5fn, {'/b': 2.0}, mode='a')
        read_dct2 = io.read_h5(h5fn)
        # sort(...): sort possible [/b, /a] -> [/a, /b]
        assert np.sort(np.array(read_dct2.keys())).tolist() == ['/a', '/b']
        assert read_dct2['/a'] == 1.0
        assert read_dct2['/b'] == 2.0
        # overwrite
        io.write_h5(h5fn, {'/b': 22.0, '/c': 33.0}, mode='w')
        read_dct3 = io.read_h5(h5fn)
        assert np.sort(np.array(read_dct3.keys())).tolist() == ['/b', '/c']

    except ImportError:
        tools.skip("skipping test_h5, no h5py importable")
Beispiel #3
0
def test_gibbs():
    # number of varied axis points
    nax = 6
    # phonon freq axis
    freq = np.linspace(0, 1000, 300)  # cm^-1
    T = np.linspace(5, 2000, 50)
    P = np.linspace(0, 5, 2)

    # 2d case
    case = '2d'
    cell_a = np.linspace(2.5, 3.5, nax)
    cell_c = np.linspace(3, 3.8, nax)
    volfunc_ax = lambda x: x[0]**2 * x[1]
    axes_flat = np.array([x for x in product(cell_a, cell_c)])
    V = np.array([volfunc_ax(x) for x in axes_flat])
    cell_a_mean = cell_a.mean()
    cell_c_mean = cell_c.mean()
    cell_a_min = cell_a.min()
    cell_c_min = cell_c.min()
    etot = np.array([(a - cell_a_mean)**2.0 + (c - cell_c_mean)**2.0
                     for a, c in axes_flat])
    phdos = []
    Vmax = V.max()
    # phonon dos (just a gaussian) shifted to lower (higher) freqs for higher
    # (lower) volume
    for ii in range(axes_flat.shape[0]):
        a, c = axes_flat[ii, :]
        fc = 550 - 50 * V[ii] / Vmax
        phdos.append(np.array([freq, gauss(freq - fc, 100) * 0.01]).T)

    gibbs = Gibbs(T=T,
                  P=P,
                  etot=etot,
                  phdos=phdos,
                  axes_flat=axes_flat,
                  volfunc_ax=volfunc_ax,
                  case=case,
                  dosarea=None)
    gibbs.set_fitfunc('C',
                      lambda x, y: num.Spline(x, y, s=None, k=5, eps=1e-5))
    g = gibbs.calc_G(calc_all=True)

    dr = 'files/gibbs/2d'
    for name in os.listdir(dr):
        fn = '%s/%s' % (dr, name)
        gref = io.read_h5(fn)
        print("testing: %s" % fn)
        compare_dicts_with_arrays(gref, g)
        tools.assert_dict_with_all_types_almost_equal(gref,
                                                      g,
                                                      keys=list(gref.keys()),
                                                      atol=1e-8,
                                                      rtol=1e-3)

    # 1d case
    case = '1d'
    V = np.linspace(10, 20, nax)
    axes_flat = V**(1 / 3.)  # cubic
    volfunc_ax = lambda x: x[0]**3.0
    etot = (V - V.mean())**2
    fcenter = 450 + 100 * (axes_flat - axes_flat.min())
    # fake phonon dos data (Gaussian), shift to lower freq for higher volume
    phdos = [np.array([freq, gauss(freq - fc, 100)]).T for fc in fcenter[::-1]]

    gibbs = Gibbs(T=T,
                  P=P,
                  etot=etot,
                  phdos=phdos,
                  axes_flat=axes_flat,
                  volfunc_ax=volfunc_ax,
                  case=case,
                  dosarea=None)
    gibbs.set_fitfunc('C',
                      lambda x, y: num.Spline(x, y, s=None, k=5, eps=1e-5))
    g = gibbs.calc_G(calc_all=True)

    dr = 'files/gibbs/1d'
    for name in os.listdir(dr):
        fn = '%s/%s' % (dr, name)
        gref = io.read_h5(fn)
        print("testing: %s" % fn)
        compare_dicts_with_arrays(gref, g)
        tools.assert_dict_with_all_types_almost_equal(gref,
                                                      g,
                                                      keys=list(gref.keys()),
                                                      atol=1e-14,
                                                      rtol=1e-8)

    # test enthalpy stuff for 1d case
    # E(V)
    ev = num.PolyFit1D(g['/ax0/V'], g['/ax0/Etot'], deg=5)
    # P(V)
    pv = lambda v: -ev(v, der=1) * constants.eV_by_Ang3_to_GPa
    assert np.allclose(g['/P/P'], pv(g['/#opt/P/V']))
    assert np.allclose(g['/#opt/P/H'],
                       ev(g['/#opt/P/V']) + g['/P/P']*g['/#opt/P/V'] / \
                       constants.eV_by_Ang3_to_GPa)
Beispiel #4
0
def test_gibbs():
    # number of varied axis points
    nax = 6
    # phonon freq axis
    freq = np.linspace(0,1000,300) # cm^-1
    T = np.linspace(5, 2000, 50)
    P = np.linspace(0,5,2)

    # 2d case
    case = '2d'
    cell_a = np.linspace(2.5,3.5,nax)
    cell_c = np.linspace(3,3.8,nax)
    volfunc_ax = lambda x: x[0]**2 * x[1]
    axes_flat = np.array([x for x in product(cell_a, cell_c)])
    V = np.array([volfunc_ax(x) for x in axes_flat])
    cell_a_mean = cell_a.mean()
    cell_c_mean = cell_c.mean()
    cell_a_min = cell_a.min()
    cell_c_min = cell_c.min()
    etot = np.array([(a-cell_a_mean)**2.0 + (c-cell_c_mean)**2.0 for a,c in axes_flat])
    phdos = []
    Vmax = V.max()
    # phonon dos (just a gaussian) shifted to lower (higher) freqs for higher
    # (lower) volume
    for ii in range(axes_flat.shape[0]):
        a,c = axes_flat[ii,:]
        fc = 550 - 50 * V[ii] / Vmax
        phdos.append(np.array([freq,gauss(freq-fc,100)*0.01]).T)

    gibbs = Gibbs(T=T, P=P, etot=etot, phdos=phdos, axes_flat=axes_flat,
                  volfunc_ax=volfunc_ax, case=case, dosarea=None)
    gibbs.set_fitfunc('C', lambda x,y: num.Spline(x,y,s=None,k=5, eps=1e-5))
    g = gibbs.calc_G(calc_all=True)
    
    dr = 'files/gibbs/2d'
    for name in os.listdir(dr):
        fn = '%s/%s' %(dr, name)
        gref = io.read_h5(fn)
        print "testing: %s" %fn
        compare_dicts_with_arrays(gref, g) 
        tools.assert_dict_with_all_types_almost_equal(gref, 
                                                      g, 
                                                      keys=gref.keys(),
                                                      atol=1e-8, rtol=1e-3)

    # 1d case
    case = '1d'
    V = np.linspace(10,20,nax)
    axes_flat = V**(1/3.) # cubic
    volfunc_ax = lambda x: x[0]**3.0
    etot = (V-V.mean())**2
    fcenter = 450 + 100*(axes_flat - axes_flat.min())
    # fake phonon dos data (Gaussian), shift to lower freq for higher volume
    phdos = [np.array([freq,gauss(freq-fc, 100)]).T for fc in
             fcenter[::-1]]

    gibbs = Gibbs(T=T, P=P, etot=etot, phdos=phdos, axes_flat=axes_flat,
                  volfunc_ax=volfunc_ax, case=case, dosarea=None)
    gibbs.set_fitfunc('C', lambda x,y: num.Spline(x,y,s=None,k=5, eps=1e-5))
    g = gibbs.calc_G(calc_all=True)
    
    dr = 'files/gibbs/1d'
    for name in os.listdir(dr):
        fn = '%s/%s' %(dr, name)
        gref = io.read_h5(fn)
        print "testing: %s" %fn
        compare_dicts_with_arrays(gref, g) 
        tools.assert_dict_with_all_types_almost_equal(gref, 
                                                      g, 
                                                      keys=gref.keys(),
                                                      atol=1e-14, rtol=1e-14)
    
    # test enthalpy stuff for 1d case
    # E(V) 
    ev = num.PolyFit1D(g['/ax0/V'], g['/ax0/Etot'], deg=5)
    # P(V)
    pv = lambda v: -ev(v, der=1)*constants.eV_by_Ang3_to_GPa
    assert np.allclose(g['/P/P'], pv(g['/#opt/P/V']))
    assert np.allclose(g['/#opt/P/H'], 
                       ev(g['/#opt/P/V']) + g['/P/P']*g['/#opt/P/V'] / \
                       constants.eV_by_Ang3_to_GPa)