.. 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-47 .. code-block:: Python algo = ot.PointToFieldFunctionalChaosAlgorithm(inputSample, outputSample, distribution) algo.run() result = algo.getResult() metaModel = result.getPointToFieldMetaModel() .. GENERATED FROM PYTHON SOURCE LINES 48-50 Validate the metamodel ---------------------- .. GENERATED FROM PYTHON SOURCE LINES 52-53 Create a validation sample. .. GENERATED FROM PYTHON SOURCE LINES 55-59 .. code-block:: Python size = 10 validationInputSample = distribution.getSample(size) validationOutputSample = model(validationInputSample) .. GENERATED FROM PYTHON SOURCE LINES 60-70 .. 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 71-76 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 78-80 Sensitivity analysis -------------------- .. GENERATED FROM PYTHON SOURCE LINES 82-83 Compute the sensitivity indices .. GENERATED FROM PYTHON SOURCE LINES 83-87 .. code-block:: Python sensitivity = ot.FieldFunctionalChaosSobolIndices(result) s1 = sensitivity.getFirstOrderIndices() st = sensitivity.getTotalOrderIndices() .. GENERATED FROM PYTHON SOURCE LINES 88-90 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 90-92 .. 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 93-94 Draw the sensitivity indices .. GENERATED FROM PYTHON SOURCE LINES 94-97 .. 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 98-100 Manual approach --------------- .. GENERATED FROM PYTHON SOURCE LINES 102-103 Step 1: compute the KL decomposition of the output .. GENERATED FROM PYTHON SOURCE LINES 105-110 .. code-block:: Python algo = ot.KarhunenLoeveSVDAlgorithm(outputSample, 1.0e-6) algo.run() klResult = algo.getResult() scaledModes = klResult.getScaledModesAsProcessSample() .. GENERATED FROM PYTHON SOURCE LINES 111-117 .. 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 118-119 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 119-121 .. code-block:: Python klLiftingFunction = ot.KarhunenLoeveLifting(klResult) .. GENERATED FROM PYTHON SOURCE LINES 122-123 The `project` method computes the projection of the output sample (i.e. the trajectories) onto the KL modes. .. GENERATED FROM PYTHON SOURCE LINES 123-125 .. code-block:: Python outputSampleChaos = klResult.project(outputSample) .. GENERATED FROM PYTHON SOURCE LINES 126-127 Step 2: compute the metamodel of the KL modes .. GENERATED FROM PYTHON SOURCE LINES 127-133 .. 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 134-136 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 138-140 .. code-block:: Python metaModel = ot.PointToFieldConnection(klLiftingFunction, chaosMetamodel) .. GENERATED FROM PYTHON SOURCE LINES 141-142 .. 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 `