.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/eigenmodes_2d/example_2.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_gallery_eigenmodes_2d_example_2.py: Example: eigenmodes 2 ====================== In this example, we calculate and visualize the eigenmodes of a finite difference operator combined with a circular mesh potential. The boundary conditions, mesh properties, and eigenmode calculations are all set up for demonstration purposes. .. GENERATED FROM PYTHON SOURCE LINES 11-16 +-------------+------------+--------------+------------+------------+ | Boundaries | left | right | top | bottom | +=============+============+==============+============+============+ | - | anti-sym | zero | zero | zero | +-------------+------------+--------------+------------+------------+ .. GENERATED FROM PYTHON SOURCE LINES 18-21 Importing required packages --------------------------- Here we import the necessary libraries for numerical computations, rendering, and finite difference operations. .. GENERATED FROM PYTHON SOURCE LINES 21-27 .. code-block:: Python from scipy.sparse import linalg from PyFinitDiff.finite_difference_2D import FiniteDifference, get_circular_mesh_triplet, Boundaries import matplotlib.pyplot as plt from PyFinitDiff import BoundaryValue .. GENERATED FROM PYTHON SOURCE LINES 28-31 Setting up the finite difference instance and boundaries --------------------------------------------------------- We define the grid size and set up the finite difference instance with specified boundary conditions. .. GENERATED FROM PYTHON SOURCE LINES 31-46 .. code-block:: Python n_y = n_x = 80 sparse_instance = FiniteDifference( n_x=n_x, n_y=n_y, dx=1, dy=1, derivative=2, accuracy=4, boundaries=Boundaries(left=BoundaryValue.ANTI_SYMMETRIC) ) triplet = sparse_instance.triplet .. GENERATED FROM PYTHON SOURCE LINES 47-50 Creating the circular mesh potential ------------------------------------- We create a circular mesh triplet, specifying the inner and outer values, and offset parameters. .. GENERATED FROM PYTHON SOURCE LINES 50-61 .. code-block:: Python mesh_triplet = get_circular_mesh_triplet( n_x=n_x, n_y=n_y, value_out=1.0, value_in=1.4444, x_offset=-100, y_offset=0, radius=70 ) .. GENERATED FROM PYTHON SOURCE LINES 62-65 Combining the finite difference and mesh triplets -------------------------------------------------- We add the circular mesh triplet to the finite difference Laplacian to form the dynamic triplet. .. GENERATED FROM PYTHON SOURCE LINES 65-68 .. code-block:: Python dynamic_triplet = sparse_instance.triplet + mesh_triplet .. GENERATED FROM PYTHON SOURCE LINES 69-72 Calculating the eigenmodes --------------------------- We compute the first four eigenmodes of the combined operator using the scipy sparse linear algebra package. .. GENERATED FROM PYTHON SOURCE LINES 72-82 .. code-block:: Python eigen_values, eigen_vectors = linalg.eigs( dynamic_triplet.to_scipy_sparse(), k=4, which='LM', sigma=1.4444 ) shape = [sparse_instance.n_x, sparse_instance.n_y] .. GENERATED FROM PYTHON SOURCE LINES 83-86 Visualizing the eigenmodes with matplotlib ------------------------------------------- We visualize the first four eigenmodes by reshaping the eigenvectors and plotting them using matplotlib. .. GENERATED FROM PYTHON SOURCE LINES 86-97 .. code-block:: Python fig, axes = plt.subplots(1, 4, figsize=(16, 4), constrained_layout=True) for i, ax in enumerate(axes): vector = eigen_vectors[:, i].real.reshape(shape) mesh = ax.pcolormesh(vector, shading='auto', cmap='viridis') ax.set_title(f'eigenvalue: {eigen_values[i]:.3f}') ax.set_aspect('equal') plt.colorbar(mesh, ax=ax) plt.show() .. image-sg:: /gallery/eigenmodes_2d/images/sphx_glr_example_2_001.png :alt: eigenvalue: 1.427+0.000j, eigenvalue: 1.413+0.000j, eigenvalue: 1.397+0.000j, eigenvalue: 1.387+0.000j :srcset: /gallery/eigenmodes_2d/images/sphx_glr_example_2_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.061 seconds) .. _sphx_glr_download_gallery_eigenmodes_2d_example_2.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: example_2.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: example_2.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: example_2.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_