dafi.random_field¶
Modules:
- dafi.random_field.covariance
- dafi.random_field.foam
foam_header_info()foam_header_logo()foam_sep()get_cell_centres()get_cell_volumes()get_neighbors()get_number_cells()read_cell_centres()read_cell_volumes()read_field()read_field_file()read_header()read_scalar_field()read_symmTensor_field()read_tensor_field()read_vector_field()write()write_Tau()write_U()write_controlDict()write_epsilon()write_field_file()write_k()write_nut()write_omega()write_p()
Random fields representation and manipulation.
These functions can be called directly from dafi.random_field, e.g.
>>> dafi.random_field.calc_kl_modes(*args)
- class dafi.random_field.field.GaussianProcess(klmodes, mean=None, weights=None, func=None, funcinv=None)[source]¶
Gaussian process class.
Also allows for the creation of a function of a Gaussian process. E.g. see ‘Lognormal’ class.
- __init__(klmodes, mean=None, weights=None, func=None, funcinv=None)[source]¶
Initialize Gaussian process class.
- Parameters:
klmodes (ndarray) – KL modes (eigenvectors). dtype=float, ndim=2, shape=(nstate, nmodes)
mean (ndarray) – Mean vector. Default zero (0). dtype=float, ndim=1, shape=(nstate)
weights (ndarray) – Weight (e.g. cell volume) associated with each state. Default ones (1). dtype=float, ndim=1, shape=(nstate)
func (function) – Function to create a random process that is a function of a Gaussian process. Default is identity function (GP).
funcinv (function) – Inverse of func.
- logpdf(coeffs)[source]¶
Logarithm of the probability density function.
log(PDF(x)) where x is a field (point in sample space) specified by KL coeffiecients.
- Parameters:
coeffs (ndarray) – Array of KL coefficients. dtype=float, ndim=2, shape=(nmodes, nsamples)
- Returns:
logpdf – Logarithm of the value of the PDF function for the given point in the sample space.
- Return type:
ndarray
- pdf(coeffs)[source]¶
Probaility density function (PDF).
PDF(x) where x is a field (point in sample space) specified by KL coeffiecients.
- Parameters:
coeffs (ndarray) – Array of KL coefficients. dtype=float, ndim=2, shape=(nmodes, nsamples)
- Returns:
pdf – Value of the PDF function for the given point in the sample space.
- Return type:
ndarray
- project_func_field(field, mean=None)[source]¶
Project a field from the function of the Gaussian process onto the KL modes.
- Parameters:
field (ndarray) – Scalar field. dtype=float, ndim=1, shape=(ncells)
mean (ndarray) – Mean vector. dtype=float, ndim=1, shape=(nstate)
- Returns:
coeffs – Projection magnitude. dtype=float, ndim=1, shape=(nmodes)
- Return type:
ndarray
- project_gp_field(field, mean=None)[source]¶
Project a field onto the KL modes.
- Parameters:
field (ndarray) – Scalar field. dtype=float, ndim=1, shape=(ncells)
mean (ndarray) – Mean vector. dtype=float, ndim=1, shape=(nstate)
- Returns:
coeffs – Projection magnitude. dtype=float, ndim=1, shape=(nmodes)
- Return type:
ndarray
- reconstruct_func(coeffs, mean=None)[source]¶
Reconstruct the function of the Gaussian process field from given KL coefficients.
- Parameters:
coeffs (ndarray) – Array of KL coefficients. dtype=float, ndim=2, shape=(nmodes, nsamples)
mean (ndarray) – Mean vector. If None, self.mean is used. dtype=float, ndim=1, shape=(nstate)
- Returns:
fields – Reconstructed fields. dtype=float, ndim=2, shape=(nstate, nsamples)
- Return type:
ndarray
- reconstruct_gp(coeffs, mean=None)[source]¶
Reconstruct the Gaussian process field from given KL coefficients.
- Parameters:
coeffs (ndarray) – Array of KL coefficients. dtype=float, ndim=2, shape=(nmodes, nsamples)
mean (ndarray) – Mean vector. If None, self.mean is used. dtype=float, ndim=1, shape=(nstate)
- Returns:
fields – Reconstructed fields. dtype=float, ndim=2, shape=(nstate, nsamples)
- Return type:
ndarray
- sample_coeffs(nsamples)[source]¶
Create Karhunen-Loève (KL) coefficents for random samples.
- Parameters:
nsamples (int) – Number of samples for which to generate KL coefficients.
- Returns:
coeffs – Matrix of samples KL coefficients for the Gaussian process. dtype=float, ndim=2, shape=(nstate, nsamples)
- Return type:
ndarray
- sample_func(nsamples, mean=None)[source]¶
Generate samples of the function of the Gaussian process.
- Parameters:
nsamples (int) – Number of samples to generate.
mean (ndarray) – Mean vector. If None, self.mean is used. dtype=float, ndim=1, shape=(nstate)
- Returns:
samples – Sample fields from the function of the Gaussian process. dtype=float, ndim=2, shape=(nstate, nsamples)
- Return type:
ndarray
- sample_gp(nsamples, mean=None)[source]¶
Generate samples of the Gaussian process.
- Parameters:
nsamples (int) – Number of samples to generate.
mean (ndarray) – Mean vector. If None, self.mean is used. dtype=float, ndim=1, shape=(nstate)
- Returns:
samples (ndarray) – Sample fields from Gaussian process. dtype=float, ndim=2, shape=(nstate, nsamples)
coeffs (ndarray) – Matrix of samples KL coefficients for the Gaussian process. dtype=float, ndim=2, shape=(nstate, nsamples)
- class dafi.random_field.field.LogNormal(klmodes_gp, median=1.0, weights=None)[source]¶
Log-normal process class.
- __init__(klmodes_gp, median=1.0, weights=None)[source]¶
Initialize log-normal process class.
- Parameters:
klmodes_gp (ndarray) – KL modes (eigenvectors) of the underlying Gaussian process. dtype=float, ndim=2, shape=(nstate, nmodes)
median (ndarray) – Median vector. Default one (1). dtype=float, ndim=1, shape=(nstate)
weights (ndarray) – Weight (e.g. cell volume) associated with each state. Default ones (1). dtype=float, ndim=1, shape=(nstate)
- dafi.random_field.field.calc_kl_modes(cov, nmodes=None, weight_field=None, eps=1e-08, normalize=True)[source]¶
Calculate the first N Karhunen-Loève modes for a covariance field.
Converts the covariance to a sparse matrix if it is not one yet.
- Parameters:
cov (ndarray) – Covariance matrix. Can be ndarray, matrix, or scipy sparse matrix. dtype=float, ndim=2, shape=(nstate, nstate)
nmodes (int) – Number of KL modes to obtain.
weight_field (ndarray) – Weight (e.g. cell volume) associated with each state. Default ones (1). dtype=float, ndim=1, shape=(nstate)
eps (float) – Small quantity to add to the diagonal of the covariance matrix for numerical stability.
normalize (bool) – Whether to normalize (norm = 1) the KL modes.
- Returns:
eig_vals (ndarray) – Eigenvalue associated with each mode. dtype=float, ndim=1, shape=(nmodes)
kl_modes (ndarray) – KL modes (eigenvectors). dtype=float, ndim=2, shape=(nstate, nmodes)
- dafi.random_field.field.calc_kl_modes_coverage(cov, coverage, weight_field=None, eps=1e-08, max_modes=None, normalize=True)[source]¶
Calculate all KL modes and return only those required to achieve a certain coverage of the variance.
- Parameters:
cov (ndarray) – Covariance matrix. Can be ndarray, matrix, or scipy sparse matrix. dtype=float, ndim=2, shape=(nstate, nstate)
coverage (float) – Desired percentage coverage of the variance. Value between 0-1.
weight_field (ndarray) – Weight (e.g. cell volume) associated with each state. Default ones (1). dtype=float, ndim=1, shape=(nstate)
eps (float) – Small quantity to add to the diagonal of the covariance matrix for numerical stability.
normalize (bool) – Whether to normalize (norm = 1) the KL modes.
- Returns:
eig_vals (ndarray) – Eigenvalue associated with each mode. For the first N modes such that the desired coverage of the variance is achieved. dtype=float, ndim=1, shape=(N)
kl_modes (ndarray) – first N KL modes (eigenvectors) such that the desired coverage of the variance is achieved. dtype=float, ndim=2, shape=(nstate, N)
- dafi.random_field.field.gp_samples_cholesky(cov, nsamples, mean=None, eps=1e-08)[source]¶
Generate samples of a Gaussian Process using Cholesky decomposition.
- Parameters:
cov (ndarray) – Covariance matrix. Can be ndarray, matrix, or scipy sparse matrix. dtype=float, ndim=2, shape=(nstate, nstate)
nsamples (int) – Number of samples to generate.
mean (ndarray) – Mean vector. dtype=float, ndim=1, shape=(nstate)
eps (float) – Small quantity to add to the diagonal of the covariance matrix for numerical stability.
- Returns:
samples – Matrix of samples. dtype=float, ndim=2, shape=(nstate, nsamples)
- Return type:
ndarray
- dafi.random_field.field.gp_samples_kl(cov, nsamples, weight_field, nmodes=None, mean=None, eps=1e-08)[source]¶
Generate samples of a Gaussian Process using KL decomposition.
- Parameters:
cov (ndarray) – Covariance matrix. Can be ndarray, matrix, or scipy sparse matrix. dtype=float, ndim=2, shape=(nstate, nstate)
nsamples (int) – Number of samples to generate.
weight_field (ndarray) – Weight (e.g. cell volume) associated with each state. dtype=float, ndim=1, shape=(nstate)
nmodes (int) – Number of modes to use when generating samples. ‘None’ to use all modes.
mean (ndarray) – Mean vector. dtype=float, ndim=1, shape=(nstate)
eps (float) – Small quantity to add to the diagonal of the covariance matrix for numerical stability.
- Returns:
samples – Matrix of samples. dtype=float, ndim=2, shape=(nstate, nsamples)
- Return type:
ndarray
- dafi.random_field.field.gp_samples_kl_coverage(cov, nsamples, weight_field, coverage=0.99, max_modes=None, mean=None, eps=1e-08)[source]¶
Generate samples of a Gaussian Process using KL decomposition.
Only the firs N modes required to get the desired variance coverage are used.
- Parameters:
cov (ndarray) – Covariance matrix. Can be ndarray, matrix, or scipy sparse matrix. dtype=float, ndim=2, shape=(nstate, nstate)
nsamples (int) – Number of samples to generate.
weight_field (ndarray) – Weight (e.g. cell volume) associated with each state. dtype=float, ndim=1, shape=(nstate)
coverage (float) – Desired percentage coverage of the variance. Value between 0-1.
max_modes (int) – Maximum number of modes used. This is the number of modes that is calculated. If less are needed to achieve the desired coverage the additional ones are discarded.
mean (ndarray) – Mean vector. dtype=float, ndim=1, shape=(nstate)
eps (float) – Small quantity to add to the diagonal of the covariance matrix for numerical stability.
- Returns:
samples (ndarray) – Matrix of samples. dtype=float, ndim=2, shape=(nstate, nsamples)
nmodes (int) – Number of modes used to achieve the requested coverage.
- dafi.random_field.field.gp_samples_klmodes(modes, nsamples, mean=None)[source]¶
Generate samples of a Gaussian Process using the given KL modes.
- Parameters:
modes (ndarray) – KL modes. dtype=float, ndim=2, shape=(nstate, nmodes)
nsamples (int) – Number of samples to generate.
mean (ndarray) – Mean vector. dtype=float, ndim=1, shape=(nstate)
- Returns:
samples – Matrix of samples. dtype=float, ndim=2, shape=(nstate, nsamples)
- Return type:
ndarray
- dafi.random_field.field.gp_sqrexp_samples(nsamples, coords, stddev, length_scales, mean=None, weight_field=None, max_modes=None)[source]¶
Generate samples from a Gaussian Process with square exponential correlation kernel.
This is a convinience function for new users or simple cases. It create the covariance matrix, does the KL decomposition, keeps the required modes for 99% coverage, and create the samples.
- Parameters:
nsamples (int) – Number of samples to generate.
coords (ndarray) – Array of coordinates. Each row correspond to a different point and the number of columns is the number of physical dimensions (e.g. 3 for (x,y,z)). dtype=float, ndim=2, shape=(npoints, ndims)
stddev (ndarray) – Standard deviation of each state. Alternatively, provide a float for a constant standard deviation. dtype=float, ndim=1, shape=(nstate)
length_scales (list) – Length scale for each physical dimensions. List length is ndims. Each entry is either a one dimensional ndarray of length nstate (length scale field) or a float (constant length scale).
mean (ndarray) – Mean vector. dtype=float, ndim=1, shape=(nstate)
weight_field (ndarray) – Weight (e.g. cell volume) associated with each state. dtype=float, ndim=1, shape=(nstate)
max_modes (int) – Maximum number of modes used. This is the number of modes that is calculated. If less are needed to achieve 99% coverage the additional ones are discarded.
- dafi.random_field.field.inner_product(field_1, field_2, weight_field)[source]¶
Calculate the inner product between two fields.
The two fields share the same weights.
- Parameters:
field_1 (ndarray) – One scalar field. dtype=float, ndim=1, shape=(ncells)
field_2 (ndarray) – Another scalar field. dtype=float, ndim=1, shape=(ncells)
weight_field (ndarray) – Cell volumes. dtype=float, ndim=1, shape=(ncells)
- Returns:
product – The inner product between the two fields.
- Return type:
float
- dafi.random_field.field.integral(field, weight_field)[source]¶
Calculate the integral of a field.
- Parameters:
field (ndarray) – Scalar field. dtype=float, ndim=1, shape=(ncells)
weight_field (ndarray) – Cell volumes. dtype=float, ndim=1, shape=(ncells)
- Returns:
field_integral – The integral of the field over the domain.
- Return type:
float
- dafi.random_field.field.interpolate_field_rbf(data, coords, kernel, length_scale)[source]¶
Interpolate data using a radial basis function (RBF) to create a field from sparse specifications.
This is used for instance to specify a variance field based on expert knowledge.
- Parameters:
data (ndarray) – Sparse data to create interpolation from. For an NxM array, the number of data points is N, the number of dimensions (coordinates) is M-1, and the Mth column is the data value. dtype=float, ndim=2, shape=(N, M)
coords (ndarray) – Coordinates of the cell centers of the full discretized field. The RBF will be evaluated at these points. dtype=float, ndim=2, shape=(ncells, M-1)
kernel (str) – Kernel (function) of the RBF. See ‘function’ input of scipy.interpolate.Rbf for list of options.
length_scale (float) – Length scale parameter (epsilon in scipy.interpolate.Rbf) in the RBF kernel.
- Returns:
field – Full field. dtype=float, ndim=1, shape=(ncells)
- Return type:
ndarray
- dafi.random_field.field.inverse_distance_weights(coords, connectivity, points, tol=1e-06)[source]¶
Create linear interpolation matrix (observation operatror H).
- dafi.random_field.field.kl_coverage(cov, eig_vals, weight_field=None)[source]¶
Calculate the percentage of the covariance covered by the the first N KL modes for N from 1-nmodes.
- Parameters:
cov (ndarray) – Covariance matrix. Can be ndarray, matrix, or scipy sparse matrix. dtype=float, ndim=2, shape=(nstate, nstate)
eig_vals (ndarray) – Eigenvalues associated with each mode. dtype=float, ndim=1, shape=(nmodes)
weight_field (ndarray) – Weight (e.g. cell volume) associated with each state. dtype=float, ndim=1, shape=(nstate)
- Returns:
coverage – Cumulative variance coverage of the first N modes. Each value is 0-1 and increasing. dtype=float, ndim=1, shape=(nmodes)
- Return type:
ndarray
- dafi.random_field.field.norm(field, weight_field)[source]¶
Calculate the L2-norm of a field.
- Parameters:
field (ndarray) – Scalar field. dtype=float, ndim=1, shape=(ncells)
weight_field (ndarray) – Cell volumes. dtype=float, ndim=1, shape=(ncells)
- Returns:
field_norm – The norm of the field.
- Return type:
float
- dafi.random_field.field.project_kl(field, modes, weight_field=None, mean=None)[source]¶
Project a field onto a set of modes.
- Parameters:
field (ndarray) – Scalar field. dtype=float, ndim=1, shape=(ncells)
modes (ndarray) – KL modes. dtype=float, ndim=2, shape=(nstate, nmodes)
weight_field (ndarray) – Weight (e.g. cell volume) associated with each state. dtype=float, ndim=1, shape=(nstate)
mean (ndarray) – Mean vector. dtype=float, ndim=1, shape=(nstate)
- Returns:
coeffs – Projection magnitude. dtype=float, ndim=1, shape=(nmodes)
- Return type:
ndarray
- dafi.random_field.field.projection(field_1, field_2, weight_field)[source]¶
Project field_1 onto field_2.
The two fields share the same weights.
- Parameters:
field_1 (ndarray) – Scalar field being projected. dtype=float, ndim=1, shape=(ncells)
field_2 (ndarray) – Scalar field used for projection direction. dtype=float, ndim=1, shape=(ncells)
weight_field (ndarray) – Cell volumes. dtype=float, ndim=1, shape=(ncells)
- Returns:
projected_field – Projected field. dtype=float, ndim=1, shape=(ncells)
- Return type:
ndarray
- dafi.random_field.field.projection_magnitude(field_1, field_2, weight_field)[source]¶
Get magnitude of projection of field_1 onto field_2.
The two fields share the same weights.
- Parameters:
field_1 (ndarray) – Scalar field being projected. dtype=float, ndim=1, shape=(ncells)
field_2 (ndarray) – Scalar field used for projection direction. dtype=float, ndim=1, shape=(ncells)
weight_field (ndarray) – Cell volumes. dtype=float, ndim=1, shape=(ncells)
- Returns:
magnitude – magnitude of the projected field.
- Return type:
float
- dafi.random_field.field.reconstruct_kl(modes, coeffs, mean=None)[source]¶
Reconstruct a field using KL modes and given coefficients.
Can create multiple fields by providing two dimensional array of coefficients.
- Parameters:
modes (ndarray) – KL modes. dtype=float, ndim=2, shape=(nstate, nmodes)
coeffs (ndarray) – Array of coefficients. dtype=float, ndim=2, shape=(nmodes, nsamples)
mean (ndarray) – Mean vector. dtype=float, ndim=1, shape=(nstate)
- Returns:
fields – Reconstructed fields. dtype=float, ndim=2, shape=(nstate, nsamples)
- Return type:
ndarray
- dafi.random_field.field.scale_kl_modes(eig_vals, kl_modes_norm)[source]¶
Weight the KL modes by the appropriate variance.
- Parameters:
eig_vals (ndarray) – Eigenvalue associated with each mode. dtype=float, ndim=1, shape=(nmodes)
kl_modes_norm (ndarray) – Normalized (norm = 1) KL modes (eigenvectors). dtype=float, ndim=2, shape=(nstate, nmodes)
- Returns:
kl_modes_weighted – KL modes with correct magnitude. dtype=float, ndim=2, shape=(nstate, nmodes)
- Return type:
ndarray
- dafi.random_field.field.sparse_cholesky(cov)[source]¶
Compute the Cholesky decomposition for a sparse (scipy) matrix.
Adapted from gist.github.com/omitakahiro.
- Parameters:
cov (ndarray) – Covariance matrix. Can be ndarray, matrix, or scipy sparse matrix. dtype=float, ndim=2, shape=(nstate, nstate)
- Returns:
lower – Lower triangular Cholesky factor of the covariance matrix.
- Return type:
scipy.sparse.csc_matrix
- dafi.random_field.field.unit_field(field, weight_field)[source]¶
Calculate the unit field (norm = 1) in same direction.
- Parameters:
field (ndarray) – Scalar field. dtype=float, ndim=1, shape=(ncells)
weight_field (ndarray) – Cell volumes. dtype=float, ndim=1, shape=(ncells)
- Returns:
field_normed – Normalized (norm = 1) scalar field. dtype=float, ndim=1, shape=(ncells)
- Return type:
ndarray