.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/eigenmodes_1d/example_3.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_1d_example_3.py: Example: 1D eigenmodes 2 ======================== In this example, we calculate and visualize the eigenmodes of a 1D 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-18 .. list-table:: 1D Finite-difference parameters :widths: 25 :header-rows: 0 * - boundaries: {left: anti-symmetric, right: anti-symmetric} * - derivative: 2 * - accuracy: 6 .. GENERATED FROM PYTHON SOURCE LINES 20-23 Importing required packages --------------------------- Here we import the necessary libraries for numerical computations, rendering, and finite difference operations. .. GENERATED FROM PYTHON SOURCE LINES 23-29 .. code-block:: Python from scipy.sparse import linalg import matplotlib.pyplot as plt from PyFinitDiff.finite_difference_1D import FiniteDifference, get_circular_mesh_triplet, Boundaries from PyFinitDiff import BoundaryValue .. GENERATED FROM PYTHON SOURCE LINES 30-33 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 33-43 .. code-block:: Python n_x = 200 sparse_instance = FiniteDifference( n_x=n_x, dx=1, derivative=2, accuracy=6, boundaries=Boundaries(left=BoundaryValue.ANTI_SYMMETRIC, right=BoundaryValue.ANTI_SYMMETRIC) ) .. GENERATED FROM PYTHON SOURCE LINES 44-47 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 47-56 .. code-block:: Python mesh_triplet = get_circular_mesh_triplet( n_x=n_x, radius=60, value_out=1, value_in=1.4444, x_offset=+100 ) .. GENERATED FROM PYTHON SOURCE LINES 57-60 Combining the finite difference and mesh triplets -------------------------------------------------- We add the circular mesh triplet to the finite difference operator to form the dynamic triplet. .. GENERATED FROM PYTHON SOURCE LINES 60-63 .. code-block:: Python dynamic_triplet = sparse_instance.triplet + mesh_triplet .. GENERATED FROM PYTHON SOURCE LINES 64-67 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 67-75 .. code-block:: Python eigen_values, eigen_vectors = linalg.eigs( dynamic_triplet.to_dense(), k=4, which='LM', sigma=1.4444 ) .. GENERATED FROM PYTHON SOURCE LINES 76-79 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 79-92 .. code-block:: Python fig, axes = plt.subplots(2, 2, figsize=(10, 8), constrained_layout=True) axes = axes.flatten() for i, ax in enumerate(axes): vector = eigen_vectors[:, i].real ax.plot(vector) ax.set_title(f'eigenvalue: {eigen_values[i]:.3f}') ax.set_xlabel('Index') ax.set_ylabel('Amplitude') ax.grid(True) plt.show() .. image-sg:: /gallery/eigenmodes_1d/images/sphx_glr_example_3_001.png :alt: eigenvalue: 1.442+0.000j, eigenvalue: 1.434+0.000j, eigenvalue: 1.421+0.000j, eigenvalue: 1.402+0.000j :srcset: /gallery/eigenmodes_1d/images/sphx_glr_example_3_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.597 seconds) .. _sphx_glr_download_gallery_eigenmodes_1d_example_3.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: example_3.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: example_3.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: example_3.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_