.. 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 6-15 In this example, we present how to create the metamodel of a field function. This examples 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 18-20 Define the model ---------------- .. GENERATED FROM PYTHON SOURCE LINES 22-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 ot.Log.Show(ot.Log.NONE) .. 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-42 .. code-block:: Python size = 2000 ot.RandomGenerator.SetSeed(0) inputSample = distribution.getSample(size) outputSample = model(inputSample) .. GENERATED FROM PYTHON SOURCE LINES 43-44 Compute the global metamodel .. GENERATED FROM PYTHON SOURCE LINES 44-51 .. code-block:: Python algo = otexp.PointToFieldFunctionalChaosAlgorithm( inputSample, outputSample, distribution ) algo.run() result = algo.getResult() metaModel = result.getPointToFieldMetaModel() .. GENERATED FROM PYTHON SOURCE LINES 52-54 Validate the metamodel ---------------------- .. GENERATED FROM PYTHON SOURCE LINES 56-57 Create a validation sample. .. GENERATED FROM PYTHON SOURCE LINES 59-63 .. code-block:: Python size = 10 validationInputSample = distribution.getSample(size) validationOutputSample = model(validationInputSample) .. GENERATED FROM PYTHON SOURCE LINES 64-74 .. 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.png :alt: Model/metamodel comparison :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 75-80 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 82-84 Sensitivity analysis -------------------- .. GENERATED FROM PYTHON SOURCE LINES 86-87 Compute the sensitivity indices .. GENERATED FROM PYTHON SOURCE LINES 87-91 .. code-block:: Python sensitivity = otexp.FieldFunctionalChaosSobolIndices(result) s1 = sensitivity.getFirstOrderIndices() st = sensitivity.getTotalOrderIndices() .. GENERATED FROM PYTHON SOURCE LINES 92-94 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 94-96 .. code-block:: Python print(s1, st) .. rst-class:: sphx-glr-script-out .. code-block:: none [0.000544767,0.761005,0.229523,0.00219036] [0.00296842,0.76534,0.23269,0.00591793] .. GENERATED FROM PYTHON SOURCE LINES 97-98 Draw the sensitivity indices .. GENERATED FROM PYTHON SOURCE LINES 98-101 .. 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.png :alt: Sobol' indices :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 102-104 Manual approach --------------- .. GENERATED FROM PYTHON SOURCE LINES 106-107 Step 1: compute the KL decomposition of the output .. GENERATED FROM PYTHON SOURCE LINES 109-114 .. code-block:: Python algo = ot.KarhunenLoeveSVDAlgorithm(outputSample, 1.0e-6) algo.run() klResult = algo.getResult() scaledModes = klResult.getScaledModesAsProcessSample() .. GENERATED FROM PYTHON SOURCE LINES 115-121 .. 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.png :alt: KL modes :srcset: /auto_meta_modeling/fields_metamodels/images/sphx_glr_plot_viscous_fall_metamodel_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 122-123 We create the lifting function which takes coefficients of the K.-L. modes as inputs and returns the trajectories. .. GENERATED FROM PYTHON SOURCE LINES 123-125 .. code-block:: Python klLiftingFunction = ot.KarhunenLoeveLifting(klResult) .. GENERATED FROM PYTHON SOURCE LINES 126-127 The `project` method computes the projection of the output sample (i.e. the trajectories) onto the K.-L. modes. .. GENERATED FROM PYTHON SOURCE LINES 127-129 .. code-block:: Python outputSampleChaos = klResult.project(outputSample) .. GENERATED FROM PYTHON SOURCE LINES 130-131 step 2: compute the metamodel of the KL modes .. GENERATED FROM PYTHON SOURCE LINES 131-137 .. code-block:: Python # We create a polynomial chaos metamodel which takes the input sample and returns the K.-L. modes. algo = ot.FunctionalChaosAlgorithm(inputSample, outputSampleChaos, distribution) algo.run() chaosMetamodel = algo.getResult().getMetaModel() .. GENERATED FROM PYTHON SOURCE LINES 138-140 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 142-144 .. code-block:: Python metaModel = ot.PointToFieldConnection(klLiftingFunction, chaosMetamodel) .. GENERATED FROM PYTHON SOURCE LINES 145-146 Reset ResourceMap .. GENERATED FROM PYTHON SOURCE LINES 146-148 .. code-block:: Python ot.ResourceMap.Reload() .. GENERATED FROM PYTHON SOURCE LINES 149-150 .. 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 `