{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "152c7cca",
   "metadata": {},
   "source": [
    "# Ising-Lenz Ergodicity: Magnetisation data generation\n",
    "\n",
    "    \n",
    "     (c) 2013, 2014, 2015, 2016, 2025 Süzen\n",
    "     GPL v3 \n",
    "\n",
    "This notebook generates Ising-Lenz 1D magnetisation data `ising1Dmagnetisation.rds` under `datasets`.\n",
    "\n",
    "* Workhorse of the data generation is method `average_magnetisation_traj`. 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",
    "     * Temperature ranges `seq(from = 0.5, by = 0.06, length.out = 16)`.\n",
    "     * `J=1.0 H=1.0`.\n",
    "     * MC steps `2e6`, writes out the config at `1e3` steps."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40400eeb",
   "metadata": {
    "vscode": {
     "languageId": "r"
    }
   },
   "source": [
    "##  Utilities \n",
    "\n",
    "Here we load the library and utilities."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "492ecaf1",
   "metadata": {
    "vscode": {
     "languageId": "r"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Loading required package: isingLenzMC\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# Average Magnetisation\n",
    "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": 2,
   "id": "8ac98fbf",
   "metadata": {
    "vscode": {
     "languageId": "r"
    }
   },
   "outputs": [],
   "source": [
    "parallel_average_magnetisation <- function(size, dynamics = 1, num_cores = 8) {\n",
    "    # Parallel runs\n",
    "    ntemp <- 16\n",
    "    max_mc_time <- 2e6\n",
    "    delta_mc_time <- 1e3\n",
    "    temperatures <- seq(from = 0.5, by = 0.06, length.out = ntemp)\n",
    "    mc_mag <- function(beta) average_magnetisation_traj(size, beta, J = 1.0, H = 1.0, transP = dynamics, max_mc_time, delta_mc_time, nrepeat = 200)\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_mag, 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",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c7de9387",
   "metadata": {},
   "source": [
    "## Data generation\n",
    "\n",
    "Here we run MC steps to get the dataset and store it under `../datasets`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e99d028b",
   "metadata": {
    "vscode": {
     "languageId": "r"
    }
   },
   "outputs": [],
   "source": [
    "# ~420 min on 8 core MacBook-M2\n",
    "result <- parallel_average_magnetisation(512, dynamics = 1)\n",
    "result <- c(result, parallel_average_magnetisation(512, dynamics = 2))\n",
    "result <- c(result, parallel_average_magnetisation(1024, dynamics = 1))\n",
    "result <- c(result, parallel_average_magnetisation(1024, dynamics = 2))\n",
    "result <- c(result, parallel_average_magnetisation(1536, dynamics = 1))\n",
    "result <- c(result, parallel_average_magnetisation(1536, dynamics = 2))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1ac1975a",
   "metadata": {
    "vscode": {
     "languageId": "r"
    }
   },
   "outputs": [],
   "source": [
    "saveRDS(result, \"../datasets/ising1Dmagnetisation.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
}
