{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2eccae4a",
   "metadata": {},
   "source": [
    "# Ising-Lenz Ergodicity: Rate of convergence to approach to ergodicity temperature dependence\n",
    "\n",
    "    \n",
    "     (c) 2013, 2014, 2015, 2016, 2025 Süzen\n",
    "     GPL v3 \n",
    "\n",
    "This notebook generates Ising-Lenz 1D, rate of convergence to ergodicity data across temperatures. \n",
    "The dataset is called `ising1DrateErgodicity.rds`. \n",
    "\n",
    "$\\Omega_{G}(t)$ : Modified TM G-metric, ergodic convergence.  \n",
    "$\\Gamma_{G}(t)$ : Rate of ergodic convergence.  \n",
    "$\\Kappa_{G}(t)$ : Inverse rate of ergodic convergence.   \n",
    "\n",
    "Here we compute $\\Gamma$ in a modified fashion, original portion is that ensemble average is \n",
    "an exact expression. \n",
    "\n",
    "* Workhorse of the data generation is method `performIS_repeat`. The RDS file is simply \n",
    "an object of list, each member is the output of this method, a named list. See return of this method.\n",
    "Average magnetisations are reported there, 40 repeats\n",
    "* Parameters are hardcoded in paralell method below.\n",
    "     * Sizes at Metropolis (`transP=1`)  and Glauber (`transP=2`) Dynamics `N=512, 1024, 1536`.\n",
    "     * Temperature ranges `seq(from = 0.5, by = 0.06, length.out = 16)`.\n",
    "     * `J=1.0 H=1.0`\n",
    "     * MC steps `5e6`.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8d683416",
   "metadata": {
    "vscode": {
     "languageId": "r"
    }
   },
   "outputs": [],
   "source": [
    "rm(list=ls()) \n",
    "require(\"isingLenzMC\"); # Developed and tested on v0.2.5 and R v4.5.1\n",
    "library(parallel)       # Core package\n",
    "\n",
    "source(\"../src/power_utilities.R\");"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6be6cb81",
   "metadata": {
    "vscode": {
     "languageId": "r"
    }
   },
   "outputs": [],
   "source": [
    "parallel_gamma <- function(size, dynamics = 1, num_cores = 8) {\n",
    "    # Parallel runs\n",
    "    ntemp <- 16\n",
    "    max_mc_time <- 5e6\n",
    "    temperatures <- seq(from = 0.5, by = 0.06, length.out = ntemp)\n",
    "    mc_gamma <- function(beta) {\n",
    "        performIS_repeat(beta, J = 1.0, H = 1.0, size, max_mc_time, dynamics, nrepeat = 40)\n",
    "    }\n",
    "\n",
    "    current_temp <- 1\n",
    "    results <- vector(\"list\", ntemp)\n",
    "    cycle <- as.integer(ntemp / num_cores)\n",
    "    temp_count <- 0\n",
    "\n",
    "    for (i in 1:cycle) {\n",
    "        fromTemp <- temp_count + 1\n",
    "        toTemp <- temp_count + num_cores\n",
    "        par_result <- mclapply(temperatures[fromTemp:toTemp], mc_gamma, mc.cores = num_cores)\n",
    "        for (j in 1:num_cores) {\n",
    "            results[[current_temp]] <- par_result[[j]]\n",
    "            current_temp <- current_temp + 1\n",
    "        }\n",
    "        temp_count <- temp_count + num_cores\n",
    "    }\n",
    "    results\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20a8e8de",
   "metadata": {
    "vscode": {
     "languageId": "r"
    }
   },
   "outputs": [],
   "source": [
    "# ~200 min on 8 core MacBook-M2\n",
    "result <- parallel_gamma(512, dynamics = 1)\n",
    "result <- c(result, parallel_gamma(512, dynamics = 2))\n",
    "result <- c(result, parallel_gamma(1024, dynamics = 1))\n",
    "result <- c(result, parallel_gamma(1024, dynamics = 2))\n",
    "result <- c(result, parallel_gamma(1536, dynamics = 1))\n",
    "result <- c(result, parallel_gamma(1536, dynamics = 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7e1990e4",
   "metadata": {
    "vscode": {
     "languageId": "r"
    }
   },
   "outputs": [],
   "source": [
    "saveRDS(result, \"../datasets/ising1DrateErgodicity.rds\")"
   ]
  }
 ],
 "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
}
