.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_stochastic_processes/plot_field_manipulation.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_stochastic_processes_plot_field_manipulation.py: Draw a field ============ .. GENERATED FROM PYTHON SOURCE LINES 7-17 The objective here is to create and manipulate a field. A field is the agregation of a mesh :math:`\mathcal{M}` of a domain :math:`\mathcal{D} \in \mathbb{R}^n` and a sample of values in :math:`\mathbb{R}^d` associated to each vertex of the mesh. We note :math:`(\vect{t}_0, \dots, \vect{t}_{N-1})` the vertices of :math:`\mathcal{M}` and :math:`(\vect{x}_0, \dots, \vect{x}_{N-1})` the associated values in :math:`\mathbb{R}^d`. A field is stored in the :class:`~openturns.Field` object that stores the mesh and the values at each vertex of the mesh. It can be built from a mesh and values or as a realization of a stochastic process. sphinx_gallery_thumbnail_number = 6 .. GENERATED FROM PYTHON SOURCE LINES 19-22 .. code-block:: Python import openturns as ot import openturns.viewer as otv .. GENERATED FROM PYTHON SOURCE LINES 23-24 First, define a regular 2-d mesh .. GENERATED FROM PYTHON SOURCE LINES 24-34 .. code-block:: Python discretization = [10, 5] mesher = ot.IntervalMesher(discretization) lowerBound = [0.0, 0.0] upperBound = [2.0, 1.0] interval = ot.Interval(lowerBound, upperBound) mesh = mesher.build(interval) graph = mesh.draw() graph.setTitle("Regular 2-d mesh") view = otv.View(graph) .. image-sg:: /auto_stochastic_processes/images/sphx_glr_plot_field_manipulation_001.svg :alt: Regular 2-d mesh :srcset: /auto_stochastic_processes/images/sphx_glr_plot_field_manipulation_001.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 35-36 Create a field as a realization of a process .. GENERATED FROM PYTHON SOURCE LINES 36-42 .. code-block:: Python amplitude = [1.0] scale = [0.2] * 2 myCovModel = ot.ExponentialModel(scale, amplitude) myProcess = ot.GaussianProcess(myCovModel, mesh) field = myProcess.getRealization() .. GENERATED FROM PYTHON SOURCE LINES 43-44 Create a field from a mesh and some values .. GENERATED FROM PYTHON SOURCE LINES 44-55 .. code-block:: Python values = ot.Normal([0.0] * 2, [1.0] * 2, ot.CorrelationMatrix(2)).getSample( len(mesh.getVertices()) ) for i in range(len(values)): x = values[i] values[i] = 0.05 * x / x.norm() field = ot.Field(mesh, values) graph = field.draw() graph.setTitle("Field on 2-d mesh and 2-d values") view = otv.View(graph) .. image-sg:: /auto_stochastic_processes/images/sphx_glr_plot_field_manipulation_002.svg :alt: Field on 2-d mesh and 2-d values :srcset: /auto_stochastic_processes/images/sphx_glr_plot_field_manipulation_002.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 56-57 Compute the input mean of the field .. GENERATED FROM PYTHON SOURCE LINES 57-59 .. code-block:: Python field.getInputMean() .. raw:: html
class=Point name=Unnamed dimension=2 values=[0.00125386,-0.0031112]


.. GENERATED FROM PYTHON SOURCE LINES 60-61 Draw the field without interpolation .. GENERATED FROM PYTHON SOURCE LINES 61-65 .. code-block:: Python graph = field.drawMarginal(0, False) graph.setTitle("Marginal field (no interpolation)") view = otv.View(graph) .. image-sg:: /auto_stochastic_processes/images/sphx_glr_plot_field_manipulation_003.svg :alt: Marginal field (no interpolation) :srcset: /auto_stochastic_processes/images/sphx_glr_plot_field_manipulation_003.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 66-68 Draw the field with interpolation sphinx_gallery_thumbnail_number = 4 .. GENERATED FROM PYTHON SOURCE LINES 68-72 .. code-block:: Python graph = field.drawMarginal(0) graph.setTitle("Marginal field (with interpolation)") view = otv.View(graph) .. image-sg:: /auto_stochastic_processes/images/sphx_glr_plot_field_manipulation_004.svg :alt: Marginal field (with interpolation) :srcset: /auto_stochastic_processes/images/sphx_glr_plot_field_manipulation_004.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 73-75 Deform the mesh from the field according to the values of the field The dimension of the mesh (ie of its vertices) must be the same as the dimension of the field (i.e., its values) .. GENERATED FROM PYTHON SOURCE LINES 75-79 .. code-block:: Python graph = field.asDeformedMesh().draw() graph.setTitle("Deformed 2-d mesh") view = otv.View(graph) .. image-sg:: /auto_stochastic_processes/images/sphx_glr_plot_field_manipulation_005.svg :alt: Deformed 2-d mesh :srcset: /auto_stochastic_processes/images/sphx_glr_plot_field_manipulation_005.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 80-81 Export to the VTK format .. GENERATED FROM PYTHON SOURCE LINES 81-85 .. code-block:: Python field.exportToVTKFile("field.vtk") with open("field.vtk") as f: print(f.read()[:100]) .. rst-class:: sphx-glr-script-out .. code-block:: none # vtk DataFile Version 3.0 Unnamed ASCII DATASET UNSTRUCTURED_GRID POINTS 66 float 0 0 0.0 0.2 0 0. .. GENERATED FROM PYTHON SOURCE LINES 86-87 Display all figures .. GENERATED FROM PYTHON SOURCE LINES 87-88 .. code-block:: Python otv.View.ShowAll() .. _sphx_glr_download_auto_stochastic_processes_plot_field_manipulation.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_field_manipulation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_field_manipulation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_field_manipulation.zip `