1#!/usr/local/bin/python3.8
2r"""
3Band structure Flow
4===================
5
6Flow to compute the band structure of silicon.
7"""
8
9import sys
10import os
11import abipy.data as abidata
12import abipy.abilab as abilab
13import abipy.flowtk as flowtk
14
15
16def make_scf_nscf_inputs(paral_kgb=0, usepaw=0):
17    """Returns two input files: GS run and NSCF on a high symmetry k-mesh."""
18    pseudos = abidata.pseudos("14si.pspnc") if usepaw == 0 else abidata.pseudos("Si.GGA_PBE-JTH-paw.xml")
19
20    # Get structure from cif file.
21    multi = abilab.MultiDataset(structure=abidata.cif_file("si.cif"), pseudos=pseudos, ndtset=2)
22
23    # Global variables
24    ecut = 6
25    multi.set_vars(
26        ecut=ecut,
27        nband=8,
28        paral_kgb=paral_kgb,
29        iomode=3,
30        timopt=-1,
31    )
32
33    if multi.ispaw:
34        multi.set_vars(pawecutdg=2 * ecut)
35
36    # Dataset 1 (GS run)
37    multi[0].set_kmesh(ngkpt=[8, 8, 8], shiftk=[0, 0, 0])
38    multi[0].set_vars(tolvrs=1e-6)
39
40    # Dataset 2 (NSCF run)
41    kptbounds = [
42        [0.5, 0.0, 0.0],  # L point
43        [0.0, 0.0, 0.0],  # Gamma point
44        [0.0, 0.5, 0.5],  # X point
45    ]
46
47    multi[1].set_kpath(ndivsm=6, kptbounds=kptbounds)
48    multi[1].set_vars(tolwfr=1e-12)
49
50    # Generate two input files for the GS and the NSCF run
51    scf_input, nscf_input = multi.split_datasets()
52    return scf_input, nscf_input
53
54
55def build_flow(options):
56    # Set working directory (default is the name of the script with '.py' removed and "run_" replaced by "flow_")
57    if not options.workdir:
58        options.workdir = os.path.basename(sys.argv[0]).replace(".py", "").replace("run_", "flow_")
59
60    # Get the SCF and the NSCF input.
61    scf_input, nscf_input = make_scf_nscf_inputs()
62
63    # Build the flow.
64    return flowtk.bandstructure_flow(options.workdir, scf_input, nscf_input, manager=options.manager)
65
66
67# This block generates the thumbnails in the AbiPy gallery.
68# You can safely REMOVE this part if you are using this script for production runs.
69if os.getenv("READTHEDOCS", False):
70    __name__ = None
71    import tempfile
72    options = flowtk.build_flow_main_parser().parse_args(["-w", tempfile.mkdtemp()])
73    build_flow(options).graphviz_imshow()
74
75
76@flowtk.flow_main
77def main(options):
78    """
79    This is our main function that will be invoked by the script.
80    flow_main is a decorator implementing the command line interface.
81    Command line args are stored in `options`.
82    """
83    return build_flow(options)
84
85
86if __name__ == "__main__":
87    sys.exit(main())
88
89############################################################################
90#
91# Run the script with:
92#
93#     run_si_ebands.py -s
94#
95# then use:
96#
97#    abirun.py flow_si_ebands ebands --plot
98#
99# to analyze (and plot) the electronic bands produced by the Flow.
100#
101# .. code-block:: bash
102#
103#    KS electronic bands:
104#           nsppol  nspinor  nspden  nkpt  nband  nelect  fermie formula  natom  \
105#    w0_t0       1        1       1    29      8     8.0   5.598     Si2      2
106#    w0_t1       1        1       1    14      8     8.0   5.598     Si2      2
107#
108#           angle0  angle1  angle2      a      b      c  volume abispg_num scheme  \
109#    w0_t0    60.0    60.0    60.0  3.867  3.867  3.867  40.888        227   none
110#    w0_t1    60.0    60.0    60.0  3.867  3.867  3.867  40.888        227   none
111#
112#           occopt  tsmear_ev  bandwidth_spin0  fundgap_spin0  dirgap_spin0  \
113#    w0_t0       1      0.272           11.856          0.562         2.532
114#    w0_t1       1      0.272           11.856          0.524         2.532
115#
116#          task_class                                   ncfile              status
117#    w0_t0    ScfTask  flow_si_ebands/w0/t0/outdata/out_GSR.nc  Completed
118#    w0_t1   NscfTask  flow_si_ebands/w0/t1/outdata/out_GSR.nc  Completed
119#
120# .. image:: https://github.com/abinit/abipy_assets/blob/master/run_si_ebands.png?raw=true
121#    :alt: Band structure of Si in the IBZ and along a k-path
122#
123