.. 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 :ref:`Go to the end ` 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 7-16 In this example, we present how to create the metamodel of a field function. This example considers the :ref:`free fall model `. We create the metamodel automatically using :class:`openturns.experimental.PointToFieldFunctionalChaosAlgorithm` and then also with a manual approach: 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 19-21 Define the model ---------------- .. GENERATED FROM PYTHON SOURCE LINES 23-29 .. code-block:: Python import openturns as ot import openturns.experimental as otexp import openturns.viewer as otv from openturns.usecases import viscous_free_fall .. GENERATED FROM PYTHON SOURCE LINES 30-31 Load the viscous free fall example. .. GENERATED FROM PYTHON SOURCE LINES 31-35 .. code-block:: Python vff = viscous_free_fall.ViscousFreeFall() distribution = vff.distribution model = vff.model .. GENERATED FROM PYTHON SOURCE LINES 36-37 Generate a training sample. .. GENERATED FROM PYTHON SOURCE LINES 37-41 .. code-block:: Python size = 2000 inputSample = distribution.getSample(size) outputSample = model(inputSample) .. GENERATED FROM PYTHON SOURCE LINES 42-43 Compute the global metamodel .. GENERATED FROM PYTHON SOURCE LINES 43-50 .. code-block:: Python algo = otexp.PointToFieldFunctionalChaosAlgorithm( inputSample, outputSample, distribution ) algo.run() result = algo.getResult() metaModel = result.getPointToFieldMetaModel() .. GENERATED FROM PYTHON SOURCE LINES 51-53 Validate the metamodel ---------------------- .. GENERATED FROM PYTHON SOURCE LINES 55-56 Create a validation sample. .. GENERATED FROM PYTHON SOURCE LINES 58-62 .. code-block:: Python size = 10 validationInputSample = distribution.getSample(size) validationOutputSample = model(validationInputSample) .. GENERATED FROM PYTHON SOURCE LINES 63-73 .. code-block:: Python 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 = otv.View(graph) .. image-sg:: /auto_meta_modeling/fields_metamodels/images/sphx_glr_plot_viscous_fall_metamodel_001.svg :alt: Model/metamodel comparison :srcset: /auto_meta_modeling/fields_metamodels/images/sphx_glr_plot_viscous_fall_metamodel_001.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 74-79 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. .. GENERATED FROM PYTHON SOURCE LINES 81-83 Sensitivity analysis -------------------- .. GENERATED FROM PYTHON SOURCE LINES 85-86 Compute the sensitivity indices .. GENERATED FROM PYTHON SOURCE LINES 86-90 .. code-block:: Python sensitivity = ot.FieldFunctionalChaosSobolIndices(result) s1 = sensitivity.getFirstOrderIndices() st = sensitivity.getTotalOrderIndices() .. GENERATED FROM PYTHON SOURCE LINES 91-93 We can notice that `v0` and `m` are the most influencial parameters and that there are almost no interactions (total indices being close to first order indices) .. GENERATED FROM PYTHON SOURCE LINES 93-95 .. code-block:: Python print(s1, st) .. rst-class:: sphx-glr-script-out .. code-block:: none [0.000533165,0.753033,0.237499,0.00217768] [0.00296975,0.757319,0.240718,0.00593206] .. GENERATED FROM PYTHON SOURCE LINES 96-97 Draw the sensitivity indices .. GENERATED FROM PYTHON SOURCE LINES 97-100 .. code-block:: Python graph = sensitivity.draw() view = otv.View(graph) .. image-sg:: /auto_meta_modeling/fields_metamodels/images/sphx_glr_plot_viscous_fall_metamodel_002.svg :alt: Sobol' indices :srcset: /auto_meta_modeling/fields_metamodels/images/sphx_glr_plot_viscous_fall_metamodel_002.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 101-103 Manual approach --------------- .. GENERATED FROM PYTHON SOURCE LINES 105-106 Step 1: compute the KL decomposition of the output .. GENERATED FROM PYTHON SOURCE LINES 108-113 .. code-block:: Python algo = ot.KarhunenLoeveSVDAlgorithm(outputSample, 1.0e-6) algo.run() klResult = algo.getResult() scaledModes = klResult.getScaledModesAsProcessSample() .. GENERATED FROM PYTHON SOURCE LINES 114-120 .. code-block:: Python graph = scaledModes.drawMarginal(0) graph.setTitle("KL modes") graph.setXTitle(r"$t$") graph.setYTitle(r"$z$") view = otv.View(graph) .. image-sg:: /auto_meta_modeling/fields_metamodels/images/sphx_glr_plot_viscous_fall_metamodel_003.svg :alt: KL modes :srcset: /auto_meta_modeling/fields_metamodels/images/sphx_glr_plot_viscous_fall_metamodel_003.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 121-122 We create the lifting function which takes coefficients of the Karhunen-Loève (KL) modes as inputs and returns the trajectories. .. GENERATED FROM PYTHON SOURCE LINES 122-124 .. code-block:: Python klLiftingFunction = ot.KarhunenLoeveLifting(klResult) .. GENERATED FROM PYTHON SOURCE LINES 125-126 The `project` method computes the projection of the output sample (i.e. the trajectories) onto the KL modes. .. GENERATED FROM PYTHON SOURCE LINES 126-128 .. code-block:: Python outputSampleChaos = klResult.project(outputSample) .. GENERATED FROM PYTHON SOURCE LINES 129-130 Step 2: compute the metamodel of the KL modes .. GENERATED FROM PYTHON SOURCE LINES 130-136 .. code-block:: Python # We create a polynomial chaos metamodel which takes the input sample and returns the KL modes. algo = ot.FunctionalChaosAlgorithm(inputSample, outputSampleChaos, distribution) algo.run() chaosMetamodel = algo.getResult().getMetaModel() .. GENERATED FROM PYTHON SOURCE LINES 137-139 The final metamodel is a composition of the KL lifting function and the polynomial chaos metamodel. We combine these two functions using the :class:`~openturns.PointToFieldConnection` class. .. GENERATED FROM PYTHON SOURCE LINES 141-143 .. code-block:: Python metaModel = ot.PointToFieldConnection(klLiftingFunction, chaosMetamodel) .. GENERATED FROM PYTHON SOURCE LINES 144-145 .. code-block:: Python otv.View.ShowAll() .. _sphx_glr_download_auto_meta_modeling_fields_metamodels_plot_viscous_fall_metamodel.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_viscous_fall_metamodel.ipynb ` .. 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-zip :download:`Download zipped: plot_viscous_fall_metamodel.zip `