.. only:: html
.. note::
:class: sphx-glr-download-link-note
Click :ref:`here ` to download the full example code
.. rst-class:: sphx-glr-example-title
.. _sphx_glr_auto_probabilistic_modeling_stochastic_processes_plot_field_manipulation.py:
Field manipulation
==================
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:`(\underline{t}_0, \dots, \underline{t}_{N-1})` the vertices of :math:`\mathcal{M}` and :math:`(\underline{x}_0, \dots, \underline{x}_{N-1})` the associated values in :math:`\mathbb{R}^d`.
A field is stored in the *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.
.. code-block:: default
from __future__ import print_function
import openturns as ot
import openturns.viewer as viewer
from matplotlib import pylab as plt
import math as m
ot.Log.Show(ot.Log.NONE)
First, define a regular 2-d mesh
.. code-block:: default
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 = viewer.View(graph)
.. image:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_field_manipulation_001.png
:alt: Regular 2-d mesh
:class: sphx-glr-single-img
Create a field as a realization of a process
.. code-block:: default
amplitude = [1.0]
scale = [0.2]*2
myCovModel = ot.ExponentialModel(scale, amplitude)
myProcess = ot.GaussianProcess(myCovModel, mesh)
field = myProcess.getRealization()
Create a field from a mesh and some values
.. code-block:: default
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 = viewer.View(graph)
.. image:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_field_manipulation_002.png
:alt: Field on 2-d mesh and 2-d values
:class: sphx-glr-single-img
Compute the input mean of the field
.. code-block:: default
field.getInputMean()
.. raw:: html
[-0.00840808,0.00576205]
Draw the field without interpolation
.. code-block:: default
graph = field.drawMarginal(0, False)
graph.setTitle('Marginal field (no interpolation)')
view = viewer.View(graph)
.. image:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_field_manipulation_003.png
:alt: Marginal field (no interpolation)
:class: sphx-glr-single-img
Draw the field with interpolation
.. code-block:: default
graph = field.drawMarginal(0)
graph.setTitle('Marginal field (with interpolation)')
view = viewer.View(graph)
.. image:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_field_manipulation_004.png
:alt: Marginal field (with interpolation)
:class: sphx-glr-single-img
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 (ie its values)
.. code-block:: default
graph = field.asDeformedMesh().draw()
graph.setTitle('Deformed 2-d mesh')
view = viewer.View(graph)
.. image:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_field_manipulation_005.png
:alt: Deformed 2-d mesh
:class: sphx-glr-single-img
Export to the VTK format
.. code-block:: default
field.exportToVTKFile('field.vtk')
with open('field.vtk') as f:
print(f.read()[:100])
plt.show()
.. rst-class:: sphx-glr-script-out
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.
.. rst-class:: sphx-glr-timing
**Total running time of the script:** ( 0 minutes 1.774 seconds)
.. _sphx_glr_download_auto_probabilistic_modeling_stochastic_processes_plot_field_manipulation.py:
.. only :: html
.. container:: sphx-glr-footer
:class: sphx-glr-footer-example
.. container:: sphx-glr-download sphx-glr-download-python
:download:`Download Python source code: plot_field_manipulation.py `
.. container:: sphx-glr-download sphx-glr-download-jupyter
:download:`Download Jupyter notebook: plot_field_manipulation.ipynb `
.. only:: html
.. rst-class:: sphx-glr-signature
`Gallery generated by Sphinx-Gallery `_