{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# Tutorial 9: Stokes equation"
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "    This tutorial is under construction, but the code below is already functional.\n",
    "\n",
    "Driver that computes the lid-driven cavity benchmark at low Reynolds numbers\n",
    "when using a mixed FE Q(k)/Pdisc(k-1)."
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "Load Gridap library"
   ],
   "metadata": {}
  },
  {
   "outputs": [],
   "cell_type": "code",
   "source": [
    "using Gridap"
   ],
   "metadata": {},
   "execution_count": null
  },
  {
   "cell_type": "markdown",
   "source": [
    "Discrete model"
   ],
   "metadata": {}
  },
  {
   "outputs": [],
   "cell_type": "code",
   "source": [
    "n = 100\n",
    "domain = (0,1,0,1)\n",
    "partition = (n,n)\n",
    "model = CartesianDiscreteModel(domain, partition)"
   ],
   "metadata": {},
   "execution_count": null
  },
  {
   "cell_type": "markdown",
   "source": [
    "Define Dirichlet boundaries"
   ],
   "metadata": {}
  },
  {
   "outputs": [],
   "cell_type": "code",
   "source": [
    "labels = get_face_labeling(model)\n",
    "add_tag_from_tags!(labels,\"diri1\",[6,])\n",
    "add_tag_from_tags!(labels,\"diri0\",[1,2,3,4,5,7,8])"
   ],
   "metadata": {},
   "execution_count": null
  },
  {
   "cell_type": "markdown",
   "source": [
    "Define reference FE (Q2/P1(disc) pair)"
   ],
   "metadata": {}
  },
  {
   "outputs": [],
   "cell_type": "code",
   "source": [
    "order = 2\n",
    "reffeᵤ = ReferenceFE(lagrangian,VectorValue{2,Float64},order)\n",
    "reffeₚ = ReferenceFE(lagrangian,Float64,order-1;space=:P)"
   ],
   "metadata": {},
   "execution_count": null
  },
  {
   "cell_type": "markdown",
   "source": [
    "Define test FESpaces"
   ],
   "metadata": {}
  },
  {
   "outputs": [],
   "cell_type": "code",
   "source": [
    "V = TestFESpace(model,reffeᵤ,labels=labels,dirichlet_tags=[\"diri0\",\"diri1\"],conformity=:H1)\n",
    "Q = TestFESpace(model,reffeₚ,conformity=:L2,constraint=:zeromean)\n",
    "Y = MultiFieldFESpace([V,Q])"
   ],
   "metadata": {},
   "execution_count": null
  },
  {
   "cell_type": "markdown",
   "source": [
    "Define trial FESpaces from Dirichlet values"
   ],
   "metadata": {}
  },
  {
   "outputs": [],
   "cell_type": "code",
   "source": [
    "u0 = VectorValue(0,0)\n",
    "u1 = VectorValue(1,0)\n",
    "U = TrialFESpace(V,[u0,u1])\n",
    "P = TrialFESpace(Q)\n",
    "X = MultiFieldFESpace([U,P])"
   ],
   "metadata": {},
   "execution_count": null
  },
  {
   "cell_type": "markdown",
   "source": [
    "Define triangulation and integration measure"
   ],
   "metadata": {}
  },
  {
   "outputs": [],
   "cell_type": "code",
   "source": [
    "degree = order\n",
    "Ωₕ = Triangulation(model)\n",
    "dΩ = Measure(Ωₕ,degree)"
   ],
   "metadata": {},
   "execution_count": null
  },
  {
   "cell_type": "markdown",
   "source": [
    "Define bilinear and linear form"
   ],
   "metadata": {}
  },
  {
   "outputs": [],
   "cell_type": "code",
   "source": [
    "f = VectorValue(0.0,0.0)\n",
    "a((u,p),(v,q)) = ∫( ∇(v)⊙∇(u) - (∇⋅v)*p + q*(∇⋅u) )dΩ\n",
    "l((v,q)) = ∫( v⋅f )dΩ"
   ],
   "metadata": {},
   "execution_count": null
  },
  {
   "cell_type": "markdown",
   "source": [
    "Build affine FE operator"
   ],
   "metadata": {}
  },
  {
   "outputs": [],
   "cell_type": "code",
   "source": [
    "op = AffineFEOperator(a,l,X,Y)"
   ],
   "metadata": {},
   "execution_count": null
  },
  {
   "cell_type": "markdown",
   "source": [
    "Solve"
   ],
   "metadata": {}
  },
  {
   "outputs": [],
   "cell_type": "code",
   "source": [
    "uh, ph = solve(op)"
   ],
   "metadata": {},
   "execution_count": null
  },
  {
   "cell_type": "markdown",
   "source": [
    "Export results to vtk"
   ],
   "metadata": {}
  },
  {
   "outputs": [],
   "cell_type": "code",
   "source": [
    "writevtk(Ωₕ,\"results\",order=2,cellfields=[\"uh\"=>uh,\"ph\"=>ph])"
   ],
   "metadata": {},
   "execution_count": null
  },
  {
   "cell_type": "markdown",
   "source": [
    "---\n",
    "\n",
    "*This notebook was generated using [Literate.jl](https://github.com/fredrikekre/Literate.jl).*"
   ],
   "metadata": {}
  }
 ],
 "nbformat_minor": 3,
 "metadata": {
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "1.10.8"
  },
  "kernelspec": {
   "name": "julia-1.10",
   "display_name": "Julia 1.10.8",
   "language": "julia"
  }
 },
 "nbformat": 4
}