.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_meta_modeling/fields_metamodels/plot_viscous_fall_metamodel.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_meta_modeling_fields_metamodels_plot_viscous_fall_metamodel.py: Viscous free fall: metamodel of a field function ================================================ .. GENERATED FROM PYTHON SOURCE LINES 6-7 In this example, we present how to create the metamodel of a field function. This examples considers the :ref:`free fall model `. We first compute the Karhunen-Loève decomposition of a sample of trajectories. Then we create a create a polynomial chaos which takes the inputs and returns the KL decomposition modes as outputs. Finally, we create a metamodel by combining the KL decomposition and the polynomial chaos. .. 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 numpy as np import openturns.viewer as viewer from matplotlib import pylab as plt 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. In order to compute all altitudes with a vectorized evaluation, we first convert the vertices into a Numpy `array` and use the Numpy functions`exp` and `maximum`: this increases the evaluation performance of the script. .. GENERATED FROM PYTHON SOURCE LINES 52-66 .. 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 67-68 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 70-74 .. code-block:: default outputDimension = 1 alti = ot.PythonPointToFieldFunction( dimension, mesh, outputDimension, AltiFunc) .. GENERATED FROM PYTHON SOURCE LINES 75-76 Compute a training sample. .. GENERATED FROM PYTHON SOURCE LINES 78-83 .. code-block:: default size = 2000 ot.RandomGenerator.SetSeed(0) inputSample = distribution.getSample(size) outputSample = alti(inputSample) .. GENERATED FROM PYTHON SOURCE LINES 84-86 Compute the KL decomposition of the output ------------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 88-93 .. code-block:: default algo = ot.KarhunenLoeveSVDAlgorithm(outputSample, 1.0e-6) algo.run() KLResult = algo.getResult() scaledModes = KLResult.getScaledModesAsProcessSample() .. GENERATED FROM PYTHON SOURCE LINES 94-100 .. code-block:: default graph = scaledModes.drawMarginal(0) graph.setTitle('KL modes') graph.setXTitle(r'$t$') graph.setYTitle(r'$z$') view = viewer.View(graph) .. image-sg:: /auto_meta_modeling/fields_metamodels/images/sphx_glr_plot_viscous_fall_metamodel_001.png :alt: KL modes :srcset: /auto_meta_modeling/fields_metamodels/images/sphx_glr_plot_viscous_fall_metamodel_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 101-102 We create the `postProcessingKL` function which takes coefficients of the the K.-L. modes as inputs and returns the trajectories. .. GENERATED FROM PYTHON SOURCE LINES 104-106 .. code-block:: default karhunenLoeveLiftingFunction = ot.KarhunenLoeveLifting(KLResult) .. GENERATED FROM PYTHON SOURCE LINES 107-108 The `project` method computes the projection of the output sample (i.e. the trajectories) onto the K.-L. modes. .. GENERATED FROM PYTHON SOURCE LINES 110-112 .. code-block:: default outputSampleChaos = KLResult.project(outputSample) .. GENERATED FROM PYTHON SOURCE LINES 113-114 We limit the sampling size of the Lilliefors selection in order to reduce the computational burden. .. GENERATED FROM PYTHON SOURCE LINES 116-119 .. code-block:: default ot.ResourceMap.SetAsUnsignedInteger( "FittingTest-LillieforsMaximumSamplingSize", 1) .. GENERATED FROM PYTHON SOURCE LINES 120-121 We create a polynomial chaos metamodel which takes the input sample and returns the K.-L. modes. .. GENERATED FROM PYTHON SOURCE LINES 123-127 .. code-block:: default algo = ot.FunctionalChaosAlgorithm(inputSample, outputSampleChaos) algo.run() chaosMetamodel = algo.getResult().getMetaModel() .. GENERATED FROM PYTHON SOURCE LINES 128-129 The final metamodel is a composition of the KL lifting function and the polynomial chaos metamodel. In order to combine these two functions, we use the `PointToFieldConnection` class. .. GENERATED FROM PYTHON SOURCE LINES 131-134 .. code-block:: default metaModel = ot.PointToFieldConnection( karhunenLoeveLiftingFunction, chaosMetamodel) .. GENERATED FROM PYTHON SOURCE LINES 135-137 Validate the metamodel ---------------------- .. GENERATED FROM PYTHON SOURCE LINES 139-140 Create a validation sample. .. GENERATED FROM PYTHON SOURCE LINES 142-146 .. code-block:: default size = 10 validationInputSample = distribution.getSample(size) validationOutputSample = alti(validationInputSample) .. GENERATED FROM PYTHON SOURCE LINES 147-158 .. code-block:: default graph = validationOutputSample.drawMarginal(0) graph.setColors(['red']) graph2 = metaModel(validationInputSample).drawMarginal(0) graph2.setColors(['blue']) graph.add(graph2) graph.setTitle('Model/metamodel comparison') graph.setXTitle(r'$t$') graph.setYTitle(r'$z$') view = viewer.View(graph) plt.show() .. image-sg:: /auto_meta_modeling/fields_metamodels/images/sphx_glr_plot_viscous_fall_metamodel_002.png :alt: Model/metamodel comparison :srcset: /auto_meta_modeling/fields_metamodels/images/sphx_glr_plot_viscous_fall_metamodel_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 159-160 We see that the blue trajectories (i.e. the metamodel) are close to the red trajectories (i.e. the validation sample). This shows that the metamodel is quite accurate. However, we observe that the trajectory singularity that occurs when the object touches the ground (i.e. when :math:`z` is equal to zero), makes the metamodel less accurate. .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 10.728 seconds) .. _sphx_glr_download_auto_meta_modeling_fields_metamodels_plot_viscous_fall_metamodel.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_metamodel.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_viscous_fall_metamodel.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_