{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2eccae4a",
   "metadata": {},
   "source": [
    "# Ising-Lenz Ergodicity: Rate of convergence to approach to ergodicity field 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 `ising1DrateErgodicityFields.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, 10 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",
    "     * Field ranges `seq(from = 0.5, by = 0.06, length.out = 16)`.\n",
    "     * `J=1.0 ikBT=1.0`.\n",
    "     * MC steps `5e6`.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "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": 26,
   "id": "6be6cb81",
   "metadata": {
    "vscode": {
     "languageId": "r"
    }
   },
   "outputs": [],
   "source": [
    "parallel_gamma_field <- function(size, dynamics = 1, num_cores = 8) {\n",
    "    # Parallel runs\n",
    "    nfield <- 16\n",
    "    max_mc_time <- 5e6\n",
    "    fields <- seq(from = 0.5, by = 0.06, length.out = nfield)\n",
    "    mc_gamma <- function(field) {\n",
    "        performIS_repeat(ikBT =1.0, J = 1.0, H = field, size, max_mc_time, dynamics, nrepeat = 40)\n",
    "    }\n",
    "\n",
    "    current_field <- 1\n",
    "    results <- vector(\"list\", nfield)\n",
    "    cycle <- as.integer(nfield / num_cores)\n",
    "    field_count <- 0\n",
    "\n",
    "    for (i in 1:cycle) {\n",
    "        fromField <- field_count + 1\n",
    "        toField <- field_count + num_cores\n",
    "        par_result <- mclapply(fields[fromField:toField], mc_gamma, mc.cores = num_cores)\n",
    "        for (j in 1:num_cores) {\n",
    "            results[[current_field]] <- par_result[[j]]\n",
    "            current_field <- current_field + 1\n",
    "        }\n",
    "        field_count <- field_count + num_cores\n",
    "    }\n",
    "    results\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "20a8e8de",
   "metadata": {
    "vscode": {
     "languageId": "r"
    }
   },
   "outputs": [],
   "source": [
    "# ~200 min on 8 core MacBook-M2\n",
    "result <- parallel_gamma_field(512, dynamics = 1)\n",
    "result <- c(result, parallel_gamma_field(512, dynamics = 2))\n",
    "result <- c(result, parallel_gamma_field(1024, dynamics = 1))\n",
    "result <- c(result, parallel_gamma_field(1024, dynamics = 2))\n",
    "result <- c(result, parallel_gamma_field(1536, dynamics = 1))\n",
    "result <- c(result, parallel_gamma_field(1536, dynamics = 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "7e1990e4",
   "metadata": {
    "vscode": {
     "languageId": "r"
    }
   },
   "outputs": [],
   "source": [
    "saveRDS(result, \"../datasets/ising1DrateErgodicityFields.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
}
