.. 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_connection.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_connection.py: Define a connection function with a field output ================================================ .. GENERATED FROM PYTHON SOURCE LINES 6-8 In this example, we define a function which has a vector input and a field output. The goal of this example is to show how to use the `PointToFieldConnection` to combine two functions. A detailed explanation of the model is presented :ref:`here `. .. GENERATED FROM PYTHON SOURCE LINES 10-12 Define the model ---------------- .. GENERATED FROM PYTHON SOURCE LINES 14-21 .. 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 22-23 We first define the time grid associated with the model. .. GENERATED FROM PYTHON SOURCE LINES 25-30 .. 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 31-33 .. code-block:: default vertices = mesh.getVertices() .. GENERATED FROM PYTHON SOURCE LINES 34-35 Creation of the input distribution. .. GENERATED FROM PYTHON SOURCE LINES 37-43 .. 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 44-48 .. code-block:: default dimension = distribution.getDimension() dimension .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 4 .. GENERATED FROM PYTHON SOURCE LINES 49-50 Then we define the Python function which computes the altitude at each time value. This function has 5 inputs: `z0`, `v0`, `m`, `c` and `zmin`. .. GENERATED FROM PYTHON SOURCE LINES 52-67 .. code-block:: default def AltiFunc(X): g = 9.81 z0 = X[0] v0 = X[1] m = X[2] c = X[3] zmin = X[4] 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, zmin) return [[zeta[0]] for zeta in z] .. GENERATED FROM PYTHON SOURCE LINES 68-72 .. code-block:: default outputDimension = 1 altitudeWithFiveInputs = ot.PythonPointToFieldFunction( 5, mesh, outputDimension, AltiFunc) .. GENERATED FROM PYTHON SOURCE LINES 73-75 Restrict the number of inputs ----------------------------- .. GENERATED FROM PYTHON SOURCE LINES 77-78 We define a function which has 4 inputs and 5 outputs: the 5th ouput `zmin` is set to zero. .. GENERATED FROM PYTHON SOURCE LINES 80-83 .. code-block:: default projectionFunction = ot.SymbolicFunction( ["z0", "v0", "m", "c"], ["z0", "v0", "m", "c", "0.0"]) .. GENERATED FROM PYTHON SOURCE LINES 84-85 Then we use the `PointToFieldConnection` to create a function which has 4 inputs and returns the output field. .. GENERATED FROM PYTHON SOURCE LINES 87-90 .. code-block:: default altitudeWithFourInputs = ot.PointToFieldConnection( altitudeWithFiveInputs, projectionFunction) .. GENERATED FROM PYTHON SOURCE LINES 91-93 Sample trajectories -------------------- .. GENERATED FROM PYTHON SOURCE LINES 95-96 In order to sample trajectories, we use the `getSample` method of the input distribution and apply the field function. .. GENERATED FROM PYTHON SOURCE LINES 98-102 .. code-block:: default size = 10 inputSample = distribution.getSample(size) outputSample = altitudeWithFourInputs(inputSample) .. GENERATED FROM PYTHON SOURCE LINES 103-104 Draw some curves. .. GENERATED FROM PYTHON SOURCE LINES 106-112 .. 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_connection_001.png :alt: Viscous free fall: 10 trajectories :srcset: /auto_functional_modeling/field_functions/images/sphx_glr_plot_viscous_fall_field_function_connection_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.129 seconds) .. _sphx_glr_download_auto_functional_modeling_field_functions_plot_viscous_fall_field_function_connection.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_connection.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_viscous_fall_field_function_connection.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_