Skip to content

General zt plot #1

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@ dmypy.json
# Cython debug symbols
cython_debug/

# debug folder
debug/

# debug outputs
*.png
*.pdf
*.csv

# PyCharm
# JetBrains specific template is maintainted in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
Expand Down
160 changes: 92 additions & 68 deletions example/Bi2SeO2-Expt-Comparison/compare_calc_expt.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,124 +27,148 @@

import numpy as np

import sys ; sys.path.append(r"/mnt/d/Repositories/ZT-Calc-Workflow")
import sys

sys.path.append(r"/mnt/d/Repositories/ZT-Calc-Workflow")

from zt_calc_workflow.amset import read_amset_csv
from zt_calc_workflow.analysis import match_data
from zt_calc_workflow.dataset import dataset_to_2d


def read_expt_data_csv(file_path):
""" Read experimental data from a two-column CSV file. """
with open(file_path, 'r') as f:
"""Read experimental data from a two-column CSV file."""

with open(file_path, "r") as f:
f_csv = csv.reader(f)

return np.array(
[[float(val) for val in r] for r in f_csv], dtype=np.float64)

return np.array([[float(val) for val in r] for r in f_csv], dtype=np.float64)


if __name__ == "__main__":
# Load AMSET calculation and create 2D datasets.

data = read_amset_csv(r"Bi2SeO2-AMSET-n.csv")
calc_n, calc_t, calc_data_2d = dataset_to_2d(data)

# Experimental carrier concentrations (nominal).

expt_n = {'S1': 1.0e18, 'S2': 6.8e18, 'S3': 1.3e19, 'S4': 2.2e19,
'S5': 8.7e19}


expt_n = {"S1": 1.0e18, "S2": 6.8e18, "S3": 1.3e19, "S4": 2.2e19, "S5": 8.7e19}

# Load experimental data.

expt_data = {}

for s in "S1", "S2", "S3", "S4", "S5":
expt_sigma = read_expt_data_csv(r"5.0063091-2a_{0}.csv".format(s))

# Resistivity data -> sigma = 1 / rho.

expt_sigma = np.array([[t, 1. / rho] for t, rho in expt_sigma],
dtype=np.float64)


expt_sigma = np.array(
[[t, 1.0 / rho] for t, rho in expt_sigma], dtype=np.float64
)

expt_s = read_expt_data_csv(r"5.0063091-2b_{0}.csv".format(s))
expt_data[s] = {'sigma': expt_sigma, 's': expt_s}

expt_data[s] = {"sigma": expt_sigma, "s": expt_s}

# Match sigma and S for each of the five samples and print results.
for expt_k, calc_k in('sigma', 'sigma_ave'), ('s', 's_ave'):

for expt_k, calc_k in ("sigma", "sigma_ave"), ("s", "s_ave"):
for s in "S1", "S2", "S3", "S4", "S5":
# List of (n, t, val) to match to.

match_to = [(expt_n[s], t, v) for t, v in expt_data[s][expt_k]]

# Match to calculated data. Using mode='same_t' will return the
# calculated n that best match the experimental data at the
# measurement T.

res = match_data(calc_n, calc_t, calc_data_2d[calc_k],
match_to, mode='same_t', num_seeds=5)


res = match_data(
calc_n,
calc_t,
calc_data_2d[calc_k],
match_to,
mode="same_t",
num_seeds=5,
)

# Print results.

header = "Sample: '{0}', Data: '{1}'".format(s, expt_k)

print(header)
print('-' * len(header))
print("-" * len(header))
print("")

print("{0: <10} | {1: <10} | {2: <10} | {3: <10} | {4: <10} | "
"{5: <10} | {6: <10}".format("n", "T", "Expt.", "n", "T",
"Calc.", "Diff."))

print('-' * (7 * 10 + 6 * 3))


print(
"{0: <10} | {1: <10} | {2: <10} | {3: <10} | {4: <10} | "
"{5: <10} | {6: <10}".format(
"n", "T", "Expt.", "n", "T", "Calc.", "Diff."
)
)

print("-" * (7 * 10 + 6 * 3))

for (e_n, e_t, e_v), (c_n, c_t, c_v) in zip(match_to, res):
print(
"{0: >10.2e} | {1: >10.0f} | {2: >10.2f} | {3: >10.2e} | "
"{4: >10.0f} | {5: >10.2f} | {6: >10.2e}".format(
e_n, e_t, e_v, c_n, c_t, c_v, c_v - e_v))

e_n, e_t, e_v, c_n, c_t, c_v, c_v - e_v
)
)

print("")

print("")

# Try one sample with all four modes.

s = "S3"
for expt_k, calc_k in ('sigma', 'sigma_ave'), ('s', 's_ave'):
for mode in 'same', 'same_t', 'same_n', 'best_match':

for expt_k, calc_k in ("sigma", "sigma_ave"), ("s", "s_ave"):
for mode in "same", "same_t", "same_n", "best_match":
match_to = [(expt_n[s], t, v) for t, v in expt_data[s][expt_k]]

# num_seeds gives dubious results with mode = 'best_match' (and
# will issue a RuntimeWarning to this effect).

num_seeds = 1 if mode == 'best_match' else 5

res = match_data(calc_n, calc_t, calc_data_2d[calc_k],
match_to, mode=mode, num_seeds=num_seeds)

header = ("Sample: '{0}', Data: '{1}', Mode: "
"'{2}'".format(s, expt_k, mode))

num_seeds = 1 if mode == "best_match" else 5

res = match_data(
calc_n,
calc_t,
calc_data_2d[calc_k],
match_to,
mode=mode,
num_seeds=num_seeds,
)

header = "Sample: '{0}', Data: '{1}', Mode: " "'{2}'".format(
s, expt_k, mode
)

print(header)
print('-' * len(header))
print("-" * len(header))
print("")

print("{0: <10} | {1: <10} | {2: <10} | {3: <10} | {4: <10} | "
"{5: <10} | {6: <10}".format("n", "T", "Expt.", "n", "T",
"Calc.", "Diff."))

print('-' * (7 * 10 + 6 * 3))


print(
"{0: <10} | {1: <10} | {2: <10} | {3: <10} | {4: <10} | "
"{5: <10} | {6: <10}".format(
"n", "T", "Expt.", "n", "T", "Calc.", "Diff."
)
)

print("-" * (7 * 10 + 6 * 3))

for (e_n, e_t, e_v), (c_n, c_t, c_v) in zip(match_to, res):
print(
"{0: >10.2e} | {1: >10.0f} | {2: >10.2f} | {3: >10.2e} | "
"{4: >10.0f} | {5: >10.2f} | {6: >10.2e}".format(
e_n, e_t, e_v, c_n, c_t, c_v, c_v - e_v))

e_n, e_t, e_v, c_n, c_t, c_v, c_v - e_v
)
)

print("")

print("")
81 changes: 45 additions & 36 deletions example/SnS-SnSe-ZT/elec_n_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,64 +30,72 @@
# Read input data.

amset_data = {
'SnS' : (read_amset_csv("SnS-Pnma-AMSET-p.csv"),
read_amset_csv("SnS-Pnma-AMSET-n.csv")),
'SnSe' : (read_amset_csv("SnSe-Pnma-AMSET-p.csv"),
read_amset_csv("SnSe-Pnma-AMSET-n.csv"))}
"SnS": (
read_amset_csv("SnS-Pnma-AMSET-p.csv"),
read_amset_csv("SnS-Pnma-AMSET-n.csv"),
),
"SnSe": (
read_amset_csv("SnSe-Pnma-AMSET-p.csv"),
read_amset_csv("SnSe-Pnma-AMSET-n.csv"),
),
}

# Setup Matplotlib.

setup_matplotlib()

# Plot parameters.

t_plot = 700.
colours = ['b', 'r']
t_plot = 700.0

colours = ["b", "r"]

xlim = (1.e16, 1.e20)
ylim_sets = [(0., 750.), (0., 750.), (0., 6.), (0., 1.)]
xlim = (1.0e16, 1.0e20)
ylim_sets = [(0.0, 750.0), (0.0, 750.0), (0.0, 6.0), (0.0, 1.0)]

# Plot.

plt.figure(figsize=(16.2 / 2.54, 13.5 / 2.54))

subplot_axes = [plt.subplot(2, 2, i + 1) for i in range(4)]

for data_k, axes in zip(
['sigma_ave', 's_ave', 'pf_ave', 'kappa_el_ave'], subplot_axes):
["sigma_ave", "s_ave", "pf_ave", "kappa_el_ave"], subplot_axes
):

for sys_k, c in zip(['SnS', 'SnSe'], colours):
for sys_k, c in zip(["SnS", "SnSe"], colours):
data_p, data_n = amset_data[sys_k]

# Extract data at plot temperature.

data_p = data_p[data_p['t'] == t_plot]
data_n = data_n[data_n['t'] == t_plot]

for data, l, d in [(data_p, sys_k, (None, None)),
(data_n, None, (3., 1.))]:

data_p = data_p[data_p["t"] == t_plot]
data_n = data_n[data_n["t"] == t_plot]

for data, l, d in [
(data_p, sys_k, (None, None)),
(data_n, None, (3.0, 1.0)),
]:
# Plot |S| rather than S.

y = (np.abs(data[data_k]) if data_k == 's_ave'
else data[data_k])

axes.plot(data['n'], y, label=l, color=c, dashes=d)

y = np.abs(data[data_k]) if data_k == "s_ave" else data[data_k]

axes.plot(data["n"], y, label=l, color=c, dashes=d)

# "Fake" line to add a single entry for n-type doping.

axes.plot([-0.75, -0.25], [-0.75, -0.25], label="n-type", color='k',
dashes=(3., 1.))
axes.plot(
[-0.75, -0.25], [-0.75, -0.25], label="n-type", color="k", dashes=(3.0, 1.0)
)

# Adjust axis scales/ranges and labels.

for axes in subplot_axes:
axes.set_xscale('log')
axes.set_xscale("log")
axes.set_xlim(xlim)

for axes, ylim in zip(subplot_axes, ylim_sets):
axes.set_ylim(ylim)

y_min, y_max = ylim
axes.set_yticks(np.linspace(y_min, y_max, 6))

Expand All @@ -101,27 +109,28 @@

# Add legend.

legend = subplot_axes[0].legend(loc='upper left')
legend.get_frame().set_edgecolor('k')
legend.get_frame().set_facecolor((1., 1., 1., 0.5))
legend = subplot_axes[0].legend(loc="upper left")
legend.get_frame().set_edgecolor("k")
legend.get_frame().set_facecolor((1.0, 1.0, 1.0, 0.5))

# Add subplot labels.

locs = ['lower left', 'upper right', 'upper left', 'upper left']
locs = ["lower left", "upper right", "upper left", "upper left"]

for i, (axes, loc) in enumerate(zip(subplot_axes, locs)):
subplot_label = AnchoredText(
r"({0})".format(chr(97 + i)), loc=loc, frameon=True)
r"({0})".format(chr(97 + i)), loc=loc, frameon=True
)

subplot_label.patch.set_edgecolor('k')
subplot_label.patch.set_facecolor((1., 1., 1., 0.5))
subplot_label.patch.set_edgecolor("k")
subplot_label.patch.set_facecolor((1.0, 1.0, 1.0, 0.5))

axes.add_artist(subplot_label)

# Add gridlines.

for axes in subplot_axes:
axes.grid(color=(0.9, 0.9, 0.9), dashes=(3., 1.), linewidth=0.5)
axes.grid(color=(0.9, 0.9, 0.9), dashes=(3.0, 1.0), linewidth=0.5)
axes.set_axisbelow(True)

plt.tight_layout()
Expand Down
Loading