First-principles calculations for free using Google Colaboratory

Recently, for those who cannot experiment with the new corona, "Introduction to first-principles calculation that can be done for free for physical property experimenters" https://cometscome.github.io/DFT/build/ In the process, I found out how to do first-principles calculations only with Google Colab, so I would like to share it here as well.

Things necessary

--Browser --Google account

Software to use

--Quantum Espresso: First Principle Calculation Software --Atomic Simulation Environment (ASE): Python library for atomic and molecular simulations https://qiita.com/cometscome_phys/items/4af134de6d959a7718b9

Install Quantum Espresso

In order to use Quantum Espresso with Google Colab, I need to compile Qunatum Espresso from source, but I found that it is possible in the following way. The point is to have FFTW3 installed.

!git clone https://github.com/QEF/q-e.git
!apt-get install -y libfftw3-3 libfftw3-dev libfftw3-doc
%cd q-e
!DFLAGS='-D__OPENMP -D__FFTW3 -D__MPI -D__SCALAPACK' FFT_LIBS='-lfftw3'  ./configure --enable-openmp

I'm downloading the source, installing FFTW3, and preparing to compile. next,

!make pw

You can run pw.x, the core code of Quantum Espresso. The rest is a post-process tool, pp:

make pp

Let's also install.

Now, Build OSS with Google colaboratory As you can see in, the binaries compiled here will disappear after 12 hours. So

from google.colab import drive
drive.mount('/content/drive')

Run to save the binaries to your Google Drive. The next time you use it, unzip it and use it.

%cd /content/
!zip -r /content/drive/'My Drive'/q-e.zip q-e 

Save q-e.zip as.

Set environment variables so that you can run Quantum Espresso. In other words

import os
os.environ['PATH'] = "/content/q-e/bin:"+os.environ['PATH']

will do.

ASE installation

ASE is easy to install,

!pip install ase

Enter with.

First-principles calculation test

Now, let's actually do first-principles calculations.

Structural optimization of NaCl

Create and move the NaCl directory.

%cd /content
!mkdir NaCl
%cd NaCl

Then download the pseudopotential.

!wget https://www.quantum-espresso.org/upf_files/Na.pbesol-spn-kjpaw_psl.1.0.0.UPF
!wget https://www.quantum-espresso.org/upf_files/Cl.pbesol-n-kjpaw_psl.1.0.0.UPF

It went into the NaCl directory.

next,

from ase.build import bulk
from ase.calculators.espresso import Espresso
from ase.constraints import UnitCellFilter
from ase.optimize import LBFGS
import ase.io 

pseudopotentials = {'Na': 'Na.pbesol-spn-kjpaw_psl.1.0.0.UPF',
                    'Cl': 'Cl.pbesol-n-kjpaw_psl.1.0.0.UPF'}  
rocksalt = bulk('NaCl', crystalstructure='rocksalt', a=6.0)
calc = Espresso(pseudopotentials=pseudopotentials,pseudo_dir = './',
                tstress=True, tprnfor=True, kpts=(3, 3, 3))

rocksalt.set_calculator(calc)

ucf = UnitCellFilter(rocksalt)
opt = LBFGS(ucf)
opt.run(fmax=0.005)

# cubic lattic constant
print((8*rocksalt.get_volume()/len(rocksalt))**(1.0/3.0))

You can optimize the structure of NaCl by executing. In this code, the location of the pseudopotential is specified by `` `pseudo_dir```. This time it's the directory you're in now.

Cu band diagram

Next, let's calculate the Cu band diagram.

Create a Cu directory.

%cd /content
!mkdir Cu
%cd Cu

Download the pseudopotential.

!wget https://www.quantum-espresso.org/upf_files/Cu.pz-d-rrkjus.UPF

Perform a self-consistent calculation to determine the electron density. Once the electron density is determined, a band diagram can be drawn by calculating at each k point.

from ase import Atoms
from ase.build import bulk
from ase.calculators.espresso import Espresso
atoms = bulk("Cu")
pseudopotentials = {'Cu':'Cu.pz-d-rrkjus.UPF'}

input_data = {
    'system': {
        'ecutwfc': 30,
        'ecutrho': 240,
        'nbnd' : 35,
    'occupations' : 'smearing',
        'smearing':'gauss',
        'degauss' : 0.01},
    'disk_io': 'low'}  # automatically put into 'control'

calc = Espresso(pseudopotentials=pseudopotentials,kpts=(4, 4, 4),input_data=input_data,pseudo_dir = './')
atoms.set_calculator(calc)

atoms.get_potential_energy()
fermi_level = calc.get_fermi_level()
print(fermi_level)

Then do the calculations for the band diagram.

input_data.update({'calculation':'bands',
                              'restart_mode':'restart',
                               'verbosity':'high'})
calc.set(kpts={'path':'GXWLGK', 'npoints':100},
          input_data=input_data)
calc.calculate(atoms)

Here, the interesting point of ASE is that you can easily draw a band diagram by putting your favorite Brillouin zone points in the path of kpts.

Finally, calculate the band diagram.

import matplotlib.pyplot as plt

bs = calc.band_structure()
bs.reference = fermi_level
bs.plot(emax=40,emin=5)
Cu.png

You can now perform first-principles calculations with just your browser.

Resume calculation

If you want to restart the calculation on another day, you can do it as follows.

from google.colab import drive
drive.mount('/content/drive')
!cp /content/drive/'My Drive'/q-e.zip ./
!unzip q-e.zip
import os
os.environ['PATH'] = "/content/q-e/bin:"+os.environ['PATH']
!pip install ase

After that, you can create a directory and download the pseudopotential to it.

!mkdir NaCl
%cd NaCl
!wget https://www.quantum-espresso.org/upf_files/Na.pbe-spn-kjpaw_psl.1.0.0.UPF
!wget https://www.quantum-espresso.org/upf_files/Cl.pbe-n-rrkjus_psl.1.0.0.UPF

Recommended Posts

First-principles calculations for free using Google Colaboratory
100 language processing knock 2020 "for Google Colaboratory"
Google colaboratory
[Beginner] Python web scraping using Google Colaboratory
Usual processing notes when using Google Colaboratory
Using Java's Jupyter Kernel with Google Colaboratory
Free Python runtime environment Google Colaboratory memo
■ Kaggle Practice for Beginners -House Sale Price (I tried using PyCaret)-by Google Colaboratory
Where to fix code when using plotly for free
How to analyze with Google Colaboratory using Kaggle API
Let's easily make a math gif using Google Colaboratory
Google Colaboratory setup summary
Is it Google Colaboratory?
Access google spreadsheet using python on raspberry pi (for myself)
■ Kaggle Practice for Beginners --Introduction of Python --by Google Colaboratory
[Python] The biggest weakness / disadvantage of Google Colaboratory [For beginners]
Try to implement linear regression using Pytorch with Google Colaboratory