{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Generating a Random 3D Vector Field\n\nIn this example we are going to generate a random 3D vector field with a\nGaussian covariance model. The mesh on which we generate the field will be\nexternally defined and it will be generated by PyVista.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import pyvista as pv\n\nimport gstools as gs\n\n# mainly for setting a white background\npv.set_plot_theme(\"document\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "create a uniform grid with PyVista\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "dims, spacing, origin = (40, 30, 10), (1, 1, 1), (-10, 0, 0)\nmesh = pv.UniformGrid(dims=dims, spacing=spacing, origin=origin)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "create an incompressible random 3d velocity field on the given mesh\nwith added mean velocity in x-direction\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "model = gs.Gaussian(dim=3, var=3, len_scale=1.5)\nsrf = gs.SRF(model, mean=(0.5, 0, 0), generator=\"VectorField\", seed=198412031)\nsrf.mesh(mesh, points=\"points\", name=\"Velocity\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now, we can do the plotting\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "streamlines = mesh.streamlines(\n    \"Velocity\",\n    terminal_speed=0.0,\n    n_points=800,\n    source_radius=2.5,\n)\n\n# set a fancy camera position\ncpos = [(25, 23, 17), (0, 10, 0), (0, 0, 1)]\n\np = pv.Plotter()\n# adding an outline might help navigating in 3D space\n# p.add_mesh(mesh.outline(), color=\"k\")\np.add_mesh(\n    streamlines.tube(radius=0.005),\n    show_scalar_bar=False,\n    diffuse=0.5,\n    ambient=0.5,\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "<div class=\"alert alert-info\"><h4>Note</h4><p>PyVista is not working on readthedocs, but you can try it out yourself by\n   uncommenting the following line of code.</p></div>\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# p.show(cpos=cpos)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The result should look like this:\n\n<img src=\"https://github.com/GeoStat-Framework/GeoStat-Framework.github.io/raw/master/img/GS_3d_vector_field.png\" width=\"400px\" align=\"center\">\n\n"
      ]
    }
  ],
  "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.8.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}