{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Wall Roughness\n",
    "\n",
    "In this example we will set up and run simulation of an Onera M6 wing in two different configurations. One will have smooth wall boundary condition while the other one will have wall boundary condition with set roughness height.\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": "fde3e437"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Importing dependencies\n",
    "\n",
    "First we import flow360 as well as `u` namespace and `OM6Wing` example assets for convenience."
   ],
   "id": "7636d758"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "import flow360 as fl\n",
    "from flow360.component.simulation.unit_system import u\n",
    "from flow360.examples import OM6wing"
   ],
   "execution_count": 1,
   "outputs": [],
   "id": "61595f14"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Project Creation\n",
    "\n",
    "A Flow360 `Project` is a container for simulation assets. Here, we'll create a project from a pre-existing volume mesh file for the OM6 wing. We also download example's asset files."
   ],
   "id": "4e063e22"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "OM6wing.get_files()\n",
    "\n",
    "project = fl.Project.from_volume_mesh(OM6wing.mesh_filename, name=\"Wall Roughness\")\n",
    "\n",
    "vm = project.volume_mesh"
   ],
   "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\">[14:11:09] </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   = Wall roughness                                                                         \n",
       "<span style=\"color: #7fbfbf; text-decoration-color: #7fbfbf\">           </span>        id     = vm-<span style=\"color: #ffff00; text-decoration-color: #ffff00\">b0693406-b929-4b69-bcf0-ed091a8e338d</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[14:11:09]\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   = Wall roughness                                                                         \n",
       "\u001b[2;36m           \u001b[0m        id     = vm-\u001b[93mb0693406-b929-4b69-bcf0-ed091a8e338d\u001b[0m                                                \n",
       "\u001b[2;36m           \u001b[0m        status = uploaded                                                                               \n",
       "\u001b[2;36m           \u001b[0m                                                                                                        \n"
      ]
     },
     "metadata": {}
    }
   ],
   "id": "e5040ce9"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Simulation Parameters\n",
    "\n",
    "This section defines the simulation parameters. This includes defining the operating conditions, time-stepping parameters, and the physical models for the simulation.\n"
   ],
   "id": "d1262b1c"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "with fl.SI_unit_system:\n",
    "    params = fl.SimulationParams(\n",
    "        reference_geometry=fl.ReferenceGeometry(\n",
    "            area=1.15315084119231,\n",
    "            moment_center=[0, 0, 0],\n",
    "            moment_length=[1.47602, 0.801672958512342, 1.47602],\n",
    "        ),\n",
    "        operating_condition=fl.AerospaceCondition(velocity_magnitude=286, alpha=10 * fl.u.deg),\n",
    "        time_stepping=fl.Steady(max_steps=2000),\n",
    "        outputs=[\n",
    "            fl.SliceOutput(\n",
    "                entities=[\n",
    "                    *[\n",
    "                        fl.Slice(\n",
    "                            name=f\"slice_y_{name}\",\n",
    "                            normal=(0, 1, 0),\n",
    "                            origin=(0, y, 0),\n",
    "                        )\n",
    "                        for name, y in zip([\"0_2\", \"0_4\", \"0_6\", \"0_8\"], [0.2, 0.4, 0.6, 0.8])\n",
    "                    ]\n",
    "                ],\n",
    "                output_fields=[\n",
    "                    \"velocity\",\n",
    "                    \"velocity_x\",\n",
    "                    \"velocity_y\",\n",
    "                    \"velocity_z\",\n",
    "                    \"Cp\",\n",
    "                ],\n",
    "            ),\n",
    "        ],\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\">[14:11:26] </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[14:11:26]\u001b[0m\u001b[2;36m \u001b[0m\u001b[36mINFO\u001b[0m: using: SI unit system for unit inference.                                                         \n"
      ]
     },
     "metadata": {}
    }
   ],
   "id": "9b769edb"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Wall Models and Roughness Height\n",
    "\n",
    "Here we define the boundary conditions for the simulation. We use `fl.Wall` for the wing surface, `fl.SlipWall` for the symmetry plane, and `fl.Freestream` for the farfield.\n",
    "\n",
    "For the wing surface, we specify a `roughness_height`. This parameter models the effect of surface roughness on the flow, which can have significant impact on the aerodynamic performance. \n",
    "\n",
    "Physically it represents the equivalent uniform sand-grain roughness height ($k_s$), which is equal to the grain's diameter. A positive value of `roughness_height` will lower near wall velocity.\n",
    "\n",
    "We will set it here to 0.1 mm, which is within the typical range of roughness heights for a wing."
   ],
   "id": "45c57950"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "with fl.SI_unit_system:\n",
    "    params.models = [\n",
    "        fl.Wall(surfaces=vm[\"1\"], roughness_height=1e-4 * fl.u.m),\n",
    "        fl.SlipWall(surfaces=vm[\"2\"]),\n",
    "        fl.Freestream(surfaces=vm[\"3\"]),\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": {}
    }
   ],
   "id": "7ac46a00"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. Executing the Simulations\n",
    "\n",
    "We will run two simulations to compare the effects of wall roughness.\n",
    "1. The first case is run with the `roughness_height` defined above.\n",
    "2. For the second case, we modify the `roughness_height` to zero to simulate a smooth wall, and then run the simulation.\n",
    "\n",
    "The cases are run in parallel on the cloud. We then wait for both simulations to complete.\n"
   ],
   "id": "ea3e6dc7"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "case_1 = project.run_case(params, \"With wall roughness\")\n",
    "\n",
    "params.models[0].roughness_height = 0 * fl.u.m\n",
    "\n",
    "case_2 = project.run_case(params, \"Without wall roughness\")\n",
    "\n",
    "cases = [case_1, case_2]\n",
    "\n",
    "for case in cases:\n",
    "    case.wait()"
   ],
   "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>: 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\">[14:11:28] </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   = With wall roughness                                                                    \n",
       "<span style=\"color: #7fbfbf; text-decoration-color: #7fbfbf\">           </span>        id     = case-<span style=\"color: #ffff00; text-decoration-color: #ffff00\">be0f88f0-cc06-42ed-ba7f-27bee6ea8b45</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[14:11:28]\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   = With wall roughness                                                                    \n",
       "\u001b[2;36m           \u001b[0m        id     = case-\u001b[93mbe0f88f0-cc06-42ed-ba7f-27bee6ea8b45\u001b[0m                                              \n",
       "\u001b[2;36m           \u001b[0m        status = pending                                                                                \n",
       "\u001b[2;36m           \u001b[0m                                                                                                        \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\">[14:11:29] </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[14:11:29]\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\">[14:11:31] </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   = Without wall roughness                                                                 \n",
       "<span style=\"color: #7fbfbf; text-decoration-color: #7fbfbf\">           </span>        id     = case-<span style=\"color: #ffff00; text-decoration-color: #ffff00\">3aa51188-c88e-4c57-ba74-a79fbec91c0e</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[14:11:31]\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   = Without wall roughness                                                                 \n",
       "\u001b[2;36m           \u001b[0m        id     = case-\u001b[93m3aa51188-c88e-4c57-ba74-a79fbec91c0e\u001b[0m                                              \n",
       "\u001b[2;36m           \u001b[0m        status = pending                                                                                \n",
       "\u001b[2;36m           \u001b[0m                                                                                                        \n"
      ]
     },
     "metadata": {}
    }
   ],
   "id": "8e1b4d92"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "flow360-m20sMBMn-py3.11",
   "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.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
