{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0e5b4f14",
   "metadata": {
    "vscode": {
     "languageId": "r"
    }
   },
   "source": [
    "# Ising-Lenz Ergodicity: Analysis of Time Power-laws in Gamma 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.  For fields"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d1069816",
   "metadata": {
    "vscode": {
     "languageId": "r"
    }
   },
   "outputs": [],
   "source": [
    "df_pl_gamma <- readRDS(\"../datasets/ising1DrateErgodicityPowerlawsDistributionFields.rds\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "77566f9d",
   "metadata": {
    "vscode": {
     "languageId": "r"
    }
   },
   "outputs": [],
   "source": [
    "get_power_law <- function(df_pl_gamma, N, dynamics) {\n",
    "    df <- df_pl_gamma\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": 5,
   "id": "e672d027",
   "metadata": {
    "vscode": {
     "languageId": "r"
    }
   },
   "outputs": [],
   "source": [
    "plot_exponents <- function(fname, dynamic, base_plot=\"../plots/powerLawGammaDist/\") {\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_gamma, 512, dynamic)\n",
    "    plot(val$x, val$y, lty=1, pch=1, type=\"p\", col=\"black\", ann=FALSE, ylim=c(1.2, 2.8), 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_gamma, 1024, dynamic)\n",
    "    lines(val$x, val$y, lty=2, pch=2, type=\"p\", col=\"blue\",ann=FALSE, ylim=c(1.2, 2.8), 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_gamma, 1536, dynamic)\n",
    "    lines(val$x, val$y, lty=3, pch=3, type=\"p\", col=\"red\", ann=FALSE, ylim=c(1.2, 2.8), 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(\"Power-Laws Distribution of \", \"P(\", Gamma, \"(t)) ~\", t^{-alpha}))\n",
    "    subtitle <- bquote(paste(\n",
    "        \"Average Magnetisation: Glauber\"\n",
    "    ))\n",
    "    title(title_main, xlab=xlabel, ylab=ylabel)\n",
    "    mtext(subtitle)\n",
    "      legend(\"right\",\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": 6,
   "metadata": {
    "vscode": {
     "languageId": "r"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<strong>agg_record_1608835530:</strong> 2"
      ],
      "text/latex": [
       "\\textbf{agg\\textbackslash{}\\_record\\textbackslash{}\\_1608835530:} 2"
      ],
      "text/markdown": [
       "**agg_record_1608835530:** 2"
      ],
      "text/plain": [
       "agg_record_1608835530 \n",
       "                    2 "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<strong>agg_record_1608835530:</strong> 2"
      ],
      "text/latex": [
       "\\textbf{agg\\textbackslash{}\\_record\\textbackslash{}\\_1608835530:} 2"
      ],
      "text/markdown": [
       "**agg_record_1608835530:** 2"
      ],
      "text/plain": [
       "agg_record_1608835530 \n",
       "                    2 "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plot_exponents(dynamic = \"Glauber\",  fname = \"power_laws_dist_glauber_fields.pdf\")\n",
    "plot_exponents(dynamic = \"Metropolis\",  fname = \"power_laws_dist_metropolis_fields.pdf\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4052a549",
   "metadata": {
    "vscode": {
     "languageId": "r"
    }
   },
   "outputs": [],
   "source": [
    "# install.packages(\"Hmisc\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "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_gamma, file=\"../tables/gamma_powerlaw_dist_fields.latex\") "
   ]
  }
 ],
 "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
}
