.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/extras/plot_WM_field.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_extras_plot_WM_field.py: Generating Whittle-Matérn field ================================= In this example, we generate a Whittle-Matérn field using a finite difference Laplacian operator. We will explore different correlation lengths and observe their effect on the resulting fields. .. GENERATED FROM PYTHON SOURCE LINES 10-13 Importing required packages --------------------------- Here we import the necessary libraries for numerical computations, rendering, and finite difference operations. .. GENERATED FROM PYTHON SOURCE LINES 13-20 .. code-block:: Python import numpy as np from scipy import linalg import matplotlib.pyplot as plt from PyFinitDiff.finite_difference_2D import FiniteDifference, Boundaries from PyFinitDiff import BoundaryValue .. GENERATED FROM PYTHON SOURCE LINES 21-24 Setting up the finite difference Laplacian ------------------------------------------ We define a grid and create a finite difference instance that provides us with a Laplacian operator. .. GENERATED FROM PYTHON SOURCE LINES 24-39 .. code-block:: Python n_x = n_y = 20 sparse_instance = FiniteDifference( n_x=n_x, n_y=n_y, dx=1000 / n_x, dy=1000 / n_y, derivative=2, accuracy=2, boundaries=Boundaries(top=BoundaryValue.SYMMETRIC) ) laplacian = sparse_instance.triplet.to_dense() .. GENERATED FROM PYTHON SOURCE LINES 40-43 Defining the function to generate the Whittle-Matérn field ----------------------------------------------------------- The following function generates a field by solving a linear system involving the Laplacian and random noise. .. GENERATED FROM PYTHON SOURCE LINES 43-56 .. code-block:: Python def get_field(D: float, lc: float, Nc: float, shape: list): n_x, n_y = shape eta = np.random.rand(n_x * n_y) left_hand_side = (-laplacian + lc**2) ** (3 / 2) right_hand_side = eta field = linalg.solve(left_hand_side, right_hand_side) return Nc * field .. GENERATED FROM PYTHON SOURCE LINES 57-60 Visualizing the fields for different correlation lengths --------------------------------------------------------- We generate and visualize fields with varying correlation lengths to see their impact on the structure of the field. .. GENERATED FROM PYTHON SOURCE LINES 60-73 .. code-block:: Python fig, axes = plt.subplots(1, 3, figsize=(12, 4), constrained_layout=True) axes = axes.flatten() for ax, correlation_length in zip(axes, [1, 2, 4]): field = get_field(D=3, lc=correlation_length, Nc=1, shape=[n_x, n_y]).reshape([n_x, n_y]) mesh = ax.pcolormesh(field, shading='auto', cmap='viridis') ax.set_title(f'Correlation length: {correlation_length}') ax.set_aspect('equal') plt.colorbar(mesh, ax=ax) plt.show() .. image-sg:: /gallery/extras/images/sphx_glr_plot_WM_field_001.png :alt: Correlation length: 1, Correlation length: 2, Correlation length: 4 :srcset: /gallery/extras/images/sphx_glr_plot_WM_field_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.766 seconds) .. _sphx_glr_download_gallery_extras_plot_WM_field.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_WM_field.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_WM_field.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_WM_field.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_