.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "tutorials/mesh_generation/3_cylindrical_mesh.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_tutorials_mesh_generation_3_cylindrical_mesh.py: Cylindrical meshes ================== Cylindrical meshes (:class:`~discretize.CylindricalMesh`) are defined in terms of *r* (radial position), *z* (vertical position) and *phi* (azimuthal position). They are a child class of the tensor mesh class. Cylindrical meshes are useful in solving differential equations that possess rotational symmetry. Here we demonstrate: - How to create basic cylindrical meshes - How to include padding cells - How to plot cylindrical meshes - How to extract properties from meshes - How to create cylindrical meshes to solve PDEs with rotational symmetry .. GENERATED FROM PYTHON SOURCE LINES 20-25 Import Packages --------------- Here we import the packages required for this tutorial. .. GENERATED FROM PYTHON SOURCE LINES 26-31 .. code-block:: Python from discretize import CylindricalMesh import matplotlib.pyplot as plt import numpy as np .. GENERATED FROM PYTHON SOURCE LINES 32-43 Basic Example ------------- The easiest way to define a cylindrical mesh is to define the cell widths in *r*, *phi* and *z* as 1D numpy arrays. And to provide a Cartesian position for the bottom of the vertical axis of symmetry of the mesh. Note that 1. *phi* is in radians 2. The sum of values in the numpy array for *phi* cannot exceed :math:`2\pi` .. GENERATED FROM PYTHON SOURCE LINES 43-63 .. code-block:: Python ncr = 10 # number of mesh cells in r ncp = 8 # number of mesh cells in phi ncz = 15 # number of mesh cells in z dr = 15 # cell width r dz = 10 # cell width z hr = dr * np.ones(ncr) hp = (2 * np.pi / ncp) * np.ones(ncp) hz = dz * np.ones(ncz) x0 = 0.0 y0 = 0.0 z0 = -150.0 mesh = CylindricalMesh([hr, hp, hz], x0=[x0, y0, z0]) mesh.plot_grid() .. image-sg:: /tutorials/mesh_generation/images/sphx_glr_3_cylindrical_mesh_001.png :alt: 3 cylindrical mesh :srcset: /tutorials/mesh_generation/images/sphx_glr_3_cylindrical_mesh_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none PolarAxes [, ] .. GENERATED FROM PYTHON SOURCE LINES 64-73 Padding Cells and Extracting Properties --------------------------------------- For practical purposes, the user may want to define a region where the cell widths are increasing/decreasing in size. For example, padding is often used to define a large domain while reducing the total number of mesh cells. Here we demonstrate how to create cylindrical meshes that have padding cells. We then show some properties that can be extracted from cylindrical meshes. .. GENERATED FROM PYTHON SOURCE LINES 73-115 .. code-block:: Python ncr = 10 # number of mesh cells in r ncp = 8 # number of mesh cells in phi ncz = 15 # number of mesh cells in z dr = 15 # cell width r dp = 2 * np.pi / ncp # cell width phi dz = 10 # cell width z npad_r = 4 # number of padding cells in r npad_z = 4 # number of padding cells in z exp_r = 1.25 # expansion rate of padding cells in r exp_z = 1.25 # expansion rate of padding cells in z # Use a list of tuples to define cell widths in each direction. Each tuple # contains the cell with, number of cells and the expansion factor (+ve/-ve). hr = [(dr, ncr), (dr, npad_r, exp_r)] hp = [(dp, ncp)] hz = [(dz, npad_z, -exp_z), (dz, ncz), (dz, npad_z, exp_z)] # We can use flags 'C', '0' and 'N' to define the xyz position of the mesh. mesh = CylindricalMesh([hr, hp, hz], x0="00C") # We can apply the plot_grid method and change the axis properties ax = mesh.plot_grid() ax[0].set_title("Discretization in phi") ax[1].set_title("Discretization in r and z") ax[1].set_xlabel("r") ax[1].set_xbound(mesh.x0[0], mesh.x0[0] + np.sum(mesh.h[0])) ax[1].set_ybound(mesh.x0[2], mesh.x0[2] + np.sum(mesh.h[2])) # The bottom end of the vertical axis of rotational symmetry x0 = mesh.x0 # The total number of cells nC = mesh.nC # An (nC, 3) array containing the cell-center locations cc = mesh.gridCC # The cell volumes v = mesh.cell_volumes .. image-sg:: /tutorials/mesh_generation/images/sphx_glr_3_cylindrical_mesh_002.png :alt: Discretization in phi, Discretization in r and z :srcset: /tutorials/mesh_generation/images/sphx_glr_3_cylindrical_mesh_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none PolarAxes .. GENERATED FROM PYTHON SOURCE LINES 116-131 Cylindrical Mesh for Rotational Symmetry ---------------------------------------- Cylindrical mesh are most useful when solving problems with perfect rotational symmetry. More precisely when: - field components in the *phi* direction are 0 - fluxes in *r* and *z* are 0 In this case, the size of the forward problem can be significantly reduced. Here we demonstrate how to create a mesh for solving differential equations with perfect rotational symmetry. Since the fields and fluxes are independent of the phi position, there will be no need to discretize along the phi direction. .. GENERATED FROM PYTHON SOURCE LINES 131-167 .. code-block:: Python ncr = 10 # number of mesh cells in r ncz = 15 # number of mesh cells in z dr = 15 # cell width r dz = 10 # cell width z npad_r = 4 # number of padding cells in r npad_z = 4 # number of padding cells in z exp_r = 1.25 # expansion rate of padding cells in r exp_z = 1.25 # expansion rate of padding cells in z hr = [(dr, ncr), (dr, npad_r, exp_r)] hz = [(dz, npad_z, -exp_z), (dz, ncz), (dz, npad_z, exp_z)] # A value of 1 is used to define the discretization in phi for this case. mesh = CylindricalMesh([hr, 1, hz], x0="00C") # The bottom end of the vertical axis of rotational symmetry x0 = mesh.x0 # The total number of cells nC = mesh.nC # An (nC, 3) array containing the cell-center locations cc = mesh.gridCC # Plot the cell volumes. v = mesh.cell_volumes fig = plt.figure(figsize=(6, 4)) ax = fig.add_subplot(111) mesh.plot_image(np.log10(v), grid=True, ax=ax) ax.set_xlabel("r") ax.set_xbound(mesh.x0[0], mesh.x0[0] + np.sum(mesh.h[0])) ax.set_ybound(mesh.x0[2], mesh.x0[2] + np.sum(mesh.h[2])) ax.set_title("Cell Log-Volumes") .. image-sg:: /tutorials/mesh_generation/images/sphx_glr_3_cylindrical_mesh_003.png :alt: Cell Log-Volumes :srcset: /tutorials/mesh_generation/images/sphx_glr_3_cylindrical_mesh_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.0, 'Cell Log-Volumes') .. GENERATED FROM PYTHON SOURCE LINES 168-170 Notice that we do not plot the discretization in phi as it is irrelevant. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.915 seconds) .. _sphx_glr_download_tutorials_mesh_generation_3_cylindrical_mesh.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 3_cylindrical_mesh.ipynb <3_cylindrical_mesh.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 3_cylindrical_mesh.py <3_cylindrical_mesh.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 3_cylindrical_mesh.zip <3_cylindrical_mesh.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_