.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_surrogate_modeling/fields_surrogate_models/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_surrogate_modeling_fields_surrogate_models_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.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-28 .. code-block:: Python import openturns as ot import openturns.viewer as otv from openturns.usecases import viscous_free_fall .. GENERATED FROM PYTHON SOURCE LINES 29-30 Load the viscous free fall example. .. GENERATED FROM PYTHON SOURCE LINES 30-34 .. code-block:: Python vff = viscous_free_fall.ViscousFreeFall() distribution = vff.distribution model = vff.model .. GENERATED FROM PYTHON SOURCE LINES 35-36 Generate a training sample. .. GENERATED FROM PYTHON SOURCE LINES 36-40 .. code-block:: Python size = 2000 inputSample = distribution.getSample(size) outputSample = model(inputSample) .. GENERATED FROM PYTHON SOURCE LINES 41-42 Compute the global metamodel .. GENERATED FROM PYTHON SOURCE LINES 42-49 .. code-block:: Python algo = ot.PointToFieldFunctionalChaosAlgorithm( inputSample, outputSample, distribution ) algo.run() result = algo.getResult() metaModel = result.getPointToFieldMetaModel() .. GENERATED FROM PYTHON SOURCE LINES 50-52 Validate the metamodel ---------------------- .. GENERATED FROM PYTHON SOURCE LINES 54-55 Create a validation sample. .. GENERATED FROM PYTHON SOURCE LINES 57-61 .. code-block:: Python size = 10 validationInputSample = distribution.getSample(size) validationOutputSample = model(validationInputSample) .. GENERATED FROM PYTHON SOURCE LINES 62-72 .. 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_surrogate_modeling/fields_surrogate_models/images/sphx_glr_plot_viscous_fall_metamodel_001.svg :alt: Model/metamodel comparison :srcset: /auto_surrogate_modeling/fields_surrogate_models/images/sphx_glr_plot_viscous_fall_metamodel_001.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 73-78 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 80-82 Sensitivity analysis -------------------- .. GENERATED FROM PYTHON SOURCE LINES 84-85 Compute the sensitivity indices .. GENERATED FROM PYTHON SOURCE LINES 85-89 .. code-block:: Python sensitivity = ot.FieldFunctionalChaosSobolIndices(result) s1 = sensitivity.getFirstOrderIndices() st = sensitivity.getTotalOrderIndices() .. GENERATED FROM PYTHON SOURCE LINES 90-92 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 92-94 .. code-block:: Python print(s1, st) .. rst-class:: sphx-glr-script-out .. code-block:: none [0.000485454,0.752422,0.238216,0.00218799] [0.0029582,0.756535,0.241556,0.00582136] .. GENERATED FROM PYTHON SOURCE LINES 95-96 Draw the sensitivity indices .. GENERATED FROM PYTHON SOURCE LINES 96-99 .. code-block:: Python graph = sensitivity.draw() view = otv.View(graph) .. image-sg:: /auto_surrogate_modeling/fields_surrogate_models/images/sphx_glr_plot_viscous_fall_metamodel_002.svg :alt: Sobol' indices :srcset: /auto_surrogate_modeling/fields_surrogate_models/images/sphx_glr_plot_viscous_fall_metamodel_002.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 100-102 Manual approach --------------- .. GENERATED FROM PYTHON SOURCE LINES 104-105 Step 1: compute the KL decomposition of the output .. GENERATED FROM PYTHON SOURCE LINES 107-112 .. code-block:: Python algo = ot.KarhunenLoeveSVDAlgorithm(outputSample, 1.0e-6) algo.run() klResult = algo.getResult() scaledModes = klResult.getScaledModesAsProcessSample() .. GENERATED FROM PYTHON SOURCE LINES 113-119 .. 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_surrogate_modeling/fields_surrogate_models/images/sphx_glr_plot_viscous_fall_metamodel_003.svg :alt: KL modes :srcset: /auto_surrogate_modeling/fields_surrogate_models/images/sphx_glr_plot_viscous_fall_metamodel_003.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 120-121 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 121-123 .. code-block:: Python klLiftingFunction = ot.KarhunenLoeveLifting(klResult) .. GENERATED FROM PYTHON SOURCE LINES 124-125 The `project` method computes the projection of the output sample (i.e. the trajectories) onto the KL modes. .. GENERATED FROM PYTHON SOURCE LINES 125-127 .. code-block:: Python outputSampleChaos = klResult.project(outputSample) .. GENERATED FROM PYTHON SOURCE LINES 128-129 Step 2: compute the metamodel of the KL modes .. GENERATED FROM PYTHON SOURCE LINES 129-135 .. 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 136-138 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 140-142 .. code-block:: Python metaModel = ot.PointToFieldConnection(klLiftingFunction, chaosMetamodel) .. GENERATED FROM PYTHON SOURCE LINES 143-144 .. code-block:: Python otv.View.ShowAll() .. _sphx_glr_download_auto_surrogate_modeling_fields_surrogate_models_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 `