.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_gallery/mplot3d/plot_quiver3d.f90" .. 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_gallery_mplot3d_plot_quiver3d.f90: 3D Vector Field: Quiver Plot ============================ This example demonstrates how to visualize a 3D vector field using the :f:subr:`quiver3d` function. Vector fields are useful for visualizing gradients, flow patterns, and other vector-valued functions in 3D space. .. GENERATED FROM PYTHON SOURCE LINES 7-17 .. code-block:: Fortran program plot_quiver3d use fplotlib, only: dp, figure, grid, quiver3d, savefig, title, xlabel, ylabel, & zlabel implicit none integer, parameter :: nx = 5, ny = 5, nz = 5 integer :: i, j, k real(dp) :: x(nx), y(ny), z(nz), dx, dy, dz real(dp), allocatable :: pos_x(:), pos_y(:), pos_z(:), vec_x(:), vec_y(:), vec_z(:) integer :: n_arrows .. GENERATED FROM PYTHON SOURCE LINES 18-19 Create a grid of points in 3D space .. GENERATED FROM PYTHON SOURCE LINES 21-30 .. code-block:: Fortran :dedent: 1 do i = 1, nx x(i) = -2.0_dp + 4.0_dp * real(i - 1, dp) / real(nx - 1, dp) end do do j = 1, ny y(j) = -2.0_dp + 4.0_dp * real(j - 1, dp) / real(ny - 1, dp) end do do k = 1, nz z(k) = -2.0_dp + 4.0_dp * real(k - 1, dp) / real(nz - 1, dp) end do .. GENERATED FROM PYTHON SOURCE LINES 31-32 Allocate storage for the arrow positions and vectors .. GENERATED FROM PYTHON SOURCE LINES 34-37 .. code-block:: Fortran :dedent: 1 n_arrows = nx * ny * nz allocate (pos_x(n_arrows), pos_y(n_arrows), pos_z(n_arrows)) allocate (vec_x(n_arrows), vec_y(n_arrows), vec_z(n_arrows)) .. GENERATED FROM PYTHON SOURCE LINES 38-43 Define a 3D vector field: a multiple of the position vector, :math:`\mathbf{F}(\mathbf{r}) = 0.3\,\mathbf{r}`, so the field is radial and points away from the origin. In a real application, you might compute the gradient of a scalar field, or the curl of a velocity field. .. GENERATED FROM PYTHON SOURCE LINES 45-58 .. code-block:: Fortran :dedent: 1 do concurrent (k = 1:nz, j = 1:ny, i = 1:nx) associate (idx => i + nx * (j - 1 + ny * (k - 1))) pos_x(idx) = x(i) pos_y(idx) = y(j) pos_z(idx) = z(k) ! Define the vector field: in this case, we use a simple radial field ! proportional to the position vector vec_x(idx) = x(i) * 0.3_dp vec_y(idx) = y(j) * 0.3_dp vec_z(idx) = z(k) * 0.3_dp end associate end do .. GENERATED FROM PYTHON SOURCE LINES 59-60 Create the 3D quiver plot .. GENERATED FROM PYTHON SOURCE LINES 62-64 .. code-block:: Fortran :dedent: 1 call figure(figsize=[10.0_dp, 8.0_dp]) call quiver3d(pos_x, pos_y, pos_z, vec_x, vec_y, vec_z, color="C0") .. GENERATED FROM PYTHON SOURCE LINES 65-66 Configure the plot with labels and title .. GENERATED FROM PYTHON SOURCE LINES 67-79 .. code-block:: Fortran call xlabel("X") call ylabel("Y") call zlabel("Z") call title("3D Vector Field (Radial)") call grid(.true.) call savefig("quiver3d.svg") print *, "wrote quiver3d.svg" deallocate (pos_x, pos_y, pos_z, vec_x, vec_y, vec_z) end program plot_quiver3d .. image-sg:: /auto_gallery/mplot3d/images/sphx_glr_plot_quiver3d_001.svg :alt: plot quiver3d :srcset: /auto_gallery/mplot3d/images/sphx_glr_plot_quiver3d_001.svg :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none wrote quiver3d.svg .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.031 seconds) .. _sphx_glr_download_auto_gallery_mplot3d_plot_quiver3d.f90: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Fortran source code: plot_quiver3d.f90 ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_quiver3d.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_