{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.11.2\n"
     ]
    }
   ],
   "source": [
    "%reload_ext autoreload\n",
    "%autoreload 2\n",
    "\n",
    "import sys\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import h5py\n",
    "\n",
    "import scipy\n",
    "print(scipy.__version__)\n",
    "scipy.interpolate.interpolate.RegularGridInterpolator = scipy.interpolate.RegularGridInterpolator\n",
    "\n",
    "sys.path.append('..')\n",
    "from darkhistory.main import evolve"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def save_h5_dict(fn, d):\n",
    "    \"\"\"Save a dictionary to an HDF5 file.\"\"\"\n",
    "    with h5py.File(fn, 'w') as hf:\n",
    "        for key, item in d.items():\n",
    "            hf.create_dataset(key, data=item)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/n/home07/yitians/.conda/envs/dm21cm/lib/python3.11/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n",
      "100%|██████████| 552/552.0 [02:16<00:00,  4.06it/s]\n"
     ]
    }
   ],
   "source": [
    "soln = evolve(\n",
    "    DM_process='decay', mDM=1e8, lifetime=3e25, primary='elec_delta',\n",
    "    start_rs = 3000,\n",
    "    coarsen_factor=12, backreaction=True, helium_TLA=True, reion_switch=True\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['rs', 'x', 'Tm', 'highengphot', 'lowengphot', 'lowengelec', 'f'])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "soln.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "soln_dict = {\n",
    "    'rs' : soln['rs'],\n",
    "    'x' : soln['x'],\n",
    "    'Tm' : soln['Tm'],\n",
    "    'highengphot' : soln['highengphot'].grid_vals,\n",
    "    'lowengphot' : soln['lowengphot'].grid_vals,\n",
    "    'lowengelec' : soln['lowengelec'].grid_vals,\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "save_h5_dict('data/test_evolve_2261700c.h5', soln_dict)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "dm21cm",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
