Skip to content

Commit

Permalink
Merge branch 'dopey_fermi' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsquires authored Oct 31, 2023
2 parents 4052cb6 + 9ee645e commit 9cb2c6f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 29 deletions.
3 changes: 2 additions & 1 deletion docs/Tutorials.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ tutorials:
YT_defects_tutorial
dope_workflow_example
dope_parsing_example
dope_chemical_potentials
dope_chemical_potentials
interface_py_sc_fermi
1 change: 1 addition & 0 deletions docs/interface_py_sc_fermi.ipynb
55 changes: 33 additions & 22 deletions doped/interface/py_sc_fermi.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
from dataclasses import dataclass, field
from doped.utils.legacy_pmg.thermodynamics import DefectPhaseDiagram
from copy import deepcopy
from typing import List, Dict, Union, Tuple, Optional
from scipy.spatial import Delaunay, ConvexHull
from tqdm import tqdm
import pandas as pd
from dataclasses import dataclass, field
from typing import Dict, List, Optional

import numpy as np
import pandas as pd
from scipy.spatial import ConvexHull, Delaunay
from tqdm import tqdm

from doped.utils.legacy_pmg.thermodynamics import DefectPhaseDiagram

try:
from py_sc_fermi.dos import DOS
from py_sc_fermi.defect_system import DefectSystem
from py_sc_fermi.defect_species import DefectSpecies
from py_sc_fermi.defect_system import DefectSystem
from py_sc_fermi.dos import DOS
except ImportError:
raise ImportError(
"Please install py-sc-fermi via `pip install py-sc-fermi` to use this functionality."
)


def _get_label_and_charge(name: str) -> tuple:
"""Extracts the label and charge from a defect name string.
"""
Extracts the label and charge from a defect name string.
Args:
name (str): Name of the defect.
Expand Down Expand Up @@ -58,7 +61,9 @@ def _defect_picker(self, name: str): # Type hint should represent the actual ty
return defect

def _generate_defect_system(self, temperature: float, chemical_potentials: dict) -> DefectSystem:
"""Generates a DefectSystem object from the DefectPhaseDiagram and a set of chemical potentials.
"""
Generates a DefectSystem object from the DefectPhaseDiagram and a set
of chemical potentials.
Args:
temperature (float): Temperature in K.
Expand Down Expand Up @@ -97,8 +102,10 @@ def _generate_defect_system(self, temperature: float, chemical_potentials: dict)
def defect_system_from_chemical_potentials(
self, chemical_potentials: dict, temperature: float = 300.0
) -> DefectSystem:
"""Updates the energies of the DefectSystem with a new set of chemical potentials."""

"""
Updates the energies of the DefectSystem with a new set of chemical
potentials.
"""
defect_system = self._generate_defect_system(
temperature=temperature, chemical_potentials=chemical_potentials
)
Expand All @@ -121,7 +128,8 @@ def scan_temperature(
file_name: str = None,
) -> pd.DataFrame:
"""
Scans a range of temperatures and saves the concentration_dict() to a DataFrame.
Scans a range of temperatures and returns the concentration_dict() as a
DataFrame.
Args:
temp_range (List[float]): List of temperatures to scan.
Expand Down Expand Up @@ -175,15 +183,15 @@ def scan_temperature(
return all_data

def _get_concentrations(self, defect_system: DefectSystem):
"""_summary_
"""
_summary_.
Args:
defect_system (DefectSystem): _description_
Returns:
_type_: _description_
"""

conc_dict = defect_system.concentration_dict()

concentration_data = []
Expand Down Expand Up @@ -267,6 +275,9 @@ def interpolate_chemical_potentials(
annealing_temperature=anneal_temperature,
target_temperature=temperature,
fix_defect_species=fix_defect_species,
annealing_temperature=anneal_temp,
target_temperature=temp,
level=level,
exceptions=exceptions,
)

Expand Down Expand Up @@ -349,14 +360,15 @@ def generate_annealed_defect_system(
elif fix_defect_species == False:
for k, v in defect_species.charge_states.items():
key = f"{defect_species.name}_{int(k)}"
if key in [k for k in fixed_concs.keys()]:
if key in list(fixed_concs.keys()):
v.fix_concentration(fixed_concs[key] / 1e24 * self.volume)

target_system = self.update_defect_system_temperature(initial_system, target_temperature)
return target_system

def chempot_grid(self, chemical_potentials, num_points=10, num_points_along_edge=5):
"""generate a grid of chemical potentials.
"""
Generate a grid of chemical potentials.
Args:
chemical_potenials ([type]): [description]
Expand Down Expand Up @@ -401,8 +413,8 @@ def grid_solve(
"""
all_concentration_data = []
all_fermi_level_data = []
for i, row in tqdm(chempot_grid.iterrows()):
row.drop(["is_vertex", "facet"], inplace=True)
for _i, row in tqdm(chempot_grid.iterrows()):
row = row.drop(["is_vertex", "facet"])
chemical_potentials = row.to_dict()

if anneal_temperature:
Expand Down Expand Up @@ -431,15 +443,14 @@ def grid_solve(
all_data = pd.merge(
concentration_df,
fermi_level_df,
on=["Temperature"] + list(chemical_potentials.keys()),
on=["Temperature", *list(chemical_potentials.keys())],
)

if file_name is not None:
all_data.to_csv(file_name)

return all_data


@dataclass
class ChemicalPotentialGrid:
chemical_potentials: dict
Expand Down Expand Up @@ -535,7 +546,7 @@ def get_grid(self) -> pd.DataFrame:

df = pd.concat([grid_df, internal_grid_df, vertices_df], ignore_index=True)
grid_df = df.drop_duplicates(
subset=list(list(self.chemical_potentials["facets"].values())[0].keys()) + ["is_vertex"]
subset=[*list(list(self.chemical_potentials["facets"].values())[0].keys()), "is_vertex"]
)

return grid_df
12 changes: 6 additions & 6 deletions examples/interface_py_sc_fermi.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@
"# define a range of chemical potentials to scan over\n",
"chem_pot_df = fs.interpolate_chemical_potentials_and_save(chem_pot_start=chemical_potentials[\"facets\"][\"Cu2SiSe3-Cu2Se-Cu\"],\n",
" chem_pot_end=chemical_potentials[\"facets\"][\"Cu2SiSe3-SiSe2-Se\"],\n",
" n_points=100, temp=300, anneal_temp=1000)\n",
" n_points=100, temp=300)\n",
"\n",
"# Create a unique list of defects\n",
"unique_defects = chem_pot_df['Defect'].unique()\n",
Expand Down Expand Up @@ -1679,23 +1679,23 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 1,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'sliced_df' is not defined",
"evalue": "name 'concs' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m/Users/alex/work/doped/examples/interface_py_sc_fermi.ipynb Cell 20\u001b[0m line \u001b[0;36m1\n\u001b[0;32m----> <a href='vscode-notebook-cell:/Users/alex/work/doped/examples/interface_py_sc_fermi.ipynb#X25sZmlsZQ%3D%3D?line=0'>1</a>\u001b[0m \u001b[39mprint\u001b[39m(sliced_df)\n",
"\u001b[0;31mNameError\u001b[0m: name 'sliced_df' is not defined"
"\u001b[1;32m/Users/alex/work/doped/examples/interface_py_sc_fermi.ipynb Cell 20\u001b[0m line \u001b[0;36m1\n\u001b[0;32m----> <a href='vscode-notebook-cell:/Users/alex/work/doped/examples/interface_py_sc_fermi.ipynb#X25sZmlsZQ%3D%3D?line=0'>1</a>\u001b[0m \u001b[39mprint\u001b[39m(concs)\n",
"\u001b[0;31mNameError\u001b[0m: name 'concs' is not defined"
]
}
],
"source": [
"print(sliced_df)"
"print(concs)"
]
},
{
Expand Down

0 comments on commit 9cb2c6f

Please sign in to comment.