{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0e5b4f14",
   "metadata": {
    "vscode": {
     "languageId": "r"
    }
   },
   "source": [
    "# Ising-Lenz Ergodicity: Analysis of Time Power-laws in Kappa Diffusion Coefficients\n",
    "\n",
    "    \n",
    "     (c) 2013, 2014, 2015, 2016, 2025 Süzen\n",
    "     GPL v3 \n",
    "\n",
    "This notebook plots power-laws and writes out latex table. Generalised diffusion coefficient."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d1069816",
   "metadata": {
    "vscode": {
     "languageId": "r"
    }
   },
   "outputs": [],
   "source": [
    "df_pl_kappa <- readRDS(\"../datasets/ising1DrateErgodicityPowerlawsTime.rds\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "77566f9d",
   "metadata": {
    "vscode": {
     "languageId": "r"
    }
   },
   "outputs": [],
   "source": [
    "get_power_law <- function(df_pl_kappa, N, dynamics) {\n",
    "    df <- df_pl_kappa\n",
    "    df <- df[df[\"N\"]==N,]\n",
    "    df <- df[df[\"dynamics\"]== dynamics,]\n",
    "    y <- as.vector(df[\"C_mean\"])$C_mean\n",
    "    y_upper <- as.vector(df[\"C_upper\"])$C_upper\n",
    "    y_lower <- as.vector(df[\"C_lower\"])$C_lower\n",
    "    x <- as.vector(df[\"ikBT\"])$ikBT\n",
    "    list(\"x\"=x, \"y\"=y, \"y_upper\"=y_upper, \"y_lower\"=y_lower)\n",
    "}\n",
    "\n",
    "get_power_law <- function(df_pl_kappa, N, dynamics) {\n",
    "    df <- df_pl_kappa\n",
    "    df <- df[df[\"N\"]==N,]\n",
    "    df <- df[df[\"dynamics\"]== dynamics,]\n",
    "    y <- as.vector(df[\"C_mean\"])$C_mean\n",
    "    y_upper <- as.vector(df[\"C_upper\"])$C_upper\n",
    "    y_lower <- as.vector(df[\"C_lower\"])$C_lower\n",
    "    x <- as.vector(df[\"ikBT\"])$ikBT\n",
    "    list(\"x\"=x, \"y\"=y, \"y_upper\"=y_upper, \"y_lower\"=y_lower)\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "e672d027",
   "metadata": {
    "vscode": {
     "languageId": "r"
    }
   },
   "outputs": [],
   "source": [
    "plot_exponents <- function(fname, dynamic, base_plot=\"../plots/temperaturesPowerLaws/\") {\n",
    "    fname <- paste(base_plot, fname, sep = \"\")\n",
    "    pdf(fname, pointsize = 20) # pointsize will effect all label and title font sizes.\n",
    "    val <- get_power_law(df_pl_kappa, 512, dynamic)\n",
    "    plot(val$x, val$y, lty=1, pch=1, type=\"p\", col=\"black\", ylim=c(3,-10), ann=FALSE, xlim=c(0.4, 1.5))\n",
    "    arrows(val$x, val$y_lower, val$x, val$y_upper, col=\"black\",length=0.05, angle=90, code=3)\n",
    "    val <- get_power_law(df_pl_kappa, 1024, dynamic)\n",
    "    lines(val$x, val$y, lty=2, pch=2, type=\"p\", col=\"blue\",ann=FALSE, ylim=c(3,-10), xlim=c(0.4, 1.5))\n",
    "    arrows(val$x, val$y_lower, val$x, val$y_upper, col=\"blue\", length=0.05, angle=90, code=3)\n",
    "    val <- get_power_law(df_pl_kappa, 1536, dynamic)\n",
    "    lines(val$x, val$y, lty=3, pch=3, type=\"p\", col=\"red\", ann=FALSE, ylim=c(3,-10), xlim=c(0.4, 1.5))\n",
    "    arrows(val$x, val$y_lower, val$x, val$y_upper, col=\"red\", length=0.05, angle=90, code=3)\n",
    "    xlabel <- \"Temperature Range\"\n",
    "    ylabel <-  bquote(paste(\"Generalized Diffusion Coefficient C  \"))\n",
    "    title_main <- bquote(paste(\"Inverse rate power laws  \", Kappa(t), \" ~ \", Ct^{alpha}))\n",
    "    subtitle <- bquote(paste(\n",
    "        \"Average Magnetisation Ergodicity \", .(dynamic)\n",
    "    ))\n",
    "    title(title_main, xlab=xlabel, ylab=ylabel)\n",
    "    mtext(subtitle)\n",
    "      legend(\"topleft\",\n",
    "        legend = c(\"N=512\", \"N=1024\", \"N=1536\"),\n",
    "        pch = c(1, 2, 3),\n",
    "        col = c(\"black\", \"blue\", \"red\"),\n",
    "        lwd = 2,\n",
    "        bty = \"n\",\n",
    "      )\n",
    "    dev.off()\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "vscode": {
     "languageId": "r"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<strong>agg_record_1023962718:</strong> 2"
      ],
      "text/latex": [
       "\\textbf{agg\\textbackslash{}\\_record\\textbackslash{}\\_1023962718:} 2"
      ],
      "text/markdown": [
       "**agg_record_1023962718:** 2"
      ],
      "text/plain": [
       "agg_record_1023962718 \n",
       "                    2 "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<strong>agg_record_1023962718:</strong> 2"
      ],
      "text/latex": [
       "\\textbf{agg\\textbackslash{}\\_record\\textbackslash{}\\_1023962718:} 2"
      ],
      "text/markdown": [
       "**agg_record_1023962718:** 2"
      ],
      "text/plain": [
       "agg_record_1023962718 \n",
       "                    2 "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plot_exponents(dynamic = \"Glauber\",  fname = \"power_laws_time_glauber_diffusion_coef.pdf\")\n",
    "plot_exponents(dynamic = \"Metropolis\",  fname = \"power_laws_time_metropolis_diffusion_coef.pdf\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "ca957034",
   "metadata": {
    "vscode": {
     "languageId": "r"
    }
   },
   "outputs": [],
   "source": [
    "df_pl_kappa <- readRDS(\"../datasets/ising1DrateErgodicityPowerlawsTimeFields.rds\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "a1c12d30",
   "metadata": {
    "vscode": {
     "languageId": "r"
    }
   },
   "outputs": [],
   "source": [
    "get_power_law <- function(df_pl_kappa, N, dynamics) {\n",
    "    df <- df_pl_kappa\n",
    "    df <- df[df[\"N\"]==N,]\n",
    "    df <- df[df[\"dynamics\"]== dynamics,]\n",
    "    y <- as.vector(df[\"C_mean\"])$C_mean\n",
    "    y_upper <- as.vector(df[\"C_upper\"])$C_upper\n",
    "    y_lower <- as.vector(df[\"C_lower\"])$C_lower\n",
    "    x <- as.vector(df[\"H\"])$H\n",
    "    list(\"x\"=x, \"y\"=y, \"y_upper\"=y_upper, \"y_lower\"=y_lower)\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "36552a85",
   "metadata": {
    "vscode": {
     "languageId": "r"
    }
   },
   "outputs": [],
   "source": [
    "plot_exponents <- function(fname, dynamic, base_plot=\"../plots/fieldsPowerLaws/\") {\n",
    "    fname <- paste(base_plot, fname, sep = \"\")\n",
    "    pdf(fname, pointsize = 20) # pointsize will effect all label and title font sizes.\n",
    "    val <- get_power_law(df_pl_kappa, 512, dynamic)\n",
    "    plot(val$x, val$y, lty=1, pch=1, type=\"p\", col=\"black\", ylim=c(3,-10), ann=FALSE, xlim=c(0.4, 1.5))\n",
    "    arrows(val$x, val$y_lower, val$x, val$y_upper, col=\"black\",length=0.05, angle=90, code=3)\n",
    "    val <- get_power_law(df_pl_kappa, 1024, dynamic)\n",
    "    lines(val$x, val$y, lty=2, pch=2, type=\"p\", col=\"blue\",ann=FALSE, ylim=c(3,-10), xlim=c(0.4, 1.5))\n",
    "    arrows(val$x, val$y_lower, val$x, val$y_upper, col=\"blue\", length=0.05, angle=90, code=3)\n",
    "    val <- get_power_law(df_pl_kappa, 1536, dynamic)\n",
    "    lines(val$x, val$y, lty=3, pch=3, type=\"p\", col=\"red\", ann=FALSE, ylim=c(3,-10), xlim=c(0.4, 1.5))\n",
    "    arrows(val$x, val$y_lower, val$x, val$y_upper, col=\"red\", length=0.05, angle=90, code=3)\n",
    "    xlabel <- \"Fields Range\"\n",
    "    ylabel <-  bquote(paste(\"Generalized Diffusion Coefficient C  \"))\n",
    "    title_main <- bquote(paste(\"Inverse rate power laws  \", Kappa(t), \" ~ \", Ct^{alpha}))\n",
    "    subtitle <- bquote(paste(\n",
    "        \"Average Magnetisation Ergodicity \", .(dynamic)\n",
    "    ))\n",
    "    title(title_main, xlab=xlabel, ylab=ylabel)\n",
    "    mtext(subtitle)\n",
    "      legend(\"topleft\",\n",
    "        legend = c(\"N=512\", \"N=1024\", \"N=1536\"),\n",
    "        pch = c(1, 2, 3),\n",
    "        col = c(\"black\", \"blue\", \"red\"),\n",
    "        lwd = 2,\n",
    "        bty = \"n\",\n",
    "      )\n",
    "    dev.off()\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "0e47a987",
   "metadata": {
    "vscode": {
     "languageId": "r"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<strong>agg_record_184680923:</strong> 2"
      ],
      "text/latex": [
       "\\textbf{agg\\textbackslash{}\\_record\\textbackslash{}\\_184680923:} 2"
      ],
      "text/markdown": [
       "**agg_record_184680923:** 2"
      ],
      "text/plain": [
       "agg_record_184680923 \n",
       "                   2 "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<strong>agg_record_184680923:</strong> 2"
      ],
      "text/latex": [
       "\\textbf{agg\\textbackslash{}\\_record\\textbackslash{}\\_184680923:} 2"
      ],
      "text/markdown": [
       "**agg_record_184680923:** 2"
      ],
      "text/plain": [
       "agg_record_184680923 \n",
       "                   2 "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plot_exponents(dynamic = \"Glauber\",  fname = \"power_laws_fields_glauber_diffusion_coef.pdf\")\n",
    "plot_exponents(dynamic = \"Metropolis\",  fname = \"power_laws_fields_metropolis_diffusion_coef.pdf\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "R",
   "language": "R",
   "name": "ir"
  },
  "language_info": {
   "codemirror_mode": "r",
   "file_extension": ".r",
   "mimetype": "text/x-r-source",
   "name": "R",
   "pygments_lexer": "r",
   "version": "4.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
