.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/c_fields/plot_elem_wise_diff.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_auto_examples_c_fields_plot_elem_wise_diff.py: ========================================== Elementwise Partial Derivative (2D Cosine) ========================================== This example demonstrates how to compute the **elementwise partial derivatives** of a scalar field in a Cartesian coordinate system using PyMetric’s high-level :class:`~fields.base.DenseField` interface. We define a scalar field of the form: .. math:: f(x, y) = \cos(3x)\sin(4y) We then compute and visualize: - The original scalar field - The partial derivative with respect to :math:`x` - The partial derivative with respect to :math:`y` This is useful for verifying numerical derivative accuracy in simple geometries. Dependencies ------------ - :class:`~fields.base.DenseField` - :class:`~grids.core.UniformGrid` - :class:`~coordinates.coordinate_systems.CartesianCoordinateSystem2D` .. GENERATED FROM PYTHON SOURCE LINES 30-33 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np .. GENERATED FROM PYTHON SOURCE LINES 34-35 Import required modules .. GENERATED FROM PYTHON SOURCE LINES 35-37 .. code-block:: Python from pymetric import CartesianCoordinateSystem2D, DenseField, UniformGrid .. GENERATED FROM PYTHON SOURCE LINES 38-40 Setup the coordinate system and uniform grid -------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 40-46 .. code-block:: Python cs = CartesianCoordinateSystem2D() # Define bounding box and resolution bbox = [[-2, -2], [2, 2]] grid = UniformGrid(cs, bbox, [100, 100], center="cell") .. GENERATED FROM PYTHON SOURCE LINES 47-50 Define the scalar field on the grid ----------------------------------- We define f(x, y) = cos(3x) * sin(4y) .. GENERATED FROM PYTHON SOURCE LINES 50-57 .. code-block:: Python field: DenseField = DenseField.from_function( lambda x, y: np.cos(3 * x) * np.sin(4 * y), grid, axes=["x", "y"] ) # Extract mesh for plotting X, Y = field.grid.compute_domain_mesh(axes=["x", "y"]) .. GENERATED FROM PYTHON SOURCE LINES 58-60 Plot the original scalar field ------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 60-69 .. code-block:: Python plt.figure(figsize=(6, 5)) plt.pcolormesh(X, Y, field[...], shading="auto", cmap="viridis") plt.title(r"$f(x, y) = \cos(3x)\sin(4y)$") plt.xlabel("x") plt.ylabel("y") plt.colorbar(label="Field Value") plt.tight_layout() plt.show() .. image-sg:: /auto_examples/c_fields/images/sphx_glr_plot_elem_wise_diff_001.png :alt: $f(x, y) = \cos(3x)\sin(4y)$ :srcset: /auto_examples/c_fields/images/sphx_glr_plot_elem_wise_diff_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 70-73 Compute and plot the partial derivatives ---------------------------------------- Use the `element_wise_partial_derivative` method from DenseField. .. GENERATED FROM PYTHON SOURCE LINES 73-99 .. code-block:: Python gradF = field.element_wise_partial_derivatives() # Create the subplots fig, axes = plt.subplots(1, 3, figsize=(12, 4), sharex=True, sharey=True) # Plot original field a = axes[0].pcolormesh(X, Y, field[...], shading="auto", cmap="seismic") axes[0].set_title(r"$f(x, y)$") plt.colorbar(a, ax=axes[0], location="top", orientation="horizontal") # Partial w.r.t x b = axes[1].pcolormesh(X, Y, gradF[..., 0], shading="auto", cmap="seismic") axes[1].set_title(r"$\partial f / \partial x$") plt.colorbar(b, ax=axes[1], location="top", orientation="horizontal") # Partial w.r.t y c = axes[2].pcolormesh(X, Y, gradF[..., 1], shading="auto", cmap="seismic") axes[2].set_title(r"$\partial f / \partial y$") plt.colorbar(c, ax=axes[2], location="top", orientation="horizontal") for ax in axes: ax.set_xlabel("x") ax.set_ylabel("y") plt.tight_layout() plt.show() .. image-sg:: /auto_examples/c_fields/images/sphx_glr_plot_elem_wise_diff_002.png :alt: $f(x, y)$, $\partial f / \partial x$, $\partial f / \partial y$ :srcset: /auto_examples/c_fields/images/sphx_glr_plot_elem_wise_diff_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.490 seconds) .. _sphx_glr_download_auto_examples_c_fields_plot_elem_wise_diff.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_elem_wise_diff.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_elem_wise_diff.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_elem_wise_diff.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_