<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>twod_materials.electronic_structure.startup &#8212; twod_materials 1.0 documentation</title>
    
    <link rel="stylesheet" href="../../../_static/alabaster.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../',
        VERSION:     '1.0',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true,
        SOURCELINK_SUFFIX: '.txt'
      };
    </script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <link rel="author" title="About these documents" href="../../../about.html" />
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />
   
  <link rel="stylesheet" href="../../../_static/custom.css" type="text/css" />
  
  
  <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />

  </head>
  <body role="document">
  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <h1>Source code for twod_materials.electronic_structure.startup</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">__future__</span> <span class="k">import</span> <span class="n">print_function</span><span class="p">,</span> <span class="n">division</span><span class="p">,</span> <span class="n">unicode_literals</span>

<span class="kn">import</span> <span class="nn">os</span>

<span class="kn">from</span> <span class="nn">twod_materials.utils</span> <span class="k">import</span> <span class="o">*</span>
<span class="kn">from</span> <span class="nn">twod_materials.stability.startup</span> <span class="k">import</span> <span class="n">relax</span>

<span class="kn">from</span> <span class="nn">pymatgen.io.vasp.inputs</span> <span class="k">import</span> <span class="n">Kpoints</span><span class="p">,</span> <span class="n">Incar</span>
<span class="kn">from</span> <span class="nn">pymatgen.symmetry.bandstructure</span> <span class="k">import</span> <span class="n">HighSymmKpath</span>
<span class="kn">from</span> <span class="nn">pymatgen.core.structure</span> <span class="k">import</span> <span class="n">Structure</span>

<span class="kn">from</span> <span class="nn">monty.serialization</span> <span class="k">import</span> <span class="n">loadfn</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>

<span class="kn">import</span> <span class="nn">math</span>

<span class="kn">import</span> <span class="nn">twod_materials</span>


<span class="n">PACKAGE_PATH</span> <span class="o">=</span> <span class="n">twod_materials</span><span class="o">.</span><span class="n">__file__</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;__init__.pyc&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>
<span class="n">PACKAGE_PATH</span> <span class="o">=</span> <span class="n">PACKAGE_PATH</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;__init__.py&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>
<span class="n">PACKAGE_PATH</span> <span class="o">=</span> <span class="s1">&#39;/&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">PACKAGE_PATH</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">)[:</span><span class="o">-</span><span class="mi">2</span><span class="p">])</span>

<span class="k">try</span><span class="p">:</span>
    <span class="n">config_vars</span> <span class="o">=</span> <span class="n">loadfn</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">expanduser</span><span class="p">(</span><span class="s1">&#39;~&#39;</span><span class="p">),</span> <span class="s1">&#39;config.yaml&#39;</span><span class="p">))</span>
<span class="k">except</span><span class="p">:</span>
    <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;WARNING: No config.yaml file was found. please configure the &#39;</span>\
    <span class="s1">&#39;config.yaml and put it in your home directory.&#39;</span><span class="p">)</span>
    <span class="c1"># Still set them for testing purposes.</span>
    <span class="n">config_vars</span> <span class="o">=</span> <span class="n">loadfn</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">PACKAGE_PATH</span><span class="p">,</span> <span class="s1">&#39;config.yaml&#39;</span><span class="p">))</span>
    <span class="n">VASP</span> <span class="o">=</span> <span class="n">config_vars</span><span class="p">[</span><span class="s1">&#39;normal_binary&#39;</span><span class="p">]</span>
<span class="k">if</span> <span class="s1">&#39;queue_system&#39;</span> <span class="ow">in</span> <span class="n">config_vars</span><span class="p">:</span>
    <span class="n">QUEUE</span> <span class="o">=</span> <span class="n">config_vars</span><span class="p">[</span><span class="s1">&#39;queue_system&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
<span class="k">elif</span> <span class="s1">&#39;/ufrc/&#39;</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">getcwd</span><span class="p">():</span>
    <span class="n">QUEUE</span> <span class="o">=</span> <span class="s1">&#39;slurm&#39;</span>
<span class="k">elif</span> <span class="s1">&#39;/scratch/&#39;</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">getcwd</span><span class="p">():</span>
    <span class="n">QUEUE</span> <span class="o">=</span> <span class="s1">&#39;pbs&#39;</span>


<div class="viewcode-block" id="run_pbe_calculation"><a class="viewcode-back" href="../../../electronic_structure.html#twod_materials.electronic_structure.startup.run_pbe_calculation">[docs]</a><span class="k">def</span> <span class="nf">run_pbe_calculation</span><span class="p">(</span><span class="n">dim</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">submit</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">force_overwrite</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Setup and submit a normal PBE calculation for band structure along</span>
<span class="sd">    high symmetry k-paths.</span>

<span class="sd">    Args:</span>
<span class="sd">        dim (int): 2 for relaxing a 2D material, 3 for a 3D material.</span>
<span class="sd">        submit (bool): Whether or not to submit the job.</span>
<span class="sd">        force_overwrite (bool): Whether or not to overwrite files</span>
<span class="sd">            if an already converged vasprun.xml exists in the</span>
<span class="sd">            directory.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">PBE_INCAR_DICT</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;EDIFF&#39;</span><span class="p">:</span> <span class="mi">1</span><span class="n">e</span><span class="o">-</span><span class="mi">6</span><span class="p">,</span> <span class="s1">&#39;IBRION&#39;</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="s1">&#39;ISIF&#39;</span><span class="p">:</span> <span class="mi">3</span><span class="p">,</span>
                      <span class="s1">&#39;ISMEAR&#39;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;NSW&#39;</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span> <span class="s1">&#39;LVTOT&#39;</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span> <span class="s1">&#39;LVHAR&#39;</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span>
                      <span class="s1">&#39;LORBIT&#39;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;LREAL&#39;</span><span class="p">:</span> <span class="s1">&#39;Auto&#39;</span><span class="p">,</span> <span class="s1">&#39;NPAR&#39;</span><span class="p">:</span> <span class="mi">4</span><span class="p">,</span>
                      <span class="s1">&#39;PREC&#39;</span><span class="p">:</span> <span class="s1">&#39;Accurate&#39;</span><span class="p">,</span> <span class="s1">&#39;LWAVE&#39;</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span> <span class="s1">&#39;SIGMA&#39;</span><span class="p">:</span> <span class="mf">0.1</span><span class="p">,</span>
                      <span class="s1">&#39;ENCUT&#39;</span><span class="p">:</span> <span class="mi">500</span><span class="p">,</span> <span class="s1">&#39;ISPIN&#39;</span><span class="p">:</span> <span class="mi">2</span><span class="p">}</span>

    <span class="n">directory</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">getcwd</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>

    <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isdir</span><span class="p">(</span><span class="s1">&#39;pbe_bands&#39;</span><span class="p">):</span>
        <span class="n">os</span><span class="o">.</span><span class="n">mkdir</span><span class="p">(</span><span class="s1">&#39;pbe_bands&#39;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">force_overwrite</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">is_converged</span><span class="p">(</span><span class="s1">&#39;pbe_bands&#39;</span><span class="p">):</span>
        <span class="n">os</span><span class="o">.</span><span class="n">system</span><span class="p">(</span><span class="s1">&#39;cp CONTCAR pbe_bands/POSCAR&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="s1">&#39;POTCAR&#39;</span><span class="p">):</span>
            <span class="n">os</span><span class="o">.</span><span class="n">system</span><span class="p">(</span><span class="s1">&#39;cp POTCAR pbe_bands/&#39;</span><span class="p">)</span>
        <span class="n">PBE_INCAR_DICT</span><span class="o">.</span><span class="n">update</span><span class="p">({</span><span class="s1">&#39;MAGMOM&#39;</span><span class="p">:</span> <span class="n">get_magmom_string</span><span class="p">()})</span>
        <span class="n">Incar</span><span class="o">.</span><span class="n">from_dict</span><span class="p">(</span><span class="n">PBE_INCAR_DICT</span><span class="p">)</span><span class="o">.</span><span class="n">write_file</span><span class="p">(</span><span class="s1">&#39;pbe_bands/INCAR&#39;</span><span class="p">)</span>
        <span class="n">structure</span> <span class="o">=</span> <span class="n">Structure</span><span class="o">.</span><span class="n">from_file</span><span class="p">(</span><span class="s1">&#39;POSCAR&#39;</span><span class="p">)</span>
        <span class="n">kpath</span> <span class="o">=</span> <span class="n">HighSymmKpath</span><span class="p">(</span><span class="n">structure</span><span class="p">)</span>
        <span class="n">Kpoints</span><span class="o">.</span><span class="n">automatic_linemode</span><span class="p">(</span><span class="mi">20</span><span class="p">,</span> <span class="n">kpath</span><span class="p">)</span><span class="o">.</span><span class="n">write_file</span><span class="p">(</span><span class="s1">&#39;pbe_bands/KPOINTS&#39;</span><span class="p">)</span>
        <span class="n">os</span><span class="o">.</span><span class="n">chdir</span><span class="p">(</span><span class="s1">&#39;pbe_bands&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">dim</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
            <span class="n">remove_z_kpoints</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">QUEUE</span> <span class="o">==</span> <span class="s1">&#39;pbs&#39;</span><span class="p">:</span>
            <span class="n">write_pbs_runjob</span><span class="p">(</span><span class="n">directory</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">16</span><span class="p">,</span> <span class="s1">&#39;800mb&#39;</span><span class="p">,</span> <span class="s1">&#39;6:00:00&#39;</span><span class="p">,</span> <span class="n">VASP</span><span class="p">)</span>
            <span class="n">submission_command</span> <span class="o">=</span> <span class="s1">&#39;qsub runjob&#39;</span>

        <span class="k">elif</span> <span class="n">QUEUE</span> <span class="o">==</span> <span class="s1">&#39;slurm&#39;</span><span class="p">:</span>
            <span class="n">write_slurm_runjob</span><span class="p">(</span><span class="n">directory</span><span class="p">,</span> <span class="mi">16</span><span class="p">,</span> <span class="s1">&#39;800mb&#39;</span><span class="p">,</span> <span class="s1">&#39;6:00:00&#39;</span><span class="p">,</span> <span class="n">VASP</span><span class="p">)</span>
            <span class="n">submission_command</span> <span class="o">=</span> <span class="s1">&#39;sbatch runjob&#39;</span>

        <span class="k">if</span> <span class="n">submit</span><span class="p">:</span>
            <span class="n">os</span><span class="o">.</span><span class="n">system</span><span class="p">(</span><span class="n">submission_command</span><span class="p">)</span>

        <span class="n">os</span><span class="o">.</span><span class="n">chdir</span><span class="p">(</span><span class="s1">&#39;../&#39;</span><span class="p">)</span></div>


<div class="viewcode-block" id="run_hse_prep_calculation"><a class="viewcode-back" href="../../../electronic_structure.html#twod_materials.electronic_structure.startup.run_hse_prep_calculation">[docs]</a><span class="k">def</span> <span class="nf">run_hse_prep_calculation</span><span class="p">(</span><span class="n">dim</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">submit</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Submits a quick static calculation to calculate the IBZKPT</span>
<span class="sd">    file using a smaller number of k-points (200/atom instead of</span>
<span class="sd">    1000/atom). The other outputs from this calculation are</span>
<span class="sd">    essentially useless.</span>

<span class="sd">    Args:</span>
<span class="sd">        dim (int): 2 for relaxing a 2D material, 3 for a 3D material.</span>
<span class="sd">        submit (bool): Whether or not to submit the job.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isdir</span><span class="p">(</span><span class="s1">&#39;hse_prep&#39;</span><span class="p">):</span>
        <span class="n">os</span><span class="o">.</span><span class="n">mkdir</span><span class="p">(</span><span class="s1">&#39;hse_prep&#39;</span><span class="p">)</span>
    <span class="n">os</span><span class="o">.</span><span class="n">chdir</span><span class="p">(</span><span class="s1">&#39;hse_prep&#39;</span><span class="p">)</span>
    <span class="n">os</span><span class="o">.</span><span class="n">system</span><span class="p">(</span><span class="s1">&#39;cp ../CONTCAR ./POSCAR&#39;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="s1">&#39;../POTCAR&#39;</span><span class="p">):</span>
        <span class="n">os</span><span class="o">.</span><span class="n">system</span><span class="p">(</span><span class="s1">&#39;cp POTCAR .&#39;</span><span class="p">)</span>
    <span class="n">relax</span><span class="p">(</span><span class="n">dim</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">submit</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
    <span class="n">incar_dict</span> <span class="o">=</span> <span class="n">Incar</span><span class="o">.</span><span class="n">from_file</span><span class="p">(</span><span class="s1">&#39;INCAR&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">as_dict</span><span class="p">()</span>
    <span class="n">incar_dict</span><span class="o">.</span><span class="n">update</span><span class="p">({</span><span class="s1">&#39;NSW&#39;</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span> <span class="s1">&#39;NELM&#39;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;LWAVE&#39;</span><span class="p">:</span> <span class="kc">False</span><span class="p">,</span> <span class="s1">&#39;LCHARG&#39;</span><span class="p">:</span> <span class="kc">False</span><span class="p">,</span>
                       <span class="s1">&#39;LAECHG&#39;</span><span class="p">:</span> <span class="kc">False</span><span class="p">})</span>
    <span class="n">Incar</span><span class="o">.</span><span class="n">from_dict</span><span class="p">(</span><span class="n">incar_dict</span><span class="p">)</span><span class="o">.</span><span class="n">write_file</span><span class="p">(</span><span class="s1">&#39;INCAR&#39;</span><span class="p">)</span>

    <span class="n">Kpoints</span><span class="o">.</span><span class="n">automatic_density</span><span class="p">(</span>
        <span class="n">Structure</span><span class="o">.</span><span class="n">from_file</span><span class="p">(</span><span class="s1">&#39;POSCAR&#39;</span><span class="p">),</span> <span class="mi">200</span>
    <span class="p">)</span><span class="o">.</span><span class="n">write_file</span><span class="p">(</span><span class="s1">&#39;KPOINTS&#39;</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">dim</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
        <span class="n">kpts_lines</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;KPOINTS&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">readlines</span><span class="p">()</span>

        <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;KPOINTS&#39;</span><span class="p">,</span> <span class="s1">&#39;w&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">kpts</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">kpts_lines</span><span class="p">[:</span><span class="mi">3</span><span class="p">]:</span>
                <span class="n">kpts</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
            <span class="n">kpts</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">kpts_lines</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="s1">&#39; &#39;</span>
                       <span class="o">+</span> <span class="n">kpts_lines</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="s1">&#39; 1&#39;</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">QUEUE</span> <span class="o">==</span> <span class="s1">&#39;pbs&#39;</span><span class="p">:</span>
        <span class="n">write_pbs_runjob</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">{}</span><span class="s1">_prep&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
            <span class="n">os</span><span class="o">.</span><span class="n">getcwd</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">)[</span><span class="o">-</span><span class="mi">2</span><span class="p">]),</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">16</span><span class="p">,</span> <span class="s1">&#39;800mb&#39;</span><span class="p">,</span> <span class="s1">&#39;6:00:00&#39;</span><span class="p">,</span> <span class="n">VASP</span><span class="p">)</span>
        <span class="n">submission_command</span> <span class="o">=</span> <span class="s1">&#39;qsub runjob&#39;</span>

    <span class="k">elif</span> <span class="n">QUEUE</span> <span class="o">==</span> <span class="s1">&#39;slurm&#39;</span><span class="p">:</span>
        <span class="n">write_slurm_runjob</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">{}</span><span class="s1">_prep&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
            <span class="n">os</span><span class="o">.</span><span class="n">getcwd</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">)[</span><span class="o">-</span><span class="mi">2</span><span class="p">]),</span> <span class="mi">16</span><span class="p">,</span> <span class="s1">&#39;800mb&#39;</span><span class="p">,</span> <span class="s1">&#39;6:00:00&#39;</span><span class="p">,</span> <span class="n">VASP</span><span class="p">)</span>
        <span class="n">submission_command</span> <span class="o">=</span> <span class="s1">&#39;sbatch runjob&#39;</span>

    <span class="k">if</span> <span class="n">submit</span><span class="p">:</span>
        <span class="n">os</span><span class="o">.</span><span class="n">system</span><span class="p">(</span><span class="n">submission_command</span><span class="p">)</span>

    <span class="n">os</span><span class="o">.</span><span class="n">chdir</span><span class="p">(</span><span class="s1">&#39;../&#39;</span><span class="p">)</span></div>


<div class="viewcode-block" id="run_hse_calculation"><a class="viewcode-back" href="../../../electronic_structure.html#twod_materials.electronic_structure.startup.run_hse_calculation">[docs]</a><span class="k">def</span> <span class="nf">run_hse_calculation</span><span class="p">(</span><span class="n">dim</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">submit</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">force_overwrite</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
                        <span class="n">destroy_prep_directory</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Setup/submit an HSE06 calculation to get an accurate band structure.</span>
<span class="sd">    Requires a previous IBZKPT from a standard DFT run. See</span>
<span class="sd">    http://cms.mpi.univie.ac.at/wiki/index.php/Si_bandstructure for more</span>
<span class="sd">    details.</span>

<span class="sd">    Args:</span>
<span class="sd">        dim (int): 2 for relaxing a 2D material, 3 for a 3D material.</span>
<span class="sd">        submit (bool): Whether or not to submit the job.</span>
<span class="sd">        force_overwrite (bool): Whether or not to overwrite files</span>
<span class="sd">            if an already converged vasprun.xml exists in the</span>
<span class="sd">            directory.</span>
<span class="sd">        destroy_prep_directory (bool): whether or not to remove</span>
<span class="sd">            (rm -r) the hse_prep directory, if it exists. This</span>
<span class="sd">            can help you to automatically clean up and save space.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">HSE_INCAR_DICT</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;LHFCALC&#39;</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span> <span class="s1">&#39;HFSCREEN&#39;</span><span class="p">:</span> <span class="mf">0.2</span><span class="p">,</span> <span class="s1">&#39;AEXX&#39;</span><span class="p">:</span> <span class="mf">0.25</span><span class="p">,</span>
                      <span class="s1">&#39;ALGO&#39;</span><span class="p">:</span> <span class="s1">&#39;D&#39;</span><span class="p">,</span> <span class="s1">&#39;TIME&#39;</span><span class="p">:</span> <span class="mf">0.4</span><span class="p">,</span> <span class="s1">&#39;NSW&#39;</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
                      <span class="s1">&#39;LVTOT&#39;</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span> <span class="s1">&#39;LVHAR&#39;</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span> <span class="s1">&#39;LORBIT&#39;</span><span class="p">:</span> <span class="mi">11</span><span class="p">,</span>
                      <span class="s1">&#39;LWAVE&#39;</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span> <span class="s1">&#39;NPAR&#39;</span><span class="p">:</span> <span class="mi">8</span><span class="p">,</span> <span class="s1">&#39;PREC&#39;</span><span class="p">:</span> <span class="s1">&#39;Accurate&#39;</span><span class="p">,</span>
                      <span class="s1">&#39;EDIFF&#39;</span><span class="p">:</span> <span class="mi">1</span><span class="n">e</span><span class="o">-</span><span class="mi">4</span><span class="p">,</span> <span class="s1">&#39;ENCUT&#39;</span><span class="p">:</span> <span class="mi">450</span><span class="p">,</span> <span class="s1">&#39;ICHARG&#39;</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="s1">&#39;ISMEAR&#39;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
                      <span class="s1">&#39;SIGMA&#39;</span><span class="p">:</span> <span class="mf">0.1</span><span class="p">,</span> <span class="s1">&#39;IBRION&#39;</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="s1">&#39;ISIF&#39;</span><span class="p">:</span> <span class="mi">3</span><span class="p">,</span> <span class="s1">&#39;ISPIN&#39;</span><span class="p">:</span> <span class="mi">2</span><span class="p">}</span>

    <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isdir</span><span class="p">(</span><span class="s1">&#39;hse_bands&#39;</span><span class="p">):</span>
        <span class="n">os</span><span class="o">.</span><span class="n">mkdir</span><span class="p">(</span><span class="s1">&#39;hse_bands&#39;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">force_overwrite</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">is_converged</span><span class="p">(</span><span class="s1">&#39;hse_bands&#39;</span><span class="p">):</span>
        <span class="n">os</span><span class="o">.</span><span class="n">chdir</span><span class="p">(</span><span class="s1">&#39;hse_bands&#39;</span><span class="p">)</span>
        <span class="n">os</span><span class="o">.</span><span class="n">system</span><span class="p">(</span><span class="s1">&#39;cp ../CONTCAR ./POSCAR&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="s1">&#39;../POTCAR&#39;</span><span class="p">):</span>
            <span class="n">os</span><span class="o">.</span><span class="n">system</span><span class="p">(</span><span class="s1">&#39;cp ../POTCAR .&#39;</span><span class="p">)</span>
        <span class="n">HSE_INCAR_DICT</span><span class="o">.</span><span class="n">update</span><span class="p">({</span><span class="s1">&#39;MAGMOM&#39;</span><span class="p">:</span> <span class="n">get_magmom_string</span><span class="p">()})</span>
        <span class="n">Incar</span><span class="o">.</span><span class="n">from_dict</span><span class="p">(</span><span class="n">HSE_INCAR_DICT</span><span class="p">)</span><span class="o">.</span><span class="n">write_file</span><span class="p">(</span><span class="s1">&#39;INCAR&#39;</span><span class="p">)</span>

        <span class="c1"># Re-use the irreducible brillouin zone KPOINTS from a</span>
        <span class="c1"># previous standard DFT run.</span>
        <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isdir</span><span class="p">(</span><span class="s1">&#39;../hse_prep&#39;</span><span class="p">):</span>
            <span class="n">ibz_lines</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;../hse_prep/IBZKPT&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">readlines</span><span class="p">()</span>
            <span class="k">if</span> <span class="n">destroy_prep_directory</span><span class="p">:</span>
                <span class="n">os</span><span class="o">.</span><span class="n">system</span><span class="p">(</span><span class="s1">&#39;rm -r ../hse_prep&#39;</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">ibz_lines</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;../IBZKPT&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">readlines</span><span class="p">()</span>

        <span class="n">n_ibz_kpts</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">ibz_lines</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">0</span><span class="p">])</span>
        <span class="n">kpath</span> <span class="o">=</span> <span class="n">HighSymmKpath</span><span class="p">(</span><span class="n">Structure</span><span class="o">.</span><span class="n">from_file</span><span class="p">(</span><span class="s1">&#39;POSCAR&#39;</span><span class="p">))</span>
        <span class="n">Kpoints</span><span class="o">.</span><span class="n">automatic_linemode</span><span class="p">(</span><span class="mi">20</span><span class="p">,</span> <span class="n">kpath</span><span class="p">)</span><span class="o">.</span><span class="n">write_file</span><span class="p">(</span><span class="s1">&#39;KPOINTS&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">dim</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
            <span class="n">remove_z_kpoints</span><span class="p">()</span>
        <span class="n">linemode_lines</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;KPOINTS&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">readlines</span><span class="p">()</span>

        <span class="n">abs_path</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">i</span> <span class="o">=</span> <span class="mi">4</span>
        <span class="k">while</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">linemode_lines</span><span class="p">):</span>
            <span class="n">start_kpt</span> <span class="o">=</span> <span class="n">linemode_lines</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
            <span class="n">end_kpt</span> <span class="o">=</span> <span class="n">linemode_lines</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
            <span class="n">increments</span> <span class="o">=</span> <span class="p">[</span>
                <span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">end_kpt</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">-</span> <span class="nb">float</span><span class="p">(</span><span class="n">start_kpt</span><span class="p">[</span><span class="mi">0</span><span class="p">]))</span> <span class="o">/</span> <span class="mi">20</span><span class="p">,</span>
                <span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">end_kpt</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span> <span class="o">-</span> <span class="nb">float</span><span class="p">(</span><span class="n">start_kpt</span><span class="p">[</span><span class="mi">1</span><span class="p">]))</span> <span class="o">/</span> <span class="mi">20</span><span class="p">,</span>
                <span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">end_kpt</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span> <span class="o">-</span> <span class="nb">float</span><span class="p">(</span><span class="n">start_kpt</span><span class="p">[</span><span class="mi">2</span><span class="p">]))</span> <span class="o">/</span> <span class="mi">20</span>
            <span class="p">]</span>

            <span class="n">abs_path</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">start_kpt</span><span class="p">[:</span><span class="mi">3</span><span class="p">]</span> <span class="o">+</span> <span class="p">[</span><span class="s1">&#39;0&#39;</span><span class="p">,</span> <span class="n">start_kpt</span><span class="p">[</span><span class="mi">4</span><span class="p">]])</span>
            <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">20</span><span class="p">):</span>
                <span class="n">abs_path</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                    <span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">start_kpt</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">+</span> <span class="n">increments</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="n">n</span><span class="p">),</span>
                     <span class="nb">str</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">start_kpt</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span> <span class="o">+</span> <span class="n">increments</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">*</span> <span class="n">n</span><span class="p">),</span>
                     <span class="nb">str</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">start_kpt</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span> <span class="o">+</span> <span class="n">increments</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">*</span> <span class="n">n</span><span class="p">),</span> <span class="s1">&#39;0&#39;</span><span class="p">]</span>
                    <span class="p">)</span>
            <span class="n">abs_path</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">end_kpt</span><span class="p">[:</span><span class="mi">3</span><span class="p">]</span> <span class="o">+</span> <span class="p">[</span><span class="s1">&#39;0&#39;</span><span class="p">,</span> <span class="n">end_kpt</span><span class="p">[</span><span class="mi">4</span><span class="p">]])</span>
            <span class="n">i</span> <span class="o">+=</span> <span class="mi">3</span>

        <span class="n">n_linemode_kpts</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">abs_path</span><span class="p">)</span>

        <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;KPOINTS&#39;</span><span class="p">,</span> <span class="s1">&#39;w&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">kpts</span><span class="p">:</span>
            <span class="n">kpts</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;Automatically generated mesh</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
            <span class="n">kpts</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">{}</span><span class="se">\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">n_ibz_kpts</span> <span class="o">+</span> <span class="n">n_linemode_kpts</span><span class="p">))</span>
            <span class="n">kpts</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;Reciprocal Lattice</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">ibz_lines</span><span class="p">[</span><span class="mi">3</span><span class="p">:]:</span>
                <span class="n">kpts</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">point</span> <span class="ow">in</span> <span class="n">abs_path</span><span class="p">:</span>
                <span class="n">kpts</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">{}</span><span class="se">\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s1">&#39; &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">point</span><span class="p">)))</span>

        <span class="k">if</span> <span class="n">QUEUE</span> <span class="o">==</span> <span class="s1">&#39;pbs&#39;</span><span class="p">:</span>
            <span class="n">write_pbs_runjob</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">{}</span><span class="s1">_hsebands&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                <span class="n">os</span><span class="o">.</span><span class="n">getcwd</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">)[</span><span class="o">-</span><span class="mi">2</span><span class="p">]),</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">64</span><span class="p">,</span> <span class="s1">&#39;1800mb&#39;</span><span class="p">,</span> <span class="s1">&#39;50:00:00&#39;</span><span class="p">,</span> <span class="n">VASP</span><span class="p">)</span>
            <span class="n">submission_command</span> <span class="o">=</span> <span class="s1">&#39;qsub runjob&#39;</span>

        <span class="k">elif</span> <span class="n">QUEUE</span> <span class="o">==</span> <span class="s1">&#39;slurm&#39;</span><span class="p">:</span>
            <span class="n">write_slurm_runjob</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">{}</span><span class="s1">_hsebands&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                <span class="n">os</span><span class="o">.</span><span class="n">getcwd</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">)[</span><span class="o">-</span><span class="mi">2</span><span class="p">]),</span> <span class="mi">64</span><span class="p">,</span> <span class="s1">&#39;1800mb&#39;</span><span class="p">,</span> <span class="s1">&#39;50:00:00&#39;</span><span class="p">,</span> <span class="n">VASP</span><span class="p">)</span>
            <span class="n">submission_command</span> <span class="o">=</span> <span class="s1">&#39;sbatch runjob&#39;</span>

        <span class="k">if</span> <span class="n">submit</span><span class="p">:</span>
            <span class="n">os</span><span class="o">.</span><span class="n">system</span><span class="p">(</span><span class="n">submission_command</span><span class="p">)</span>

        <span class="n">os</span><span class="o">.</span><span class="n">chdir</span><span class="p">(</span><span class="s1">&#39;../&#39;</span><span class="p">)</span></div>
</pre></div>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper"><div class="relations">
<h3>Related Topics</h3>
<ul>
  <li><a href="../../../index.html">Documentation overview</a><ul>
  <li><a href="../../index.html">Module code</a><ul>
  </ul></li>
  </ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="../../../search.html" method="get">
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Go" /></div>
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="footer">
      &copy;2016, Michael Ashton.
      
      |
      Powered by <a href="http://sphinx-doc.org/">Sphinx 1.5</a>
      &amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.9</a>
      
    </div>

    

    
  </body>
</html>