.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/advanced/plot_unstructured_simu.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_advanced_plot_unstructured_simu.py: Plot bed interface from unstructured mesh ======================================= This example shows how to create a pointer list of profiles from an unstructured mesh and to extract the bed interface elevation. The results are plotted as a scatter plot. .. GENERATED FROM PYTHON SOURCE LINES 11-21 .. code-block:: Python #path where the simulation is located path = '../../output_samples' #Name of the simulation to load simu = '3dscour' #time step to load timeStep = '0' #Switch to save postprocessed data on output saveOutput = 1 .. GENERATED FROM PYTHON SOURCE LINES 22-26 Read the mesh and extract the pointerList -------------------------------------- import python packages .. GENERATED FROM PYTHON SOURCE LINES 26-57 .. code-block:: Python from fluidfoam import readmesh,readfield import os import numpy as np # #---read mesh (in folder constant, if not done type writeCellCenters -time constant in a terminal) # sol = os.path.join(path, simu) Xb, Yb, Zb = readfield(sol, 'constant', 'C', precision=12) # #---create a list of (x,y) positions correpsonding to Zb = min(Zb) # print("Generate pointer list") pbed = np.where(Zb == np.amin(Zb))[0] n2d = np.size(pbed) nz = 0 profileList = [] for i in range(n2d): #Extract the list of points having the same (Xb, Yb) values indices = np.where(np.logical_and(Xb == Xb[pbed[i]], Yb == Yb[pbed[i]]))[0] nz = max(nz, np.size(indices)) profile = list(indices) #Sort the list of points by increasing Zb values (profile) profileSorted = [x for _, x in sorted(zip(Zb[profile], profile))] #Append the list to the profileList profileList.append(profileSorted) # Convert profileList to numpy.array pointer = np.array(profileList) .. rst-class:: sphx-glr-script-out .. code-block:: none Reading file ../../output_samples/3dscour/constant/C Generate pointer list .. GENERATED FROM PYTHON SOURCE LINES 58-61 Extract bed surface from alpha.a profiles: min{ Zb | alpha.a <= 0.57 } -------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 61-69 .. code-block:: Python print("Extract bed surface") a = readfield(sol, timeStep, 'alpha.a') zbed = np.zeros(n2d) for i in range(n2d): bed_surface=np.where(a[pointer[i,:]] <= 0.57)[0] zbed[i] = np.min(Zb[pointer[i,bed_surface]]) .. rst-class:: sphx-glr-script-out .. code-block:: none Extract bed surface Reading file ../../output_samples/3dscour/0/alpha.a .. GENERATED FROM PYTHON SOURCE LINES 70-73 Now plot the zbed elevation -------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 73-96 .. code-block:: Python import matplotlib.pyplot as plt D = 0.1 zmin = np.min([np.min(zbed), -np.max(zbed)]) zmax = np.max([-np.min(zbed), np.max(zbed)]) zlevels = np.linspace(zmin, zmax, 51) plt.figure() plt.scatter(Xb[pbed]/D, Yb[pbed]/D, c=zbed, vmin=zmin, vmax=zmax, cmap = 'seismic') plt.colorbar() #Setting axis labels plt.xlabel('x/D') plt.ylabel('y/D') # add grid plt.grid() # show figure plt.show() .. image-sg:: /auto_examples/advanced/images/sphx_glr_plot_unstructured_simu_001.png :alt: plot unstructured simu :srcset: /auto_examples/advanced/images/sphx_glr_plot_unstructured_simu_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 97-100 save NetCDF files in constant and timeStep folders -------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 100-148 .. code-block:: Python if saveOutput == 1: from netCDF4 import Dataset postProcFile = os.path.join(sol, 'constant/pointerpostproc.nc') print ("save postprocFile in:",postProcFile) # NetCDF file creation rootgrp = Dataset(postProcFile, 'w') # Dimensions creation rootgrp.createDimension('XY', n2d) rootgrp.createDimension('Z', nz) # Variables creation pb_file = rootgrp.createVariable('pbed', np.int64, 'XY') x_file = rootgrp.createVariable('x', np.float64, 'XY') y_file = rootgrp.createVariable('y', np.float64, 'XY') z_file = rootgrp.createVariable('z', np.float64, ('XY','Z')) p_file = rootgrp.createVariable('p', np.int64, ('XY','Z')) # Writing variables pb_file[:] = pbed x_file[:] = Xb[pbed] y_file[:] = Yb[pbed] z_file[:,:] = Zb[pointer[:,:]] p_file[:,:] = pointer[:,:] # File closing rootgrp.close() postProcFile2 = os.path.join(sol, timeStep, 'zbed.nc') print ("save zbed file in:",postProcFile2) # NetCDF file creation rootgrp = Dataset(postProcFile2, 'w') # Dimensions creation rootgrp.createDimension('XY', n2d) rootgrp.createDimension('Z', nz) # Variables creation zb_file = rootgrp.createVariable('zbed', np.float64, 'XY') # Writing variables zb_file[:] = zbed # File closing rootgrp.close() .. rst-class:: sphx-glr-script-out .. code-block:: none save postprocFile in: ../../output_samples/3dscour/constant/pointerpostproc.nc save zbed file in: ../../output_samples/3dscour/0/zbed.nc .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 7.671 seconds) .. _sphx_glr_download_auto_examples_advanced_plot_unstructured_simu.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_unstructured_simu.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_unstructured_simu.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_unstructured_simu.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_