{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Mesh basics\n", "\n", "In order to solve a model numerically in a region, we have to discretise it. There are two main ways of discretising the space: finite-difference and finite-element discretisation. `discretisedfield` deals only with finite-difference discretisation at the moment. This means that we are dividing our cubic region into smaller \"chunks\" - small cubes. We refer to the discretised region as a **mesh**:\n", "\n", "$$\\text{MESH} = \\text{REGION} + \\text{DISCRETISATION}$$\n", "\n", "In this tutorial, we show how to define it, as well as some basic operations we can perform with meshes.\n", "\n", "As we showed in previous tutorials, region is always cuboidal and it is defined by any two diagonally opposite corner points. We are going to use the same region as before, defined by the following two diagonally opposite points\n", "\n", "$$\\mathbf{p}_{1} = (0, 0, 0)$$\n", "$$\\mathbf{p}_{2} = (l_{x}, l_{y}, l_{z})$$\n", "\n", "with $l_{x} = 100 \\,\\text{nm}$, $l_{y} = 50 \\,\\text{nm}$, and $l_{z} = 20 \\,\\text{nm}$.\n", "\n", "So, let us start by defining the region:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import discretisedfield as df\n", "\n", "p1 = (0, 0, 0)\n", "p2 = (100e-9, 50e-9, 20e-9)\n", "\n", "region = df.Region(p1=p1, p2=p2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The region is now defined. Another missing piece is the discretisation and we need to decide how we are going to discretise the region. In other words, we need to decide into what size \"chunks\" we are going to discretise our region in. We refer to the \"chunk\" as the **discretisation cell**. In `discretisedfield`, there are two ways how we can define the discretisation. We can define either:\n", "\n", "1. The number of discretisation cells in all geometric directions, or\n", "2. The size of a single discretisation cell.\n", "\n", "Let us start with the first case. The number of discretisation cells in all geometric directions can be passed using `n` argument, which is, for a three-dimensional geometry, a length-3 tuple:\n", "\n", "$$n = (n_{x}, n_{y}, n_{z})$$\n", "\n", "For instance, we want to discretise our region in 5 cells in the x-direction, 2 in the y-direction and 1 cell in the z-direction. Therefore, knowing the region as well as the discretisation `n`, we pass them both to `Mesh`:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "n = (5, 2, 1)\n", "\n", "mesh = df.Mesh(region=region, n=n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The mesh is defined. Based on the region dimensions and the number of discretisation cells, we can ask the mesh to give us the size of a single discretisation cell:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2.0e-08, 2.5e-08, 2.0e-08])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mesh.cell" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Knowing this value, we could have defined the mesh passing this value using `cell` argument, and we would have got exactly the same mesh." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "cell = (20e-9, 25e-9, 20e-9)\n", "\n", "mesh = df.Mesh(region=region, cell=cell)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we now ask our new mesh about the number of discretisation cells:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([5, 2, 1])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mesh.n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There is no difference whatsoever how we are going to define the mesh. However, defining the mesh with `cell` can result in an error, if the region cannot be divided into chunks of that size. For instance:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Exception raised.\n" ] } ], "source": [ "try:\n", " mesh = df.Mesh(region=region, cell=(3e-9, 3e-9, 3e-9))\n", "except ValueError:\n", " print(\"Exception raised.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let us now have a look at some basic properties we can ask the mesh object for. First of all, region object is a part of the mesh object:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "Region\n", "