.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_stochastic_processes/plot_circulant_embedding_gaussian_process.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_circulant_embedding_gaussian_process.py: Circulant embedding Gaussian process and P1 interpolation ========================================================= This example illustrates the use of the :class:`~openturns.experimental.CirculantEmbeddingGaussianProcess` class to generate realizations of a stationary Gaussian process defined on a regular discretization of an interval, and the use of the :class:`~openturns.experimental.P1InterpolatedProcess` class to interpolate these realizations onto an unstructured mesh. The circulant embedding method relies on the fast Fourier transform, so very large regular grids can be sampled in a blink: this method is fast and generates exact simulations (see the Davies-Harte algorithm [davies1987]_, [wood1994]_). The P1 interpolation then allows one to evaluate the simulated trajectories at any location inside the simulation domain, whatever the mesh topology. .. GENERATED FROM PYTHON SOURCE LINES 21-29 .. code-block:: Python import numpy as np import openturns as ot import openturns.experimental as otexp import openturns.viewer as otv ot.Log.Show(ot.Log.NONE) ot.RandomGenerator.SetSeed(0) .. GENERATED FROM PYTHON SOURCE LINES 30-32 We first define the underlying covariance model: a Matérn model with smoothness :math:`\nu = 5/2`. .. GENERATED FROM PYTHON SOURCE LINES 34-36 .. code-block:: Python covarianceModel = ot.MaternModel([1.0, 1.0], [1.0], 2.5) .. GENERATED FROM PYTHON SOURCE LINES 37-40 The process is defined on the interval :math:`[-2, 2]^2`, meshed with an :class:`~openturns.IntervalMesher`: the regular grid required by the circulant embedding method is built automatically. .. GENERATED FROM PYTHON SOURCE LINES 42-47 .. code-block:: Python interval = ot.Interval([-2.0] * 2, [2.0] * 2) sourceMesh = ot.IntervalMesher([16, 16]).build(interval) process = otexp.CirculantEmbeddingGaussianProcess(covarianceModel, interval, [16, 16]) process.setMesh(sourceMesh) .. GENERATED FROM PYTHON SOURCE LINES 48-50 A single realization of the process is a random field over this grid. The circulant size actually used for the simulation is reported below. .. GENERATED FROM PYTHON SOURCE LINES 52-58 .. code-block:: Python field = process.getRealization() print("circular size:", process.getCircularSize()) graph = field.drawMarginal(0) graph.setTitle("Circulant embedding process on the interval mesh") view = otv.View(graph) .. image-sg:: /auto_stochastic_processes/images/sphx_glr_plot_circulant_embedding_gaussian_process_001.svg :alt: Circulant embedding process on the interval mesh :srcset: /auto_stochastic_processes/images/sphx_glr_plot_circulant_embedding_gaussian_process_001.svg :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none circular size: [64,64] .. GENERATED FROM PYTHON SOURCE LINES 59-62 We now define the target mesh: the unit disk. Since it is not regular, the circulant embedding method cannot be applied directly, but the P1 interpolation of the process can be evaluated on it. .. GENERATED FROM PYTHON SOURCE LINES 64-98 .. code-block:: Python def build_unit_disk_mesh(n_rings=8, n_theta=32): """Build a triangulated approximation of the unit disk.""" vertices = [[0.0, 0.0]] for r in range(1, n_rings + 1): radius = float(r) / n_rings for j in range(n_theta): theta = 2.0 * np.pi * j / n_theta vertices.append([radius * np.cos(theta), radius * np.sin(theta)]) simplices = [] for j in range(n_theta): j_next = (j + 1) % n_theta simplices.append([0, 1 + j, 1 + j_next]) for r in range(2, n_rings + 1): base_inner = 1 + (r - 2) * n_theta base_outer = 1 + (r - 1) * n_theta for j in range(n_theta): j_next = (j + 1) % n_theta a = base_inner + j b = base_outer + j c = base_outer + j_next d = base_inner + j_next simplices.append([a, b, c]) simplices.append([a, c, d]) return ot.Mesh(vertices, simplices) diskMesh = build_unit_disk_mesh() print( "unit disk mesh: %d vertices, %d simplices" % (diskMesh.getVerticesNumber(), diskMesh.getSimplicesNumber()) ) .. rst-class:: sphx-glr-script-out .. code-block:: none unit disk mesh: 257 vertices, 480 simplices .. GENERATED FROM PYTHON SOURCE LINES 99-101 The simulated process is then interpolated onto the unit disk, giving a realization whose statistical properties are preserved. .. GENERATED FROM PYTHON SOURCE LINES 103-109 .. code-block:: Python interpolatedProcess = otexp.P1InterpolatedProcess(process, diskMesh) realization = interpolatedProcess.getRealization() graph = realization.drawMarginal(0) graph.setTitle("P1-interpolated realization on the unit disk") view = otv.View(graph) .. image-sg:: /auto_stochastic_processes/images/sphx_glr_plot_circulant_embedding_gaussian_process_002.svg :alt: P1-interpolated realization on the unit disk :srcset: /auto_stochastic_processes/images/sphx_glr_plot_circulant_embedding_gaussian_process_002.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 110-118 Same-trajectory comparison -------------------------- The realization of the interpolated process is computed by P1 interpolation of the realization of the circulant embedding process. To compare the two fields on the disk, the state of the random generator has to be reset between the two calls to `getRealization`, so that both realizations correspond to the very same Gaussian trajectory. .. GENERATED FROM PYTHON SOURCE LINES 120-125 .. code-block:: Python ot.RandomGenerator.SetSeed(0) realization = interpolatedProcess.getRealization() ot.RandomGenerator.SetSeed(0) direct = process.getRealization() .. GENERATED FROM PYTHON SOURCE LINES 126-131 The reference field on the disk is obtained by interpolating the direct realization with the same P1 interpolation as the interpolated process. Since both realizations are built from the same trajectory with the same interpolation, the two fields coincide on the disk: the max residual is equal to 0 up to floating point precision. .. GENERATED FROM PYTHON SOURCE LINES 133-138 .. code-block:: Python interpolation = ot.P1LagrangeInterpolation(process.getMesh(), diskMesh, 1) reference = ot.Field(diskMesh, interpolation(direct.getValues())) residual = np.abs(np.asarray(reference.getValues() - realization.getValues())) print("max |residual| on the disk: %.3e" % residual.max()) .. rst-class:: sphx-glr-script-out .. code-block:: none max |residual| on the disk: 0.000e+00 .. GENERATED FROM PYTHON SOURCE LINES 139-142 The interpolated process and the direct P1 interpolation of the process realization are two consistent ways to evaluate the same Gaussian field on the disk. .. _sphx_glr_download_auto_stochastic_processes_plot_circulant_embedding_gaussian_process.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_circulant_embedding_gaussian_process.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_circulant_embedding_gaussian_process.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_circulant_embedding_gaussian_process.zip `