.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/unstructured/plot_contour_unstructured.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_unstructured_plot_contour_unstructured.py: Contour and streamlines from an unstructured mesh ================================================= This example reads and plots a contour of an OpenFoam vector field from an unstructured mesh by interpolation on a structured grid .. GENERATED FROM PYTHON SOURCE LINES 10-15 Reads the mesh -------------- .. note:: It reads the mesh coordinates and stores them in variables x, y and z .. GENERATED FROM PYTHON SOURCE LINES 15-23 .. code-block:: Python # import readmesh function from fluidfoam package from fluidfoam import readmesh sol = '../../output_samples/pipeline/' x, y, z = readmesh(sol) .. rst-class:: sphx-glr-script-out .. code-block:: none Reading file ../../output_samples/pipeline//constant/polyMesh/owner Reading file ../../output_samples/pipeline//constant/polyMesh/faces Reading file ../../output_samples/pipeline//constant/polyMesh/points Reading file ../../output_samples/pipeline//constant/polyMesh/neighbour .. GENERATED FROM PYTHON SOURCE LINES 24-29 Reads vector and scalar field ----------------------------- .. note:: It reads vector and scalar field from an unstructured mesh and stores them in vel and alpha variables .. GENERATED FROM PYTHON SOURCE LINES 29-37 .. code-block:: Python # import readvector and readscalar functions from fluidfoam package from fluidfoam import readvector, readscalar timename = '25' vel = readvector(sol, timename, 'Ub') alpha = readscalar(sol, timename, 'alpha') .. rst-class:: sphx-glr-script-out .. code-block:: none Reading file ../../output_samples/pipeline/25/Ub Reading file ../../output_samples/pipeline/25/alpha .. GENERATED FROM PYTHON SOURCE LINES 38-43 Interpolate the fields on a structured grid ------------------------------------------- .. note:: The vector and scalar fields are interpolated on a specified structured grid .. GENERATED FROM PYTHON SOURCE LINES 43-69 .. code-block:: Python import numpy as np from scipy.interpolate import griddata # Number of division for linear interpolation ngridx = 500 ngridy = 180 # Interpolation grid dimensions xinterpmin = -0.1 xinterpmax = 0.35 yinterpmin = -0.075 yinterpmax = 0.075 # Interpolation grid xi = np.linspace(xinterpmin, xinterpmax, ngridx) yi = np.linspace(yinterpmin, yinterpmax, ngridy) # Structured grid creation xinterp, yinterp = np.meshgrid(xi, yi) # Interpolation of scalra fields and vector field components alpha_i = griddata((x, y), alpha, (xinterp, yinterp), method='linear') velx_i = griddata((x, y), vel[0, :], (xinterp, yinterp), method='linear') vely_i = griddata((x, y), vel[1, :], (xinterp, yinterp), method='linear') .. GENERATED FROM PYTHON SOURCE LINES 70-75 Plots the contour of the interpolted scalarfield alpha, streamlines and a patch ------------------------------------------------------------------------------- .. note:: The scalar field alpha reprensents the concentration of sediment in in a 2D two-phase flow simulation of erosion below a pipeline .. GENERATED FROM PYTHON SOURCE LINES 75-100 .. code-block:: Python import matplotlib.pyplot as plt # Define plot parameters fig = plt.figure(figsize=(8.5, 3), dpi=100) plt.rcParams.update({'font.size': 10}) plt.xlabel('x/D') plt.ylabel('y/D') d = 0.05 # Add a cuircular patch representing the pipeline circle = plt.Circle((0, 0), radius=0.5, fc='silver', zorder=10, edgecolor='k') plt.gca().add_patch(circle) # Plots the contour of sediment concentration levels = np.arange(0.1, 0.63, 0.001) plt.contourf(xi/d, yi/d, alpha_i, cmap=plt.cm.Reds, levels=levels) # Calculation of the streamline width as a function of the velociy magnitude vel_i = np.sqrt(velx_i**2 + vely_i**2) lw = pow(vel_i, 1.5)/vel_i.max() # Plots the streamlines plt.streamplot(xi/d, yi/d, velx_i, vely_i, color='C0', density=[2, 1], linewidth=lw, arrowsize=0.05) .. image-sg:: /auto_examples/unstructured/images/sphx_glr_plot_contour_unstructured_001.png :alt: plot contour unstructured :srcset: /auto_examples/unstructured/images/sphx_glr_plot_contour_unstructured_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 12.881 seconds) .. _sphx_glr_download_auto_examples_unstructured_plot_contour_unstructured.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_contour_unstructured.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_contour_unstructured.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_contour_unstructured.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_