Commit 9822a160 authored by davidwaroquiers's avatar davidwaroquiers
Browse files

Added relaxation and phonon workflow.

parent a05b6c24
%% Cell type:markdown id:4212729e tags:
# Abiflows : relaxation workflow
In this exercise, you will use abiflows to perform the relaxation of bulk silicon.
First, import all the necessary modules and objects:
%% Cell type:code id:64a3318e tags:
``` python
from fireworks import FWorker
import os
import pseudo_dojo
from abiflows.fireworks.workflows.abinit_workflows import RelaxFWWorkflow
from abiflows.database.mongoengine.utils import DatabaseData
from pymatgen.ext.matproj import MPRester
from pymatgen.core.structure import Structure
from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
import abipy.data as abidata
from jupyter_jsmol.pymatgen import quick_view
from abiflows.database.mongoengine.abinit_results import RelaxResult
```
%% Cell type:markdown id:1b473a01 tags:
### Pseudo-potentials
We will use the pseudo potentials from the Pseudo-Dojo. These pseudopotentials have been extensively tested and assessed with respect to various properties and have standard cut-offs defined. If you want to use some other kind of pseudos you will need to provide explicitly the cutoff for the calculation.
%% Cell type:code id:e91b727e tags:
``` python
pseudo_table = pseudo_dojo.OfficialDojoTable.from_djson_file(
os.path.join(pseudo_dojo.dojotable_absdir("ONCVPSP-PBE-PDv0.4"), 'standard.djson'))
pseudo_path = pseudo_dojo.dojotable_absdir("ONCVPSP-PBE-PDv0.4")
```
%% Cell type:markdown id:5705a888 tags:
### Define the database for the results
It can be the same database used for fireworks with other collections or a different one
%% Cell type:code id:34ca1675 tags:
``` python
db = DatabaseData(host='mongo', port=27017, collection='relax',
database='fireworks', username=None, password=None)
```
%% Cell type:markdown id:c7183ce3 tags:
### Structure
Get the structure (from abipy's standard set of structures). You can also get it from the Materials Project if you set up your MP api key. mp-149 is the id for silicon in the Materials Project.
%% Cell type:code id:36eca101 tags:
``` python
mp_id = 'mp-149'
#structure = MPRester().get_structure_by_material_id(mp_id)
structure = abidata.structure_from_cif("si.cif")
spga = SpacegroupAnalyzer(structure)
structure = spga.get_primitive_standard_structure()
```
%% Cell type:markdown id:e2191732 tags:
You can easily view the structure using:
%% Cell type:code id:702bad75 tags:
``` python
quick_view(structure)
```
%% Cell type:markdown id:9ea4ee68 tags:
### Parameters of the calculation
Check that the pseudo-potentials are indeed available for the structure:
%% Cell type:code id:92488756 tags:
``` python
try:
pseudos = pseudo_table.get_pseudos_for_structure(structure)
print("pseudos found")
except BaseException as e:
print("no pseudo")
exit(1)
```
%% Cell type:markdown id:8334a064 tags:
Define the k-point density:
%% Cell type:code id:4f6761ee tags:
``` python
kppa = 500
```
%% Cell type:markdown id:da5ab529 tags:
Add some initialization info to be stored in the results database
%% Cell type:code id:35b61f29 tags:
``` python
initialization_info = dict(kppa=kppa, mp_id=mp_id)
```
%% Cell type:markdown id:32d67a01 tags:
Tolerance for the relaxation and additional parameters for abinit:
%% Cell type:code id:f2de7117 tags:
``` python
tolmxf = 1e-5
extra_abivars = dict(tolmxf=tolmxf, ionmov=2, chksymbreak=1, ntime=30, nstep=100)
```
%% Cell type:markdown id:f154b0e9 tags:
### Workflow
Create the relaxation workflow. This will create a fireworks workflow object (still not added to fireworks database) check the function for the different options available. The OneSymmetric option will set a single shift that respects the symmetry of the crystal. The target_dilatmx means that the dilatmx parameter will be automatically progressively reduced and relaxation restarted until the desired value has been used.
%% Cell type:code id:31506eba tags:
``` python
gen = RelaxFWWorkflow.from_factory(structure, pseudo_table, kppa=kppa, spin_mode="unpolarized", extra_abivars=extra_abivars,
autoparal=False, initialization_info=initialization_info, target_dilatmx=1.01,
smearing=None, shift_mode='OneSymmetric', ecut=5)
```
%% Cell type:markdown id:eda7b2b5 tags:
Add the insertion in the results database as well as the clean up of the files that are not needed after execution:
%% Cell type:code id:710a7097 tags:
``` python
gen.add_mongoengine_db_insertion(db)
gen.add_final_cleanup(["WFK", "1WF", "DEN", "WFQ", "DDB"])
```
%% Cell type:markdown id:812b079d tags:
Add the workflow to the database. It will use the fireworks LaunchPad that has been set by default. If a different one should be used it can be passed as an argument.
%% Cell type:code id:6b5ab2f6 tags:
``` python
fw_id_maps = gen.add_to_db()
print("{} submitted".format(mp_id))
```
%% Cell type:markdown id:7a20313c tags:
Now your workflow is ready to be executed. Look at the Fireworks webgui to see your workflow diagrammatically. Use the terminal to launch fireworks using the rapidfire mode. You can use the --nlaunches=1 option to see what is happening one step at a time.
%% Cell type:markdown id:1f59514b tags:
### Relaxed structure
You can get the relaxed structure directly from the database
%% Cell type:code id:618c4316 tags:
``` python
db.connect_mongoengine()
with db.switch_collection(RelaxResult) as RelaxResult:
relaxed_results = RelaxResult.objects(mp_id="mp-149")
relaxed = relaxed_results[0]
# load the relaxed Structure
relaxed_structure = Structure.from_dict(relaxed.abinit_output.structure)
```
%% Cell type:code id:98a37be1 tags:
``` python
print(f"Volume of the initial structure: {structure.volume}")
print(f"Volume of the relaxed structure: {relaxed_structure.volume}")
```
%% Cell type:code id:4b27611f tags:
``` python
```
%% Cell type:markdown id:d991bd8e tags:
# Abiflows : phonon workflow
In this exercise, you will use abiflows to perform a phonon calculation for bulk silicon. You will use the relaxed structure obtained in the previous exercise.
First, import all the necessary modules and objects:
%% Cell type:code id:9f484568 tags:
``` python
from abipy.abilab import Structure
from abiflows.fireworks.workflows.abinit_workflows import DfptFWWorkflow
from abiflows.database.mongoengine.utils import DatabaseData
from abiflows.database.mongoengine.abinit_results import RelaxResult, DfptResult
```
%% Cell type:markdown id:9aa33efb tags:
Define where to take the relaxed structure from:
%% Cell type:code id:8ef86d8a tags:
``` python
source_db = DatabaseData(host='mongo', port=27017, collection='relax',
database='fireworks', username=None, password=None)
source_db.connect_mongoengine()
```
%% Cell type:markdown id:42082197 tags:
Define the database for the phonon results:
%% Cell type:code id:b6ea6a04 tags:
``` python
db = DatabaseData(host='mongo', port=27017, collection='phonon',
database='fireworks', username=None, password=None)
```
%% Cell type:markdown id:a66dc661 tags:
### Get the relaxed structure
The context manager is required to use the collection name selected in source_db.
By default mongoengine uses the name of the class (in this case RelaxResult) as name of the collection to query.
%% Cell type:code id:c0b65632 tags:
``` python
with source_db.switch_collection(RelaxResult) as RelaxResult:
mp_id = 'mp-149'
# download from the database the relaxed structure
# This relies on mongoengine (http://mongoengine.org/) to interact with the database.
# See the module abiflows.database.mongoengine.abinit_results for the objects used to store the results
relaxed_results = RelaxResult.objects(mp_id=mp_id)
# Assume that there is one and only one result matching the query. In real cases you might want to check this.
# At this point is an instance of a RelaxResult object
relaxed = relaxed_results[0]
structure = Structure.from_dict(relaxed.abinit_output.structure)
```
%% Cell type:markdown id:6989af46 tags:
### Parameters of the calculation
Define the k-point sampling (same as the that of the relaxation):
%% Cell type:code id:16b6f993 tags:
``` python
kppa = relaxed.abinit_input.kppa
ngkpt = relaxed.abinit_input.ngkpt
```
%% Cell type:markdown id:aac3f736 tags:
Get the input used for the relaxation to use the same approximations for the phonon calculation.
%% Cell type:code id:92e420e5 tags:
``` python
relax_input = relaxed.abinit_input.last_input.to_mgobj()
```
%% Cell type:markdown id:91c1db20 tags:
Define the q-point grid (same as the k-point grid), additional abinit parameters and initialization info:
%% Cell type:code id:8eccb32a tags:
``` python
qppa = kppa
extra_abivars = dict(chkprim=1, nstep=100, chksymbreak=1, paral_kgb=1)
initialization_info = dict(kppa=kppa, mp_id=mp_id,
relax_db=source_db.as_dict_no_credentials(), relax_id=relaxed.id,
relax_tol_val=1e-6, qppa=qppa)
```
%% Cell type:markdown id:9cea180b tags:
### Workflow
Create the phonon workflow. In this case the base is the input file of the relaxation workflow. Use the DfptFWWorkflow that allow to calculate the different kind of Dfpt perturbations with abinit in a single workflow. In this case only the phonons.
%% Cell type:code id:3975b571 tags:
``` python
gen = DfptFWWorkflow.from_gs_input(structure=structure, gs_input=relax_input,
extra_abivars=extra_abivars, autoparal=False,
initialization_info=initialization_info, do_ddk=True, do_dde=True,
ph_ngqpt=[1,1,1],
do_strain=False)
```
%% Cell type:markdown id:4ccd8749 tags:
Add the insertion in the results database as well as the clean up of the files that are not needed after execution:
%% Cell type:code id:f5a4973b tags:
``` python
gen.add_mongoengine_db_insertion(db)
gen.add_final_cleanup(["WFK", "1WF", "WFQ", "1POT", "1DEN"])
```
%% Cell type:markdown id:7d0b5376 tags:
Add the workflow to the database. It will use the fireworks LaunchPad that has been set by default. If a different one should be used it can be passed as an argument.
%% Cell type:code id:1233793d tags:
``` python
gen.add_to_db()
```
%% Cell type:markdown id:d49a88dd tags:
Now your workflow is ready to be executed. Look at the Fireworks webgui to see your workflow diagrammatically. Use the terminal to launch fireworks using the rapidfire mode. You can use the --nlaunches=1 option to see what is happening one step at a time.
%% Cell type:markdown id:e810dd52 tags:
### Results
You can now get the results of the phonon calculation.
%% Cell type:code id:b7ecba8d tags:
``` python
db.collection = "phonon"
with db.switch_collection(DfptResult) as DfptResult:
ph_results = DfptResult.objects()
ph_res = ph_results[0]
```
%% Cell type:markdown id:7e832de8 tags:
Get the ddb (derivatives database) computed and extract the phonon frequencies at Gamma:
%% Cell type:code id:8a4ee642 tags:
``` python
ddb = ph_res.abinit_output.ddb.abiopen()
phb = ddb.anaget_phmodes_at_qpoint([0,0,0])
print("Phonon frequencies at Gamma: ", phb.phfreqs)
```
%% Cell type:markdown id:293d0d73 tags:
Get the phonon band structure and density of states:
%% Cell type:code id:f23a3970 tags:
``` python
phbst, phdos = ddb.anaget_phbst_and_phdos_files(line_density=10)
```
%% Cell type:markdown id:126d1920 tags:
Plot the phonon band structure and density of states
%% Cell type:code id:fb62792f tags:
``` python
from abipy.dfpt.phonons import PhononBandsPlotter
pbp = PhononBandsPlotter()
pbp.add_phbands(label='Silicon Band structure', bands=phbst.phbands, phdos=phdos.phdos)
pbp.plot()
```
%% Cell type:markdown id:4dcd7198 tags:
### Visualizing phonon modes
Create a specific json file to be used in the following website to visualize the phonon modes:
http://henriquemiranda.github.io/phononwebsite/phonon.html. Download the file on your local machine and upload it on the following website to display the atomic displacents associated to the phonon modes.
%% Cell type:code id:88f229c4 tags:
``` python
phbst.phbands.create_phononwebsite_json("/home/jovyan/Si_phononwebsite.json")
```
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment