0% found this document useful (0 votes)
15 views67 pages

Productivity Tools for QuantumESPRESSO

Uploaded by

Rahul R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views67 pages

Productivity Tools for QuantumESPRESSO

Uploaded by

Rahul R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Chapter 6

Productivity Tools

In practical use of first‐principles calculation software, we will


need to manage and visualize our data in an attractive manner.
Furthermore, when we research some unknown materials, we have
to know the starting point for generating the Quantum ESPRESSO
input files. In most cases, we should repeat similar procedures
to check the consistency of the first‐principles calculations. Such
situations require us to supplement our skills of using Quantum
ESPRESSO with other utilities. In particular, understanding the way
of plotting figures and using a few Linux commands along with
basic shell‐scripting will be useful for us to be more productive
when performing research. In this chapter, we will learn these extra
tools, from the preparation of our own Quantum ESPRESSO files,
some useful Linux commands/scripts, plotting with Matplotlib in the
Python environment, to additional packages online.

6.1 Quantum ESPRESSO input generators

Recent advances in artificial intelligence and machine learning have


allowed us to obtain pre‐defined input files for Quantum ESPRESSO
calculations from input generator tools available online. Two out‐

Quantum ESPRESSO Course for Solid‐State Physics


Nguyen Tuan Hung, Ahmad R. T. Nugraha, and Riichiro Saito
Copyright © 2023 Jenny Stanford Publishing Pte. Ltd.
ISBN 978‐981‐4968‐37‐9 (Hardcover), 978‐981‐4968‐63‐8 (Paperback), 978‐1‐003‐29096‐4 (eBook)
[Link]
278 | Productivity Tools

standing examples are AFLOW ([Link]) and MaterialsCloud


([Link]). We can generate Quantum ESPRESSO input
files based on the information provided in those websites for a
particular material. The two websites also provide us with the
structure visualization tools that can be used to confirm the structure
of the material under study and to select the possible k‐path for the
band structure plot, which is helpful to accelerate our work.
As an example of this section, we will learn how to make
the Quantum ESPRESSO input files for bulk germanium telluride
(GeTe) so that we can obtain its density of states (DOS) and energy
dispersion. The input files will be named as follows:

1. [Link] for the SCF calculation.


2. [Link] for the NSCF DOS calculation.
3. [Link] for post‐processing the DOS data.
4. [Link] for the energy dispersion calculation.
5. [Link] for post‐processing the dispersion data.

Let us see how we can make all the input files.

6.1.1 Obtaining a structural CIF file from AFLOW

The first step to creating the Quantum ESPRESSO input files is


obtaining a structure definition for the material that we are studying.
We will use AFLOW to get the structure information that will define
the material in all Quantum ESPRESSO input files. We can click the
address of the AFLOW website ([Link]) in the web browser,
and we will see its front page as shown in Fig. 6.1. There are several
menus available in the top panel of the website. What we need is the
Search menu, highlighted by the box on the top right of Fig. 6.1.
After clicking the Search menu, we can see the search page as
shown in Fig. 6.2. We highlight four steps in the case of specifying
GeTe for obtaining its structural information:

1. Click atom Ge.


2. Click atom Te.
3. Make sure “Ge, Te” appear in the search box.
Quantum ESPRESSO Course for Solid‐State Physics | 279

Figure 6.1 The front page of the AFLOW website.

Figure 6.2 AFLOW search page with some highlighted steps for obtaining the
GeTe information. (1) Push Ge, (2) push Te and then we get Ge, Te in (3) and
we select “2” for the number of species.

4. Set the number of species to “2” because we want to focus on


finding compounds that strictly consist of Ge and Te atoms only,
without other atoms.

It is also possible to skip Steps 1–2 and jump directly to Step 3 by


manually writing “Ge, Te” in the search box.
After specifying the Ge and Te atoms on the AFLOW search page,
as shown in Fig. 6.2, we can click the Search button beside the
search box to let AFLOW find all relevant information about GeTe
compounds. A screenshot of the search results is shown in Fig. 6.3. At
the time of writing this book, there are 29 entries found related to our
280 | Productivity Tools

Figure 6.3 Sample search results for GeTe. We click the first entry to get the
information of bulk GeTe in its cubic phase with the Fm3̄m (#225) space
group.

specified “Ge, Te” keyword. The different entries mean that various
structures and parameters for the GeTe calculations are reported to
be explored.
For example, in the “space group” column, we can find Fm3̄m
(#225) and R3m (#160) groups that correspond to the cubic and
rhombohedral lattice systems, respectively, as shown in the first and
the second rows of Fig. 6.3. The same space groups may appear
several times, indicating the other details of the parameters used in
the structures. Here we take the first row of the search results as
our example and click the corresponding GeTe entry as highlighted
in Fig. 6.3. This GeTe entry gives information about the bulk GeTe in
its cubic phase.
By opening the page after clicking the first entry of GeTe, we
will see several structural details and physical properties of the
bulk cubic GeTe. The first information available on that page is the
structure visualization. The screenshot is given in Fig. 6.4, from
which we can obtain the crystallographic information file (CIF) of the
material. It is recommended to set the relaxed structure as calculated
in AFLOW by clicking the corresponding button indicated by number
1 in Fig. 6.4. Then, we can save it in the CIF format by clicking the
button number 2 in Fig. 6.4. We may save it as any filename we like.
In this example, we use the default one given by AFLOW:
Quantum ESPRESSO Course for Solid‐State Physics | 281

Figure 6.4 Structure visualization of bulk cubic GeTe in AFLOW. Button 1


should be clicked to set the relaxed structure as calculated originally in
AFLOW. Button 2 is to save as the CIF format.

Ge1Te1_ICSD_638014.cif

Note that AFLOW, in this case, already gives us the relaxed geometry
of GeTe. If we are not satisfied with the relaxed structure, we may
repeat the structural optimization process in Quantum ESPRESSO by
following the related hands‐on in Sec. 3.1.5. However, for simplicity,
in this chapter, we will not do such a process again. Instead, we rely
on the relaxed structure given by AFLOW.

6.1.2 Generating SCF input file from MaterialsCloud

On the same AFLOW page of Fig. 6.4 below the structure visual‐
ization, we can explore physical properties such as thermodynamic,
magnetic, and electronic properties calculated by AFLOW. When
we further scroll down the page, there are also AFLOW calculation
282 | Productivity Tools

Figure 6.5 The default view of Quantum ESPRESSO input generator and
structure visualizer in MaterialsCloud ([Link]).

details such as cutoff energy, k‐point grid, and pseudopotential


version used for the material under investigation, but all the results
are given there are not calculated by using Quantum ESPRESSO.
Therefore, we turn to the other website, i.e., MaterialsCloud, for
obtaining the Quantum ESPRESSO inputs. The relevant tool for
generating the SCF input file in MaterialsCloud can be accessed
at the following address: [Link]
tools/qeinputgenerator. The web address written above may
change in the future depending on the MaterialsCloud developers.
However, we should be able to access it from the main page of
MaterialsCloud through the following order of menus: Work →
Tools → Quantum ESPRESSO input generator and structure
visualizer. The default view of this tool is shown in Fig. 6.5
What we need now is only a structure file, which has been
obtained for the GeTe example as explained in Sec. 6.1.1 in the CIF
format. Provided the CIF file, we can do the following steps:

1. Click the Browse button in the Quantum ESPRESSO input


generator on the MaterialsCloud website as shown in Fig. 6.5
and find the CIF file that we want to calculate.
2. Select the CIF file format in MaterialsCloud (either by ASE or
pymatgen parser is fine; do not use the qetools parser).
Quantum ESPRESSO Course for Solid‐State Physics | 283
3. Choose the PBE or PBEsol SSSP pseudopotential libraries.
For each library, there are two versions: (i) a more accurate
one, called “Precision” and (ii) a general‐purpose one, called
“Efficiency”. Later, we may change the pseudopotential files
manually if we do not want to use those recommended by
MaterialsCloud.
4. Decide if we want to treat our system as (i) a non‐magnetic
metal (i.e., using smearing/fractional occupations), (ii) as a
non‐magnetic insulator (i.e., using fixed occupations equal to
2), or (iii) as a magnetic system (in this case, it is safer to use
smearing/fractional occupations). When we are not sure about
the metallicity and magnetism, just leave them and choose the
default.
5. Decide the density of the k‐points sampling. The default value
is 0.2 Å−1 , which is a conservative, all‐purpose choice. We call
this “fine” sampling, and the appropriate smearing (“degauss”)
value is 0.2 eV. The two other options are “very fine” sampling
(0.15 Å−1 , 0.1 eV), and “normal” (0.3 Å−1 , 0.3 eV). Note that
we change the “degauss” parameter since a coarser sampling
benefits from more smearing. See Sec. 3.1.1 for the other details
about the smearing parameter.
6. Finally, we can choose whether or not we want to refine the cell.
Normally we do not need to do it, so we can directly click the
“Generate” button.

In the above steps, beware that there is no point in sampling


the Brillouin Zone in a direction where there should not be any
dispersion, i.e., in the case of a 0D, 1D, or 2D system in which 3, 2,
or 1 direction(s) should not be sampled, we should update the input
file manually. We may also want to use a shifted Monkhorst‐Pack
mesh instead of the unshifted one by changing 0 0 0 to 1 1 1 in the
last three numbers of the k‐point entry from the input file generated
by Materials Cloud (c.f. Sec. 3.1.3). All these manual modifications
can be done after we obtain the PWscf input file generated by the
MaterialsCloud tool.
For the GeTe example, we show our selected options as
highlighted in Fig. 6.6. We mostly select the default options, except
the file format, which is CIF, and the magnetism, which we select as
284 | Productivity Tools

Figure 6.6 Options selected to generate the SCF input file for the bulk cubic
GeTe in MaterialsCloud.

Figure 6.7 Downloading the SCF input file generated by MaterialsCloud.

“non‐magnetic insulator” because we already expect GeTe to have


a band gap. After we click the “Generate” button, we will see the
resulting SCF input file given by MaterialsCloud. Note that it may take
a few seconds until we can see the input file fully. It is recommended
to download the SCF input file along with the pseudopotentials that
are zipped in a file called [Link]. We can click the download
button as shown in Fig. 6.7. It is recommended to save the zip file
under the QE-SSP folder following all the examples in this book.
The zipped file can be unzipped in the Linux terminal by the
following command:

$ unzip [Link]

After unzipping the file, we will see that there is a folder named
PWscf containing a [Link] file and a pseudo directory with the
suggested pseudopotential files from MaterialsCloud. To match with
the material’s name (GeTe), it is better if we rename the folder and
the SCF input file by the following set of command lines:

$ mv PWscf gete
$ cd gete
$ mv [Link] [Link]
Quantum ESPRESSO Course for Solid‐State Physics | 285
The first line means that we change the name of the PWscf folder
to gete. The second line is to enter the gete folder (which was
previously named PWscf). In this folder, by using the third command
line, we rename one more file, i.e., [Link] to [Link]. Now
we can see the contents of [Link] by either opening it with a
text editor or by typing the cat command in the terminal:

$ cat [Link]

QE‑SSP/gete/[Link]

1 & CONTROL
2 calculation = 'scf '
3 outdir = './out/'
4 prefix = 'gete ' # originally 'aiida '
5 pseudo_dir = './ pseudo/'
6 verbosity = 'high '
7 /
8 &SYSTEM
9 ecutrho = 3.2000000000d+02
10 ecutwfc = 4.0000000000d+01
11 ibrav = 0
12 nat = 2
13 ntyp = 2
14 /
15 & ELECTRONS
16 conv_thr = 4.0000000000d-10
17 mixing_beta = 4.0000000000d-01
18 /
19 ATOMIC_SPECIES
20 Ge 72.64 ge_pbesol_v1 .4. [Link]
21 Te 127.6 te_pbesol_v1.[Link]
22 ATOMIC_POSITIONS crystal
23 Ge 0.0000000000 0.0000000000 0.0000000000
24 Te 0.5000000000 0.5000000000 0.5000000000
25 CELL_PARAMETERS angstrom
26 4.2603675143 0.0000000000 0.0000000000
27 2.1301837572 3.6895864968 0.0000000000
28 2.1301837572 1.2298621656 3.4785755089
29 K_POINTS automatic
30 13 13 13 1 1 1 # originally 10 10 10 0 0 0

Note in [Link] above, we need to change the prefix


variable, which originally read ‘aiida’ in the MaterialsCloud
input generator, to gete in our computer (at line 4). We also
286 | Productivity Tools

use slightly denser k‐points and shifted Monkhorst‐Pack mesh


(13 13 13 1 1 1) and remove some variables, such as
etot_conv_thr, force_conv_thr, tprnfor, tstress, nosym,
electron_maxstep, and occupations, which are too detailed to be
specified in the SCF file. In the next section, for the DOS calculation,
we will need to set the occupations variable back in the NSCF
(non‐SCF) input file.

6.1.3 Preparing DOS and band structure inputs

Next, let us prepare the input files for DOS and band structure
calculations from the information already available in the previous
subsection combined with the new information we will gather here.
In the following example, we use [Link] as the starting
point for the DOS and band structure calculations, which are both
categorized as the NSCF calculations. We can copy the SCF input file
to make new input files for the NSCF calculations of DOS and band
structure:

$ cp [Link] [Link]
$ cp [Link] [Link]

Then, we should edit [Link] and [Link]


according to our needs.

DOS input files


As explained in Sec. 3.2.3, the main differences between the SCF
and NSCF inputs for the DOS calculation are the appearance of
occupations parameter (= 'tetrahedra_opt') and setting much
denser k‐points mesh in the NSCF input file. In this GeTe example, we
set the number of k‐points twice larger than that in the SCF input file
(line 28). It is also recommended to make the nbnd parameter same
as in the NSCF input for the band structure calculation. The input file
for the DOS calculation is shown as below:

QE‑SSP/gete/[Link]

1 & CONTROL
2 calculation = 'nscf '
Quantum ESPRESSO Course for Solid‐State Physics | 287
3 outdir = './out/'
4 prefix = 'gete '
5 pseudo_dir = './ pseudo/'
6 verbosity = 'high '
7 /
8 &SYSTEM
9 ecutrho = 3.2000000000d+02
10 ecutwfc = 4.0000000000d+01
11 ibrav = 0
12 nat = 2
13 ntyp = 2
14 nbnd = 30 # same as in *. [Link]
15 occupations = 'tetrahedra_opt ' # only for DOS
16 /
17 & ELECTRONS
18 conv_thr = 4.0000000000d-10
19 mixing_beta = 4.0000000000d-01
20 /
21 ATOMIC_SPECIES
22 Ge 72.64 ge_pbesol_v1 .4. [Link]
23 Te 127.6 te_pbesol_v1.[Link]
24 ATOMIC_POSITIONS crystal
25 Ge 0.0000000000 0.0000000000 0.0000000000
26 Te 0.5000000000 0.5000000000 0.5000000000
27 K_POINTS automatic
28 26 26 26 1 1 1 # twice the SCF 's case
29 CELL_PARAMETERS angstrom
30 4.2603675143 0.0000000000 0.0000000000
31 2.1301837572 3.6895864968 0.0000000000
32 2.1301837572 1.2298621656 3.4785755089

Besides [Link], we should create a post‐processing


input file to gather the DOS data. In this case, we do not have to copy
the SCF input file, just make a new file with a few lines of information
for the DOS post‐process using the &DOS namelist. The specifications
of outdir (line 2) and prefix (line 3) should be consistent with
the *.[Link] file. We also add parameters emin and emax in eV
(lines 5 and 6) in the following DOS post‐processing file:

QE‑SSP/gete/[Link]

1 &DOS
2 outdir = './out/'
3 prefix = 'gete '
4 fildos = '[Link] '
5 emin = -5
288 | Productivity Tools

Figure 6.8 Obtaining the k‐path and high‐symmetry points of bulk cubic GeTe
in MaterialsCloud.

6 emax = 25
7 /

Band structure input file


The most important point when creating the band input file is
the k‐path for plotting the energy dispersion. MaterialsCloud has
an additional tool called “SeeK‐path” that can accept a CIF format,
which is used for finding and visualizing a possible k‐path, as
shown in Fig. 6.8. The SeeK‐path tool will show the coordinates
of the high‐symmetry points that we can copy to the band input
file. The SeeK‐path tool can be accessed at the following address:
[Link] or through
the following order of MaterialsCloud menus: Work → Tools →
SeeK-path: the k-path finder and visualizer.
In Fig. 6.8, we show how to obtain the k‐path and high‐symmetry
points of bulk cubic GeTe in MaterialsCloud. We only need to
browse/upload the CIF file of GeTe obtained earlier (from Sec. 6.1.1)
and specify, for example, the ase parser for the CIF format. After
pressing the button to “Calculate my structure” in the SeeK‐path tool,
we will be able to see the high‐symmetry points of bulk cubic GeTe,
as shown in Fig. 6.9.
Now suppose that we want to calculate and plot the energy dis‐
persion along the path of W–L–U–X–Γ–K high‐symmetry points. We
just need to change the automatic K_points setup into crystal_b
in the [Link] file and list all the corresponding high‐
Quantum ESPRESSO Course for Solid‐State Physics | 289

Figure 6.9 The high‐symmetry points of bulk cubic GeTe in the scaled units
or crystal_b coordinates given by the SeeK‐path tool in MaterialsCloud.

symmetry points along which we want to obtain the dispersion data


(lines 31–37). In this GeTe example, we list the 6 high‐symmetry
points for the specified path using the coordinate information from
the SeeK‐path tool. Do not forget that in [Link] we
should set the nbnd parameter (line 14), which was not written in the
[Link] file. It is recommended to use the same value of nbnd
in both [Link] and [Link] files.

QE‑SSP/gete/[Link]

1 & CONTROL
2 calculation = 'bands '
3 outdir = './out/'
4 prefix = 'gete '
5 pseudo_dir = './ pseudo/'
6 verbosity = 'high '
7 /
8 &SYSTEM
9 ecutrho = 3.2000000000d+02
10 ecutwfc = 4.0000000000d+01
11 ibrav = 0
12 nat = 2
13 ntyp = 2
14 nbnd = 30 # same as in *. [Link]
15 /
16 & ELECTRONS
17 conv_thr = 4.0000000000d-10
18 mixing_beta = 4.0000000000d-01
19 /
290 | Productivity Tools

20 ATOMIC_SPECIES
21 Ge 72.64 ge_pbesol_v1 .4. [Link]
22 Te 127.6 te_pbesol_v1.[Link]
23 ATOMIC_POSITIONS crystal
24 Ge 0.0000000000 0.0000000000 0.0000000000
25 Te 0.5000000000 0.5000000000 0.5000000000
26 CELL_PARAMETERS angstrom
27 4.2603675143 0.0000000000 0.0000000000
28 2.1301837572 3.6895864968 0.0000000000
29 2.1301837572 1.2298621656 3.4785755089
30 K_POINTS crystal_b # was automatic in SCF file
31 6
32 0.5000000000 0.2500000000 0.7500000000 50 !W
33 0.5000000000 0.5000000000 0.5000000000 30 !L
34 0.6250000000 0.2500000000 0.6250000000 20 !U
35 0.5000000000 0.0000000000 0.5000000000 50 !X
36 0.0000000000 0.0000000000 0.0000000000 50 !G
37 0.3750000000 0.3750000000 0.7500000000 1 !K

Similar to the DOS part, we create a post‐processing input file to


gather the energy dispersion data. We can make a new file with a few
lines of information for post‐processing the band structure using the
&BANDS namelist. The specifications of outdir and prefix should be
consistent with the *.[Link] file.

QE‑SSP/gete/[Link]

1 &BANDS
2 outdir = './out/'
3 prefix = 'gete '
4 filband = '[Link] '
5 /

Having created the above file, we have completed all the steps in
the preparation of Quantum ESPRESSO input files for the DOS and
band structure calculations.

6.1.4 Wannier90 input generator from CIF file

In addition to the SCF, NSCF, and DOS or band processing input files,
we may want to generate the Wannier90 input file directly from the
provided CIF file. For this purpose, there is a useful tool in the forms
Quantum ESPRESSO Course for Solid‐State Physics | 291
of Python script named as cif2qewan, which can be downloaded by
the following command:

$ git clone https :// [Link]/wannier -utils -dev/


[Link]

The above command will create a cif2qewan directory in the current


working directory. We should also execute the following command if
we do not have some packages needed by cif2qewan tool:

$ pip install docopt toml cif2cell

Now, move to the cif2qewan directory and copy the


Ge1Te1_ICSD_638014.cif file to this directory. Suppose that
the CIF file was located in ~/QE-SSP/gete directory.

$ cd cif2qewan
$ cp ~/QE -SSP/ Ge1Te1_ICSD_638014 .cif .
$ python [Link] Ge1Te1_ICSD_638014 .cif
[Link]

With the above set of command lines, we can obtain the Quan‐
tum ESPRESSO and Wannier90 input files automatically ([Link],
[Link], [Link], and [Link]). In order to understand
and run these files, please check Sec. 3.6.1. Make sure that the
pseudo_dir in \[Link] file has been edited properly to
be the location of “PS Library” pseudopotentials as explained in
Sec. 3.1.6, and the name of the pseudopotential of each atom in
pp_psl_rrkj.csv file is correct.

6.2 Linux commands

The most basic way that we have explained in Chapters 2 and 3 for
running the Quantum ESPRESSO calculations is using the following
Linux commands typed in the terminal:
292 | Productivity Tools

$ QEcommand < [Link] > [Link]

where QEcommand is one of any Quantum ESPRESSO binary (appli‐


cation) file such as pw.x, bands.x, dos.x, etc. For obtaining the DOS
and band structure of bulk cubic GeTe, we might want to execute the
following commands one by one in the terminal:

$ pw.x < [Link] > [Link]


$ pw.x < [Link] > [Link]
$ dos.x < [Link] > [Link]
$ pw.x < [Link] > [Link]
$ bands.x < [Link] > [Link]

However, as we become more experienced with Quantum ESPRESSO


and when we want to perform many different calculations (usually at
the same time), we will realize that typing commands one by one is
not efficient. Furthermore, if we have parallel computing resources,
we will need to type an additional command, such as mpirun -np 4,
before the QEcommand for specifying the parallel calculations. This
situation will force us to type longer command lines repeatedly.
Imagine also when the calculation takes a lot of hours (or even
days and weeks), we surely do not want to wait in front of our
computer without doing other productive works just to see that the
calculation is still running. Therefore, it is better for us to learn some
Linux commands and so‐called scripts that will run even after we log
out. We also need to manage Quantum ESPRESSO calculations as well
as for the administration of files and folders in our working directory.
We expect that the workflow or calculation routine developed
by using sufficient (“not so much”) knowledge of Linux commands
and scripts should be more efficient than repeating long commands.
We may notice in Chapter 3 there is a script file like [Link], which
consists of several Linux commands in a single file. This file is also
known as a “batch” file because it is useful for running many jobs
one‐by‐one by a single execution of the file. In this section, we review
some commonly‐used Linux commands. If the readers are familiar
with Linux commands, the readers can go to in Sec. 6.3, in which we
explain how to make shell scripts.
Quantum ESPRESSO Course for Solid‐State Physics | 293

bin/ usr/ var/ etc/ lib/ home/ ...

... ... ... user1/ user2/ quantum/ ...

Figure 6.10 Linux directory structure.

6.2.1 File‐ and directory‐related commands

In Linux (also in macOS or other Unix‐like systems), every file


and directory is put under the root directory, which is the first or top‐
most directory in the directory tree. The root directory is referred
to by a single leading slash (/). Then, as shown in Fig. 6.10, under
the root directory, there can be several default directories, such as
bin/, usr/, var/, etc/, lib/, and home/, depending on the Linux
distribution we install in our computer.
When navigating the file and directory structure, we can use
either the absolute path or the relative path to the resource. For
example, a user in Linux with a specific username usually has
their main working directory as /home/username/ in its absolute
path that starts by “/”. If our username is quantum, the home
directory will be /home/quantum/ (unless we specified another
directory during the Linux installation). Suppose we are already
inside /home/quantum/ and we have a folder named QE-SSP, we can
refer to that folder as ./QE-SSP or simply QE-SSP in its relative path
(while the absolute path is /home/quantum/QE-SSP).
Now let us learn some frequently‐used commands that will be
useful for the file and directory management as shown in Table 6.1.

pwd: Show present working directory


The present working directory is the directory in which the user is
currently working. Each time we interact with the command prompt,
we are working within a directory. We can use the pwd command to
find out what directory we are currently working in:
294 | Productivity Tools

Table 6.1 File‐ and directory‐related commands in Linux, where file1 or dir1
are the name of file or directory, respectively.

Command Purpose
pwd show present directory
....................................................................................................................................................................

cd dirname change directory to dirname


....................................................................................................................................................................

cd change to home directory


....................................................................................................................................................................

ls list the current directory


....................................................................................................................................................................

ls -al list the current directory with hidden files


....................................................................................................................................................................

touch filename create or update filename


....................................................................................................................................................................

ln -s filename create a symbolic link called linkname to


linkname filename; it can also works for directory
....................................................................................................................................................................

mkdir dirname create a directory called dirname


....................................................................................................................................................................

rm filename delete a file called filename


....................................................................................................................................................................

rm -r dirname delete dirname


....................................................................................................................................................................

rm -f filename force remove filename


....................................................................................................................................................................

rm -rf dirname force remove dirname


....................................................................................................................................................................

copy filename1 to filename2


cp filename1
filename2
....................................................................................................................................................................

cp -r dirname1 copy dirname1 to dirname2; create


dirname2 dirname2 if it does not exist
....................................................................................................................................................................

rename or move filename1 to filename2;


mv filename1 however, if filename2 is an existing
filename2 directory, moves file1 into directory
filename2
....................................................................................................................................................................

cat > filename place standard input into filename


....................................................................................................................................................................

more filename output the contents of filename


....................................................................................................................................................................

head filename output the first 10 lines of filename


....................................................................................................................................................................

tail filename output the last 10 lines of filename


....................................................................................................................................................................

output the contents of filename as it grows,


starting with the last 10 lines
tail -f filename
....................................................................................................................................................................

change the permissions of filename (or


dirname) to octal (see the main text for the
chmod octal
detailed explanation)
filename
Quantum ESPRESSO Course for Solid‐State Physics | 295

$ pwd

The pwd command displays the absolute (full) path of our present
working directory. Below is an example of pwd execution along with
its output:

$ pwd
/home/quantum/QE -SSP

cd: Change directory


The cd (“change directory”) command is used to move from
the present working directory to another directory. Without any
argument, cd will take us to our home folder:

$ cd

We can check its output by the pwd command

$ pwd
/home/quantum

Now to change to a particular directory, we can use its absolute


path or relative path. Assuming that the directory QE-SSP exists in
the home folder, we can navigate to QE-SSP by using the relative path
as follows

$ cd QE -SSP

or

$ cd QE -SSP/

For better clarity of the relative path, we may also add a period
character (.) before the directory name we want to enter since
the period character emphasizes the path of the current working
296 | Productivity Tools

directory. The following command will give the same output as


before:

$ cd ./QE -SSP/

If we prefer to navigate to the directory using its absolute path,


we should specify the full path of the directory starting from the root
“/” until the folder name we want to enter. Using the same directory
example as before, we should type the cd command as follows in its
absolute path:

$ cd /home/quantum/QE -SSP

Note that the home directory in Linux (in this example:


/home/quantum) also has an alias denoted by a tilde symbol
(~). Therefore, the following command:

$ cd ~/QE -SSP

is equivalent to the previous one.


What if we want to move back from QE-SSP to /home/quantum
and to /home? Two dots (..) represent the parent directory, which
is the directory immediately above the current one. It means the
parent directory of QE-SSP in this case is /home/quantum, while the
parent directory of /home/quantum is /home (see Fig. 6.10 for the
illustration). Therefore, to switch to /home/quantum from the current
working directory QE-SSP, we would type:

$ cd ../

If we want to directly move two levels up from QE-SSP to /home,


it is possible to type the following command

$ cd ../../
Quantum ESPRESSO Course for Solid‐State Physics | 297
Note that if the directory that we want to change has spaces in
its name, we should either surround the path with quotes (single or
double quotes):

$ cd "dirname with space"

or use the backslash (\) character to escape the space:

$ cd dirname\ with\ space

ls: List directory contents


The ls command will list information about files and directories
within a directory. If we use it without any options and arguments,
ls displays a list in alphabetical order of the names of all files in the
current working directory:

$ ls

If we are already in /home/quantum/QE-SSP/gete, the output of


the ls command execution would be similar to the following:

[Link] [Link]
[Link] [Link]
[Link] pseudo

To list files in a specific directory, we can pass the path


to the directory as an argument. For example, if we are in
/home/quantum/QE-SSP/, the following command will give exactly
the same output as before:

$ ls gete
[Link] [Link]
[Link] [Link]
[Link] pseudo
298 | Productivity Tools

We see that the default output of the ls command shows only the
names of the files and directories. We can use the -l option to print
files in a “long” listing format:

$ ls -l

The command output includes file/directory size, file/directory


type, permissions, number of files/directories therein, owner, and
timestamps (of latest modifications). An example of the output is
given below:

$ ls -l
total 24K
-rw -r--r-- 1 user group 74 Jul 10 06:39 file1
-rw -r--r-- 1 user group 100 Jul 10 06:39 file2
-rw ------- 1 user group 1.2K Jul 10 06:39 file3
-rw ------- 1 user group 892 Jul 10 06:39 file4
-rw ------- 1 user group 829 Jul 10 06:39 file5
drwxr -xr -x 2 user group 4.0K Jul 10 06:39 dir1
drwxr -xr -x 1 user group 4.0K Jul 10 06:39 dir2

The first line of the output is the total size of files (and directory)
in the directory. Then, the next lines list the full information of the
files and directories. The leftmost character in each line will be either
a dash (-), which represents a file or a letter (d), which represents
a directory. The set of following letters (rwxrwxrwx) indicate the
permissions given to the particular file or directory.
The permissions are broken into three roles, i.e. rwx, rwx, and
rwx for a user, a group, and others, respectively. The name of the user
and the group are listed in user and group columns in the output
of ls -al. Then, each role has specified permission for the file or
directory in the following order: read (r), write (w), and execute (x).
A dash character (-) is shown if there is no permission to read, write,
or execute. Therefore, the following line,

rw-r--r-- ... file1

means that file1 is

• rw-: readable, writable, and unexecutable by the user;


Quantum ESPRESSO Course for Solid‐State Physics | 299
• r--: readable, unwritable, and unexecutable by the group;
• r--: readable, unwritable, and unexecutable by the others
(beyond the user and the group).

Similarly,

rwxr-xr-x ... dir2

means that dir2 is

• rw-: readable, writable, and executable by the user;


• r--: readable, unwritable, and executable by the group;
• r--: readable, unwritable, and executable by the others.

Note that the ls command does not list the hidden files by
default. A hidden file is any file that begins with a period character.
To display all files, including the hidden files, use the -a option:

ls -al

We can then combine different options in one line, whether with an


argument or not.

$ ls -al
$ ls -al /home/quantum/QE -SSP/gete

The first command above will list all files and folders in the current
working directory, including the hidden files therein in the long
listing format. Meanwhile, the second command will output the list
for the specified directory. An example of the output is given below:

$ ls -al
total 36K
drwxr -xr -x 3 user group 4.0K Jul 10 06:39 .
drwxr -xr -x 5 user group 4.0K Jul 10 06:39 ..
-rw -r--r-- 1 user group 74 Jul 10 06:39 file1
-rw -r--r-- 1 user group 100 Jul 10 06:39 file2
-rw ------- 1 user group 1.2K Jul 10 06:39 file3
-rw ------- 1 user group 892 Jul 10 06:39 file4
300 | Productivity Tools

-rw ------- 1 user group 829 Jul 10 06:39 file5


-rw ------- 1 user group 829 Jul 10 06:39 .file6
drwxr -xr -x 2 user group 4.0K Jul 10 06:39 dir1
drwxr -xr -x 1 user group 4.0K Jul 10 06:39 dir2
drwxr -xr -x 2 user group 4.0K Jul 10 06:39 .dir3

In this example, .file6 and .dir3, which were previously not


shown, are now listed in the output. You can practice the ls -al
command to the other directories that contain hidden files.

touch: Create files


The touch command is used to update the timestamps on existing
files and directories as well as to create new and empty files, which
previously do not exist.
To create a file, specify the file name as an argument:

$ touch new -[Link]

If the file already exists, the touch command will change the file’s
last access and modification times to the current time. If the file does
not exist yet, the command creates an empty [Link] with a
zero‐byte size.

ln: Create symbolic links


A symbolic link (or symlink) is a special type of file that points to
another file or directory. The concept is similar to making a shortcut
in Windows operating system (OS). In Linux‐based OS, to create a
symbolic link to a given file, we use the ln command with the -s
option, the name of the file as the first argument, and the name of
the symbolic link as the second argument:

$ ln -s sourcefile symboliclink

Suppose our current working directory is the home folder. We want to


create a symbolic link to ~/QE-SSP/gete and put it under the home
directory as GeTe. We should type
Quantum ESPRESSO Course for Solid‐State Physics | 301

$ cd ~
$ ln -s ~/QE -SSP/gete gete

The above commands will give a symlink named as gete in the home
directory so we can access gete folder directly from the home
directory.

$ cd ~/ gete

If only one file/directory is given as an argument, ln creates a


link to that file/directory in the current working directory with the
same name as the file/directory it points to. The following command
executed in the home directory will give the same result as the
previous symlink:

$ ln -s ~/QE -SSP/gete

mkdir: Create a new directory


We can create a new directory using the mkdir command. To create
a directory, pass the name of the directory as the argument to the
mkdir command. For example,

$ mkdir /tmp/newdirectory

mkdir can take one or more directory names as its arguments. If the
argument is a directory name, without the full path, the new directory
is created in the present working directory.
To create directories along with the parent directory, we should
use the -p option. For example,

$ mkdir -p QE -SSP/gete/out

creates the whole directory structure in the present working


directory, from QE-SSP going to gete, and finally out. Note that
when mkdir is invoked with the -p option, the command creates the
directory only if it does not exist.
302 | Productivity Tools

rm: Remove files or directories


To remove files or directories in Linux, use the rm command. We
should be very careful when using this command because we cannot
(in most cases) recover the files after removing them. By default (and
also for safety), the rm command will not work when it is executed
without any option.
To delete a file or a symbolic link, use the rm command followed
by the file name as an argument:

$ rm [Link]

The rm command accepts one or more files or directories as


its arguments. Note that when we delete a symlink, the original
sourcefile will not be removed by the rm command.
The -i option tells rm to prompt the user for each given file
before removing it:

$ rm -i [Link]
...
rm: remove regular empty file '[Link]'?

Only when we answer the above question with “y” (without quotes),
the rm command will perform.
Use the ‐d option to remove one or more empty directories:

$ rm -d dirname

To remove non‐empty directories and all the files within them


recursively, use the -r (recursive) option:

$ rm -rf dirname

The -f option does not prompt the user, and rm -rf command
deletes all files and arguments under the directory without asking
remove or not. It should be carefully noted that the rm -rf ./*
command is considered as the most dangerous command in Linux
since all files and folders under the current working directory will
Quantum ESPRESSO Course for Solid‐State Physics | 303
completely disappear. Imagine if we execute this command in our
home directory, all our works will be wiped out, and if we execute
the command in the root (/), our OS can be broken. Therefore, the
readers should not use this command or use it after to back up the
contents of the directory before performing the command.

cp: Copy files and directories


The cp command allows us to copy files and directories. To copy a
file in the present working directory, use the source file as a first
argument and the new file as the second:

$ cp [Link] [Link]

To copy a file to another directory, we should specify the absolute


or the relative path to the destination directory. When only the
directory name is specified as a destination, the copied file under the
destination will have the same name as the original file. For example,
the following command

$ cp [Link] /home/quantum/QE -SSP

will copy [Link] in the present working directory to the


/home/quantum/QE-SSP (or ~/QE-SSP) folder with the same file‐
name ([Link]). Note that by default, if the destination file exists, it
will be overwritten without asking you unless you do not use cp -i.
To copy a directory, including all its files and subdirectories, use
the -R or -r option, as in the following example:

$ cp -r QE -SSP QE -SSP -backup

mv: Move and rename files/directories


The mv command (short from move) is used to rename and move
files/directories from one location to another. For example, to move
a file named [Link] to the /tmp folder, we execute:

$ mv [Link] /tmp
304 | Productivity Tools

To rename the file, we need to specify the destination file name:

$ mv [Link] [Link]

The command order for moving directories is the same as when


moving files. To move multiple files and directories at once, specify
the destination directory as the last argument:

$ mv [Link] [Link] /tmp

cat: Show file contents


The cat command shows the contents of one or more files and merge
(concatenate) files by appending one file’s contents to the end of
another file. To display the contents of a file on the screen, pass the
file name to cat as an argument. For example, executing

$ cat [Link]

will output the contents of [Link] (Sec. 6.1.1) to the terminal’s


screen. Adding an operator of standard output (>) after the cat
command can make the command act as the cp. The following
command line:

$ cat [Link] > [Link]

will give an exact copy of the [Link] file in the


[Link] file.

more and less: Show file contents interactively


The more or less command is used to view the text files in the
command prompt, displaying one screen at a time in case the file has
many lines. The two commands are very similar, so we will consider
that they are just the same command for our purpose of navigating
large output files of Quantum ESPRESSO calculations. The merit of
the commands is that they allow us to scroll up and down the file page
by page. For example, we can check the usage of the more command
on the [Link] file:
Quantum ESPRESSO Course for Solid‐State Physics | 305

$ more [Link]

After executing the above command, we will be prompted with an


output page (starting from the first line down to a certain line
number) waiting for instruction. Press the space bar (or letter f) key
to go to the next page. If we want to go back to the previous page, we
can press letter b or p key. Use letter q key to quit the prompt.

head (or tail): Display the beginning (or end) of the file
The head and tail commands can be used to display the beginning
and end of the file, respectively. By default, the number of lines shown
by the two commands is limited to 10. If we want to change the
number of lines printed on the screen, we can use the -n option
followed by the desired number. We can try performing the head and
tail commands on the [Link] file. For the head command,
the example execution along with the resulting output is as follows:

$ head -n 13 [Link]

Program PWSCF 6.7 starts on 13 Aug2021 at [Link]

This program is part Quantum ESPRESSO suite


for quantum simulation of materials; please cite
"P. Giannozzi et al., JPCM 21 395502 (2009);
"P. Giannozzi et al., JPCM 29 465901 (2017);
URL http :// [Link] -[Link]",
in publications or presentations.
More details at http :// [Link] -[Link]/

Parallel version (MPI), running on 8 processors

For the tail command (this is useful to see JOB DONE message):

$ tail -n 8 [Link]
PWSCF : 10m33 .89s CPU 16m 8.73s WALL

This run was terminated on: 7: 9:24 13 Aug2021

=-----------------------------------------------=
JOB DONE.
306 | Productivity Tools

=-----------------------------------------------=

Similar to the cat command, we can combine either head or tail


command with the standard output operation.

$ tail -n 8 [Link] > [Link]

The above command will print the last 8 lines of [Link] into
the [Link] file, which can be opened later by a text editor.
The tail command also has a special option: -f, which is useful
for displaying the contents of the file as it grows, starting with the
last 10 lines. We can benefit from such an option, in particular, when
monitoring a job running on the background. For example, when we
execute

$ pw.x < [Link] > [Link]

we may want to observe the evolution of [Link] during the


running process of pw.x, which can take several minutes or even
hours depending on the size of the calculated system. After executing
the above command, we can follow it by the tail -f command to see
the output ([Link]) in real time when the tail -f command
is entered:

$ tail -f [Link]

highest occupied level (ev): 7.1721

! total energy = -239.63636259 Ry


estimated scf accuracy < 3.6E-11 Ry

The total energy is the sum of following terms:


one -electron contribution = -147.05152059 Ry
hartree contribution = 88.78504266 Ry
xc contribution = -71.86764050 Ry

Note that, depending on the time of execution, it is normal if the


output obtained by the readers is different from the above example.
If pw.x calculation is successfully completed, the same “JOB DONE”
Quantum ESPRESSO Course for Solid‐State Physics | 307
Table 6.2 Process management and system information commands.

Command Purpose
whoami display who we are logged in as
....................................................................................................................................................................

w display who is online on the computer


....................................................................................................................................................................

df show disk usage


....................................................................................................................................................................

du show directory space usage


....................................................................................................................................................................

show possible locations of an application


named app
whereis app
....................................................................................................................................................................

which app show which app will be run by default


....................................................................................................................................................................

lists stopped or background jobs; resume a


stopped job in the background
bg
....................................................................................................................................................................

fg bring the most recent job to foreground


....................................................................................................................................................................

top display all running processes


kill pid kill a process whose “id” number is pid
....................................................................................................................................................................

killall proc kill all processes named proc


....................................................................................................................................................................

show cpu information, a shortened version of


cat /proc/cpuinfo command
lscpu
....................................................................................................................................................................

show memory and swap usage, a shortened


version of cat /proc/meminfo command
free
....................................................................................................................................................................

mpirun -np N app run app in parallel with N processors


....................................................................................................................................................................

keep command (or app) process running even


nohup command if we exit the shell (but not when the
computer is shutdown)
....................................................................................................................................................................

exit logout from the current session

message for [Link] (and other kinds of Quantum ESPRESSO


calculation outputs) should appear.

6.2.2 System information and process management

Linux and other Unix‐based systems allow multiple processes to


operate simultaneously without interfering with each other. Due to
308 | Productivity Tools

this nature, there are many commands dedicated to managing the


processes and for displaying system information in Linux. We will not
discuss all the commands, but we will list some of them that can be
used along with the Quantum ESPRESSO calculations. The summary
of Linux commands for process management and for displaying
system information is given in Table. 6.2. We briefly describe each
command one by one below.

whoami: Ensuring we know ourselves


The whoami command is used to check our own username. Suppose
our username is quantum, the output of this command will also be
quantum.

$ whoami
quantum

At a glance, we may consider this command not useful if we only


have a simple username that is easy to memorize. However, in multi‐
user environments such as high‐performance computing (HPC)
clusters and supercomputers, we usually cannot set the username by
ourselves because the system administrator will create the account
for us. It is also possible that we obtain several usernames for one
HPC system. In that case, the usernames can be a combination of
letters and numbers that could be difficult to memorize. When we log
into one username and switch to another username, we can forget
who we are. In such a situation, the whoami command is a handy
command to rescue us.

w: Display online users


The w command is used to know who is online on the computer we
are logged in to. This command also shows other details such as the
load average of the computer, the terminal number (“tty”) we are
connected to, how long the computer is already on, and how long the
users have been logged in (or oppositely, have been idle). We simply
type the letter w to run this command.

$ w
Quantum ESPRESSO Course for Solid‐State Physics | 309

Figure 6.11 Example output of the w command in a HPC.

An example output of this command performed on a HPC is shown


in Fig. 6.11. The username of one of the book authors is ahma079,
running the w command, shown with other users who are online at
that time. When more than two users use a computer, it would be nice
to check it other users run Quantum ESPRESSO by w command.

df and du: Disk and directory space usage


Sometimes we want to know the disk (or file system in Linux) and
directory space usage. For that purpose, we can use the df and du
commands. In particular, if we are a user of a computer with limited
space, we might be concerned with the directory space usage (the
du command). Suppose we are in the directory where the input and
output of the GeTe calculation files are located. We can use the du
command to check the space used by all these files. An example
execution and output of the du command is given below.

$ du -h
392K ./out
1.0M ./ pseudo
6.7M .

From the output above, we can see the usage of that directory
(denoted by the period symbol) is around 6.7 MB. The output
obtained by the readers may be different with this example
depending on the readers’ activities in the directory.

whereis and which: Understanding app locations


To know possible locations of an application named app, we can use
the whereis command:
310 | Productivity Tools

$ whereis app

For example, in a HPC system at which one of the book authors is


registered, the output of whereis pw.x is as follows.

$ whereis pw.x
pw:
/apps/tools/qe -6.4.1/ bin/pw.x
/home/ahma079/qe -6.7/ bin/pw.x

The output indicates that there are two locations of pw.x. The first
one is /apps/tools/qe-6.4.1/bin/pw.x and the second one is
/home/ahma079/qe-6.7/bin/pw.x. This output can vary depending
on the system used by the readers.
The question now is, “Which app is used by default when we call
pw.x?” To get the answer, we can execute the which command with
the following format:

$ which app

For example, in the HPC system same as before:

$ which pw.x
/apps/tools/qe -6.4.1/ bin/pw.x

The output above informs us that the pw.x command from


/apps/tools/qe-6.4.1/bin/ folder will be used when we execute
the pw.x command without its absolute path. If we want to use pw.x
from /home/ahma079/qe-6.7/bin/ instead, we must supply its full
absolute path to pw.x, i.e.,

$ /home/username/qe -6.7/ bin/pw.x < input > output

bg and fg: Background and foreground jobs


The bg command is used to resume a stopped job in the background.
On the other hand, the fg command is used to bring the most recent
job to the foreground. To understand the concepts of background
Quantum ESPRESSO Course for Solid‐State Physics | 311
and foreground in the Linux terminal, we can try running the SCF
calculation of GeTe on our computer.

$ pw.x < [Link] > [Link]

By entering the above command only, we should notice that the


cursor in the terminal does not return to the original command
prompt. The cursor waits until the system finishes running pw.x as a
foreground job, which actually takes a long time.
If we want to stop the calculation temporarily, we can press
CTRL+z keystroke combination and continue writing other com‐
mands in the terminal as we wish. An example output message that
appears after we press CTRL+z is given below.

^Z
[1]+ Stopped pw.x < [Link] > [Link]

Now the command prompt returns to the original state with the
cursor waiting for our action. What happens to the pw.x calculation
above is that it stops until we enter either the fg command or bg
command. First, let us try the fg command. Its execution with the
example output is as follows.

$ fg
pw.x < [Link] > [Link]

In the above case, after executing the fg command, we can


realize that pw.x continues running as a foreground job, indicated
by the appearance of the second line (pw.x < [Link] >
[Link]) on the screen, but then the cursor gets stuck until the
calculation finishes. If the calculation is fast enough, we can do this
kind of fg command and just wait for a moment with the inability
of typing further commands in the terminal. However, we might
not want to wait for the calculation, especially when the calculation
takes a very long time. Instead, we want to continue other works
in the terminal. In this case, it is a good idea to send the job to
the background. We can press CTRL+z again, followed with the bg
command.
312 | Productivity Tools

$ bg
[1]+ pw.x < [Link] > [Link] &

The output message appearing on the second line indicates that


pw.x is running as a background job, and the calculation will continue
running while at the same time we can type other commands on the
terminal. The ampersand character (&) in the example above tells
the job to be run in the background. Therefore, instead of pressing
CTRL+z and the bg command, we can also accompany the pw.x
command line with the ampersand character if we decide to run the
calculation on the background since the beginning of the calculation.

$ pw.x < [Link] > [Link] &

This command will take several minutes or hours depending on the


computing resources we have in our PC. It is then natural to ask how
we can monitor the process and how we can stop it when we wish to
do so. The top and kill command will be helpful now.

top: Displaying all running processes


The top command is used to show the Linux processes. The
command provides a dynamic real‐time view of the running system,
including the summary information of the system and the list
of processes. As soon as we run this command, it will open an
interactive command mode where the top half portion will contain
the statistics of processes and resource usage. The lower half
contains a list of the currently running processes. Pressing the letter
q will exit the top command mode.
After we run the SCF calculation in our PC, we can check its
status by the top command. Below is an example of the top command
execution with some parts of its output.

$ top
Tasks: 16 total , ...
...
PID USER ... COMMAND
850 quantum ... pw.x
...
Quantum ESPRESSO Course for Solid‐State Physics | 313
There are actually further details (…) of the output but we just show
the number of tasks, the process number (PID), the user who has
the process, and the command related to the process. Note that the
output of PID and USER may vary. However, if we run the same pw.x
command for the SCF calculation of GeTe, we can see it somewhere
below the COMMAND information of the top output. If we think that the
calculation was run wrongly or if we just want to stop it, we should
write down its PID, press q to exit the top command mode, and run
the kill command as will be explained next.

kill: Termination of jobs in the middle


Suppose we want to stop the SCF calculation with PID already known
from the previous example of top command, we can execute

$ kill pid

In our example of GeTe above, we should perform

$ kill 850

Is the process killed completely? Sometimes, no! We should confirm


it by executing the top command again. If we still see the pw.x
command with the same PID, we have two ways to completely kill
the process. The first way is by using -9 option:

$ kill -9 850

The second way is by using the killall with the process (or
command) name as its argument.

$ killall pw.x

lscpu and free: CPU and memory information


We can use the lscpu and free commands to check the processor
(CPU) and memory information of the computer we are logged in.
The lscpu command can be considered as a shortened version of cat
/proc/cpuinfo command, while the free command is a shortened
314 | Productivity Tools

version of cat /proc/meminfo. The example execution of lscpu and


free are shown below with some parts of their outputs.

$ lscpu
Architecture: x86_64
CPU op -mode(s): 32-bit , 64-bit
Byte Order: Little Endian
CPU(s): 8
On -line CPU(s) list: 0-7
Thread(s) per core: 2
Core(s) per socket: 4
Socket(s): 1
...

$ free
total used free ...
Mem: 11337068 143932 10964292 ...
Swap: 3145728 0 3145728 ...

nohup: No hang up
When exiting the terminal or shell of a Linux System, all running
processes are usually terminated or hang up. If we want to keep the
processes running when exiting the terminal, we can use the nohup
command. It should be noted, however, that if the computer (not only
a physical computer but also a virtual machine or WSL) is shut down,
the nohup command and the processes to which it is assigned will
also stop. Therefore, if we want to run very long Quantum ESPRESSO
jobs, in addition to using the nohup command, we should make sure
that the computer can survive running within the calculation time
frame.
The nohup command can be combined with the background
operator & as follows

$ nohup command &

where command can also be an app or other processes. For example, if


we want to keep running the GeTe calculation in the background and
safely exit our terminal, we can execute
Quantum ESPRESSO Course for Solid‐State Physics | 315

$ nohup pw.x < [Link] > [Link] &

When we re‐login to the terminal (as long as the computer is


not shutdown), we can find by using the top command that the
calculation is not interrupted. The nohup command is often used with
a batch script as we will explain in Sec. 6.3.

exit: Quitting the session


As the name clearly tells us, the exit command is used to quit or
logout from the current session of terminal/shell. We can simply type
this command without any argument

$ exit

We can practice the exit command after the nohup command


executed previously, and then login again to check (by using the top
command) that the Quantum ESPRESSO calculations we submitted
to the computer are still running.

6.2.3 Running parallel calculations

Recently, with the availability of “multicore” and “multithread”


processors, most personal computers already allow us to perform
parallel calculations with the mpirun command. However, to use
this command, we should make sure that mpirun is installed in
our system. If it is not installed in a Debian/Ubuntu Linux‐based
distribution, we can select one of few messages passing interfaces
(MPI) implementations such as Open MPI, MPICH, and Intel MPI. The
following is an example installation for the Open MPI library:

$ sudo apt install libopenmpi -dev

Note that we should have already done this step in Chapter 2 if we


compile Quantum ESPRESSO with the parallel processing option.
A generic way to use the mpirun command is by using the -np
option followed by the number of cores (more precisely threads) in
a CPU (or CPUs) (N) for parallel processing:
316 | Productivity Tools

mpirun -np N app

In simple HPC systems, the number of cores (threads) given by lscpu


command is usually the maximum limit that we can assign to mpirun
when running Quantum ESPRESSO in parallel. For example, if the
number of CPUs from the lscpu output is 8, we can put 8 as the value
of N for the -np option in the mpirun command, combined with pw.x
or other Quantum ESPRESSO apps. However, we recommend not to
use the maximum number for the ‐np option since OS is also running
at least one core.

$ mpirun -np 8 pw.x < [Link] > [Link]

The majority of the codes that come with the Quantum ESPRESSO
package can run in parallel similarly to the above way.
In a more sophisticated HPC system (let us say a supercom‐
puter), the result of lscpu should not be taken into consideration
for deciding the value of the -np option because when we login
to that supercomputer, we are only brought to the “control node”
that will distribute our job to the other nodes dedicated for parallel
processing. In this case, we should consult the system administrator
or the manual of the supercomputer for the best setting of the CPU
number we can use for our calculations. On the other hand, if we are
just running Quantum ESPRESSO calculations on our own laptop, it
is safe to use a smaller CPU number than what is indicated by lscpu.
We should also be aware that plane‐wave DFT calculations (like
that in Quantum ESPRESSO) do not scale linearly. Our calculation
will get faster to a certain value of N, after which the calculation
will become slow if we add more parallel processes. The situation
can vary depending on the PC hardware (chipset, memory size, etc.),
software, and type of calculation we are doing, but usually we will see
a reduction in the speedup after around 8–32 processes.
If we do the generic way of mpirun above (such as mpirun -np
8 pw.x < [Link] > [Link]), we accept the default
“strategy” for parallelizing the calculation. Using this default strategy
for the pw.x command, in most cases, is already a good choice.
However, if we are not satisfied with the speedup given by the default
Quantum ESPRESSO Course for Solid‐State Physics | 317
strategy, we can use other parallelization schemes as discussed
in detail in the Quantum ESPRESSO documentation, especially for
pw.x (“PWscf”) and ph.x (“PHonon”) applications, in the sections of
performances and parallelism, respectively.

6.2.4 Parallelization in Quantum ESPRESSO


In the following, we just briefly discuss some types of parallelization
that can be implemented in Quantum ESPRESSO because advanced
users may want to play by themselves with the possible paralleliza‐
tion strategies beyond the default one.
Parallelization for several independent calculations. If the
calculation we have asked for involves running several similar
calculations, the computers can break up these calculations between
parallel processes. Quantum ESPRESSO offers this functionality for
some applications, particularly ph.x), which refers to these sets of
calculations as “images”. We can set how many of these images are
used for a parallel calculation with -nimage or -ni. If we run with
20 processors and specify -ni 2, each image will use 10 processors.
The default for -ni is just 1.
Parallelization of k‑points. This parallelization strategy needs
very little communication between processes and thus can offer good
scaling, as each k‐point can be treated as effectively an independent
calculation where results are added together at the end. This strategy
will be effective when the number of k‐points in our calculation is
quite large (typically larger than 10 × 10 × 10 k‐point grid in the
Monkhorst‐Pack scheme). We can set how many parallel groups of
k‐points for our calculation with the -npool or -nk flag in Quantum
ESPRESSO (e.g., add -nb 2 option next to the pw.x command). The
default -nk is 1.
Parallelization over bands. The calculation for each energy
band can also be parallelized. This strategy may cut down the
amount of memory used by each process but requires a bit more
communication between processes. We can set the number of bands
to be grouped in the parallel calculation by using the -nband or -nb
flag (e.g., add -nb 2 option next to the pw.x command). The default
-nb is 1.
Parallelization for plane waves. For the sake of completeness
of the information, we should note that Quantum ESPRESSO by
318 | Productivity Tools

Table 6.3 Search in Linux using the grep commands.

Command Purpose
grep pattern files search for pattern in files
....................................................................................................................................................................

search recursively for pattern in dirname


grep - r pattern
dirname
....................................................................................................................................................................

search for pattern in the output of command


command | grep
pattern

default already distributes the plane wave basis sets efficiently to


the parallel processors. This strategy is always turned on if Quantum
ESPRESSO is compiled and run in parallel.

6.2.5 Searching

For searching files and patterns, Linux has some powerful commands
such as grep, find, and locate. For our purpose of analyzing and
understanding Quantum ESPRESSO output files, it is sufficient for
us to focus on the grep command. The usage summary of the grep
command is given in Table. 6.3. Let us practice this command for
obtaining some important information about our calculation.
The most basic syntax for grep is

$ grep pattern files

where the pattern can be a character, a word, a sentence, or


any string we want to find in files. Suppose we already have a
[Link] file in the ~/QE-SSP/gete/ directory, we can search
for all occurrences of “total energy” in [Link] as follows:

$ grep "total energy" [Link]

The output will be similar to as follows:


Quantum ESPRESSO Course for Solid‐State Physics | 319

total energy = -239.63505730 Ry


total energy = -239.63624073 Ry
total energy = -239.63633447 Ry
total energy = -239.63635918 Ry
total energy = -239.63636081 Ry
total energy = -239.63636155 Ry
total energy = -239.63636227 Ry
total energy = -239.63636260 Ry
total energy = -239.63636259 Ry
total energy = -239.63636258 Ry
! total energy = -239.63636259 Ry
The total energy is the sum of the following terms
:

In this case, since the SCF calculation performed iterations for finding
the minimum total energy, we see the words “total energy” several
times according to the number of iterations. The one with the
exclamation mark (!) is the final result of total energy. Therefore,
if we just want to know this final result, we can change the search
pattern to the exclamation mark. The grep execution with its output
in this case is

$ grep ! [Link]
! total energy = -239.63636259 Ry

The grep command can be used recursively to find the pattern


in a specified directory. The syntax is

$ grep - r pattern dirname

We can practice this syntax for finding the Fermi energy in the gete
working directory (~/QE-SSP/gete/).

$ grep -r Fermi .

Remember that the period (.) sign denotes the current directory,
so that the above syntax will look for all occurrences of “Fermi” in
all files of the current directory. The example of the output is given
below:
320 | Productivity Tools

./ [Link]: the Fermi energy is 7.2526 ev


./ [Link] : ... EFermi = 7.253 eV

The output tells us that the grep command could find the word
“Fermi” in the [Link] and [Link] files in the current
directory.
The grep command can also be combined with the other Linux
commands through the “piping” method with the pipe (|) character.
The syntax is

$ command | grep pattern

where command is any command or app. For example, we can list the
current directory and find any file or directory which contains “out”
pattern. The example execution and some parts of its output are given
below.

$ ls -al | grep out


-rw -r--r-- 1 ... [Link]
-rw -r--r-- 1 ... [Link]
-rw -r--r-- 1 ... [Link]
-rw -r--r-- 1 ... [Link]
-rw -r--r-- 1 ... [Link]
drwxr -xr -x 2 ... out

6.2.6 Keyboard shortcuts

There are some useful shortcuts available in the Linux terminal. The
summary of these keyboard shortcuts is given in Table. 6.4. You can
try them one by one following the table.

Self‐contained “help” of the command


Usually, a Linux command can have many options that are specified
by a single hyphen (-) or double hyphen (--). If we do not use
the command frequently, we might forget its options. However,
memorizing command options is not necessary because almost all
Linux commands have a --help option, which prints a short message
Quantum ESPRESSO Course for Solid‐State Physics | 321
Table 6.4 Keyboard shortcuts in the terminal.

Shortcut Purpose
CTRL+c halt the current command
....................................................................................................................................................................

stop the current command, resume with fg


in the foreground or bg in the background
CTRL+z
....................................................................................................................................................................

logout from the current session, similar to


CTRL+d
exit
....................................................................................................................................................................

erase one word before the cursor in the


current line
CTRL+w
....................................................................................................................................................................

CTRL+k erase any characters after the cursor


....................................................................................................................................................................

CTRL+u erase the whole line


....................................................................................................................................................................

move to a command backward;


CTRL+p browse/recover previous command(s)
according to the terminal history
....................................................................................................................................................................

move to a command forward; in combination


with CTRL+p, browse/recover previous
command(s) according to the terminal
CTRL+n
history
....................................................................................................................................................................

move the cursor forward to the end of the


next word in the current line
ALT+f
....................................................................................................................................................................

move the cursor backward to the beginning


of the previous word in the current line
ALT+b
....................................................................................................................................................................

CTRL+e move the cursor to the end of the current line


....................................................................................................................................................................

move the cursor to the beginning of the


current line line
CTRL+a

about how to use the command. The way we write it in the terminal
is as follows.

$ command --help

For example,
322 | Productivity Tools

$ cp --help

will give us the details of the cp command as well as its possible


options.
The Linux commands are also often distributed together
with manual (man) pages. A man or manual page is a form of
documentation that explains what the command does, examples of
how you run the command, and what arguments it accepts. The man
command is used to display the manual page of a given command.

$ man command_name

For example, to open the man page of the command to change the
directory (cd), you would type

$ man cd

To navigate the man pages, use the Arrow, Page Up, and Page
Down keys. We can also press the Enter key to move one line at a time,
the Space bar to move to the next screen, and the letter b key to go
one screen back. To exit the man page, press the letter q key.

6.3 Shell scripts and batch jobs

Shell scripts are useful for automating processes and running many
jobs simultaneously because shell scripts allow us to string together
sets of commands. With the shell scripts, we could write a script
that runs calculations, parses the important results to another file,
and even generates a plot. In this section, we will learn basic shell
scripting to be able to automate Quantum ESPRESSO calculations and
to submit batch jobs as a background job with nohup command.
There are often several different shells installed on a Linux
system, and bash is typically the default one in most Linux
distributions. The bash shell is running in the terminal, and it
interprets and executes the commands we type in.
Quantum ESPRESSO Course for Solid‐State Physics | 323
6.3.1 Environment

On startup, such as when a new terminal is opened, or when we


connect to a remote system over the network, bash will read several
configuration files.

• For login shells (the shells where we are prompted to login


when we start it), bash will read configuration options in
~/.bash_profile or ~/.profile if this file exist. Note ~ is
interpreted by the shell as the users home directory.
• For non‐login shells (such as when we open the terminal
emulator), bash will read configuration options in ~/.bashrc.
• For simplicity, we can put all our configuration options in
~/.bashrc and leave the ~/.profile file empty except for a
command to read the ~/.bashrc file.

These files can be used to configure many aspects of the shell,


such as how the prompt looks in the terminal and also how specific
apps such as Quantum ESPRESSO can be found by defining the so‐
called environment variables.
Many important aspects of how bash behaves are governed
by environment variables. These variables can be modified to our
preference in the configuration files. To see the current value of one
of these variables, e.g., the PATH variable, we can do the following

$ echo $PATH

The $ symbol in front of the variable (e.g., PATH variable) is used


when referring to the data stored in a variable, and echo is a built‐in
bash command that outputs something to the terminal.
The PATH variable tells bash which directories to look in for
executables so they can be run without typing in their full path.
For example, we can just type in nano in the terminal to launch
the program, as the PATH variable contains the directory holding an
executable of this name. Different directories are separated by “:”.
To add the directory ~/.bin to our path, we can add the line
export PATH="~/bin:$PATH" to the .bashrc file. Here the export
statement is used to allow the variable to be usable by any child
324 | Productivity Tools

processes rather than local to the script. Note when we are naming
the variable we want to assign a value to we do not use a $. Also we
just want to add a directory to the existing PATH so we add :$PATH to
keep all the existing directories.
We can practice updating the PATH variable to include not
only the ~/bin directory but also the Quantum ESPRESSO binary
directory. Suppose we install Quantum ESPRESSO latest version
(q-e) under ~/opt directory, we can add the export statements for
the PATH variable as follows:

~/.bashrc

export PATH="~/bin:$PATH"
export PATH="~/opt/q-e/bin:$PATH"

We need to execute the following command in the terminal to ensure


that the updated variables will be read by the shell without logout.

$ source ~/. bashrc

When you login the next time, you do not need this.

6.3.2 Scripting

This part covers basic scripting. If readers are interested in


developing more advanced scripts, we suggest referring to the http:
//[Link]/LDP/abs/html/[Link].
Let us make a simple example script to output “Hello World!”. We
can use the nano text editor to create it.

$ nano [Link]

The contents are as follows:

QE‑SSP/scripts/[Link]

#!/ bin/bash
# Simple script that outputs " Hello World !"
Quantum ESPRESSO Course for Solid‐State Physics | 325

echo "Hello World!"

The first line above tells the system what command is used to
interpret the contents of the script. This line should always begin
with #! and then the path to the executable. For bash the executable
will almost always be /bin/bash. In the script, comments are
represented by #. Linux commands can be typed the same as we
would enter them to the terminal. After writing the script, we will
need to make the script executable with chmod u+x [Link].

$ chmod u+x [Link]

Then, we can run it by typing ./[Link]. The execution and


example output are given below.

$ ./ [Link]
Hello World!

Note that if we move the [Link] file to ~/bin/ that has


been added to the PATH variable, we can execute the script from any
directory without specifying the current working directory (./).

$ mv [Link] ~/bin/
$ [Link]
Hello World!

Variables in shell scripts


In the bash shell scripts, variables need not be declared in any way,
we can assign a value and start using them. For example:

QE‑SSP/scripts/helloworld‑[Link]

#!/ bin/bash
# Example using variables .

var1="Hello"
var2="World!"
326 | Productivity Tools

echo $var1 $var2


# Note the use of the $ symbol when
# we want to use the value stored in the variable .

# Any command can use these variables


dirname="tmpdir"
mkdir $dirname

We can also read a value from standard input using the read
command as follows:

QE‑SSP/scripts/[Link]

#!/ bin/bash
# Example showing how the read command is used.

echo "Please enter some text:"


read user_text

echo "The text you entered was:" $user_text

Variable names are case sensitive in bash. The usual convention


used is that environment variables (e.g., PATH), and internal shell
variables (e.g., BASH_VERSION) are capitalized, while other variables
are in lowercase. Adopting this convention will ensure that we do not
accidentally overwrite any important variables in our scripts.

Command substitution
Command substitution allows the result of a command to replace the
command itself, acting much like a variable in practice. For example:

QE‑SSP/scripts/[Link]

#!/ bin/bash
# Example of command substitution .

# This can be done by


# enclosing the command in $( )
echo "It is now $(date)."
# Note: the "date" command outputs
# the current date and time.

# This can also be done by


# enclosing the command in backtics ` `
Quantum ESPRESSO Course for Solid‐State Physics | 327
echo "The files in this directory are:" `ls `

Conditional statements
The conditional if statements can be used in bash, and many types
of conditional tests are possible. We can test if the value stored in a
variable equals something as follows:

QE‑SSP/scripts/[Link]

#!/ bin/bash
# Example using if statements

echo "Please enter a yes or no: "


read user_response

# Note the spaces


# following `[` and before `]` are important .
if [ $user\_response = yes ]
then
echo "You entered yes."
elif [ $user\_response = no ]
# "elif" is the same as "else if"
then
echo "You entered no."
else
echo "You didn 't enter yes or no."
fi
# "if" statements are always ended with "fi ".

We can also check, e.g., if a file or directory exists:

QE‑SSP/scripts/[Link]

#!/ bin/bash
# Check if the directory " tmpdir " exists , and if not
, create it , then check
# if the file " tmpdir / testfile " exists , and if not ,
create it.

dirname="tmpdir"
filename="testfile"

if [ ! -d "$dirname" ]
# The "!" is logical negation
# -d tests that a file exists and is a directory .
328 | Productivity Tools

then
mkdir $dirname
fi

if [ ! -f "$dirname/$filename" ]
# -d tests that a file exists and is a regular file
(i.e., not a directory ).
then
touch "$dirname/$filename"
fi

Arithmetic and testing


To test numeric values, the (( ... )) construction can be used. For
example:

QE‑SSP/scripts/[Link]

#!/ bin/bash
# Example showing the use of the (( )) construction

var1 =4
var2 =5
var3 =8

if (( var1 + var2 > var3 ))


# Note within the (( ))
# don 't use $ symbols before variables .
then
echo "$var1 + $var2 > $var3"
else
echo "$var1 + $var2 <= $var3"
fi

# We can also use the construction


# to perform basic arithmetic
var4=$(( var1 var3 ))echo "\$var1 $var3 = $var4"

Looping
In bash shell, we can use the for loops to iterate a variable over a
range of values. For example:
Quantum ESPRESSO Course for Solid‐State Physics | 329
QE‑SSP/scripts/[Link]

#!/ bin/bash

# Simple for loop example .


# This construction loops
# over a space separated list of values .

# A variable whose contents contains spaces


# would work in the same way.

for i in 1 2 3
do
echo "Iteration number $i"
done

The for loop can be used to apply a command repeatedly to a set


of files. For example:

QE‑SSP/scripts/[Link]

#!/ bin/bash

# Example showing a simple for loop


# over a list of arguments used to convert
# a set of data files with comma separated columns
# to tab separated columns .

for csvfile in "$(ls .csv)"dodatfile="\$(basename


\$csvfile .csv).dat"sed 's/,/
/g' \$csvfile >
\$datfiledone

In the above example, basename is a tool to strip the suffix from a file.
Here we use it to construct a new filename with the .dat extension.
Bash also supports numeric loop ranges using the syntax
{start..finish..increment). For example:

#!/ bin/bash
# Example of for loop with numeric increments

for i in {0..6..2}
do
echo "The value of i is $i"
done
330 | Productivity Tools

In addition to the for loops, the bash shell also supports while
loops, where a set of commands are repeated continuously when a
given condition is true. For example:

QE‑SSP/scripts/[Link]

#!/ bin/bash
# Example of while loop in bash

counter =0

# Here -lt means less than


while [ $counter -lt 10 ]
do
echo $counter
counter=$(( counter + 1))
done

It is often convenient to use while loops to operate on every line


of a file. For example:

QE‑SSP/scripts/[Link]

#!/ bin/bash
# Example of while loop reading lines from a file

linenumber =0

while read line


do
linenumber=$(( linenumber + 1))
echo "$linenumber: $line"
# This will prepend each line of a file with its
line number .
done < [Link]

Arguments
We can pass arguments to bash scripts from the command line. For
example:

QE‑SSP/scripts/[Link]

#!/ bin/bash
# Example using command line arguments .
Quantum ESPRESSO Course for Solid‐State Physics | 331

# Call this script with several arguments .


echo "The first argument is $1"
echo "The second argument is $2"
echo "The number of arguments is $#"
echo "The full list of arguments is $@"

We can loop over arguments using a for loop as follows:

QE‑SSP/scripts/[Link]

#!/ bin/bash
# Example using a for loop to iterate over command
line arguments .

for arg in $@
do
echo $arg
done

6.3.3 Quantum ESPRESSO job script

Now let us make a simple script for submitting batch jobs of Quantum
ESPRESSO calculations. The script [Link] includes Quantum
ESPRESSO calculations of SCF, NSCF for obtaining DOS, and NSCF for
obtaining the energy dispersion.

QE‑SSP/gete/[Link]

#!/ bin/bash
# Simple QE job script
# This script can be re -used for other materials
# Later we just need to change
# the following 3 variables :
# first is the Quantum ESPRESSO binary location
export QEBIN =/ home/quantum/opt/q-e/bin/
# second is the number of CPUs
export NCPU =4
# last is the prefix of the calculations
export PREFIX=gete
# QE binaries that we want to use
export PW=$QEBIN/pw.x
export DOS=$QEBIN/dos.x
332 | Productivity Tools

export BND=$QEBIN/bands.x
# define variables for calculations of
# SCF , DOS , and energy dispersion
export SCF=$[Link]
export NSCFD=$[Link]
export POSTD=$[Link]
export NSCFB=$[Link]
export POSTB=$[Link]
echo "start SCF"
mpirun -np $NCPU $PW < $[Link] > $[Link]
echo "SCF done"
echo "start NSCF DOS"
mpirun -np $NCPU $PW < $[Link] > $[Link]
echo "NSCF DOS done"
echo "start Postprocessing DOS"
mpirun -np $NCPU $DOS < $[Link] > $[Link]
echo "Postprocessing DOS done"
echo "start NSCF BANDS"
mpirun -np $NCPU $PW < $[Link] > $[Link]
echo "NSCF BANDS done"
echo "start Postprocessing BANDS"
mpirun -np $NCPU $BND < $[Link] > $[Link]
echo "Postprocessing BANDS done"
echo "All jobs are finished"

The script can be run in the background using the nohup


command. This method will ensure that we can leave our terminal
safely and continue with other works.

$ nohup [Link] &

6.4 Plotting and visualization tools

Plotting a graph is essential for the readers of the scientific paper to


understand the numerical output values in a consistent and scientific
way. There are several libraries in the Python programming language
that can be used for plotting. Matplotlib is one of the most well‐
known and free Python libraries that can be used to produce a variety
of plots and graphics in many styles and can be interacted with in
flexible ways. Therefore, in this book, we choose Matplotlib in the
Python environment as the platform to produce graphical outputs
Quantum ESPRESSO Course for Solid‐State Physics | 333
from Quantum ESPRESSO data. The Matplotlib official site at https:
//[Link]/users/[Link] gives us a full list of
tutorials and various options we can adopt for any visualization
project. We also use the JupyterLab ([Link] which
is the next‐generation web‐based user interface for Python and
Matplotlib. The installation of Python, Matplotlib, JupyterLab, along
with other important Python libraries, is already explained in Sec.
2.2. This section will show you the most direct and relevant practice
of using Matplotlib that will give us a reasonable publication quality,
which is good enough to appear in a scientific paper.
Hereafter, we will explain how to write a Python plotting script
file with the *.ipynb extension, which can be opened and run directly
by JupyterLab as

$ jupyter -lab [Link]

In the Python script, we have to put import statements to load


some libraries related to the plotting and numerical computations.
We will load Matplotlib along with NumPy libraries for our purpose.
It is also recommended to create a “short form” of the library (e.g.,
mpl for matplotlib) so that we can refer to its functions using the
this short form instead of typing out matplotlib every single time.
The # sign is used for comments.

QE‑SSP/scripts/[Link]

1 # Import required packages


2 import matplotlib as mpl
3 import [Link] as plt
4 import numpy as np

Now suppose we have a data file called “[Link]”, which can be


found in QE-SSP/scripts/[Link]. The readers can also obtain the
[Link] file by running the tutorial in Sec. 3.3.2. The [Link] file is the
output file of the phonon density of states (phonon DOS) of graphene,
which contains four columns separated by space. The 1st column
corresponds to the frequency ω in cm−1 , the 2nd column is the total
phonon DOS in state/cm−1 /unit‐cell, and 3rd and 4th columns are
the partial phonon DOS of the C atom number 1 and number 2,
334 | Productivity Tools

respectively. We can load this data using the loadtxt function from
the NumPy package that we imported as np.

QE‑SSP/scripts/[Link]

1 # Use [Link] to import our data


2 omega , ph_tot , ph_c1 , ph_c2 = [Link]('[Link]',
unpack=True)

In the above blocks, we read the variable set omega, ph_tot,


ph_c1, and ph_c2 by the [Link] function. By setting
unpack=True (line 2), we transpose each column into an array.

6.4.1 Plain plotting of the data

Once we have loaded the data, we can make a default plot by


[Link]() (the short form of [Link]())
and inspect the dataset with the following code:

QE‑SSP/scripts/[Link]

1 # Create figure and add axes object


2 fig = [Link] ()
3 ax = fig.add_axes ([0, 0, 1, 1])
4
5 # Plot our data
6 [Link](omega , ph_tot)
7 [Link](omega , ph_c1)
8
9 # Save and show our data
10 [Link]('[Link]', bbox_inches='tight ')
11 [Link] ()

Some brief explanations for this block of code are as follows:

• fig = [Link](): assigns a figure object with the default


setting of Matplotlib.
• ax = fig.add_axes([0, 0, 1, 1]): defines default axes of
the fig object with the axis origin (the corner of the axis at the
bottom left) located at (0, 0), width = 1, and height = 1 in the
scaled units.
Quantum ESPRESSO Course for Solid‐State Physics | 335

Figure 6.12 A default plot of [Link], which is not suitable for a scientific paper.

• [Link](omega, ph_tot): calls the plot function for the ax


axes defined earlier, for which we plot ph_tot as a function of
omega.
• [Link](omega, ph_c1): on the same ax axes, calls the plot
function again to plot ph_c1 as a function of omega.
• [Link]('[Link]', bbox_inches='tight'):
saves the figure to “[Link]”, in which the setting
bbox_inches='tight' will remove all of the extra white
space around the figure.
• [Link](): displays the figure on the web‐based user
interface of JupyterLab.

We can see that the plot shown in Fig. 6.12 has a few meanings
already, but we notice that the default Matplotlib settings do not give
the figure with sufficient publication quality. For example, there is no
axis label, the font size is too small, and we may also not like the tick
marks outside the main frame. As we change some of the parameters
described in the next subsections, we can get a better looking plot
that is good enough for publication as shown in Fig. 6.13.
It is safe to say that the appearance of Fig. 6.13 is better than
Fig. 6.12 in many ways. In particular, the plot now has axis labels
for variables along with their units, readable fonts with a reasonable
size, standard colors, clear tick marks for both the major and minor
336 | Productivity Tools

Figure 6.13 An improved, polished version of Fig. 6.12.

ticks, and also well‐placed annotations or legends to distinguish two


plots of different samples. Note that to clearly distinguish the two
plots, if we use the only color, it would be troublesome for black‐
white printing or for people who have difficulties in recognizing
colors. We should use both different colors and line styles at the same
time, such as “blue‐solid line” and “red‐dashed line”. All such details
can be precisely managed through some Matplotlib settings that will
be discussed below.

6.4.2 Changing general plot parameters

There are at least three general parameters that we need to set at the
beginning of the Python plotting script: (1) font, (2) font size, and (3)
axis line width. These three are essentially global parameters defined
at the beginning of the Python script that we do not change later, i.e.,
we do not have to explicitly set font/sizes for each label down the
line). We can add the following code before we generate any figures:

QE‑SSP/scripts/[Link]

1 # Edit the font , font size , and axes width


2 [Link]['[Link] '] = 'Arial '
Quantum ESPRESSO Course for Solid‐State Physics | 337
3 [Link]['[Link] '] = 22
4 [Link]['[Link] '] = 2

This block of code is typically put after importing packages.


You may change the font with other fonts you like, but it is
recommended to use the “Sans” font family, such as “Helvetica”,
“Arial”, and “Verdana”. The “Sans” font family is suitable for reading
the font clearly even when we reduce the size of the graph. If you
are not sure whether or not the font is available, you can type the
following command in Python and see the output that lists all the font
names available to Matplotlib in your computer.

QE‑SSP/scripts/[Link]

1 import matplotlib.font_manager as fm
2
3 # Collect all the available fonts
4 font_names = [[Link] for f in [Link]
]
5 print(font_names)

6.4.3 Setting axes and ticks

We should not forget to add a label to each axis. The tick widths and
lengths should be edited to match our axis parameters. If we have
minor ticks, we can also specify by a parameter which='mirror'.

QE‑SSP/scripts/[Link]

1 # Add the x and y-axis labels


2 ax.set_xlabel(r'Frequency (cm$ ^{-1}$)')
3 ax.set_ylabel(r'Phonon DOS (state/cm$ ^{-1}$/unit -
cell)')
4 # Set the axis limits
5 ax.set_xlim (0, 1700)
6 ax.set_ylim (0, 0.025)
7 # Edit the major and minor ticks
8 [Link].set_tick_params(which='major ', size =10,
9 width=2, direction='in', top='on')
10 [Link].set_tick_params(which='minor ', size=6,
11 width=2, direction='in', top='on')
12 [Link].set_tick_params(which='major ', size =10,
338 | Productivity Tools

13 width=2, direction='in', right='on')


14 [Link].set_tick_params(which='minor ', size=6,
15 width=2, direction='in', right='on')
16 # Edit the major and minor ticks of x and y axes
17 [Link]. set_major_locator([Link].
MultipleLocator (500))
18 [Link]. set_minor_locator([Link].
MultipleLocator (100))
19 [Link]. set_major_locator([Link].
MultipleLocator (0.01))
20 [Link]. set_minor_locator([Link].
MultipleLocator (0.002))

Note that Matplotlib can accept special mathematical formatting


from the LaTeX such as cm$^{-1}$.

6.4.4 Annotations and saving the plots

After all basic settings are ready, we can plot the dataset and give
annotations or legends to the plots.

QE‑SSP/scripts/[Link]

1 # Plot our data


2 [Link](omega , ph_tot , linewidth =2, color='blue ',
linestyle='solid ')
3 [Link](omega , ph_c1 , linewidth =2, color='red',
linestyle='dashed ')
4
5 # Add annotate
6 [Link]('Total ', color='blue ',
7 xy =(1390 , 0.015) , xycoords='data ',
8 xytext =(1000 , 0.018) , textcoords='data ',
9 arrowprops=dict(arrowstyle="->", color='
blue ')
10 )
11 [Link]('C atom ', color='red',
12 xy =(1300 , 0.002) , xycoords='data ',
13 xytext =(950 , 0.01) , textcoords='data ',
14 arrowprops=dict(arrowstyle="->", color='
red')
15 )
16
17 # Save and show figure
18 [Link]('plot -[Link]', bbox_inches='tight ')
Quantum ESPRESSO Course for Solid‐State Physics | 339
19 [Link] ()

At the very last, do not forget to save the plot. The PDF format
is recommended because it is a vector image that will not lose the
resolution.

6.4.5 Creating and using your Matplotlib style

When the readers want to plot many figures with the same style,
making your Matplotlib style is helpful to save time. You do not
need to copy/paste settings in Matplotlib whenever you create a new
figure. By importing a style file, you can ensure consistency while still
maintaining the ability to override settings as you wish within the
individual scripts.
The readers can find a style file for this book at
QE-SSP/matplotlib/[Link]. The [Link] file
can open and edit by any text editor for example, nano:

$ nano [Link]

QE‑SSP/matplotlib/[Link]

1 # Figure properties
2 [Link] : 6.5, 5
3
4 # Font properties
5 [Link] : Arial
6 [Link] : 22
7
8 #### LaTeX
9 [Link] : regular
10
11 # Axes properties
12 [Link] : medium # fontsize of the axes
title
13 [Link] : 10 # pad between axes and
title in points
14 [Link] : normal # font weight for axes
title
15 [Link] : 2 # edge linewidth
340 | Productivity Tools

16 [Link] : 10 # space between label


and axis
17 axes.prop_cycle : cycler(color =['1f77b4 ', 'd62728
', '2ca02c ', 'ff7f0e ', '9467 bd', '8c564b ', '
e377c2 ', '7f7f7f ', 'bcbd22 ', '17 becf '])
18
19 # Tick properties
20 # x-axis
21 [Link] : True
22 [Link] : in
23 [Link] : 9
24 [Link] : 2
25 [Link] : 4
26 [Link]: True
27 [Link] : 6
28 [Link] : 2
29 [Link] : 4
30
31 # y-axis
32 [Link] : True
33 [Link] : in
34 [Link] : 9
35 [Link] : 2
36 [Link] : 4
37 [Link]: True
38 [Link] : 6
39 [Link] : 2
40 [Link] : 4
41
42 # Line properties
43 [Link] : 2
44 [Link] : 10
45
46 # Legend properties
47 [Link] : 1
48 [Link] : False
49 [Link] : 19
50
51 # Increase the default DPI , and change the file type
from png to pdf
52 [Link] : 600 # figure dots per inch
53 [Link] : pdf # png , ps , pdf , svg
54 [Link] : tight # {tight , standard}
55 [Link]: True # transparent background

The readers can modify the file freely. The details for each setting
can be found at [Link]
Quantum ESPRESSO Course for Solid‐State Physics | 341

Figure 6.14 Energy dispersion (left) and density of state (DOS) (right) of
GeTe.

introductory/[Link]. Now, when we want to use this


style, we need to add the following line in our Python script:

[Link]('~/QE -SSP/matplotlib/[Link] ')

It is important to note that the directory path of [Link] must


be correct.

6.4.6 Plotting DOS and energy dispersion

With the knowledge of Python and Matplotlib we have obtained


before, we can create our own script for plotting DOS and band
structure of Quantum ESPRESSO calculation outputs. We show an
example for GeTe in Fig. 6.14 with the following [Link]
script.

QE‑SSP/gete/plot‑[Link]

1 # Import the necessary packages and modules


2 import [Link] as plt
3 [Link]('../ matplotlib/[Link] ')
4 import numpy as np
5
6 # Load data from [Link]
7 data = [Link]('[Link]')
342 | Productivity Tools

8 k = [Link](data[:, 0])
9 bands = [Link](data[:, 1], (-1, len(k)))
10 # Load data from [Link]
11 ener , dos , idos = [Link]('[Link]', unpack=True
)
12
13 # Fermi energy from ./out /*. xml :
14 HOMO = 2.635710373417968e-1
15 LUMO = 2.758546355831248e-1
16 unitE = 27.2114
17 E_F = (HOMO+LUMO)* unitE /2
18
19 # Set high -symmetry points from *. [Link] file
20 rW = k[0]; rL = k[50]; rU = k[80]; rX = k[100]; rG =
k[150]; rK = k[200]
21
22 # Create figure object
23 fig = [Link](figsize =(6, 3))
24 # Add x and y-axes
25 axE = fig.add_axes ([0.00 , 0.0, 0.60, 1])
26 axD = fig.add_axes ([0.70 , 0.0, 0.30, 1])
27
28 # Plot band structure
29 for band in range(len(bands)):
30 [Link](k, bands[band , :]-E_F , c='b')
31 # Plot dotted line at Fermi energy
32 [Link] (0, c='gray ', ls=':')
33 # Plot dotted lines at high -symmetry points
34 [Link](rL , c='gray ')
35 [Link](rU , c='gray ')
36 [Link](rX , c='gray ')
37 [Link](rG , c='gray ')
38 # Add labels for high -symmetry points
39 axE.set_xticks ([rW , rL , rU , rX , rG , rK])
40 axE. set_xticklabels (['W', 'L', 'U', 'X', '$\Gamma$ ',
'K'])
41 # Hide x-axis minor ticks
42 axE.tick_params(axis='x', which='both ', length =0)
43 # Set the axis limits
44 axE.set_xlabel('')
45 axE.set_ylabel('Energy (eV)')
46 # Set the axis limits
47 axE.set_xlim(rW , rK)
48 axE.set_ylim (-2.2, 2.2)
49
50 # Plot the DOS
51 [Link](dos , ener -E_F , c='r')
52 # Set the axis limits
53 axD.set_xlim (0, 2)
Quantum ESPRESSO Course for Solid‐State Physics | 343
54 axD.set_ylim (-2.2, 2.2)
55 # Add the x label
56 axD.set_xlabel('DOS (state/eV/u.c.)')
57
58 # Save figure to the pdf file
59 [Link]('[Link]')
60 # Show figure
61 [Link] ()

You might also like