Back (Current repo: kronig-penney)

simple Kronig-Penney band structure calculator
To clone this repository:
git clone https://git.viktor1993.net/kronig-penney.git
Log | Download | Files | Refs

commit b8ac6945b21f9de3576b3f40873c0d4df04e2dbd
Author: root <root>
Date:   Tue, 12 Aug 2025 01:56:22 +0200

create files

Diffstat:
Akp.c | 69+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Akp.h | 3+++
Akp_py.c | 38++++++++++++++++++++++++++++++++++++++
3 files changed, 110 insertions(+), 0 deletions(-)

diff --git a/kp.c b/kp.c @@ -0,0 +1,69 @@ +#include <stdio.h> +#include <math.h> +#include <complex.h> +#include "kp.h" + +void solve(const double E, double complex k[2], double b, double V_0, double Delta) { + double complex q = csqrt(2.0 * E); + double complex kappa = csqrt(2.0 * (E - V_0)); + + double complex psi_11, psi_12, psi_21, psi_22; + + psi_11 = cexp(I * q * (b - Delta)) / (4.0 * q * kappa) + * ( cexp(I * kappa * Delta) * (q + kappa) * (q + kappa) + - cexp(-I * kappa * Delta) * (q - kappa) * (q - kappa) ); + + psi_22 = conj(psi_11); + + psi_12 = -I * cexp(I * q * (b - Delta)) / (2.0 * q * kappa) + * (q * q - kappa * kappa) * csin(kappa * Delta); + + psi_21 = conj(psi_12); + + // quadratic determinantal eqn + double complex u = -(psi_11 + psi_22); + double complex v = (psi_11 * psi_22 - psi_12 * psi_21); + double complex lam[2]; + + lam[0] = (-u + csqrt(u * u - 4.0 * v)) / 2.0; + lam[1] = (-u - csqrt(u * u - 4.0 * v)) / 2.0; + + for (int x = 0; x < 2; x++) { + k[x] = clog(lam[x]) / (I * b); + } +} + +void gen_data(double b, double V_0, double Delta) { + const double pi = 4.0 * atan(1.0); + const double dE = 0.01; + double E = dE; + const int N = 3000; + + FILE *fp = fopen("data.csv", "w"); + if (!fp) { + fp = fopen("/dev/null", "w"); + } + + for (int y = 0; y < N; y++) { + double complex q[2]; + solve(E, q, b, V_0, Delta); + + double rq = creal(q[0]); + + if (rq > 0 && rq < pi / b) { + fprintf(fp, "%.15g,%.15g\n", rq, E); + fprintf(fp, "%.15g,%.15g\n", -rq, E); + } + + rq = creal(q[1]); + + if (rq > 0 && rq < pi / b) { + fprintf(fp, "%.15g,%.15g\n", rq, E); + fprintf(fp, "%.15g,%.15g\n", -rq, E); + } + + E += dE; + } + + fclose(fp); +} diff --git a/kp.h b/kp.h @@ -0,0 +1,3 @@ +#include <complex.h> + +void gen_data(double b, double V_0, double Delta); diff --git a/kp_py.c b/kp_py.c @@ -0,0 +1,38 @@ +#define PY_SSIZE_T_CLEAN +#include <Python.h> +#include <complex.h> +#include "kp.h" + +static PyObject* py_gen_data(PyObject* self, PyObject* args, PyObject* kwargs) { + static char* kwlist[] = {"b", "V_0", "Delta", NULL}; + double b = 1.0, V_0 = -5.0, Delta = 0.2; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ddd", kwlist, &b, &V_0, &Delta)) { + return NULL; + } + + gen_data(b, V_0, Delta); + + Py_RETURN_NONE; +} + +static PyMethodDef KPMethods[] = { + {"gen_data", (PyCFunction)py_gen_data, METH_VARARGS | METH_KEYWORDS, + "gen_data(b=1.0, V_0=-5.0, Delta=0.2) -> None\n" + "Generates ./data.csv using hardcoded path; silent on I/O failure."}, + {NULL, NULL, 0, NULL} +}; + +static struct PyModuleDef kp_module = { + PyModuleDef_HEAD_INIT, + "kp", + "Kronig–Penney data generator", + -1, + KPMethods +}; + +PyMODINIT_FUNC PyInit_kp(void) { + return PyModule_Create(&kp_module); +} + +