.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_functional_modeling/field_functions/plot_process_sample.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_functional_modeling_field_functions_plot_process_sample.py: Create a process sample from a sample ===================================== .. GENERATED FROM PYTHON SOURCE LINES 5-7 .. code-block:: Python # sphinx_gallery_thumbnail_number = 3 .. GENERATED FROM PYTHON SOURCE LINES 10-24 In this example, a :class:`~openturns.ProcessSample` is created from data. The purpose is to illustrate how to create a :class:`~openturns.ProcessSample` from an already available sample of a field. In addition, the computation of statistics of the process sample is presented. The :class:`~openturns.ProcessSample` is defined over a :class:`~openturns.Mesh`. For this example, the realizations of a stochastic process are obtained using the Chaboche model. A detailed explanation of this mechanical law is presented :ref:`here `. In this example, the model is defined such that: :math:`\sigma=g(\vect{X},\epsilon)` with :math:`\vect{X}=[R,C,\gamma]^T` a vector of random variables and - the strain :math:`\epsilon\in[0.,0.07]` - the stress :math:`\sigma` .. GENERATED FROM PYTHON SOURCE LINES 26-32 .. code-block:: Python import openturns as ot import openturns.viewer as viewer from openturns.usecases import chaboche_model ot.Log.Show(ot.Log.NONE) .. GENERATED FROM PYTHON SOURCE LINES 33-35 Define the model and the mesh ----------------------------- .. GENERATED FROM PYTHON SOURCE LINES 37-38 First, the Chaboche model is loaded. .. GENERATED FROM PYTHON SOURCE LINES 40-42 .. code-block:: Python cm = chaboche_model.ChabocheModel() .. GENERATED FROM PYTHON SOURCE LINES 43-44 Then, a mesh using a :class:`~openturns.RegularGrid` is defined for the strain :math:`\epsilon\in[0.,0.07]` with :math:`N=100` vertices. .. GENERATED FROM PYTHON SOURCE LINES 46-50 .. code-block:: Python N = 100 mesh = ot.RegularGrid(cm.strainMin, (cm.strainMax - cm.strainMin) / N, N) vertices = mesh.getVertices() .. GENERATED FROM PYTHON SOURCE LINES 51-53 Generate one sample of the field -------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 55-57 One sample of the field is obtained by evaluating the Chaboche model on the mesh vertices for one specific realization of the input vector :math:`\vect{X}_0=[700\times 10^6,2500\times 10^6,8.]^T`. .. GENERATED FROM PYTHON SOURCE LINES 59-67 .. code-block:: Python R = 700e6 C = 2500e6 gamma = 8.0 X0 = [R, C, gamma] X_indices = [1, 2, 3] f = ot.ParametricFunction(cm.model, X_indices, X0) Y = f(vertices) .. GENERATED FROM PYTHON SOURCE LINES 68-69 Let's visualize this sample of the field. .. GENERATED FROM PYTHON SOURCE LINES 71-78 .. code-block:: Python graph = ot.Graph( "One realization of the stochastic process", "Strain", "Stress (Pa)", True, "" ) curve = ot.Curve(vertices, Y) graph.add(curve) view = viewer.View(graph) .. image-sg:: /auto_functional_modeling/field_functions/images/sphx_glr_plot_process_sample_001.png :alt: One realization of the stochastic process :srcset: /auto_functional_modeling/field_functions/images/sphx_glr_plot_process_sample_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 79-80 The distribution of the input vector :math:`\vect{X}` is defined here: .. GENERATED FROM PYTHON SOURCE LINES 80-86 .. code-block:: Python param_R = ot.LogNormalMuSigma(750e6, 11e6, 0.0) dist_R = ot.ParametrizedDistribution(param_R) dist_C = ot.Normal(2750e6, 250e6) dist_gamma = ot.Normal(10, 2) X_distribution = ot.JointDistribution([dist_R, dist_C, dist_gamma]) .. GENERATED FROM PYTHON SOURCE LINES 87-89 Generate different samples of the field --------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 91-92 50 samples of the input vector :math:`\vect{X}` are generated. .. GENERATED FROM PYTHON SOURCE LINES 94-97 .. code-block:: Python n_samples = 50 X_samples = X_distribution.getSample(n_samples) .. GENERATED FROM PYTHON SOURCE LINES 98-99 The Chaboche model is evaluated for each of the input vector samples. .. GENERATED FROM PYTHON SOURCE LINES 101-107 .. code-block:: Python Y_list = [] for i in range(n_samples): f = ot.ParametricFunction(cm.model, X_indices, X_samples[i, :]) Y = f(vertices) Y_list.append(Y) .. GENERATED FROM PYTHON SOURCE LINES 108-109 Let's visualize the 50 resulting samples of the field. .. GENERATED FROM PYTHON SOURCE LINES 111-120 .. code-block:: Python graph = ot.Graph( "Realizations of the stochastic process", "Strain", "Stress (Pa)", True, "" ) for i in range(n_samples): curve = ot.Curve(vertices, Y_list[i]) curve.setColor("blue") graph.add(curve) view = viewer.View(graph) .. image-sg:: /auto_functional_modeling/field_functions/images/sphx_glr_plot_process_sample_002.png :alt: Realizations of the stochastic process :srcset: /auto_functional_modeling/field_functions/images/sphx_glr_plot_process_sample_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 121-123 Creation of the process sample ------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 125-129 It is possible to create a :class:`~openturns.ProcessSample` from the obtained field samples. For that, each obtained sample is added to the :class:`~openturns.ProcessSample` using the :class:`~openturns.Field` structure. When the :class:`~openturns.ProcessSample` is created, by default a process sample with a value of 0. for all the vertices is stored so it is important to remove it. .. GENERATED FROM PYTHON SOURCE LINES 131-136 .. code-block:: Python process_sample = ot.ProcessSample(mesh, n_samples, 1) process_sample.clear() for i in range(n_samples): process_sample.add(ot.Field(mesh, Y_list[i])) .. GENERATED FROM PYTHON SOURCE LINES 137-138 It is then possible to compute different statistics on the :class:`~openturns.ProcessSample` such as the mean, median, variance, ... .. GENERATED FROM PYTHON SOURCE LINES 140-144 .. code-block:: Python process_sample_mean = process_sample.computeMean() process_sample_median = process_sample.computeMedian() process_sample_variance = process_sample.computeVariance() .. GENERATED FROM PYTHON SOURCE LINES 145-146 Let's visualize the mean of the process sample. .. GENERATED FROM PYTHON SOURCE LINES 148-166 .. code-block:: Python graph = ot.Graph( "Sample process mean and realizations", "Strain", "Stress (Pa)", True, "" ) for i in range(n_samples): if i == 0: label = "process samples" else: label = "" curve = ot.Curve(vertices, Y_list[i], label) curve.setColor("blue") graph.add(curve) curve = ot.Curve(vertices, process_sample_mean, "process sample mean") curve.setColor("red") graph.add(curve) graph.setLegendPosition("topleft") view = viewer.View(graph) .. image-sg:: /auto_functional_modeling/field_functions/images/sphx_glr_plot_process_sample_003.png :alt: Sample process mean and realizations :srcset: /auto_functional_modeling/field_functions/images/sphx_glr_plot_process_sample_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 167-168 Display all figures .. GENERATED FROM PYTHON SOURCE LINES 170-171 .. code-block:: Python viewer.View.ShowAll() .. _sphx_glr_download_auto_functional_modeling_field_functions_plot_process_sample.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_process_sample.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_process_sample.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_process_sample.zip `