.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_1_timeseries.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_plot_1_timeseries.py: Time series of OpenFoam scalar field ==================================== This example reads and plots a time series of an OpenFoam scalar field .. GENERATED FROM PYTHON SOURCE LINES 9-13 Gets the time directories ------------------------- .. note:: Tries if directory is a number and adds it in the time array .. GENERATED FROM PYTHON SOURCE LINES 13-29 .. code-block:: Python import os import numpy as np sol = '../output_samples/box/' dir_list = os.listdir(sol) time_list = [] for directory in dir_list: try: float(directory) time_list.append(directory) except: pass time_list.sort(key=float) time_list=np.array(time_list) .. GENERATED FROM PYTHON SOURCE LINES 30-35 Reads a scalar value at a given position for different times ------------------------------------------------------------ .. note:: It reads the scalar field p at position 20 and stores it in the numpy array time_series .. GENERATED FROM PYTHON SOURCE LINES 35-47 .. code-block:: Python # import readvector function from fluidfoam package from fluidfoam import readscalar sol = '../output_samples/box/' time_series = np.empty(0) for timename in time_list: p = readscalar(sol, timename, 'p') time_series = np.append(time_series, p[20]) .. rst-class:: sphx-glr-script-out .. code-block:: none Reading file ../output_samples/box/0/p Reading file ../output_samples/box/1/p Reading file ../output_samples/box/2/p Reading file ../output_samples/box/3/p Reading file ../output_samples/box/4/p .. GENERATED FROM PYTHON SOURCE LINES 48-51 Now plots the time series ------------------------- .. GENERATED FROM PYTHON SOURCE LINES 51-66 .. code-block:: Python import matplotlib.pyplot as plt plt.figure() # Converts strings to float for plot time_list = [float(i) for i in time_list] plt.plot(time_list, time_series) # Setting axis labels plt.xlabel('t (s)') plt.ylabel('p (Pa)') # add grid plt.grid() .. image-sg:: /auto_examples/images/sphx_glr_plot_1_timeseries_001.png :alt: plot 1 timeseries :srcset: /auto_examples/images/sphx_glr_plot_1_timeseries_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.076 seconds) .. _sphx_glr_download_auto_examples_plot_1_timeseries.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_1_timeseries.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_1_timeseries.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_1_timeseries.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_