{ "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 the mesh we are going to 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 = (2, 2, 2)\n", "mesh = df.Mesh(p1=p1, p2=p2, n=n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We are going to initialise the vector field (`dim=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, dim=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 3-length iterable and it contains the coordinates of the point." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(625.0, 1250.0, -15625.0)" ] }, "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 `x`, `y`, or `z` attribute of the field." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "625.0" ] }, "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", "