Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ph #121

Merged
merged 5 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions calphy/phase_diagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import matplotlib.pyplot as plt
import warnings
import itertools
import math

from calphy.integrators import kb

Expand All @@ -17,14 +18,30 @@


def _get_temp_arg(tarr, temp, threshold=1E-1):
if tarr is None:
return None
arg = np.argsort(np.abs(tarr-temp))[0]

th = np.abs(tarr-temp)[arg]
if th > threshold:
arg = None
return arg

def _is_val_ok(val):
if val is None:
return False
elif math.isnan(val):
return False
else:
return True

def _get_fe_at_args(arr, args):
fes = [arr[count][x] for count, x in enumerate(args)]
fes = []
for count, x in enumerate(args):
if _is_val_ok(x):
fes.append(arr[count][int(x)])
else:
fes.append(None)
return fes

def _calculate_configurational_entropy(x, correction=0):
Expand Down Expand Up @@ -125,13 +142,15 @@ def get_phase_free_energy(df, phase, temp,
To be added
"""
df_phase = df.loc[df['phase']==phase]
#drop Nones
df_phase = df_phase.sort_values(by="composition")
df_phase = df_phase[(df_phase['composition'] >= composition_interval[0]) & (df_phase['composition'] <= composition_interval[1])]

composition = df_phase['composition'].values
args = df_phase["temperature"].apply(_get_temp_arg, args=(temp,))
fes = _get_fe_at_args(df["free_energy"], args)
fes = _get_fe_at_args(df_phase["free_energy"].values, args)

#print(fes)
#filter out None values
composition = np.array([composition[count] for count, x in enumerate(fes) if x is not None])
fes = np.array([x for x in fes if x is not None])
Expand All @@ -147,7 +166,7 @@ def get_phase_free_energy(df, phase, temp,
entropy_term = []

fe_fit = _get_free_energy_fit(composition, fes, fit_order=fit_order)
compfine = np.linspace(np.min(comp), np.max(comp), composition_grid)
compfine = np.linspace(np.min(composition), np.max(composition), composition_grid)

#now fit on the comp grid again
fe = np.polyval(fe_fit, compfine)
Expand All @@ -158,7 +177,7 @@ def get_phase_free_energy(df, phase, temp,
fe[filters] = reset_value

if plot:
plt.scatter(comp, fes, s=4, label=f'{phase}-calc.', color="#e57373")
plt.scatter(composition, fes, s=4, label=f'{phase}-calc.', color="#e57373")
plt.plot(compfine, fe, label=f'{phase}-fit', color="#b71c1c")
plt.xlabel("x")
plt.ylabel("F (eV/atom)")
Expand All @@ -176,6 +195,8 @@ def get_free_energy_mixing(dict_list, threshold=1E-3):
Get free energy of mixing by subtracting end member values.
End members are chosen automatically.
"""
dict_list = np.atleast_1d(dict_list)

#we have to get min_comp from all possible values
min_comp = np.min([np.min(d["composition"]) for d in dict_list])
max_comp = np.max([np.max(d["composition"]) for d in dict_list])
Expand Down
79 changes: 56 additions & 23 deletions calphy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,32 +43,65 @@ def create_job_from_inputfile(pr, inputfile, potential, kernel=None):
if os.path.exists(basedir_path):
#create job and copy files
try:
job = pr.create.job.Calphy(basedir.replace('-', '_'))
job._job_id = pr.db.add_item_dict(job.db_entry())
job.refresh_job_status()
shutil.copytree(basedir_path, job.working_directory, dirs_exist_ok=True)
#make sure that the report file exists
reportfile = os.path.join(basedir_path, 'report.yaml')
if os.path.exists(reportfile):
job = pr.create.job.Calphy(basedir.replace('-', '_'))
job._job_id = pr.db.add_item_dict(job.db_entry())
job.refresh_job_status()
shutil.copytree(basedir_path, job.working_directory, dirs_exist_ok=True)

#read in structure, assign potential
Z_of_type = dict([(count+1, calc._element_dict[element]['atomic_number']) for count, element in enumerate(calc.element)])
structure = read(calc.lattice, format='lammps-data', style='atomic', Z_of_type=Z_of_type)
job.structure = ase_to_pyiron(structure)
job.potential = potential
pr.db.item_update({"ChemicalFormula": job.structure.get_chemical_formula()}, job._job_id)
#read in structure, assign potential
Z_of_type = dict([(count+1, calc._element_dict[element]['atomic_number']) for count, element in enumerate(calc.element)])
structure = read(calc.lattice, format='lammps-data', style='atomic', Z_of_type=Z_of_type)
job.structure = ase_to_pyiron(structure)
job.potential = potential
pr.db.item_update({"ChemicalFormula": job.structure.get_chemical_formula()}, job._job_id)

#collect output
job.input.mode = calc.mode
job.status.collect = True
job.collect_output()
#collect output
job.input.mode = calc.mode
job.status.collect = True
job.collect_output()

#populate inputs
calcdict = calc.model_dump()
#temporary fix for comp scaling until its introduced in pyiron
del calcdict['composition_scaling']
job.input.update(calcdict)
job._create_calc()
job.to_hdf()
job.status.finished = True
#populate inputs
calcdict = calc.model_dump()
#temporary fix for comp scaling until its introduced in pyiron
del calcdict['composition_scaling']
job.input.update(calcdict)
job._create_calc()
job.to_hdf()
job.status.finished = True
else:
print(f'parsing {basedir_path} failed, skipping')
except:
#delete job
pr.remove_job(basedir.replace('-', '_'))
print(f'parsing {basedir_path} failed, skipping')
else:
print(f'could not find {basedir_path}, skipping')
print(f'could not find {basedir_path}, skipping')


def get_energy_free(job):
return job["output/energy_free"]

def get_temperature(job):
return job["output/temperature"]

def get_phase(job):
raw = job.name.split('_')
if raw[-3] == 'liquid':
phase = 'liquid'
else:
phase = raw[1]
return phase

def get_composition(job):
chem = job.project.db.get_item_by_id(job.id)['chemicalformula']
comp_split = re.split('(\d+)', chem)[:-1]
if len(comp_split) == 2:
if comp_split[0] == 'Al':
return 0.00
else:
return 1.00
else:
return int(comp_split[3])/(int(comp_split[1])+int(comp_split[3]))
Loading