{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Field operations\n", "\n", "There are several convenience methods that can be used to analyse the field. Let us first define a mesh with three spatial dimensions (`ndim=3`) which we can work with." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "import discretisedfield as df\n", "\n", "p1 = (-50, -50, -50)\n", "p2 = (50, 50, 50)\n", "n = (4, 2, 2)\n", "mesh = df.Mesh(p1=p1, p2=p2, n=n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We are going to initialise a three dimensional vector field (`nvdim=3`), with\n", "\n", "$$\\mathbf{f}(x, y, z) = (xy, 2xy, xyz)$$\n", "\n", "For that, we are going to use the following Python function." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def value_function(pos):\n", " x, y, z = pos\n", " return x * y, 2 * x * y, x * y * z" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, our field is" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "field = df.Field(mesh, nvdim=3, value=value_function)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Sampling the field\n", "\n", "As we have shown previously, a field can be sampled by calling it. The argument must be a iterable with the same length as `mesh.ndim` which in our case is three - as we are working in three spatial dimensions. The coordinates of the point must also be contained inside the region." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 312.5, 625. , 7812.5])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "point = (0, 0, 0)\n", "field(point)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "However, if the point is outside the mesh, an exception is raised." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Exception raised.\n" ] } ], "source": [ "point = (100, 100, 100)\n", "try:\n", " field(point)\n", "except ValueError:\n", " print(\"Exception raised.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Extracting the component of a vector field\n", "\n", "A three-dimensional vector field can be understood as three separate scalar fields, where each scalar field is a component of a vector field value. A scalar field of a component can be extracted by accessing the associated `vdims` attribute of the field. In our case this is `x`, `y`, or `z`." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([312.5])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x_component = field.x\n", "x_component((0, 0, 0))" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "Field\n", "