{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0e5b4f14",
   "metadata": {
    "vscode": {
     "languageId": "r"
    }
   },
   "source": [
    "# Ising-Lenz Ergodicity: Analysis of Time Power-laws in Kappa Fields\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. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d1069816",
   "metadata": {
    "vscode": {
     "languageId": "r"
    }
   },
   "outputs": [],
   "source": [
    "df_pl_kappa <- readRDS(\"../datasets/ising1DrateErgodicityPowerlawsTimeFields.rds\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "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[\"alpha_mean\"])$alpha_mean\n",
    "    y_upper <- as.vector(df[\"alpha_upper\"])$alpha_upper\n",
    "    y_lower <- as.vector(df[\"alpha_lower\"])$alpha_lower\n",
    "    x <- as.vector(df[\"H\"])$H\n",
    "    list(\"x\"=x, \"y\"=y, \"y_upper\"=y_upper, \"y_lower\"=y_lower)\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e672d027",
   "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\", ann=FALSE, ylim=c(0.0, 2.2), 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(0.0, 2.2), 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(0.0, 2.2), 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(\"Scaling exponent \", alpha))\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": 4,
   "metadata": {
    "vscode": {
     "languageId": "r"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<strong>agg_record_1324219940:</strong> 2"
      ],
      "text/latex": [
       "\\textbf{agg\\textbackslash{}\\_record\\textbackslash{}\\_1324219940:} 2"
      ],
      "text/markdown": [
       "**agg_record_1324219940:** 2"
      ],
      "text/plain": [
       "agg_record_1324219940 \n",
       "                    2 "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<strong>agg_record_1324219940:</strong> 2"
      ],
      "text/latex": [
       "\\textbf{agg\\textbackslash{}\\_record\\textbackslash{}\\_1324219940:} 2"
      ],
      "text/markdown": [
       "**agg_record_1324219940:** 2"
      ],
      "text/plain": [
       "agg_record_1324219940 \n",
       "                    2 "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plot_exponents(dynamic = \"Glauber\",  fname = \"power_laws_time_fields_glauber.pdf\")\n",
    "plot_exponents(dynamic = \"Metropolis\",  fname = \"power_laws_time_fields_metropolis.pdf\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "5e1ec2b7",
   "metadata": {
    "vscode": {
     "languageId": "r"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n",
      "Attaching package: ‘Hmisc’\n",
      "\n",
      "\n",
      "The following objects are masked from ‘package:base’:\n",
      "\n",
      "    format.pval, units\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "library(Hmisc)\n",
    "latex(df_pl_kappa, file=\"../tables/kappa_powerlaw_time.ipynb\") "
   ]
  }
 ],
 "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
}
