.. 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-30 .. 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 31-32 Load the viscous free fall example. .. GENERATED FROM PYTHON SOURCE LINES 32-36 .. code-block:: Python vff = viscous_free_fall.ViscousFreeFall() distribution = vff.distribution model = vff.model .. GENERATED FROM PYTHON SOURCE LINES 37-38 Generate a training sample. .. GENERATED FROM PYTHON SOURCE LINES 38-43 .. code-block:: Python size = 2000 ot.RandomGenerator.SetSeed(0) inputSample = distribution.getSample(size) outputSample = model(inputSample) .. GENERATED FROM PYTHON SOURCE LINES 44-45 Compute the global metamodel .. GENERATED FROM PYTHON SOURCE LINES 45-52 .. code-block:: Python algo = otexp.PointToFieldFunctionalChaosAlgorithm( inputSample, outputSample, distribution ) algo.run() result = algo.getResult() metaModel = result.getPointToFieldMetaModel() .. GENERATED FROM PYTHON SOURCE LINES 53-55 Validate the metamodel ---------------------- .. GENERATED FROM PYTHON SOURCE LINES 57-58 Create a validation sample. .. GENERATED FROM PYTHON SOURCE LINES 60-64 .. code-block:: Python size = 10 validationInputSample = distribution.getSample(size) validationOutputSample = model(validationInputSample) .. GENERATED FROM PYTHON SOURCE LINES 65-75 .. 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 76-81 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 83-85 Sensitivity analysis -------------------- .. GENERATED FROM PYTHON SOURCE LINES 87-88 Compute the sensitivity indices .. GENERATED FROM PYTHON SOURCE LINES 88-92 .. code-block:: Python sensitivity = otexp.FieldFunctionalChaosSobolIndices(result) s1 = sensitivity.getFirstOrderIndices() st = sensitivity.getTotalOrderIndices() .. GENERATED FROM PYTHON SOURCE LINES 93-95 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 95-97 .. code-block:: Python print(s1, st) .. rst-class:: sphx-glr-script-out .. code-block:: none [0.000551192,0.753887,0.236617,0.00216981] [0.00298947,0.758183,0.239842,0.00594157] .. GENERATED FROM PYTHON SOURCE LINES 98-99 Draw the sensitivity indices .. GENERATED FROM PYTHON SOURCE LINES 99-102 .. 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 103-105 Manual approach --------------- .. GENERATED FROM PYTHON SOURCE LINES 107-108 Step 1: compute the KL decomposition of the output .. GENERATED FROM PYTHON SOURCE LINES 110-115 .. code-block:: Python algo = ot.KarhunenLoeveSVDAlgorithm(outputSample, 1.0e-6) algo.run() klResult = algo.getResult() scaledModes = klResult.getScaledModesAsProcessSample() .. GENERATED FROM PYTHON SOURCE LINES 116-122 .. 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 123-124 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 124-126 .. code-block:: Python klLiftingFunction = ot.KarhunenLoeveLifting(klResult) .. GENERATED FROM PYTHON SOURCE LINES 127-128 The `project` method computes the projection of the output sample (i.e. the trajectories) onto the KL modes. .. GENERATED FROM PYTHON SOURCE LINES 128-130 .. code-block:: Python outputSampleChaos = klResult.project(outputSample) .. GENERATED FROM PYTHON SOURCE LINES 131-132 Step 2: compute the metamodel of the KL modes .. GENERATED FROM PYTHON SOURCE LINES 132-138 .. 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 139-141 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 143-145 .. code-block:: Python metaModel = ot.PointToFieldConnection(klLiftingFunction, chaosMetamodel) .. GENERATED FROM PYTHON SOURCE LINES 146-147 Reset ResourceMap .. GENERATED FROM PYTHON SOURCE LINES 147-149 .. code-block:: Python ot.ResourceMap.Reload() .. GENERATED FROM PYTHON SOURCE LINES 150-151 .. 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 `