{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Unsteady 2D Cylinder\n",
    "\n",
    "This tutorial shows how to run a 2D unsteady simulation of a cylinder.\n",
    "\n",
    "**Note:** The settings in this example are by no means a validation setup; they are crafted to showcase the capabilities of Flow360 and we have intentionally reduced node count and example FC cost. For rigorous validation, modify the settings as needed."
   ],
   "id": "18f8d8d5"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Create Project from volume mesh\n",
    "- Load Python libraries and Flow360 client. If you use environment variables or tokens, initialize them here so later API calls can authenticate.\n",
    "- Project is created from a volume mesh"
   ],
   "id": "ab4889dd"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "import flow360 as fl\n",
    "from flow360.examples import Cylinder2D\n",
    "\n",
    "Cylinder2D.get_files()\n",
    "\n",
    "\n",
    "project = fl.Project.from_volume_mesh(Cylinder2D.mesh_filename, name=\"Unsteady 2D Cylinder\")\n",
    "\n",
    "vm = project.volume_mesh"
   ],
   "execution_count": 1,
   "outputs": [
    {
     "output_type": "display_data",
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #7fbfbf; text-decoration-color: #7fbfbf\">[17:47:54] </span><span style=\"color: #008080; text-decoration-color: #008080\">INFO</span>: VolumeMesh successfully submitted:                                                                \n",
       "<span style=\"color: #7fbfbf; text-decoration-color: #7fbfbf\">           </span>        type   = Volume Mesh                                                                            \n",
       "<span style=\"color: #7fbfbf; text-decoration-color: #7fbfbf\">           </span>        name   = Unsteady 2D Cylinder from Python                                                       \n",
       "<span style=\"color: #7fbfbf; text-decoration-color: #7fbfbf\">           </span>        id     = vm-<span style=\"color: #ffff00; text-decoration-color: #ffff00\">cf43d640-0fb0-44b8-8c39-3d026f7529ed</span>                                                \n",
       "<span style=\"color: #7fbfbf; text-decoration-color: #7fbfbf\">           </span>        status = uploaded                                                                               \n",
       "<span style=\"color: #7fbfbf; text-decoration-color: #7fbfbf\">           </span>                                                                                                        \n",
       "</pre>\n"
      ],
      "text/plain": [
       "\u001b[2;36m[17:47:54]\u001b[0m\u001b[2;36m \u001b[0m\u001b[36mINFO\u001b[0m: VolumeMesh successfully submitted:                                                                \n",
       "\u001b[2;36m           \u001b[0m        type   = Volume Mesh                                                                            \n",
       "\u001b[2;36m           \u001b[0m        name   = Unsteady 2D Cylinder from Python                                                       \n",
       "\u001b[2;36m           \u001b[0m        id     = vm-\u001b[93mcf43d640-0fb0-44b8-8c39-3d026f7529ed\u001b[0m                                                \n",
       "\u001b[2;36m           \u001b[0m        status = uploaded                                                                               \n",
       "\u001b[2;36m           \u001b[0m                                                                                                        \n"
      ]
     },
     "metadata": {}
    }
   ],
   "id": "881341eb"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Define time settings\n",
    "To run an unsteady simulation, we need to define a step size and the total number of steps. Additionally, it is also possible to define the maximum number of pseudo steps done between two physical steps."
   ],
   "id": "cc17cacc"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "with fl.SI_unit_system:\n",
    "    time_settings = fl.Unsteady(\n",
    "        step_size=2,\n",
    "        steps=20,\n",
    "        max_pseudo_steps=40,\n",
    "    )"
   ],
   "execution_count": 2,
   "outputs": [
    {
     "output_type": "display_data",
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #7fbfbf; text-decoration-color: #7fbfbf\">[17:48:13] </span><span style=\"color: #008080; text-decoration-color: #008080\">INFO</span>: using: SI unit system for unit inference.                                                         \n",
       "</pre>\n"
      ],
      "text/plain": [
       "\u001b[2;36m[17:48:13]\u001b[0m\u001b[2;36m \u001b[0m\u001b[36mINFO\u001b[0m: using: SI unit system for unit inference.                                                         \n"
      ]
     },
     "metadata": {}
    }
   ],
   "id": "768e09f4"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Define boundary conditions and solver settings\n",
    "The models section includes the boundary conditions definition and the solver settings."
   ],
   "id": "8206b22c"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "with fl.SI_unit_system:\n",
    "    models = [\n",
    "        fl.Fluid(\n",
    "            navier_stokes_solver=fl.NavierStokesSolver(\n",
    "                absolute_tolerance=1e-9,\n",
    "                linear_solver=fl.LinearSolver(max_iterations=25),\n",
    "            ),\n",
    "            turbulence_model_solver=fl.NoneSolver(),\n",
    "        ),\n",
    "        fl.Wall(surfaces=[vm[\"fluid/wall\"]]),\n",
    "        fl.Freestream(surfaces=[vm[\"fluid/farfield\"]]),\n",
    "        fl.SlipWall(surfaces=[vm[\"fluid/periodic_0_l\"], vm[\"fluid/periodic_0_r\"]]),\n",
    "    ]"
   ],
   "execution_count": 3,
   "outputs": [
    {
     "output_type": "display_data",
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #7fbfbf; text-decoration-color: #7fbfbf\">           </span><span style=\"color: #008080; text-decoration-color: #008080\">INFO</span>: using: SI unit system for unit inference.                                                         \n",
       "</pre>\n"
      ],
      "text/plain": [
       "\u001b[2;36m          \u001b[0m\u001b[2;36m \u001b[0m\u001b[36mINFO\u001b[0m: using: SI unit system for unit inference.                                                         \n"
      ]
     },
     "metadata": {}
    }
   ],
   "id": "f79d204f"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. Define SimulationParams\n",
    "- The simulation parameters are defined in the python class fl.SimulationParams()\n",
    "- THe previous settings are included as well as the operating conditions, outputs and reference geometries."
   ],
   "id": "53735fa8"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "with fl.SI_unit_system:\n",
    "    params = fl.SimulationParams(\n",
    "        operating_condition=fl.AerospaceCondition.from_mach_reynolds(\n",
    "            reynolds_mesh_unit=50, mach=0.1, project_length_unit=fl.u.m\n",
    "        ),\n",
    "        models=models,\n",
    "        time_stepping=time_settings,\n",
    "        outputs=[\n",
    "            fl.SurfaceOutput(output_fields=[\"Cp\"], surfaces=[vm[\"*\"]]),\n",
    "            fl.VolumeOutput(\n",
    "                output_fields=[\n",
    "                    \"primitiveVars\",\n",
    "                    \"vorticity\",\n",
    "                    \"residualNavierStokes\",\n",
    "                    \"T\",\n",
    "                    \"Cp\",\n",
    "                    \"mut\",\n",
    "                ],\n",
    "            ),\n",
    "        ],\n",
    "        reference_geometry=fl.ReferenceGeometry(\n",
    "            area=20, moment_center=[0, 0, 0], moment_length=[1, 1, 1]\n",
    "        ),\n",
    "    )"
   ],
   "execution_count": 4,
   "outputs": [
    {
     "output_type": "display_data",
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #7fbfbf; text-decoration-color: #7fbfbf\">           </span><span style=\"color: #008080; text-decoration-color: #008080\">INFO</span>: using: SI unit system for unit inference.                                                         \n",
       "</pre>\n"
      ],
      "text/plain": [
       "\u001b[2;36m          \u001b[0m\u001b[2;36m \u001b[0m\u001b[36mINFO\u001b[0m: using: SI unit system for unit inference.                                                         \n"
      ]
     },
     "metadata": {}
    },
    {
     "output_type": "display_data",
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #7fbfbf; text-decoration-color: #7fbfbf\">           </span><span style=\"color: #008080; text-decoration-color: #008080\">INFO</span>: Density and viscosity were calculated based on input data, ThermalState will be automatically     \n",
       "<span style=\"color: #7fbfbf; text-decoration-color: #7fbfbf\">           </span>created.                                                                                                \n",
       "</pre>\n"
      ],
      "text/plain": [
       "\u001b[2;36m          \u001b[0m\u001b[2;36m \u001b[0m\u001b[36mINFO\u001b[0m: Density and viscosity were calculated based on input data, ThermalState will be automatically     \n",
       "\u001b[2;36m           \u001b[0mcreated.                                                                                                \n"
      ]
     },
     "metadata": {}
    }
   ],
   "id": "b8d2a535"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5. Run Case"
   ],
   "id": "f51b22f2"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "case = project.run_case(params, \"Unsteady 2D Cylinder case from Python\")"
   ],
   "execution_count": 5,
   "outputs": [
    {
     "output_type": "display_data",
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #7fbfbf; text-decoration-color: #7fbfbf\">           </span><span style=\"color: #008080; text-decoration-color: #008080\">INFO</span>: Density and viscosity were calculated based on input data, ThermalState will be automatically     \n",
       "<span style=\"color: #7fbfbf; text-decoration-color: #7fbfbf\">           </span>created.                                                                                                \n",
       "</pre>\n"
      ],
      "text/plain": [
       "\u001b[2;36m          \u001b[0m\u001b[2;36m \u001b[0m\u001b[36mINFO\u001b[0m: Density and viscosity were calculated based on input data, ThermalState will be automatically     \n",
       "\u001b[2;36m           \u001b[0mcreated.                                                                                                \n"
      ]
     },
     "metadata": {}
    },
    {
     "output_type": "display_data",
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #7fbfbf; text-decoration-color: #7fbfbf\">           </span><span style=\"color: #008080; text-decoration-color: #008080\">INFO</span>: using: SI unit system for unit inference.                                                         \n",
       "</pre>\n"
      ],
      "text/plain": [
       "\u001b[2;36m          \u001b[0m\u001b[2;36m \u001b[0m\u001b[36mINFO\u001b[0m: using: SI unit system for unit inference.                                                         \n"
      ]
     },
     "metadata": {}
    },
    {
     "output_type": "display_data",
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #7fbfbf; text-decoration-color: #7fbfbf\">[17:48:15] </span><span style=\"color: #008080; text-decoration-color: #008080\">INFO</span>: Successfully submitted:                                                                           \n",
       "<span style=\"color: #7fbfbf; text-decoration-color: #7fbfbf\">           </span>        type   = Case                                                                                   \n",
       "<span style=\"color: #7fbfbf; text-decoration-color: #7fbfbf\">           </span>        name   = Unsteady 2D Cylinder case from Python                                                  \n",
       "<span style=\"color: #7fbfbf; text-decoration-color: #7fbfbf\">           </span>        id     = case-<span style=\"color: #ffff00; text-decoration-color: #ffff00\">e96c2490-48ea-4f96-b99b-1a191d63c342</span>                                              \n",
       "<span style=\"color: #7fbfbf; text-decoration-color: #7fbfbf\">           </span>        status = pending                                                                                \n",
       "<span style=\"color: #7fbfbf; text-decoration-color: #7fbfbf\">           </span>                                                                                                        \n",
       "</pre>\n"
      ],
      "text/plain": [
       "\u001b[2;36m[17:48:15]\u001b[0m\u001b[2;36m \u001b[0m\u001b[36mINFO\u001b[0m: Successfully submitted:                                                                           \n",
       "\u001b[2;36m           \u001b[0m        type   = Case                                                                                   \n",
       "\u001b[2;36m           \u001b[0m        name   = Unsteady 2D Cylinder case from Python                                                  \n",
       "\u001b[2;36m           \u001b[0m        id     = case-\u001b[93me96c2490-48ea-4f96-b99b-1a191d63c342\u001b[0m                                              \n",
       "\u001b[2;36m           \u001b[0m        status = pending                                                                                \n",
       "\u001b[2;36m           \u001b[0m                                                                                                        \n"
      ]
     },
     "metadata": {}
    }
   ],
   "id": "af461744"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "flow360",
   "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.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
