From b523cc8605b739bf35378f8657fe332f7b201703 Mon Sep 17 00:00:00 2001 From: Philipp Holl Date: Tue, 23 Apr 2024 18:27:56 +0200 Subject: [PATCH] [doc] Update Staggered_Grids.ipynb --- docs/Staggered_Grids.ipynb | 255 ++++++++++++++++++------------------- 1 file changed, 122 insertions(+), 133 deletions(-) diff --git a/docs/Staggered_Grids.ipynb b/docs/Staggered_Grids.ipynb index b9c8338c7..950fe18ad 100644 --- a/docs/Staggered_Grids.ipynb +++ b/docs/Staggered_Grids.ipynb @@ -32,10 +32,17 @@ { "cell_type": "code", "execution_count": 2, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, "outputs": [ { "data": { - "text/plain": "\u001B[92m(xˢ=\u001B[94m(x=11, y=10)\u001B[92m, yˢ=\u001B[94m(x=10, y=11)\u001B[92m, vectorᶜ=x,y)\u001B[0m \u001B[94mconst 0.0\u001B[0m" + "text/plain": [ + "\u001b[92m(xˢ=\u001b[94m(x=11, y=10)\u001b[92m, yˢ=\u001b[94m(x=10, y=11)\u001b[92m, vectorᶜ=x,y)\u001b[0m \u001b[94mconst 0.0\u001b[0m" + ] }, "execution_count": 2, "metadata": {}, @@ -48,29 +55,27 @@ "\n", "grid = StaggeredGrid(0, extrapolation.BOUNDARY, x=10, y=10)\n", "grid.values" - ], - "metadata": { - "collapsed": false, - "pycharm": { - "name": "#%%\n" - } - } + ] }, { "cell_type": "markdown", - "source": [ - "Here, each component of the values tensor has one more sample point in the direction it is facing.\n", - "If the extrapolation was `extrapolation.ZERO`, it would be one less (see above image)." - ], "metadata": { - "collapsed": false, "pycharm": { "name": "#%% md\n" } - } + }, + "source": [ + "Here, each component of the values tensor has one more sample point in the direction it is facing.\n", + "If the extrapolation was `extrapolation.ZERO`, it would be one less (see above image)." + ] }, { "cell_type": "markdown", + "metadata": { + "pycharm": { + "name": "#%% md\n" + } + }, "source": [ "## Creating Staggered Grids\n", "\n", @@ -85,17 +90,16 @@ "\n", "\n", "Examples:" - ], - "metadata": { - "collapsed": false, - "pycharm": { - "name": "#%% md\n" - } - } + ] }, { "cell_type": "code", "execution_count": 5, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, "outputs": [], "source": [ "domain = dict(x=10, y=10, bounds=Box(x=1, y=1), extrapolation=extrapolation.ZERO)\n", @@ -106,33 +110,33 @@ "grid = StaggeredGrid(lambda x: math.exp(-x), **domain) # function value(location)\n", "grid = resample(Sphere(x=0, y=0, radius=1), StaggeredGrid(0, **domain)) # no anti-aliasing\n", "grid = resample(Sphere(x=0, y=0, radius=1), StaggeredGrid(0, **domain), soft=True) # with anti-aliasing" - ], - "metadata": { - "collapsed": false, - "pycharm": { - "name": "#%%\n" - } - } + ] }, { "cell_type": "markdown", - "source": [ - "To construct a `StaggeredGrid` from NumPy arrays (or TensorFlow/PyTorch/Jax tensors), the tensors first need to be converted to ΦFlow tensors using `tensor()` or `wrap()`." - ], "metadata": { - "collapsed": false, "pycharm": { "name": "#%% md\n" } - } + }, + "source": [ + "To construct a `StaggeredGrid` from NumPy arrays (or TensorFlow/PyTorch/Jax tensors), the tensors first need to be converted to ΦFlow tensors using `tensor()` or `wrap()`." + ] }, { "cell_type": "code", "execution_count": 6, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, "outputs": [ { "data": { - "text/plain": "StaggeredGrid[(xˢ=32, yˢ=32, vectorᶜ=2), size=\u001B[94m(x=32, y=32)\u001B[0m, extrapolation=\u001B[94m0\u001B[0m]" + "text/plain": [ + "StaggeredGrid[(xˢ=32, yˢ=32, vectorᶜ=2), size=\u001b[94m(x=32, y=32)\u001b[0m, extrapolation=\u001b[94m0\u001b[0m]" + ] }, "execution_count": 6, "metadata": {}, @@ -142,25 +146,24 @@ "source": [ "vx = tensor(np.zeros([33, 32]), spatial('x,y'))\n", "vy = tensor(np.zeros([32, 33]), spatial('x,y'))\n", - "StaggeredGrid(math.stack([vx, vy], channel('vector')), extrapolation.BOUNDARY)\n", + "StaggeredGrid(math.stack([vx, vy], dual(vector='x,y')), extrapolation.BOUNDARY)\n", "\n", "vx = tensor(np.zeros([32, 32]), spatial('x,y'))\n", "vy = tensor(np.zeros([32, 32]), spatial('x,y'))\n", - "StaggeredGrid(math.stack([vx, vy], channel('vector')), extrapolation.PERIODIC)\n", + "StaggeredGrid(math.stack([vx, vy], dual(vector='x,y')), extrapolation.PERIODIC)\n", "\n", "vx = tensor(np.zeros([31, 32]), spatial('x,y'))\n", "vy = tensor(np.zeros([32, 31]), spatial('x,y'))\n", - "StaggeredGrid(math.stack([vx, vy], channel('vector')), 0)" - ], - "metadata": { - "collapsed": false, - "pycharm": { - "name": "#%%\n" - } - } + "StaggeredGrid(math.stack([vx, vy], dual(vector='x,y')), 0)" + ] }, { "cell_type": "markdown", + "metadata": { + "pycharm": { + "name": "#%% md\n" + } + }, "source": [ "Staggered grids can also be created from other fields using `field.at()` or `@` by passing an existing `StaggeredGrid`.\n", "\n", @@ -168,35 +171,35 @@ "\n", "* [`spatial_gradient()`](phi/field/#phi.field.spatial_gradient) with `type=StaggeredGrid`\n", "* [`stagger()`](phi/field/#phi.field.stagger)\n" - ], + ] + }, + { + "cell_type": "markdown", "metadata": { - "collapsed": false, "pycharm": { "name": "#%% md\n" } - } - }, - { - "cell_type": "markdown", + }, "source": [ "## Values Tensor\n", "For non-periodic staggered grids, the `values` tensor is [non-uniform](https://tum-pbs.github.io/PhiFlow/Math.html#non-uniform-tensors)\n", "to reflect the different number of sample points for each component." - ], - "metadata": { - "collapsed": false, - "pycharm": { - "name": "#%% md\n" - } - } + ] }, { "cell_type": "code", "execution_count": 7, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, "outputs": [ { "data": { - "text/plain": "\u001B[92m(xˢ=\u001B[94m(x=9, y=10)\u001B[92m, yˢ=\u001B[94m(x=10, y=9)\u001B[92m, vectorᶜ=x,y)\u001B[0m \u001B[94mconst 0.8027918338775635\u001B[0m" + "text/plain": [ + "\u001b[92m(xˢ=\u001b[94m(x=9, y=10)\u001b[92m, yˢ=\u001b[94m(x=10, y=9)\u001b[92m, vectorᶜ=x,y)\u001b[0m \u001b[94mconst 0.8027918338775635\u001b[0m" + ] }, "execution_count": 7, "metadata": {}, @@ -205,37 +208,37 @@ ], "source": [ "grid.values" - ], - "metadata": { - "collapsed": false, - "pycharm": { - "name": "#%%\n" - } - } + ] }, { "cell_type": "markdown", + "metadata": { + "pycharm": { + "name": "#%% md\n" + } + }, "source": [ "Functions to get a uniform tensor:\n", "\n", "* [`uniform_values()`](phi/field/#phi.field.StaggeredGrid.uniform_values) interpolates the staggered values to the cell centers and returns a `CenteredGrid`\n", "* [`at_centers()`](phi/field/#phi.field.StaggeredGrid.at_centers) interpolates the staggered values to the cell centers and returns a `CenteredGrid`\n", "* [`staggered_tensor()`](phi/field/#phi.field.StaggeredGrid.staggered_tensor) pads the internal tensor to an invariant shape with n+1 entries along all dimensions." - ], - "metadata": { - "collapsed": false, - "pycharm": { - "name": "#%% md\n" - } - } + ] }, { "cell_type": "code", "execution_count": 9, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, "outputs": [ { "data": { - "text/plain": "\u001B[92m(xˢ=11, yˢ=11, vectorᶜ=x,y)\u001B[0m \u001B[94m0.597 ± 0.466\u001B[0m \u001B[37m(0e+00...1e+00)\u001B[0m" + "text/plain": [ + "\u001b[92m(xˢ=11, yˢ=11, vectorᶜ=x,y)\u001b[0m \u001b[94m0.597 ± 0.466\u001b[0m \u001b[37m(0e+00...1e+00)\u001b[0m" + ] }, "execution_count": 9, "metadata": {}, @@ -244,121 +247,107 @@ ], "source": [ "grid.uniform_values()" - ], - "metadata": { - "collapsed": false, - "pycharm": { - "name": "#%%\n" - } - } + ] }, { "cell_type": "markdown", - "source": [ - "## Slicing\n", - "Like tensors, grids can be sliced using the standard syntax.\n", - "When selecting a vector component, such as `x` or `y`, the result is represented as a `CenteredGrid` with shifted locations." - ], "metadata": { - "collapsed": false, "pycharm": { "name": "#%% md\n" } - } + }, + "source": [ + "## Slicing\n", + "Like tensors, grids can be sliced using the standard syntax.\n", + "When selecting a vector component, such as `x` or `y`, the result is represented as a `CenteredGrid` with shifted locations." + ] }, { "cell_type": "code", "execution_count": null, - "outputs": [], - "source": [ - "grid.vector['x'] # select component" - ], "metadata": { - "collapsed": false, "pycharm": { "name": "#%%\n" } - } + }, + "outputs": [], + "source": [ + "grid.vector['x'] # select component" + ] }, { "cell_type": "markdown", - "source": [ - "Grids do not support slicing along spatial dimensions because the result would be ambiguous with StaggeredGrids.\n", - "Instead, slice the `values` directly." - ], "metadata": { - "collapsed": false, "pycharm": { "name": "#%% md\n" } - } + }, + "source": [ + "Grids do not support slicing along spatial dimensions because the result would be ambiguous with StaggeredGrids.\n", + "Instead, slice the `values` directly." + ] }, { "cell_type": "code", "execution_count": null, - "outputs": [], - "source": [ - "grid.values.x[3:4] # spatial slice" - ], "metadata": { - "collapsed": false, "pycharm": { "name": "#%%\n" } - } + }, + "outputs": [], + "source": [ + "grid.values.x[3:4] # spatial slice" + ] }, { "cell_type": "code", "execution_count": null, - "outputs": [], - "source": [ - "grid.values.x[0] # spatial slice" - ], "metadata": { - "collapsed": false, "pycharm": { "name": "#%%\n" } - } + }, + "outputs": [], + "source": [ + "grid.values.x[0] # spatial slice" + ] }, { "cell_type": "markdown", - "source": [ - "Slicing along batch dimensions has no special effect, this just slices the `values`." - ], "metadata": { - "collapsed": false, "pycharm": { "name": "#%% md\n" } - } + }, + "source": [ + "Slicing along batch dimensions has no special effect, this just slices the `values`." + ] }, { "cell_type": "code", "execution_count": null, - "outputs": [], - "source": [ - "grid.batch[0] # batch slice" - ], "metadata": { - "collapsed": false, "pycharm": { "name": "#%%\n" } - } + }, + "outputs": [], + "source": [ + "grid.batch[0] # batch slice" + ] }, { "cell_type": "markdown", - "source": [ - "Fields can also be sliced using [`unstack()`](phi/field/#phi.field.unstack).\n", - "This returns a `tuple` of all slices along a dimension." - ], "metadata": { - "collapsed": false, "pycharm": { "name": "#%% md\n" } - } + }, + "source": [ + "Fields can also be sliced using [`unstack()`](phi/field/#phi.field.unstack).\n", + "This returns a `tuple` of all slices along a dimension." + ] } ], "metadata": { @@ -370,16 +359,16 @@ "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.6" + "pygments_lexer": "ipython3", + "version": "3.8.5" } }, "nbformat": 4, - "nbformat_minor": 0 -} \ No newline at end of file + "nbformat_minor": 1 +}