{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Stretch CdTe and CdSe₀.₅Te₀.₅ nanowires\n",
    "\n",
    "Compare the tensile strength of a zinc-blende CdTe nanowire before and after random Se substitution, with the Cd–Te–Se bond-order potential.\n",
    "\n",
    "Paper: Atomistic Study of Selenium Doping Effects on the Mechanical Properties of Zinc-Blende and Wurtzite CdTe Nanowires\n",
    "\n",
    "Website tutorial: reproduce/cdte-nanowires/ · Status: Inputs not yet validated · Runtime: ~15 min (4 cores, estimate)."
   ],
   "id": "cell-1"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "!pip install -q lammps numpy matplotlib"
   ],
   "id": "cell-2"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Download the potential files below and upload them to the notebook's working directory before running the simulation. Keep these exact filenames.\n",
    "\n",
    "- [CdTeSe.bop.table](https://github.com/lammps/lammps/blob/develop/potentials/CdTeSe.bop.table)"
   ],
   "id": "cell-3"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "%%writefile in.cdte.lmp\n",
    "units metal\n",
    "atom_style atomic\n",
    "boundary f f p\n",
    "variable T index 300\n",
    "variable rate index 0.01\n",
    "variable seed index 87287\n",
    "timestep 0.001\n",
    "variable x index 0.5\n",
    "variable out index tension_x05.txt\n",
    "lattice custom 6.482 a1 1 0 0 a2 0 1 0 a3 0 0 1 &\n",
    " basis 0 0 0 basis 0 0.5 0.5 basis 0.5 0 0.5 basis 0.5 0.5 0 &\n",
    " basis 0.25 0.25 0.25 basis 0.25 0.75 0.75 basis 0.75 0.25 0.75 basis 0.75 0.75 0.25\n",
    "region box block -24 24 -24 24 -25.928 25.928 units box\n",
    "create_box 3 box\n",
    "region wire cylinder z 0 0 21 -25.928 25.928 units box\n",
    "create_atoms 1 region wire basis 5 2 basis 6 2 basis 7 2 basis 8 2\n",
    "mass 1 112.414\n",
    "mass 2 127.60\n",
    "mass 3 78.971\n",
    "# Substitute on the Te sublattice only.\n",
    "set type 2 type/fraction 3 ${x} ${seed}\n",
    "pair_style bop\n",
    "pair_coeff * * CdTeSe.bop.table Cd Te Se\n",
    "comm_modify cutoff 15.0\n",
    "neighbor 2 bin\n",
    "minimize 1e-7 1e-7 200 2000\n",
    "velocity all create ${T} ${seed} mom yes rot yes dist gaussian\n",
    "fix thermostat all nvt temp ${T} ${T} 0.1\n",
    "thermo 500\n",
    "run 2000\n",
    "# Freeze initial length and normalize global virial by the initial material volume.\n",
    "variable L0 equal $(lz)\n",
    "variable A0 equal 1385.44236\n",
    "variable strain equal (lz-v_L0)/v_L0\n",
    "variable stress equal -pzz*vol/(v_A0*v_L0)*0.0001\n",
    "fix deform all deform 1 z erate ${rate} units box remap x\n",
    "fix output all print 100 \"${strain} ${stress}\" file ${out} screen no title \"# strain stress_GPa\"\n",
    "run 15000\n"
   ],
   "id": "cell-4"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "%%writefile analyze.py\n",
    "from pathlib import Path\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "CASES = [('tension_x0.txt','x = 0'),('tension_x05.txt','x = 0.5')]\n",
    "fig, ax = plt.subplots()\n",
    "found = 0\n",
    "for filename, label in CASES:\n",
    "    path = Path(filename)\n",
    "    if not path.exists():\n",
    "        print(f\"Missing output: {path}. Run the matching LAMMPS command first.\")\n",
    "        continue\n",
    "    try:\n",
    "        data = np.loadtxt(path, comments=\"#\", ndmin=2)\n",
    "        if data.shape[1] < 2 or data.shape[0] == 0:\n",
    "            print(f\"{path}: expected strain and stress_GPa columns; skipping.\")\n",
    "            continue\n",
    "        data = data[np.isfinite(data[:, :2]).all(axis=1)]\n",
    "        if not len(data):\n",
    "            print(f\"{path}: no finite data rows; skipping.\")\n",
    "            continue\n",
    "    except (ValueError, OSError) as exc:\n",
    "        print(f\"Cannot read {path}: {exc}\")\n",
    "        continue\n",
    "    strain, stress = data[:, 0], data[:, 1]\n",
    "    ax.plot(strain, stress, label=label); print(f\"{label}: UTS = {stress.max():.4g} GPa\")\n",
    "    found += 1\n",
    "if found:\n",
    "    ax.set(xlabel=\"Engineering strain\", ylabel=\"Engineering stress (GPa)\")\n",
    "    ax.legend()\n",
    "    fig.tight_layout()\n",
    "    fig.savefig(\"stress_strain.png\", dpi=200)\n",
    "    print(\"Saved stress_strain.png\")\n",
    "else:\n",
    "    print(\"No usable data; no figure written.\")\n"
   ],
   "id": "cell-5"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "from lammps import lammps\n",
    "simulation = lammps(cmdargs=[\"-var\",\"x\",\"0\",\"-var\",\"out\",\"tension_x0.txt\"])\n",
    "try:\n",
    "    simulation.file(\"in.cdte.lmp\")\n",
    "finally:\n",
    "    simulation.close()"
   ],
   "id": "cell-6"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "from lammps import lammps\n",
    "simulation = lammps(cmdargs=[\"-var\",\"x\",\"0.5\",\"-var\",\"out\",\"tension_x05.txt\"])\n",
    "try:\n",
    "    simulation.file(\"in.cdte.lmp\")\n",
    "finally:\n",
    "    simulation.close()"
   ],
   "id": "cell-7"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "!python analyze.py"
   ],
   "id": "cell-8"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "from IPython.display import Image, display\n",
    "import glob\n",
    "for f in sorted(glob.glob('out/*.png') + glob.glob('*.png')):\n",
    "    print(f); display(Image(f))"
   ],
   "id": "cell-9"
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3"
  },
  "language_info": {
   "name": "python"
  },
  "colab": {
   "provenance": []
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}