pyforce.offline package

Submodules

pyforce.offline.eim module

class pyforce.offline.eim.EIM(grid, gdim=3, varname='u', **kwargs)[source]

Bases: OfflineDDROM

A class implementing the Empirical Interpolation Method (EIM) for dimensionality reduction and sensor placement.

Parameters:
  • grid (pv.UnstructuredGrid) – The computational grid.

  • gdim (int, optional (default=3)) – The geometric dimension of the problem. It can be either 2 or 3.

  • varname (str, optional (default='u')) – The name of the variable to analyse. Default is ‘u’.

compute_errors(snaps: FunctionsList | ndarray, Mmax: int | None = None, verbose: bool = False)[source]

Computes the errors between the original snapshots and the reconstructed ones.

Parameters:
  • snaps (FunctionsList or np.ndarray) – Original snapshots to compare with.

  • Mmax (int, optional) – Maximum number of sensors and magic functions to use for the reconstruction. If None, all reduced basis is used.

  • verbose (bool, optional) – If True, print progress messages. Default is False.

Returns:

  • mean_abs_err (np.ndarray) – Average absolute error measured in \(L^2\).

  • mean_rel_err (np.ndarray) – Average relative error measured in \(L^2\).

  • computational_time (dict) – Dictionary with the CPU time of the most relevant operations during the online phase.

compute_lebesgue_constant()[source]

This method computes the Lebesgue constant associated with the selected magic points.

The procedure exploits the Lagrange basis functions, depicted in Tiglio and Villanueva (2021).

Returns:

Lambda – The Lebesgue constant.

Return type:

float

fit(train_snaps: FunctionsList, Mmax: int, _xm_idx: list | None = None, verbose=False)[source]

The greedy algorithm chooses the magic functions and magic points by minimising the reconstruction error.

Parameters:
  • train_snaps (FunctionsList) – List of snapshots serving as training set.

  • Mmax (int) – Integer input indicating the maximum number of functions and sensors to define

  • _xm_idx (list, optional (default = None)) – User-defined available indices for the points (sensors), if None the indices are all mesh elements.

  • verbose (boolean, optional (Default = False)) – If True, print of the progress is enabled.

Returns:

  • maxAbsErr (np.ndarray) – Maximum absolute error measured in \(L^\infty\) (max norm)

  • maxRelErr (np.ndarray) – Maximum relative error measured in \(L^\infty\) (max norm)

  • beta_coeff (np.ndarray) – Matrix of the reduced coefficients \(\{ \beta_m \}_{m=1}^M\), obtained by greedy procedure

reconstruct(measures: ndarray)[source]

This method reconstructs the state by obtaining the reduced coefficients \(\{\beta_m\}_{m=1}^M\) from the input measures \(\{y_m\}_{m=1}^M\) by solving the EIM linear system

\[\mathbb{B} \boldsymbol{\beta} = \mathbf{y}\]

where - \(\mathbb{B}\) is the matrix of magic functions - \(\boldsymbol{\beta}\) are the reduced coefficients - \(\mathbf{y}\) are the input measures

Parameters:

measures (np.ndarray) – Input measures \(\{y_m\}_{m=1}^M\), shaped \((M,N_s)\).

Returns:

u – Reconstructed functions.

Return type:

FunctionsList

reduce(snaps: FunctionsList | ndarray, M: int | None = None)[source]

This method can be used to generate the reduced coefficients based on the magic functions and points, by extracting the measures from the input functions and solve the associated EIM linear system.

Parameters:
  • snaps (FunctionsList or np.ndarray) – Function object to project onto the reduced space of dimension M.

  • M (int, optional (Default = None)) – Number of magic functions/points to use for the projection. If None, all available magic functions/points are used.

Returns:

beta_coeff – Reduced coefficients \(\{\beta_m\}_{m=1}^M\), shaped \((M,N_s)\).

Return type:

np.ndarray

save(path_folder: str, **kwargs)[source]

Save the magic functions and points to a specified path.

Parameters:
  • path_folder (str) – The folder path where the model will be saved.

  • **kwargs (dict) – Additional keyword arguments for saving options.

pyforce.offline.eim.deim(basis_functions: FunctionsList, Mmax: int, varname: str = 'u', _xm_idx: list | None = None, path_folder: str | None = None)[source]

This function implements the Discrete Empirical Interpolation Method (DEIM) for selecting magic points from a given set of basis functions.

Parameters:
  • basis_functions (FunctionsList) – The list of basis functions from which to select magic points.

  • Mmax (int) – The maximum number of magic points to select.

  • varname (str, optional) – The name of the variable for which the magic points are being selected. Default is ‘u’.

  • _xm_idx (list, optional) – A list of indices to consider for magic point selection. If None, all indices are considered.

  • path_folder (str, optional) – The folder path where the selected magic points will be saved. If None, the points are simply returned.

Returns:

  • magic_pt (np.ndarray) – An array of selected magic point indices.

  • P (np.ndarray) – The DEIM selection matrix (observation operator).

pyforce.offline.geim module

class pyforce.offline.geim.GEIM(grid, gdim=3, varname='u', sensors_type='Gaussian')[source]

Bases: OfflineDDROM

A class implementing the Generalised Empirical Interpolation Method (GEIM) for dimensionality reduction and sensor placement.

Parameters:
  • grid (pv.UnstructuredGrid) – The computational grid.

  • gdim (int, optional (default=3)) – The geometric dimension of the problem. It can be either 2 or 3.

  • varname (str, optional (default='u')) – The name of the variable to analyse. Default is ‘u’.

  • sensors_type (str, optional (default='Gaussian')) – The type of sensors to use in the GEIM algorithm. It can be either ‘Gaussian’, ‘Exponential’, or ‘IndicatorFunction’.

compute_errors(snaps: FunctionsList | ndarray, Mmax: int | None = None, verbose: bool = False)[source]

Computes the errors between the original snapshots and the reconstructed ones.

Parameters:
  • snaps (FunctionsList or np.ndarray) – Original snapshots to compare with.

  • Mmax (int, optional) – Maximum number of sensors and magic functions to use for the reconstruction. If None, all reduced basis is used.

  • verbose (bool, optional) – If True, print progress messages. Default is False.

Returns:

  • mean_abs_err (np.ndarray) – Average absolute error measured in \(L^2\).

  • mean_rel_err (np.ndarray) – Average relative error measured in \(L^2\).

  • computational_time (dict) – Dictionary with the CPU time of the most relevant operations during the online phase.

compute_lebesgue_constant(verbose: bool = False)[source]

This method computes the Lebesgue constant based on the magic functions and sensors, based on the procedure outlined in Maday et al., 2015.

Returns:

Lambda – The Lebesgue constant.

Return type:

float

fit(train_snaps: FunctionsList, Mmax: int, sensor_params: dict, verbose=False)[source]

The greedy algorithm chooses the magic functions and magic points by minimising the reconstruction error.

Parameters:
  • train_snaps (FunctionsList) – List of snapshots serving as training set.

  • Mmax (int) – Integer input indicating the maximum number of functions and sensors to define

  • sensor_params (dict) – Dictionary containing the parameters for the sensor library creation.

  • verbose (boolean, optional (Default = False)) – If True, print of the progress is enabled.

Returns:

  • maxAbsErr (np.ndarray) – Maximum absolute error measured in \(L^\infty\) (max norm)

  • maxRelErr (np.ndarray) – Maximum relative error measured in \(L^\infty\) (max norm)

  • beta_coeff (np.ndarray) – Matrix of the reduced coefficients \(\{ \beta_m \}_{m=1}^M\), obtained by greedy procedure

plot_sensors(M: int | None = None, view='xy', cmap='jet', levels=50, color_sens='black', fontsize_sens=10, fig_length=6, show_ticks=False)[source]

Plot the Riesz representation of the first M sensors, cumulated:

\[G(\mathbf{x}) = \sum_{m=1}^M \frac{g_m(\mathbf{x})}{\max |g_m|}\]
Parameters:
  • M (int, optional (Default = None)) – Number of sensors to plot. If None, all available sensors are plotted.

  • view (str, optional (Default = 'xy')) – View of the plot. It can be either ‘xy’, ‘xz’ or ‘yz’.

  • cmap (str, optional (Default = 'jet')) – Colormap to use for the plot.

  • levels (int, optional (Default = 50)) – Number of levels to use for the contour plot.

  • color_sens (str, optional (Default = 'black')) – Color of the sensor labels.

  • fontsize_sens (int, optional (Default = 10)) – Font size of the sensor labels.

  • fig_length (float, optional (Default = 6)) – Length of the figure in inches.

  • show_ticks (bool, optional (Default = False)) – If True, show the ticks on the axes.

reconstruct(measures: ndarray)[source]

This method reconstructs the state by obtaining the reduced coefficients \(\{\beta_m\}_{m=1}^M\) from the input measures \(\{y_m\}_{m=1}^M\) by solving the EIM linear system

\[\mathbb{B} \boldsymbol{\beta} = \mathbf{y}\]

where - \(\mathbb{B}\) is the matrix of magic functions - \(\boldsymbol{\beta}\) are the reduced coefficients - \(\mathbf{y}\) are the input measures

Parameters:

measures (np.ndarray) – Input measures \(\{y_m\}_{m=1}^M\), shaped \((M,N_s)\).

Returns:

u – Reconstructed functions.

Return type:

FunctionsList

reduce(snaps: FunctionsList | ndarray, M: int | None = None)[source]

This method can be used to generate the reduced coefficients based on the magic functions and points, by extracting the measures from the input functions and solve the associated EIM linear system.

Parameters:
  • snaps (FunctionsList or np.ndarray) – Function object to project onto the reduced space of dimension M.

  • M (int, optional (Default = None)) – Number of magic functions/points to use for the projection. If None, all available magic functions/points are used.

Returns:

beta_coeff – Reduced coefficients \(\{\beta_m\}_{m=1}^M\), shaped \((M,N_s)\).

Return type:

np.ndarray

save(path_folder: str, **kwargs)[source]

Save the magic functions and sensors to a specified path.

Parameters:
  • path_folder (str) – The folder path where the model will be saved.

  • **kwargs (dict) – Additional keyword arguments for saving options.

pyforce.offline.offline_base module

Module for offline base class of offline phase in pyforce library. This module provides the base class for the offline phase of the pyforce library.

class pyforce.offline.offline_base.OfflineDDROM(grid, gdim=3, varname='u', **kwargs)[source]

Bases: ABC

Abstract base class for the offline phase of the pyforce library.

This class defines the interface for the offline phase, which is responsible for preparing data and models before the online phase begins.

abstract fit(train_snaps: FunctionsList, **kwargs)[source]

Abstract method to fit the model using training snapshots.

abstract reconstruct(**kwargs)[source]

Abstract method to reconstruct the state for a given input.

abstract reduce(**kwargs)[source]

Abstract method to reduce the dimensionality of the data and obtain the latent dynamics.

abstract save(path: str, **kwargs)[source]

Abstract method to save the model to a specified path.

pyforce.offline.pod module

class pyforce.offline.pod.HierarchicalSVD(grid, gdim=3, varname='u', **kwargs)[source]

Bases: rSVD

A class to perform hierarchical Singular Value Decomposition (hSVD) from Iwen and Ong (2016). This class inherits from the rSVD class and extends its functionality to perform hSVD and update the SVD modes accordingly. This method is particularly useful for large datasets where a hierarchical approach can improve computational efficiency, especially when dealing with parametric problems.

Parameters:
  • grid (pyvista.UnstructuredGrid) – The grid on which the hSVD is performed. It is used to define the spatial domain of the snapshots.

  • gdim (int, optional (Default = 3)) – The geometric dimension of the grid. It can be either 2 or 3.

  • varname (str, optional (default='u')) – The name of the variable to be used for the hSVD. Default is ‘u’.

update(train_snaps: FunctionsList | None = None, new_modes: FunctionsList | None = None, new_sing_vals: ndarray | None = None, rank: int | None = None, **kwargs)[source]

This method updates the SVD modes and singular values using the hierarchical SVD approach, either using new training snapshots or directly providing new modes and singular values.

Parameters:
  • train_snaps (FunctionsList, optional) – New training snapshots used to compute the new SVD modes and singular values.

  • new_modes (FunctionsList, optional) – New SVD modes to be used for the update.

  • new_sing_vals (np.ndarray, optional) – New singular values to be used for the update.

class pyforce.offline.pod.IncrementalSVD(grid, gdim=3, varname='u', **kwargs)[source]

Bases: rSVD

A class to perform incremental Singular Value Decomposition (iSVD) from Brand (2002). This class inherits from the rSVD class and extends its functionality to perform iSVD and update the SVD modes accordingly. This method is particularly useful for streaming data or when new snapshots become available over time.

Parameters:
  • grid (pyvista.UnstructuredGrid) – The grid on which the iSVD is performed. It is used to define the spatial domain of the snapshots.

  • gdim (int, optional (Default = 3)) – The geometric dimension of the grid. It can be either 2 or 3.

  • varname (str, optional (default='u')) – The name of the variable to be used for the iSVD. Default is ‘u’.

fit(train_snaps: FunctionsList, rank: int, verbose: bool = False, **kwargs)[source]

This method is used to perform the randomized SVD on the training snapshots.

Parameters:
  • train_snaps (FunctionsList) – The training snapshots used to compute the rSVD modes.

  • rank (int) – The rank for the truncated SVD.

  • verbose (bool, optional) – If True, print progress messages. Default is False.

  • kwargs (dict, optional) – Additional keyword arguments for the SVD solver.

update(new_snap: FunctionsList | ndarray)[source]

This method updates the SVD modes and singular values using the incremental SVD approach with a new snapshot.

class pyforce.offline.pod.POD(grid, gdim=3, varname='u', **kwargs)[source]

Bases: OfflineDDROM

A class to perform the POD on a list of snapshots \(u(\mathbf{x};\,\boldsymbol{\mu})\) dependent on some parameter \(\boldsymbol{\mu}\in\mathcal{P}\subset\mathbb{R}^p\).

Parameters:
  • grid (pyvista.UnstructuredGrid) – The grid on which the POD is performed. It is used to define the spatial domain of the snapshots.

  • gdim (int, optional (Default = 3)) – The geometric dimension of the grid. It can be either 2 or 3.

  • varname (str, optional) – The name of the variable to be used for the POD. Default is ‘u’.

compute_basis(train_snaps: FunctionsList, rank: int, normalise: bool = False)[source]

Computes the POD modes.

To enforce the orthonormality in \(L^2\), the Gram-Schmidt procedure can be used, if the number of modes to be used is high the numerical error in the eigendecomposition may be too large and the orthonormality is lost.

Parameters:
  • train_snap (FunctionsList) – List of snapshots onto which the POD is performed.

  • rank (int) – Integer input indicating the number of modes to define.

  • normalise (boolean, optional (Default = False)) – If True, the Gram-Schmidt procedure is used to normalise the POD modes.

compute_errors(snaps: FunctionsList | ndarray, Nmax: int | None = None, verbose: bool = False)[source]

Computes the errors between the original snapshots and the reconstructed ones.

Parameters:
  • snaps (FunctionsList or np.ndarray) – Original snapshots to compare with.

  • Nmax (int, optional) – Maximum number of modes to use for the reconstruction. If None, all modes are used.

  • verbose (bool, optional) – If True, print progress messages. Default is False.

Returns:

  • mean_abs_err (np.ndarray) – Average absolute error measured in \(L^2\).

  • mean_rel_err (np.ndarray) – Average relative error measured in \(L^2\).

  • computational_time (dict) – Dictionary with the CPU time of the most relevant operations during the online phase.

fit(train_snaps: FunctionsList, verbose: bool = False)[source]

This method is used to obtain the eigendecomposition of the correlation matrix \(C\in\mathbb{R}^{N_s\times N_s}\) from a list of snapshots as FunctionsList.

\[C_{ij} = \left(u(\cdot;\,\boldsymbol{\mu}_i),\,u(\cdot;\,\boldsymbol{\mu}_j)\right)_{L^2}\qquad i,j = 1, \dots, N_s\]
\[C \boldsymbol{\eta_n} = \lambda_n \boldsymbol{\eta_n}\qquad\qquad\qquad n = 1, \dots, N_s\]

The eigenvalues \(\lambda_n\) and eigenvectors \(\boldsymbol{\eta_n}\) are then computed.

Parameters:
  • train_snaps (FunctionsList) – The training snapshots used to compute the POD modes.

  • verbose (bool, optional) – If True, print progress messages. Default is False.

gram_schmidt(fun: ndarray)[source]

Perform a step of the Gram-Schmidt process on POD modes \(\{\psi_k\}_{k=1}^r\) adding fun \(=f\) to enforce the orthonormality in \(L^2\)

\[\psi_{r+1} = f - \sum_{k=1}^r \frac{(f, \psi_k)_{L^2}}{(\psi_k, \psi_k)_{L^2}}\psi_k\]
Parameters:

fun (np.ndarray) – Function to add to the POD basis.

Returns:

normalised_fun – Orthonormalised function \(\psi_{r+1}\) with respect to the POD basis \(\{\psi_k\}_{k=1}^r\).

Return type:

np.ndarray

plot_eigenvalues()[source]

Simple method to plot the eigenvalues of the POD modes: in linear scale, residual and cumulative energy.

Returns:

fig – The figure containing the plots of the eigenvalues, residual energy, and cumulative energy.

Return type:

matplotlib.figure.Figure

reconstruct(coeffs: ndarray)[source]

This method reconstructs the function u from the reduced coefficients \(\{\alpha_k\}_{k=1}^N\) using the POD modes \(\{\psi_k\}_{k=1}^N\).

\[u(\cdot;\,\boldsymbol{\mu}) = \sum_{k=1}^N \alpha_k(\boldsymbol{\mu}) \psi_k(\cdot)\]
Parameters:

coeffs (np.ndarray) – Reduced coefficients \(\{\alpha_k\}_{k=1}^N\), shaped \((N,N_s)\).

Returns:

u – Reconstructed functions.

Return type:

FunctionsList

reduce(snaps: FunctionsList | ndarray, N: int | None = None)[source]

The reduced coefficients \(\{\alpha_k\}_{k=1}^N\) of the snapshots using N modes \(\{\psi_k\}_{k=1}^N\) are computed using projection in \(L_2\).

Parameters:
  • snaps (FunctionsList or np.ndarray) – Function object to project onto the reduced space of dimension N.

  • N (int) – Dimension of the reduced space, modes to be used.

Returns:

coeff – Modal POD coefficients of u, \(\{\alpha_k\}_{k=1}^N\).

Return type:

np.ndarray

save(path_folder: str, **kwargs)[source]

Save the POD modes and the eigenvalues to a specified path.

Parameters:
  • path_folder (str) – The folder path where the model will be saved.

  • **kwargs (dict) – Additional keyword arguments for saving options.

class pyforce.offline.pod.rSVD(grid, gdim=3, varname='u', **kwargs)[source]

Bases: OfflineDDROM

A class to perform randomized Singular Value Decomposition (rSVD) on a list of snapshots \(u(\mathbf{x};\,\boldsymbol{\mu})\) dependent on some parameter \(\boldsymbol{\mu}\in\mathcal{P}\subset\mathbb{R}^p\).

Parameters:
  • grid (pyvista.UnstructuredGrid) – The grid on which the rSVD is performed. It is used to define the spatial domain of the snapshots.

  • gdim (int, optional (Default = 3)) – The geometric dimension of the grid. It can be either 2 or 3.

  • varname (str, optional (default='u')) – The name of the variable to be used for the rSVD. Default is ‘u’.

compute_errors(snaps: FunctionsList | ndarray, Nmax: int | None = None, verbose: bool = False)[source]

Computes the errors between the original snapshots and the reconstructed ones.

Parameters:
  • snaps (FunctionsList or np.ndarray) – Original snapshots to compare with.

  • Nmax (int, optional) – Maximum number of modes to use for the reconstruction. If None, all modes are used.

  • verbose (bool, optional) – If True, print progress messages. Default is False.

Returns:

  • mean_abs_err (np.ndarray) – Average absolute error measured in \(L^2\).

  • mean_rel_err (np.ndarray) – Average relative error measured in \(L^2\).

  • computational_time (dict) – Dictionary with the CPU time of the most relevant operations during the online phase.

fit(train_snaps: FunctionsList, rank: int, verbose: bool = False, **kwargs)[source]

This method is used to perform the randomized SVD on the training snapshots.

Parameters:
  • train_snaps (FunctionsList) – The training snapshots used to compute the rSVD modes.

  • rank (int) – The rank for the truncated SVD.

  • verbose (bool, optional) – If True, print progress messages. Default is False.

  • kwargs (dict, optional) – Additional keyword arguments for the SVD solver.

plot_sing_vals()[source]

Simple method to plot the eigenvalues of the POD modes: in linear scale, residual and cumulative energy.

Returns:

fig – The figure containing the plots of the eigenvalues, residual energy, and cumulative energy.

Return type:

matplotlib.figure.Figure

reconstruct(coeffs: ndarray)[source]

This method reconstructs the function u from the reduced coefficients \(\{\alpha_k\}_{k=1}^N\) using the POD modes \(\{\psi_k\}_{k=1}^N\).

\[u(\cdot;\,\boldsymbol{\mu}) = \sum_{k=1}^N \alpha_k(\boldsymbol{\mu}) \psi_k(\cdot)\]
Parameters:

coeffs (np.ndarray) – Reduced coefficients \(\{\alpha_k\}_{k=1}^N\), shaped \((N,N_s)\).

Returns:

u – Reconstructed functions.

Return type:

FunctionsList

reduce(snaps: FunctionsList | ndarray, N: int | None = None)[source]

The reduced coefficients \(\{\alpha_k\}_{k=1}^N\) of the snapshots using N modes \(\{\psi_k\}_{k=1}^N\) are computed using projection in \(l_2\).

Parameters:
  • snaps (FunctionsList or np.ndarray) – Function object to project onto the reduced space of dimension N.

  • N (int) – Dimension of the reduced space, modes to be used.

Returns:

coeff – Modal SVD coefficients of u, \(\{\alpha_k\}_{k=1}^N\).

Return type:

np.ndarray

save(path_folder: str, **kwargs)[source]

Save the SVD modes and the singular values to a specified path.

Parameters:
  • path_folder (str) – The folder path where the model will be saved.

  • **kwargs (dict) – Additional keyword arguments for saving options.

pyforce.offline.sensors module

class pyforce.offline.sensors.ExponentialSensorLibrary(grid: pyvista.UnstructuredGrid, use_centroids: bool = True, gdim: int = 3)[source]

Bases: SensorLibraryBase

A class implementing a library of Exponential sensors.

An Exponential sensor is mathematically modelled as a linear functional, with an Exponential kernel function with two parameters: centre of mass and variance:

\[v(u(\mathbf{x}); \mathbf{x}_m, s) = C\cdot \int_{\Omega} u(\mathbf{x}) \exp\left(-\frac{||\mathbf{x} - \mathbf{x}_m||}{s}\right) d\mathbf{x}\]

where \(\mathbf{x}_m\) is the centre of mass, \(s\) is the standard deviation (variance), and \(C\) is a normalization constant, such that \(v(1; \mathbf{x}_m, s) = 1\) or equivalently that the \(L^1\) norm of the kernel is equal to one.

Parameters:
  • grid (pyvista.UnstructuredGrid) – The computational grid.

  • use_centroids (bool, optional (default=True)) – If True, the sensors are placed at the centroids of the grid cells. If False, the sensors are placed at the grid points.

  • gdim (int, optional (default=3)) – The geometric dimension of the problem. Default is 3.

create_library(s: float, xm_list: ndarray | None = None, verbose: bool = False)[source]

Create a library of Exponential sensors, given a variance and a list of centres of mass (if provided).

Parameters:
  • s (float) – The standard deviation (variance) of the Exponential sensors.

  • xm_list (np.ndarray, optional (default=None)) – A list of centres of mass for the Exponential sensors. If None, the sensors are placed at the grid nodes.

  • verbose (bool, optional (default=False)) – If True, print progress information.

class pyforce.offline.sensors.GaussianSensorLibrary(grid: pyvista.UnstructuredGrid, use_centroids: bool = True, gdim: int = 3)[source]

Bases: SensorLibraryBase

A class implementing a library of Gaussian sensors.

A Gaussian sensor is mathematically modelled as a linear functional, with a Gaussian kernel function with two parameters: centre of mass and variance:

\[v(u(\mathbf{x}); \mathbf{x}_m, s) = C\cdot \int_{\Omega} u(\mathbf{x}) \exp\left(-\frac{||\mathbf{x} - \mathbf{x}_m||^2}{2s^2}\right) d\mathbf{x}\]

where \(\mathbf{x}_m\) is the centre of mass, \(s\) is the standard deviation (variance), and \(C\) is a normalization constant, such that \(v(1; \mathbf{x}_m, s) = 1\) or equivalently that the \(L^1\) norm of the kernel is equal to one.

Parameters:
  • grid (pyvista.UnstructuredGrid) – The computational grid.

  • use_centroids (bool, optional (default=True)) – If True, the sensors are placed at the centroids of the grid cells. If False, the sensors are placed at the grid points.

  • gdim (int, optional (default=3)) – The geometric dimension of the problem. Default is 3.

create_library(s: float, xm_list: ndarray | None = None, verbose: bool = False)[source]

Create a library of Gaussian sensors, given a variance and a list of centres of mass (if provided).

Parameters:
  • s (float) – The standard deviation (variance) of the Gaussian sensors.

  • xm_list (np.ndarray, optional (default=None)) – A list of centres of mass for the Gaussian sensors. If None, the sensors are placed at the grid nodes.

  • verbose (bool, optional (default=False)) – If True, print progress information.

class pyforce.offline.sensors.IndicatorFunctionSensorLibrary(grid: pyvista.UnstructuredGrid, use_centroids: bool = True, gdim: int = 3)[source]

Bases: SensorLibraryBase

A class implementing a library of Indicator Function sensors.

An Indicator Function sensor is mathematically modelled as a linear functional, with an Indicator Function kernel:

\[v(u(\mathbf{x}); \mathbf{x}_m, r) = \int_{\Omega} u(\mathbf{x}) \mathcal{I}(\mathbf{x}, \mathbf{x}_m, r) d\mathbf{x}\]

where \(\mathcal{I}(\mathbf{x}, \mathbf{x}_m, r)\) is the indicator function defined as:

\[\begin{split}\mathcal{I}(\mathbf{x}, \mathbf{x}_m, r) = \begin{cases} 1 & \text{if } ||\mathbf{x} - \mathbf{x}_m|| \leq r \\ 0 & \text{otherwise} \end{cases}\end{split}\]

where \(\mathbf{x}_m\) is the centre of mass and \(r\) is the radius.

Parameters:
  • grid (pyvista.UnstructuredGrid) – The computational grid.

  • use_centroids (bool, optional (default=True)) – If True, the sensors are placed at the centroids of the grid cells. If False, the sensors are placed at the grid points.

  • gdim (int, optional (default=3)) – The geometric dimension of the problem. Default is 3.

create_library(r: float, xm_list: ndarray | None = None, verbose: bool = False)[source]

Create a library of Indicator Function sensors, given a radius and a list of centres of mass (if provided).

Parameters:
  • r (float) – The radius of the Indicator Function sensors.

  • xm_list (np.ndarray, optional (default=None)) – A list of centres of mass for the Indicator Function sensors. If None, the sensors are placed at the grid nodes.

  • verbose (bool, optional (default=False)) – If True, print progress information.

class pyforce.offline.sensors.SensorLibraryBase[source]

Bases: ABC

Abstract base class for sensors, mathematically modelled as linear functionals:

\[v(u; \boldsymbol{\xi}) = \int_{\Omega} u(\mathbf{x}) \mathcal{K}(\mathbf{x}, \boldsymbol{\xi}) d\mathbf{x}\]

where \(\mathcal{K}(\mathbf{x}, \boldsymbol{\xi})\) is the kernel function and \(\boldsymbol{\xi}\) are the sensor parameters (e.g., centre of mass and variance for Gaussian sensors).

action(func: FunctionsList | ndarray, M: int | None = None)[source]

Compute the action of the sensor library on a given function (both single or matrix) or a list of functions.

Parameters:
  • func (FunctionsList | np.ndarray) – The function or list of functions to be sensed.

  • M (int, optional (default=None)) – If provided, only the first M sensors in the library are used.

Returns:

actions – The actions of the sensor library on the function(s). Shape (Nsensors, Ns).

Return type:

np.ndarray

add_sensor(kernel: ndarray)[source]

Add a sensor to the library.

Parameters:

kernel (np.ndarray) – The kernel function of the sensor to be added.

abstract create_library(**kwargs)[source]

Abstract method to create a library of sensors.

property library

Return the library of sensors.

property nodes

Return the nodes where the sensors are placed.

set_library(library: FunctionsList)[source]

Set the sensor library to a given FunctionsList.

Parameters:

library (FunctionsList) – The library of sensors to be set.

pyforce.offline.sgreedy module

class pyforce.offline.sgreedy.SGREEDY(grid, gdim=3, varname='u', sensors_type='Gaussian')[source]

Bases: object

A class to place sensors using the SGREEDY algorithm in order to maximize the information contained in the update space spanned by the Riesz representation of sensors.

The algorithm is described in the work of Haik et al. (2023).

Parameters:
  • grid (pyvista.UnstructuredGrid) – The computational grid.

  • gdim (int, optional) – The geometric dimension of the problem (default is 3).

  • varname (str, optional) – The variable name to be processed (default is ‘u’).

  • sensors_type (str, optional (default='Gaussian')) – The type of sensors to use in the GEIM algorithm. It can be either ‘Gaussian’, ‘Exponential’, or ‘IndicatorFunction’.

fit(basis_functions: FunctionsList, Mmax: int, sensor_params: dict, Nmax: int | None = None, tol: float = 0.2, verbose: bool = True)[source]

Selection of sensors position with a Riesz representation \(\{g_m\}_{m=1}^M\) in \(L^2\). The positions of the sensors are either freely selected on the mesh or given as input with the ‘sensors_params’ dictionary.

Parameters:
  • basis_functions (FunctionsList) – The reduced basis functions.

  • Mmax (int) – The maximum number of sensors to be placed (it must be greater or equal to the number of basis functions).

  • sensors_params (dict) – Dictionary containing the parameters for the sensor library creation.

  • Nmax (int, optional) – The maximum number of basis functions to be considered (default is None, which means all the basis functions are considered).

  • tol (float, optional) – Tolerance to exit the stability loop (default is 0.2).

  • verbose (bool, optional) – If True, prints the progress of the algorithm (default is True).

plot_sensors(M: int | None = None, view='xy', cmap='jet', levels=50, color_sens='black', fontsize_sens=10, fig_length=6, show_ticks=False)[source]

Plot the Riesz representation of the first M sensors, cumulated:

\[G(\mathbf{x}) = \sum_{m=1}^M \frac{g_m(\mathbf{x})}{\max |g_m|}\]
Parameters:
  • M (int, optional (Default = None)) – Number of sensors to plot. If None, all available sensors are plotted.

  • view (str, optional (Default = 'xy')) – View of the plot. It can be either ‘xy’, ‘xz’ or ‘yz’.

  • cmap (str, optional (Default = 'jet')) – Colormap to use for the plot.

  • levels (int, optional (Default = 50)) – Number of levels to use for the contour plot.

  • color_sens (str, optional (Default = 'black')) – Color of the sensor labels.

  • fontsize_sens (int, optional (Default = 10)) – Font size of the sensor labels.

  • fig_length (float, optional (Default = 6)) – Length of the figure in inches.

  • show_ticks (bool, optional (Default = False)) – If True, show the ticks on the axes.

save(path_folder: str, **kwargs)[source]

Save the magic functions and sensors to a specified path.

Parameters:
  • path_folder (str) – The folder path where the model will be saved.

  • **kwargs (dict) – Additional keyword arguments for saving options.

pyforce.offline.sgreedy.compute_inf_sup(sensors: SensorLibraryBase, basis_functions: FunctionsList, calculator: IntegralCalculator, N: int, return_eigenvector: bool = False)[source]

Compute the inf-sup constants for a given set of sensors and basis functions.

Parameters:
  • sensors (FunctionsList) – The list of sensors.

  • basis_functions (FunctionsList) – The reduced basis functions.

  • calculator (IntegralCalculator) – The integral calculator object.

  • Nmax (int, optional) – The maximum number of basis functions to be considered (default is None, which means all the basis functions are considered).

Returns:

inf_sup_constants – The list of inf-sup constants at each iteration.

Return type:

list

Module contents

pyforce/offline.

Offline phase of the pyforce library.