.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_functional_modeling/field_functions/plot_viscous_fall_field_function.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_functional_modeling_field_functions_plot_viscous_fall_field_function.py: Define a function with a field output: the viscous free fall example ==================================================================== .. GENERATED FROM PYTHON SOURCE LINES 6-9 In this example, we define a function which has a vector input and a field output. This is why we use the `PythonPointToFieldFunction` class to create the associated function and propagate the uncertainties through it. We consider a viscous free fall as explained :ref:`here `. .. GENERATED FROM PYTHON SOURCE LINES 11-13 Define the model ---------------- .. GENERATED FROM PYTHON SOURCE LINES 15-22 .. code-block:: default from __future__ import print_function import openturns as ot import openturns.viewer as viewer from matplotlib import pylab as plt import numpy as np ot.Log.Show(ot.Log.NONE) .. GENERATED FROM PYTHON SOURCE LINES 23-24 We first define the time grid associated with the model. .. GENERATED FROM PYTHON SOURCE LINES 26-31 .. code-block:: default tmin = 0.0 # Minimum time tmax = 12. # Maximum time gridsize = 100 # Number of time steps mesh = ot.IntervalMesher([gridsize-1]).build(ot.Interval(tmin, tmax)) .. GENERATED FROM PYTHON SOURCE LINES 32-33 The `getVertices` method returns the time values in this mesh. .. GENERATED FROM PYTHON SOURCE LINES 35-38 .. code-block:: default vertices = mesh.getVertices() vertices[0:5] .. raw:: html
v0
00
10.1212121
20.2424242
30.3636364
40.4848485


.. GENERATED FROM PYTHON SOURCE LINES 39-40 Creation of the input distribution. .. GENERATED FROM PYTHON SOURCE LINES 42-48 .. code-block:: default distZ0 = ot.Uniform(100.0, 150.0) distV0 = ot.Normal(55.0, 10.0) distM = ot.Normal(80.0, 8.0) distC = ot.Uniform(0.0, 30.0) distribution = ot.ComposedDistribution([distZ0, distV0, distM, distC]) .. GENERATED FROM PYTHON SOURCE LINES 49-53 .. code-block:: default dimension = distribution.getDimension() dimension .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 4 .. GENERATED FROM PYTHON SOURCE LINES 54-55 Then we define the Python function which computes the altitude at each time value. In order to compute all altitudes with a vectorized evaluation, we first convert the vertices into a `numpy` `array` and use the `numpy` function `exp` and `maximum`: this increases the evaluation performance of the script. .. GENERATED FROM PYTHON SOURCE LINES 57-71 .. code-block:: default def AltiFunc(X): g = 9.81 z0 = X[0] v0 = X[1] m = X[2] c = X[3] tau = m / c vinf = - m * g / c t = np.array(vertices) z = z0 + vinf * t + tau * (v0 - vinf) * (1 - np.exp(- t / tau)) z = np.maximum(z, 0.) return [[zeta[0]] for zeta in z] .. GENERATED FROM PYTHON SOURCE LINES 72-73 In order to create a `Function` from this Python function, we use the `PythonPointToFieldFunction` class. Since the altitude is the only output field, the third argument `outputDimension` is equal to `1`. If we had computed the speed as an extra output field, we would have set `2` instead. .. GENERATED FROM PYTHON SOURCE LINES 75-79 .. code-block:: default outputDimension = 1 alti = ot.PythonPointToFieldFunction( dimension, mesh, outputDimension, AltiFunc) .. GENERATED FROM PYTHON SOURCE LINES 80-82 Sample trajectories ------------------- .. GENERATED FROM PYTHON SOURCE LINES 84-85 In order to sample trajectories, we use the `getSample` method of the input distribution and apply the field function. .. GENERATED FROM PYTHON SOURCE LINES 87-91 .. code-block:: default size = 10 inputSample = distribution.getSample(size) outputSample = alti(inputSample) .. GENERATED FROM PYTHON SOURCE LINES 92-94 .. code-block:: default ot.ResourceMap.SetAsUnsignedInteger('Drawable-DefaultPalettePhase', size) .. GENERATED FROM PYTHON SOURCE LINES 95-96 Draw some curves. .. GENERATED FROM PYTHON SOURCE LINES 98-104 .. code-block:: default graph = outputSample.drawMarginal(0) graph.setTitle('Viscous free fall: %d trajectories' % (size)) graph.setXTitle(r'$t$') graph.setYTitle(r'$z$') view = viewer.View(graph) plt.show() .. image-sg:: /auto_functional_modeling/field_functions/images/sphx_glr_plot_viscous_fall_field_function_001.png :alt: Viscous free fall: 10 trajectories :srcset: /auto_functional_modeling/field_functions/images/sphx_glr_plot_viscous_fall_field_function_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 105-106 We see that the object first moves up and then falls down. Not all objects, however, achieve the same maximum altitude. We see that some trajectories reach a higher maximum altitude than others. Moreover, at the final time :math:`t_{max}`, one trajectory hits the ground: :math:`z(t_{max})=0` for this trajectory. .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.138 seconds) .. _sphx_glr_download_auto_functional_modeling_field_functions_plot_viscous_fall_field_function.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_viscous_fall_field_function.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_viscous_fall_field_function.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_