{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Pull an Inconel-718 nanowire at 100 K and 800 K\n",
    "\n",
    "Build a random Ni–Cr–Fe alloy nanowire, stretch it at two temperatures with the Bonny FeNiCr EAM potential, and compare the stress–strain curves.\n",
    "\n",
    "Paper: Deformation Mechanisms of Inconel-718 at the Nanoscale by Molecular Dynamics\n",
    "\n",
    "Website tutorial: reproduce/inconel-nanoscale/ · Status: Inputs not yet validated · Runtime: ~10–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",
    "- [FeNiCr.eam.alloy](https://www.ctcms.nist.gov/potentials/Download/2011--Bonny-G-Terentyev-D-Pasianot-R-C-et-al--Fe-Ni-Cr/3/FeNiCr.eam.alloy)"
   ],
   "id": "cell-3"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "%%writefile in.inconel.lmp\n",
    "units metal\n",
    "atom_style atomic\n",
    "boundary f f p\n",
    "variable T index 100\n",
    "variable rate index 0.01\n",
    "variable seed index 87287\n",
    "timestep 0.001\n",
    "variable out index tension_100.txt\n",
    "lattice fcc 3.52\n",
    "region box block -19 19 -19 19 -28.16 28.16 units box\n",
    "create_box 3 box\n",
    "region wire cylinder z 0 0 14 -28.16 28.16 units box\n",
    "create_atoms 1 region wire\n",
    "mass 1 58.6934\n",
    "mass 2 51.9961\n",
    "mass 3 55.845\n",
    "set type 1 type/fraction 2 0.21 ${seed}\n",
    "variable seed2 equal v_seed+17\n",
    "set type 1 type/fraction 3 0.2405063291 ${seed2}\n",
    "pair_style eam/alloy\n",
    "pair_coeff * * FeNiCr.eam.alloy Ni Cr Fe\n",
    "neighbor 2 bin\n",
    "min_style cg\n",
    "minimize 1e-8 1e-8 500 5000\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 615.75216\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_100.txt','100 K'),('tension_800.txt','800 K')]\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}: peak stress = {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\",\"T\",\"100\",\"-var\",\"out\",\"tension_100.txt\"])\n",
    "try:\n",
    "    simulation.file(\"in.inconel.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\",\"T\",\"800\",\"-var\",\"out\",\"tension_800.txt\"])\n",
    "try:\n",
    "    simulation.file(\"in.inconel.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
}