aboutsummaryrefslogtreecommitdiff
path: root/notebook/ldc_interactive.ipynb
blob: c7c6304f6028b678be9f9ae5e61f5cf2d8a4d511 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from simulation import Lattice, Geometry\n",
    "from symbolic.generator import LBM\n",
    "import symbolic.D2Q9 as D2Q9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "lbm = LBM(D2Q9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def cavity(geometry, x, y):\n",
    "    if x == 1 or y == 1 or x == geometry.size_x-2:\n",
    "        return 2\n",
    "    elif y == geometry.size_y-2:\n",
    "        return 3\n",
    "    else:\n",
    "        return 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "boundary = \"\"\"\n",
    "    if ( m == 2 ) {\n",
    "        u_0 = 0.0;\n",
    "        u_1 = 0.0;\n",
    "    }\n",
    "    if ( m == 3 ) {\n",
    "        u_0 = 0.1;\n",
    "        u_1 = 0.0;\n",
    "    }\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def generate_moment_plot(lattice, m):\n",
    "    velocity = numpy.ndarray(shape=tuple(reversed(lattice.geometry.inner_size())))\n",
    "    for x, y in lattice.geometry.inner_cells():\n",
    "        velocity[y-1,x-1] = numpy.sqrt(m[1,lattice.gid(x,y)]**2 + m[2,lattice.gid(x,y)]**2)\n",
    "    plt.figure(figsize=(8,8))\n",
    "    plt.imshow(velocity, origin='lower', vmin=0.0, cmap=plt.get_cmap('seismic'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def test(nX=64, nY=64, nSteps=1000, tau=0.6):\n",
    "    lattice = Lattice(\n",
    "        descriptor = D2Q9,\n",
    "        geometry   = Geometry(nX, nY),\n",
    "\n",
    "        moments = lbm.moments(optimize = False),\n",
    "        collide = lbm.bgk(f_eq = lbm.equilibrium(), tau = tau),\n",
    "\n",
    "        boundary_src = boundary)\n",
    "    lattice.setup_geometry(cavity)\n",
    "    \n",
    "    for i in range(0,nSteps):\n",
    "        lattice.evolve()\n",
    "    generate_moment_plot(lattice, lattice.get_moments())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ipywidgets as widgets\n",
    "from IPython.display import display\n",
    "from ipywidgets import interactive"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "ba655c48ce2b4f4898ca9fcb61e693a5",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "interactive(children=(IntSlider(value=64, description='nX', max=1024, min=32, step=32), IntSlider(value=64, de…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "interactive(test, nX=(32,1024,32), nY=(32,1024,32), nSteps=(0,100000,500), tau=(0.515,1.0,0.01))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}