コード例 #1
0
ファイル: keywords.py プロジェクト: georgeyumnam/pylada
def test_newk():
  from pickle import loads, dumps
  from numpy import array, all
  from pylada.dftcrystal.properties import Properties
  from pylada.dftcrystal.properties.keywords import NewK
  from pylada.dftcrystal.input import SetPrint
  a = Properties()
  assert len(a.output_map()) == 0
  a.newk.mp = 5
  assert len(a.output_map()) != 0
  assert all(array(a.output_map()['newk'].split(), dtype='int64') == [5, 5, 0, 0])
  a.newk.printing[66] = -5
  assert all(array(a.output_map()['newk'].split(), dtype='int64') == [5, 5, 0, 1, 66, -5])

  b = a._input['newk']
  assert eval(repr(b), {'NewK': NewK}).output_map()['newk'] == a.output_map()['newk']
  assert loads(dumps(b)).output_map()['newk'] == a.output_map()['newk']
  assert b.__ui_repr__({})['newk'] == repr(b)
  assert b.__ui_repr__({}, defaults=NewK())['newk'] == repr(b)
  del b.printing
  assert len(b.printing) == 0
  assert type(b.printing) is SetPrint
  assert b.__ui_repr__({})['newk'] == repr(b)
  assert b.__ui_repr__({}, defaults=NewK())['newk'] == '5, None'

  assert not b.recompute_fermi
  a.newk.recompute_fermi = True
  assert b.recompute_fermi
  assert all(array(a.output_map()['newk'].split(), dtype='int64') == [5, 5, 1, 0])
  assert eval(repr(b), {'NewK': NewK}).output_map()['newk'] == a.output_map()['newk']
  assert loads(dumps(b)).output_map()['newk'] == a.output_map()['newk']
  assert b.__ui_repr__({})['newk'] == repr(b)
  assert b.__ui_repr__({}, defaults=NewK())['newk'] == repr(b)
コード例 #2
0
ファイル: keywords.py プロジェクト: georgeyumnam/pylada
def test_nosymada():
  from pylada.dftcrystal.properties import Properties
  a = Properties()
  assert a.nosymada is None
  assert len(a.output_map()) == 0
  a.nosymada = True
  assert a.print_input() == "NOSYMADA\nEND\n"
コード例 #3
0
ファイル: wf.py プロジェクト: georgeyumnam/pylada
def test():
  from tempfile import mkdtemp
  from shutil import rmtree
  from os.path import join, exists
  from os import mkdir
  from pylada.dftcrystal import Crystal, Functional, Shell
  from pylada.dftcrystal.properties import Properties
  from pylada import default_comm

  functional = Functional()
  functional.basis['Si'] = [
      Shell('s', a0=[16120.0, 0.001959],
                 a1=[2426.0, 0.01493], 
                 a2=[553.9, 0.07285],
                 a3=[156.3, 0.2461], 
                 a4=[50.07, 0.4859],
                 a5=[17.02, 0.325]),
      Shell('sp', a0=[292.7, -0.002781, 0.004438],
                  a1=[69.87, -0.03571, 0.03267],
                  a2=[22.34, -0.115, 0.1347],
                  a3=[8.15, 0.09356, 0.3287],
                  a4=[3.135, 0.603, 0.4496]), 
      Shell('sp', 4.0, a0=[1.22, 1.0, 1.0]),
      Shell('sp', 0.0, a0=[0.55, 1.0, 1.0]),
      Shell('sp', 0.0, a0=[0.27, 1.0, 1.0]) ]

  functional.dft.pbe0 = True
  functional.fmixing = 30
  functional.shrink = 8, 16
  functional.levshift = 5, True
  functional.maxcycle = 600
  functional.dft.spin = True

  crystal = Crystal(227, 5.43).add_atom(0.125, 0.125, 0.125, 'Si')
  directory =  mkdtemp() 
  if directory == '/tmp/test/':
    rmtree(directory)
    mkdir(directory)
  firstdir, seconddir = join(directory, '0'), join(directory, '1')
  try: 
     si = functional(crystal, outdir=firstdir, comm=default_comm)
     assert exists(join(firstdir, 'crystal.f9'))
     assert not exists(join(directory, 'crystal.f98'))
     properties = Properties(si)
     properties.rdfmwf = True
     result = properties(outdir=directory, workdir=join(directory, 'try0'))
     assert result.success
     assert exists(join(directory, 'crystal.f9'))
     assert not exists(join(directory, 'crystal.f98'))
     properties = Properties(directory)
     properties.fmwf = True
     result = properties(outdir=directory, workdir=join(directory, 'try1'))
     assert not exists(join(directory, 'crystal.f98'))
     result = properties(outdir=directory, workdir=join(directory, 'try1'), overwrite=True)
     assert result.success
     assert exists(join(directory, 'crystal.f98'))

  finally: 
    if directory != '/tmp/test/': rmtree(directory)
コード例 #4
0
ファイル: keywords.py プロジェクト: mdavezac/LaDa
def test_band():
    from pickle import loads, dumps
    from collections import namedtuple
    from random import randint
    from numpy import all, abs, array
    from pylada.dftcrystal.properties import Properties
    from pylada.dftcrystal.properties.keywords import Band

    Shell = namedtuple("Shell", ["charge"])
    Functional = namedtuple("Functional", ["basis"])
    Extract = namedtuple("Extract", ["functional", "directory", "structure"])
    Atom = namedtuple("Atom", ["type"])

    class Structure(list):
        def __init__(self, *args, **kwargs):
            super(Structure, self).__init__(*args, **kwargs)

        def eval(self):
            return self

    shells = {}
    shells["Al"] = [Shell(randint(0, 10)) for i in xrange(5)]
    shells["Be"] = [Shell(randint(0, 10)) for i in xrange(5)]
    functional = Functional(shells)
    structure = Structure([Atom(u) for u in ["Al"] * 3 + ["Be"] * 4])
    input = Extract(functional, "/dev/null", structure)
    kwargs = {"input": input, "filework": False}

    nelectrons = sum(u.charge for u in shells["Al"]) * 3 + sum(u.charge for u in shells["Be"]) * 4

    a = Properties()
    assert len(a.output_map()) == 0
    assert len(a.output_map(**kwargs)) == 0
    a.band += [[0, 1, 0], [0, 0, 0]], [[0, 0, 0], [1, 0, 0]]
    assert all(abs(eval(repr(a.band), {"Band": Band})._lines - a.band._lines) < 1e-8)
    assert all(abs(loads(dumps(a.band))._lines - a.band._lines) < 1e-8)
    c = a.output_map(**kwargs)["band"].split()
    assert all(array(c[:7], dtype="float64") == [2, 10000, 40, 1, nelectrons // 2 + 4, 1, 0])
    assert all(abs(array(c[7:], dtype="float64") / 1e4 - a.band._lines.flatten()) < 1e-8)
    assert a._input["band"].__ui_repr__({}, name="band")["band"] == repr(a.band)
    c = a._input["band"].__ui_repr__({}, name="band", defaults=Band())["band"]
    assert all(abs(array(eval(c), dtype="float64") - a.band._lines) < 1e-8)

    a.band.maxband = nelectrons // 2 + 1
    assert a.band.maxband == nelectrons // 2 + 1
    assert all(abs(eval(repr(a.band), {"Band": Band})._lines - a.band._lines) < 1e-8)
    assert eval(repr(a.band), {"Band": Band}).maxband == nelectrons // 2 + 1
    assert all(abs(loads(dumps(a.band))._lines - a.band._lines) < 1e-8)
    assert loads(dumps(a.band)).maxband == nelectrons // 2 + 1
    assert a._input["band"].__ui_repr__({}, name="band")["band"] == repr(a.band)
    assert a._input["band"].__ui_repr__({}, name="band", defaults=Band())["band"] == repr(a.band)

    # check title
    assert len(a.output_map(properties=a, **kwargs)["band"].splitlines()[0]) == 0
    Extract = namedtuple("Extract", ["functional", "directory", "structure", "title"])
    input = Extract(functional, "/dev/null", structure, "hello")
    kwargs["input"] = input
    assert a.output_map(properties=a, **kwargs)["band"].splitlines()[0] == "hello"
    a.band.title = "goodbye"
    assert a.output_map(properties=a, **kwargs)["band"].splitlines()[0] == "goodbye"
コード例 #5
0
ファイル: keywords.py プロジェクト: georgeyumnam/pylada
def test_band():
  from pickle import loads, dumps
  from collections import namedtuple
  from random import randint
  from numpy import all, abs, array
  from pylada.dftcrystal.properties import Properties
  from pylada.dftcrystal.properties.keywords import Band

  Shell = namedtuple('Shell', ['charge'])
  Functional = namedtuple('Functional', ['basis'])
  Extract    = namedtuple('Extract', ['functional', 'directory', 'structure'])
  Atom       = namedtuple('Atom', ['type'])
  class Structure(list):
    def __init__(self, *args, **kwargs):
      super(Structure, self).__init__(*args, **kwargs)
    def eval(self): return self
  shells     = {}
  shells['Al'] = [Shell(randint(0, 10)) for i in xrange(5)]
  shells['Be'] = [Shell(randint(0, 10)) for i in xrange(5)]
  functional = Functional(shells)
  structure = Structure([Atom(u) for u in ['Al']*3 + ['Be'] * 4])
  input = Extract(functional, '/dev/null', structure)
  kwargs = {'input': input, 'filework': False}

  nelectrons = sum(u.charge for u in shells['Al']) * 3                         \
               + sum(u.charge for u in shells['Be']) * 4  

  a = Properties()
  assert len(a.output_map()) == 0
  assert len(a.output_map(**kwargs)) == 0
  a.band += [[0, 1, 0], [0, 0, 0]], [[0, 0, 0], [1, 0, 0]]
  assert all(abs(eval(repr(a.band), {'Band': Band})._lines - a.band._lines) < 1e-8)
  assert all(abs(loads(dumps(a.band))._lines - a.band._lines) < 1e-8)
  c = a.output_map(**kwargs)['band'].split()
  assert all(array(c[:7], dtype='float64') == [2, 10000, 40, 1, nelectrons//2+4, 1, 0])
  assert all(abs(array(c[7:], dtype='float64') / 1e4 - a.band._lines.flatten()) < 1e-8)
  assert a._input['band'].__ui_repr__({}, name='band')['band'] == repr(a.band)
  c = a._input['band'].__ui_repr__({}, name='band', defaults=Band())['band']
  assert all(abs(array(eval(c), dtype='float64') - a.band._lines) < 1e-8)
  
  a.band.maxband = nelectrons // 2 + 1
  assert a.band.maxband == nelectrons // 2 + 1
  assert all(abs(eval(repr(a.band), {'Band': Band})._lines - a.band._lines) < 1e-8)
  assert eval(repr(a.band), {'Band': Band}).maxband == nelectrons // 2 + 1
  assert all(abs(loads(dumps(a.band))._lines - a.band._lines) < 1e-8)
  assert loads(dumps(a.band)).maxband == nelectrons // 2 + 1
  assert a._input['band'].__ui_repr__({}, name='band')['band'] == repr(a.band)
  assert a._input['band'].__ui_repr__({}, name='band', defaults=Band())['band'] == repr(a.band)
  
  # check title
  assert len(a.output_map(properties=a, **kwargs)['band'].splitlines()[0]) == 0
  Extract = namedtuple('Extract', ['functional', 'directory', 'structure', 'title'])
  input = Extract(functional, '/dev/null', structure, 'hello')
  kwargs['input'] = input
  assert a.output_map(properties=a, **kwargs)['band'].splitlines()[0] == 'hello'
  a.band.title = 'goodbye'
  assert a.output_map(properties=a, **kwargs)['band'].splitlines()[0] == 'goodbye'
コード例 #6
0
ファイル: bandstructure.py プロジェクト: mdavezac/LaDa
def test():
  from tempfile import mkdtemp
  from shutil import rmtree
  from os.path import join
  from numpy import all, array
  from quantities import hartree
  from pylada.dftcrystal import Crystal, Functional, Shell
  from pylada.dftcrystal.properties import Properties
  from pylada import default_comm

  functional = Functional()
  functional.basis['Si'] = [
      Shell('s', a0=[16120.0, 0.001959],
                 a1=[2426.0, 0.01493], 
                 a2=[553.9, 0.07285],
                 a3=[156.3, 0.2461], 
                 a4=[50.07, 0.4859],
                 a5=[17.02, 0.325]),
      Shell('sp', a0=[292.7, -0.002781, 0.004438],
                  a1=[69.87, -0.03571, 0.03267],
                  a2=[22.34, -0.115, 0.1347],
                  a3=[8.15, 0.09356, 0.3287],
                  a4=[3.135, 0.603, 0.4496]), 
      Shell('sp', 4.0, a0=[1.22, 1.0, 1.0]),
      Shell('sp', 0.0, a0=[0.55, 1.0, 1.0]),
      Shell('sp', 0.0, a0=[0.27, 1.0, 1.0]) ]

  functional.dft.pbe0 = True
  functional.fmixing = 30
  functional.shrink = 8, 16
  functional.levshift = 5, True
  functional.maxcycle = 600
  functional.dft.spin = True

  crystal = Crystal(227, 5.43).add_atom(0.125, 0.125, 0.125, 'Si')
  directory =  mkdtemp() 
  firstdir, seconddir = join(directory, '0'), join(directory, '1')
  try: 
     si = functional(crystal, outdir=firstdir, comm=default_comm)
     properties = Properties(si)
     properties.band = [0, 0, 0], [1, 0, 0], [1, 0, 0], [0, 1, 0]
     result0 = properties(outdir=firstdir)
     assert result0.success 
     assert all(abs(result0.bandstructure.eigenvalues[0] - result0.bandstructure.eigenvalues[1]) < 1e-6)
     test = [ [-64.55637917, -64.55637522,  -3.64877203,  -3.64854929,
                -1.98183315,  -1.98183315,  -1.98183315,  -1.98097196,
                -1.98097196,  -1.98097196,   1.00052165,   1.49996607,
                 1.49996607,   1.49996607,   1.63212276,   1.63212276,
                 1.63212276,   1.66407077],
               [-64.55637917, -64.55637522,  -3.64877203,  -3.64854929,
                 -1.98183315,  -1.98183315,  -1.98183315,  -1.98097196,
                 -1.98097196,  -1.98097196,   1.00052165,   1.49996607,
                  1.49996607,   1.49996607,   1.63212276,   1.63212276,
                  1.63212276,   1.66407077] ] * hartree
     assert all(abs(result0.bandstructure.eigenvalues[0,0] - test[0]) < 1e-8)
     assert all(abs(result0.bandstructure.eigenvalues[0,-1] - test[1]) < 1e-8)
     test = array([[  6.66666667e-02,   0, 0],
                   [  1.33333333e-01,   0, 0],
                   [  2.00000000e-01,   0, 0],
                   [  2.66666667e-01,   0, 0],
                   [  3.33333333e-01,   0, 0],
                   [  4.00000000e-01,   0, 0],
                   [  4.66666667e-01,   0, 0],
                   [  5.33333333e-01,   0, 0],
                   [  6.00000000e-01,   0, 0],
                   [  6.66666667e-01,   0, 0],
                   [  7.33333333e-01,   0, 0],
                   [  8.00000000e-01,   0, 0],
                   [  8.66666667e-01,   0, 0],
                   [  9.33333333e-01,   0, 0],
                   [  1.00000000e+00,   0, 0],
                   [  1.06666667e+00,   0, 0],
                   [  9.58333333e-01,   4.16666667e-02,   0],
                   [  9.16666667e-01,   8.33333333e-02,   0],
                   [  8.75000000e-01,   1.25000000e-01,   0],
                   [  8.33333333e-01,   1.66666667e-01,   0],
                   [  7.91666667e-01,   2.08333333e-01,   0],
                   [  7.50000000e-01,   2.50000000e-01,   0],
                   [  7.08333333e-01,   2.91666667e-01,   0],
                   [  6.66666667e-01,   3.33333333e-01,   0],
                   [  6.25000000e-01,   3.75000000e-01,   0],
                   [  5.83333333e-01,   4.16666667e-01,   0],
                   [  5.41666667e-01,   4.58333333e-01,   0],
                   [  5.00000000e-01,   5.00000000e-01,   0],
                   [  4.58333333e-01,   5.41666667e-01,   0],
                   [  4.16666667e-01,   5.83333333e-01,   0],
                   [  3.75000000e-01,   6.25000000e-01,   0],
                   [  3.33333333e-01,   6.66666667e-01,   0],
                   [  2.91666667e-01,   7.08333333e-01,   0],
                   [  2.50000000e-01,   7.50000000e-01,   0],
                   [  2.08333333e-01,   7.91666667e-01,   0],
                   [  1.66666667e-01,   8.33333333e-01,   0],
                   [  1.25000000e-01,   8.75000000e-01,   0],
                   [  8.33333333e-02,   9.16666667e-01,   0],
                   [  4.16666667e-02,   9.58333333e-01,   0],
                   [ -1.11022302e-16,   1.00000000e+00,   0]])
     assert all(abs(result0.bandstructure.kpoints - test) < 1e-8)
     
     functional.dft.spin = False
     si = functional(crystal, outdir=seconddir, comm=default_comm)
     properties = Properties(si)
     properties.band = [0, 0, 0], [1, 0, 0], [1, 0, 0], [0, 1, 0]
     result1 = properties(outdir=seconddir)
     assert result1.success 
     assert all(abs(result1.bandstructure.eigenvalues - result0.bandstructure.eigenvalues[0]) < 1e-6)
     assert all(abs(result1.bandstructure.kpoints - result0.bandstructure.kpoints) < 1e-6)
  finally: 
    if directory != '/tmp/test/': rmtree(directory)
コード例 #7
0
ファイル: keywords.py プロジェクト: georgeyumnam/pylada
def test_rdfmwf():
  from collections import namedtuple
  from shutil import rmtree
  from tempfile import mkdtemp
  from os.path import join, exists
  from os import remove
  from pylada.dftcrystal.properties import Properties
  from pylada.error import IOError

  Extract = namedtuple('Extract', ['directory'])
  try: 
    indir = mkdtemp()
    outdir = mkdtemp()

    extract = Extract(indir)
    a = Properties(input=extract)
    assert a.rdfmwf is None
    assert len(a.output_map(filework=False)) == 0
    assert len(a.print_input(filework=False)) == 0
    try: a.output_map(filework=True, workdir=outdir)
    except IOError: pass
    else: raise Exception()

    a.rdfmwf = True
    assert len(a.output_map(filework=False)) == 1
    assert a.output_map(filework=False)['rdfmwf'] == True
    try: a.output_map(filework=True, workdir=outdir)
    except IOError: pass
    else: raise Exception()

    a.rdfmwf = False
    assert len(a.output_map(filework=False)) == 0
    try: a.output_map(filework=True, workdir=outdir)
    except IOError: pass
    else: raise Exception()

    # now with .f98 file
    with open(join(indir, 'crystal.f98'), 'w') as file: file.write('hello')
    a.rdfmwf = None
    assert len(a.output_map(filework=False)) == 1
    assert a.output_map(filework=False)['rdfmwf'] == True
    assert not exists(join(outdir, 'fort.98'))
    # should fail
    a.rdfmwf = False
    try: a.output_map(filework=True, workdir=outdir)
    except IOError: pass
    else: raise Exception()
    # now check writing
    a.rdfmwf = None
    assert not exists(join(outdir, 'fort.98'))
    assert a.output_map(filework=True, workdir=outdir)['rdfmwf'] == True
    assert exists(join(outdir, 'fort.98'))
    remove(join(outdir, 'fort.98'))
    a.rdfmwf = True
    assert not exists(join(outdir, 'fort.98'))
    assert a.output_map(filework=True, workdir=outdir)['rdfmwf'] == True
    assert exists(join(outdir, 'fort.98'))
    remove(join(indir, 'crystal.f98'))
    remove(join(outdir, 'fort.98'))
    with open(join(outdir, 'crystal.f98'), 'w') as file: file.write('hello')
    assert exists(join(outdir, 'crystal.f98'))
    assert not exists(join(outdir, 'fort.98'))
    assert a.output_map(filework=True, workdir=outdir, outdir=outdir)['rdfmwf'] == True
    assert exists(join(outdir, 'fort.98'))
    remove(join(outdir, 'fort.98'))
    
    # now for .f9
    with open(join(indir, 'crystal.f9'), 'w') as file: file.write('hello')
    a.rdfmwf = None
    assert len(a.output_map(filework=False)) == 0
    assert not exists(join(outdir, 'fort.9'))
    # should fail
    a.rdfmwf = True
    try: a.output_map(filework=True, workdir=outdir)
    except IOError: pass
    else: raise Exception()
    # now check writing
    a.rdfmwf = None
    assert not exists(join(outdir, 'fort.9'))
    assert len(a.output_map(filework=True, workdir=outdir)) == 0
    assert exists(join(outdir, 'fort.9'))
    remove(join(outdir, 'fort.9'))
    a.rdfmwf = False
    assert not exists(join(outdir, 'fort.9'))
    assert len(a.output_map(filework=True, workdir=outdir)) == 0
    assert exists(join(outdir, 'fort.9'))
    remove(join(indir, 'crystal.f9'))
    remove(join(outdir, 'fort.9'))
    with open(join(outdir, 'crystal.f9'), 'w') as file: file.write('hello')
    assert not exists(join(outdir, 'fort.9'))
    assert exists(join(outdir, 'crystal.f9'))
    assert len(a.output_map(filework=True, workdir=outdir, outdir=outdir)) == 0
    assert exists(join(outdir, 'fort.9'))
    remove(join(outdir, 'fort.9'))

  finally:
    try: rmtree(indir)
    except: pass
    try: rmtree(outdir)
    except: pass