{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Higher Dimensions\n\nGSTools provides experimental support for higher dimensions.\n\nAnisotropy is the same as in lower dimensions:\n\n- in `n` dimensions we need `(n-1)` anisotropy ratios\n\nRotation on the other hand is a bit more complex.\nWith increasing dimensions more and more rotation angles are added in order\nto properply describe the rotated axes of anisotropy.\n\nBy design the first rotation angles coincide with the lower ones:\n\n- 2D (rotation in x-y plane) -> 3D: first angle describes xy-plane rotation\n- 3D (Tait-Bryan angles) -> 4D: first 3 angles coincide with Tait-Bryan angles\n\nBy increasing the dimension from `n` to `(n+1)`, `n` angles are added:\n\n- 2D (1 angle) -> 3D: 3 angles (2 added)\n- 3D (3 angles) -> 4D: 6 angles (3 added)\n\nthe following list of rotation-planes are described by the list of\nangles in the model:\n\n1. x-y plane\n2. x-z plane\n3. y-z plane\n4. x-v plane\n5. y-v plane\n6. z-v plane\n7. ...\n\nThe rotation direction in these planes have alternating signs\nin order to match Tait-Bryan in 3D.\n\nLet's have a look at a 4D example, where we naively add a 4th dimension.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\n\nimport gstools as gs\n\ndim = 4\nsize = 20\npos = [range(size)] * dim\nmodel = gs.Exponential(dim=dim, len_scale=5)\nsrf = gs.SRF(model, seed=20170519)\nfield = srf.structured(pos)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "In order to \"prove\" correctness, we can calculate an empirical variogram\nof the generated field and fit our model to it.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "bin_center, vario = gs.vario_estimate(\n    pos, field, sampling_size=2000, mesh_type=\"structured\"\n)\nmodel.fit_variogram(bin_center, vario)\nprint(model)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "As you can see, the estimated variance and length scale match our input\nquite well.\n\nLet's have a look at the fit and a x-y cross-section of the 4D field:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "f, a = plt.subplots(1, 2, gridspec_kw={\"width_ratios\": [2, 1]}, figsize=[9, 3])\nmodel.plot(x_max=max(bin_center), ax=a[0])\na[0].scatter(bin_center, vario)\na[1].imshow(field[:, :, 0, 0].T, origin=\"lower\")\na[0].set_title(\"isotropic empirical variogram with fitted model\")\na[1].set_title(\"x-y cross-section\")\nf.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "GSTools also provides plotting routines for higher dimensions.\nFields are shown by 2D cross-sections, where other dimensions can be\ncontrolled via sliders.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "srf.plot()"
      ]
    }
  ],
  "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
}