Skip to content

Commit

Permalink
added example file and documentation for the method along with anothe…
Browse files Browse the repository at this point in the history
…r test for KLE 2d code
  • Loading branch information
lohitv96 committed Apr 21, 2022
1 parent c179ed8 commit 07e34fc
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
"""
Karhunen Loeve Expansion 2 Dimesional
=================================================================
In this example, the KL Expansion is used to generate 2 dimensional stochastic fields from a prescribed Autocorrelation
Function. This example illustrates how to use the :class:`.KarhunenLoeveExpansion2D` class for a 2 dimensional
random field and compare the statistics of the generated random field with the expected values.
"""

# %% md
#
# Import the necessary libraries. Here we import standard libraries such as numpy and matplotlib, but also need to
# import the :class:`.KarhunenLoeveExpansionTwoDimension` class from the :py:mod:`stochastic_processes` module of UQpy.

# %%
from matplotlib import pyplot as plt
from UQpy.stochastic_process import KarhunenLoeveExpansion2D
import numpy as np

plt.style.use('seaborn')

# %% md
#
# The input parameters necessary for the generation of the stochastic processes are given below:

# %%

n_samples = 100 # Num of samples
nx, nt = 20, 10
dx, dt = 0.05, 0.1

x = np.linspace(0, (nx - 1) * dx, nx)
t = np.linspace(0, (nt - 1) * dt, nt)
xt_list = np.meshgrid(x, x, t, t, indexing='ij') # R(t_1, t_2, x_1, x_2)

# %% md

# Defining the Autocorrelation Function.

# %%

R = np.exp(-(xt_list[0] - xt_list[1]) ** 2 - (xt_list[2] - xt_list[3]) ** 2)
# R(x_1, x_2, t_1, t_2) = exp(-(x_1 - x_2) ** 2 -(t_1 - t_2) ** 2)

KLE_Object = KarhunenLoeveExpansion2D(n_samples=n_samples, correlation_function=R, time_intervals=np.array([dt, dx]),
thresholds=[4, 5], random_state=128)

# %% md

# Simulating the samples.

# %%

samples = KLE_Object.samples

# %% md

# Plotting a sample of the stochastic field.

# %%

fig = plt.figure()
plt.title('Realisation of the Karhunen Loeve Expansion for a 2D stochastic field')
plt.plot(samples[0, 0])
plt.xlabel('t (Time)')
plt.ylabel('x (Space)')
plt.show()

print('The mean of the samples is ', np.mean(samples), 'whereas the expected mean is 0.000')
print('The variance of the samples is ', np.var(samples), 'whereas the expected variance is 1.000')
32 changes: 32 additions & 0 deletions docs/source/stochastic_process/karhunen_loeve_2d.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
Karhunen Loève Expansion for Multi-Dimensional Fields
----------------------------

The Karhunen Loève Expansion expands the stochastic field as follows:

.. math:: A(x, t) = \sum_{n=1}^{\infty} \sum_{k=1}^{\infty}\eta_{nk}(\theta) \sqrt{\lambda_n(x)} f_n(t, x) \sqrt{\mu_{nk}} g_{nk}(x)

where :math:`\eta_{nk}(\theta)` are uncorrelated standardized normal random variables and :math:`\lambda_n(x)` and :math:`f_n(x, t)` are the eigenvalues and eigenvectors repsectively of the "quasi" one dimensional covariance function :math:`C(x, t_1, t_2)`. :math:`\mu_{nk}` and :math:`g_{nk}(x)` are the eigenvalues and eigenvectors of the derived "one" dimensional covariance function :math:`H(x_1, x_2)`. Additional details regarding the simulation formula can be found at https://www.sciencedirect.com/science/article/abs/pii/S0045782516318692.

KarhunenLoeve2D Class
^^^^^^^^^^^^^^^^^^^^

The :class:`.KarhunenLoeve2D` class is imported using the following command:

>>> from UQpy.stochastic_process.KarhunenLoeveExpansionTwoDimension2D import KarhunenLoeveExpansion

Methods
"""""""
.. autoclass:: UQpy.stochastic_process.KarhunenLoeveExpansion2D
:members: run

Attributes
""""""""""
.. autoattribute:: UQpy.stochastic_process.KarhunenLoeveExpansion2D.samples
.. autoattribute:: UQpy.stochastic_process.KarhunenLoeveExpansion2D.xi

Examples
""""""""""

.. toctree::

Karhunen Loeve Examples <../auto_examples/stochastic_processes/karhunen_loeve_2d/index>
4 changes: 2 additions & 2 deletions src/UQpy/stochastic_process/KarhunenLoeveExpansion2D.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def run(self, n_samples, random_variables=None):

if self.samples is None:
self.samples = samples
self.random_variables = random_variables
self.xi = random_variables
else:
self.samples = np.concatenate((self.samples, samples), axis=0)
self.random_variables = np.concatenate((self.random_variables, random_variables), axis=2)
self.xi = np.concatenate((self.xi, random_variables), axis=2)
6 changes: 6 additions & 0 deletions tests/unit_tests/stochastic_process/test_karhunen_loeve_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,9 @@ def test_samples_shape():

def test_samples_values():
assert np.isclose(samples[13, 0, 13, 6], -1.4385717324103362, rtol=0.01)


def test_run_method():
nsamples_second_run = 100
KLE_Object.run(n_samples=nsamples_second_run)
assert samples.shape == (n_samples + nsamples_second_run, 1, len(x), len(t))

0 comments on commit 07e34fc

Please sign in to comment.