.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/eigenmodes_1d/example_0.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_0.py: Example: 1D eigenmodes 0 ======================== 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 12-19 .. list-table:: 1D Finite-difference parameters :widths: 25 :header-rows: 0 * - boundaries: {left: none, right: none} * - derivative: 2 * - accuracy: 6 .. GENERATED FROM PYTHON SOURCE LINES 21-24 Importing required packages --------------------------- Here we import the necessary libraries for numerical computations, rendering, and finite difference operations. .. GENERATED FROM PYTHON SOURCE LINES 24-30 .. 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 31-34 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 34-45 .. code-block:: Python n_x = 100 sparse_instance = FiniteDifference( n_x=n_x, dx=1, derivative=2, accuracy=2, boundaries=Boundaries(left=BoundaryValue.NONE, right=BoundaryValue.NONE) ) .. GENERATED FROM PYTHON SOURCE LINES 46-49 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 49-58 .. code-block:: Python mesh_triplet = get_circular_mesh_triplet( n_x=n_x, radius=60, value_out=1, value_in=1.4444, x_offset=0 ) .. GENERATED FROM PYTHON SOURCE LINES 59-62 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 62-65 .. code-block:: Python dynamic_triplet = sparse_instance.triplet + mesh_triplet .. GENERATED FROM PYTHON SOURCE LINES 66-69 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 69-77 .. 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 78-81 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 81-94 .. 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_0_001.png :alt: eigenvalue: 1.442+0.000j, eigenvalue: 1.435+0.000j, eigenvalue: 1.422+0.000j, eigenvalue: 1.405+0.000j :srcset: /gallery/eigenmodes_1d/images/sphx_glr_example_0_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.646 seconds) .. _sphx_glr_download_gallery_eigenmodes_1d_example_0.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: example_0.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: example_0.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: example_0.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_