Exemple #1
0
 def new(job):
     with Changedir(r_job) as pwd:
         func(r_job)
     job.params['structure'] = r_job.params['result'].structure
     job.params['vasp'] = r_job.params['vasp']
     job.params['result'] = r_job.params['result']
     job.params['optcell'] = r_job.params['optcell']
Exemple #2
0
def functional(indiv, outdir=None, value=False, **kwargs):
    from pylada.misc import Changedir
    from pickle import dump

    with Changedir(outdir) as pwd:
        with open('OUTCAR', 'w') as file:
            dump((indiv, value), file)

    return Extract(outdir)
Exemple #3
0
def vaspkitkpoints(poscarsource, kpointtype=2, kspacing=0.03):
    import os
    outdir = gettmpdir()
    copyorlinkfile(outdir, "POSCAR", poscarsource, "cp")
    from pylada.misc import Changedir
    with Changedir(outdir) as pwd:
        os.system("""vaspkit <<EOF
7
%d
%.2f
EOF
""" % (kpointtype, kspacing))
        kpoints = os.path.abspath("KPOINTS")
    return kpoints
Exemple #4
0
 def new(job):
     print("-----------COMPUTE:start-----------")
     print("name:" + job.name[1:])
     if maxLoop > 0:
         for i in range(maxLoop - 1):
             with Changedir("Relax_%02d" % i) as pwd:
                 func(job)
             structure = deepcopy(job.params["result"].structure)
             job.params["structure"] = structure
             if len(job.params["result"].total_energies) == 1:
                 break
         else:
             raise Exception("It's not convergence")
     func(job)
     print("-----------COMPUTE:end-------------")
Exemple #5
0
def functional(structure, outdir=None, value=False, **kwargs):
    """ A dummy functional """
    from copy import deepcopy
    from pickle import dump
    from random import random
    from pylada.misc import Changedir

    structure = deepcopy(structure)
    structure.value = value
    with Changedir(outdir) as pwd:
        with open('OUTCAR', 'w') as file:
            dump((random(), structure, value, functional), file)

    return Extract(outdir)
    return structure
Exemple #6
0
def Extract(outdir=None):
  """ An extraction function for a siumlated annealing functional """
  from os.path import exists
  from os import getcwd
  from collections import namedtuple
  from pickle import load
  from pylada.misc import Changedir

  if outdir == None: outdir = getcwd()
  Extract = namedtuple('Extract', ['success', 'directory', 'energy', 'structure', 'value', 'functional'])
  if not exists(outdir): return Extract(False, outdir, None, None, None, None)
  with Changedir(outdir) as pwd:
    if not exists('OUTCAR'): return Extract(False, outdir, None, None, None, None)
    with open('OUTCAR', 'r') as file:
      #structure, energy, value, functional = load(file)
      return Extract(True, outdir, None, None, None, None)
Exemple #7
0
def Extract(outdir=None):
    from os.path import exists
    from os import getcwd
    from collections import namedtuple
    from pickle import load
    from pylada.misc import Changedir

    if outdir == None: outdir = getcwd()
    Extract = namedtuple('Extract',
                         ['success', 'directory', 'indiv', 'functional'])
    if not exists(outdir): return Extract(False, outdir, None, functional)
    with Changedir(outdir) as pwd:
        if not exists('OUTCAR'):
            return Extract(False, outdir, None, functional)
        with open('OUTCAR', 'r') as file:
            indiv, value = load(file)
    return Extract(True, outdir, indiv, functional)
Exemple #8
0
def resultInfo(job):
    """
		information : success path energy structure
	"""
    with Changedir(job.name[1:]) as pwd:
        Extract = namedtuple('resultInfo',
                             ['success', 'path', 'energy', 'structure'])
        if not os.path.exists("RESULTINFO"):
            with open("RESULTINFO", "wb") as wfile:
                energy = job.params['result'].total_energy
                structure = job.params['result'].structure
                path = job.name[1:]
                Et = Extract(True, path, energy, structure)
                dump((True, path, energy, structure), wfile)
            return Et
        else:
            with open("RESULTINFO", "rb") as rfile:
                success, path, energy, structure = load(rfile)
            return Extract(success, path, energy, structure)
Exemple #9
0
def standInfo(job):
    """
		information : success path energy x y z
	"""
    with Changedir(job.name[1:]) as pwd:
        Extract = namedtuple('standInfo',
                             ['success', 'path', 'energy', 'a', 'b', 'c'])
        if not os.path.exists("STANDINFO"):
            with open("STANDINFO", "wb") as wfile:
                energy = job.params['result'].total_energy
                a, b, c = standabc(job)
                path = job.name[1:]
                Et = Extract(True, path, energy, a, b, c)
                dump((True, path, energy, a, b, c), wfile)
            return Et
        else:
            with open("STANDINFO", "rb") as rfile:
                success, path, energy, a, b, c = load(rfile)
            return Extract(success, path, energy, a, b, c)
Exemple #10
0
def aflowkpoints(outdir, poscarsource):
    import os
    outdir = gettmpdir()
    copyorlinkfile(outdir, "POSCAR", poscarsource, "cp")
    from pylada.misc import Changedir
    with Changedir(outdir) as pwd:
        with open("KPOINTS", "w") as wfile:
            tmp = []
            try:
                for i in os.popen("aflow --kpath < POSCAR "):
                    tmp.append(i)
                index = tmp.index("Line-mode\n")
                kpoints = tmp[index - 2:-1]
                if len(kpoints) < 1:
                    raise Exception("aflow error")
            except Exception as e:
                raise e
            wfile.writelines(kpoints)
        kpoints = os.path.abspath("KPOINTS")
    return kpoints
Exemple #11
0
def stupidcompute(outdir, common, **kwargs):
    import os
    import pylada
    from pylada.misc import Changedir
    copyorlinkfile(outdir, "POSCAR", kwargs["poscar"], "cp")
    copyorlinkfile(outdir, "INCAR", kwargs["incar"], "cp")
    copyorlinkfile(outdir, "POTCAR", kwargs["potcar"], "cp")
    copyorlinkfile(outdir, "CHGCAR", kwargs["chgcar"], "cp")
    copyorlinkfile(outdir, "WAVECAR", kwargs["wavecar"], "cp")
    #copyorlinkfile(outdir,"KPOINTS",kwargs["kpoints"],"cp")
    try:
        with Changedir(outdir) as pwd:
            tmp = []
            for i in os.popen(pylada.vasp_program):
                tmp.append(i)
            with open("stdout", "w") as wfile:
                wfile.writelines(i)
    except Exception as e:
        print("path:" + outdir)
        raise e
    return result
Exemple #12
0
def hsekpoints(poscarsource, ibzkpt, symlsource=None):
    import os
    outdir = gettmpdir()
    copyorlinkfile(outdir, "POSCAR", poscarsource, "cp")
    with open(ibzkpt, "r") as rfile:
        kpoints = rfile.readlines()
    line = int(kpoints[1].strip())
    kpoints = kpoints[:3 + line]
    from pylada.misc import Changedir
    with Changedir(outdir) as pwd:
        if symlsource == None:
            symlsource = "syml_" + getSpaceGroupName() + "_hse"
            symlsource = os.path.join(os.environ["SYML"], symlsource)
            copyorlinkfile("./", "syml", symlsource, "ln -s")
        os.system("adogk")
        zkpoints = []
        for i in os.popen("cat inp.kpt | awk '{print $1,$2,$3,0.00000}'"):
            zkpoints.append(i)
        kpoints[1] = str(len(kpoints) + len(zkpoints) - 3) + "\n"
        kpoints.extend(zkpoints)
        with open("KPOINTS", "w") as wfile:
            wfile.writelines(kpoints)
        kpoints = os.path.abspath("KPOINTS")
    return kpoints
Exemple #13
0
def test(executable):
    """ Tests ProgramProcess. Includes failure modes.  """
    from tempfile import mkdtemp
    from os.path import join
    from shutil import rmtree
    from pylada.process.program import ProgramProcess
    from pylada.process import Fail, NotStarted
    from pylada.misc import Changedir
    from pylada import default_comm as comm
    from functional import ExtractSingle as Extract
    dir = mkdtemp()
    try:
        with Changedir(dir) as pwd:
            pass
        stdout = join(dir, 'stdout')
        program = ProgramProcess(executable,
                                 outdir=dir,
                                 cmdline=['--sleep', 0, '--order', 4],
                                 stdout=stdout,
                                 dompi=True)
        # program not started. should fail.
        try:
            program.poll()
        except NotStarted:
            pass
        else:
            raise Exception()
        try:
            program.wait()
        except NotStarted:
            pass
        else:
            raise Exception()

        # now starting for real.
        assert program.start(comm) == False
        assert program.process is not None
        while not program.poll():
            continue
        extract = Extract(stdout)
        assert extract.success
        assert abs(extract.pi - 3.146801e+00) < 1e-2 * extract.error
        assert abs(extract.error - 0.005207865) < 1e-2 * extract.error
        assert extract.comm['n'] == comm['n']
        # restart
        assert program.process is None
        program.start(comm)
        assert program.process is None
    finally:
        rmtree(dir)

    # fail on poll
    try:
        with Changedir(dir) as pwd:
            pass
        stdout = join(dir, 'stdout')
        program = ProgramProcess(executable,
                                 outdir=dir,
                                 stderr=join(dir, 'shit'),
                                 cmdline=['--sleep', 0, '--order', 666],
                                 stdout=stdout,
                                 dompi=True)
        program.start(comm)
        while not program.poll():
            continue
    except Fail:
        pass
    except:
        raise
    else:
        raise Exception()
    finally:
        rmtree(dir)

    # fail on wait
    try:
        with Changedir(dir) as pwd:
            pass
        stdout = join(dir, 'stdout')
        program = ProgramProcess(executable,
                                 outdir=dir,
                                 stderr=join(dir, 'shit'),
                                 cmdline=['--sleep', 0, '--order', 666],
                                 stdout=stdout,
                                 dompi=True)
        program.start(comm)
        program.wait()
    except Fail:
        pass
    else:
        raise Exception()
    finally:
        rmtree(dir)

    try:
        with Changedir(dir) as pwd:
            pass
        stdout = join(dir, 'stdout')
        program = ProgramProcess(executable,
                                 outdir=dir,
                                 stderr=join(dir, 'shit'),
                                 cmdline=['--sleep', 0, '--order', 6666],
                                 stdout=stdout,
                                 dompi=True)
        program.start(comm)
        while not program.poll():
            continue
    except Fail:
        pass
    else:
        raise Exception()
    finally:
        rmtree(dir)
Exemple #14
0
def launch_program(cmdl,
                   comm=None,
                   formatter=None,
                   env=None,
                   stdout=None,
                   stderr=None,
                   stdin=None,
                   outdir=None):
    """ Command used to launch a program.
  
      This function launches external programs for Pylada. It is included as a
      global so that it can be adapted to different computing environment. It
      also makes it easier to debug Pylada's mpi configuration when installing on
      a new machine.

      .. note::

        The number one configuration problem is an incorrect
        :py:data:`~pylada.mpirun_exe`.

      .. note::
       
        The number two configuration problem is mpi-placement (eg how to launch
        two different mpi program simultaneously in one PBS/SLURM job). First
        read the manual for the mpi environment on the particular machine Pylada
        is installed on. Then adapt
        :py:function:`~pylada.machine_dependent_call_modifier` by redeclaring it
        in $HOME/.pylada.

      :param str cmld: 
        Command-line string. It will be formatted using ``formatter`` or
        ``comm`` if either are present. Otherwise, it should be exactly the
        (bash) command-prompt.
      :param comm: 
        Should contain everythin needed to launch an mpi call. 
        In practice, it is copied from :py:data:`~pylada.default_comm` and
        modified for the purpose of a particular call (e.g. could use fewer
        than all available procs)
      :type comm: :py:class:`~pylada.process.mpi.Communicator`
      :param dict formatter:
        Dictionary with which to format the communicator. If ``comm`` is
        present, then it will be updated with ``comm``'s input.
      :param dict env: 
        Dictionary containing the environment variables in which to do call.
      :param stdout:
        File object to which to hook-up the standard output. See Popen_.
      :param stderr:
        File object to which to hook-up the standard error. See Popen_.
      :param str outdir:
        Path to the working directory.

      .. _Popen:: http://docs.python.org/library/subprocess.html#popen-constructor
  """
    from shlex import split as shlex_split
    from subprocess import Popen
    from pylada import machine_dependent_call_modifier
    from pylada.misc import Changedir
    from pylada.misc import bugLev
    from pylada.misc import testValidProgram

    if bugLev >= 5:
        print "config/mpi: launch_program: entry"
        print "  parm cmdl: %s  type: %s" % (
            cmdl,
            type(cmdl),
        )
        print "  parm comm: %s  type: %s" % (
            comm,
            type(comm),
        )
        print "  parm formatter: %s  type: %s" % (
            formatter,
            type(formatter),
        )
        print "  parm env: %s  type: %s" % (
            env,
            type(env),
        )
        print "  parm outdir: %s  type: %s" % (
            outdir,
            type(outdir),
        )

    # At this point formatter is {"program": vasp}
    # and cmdl is "mpirun -n {n} {placement} {program}"

    # Set in formatter: 'placement': '', 'ppn': 8, 'n': 8
    # make sure that the formatter contains stuff from the communicator, eg the
    # number of processes.
    if comm is not None and formatter is not None:
        formatter.update(comm)
    if bugLev >= 5:
        print "config/mpi: launch_program: comm mod formatter: %s" % (
            formatter, )

    # Set in formatter: 'placement': '-machinefile /home.../pylada_commtempfile'
    # Stuff that will depend on the supercomputer.
    machine_dependent_call_modifier(formatter, comm, env)
    if bugLev >= 5:
        print "config/mpi: launch_program: mach mod formatter: %s" % (
            formatter, )

    if bugLev >= 5:
        print "config/mpi: launch_program: plac formatter: %s" % (formatter, )

    # if a formatter exists, then use it on the cmdl string.
    if formatter is not None:
        cmdl = cmdl.format(**formatter)
        # otherwise, if comm is not None, use that.
    elif comm is not None:
        cmdl = cmdl.format(**comm)

    # Split command from string to list
    if bugLev >= 1:
        print "config/mpi: launch_program: final full cmdl: \"%s\"" % (cmdl, )
    cmdl = shlex_split(cmdl)
    if bugLev >= 1:
        print "config/mpi: launch_program: final split cmdl: %s" % (cmdl, )
        print "config/mpi: launch_program: final stdout: %s" % (stdout, )
        print "config/mpi: launch_program: final stderr: %s" % (stderr, )
        print "config/mpi: launch_program: final stdin: %s" % (stdin, )
        print "config/mpi: launch_program: final outdir: \"%s\"" % (outdir, )
        print "config/mpi: launch_program: final env: %s" % (env, )

    # makes sure the directory exists:
    if outdir is not None:
        with Changedir(outdir) as cwd:
            pass

    # Finally, start the process.
    popen = Popen(cmdl,
                  stdout=stdout,
                  stderr=stderr,
                  stdin=stdin,
                  cwd=outdir,
                  env=env)
    if bugLev >= 1:
        print "config/mpi: launch_program: popen: %s" % (popen, )
        print "config/mpi: launch_program: popen.pid: %s" % (popen.pid, )
    if testValidProgram: popen.wait()
    return popen
Exemple #15
0
def test_istart():
    from time import sleep
    from collections import namedtuple
    from pickle import loads, dumps
    from os import remove, makedirs
    from os.path import join, exists
    from shutil import rmtree
    from tempfile import mkdtemp
    from pylada.vasp.files import WAVECAR
    from pylada.vasp import Vasp
    from pylada.misc import Changedir

    Extract = namedtuple("Extract", ['directory', 'success'])
    a = Vasp()
    o = a._input['istart']
    d = {'IStart': o.__class__}

    directory = mkdtemp()
    if directory in ['/tmp/test', '/tmp/test/']:
        if exists(directory): rmtree(directory)
        makedirs(directory)
    try:
        assert a.istart == 'auto'
        assert o.output_map(vasp=a, outdir=directory)['istart'] == '0'
        assert eval(repr(o), d).value == 'auto'
        assert loads(dumps(o)).value == 'auto'

        restartdir = join(directory, 'restart')
        with Changedir(restartdir) as pwd:
            pass
        with open(join(restartdir, WAVECAR), 'w') as file:
            file.write('hello')

        # do not copy if not successful
        a.restart = Extract(restartdir, False)
        assert o.output_map(vasp=a, outdir=directory)['istart'] == '0'
        assert not exists(join(directory, 'WAVECAR'))

        # do not copy if file is empty
        a.restart = Extract(restartdir, True)
        with open(join(restartdir, WAVECAR), 'w') as file:
            pass
        assert o.output_map(vasp=a, outdir=directory)['istart'] == '0'
        assert not exists(join(directory, 'WAVECAR'))

        # now should copy
        assert a.istart == 'auto'
        with open(join(restartdir, WAVECAR), 'w') as file:
            file.write('hello')
        assert o.output_map(vasp=a, outdir=directory)['istart'] == '1'
        assert exists(join(directory, 'WAVECAR'))

        # check it copies only latest file.
        with open(join(restartdir, WAVECAR), 'w') as file:
            file.write('hello')
            file.flush()
        with open(join(restartdir, WAVECAR), 'r') as file:
            pass
        sleep(1.5)
        with open(join(directory, WAVECAR), 'w') as file:
            file.write('hello world')
        with open(join(directory, WAVECAR), 'r') as file:
            pass
        assert o.output_map(vasp=a, outdir=directory)['istart'] == '1'
        assert exists(join(directory, 'WAVECAR'))
        with open(join(directory, WAVECAR), 'r') as file:
            assert file.read().rstrip().lstrip() == 'hello world'

        sleep(0.2)
        with open(join(restartdir, WAVECAR), 'w') as file:
            file.write('hello')
        assert o.output_map(vasp=a, outdir=directory)['istart'] == '1'
        assert exists(join(directory, 'WAVECAR'))
        with open(join(directory, WAVECAR), 'r') as file:
            assert file.read().rstrip().lstrip() == 'hello'

        # check if scratch is required
        remove(join(directory, WAVECAR))
        a.istart = 'scratch'
        assert a.istart == 'scratch'
        assert o.output_map(vasp=a, outdir=directory)['istart'] == '0'
        assert eval(repr(o), d).value == 'scratch'
        assert loads(dumps(o)).value == 'scratch'

    finally:
        if directory not in ['/tmp/test', '/tmp/test/'] and exists(directory):
            rmtree(directory)
Exemple #16
0
def test_icharg():
    from time import sleep
    from collections import namedtuple
    from pickle import loads, dumps
    from os import remove, makedirs
    from os.path import join, exists
    from shutil import rmtree
    from tempfile import mkdtemp
    from pylada.vasp.files import WAVECAR, CHGCAR
    from pylada.vasp import Vasp
    from pylada.misc import Changedir
    from pylada.error import ValueError

    Extract = namedtuple("Extract", ['directory', 'success'])
    a = Vasp()
    o = a._input['icharg']
    d = {'ICharg': o.__class__}

    directory = mkdtemp()
    if directory in ['/tmp/test', '/tmp/test/']:
        if exists(directory): rmtree(directory)
        makedirs(directory)
    try:
        assert a.icharg == 'auto'
        assert o.output_map(vasp=a, outdir=directory)['icharg'] == '2'
        assert eval(repr(o), d).value == 'auto'
        assert loads(dumps(o)).value == 'auto'

        restartdir = join(directory, 'restart')
        with Changedir(restartdir) as pwd:
            pass
        with open(join(restartdir, CHGCAR), 'w') as file:
            file.write('hello')
        with open(join(restartdir, WAVECAR), 'w') as file:
            file.write('hello')

        # do not copy if not successful
        a.restart = Extract(restartdir, False)
        assert o.output_map(vasp=a, outdir=directory)['icharg'] == '2'
        assert not exists(join(directory, CHGCAR))
        assert not exists(join(directory, WAVECAR))

        # do not copy if empty
        with open(join(restartdir, CHGCAR), 'w') as file:
            pass
        with open(join(restartdir, WAVECAR), 'w') as file:
            pass
        a.restart = Extract(restartdir, True)
        assert o.output_map(vasp=a, outdir=directory)['icharg'] == '2'
        assert not exists(join(directory, CHGCAR))
        assert not exists(join(directory, WAVECAR))

        # now copy only CHGCAR
        with open(join(restartdir, CHGCAR), 'w') as file:
            file.write('hello')
        a.restart = Extract(restartdir, True)
        assert o.output_map(vasp=a, outdir=directory)['icharg'] == '1'
        assert exists(join(directory, CHGCAR))
        assert not exists(join(directory, WAVECAR))
        remove(join(directory, CHGCAR))
        # now copy only CHGCAR with scf
        a.nonscf = True
        a.restart = Extract(restartdir, True)
        assert o.output_map(vasp=a, outdir=directory)['icharg'] == '11'
        assert exists(join(directory, CHGCAR))
        assert not exists(join(directory, WAVECAR))
        remove(join(directory, CHGCAR))

        # now copy both
        a.nonscf = False
        with open(join(restartdir, WAVECAR), 'w') as file:
            file.write('hello')
        a.restart = Extract(restartdir, True)
        assert o.output_map(vasp=a, outdir=directory)['icharg'] == '0'
        assert exists(join(directory, CHGCAR))
        assert exists(join(directory, WAVECAR))

        # now copy both with scf
        a.nonscf = True
        a.restart = Extract(restartdir, True)
        assert o.output_map(vasp=a, outdir=directory)['icharg'] == '10'
        assert exists(join(directory, CHGCAR))
        assert exists(join(directory, WAVECAR))

        # now check that latest is copied
        remove(join(restartdir, CHGCAR))
        remove(join(directory, CHGCAR))
        sleep(1.2)
        with open(join(directory, WAVECAR), 'w') as file:
            file.write('hello world')
        with open(join(directory, WAVECAR), 'r') as file:
            pass  # Buffering issues..
        a.nonscf = False
        a.restart = Extract(restartdir, True)
        assert o.output_map(vasp=a, outdir=directory)['icharg'] == '0'
        assert exists(join(directory, WAVECAR))
        with open(join(directory, WAVECAR), 'r') as file:
            assert file.read().rstrip().lstrip() == 'hello world'
        with open(join(directory, WAVECAR), 'r') as file:
            assert file.read().rstrip().lstrip() != 'hello'

        with open(join(restartdir, WAVECAR), 'w') as file:
            file.write('hello')
        assert o.output_map(vasp=a, outdir=directory)['icharg'] == '0'
        assert exists(join(directory, WAVECAR))
        with open(join(directory, WAVECAR), 'r') as file:
            assert file.read().rstrip().lstrip() == 'hello'
        with open(join(directory, WAVECAR), 'r') as file:
            assert file.read().rstrip().lstrip() != 'hello world'

        # makes sure requests are honored
        # tries request. Should fail since CHGCAR does not exist.
        remove(join(directory, WAVECAR))
        a.icharg = 'chgcar'
        assert a.icharg == 'chgcar'
        try:
            o.output_map(vasp=a, outdir=directory)
        except ValueError:
            pass
        else:
            raise Exception()
        # now try for gold.
        with open(join(restartdir, CHGCAR), 'w') as file:
            file.write('hello')
        assert o.output_map(vasp=a, outdir=directory)['icharg'] == '1'
        assert exists(join(directory, CHGCAR))
        assert not exists(join(directory, WAVECAR))
        assert eval(repr(o), d).value == 'chgcar'
        assert loads(dumps(o)).value == 'chgcar'

    finally:
        if directory not in ['/tmp/test', '/tmp/test/'] and exists(directory):
            rmtree(directory)
Exemple #17
0
def functional(vasp, structure, programs, func_param, outdir=None, value=False, **kwargs):
  """ A simulated annealing functional """
  from copy import deepcopy
  from pickle import dump
  from random import random
  from pylada.misc import Changedir
  from os.path import join
  from pylada.crystal import vasp_ordered
  from pylada.misc import  RelativePath
  import numpy as np
  import move
  import str_util
  import deposit
  import os
  import shutil

  structure_tmp = deepcopy(structure)
  vasp = deepcopy(vasp)
  outdir = getcwd() if outdir is None else RelativePath(outdir).path
  #structure = vasp_ordered(structure)
  if programs  is None: programs = [{}]

  # TODO: restart feature does not work in pylada
  step_todo = 0
  output = None

  mode  = func_param.get("mode", "rls")
  min_dist_AA = func_param.get("min_dist_AA", 1.0)
  min_dist_AB = func_param.get("min_dist_AB", 1.0)
  T = func_param.get("T", 1.)
  move_type =   func_param.get("move_type", "gauss")
  print 'MOVE type', move_type
  restart = func_param.get("restart", False)

  beta = 1./(8.617e-5 * T)


  #write_parameters(outdir, **kwargs)
  Nat = len(structure)
  if restart:
      for step_todo, program in enumerate(programs): 
        restartdir = join(outdir, str(step_todo))
        with Changedir(restartdir) as pwd:
          last_iter = 0
          if os.path.exists('energy.dat'):
            with open("energy.dat", "r") as file:
              lines = file.readlines()
              last_iter = int(lines[-1][:10])

          iter_todo =  programs[step_todo]["prog_param"]["Nstep"] - last_iter  
          print 'restart info: step_todo, iter_todo', step_todo, iter_todo

          if iter_todo > 0:
            if os.path.exists('accept.dat'):
              with open("accept.dat", "r") as file:
                lines = file.readlines()
                last_accept_iter =  int(lines[-1][:10])
                last_accept_iter_dir = join(outdir, join(join(str(step_todo), "data"), str(last_accept_iter) +'_0' ) )
                with Changedir(last_accept_iter_dir) as pwd:
                  structure_tmp = vasp.Extract().structure
                  E = float(vasp.Extract().energy_sigma0)
                N_min_e = last_accept_iter 
            else:
              N_min_e = 0
              E = 0.0

            programs[step_todo]["prog_param"]["Nstep"] = iter_todo
            with open("tot_iter", "w") as file:
              file.write( "last_iter %d\n" %  last_iter)
              file.write( "iter_todo %d\n" %  iter_todo)
              file.write( "step %d\n" %  step_todo)
              file.write( "Energy %.3f\n" %  E)
            break
          else:
            if os.path.exists('accept.dat'):
              with open("accept.dat", "r") as file:
                lines = file.readlines()
                last_accept_iter =  int(lines[-1][:10])
                last_accept_iter_dir = join(outdir, join(join(str(step_todo), "data"), str(last_accept_iter) +'_0' ) )
                with Changedir(last_accept_iter_dir) as pwd:
                  structure_tmp = vasp.Extract().structure
            with open("tot_iter", "w") as file:
              file.write( "success")
              file.write( "last_iter %d\n" %  last_iter)
              file.write( "iter_todo %d\n" %  iter_todo)
              file.write( "step %d\n" %  step_todo)
  else:
    step_todo = 0
    last_iter = 0
    N_min_e = 0
    E = 0.0
    str_util.relax_structure_v1(structure_tmp, min_dist_AA, min_dist_AB)

  '''
  params = kwargs.copy()
  vasp_tmp = deepcopy(vasp)
  output = vasp_tmp\
               (\
                 structure_tmp,
                 outdir = join(outdir, join(str(step_todo), join("init", str(last_iter) ) )),\
                 relaxation = "static",\
                 **params
               )
  output = vasp_tmp.Extract(join(outdir, join(str(step_todo), join("init", str(last_iter) ) )))
  structure = output.structure
  assert output.success, RuntimeError("VASP run did not succeed.")

  structure_tmp = structure.copy()
  E = float(output.energy_sigma0)
  '''
  disp = 0

  for step, program in enumerate(programs[step_todo:]): 
      print 'entering step', step
      params = kwargs.copy()
      params.update(program["vasp_param"])
      vasp_tmp = deepcopy(vasp)
      if 'add_param' in program.keys():
        for key in program['add_param'].keys():
          if key not in vasp_tmp.__dict__['_input'].keys():
            print key
            vasp_tmp.add_keyword(key, program['add_param'][key])
      if 'vasp_param_final' in program.keys():
        vasp_final = deepcopy(vasp)
        params_final = kwargs.copy()
        params_final.update(program["vasp_param_final"])
        if 'add_param_final' in program.keys():
          for key in program['add_param_final'].keys():
            if key not in vasp_final.__dict__['_input'].keys():
              print key
              vasp_final.add_keyword(key, program['add_param_final'][key])


      for N in range(1, program["prog_param"]["Nstep"]+1):
        dir_out = join(outdir, join(str(step+step_todo), join("data", str(N+last_iter)+'_0' ) ))
        if os.path.isdir(dir_out): shutil.rmtree(dir_out, ignore_errors=True)
        print 'entering iter', N
        output = vasp_tmp\
               (\
                 structure_tmp,\
                 outdir = dir_out,\
                 #relaxation = 'ionic',\
                 #relaxation = 'static',\
                 **params\
               )
        output = vasp_tmp.Extract(dir_out)
        assert output.success, RuntimeError("VASP run did not succeed.")
        if 'vasp_param_final' in program.keys():
          output = vasp_final\
               (\
                 output.structure,\
                 outdir = join(outdir, join(str(step+step_todo), join("data", str(N+last_iter) ) )),\
                 #relaxation = 'ionic',\
                 #relaxation = 'static',\
                 **params_final\
               )
          output = vasp_tmp.Extract(join(outdir, join(str(step+step_todo), join("data", str(N+last_iter)) )) )
          assert output.success, RuntimeError("VASP run did not succeed.")

        fmax = deposit.get_mforce(output.structure)
        E_new = float(output.energy_sigma0)
        dE = E_new - E
        disp_final = str_util.get_disp(structure_tmp, output.structure)
        type_run = 'i' 
        if 1:
          dE = E_new - E
          if dE <0:
            E = float(E_new)
            accept = 1
            structure = output.structure.copy()
            structure_tmp = output.structure.copy()
            structure_min_e = output.structure.copy()
            N_min_e = N+last_iter
          else:
            A_move = np.exp( -beta * dE)
            if A_move > np.random.random():
              E = float(E_new)
              accept = A_move
              structure = output.structure.copy()
              structure_tmp = output.structure.copy()
            else:
              structure_tmp = structure.copy()
              accept = 0
          with Changedir(join(outdir, str(step+step_todo)) ) as pwd:
            #output.structure.write_poscar('poscar_%d'%N )
            with open("energy.dat", "a") as file:
              tag = '%10d %10.5f %10.5f %10.3f %10.3f %10.3f %.2f %s %d\n' %(N + last_iter, E_new/Nat, dE/Nat, disp, disp_final, fmax, accept, type_run, N_min_e)
              file.write(tag) # need to print structure new
            if accept:
              with open("accept.dat", "a") as file:
                tag = '%10d %10.5f %10.5f %10.3f\n' %(N+last_iter, E_new/Nat, dE/Nat, disp_final)
                file.write(tag) # need to print structure new
              with open("traj.xyz", "a") as file:
                tag = 'disp  %.3f' %(disp)
                file.write( move.print_to_xyz(structure, tag)) # need to print structure new
            else:
              #rmtree(join(outdir, join("data", str(N))))
              pass
            with open("accept_all.dat", "a") as file:
              tag = '%10d %10.5f\n' %(N+last_iter, E/Nat)
              file.write(tag) # need to print structure new
          if program["prog_param"]["move_atoms"]:
            if mode == 'rls':
                move.move_atom(structure_tmp, move_type=move_type, seed=N+last_iter, min_dist_AA=min_dist_AA, min_dist_AB=min_dist_AB, relax = True)
                disp = str_util.get_disp(structure_tmp, structure)
            else:
              pass
          else:
            structure_tmp = output.structure.copy()

        if N == program["prog_param"]["Nstep"] and program["prog_param"]["move_atoms"]:
          structure = structure_min_e.copy()
          with Changedir(join(outdir, str(step+step_todo)) ) as pwd:
            with open("N_min_e.dat", "w") as file:
              tag = '%d' %(N_min_e)
              file.write( tag )
        assert output.success, RuntimeError("VASP run did not succeed.")


    # performs final calculation
  output = vasp\
             (\
               structure, \
               outdir = outdir,\
               relaxation = "static",\
               restart = output, \
               **kwargs\
             )
  return Extract(outdir)