{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b37e9d9b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import Tuple, Union\n",
    "from collections import deque\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from stable_baselines3 import PPO\n",
    "import pycuda.driver as cuda\n",
    "import pandas as pd\n",
    "import pickle\n",
    "import sys\n",
    "import os\n",
    "from gym_dummy import CustomEnv as DummyEnv\n",
    "\n",
    "current_dir = os.path.dirname(os.path.abspath(\"__file__\"))\n",
    "parent_dir = os.path.abspath(os.path.join(current_dir, os.pardir))\n",
    "sys.path.append(parent_dir)\n",
    "\n",
    "from CelerisLab import FlowField\n",
    "from CelerisLab import utils\n",
    "\n",
    "cuda.init()\n",
    "context = cuda.Device(0).make_context()\n",
    "config_cuda = utils.load_cuda_config(os.path.join(parent_dir, \"configs\", \"config_cuda.json\"))\n",
    "config_field = utils.load_flow_field_config(os.path.join(parent_dir, \"configs\", \"config_flowfield.json\"))\n",
    "\n",
    "L0 = 20\n",
    "U0 = config_field.velocity\n",
    "DATA_TYPE = np.float32\n",
    "CONV_LEN = 36\n",
    "\n",
    "context.push()\n",
    "flow_field = FlowField(config_field, config_cuda, device_id=1)\n",
    "NX = flow_field.FIELD_SHAPE[0]\n",
    "NY = flow_field.FIELD_SHAPE[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7a6a6f54",
   "metadata": {},
   "outputs": [],
   "source": [
    "center: Tuple[float, float, float] = (10 * L0, (NY - 1) / 2, 0)\n",
    "flow_field.add_cylinder(center, L0 / 2)\n",
    "center: Tuple[float, float, float] = (30 * L0, (NY - 1) / 2, 0)\n",
    "flow_field.add_cylinder(center, L0 / 2)\n",
    "center: Tuple[float, float, float] = (31.3 * L0, (NY - 1) / 2 + 0.75 * L0, 0)\n",
    "flow_field.add_cylinder(center, L0 / 2)\n",
    "center: Tuple[float, float, float] = (31.3 * L0, (NY - 1) / 2 - 0.75 * L0, 0)\n",
    "flow_field.add_cylinder(center, L0 / 2)\n",
    "flow_field.run(int(4*NX/U0), np.zeros(4, dtype=DATA_TYPE))\n",
    "flow_field.get_ddf()\n",
    "flow_field.save_ddf()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0df4f52a",
   "metadata": {},
   "outputs": [],
   "source": [
    "states_ref = []\n",
    "states_react = []\n",
    "states_react2 = []\n",
    "flow_field.restore_ddf()\n",
    "flow_field.apply_ddf()\n",
    "for i in range(200):\n",
    "    flow_field.run(600, np.zeros(4, dtype=DATA_TYPE))\n",
    "    states_ref.append(flow_field.obs.copy())\n",
    "flow_field.restore_ddf()\n",
    "flow_field.apply_ddf()\n",
    "for i in range(200):\n",
    "    if i == 0:\n",
    "        flow_field.run(600, np.array([5*U0, 0, 0, 0], dtype=DATA_TYPE))\n",
    "    elif i == 1:\n",
    "        flow_field.run(600, np.array([-5*U0, 0, 0, 0], dtype=DATA_TYPE))\n",
    "    else:\n",
    "        flow_field.run(600, np.zeros(4, dtype=DATA_TYPE))\n",
    "    states_react.append(flow_field.obs.copy())\n",
    "flow_field.restore_ddf()\n",
    "flow_field.apply_ddf()\n",
    "for i in range(200):\n",
    "    if i == 50:\n",
    "        flow_field.run(600, np.array([5*U0, 0, 0, 0], dtype=DATA_TYPE))\n",
    "    elif i == 51:\n",
    "        flow_field.run(600, np.array([-5*U0, 0, 0, 0], dtype=DATA_TYPE))\n",
    "    else:\n",
    "        flow_field.run(600, np.zeros(4, dtype=DATA_TYPE))\n",
    "    states_react2.append(flow_field.obs.copy())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ed3ff1ae",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "No artists with labels found to put in legend.  Note that artists whose label start with an underscore are ignored when legend() is called with no argument.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x7a9e4153f580>]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 433200 characters]",
      "text/plain": [
       "<Figure size 1200x1800 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "states_substract = np.array([states_react[i] - states_ref[i] for i in range(200)])\n",
    "states_substract2 = np.array([states_react2[i] - states_ref[i] for i in range(200)])\n",
    "plt.figure(figsize=(12,18))\n",
    "plt.subplot(3,1,1)\n",
    "plt.plot(states_substract[:, 3] + states_substract[:, 5] + states_substract[:, 7])\n",
    "plt.plot(states_substract2[:, 3] + states_substract2[:, 5] + states_substract2[:, 7])\n",
    "plt.axvline(x=60, color='r', linestyle=':', label='x=51')\n",
    "plt.subplot(3,1,2)\n",
    "plt.legend()\n",
    "plt.plot(np.array(states_ref)[:, 1])\n",
    "plt.plot(np.array(states_react)[:, 1], linestyle='--')\n",
    "plt.plot(np.array(states_react2)[:, 1], linestyle='--')\n",
    "plt.subplot(3,1,3)\n",
    "plt.plot(np.array(states_ref)[:, 3] + np.array(states_ref)[:, 5] + np.array(states_ref)[:, 7])\n",
    "plt.plot(np.array(states_react)[:, 3] + np.array(states_react)[:, 5] + np.array(states_react)[:, 7], linestyle='--')\n",
    "plt.plot(np.array(states_react2)[:, 3] + np.array(states_react2)[:, 5] + np.array(states_react2)[:, 7], linestyle='--')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "dd853c55",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "60\n"
     ]
    }
   ],
   "source": [
    "print(int(18*L0/U0/600))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pycuda_3_10",
   "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.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
