pyforce.online package
Submodules
pyforce.online.failing_sensors module
- class pyforce.online.failing_sensors.PBDW(basis_functions: FunctionsList, basis_sensors: FunctionsList, name: str, is_H1: bool = False)[source]
Bases:
object
This class can be used to perform the online phase of the PBDW formulation for synthetic measures \(\mathbf{y}\in\mathbb{R}^M\) obtained as evaluations of the magic sensors on the snapshot \(u(\mathbf{x};\,\boldsymbol{\mu})\) as
\[y_m = v_m(u(\boldsymbol{\mu})) + \epsilon_m + \delta_{m} \qquad \qquad m = 1, \dots, M\]in which \(\epsilon_,\sim \mathcal{N}(0, \sigma^2)\) is random noise and \(\delta_m \sim \mathcal{N}(\kappa, \rho^2)\) acting on some measurements. This term is referred to as drift.
- Parameters:
basis_functions (FunctionsList) – List of functions spanning the reduced space
basis_sensors (FunctionsList) – List of sensors representation spanning the update space
name (str) – Name of the snapshots (e.g., temperature T)
is_H1 (boolean, optional (Default= False)) – Boolean indicating whether to use scalar product in \(\mathcal{H}^1\) or \(L^2\).
- drift_test_err(snaps: FunctionsList, N: int, M: int, noise_value: float, reg_param: ndarray, kappa: float, rho: float, idx_failed: list[int], num_rep_exp: int = 30, mu_failure: int = 0, verbose=False)[source]
The PBDW online algorithm is used to reconstruct the snaps (FunctionsList), by solving the PBDW linear system
\[\begin{split}\left[ \begin{array}{ccc} \xi \cdot M \cdot \mathbb{I} + \mathbb{A} & & \mathbb{K} \\ & & \\ \mathbb{K}^T & & 0 \end{array} \right] \cdot \left[ \begin{array}{c} \boldsymbol{\alpha} \\ \\ \boldsymbol{\theta} \end{array} \right] = \left[ \begin{array}{c} \mathbf{y} \\ \\ \mathbf{0} \end{array} \right]\end{split}\]then the inteprolant and residual are computed and returned per each element of the list.
- Parameters:
snaps (FunctionsList) – Function to reconstruction
N (int) – Dimension of the reduced space
M (int) – Number of sensor to use
noise_value (float) – Standard deviation of the noise, modelled as a normal \(\mathcal{N}(0, \sigma^2)\)
reg_param (np.ndarray) – Regularising parameter \(\lambda\)
kappa (float) – Mean value \(\kappa\) of the drift
rho (float) – Standard deviation \(\rho\) of the drift
idx_failed (list[int]) – List of integers with the failed sensors
num_rep_exp (int, optional (default = 30)) – Number of repeated experiments.
mu_failure (int, optional (default = 0)) – Index from which failure starts, typically time.
verbose (boolean, optional (default = False)) – If true, output is printed.
- Returns:
errors (np.ndarray) – Errors per each element in snaps (first column: absolute, second column: relative), measure in \(||\cdot ||_{L^2}\) and averaged for the numerical experiments.
interps (FunctionsList) – Interpolant Field \(\mathcal{I}_M[u]\) of TR-GEIM.
resids (FunctionsList) – Residual Field \(r_M[u]\),
- gpr_measure_test_err(snaps: FunctionsList, N: int, M: int, noise_value: float, reg_param: ndarray, ext_sens: FunctionsList, surrogate_model: list, idx_failed: list[int], mu_failure: int = 0, verbose=False)[source]
The PBDW online algorithm is used to reconstruct the snaps (FunctionsList), by solving the PBDW linear systemwith idx_failed measure. In order to retrieve information on the “failed measure”, a surrogate model (e.g., GPR) has been trained to learn the map from non-failed external measures and the one related to idx_failed.
The interpolant is then defined in the standard way.
- Parameters:
snaps (FunctionsList) – Function to reconstruction
N (int) – Dimension of the reduced space
M (int) – Number of sensor to use
noise_value (float) – Standard deviation of the noise, modelled as a normal \(\mathcal{N}(0, \sigma^2)\)
reg_param (np.ndarray) – Regularising parameter \(\lambda\)
ext_sens (FunctionsList) – Basis sensors adopted to compute the external measures, input of surrogate_model
surrogate_model (list) – List of all the trained surrogate models
idx_failed (list[int]) – List of integers with the failed sensors
mu_failure (int, optional (default = 0)) – Index from which failure starts, typically time.
verbose (boolean, optional (default = False)) – If true, output is printed.
- Returns:
errors (np.ndarray) – Errors per each element in snaps (first column: absolute, second column: relative), measure in \(||\cdot ||_{L^2}\).
interps (FunctionsList) – Interpolant Field \(\mathcal{I}_M[u]\) of TR-GEIM.
resids (FunctionsList) – Residual Field \(r_M[u]\),
- pure_remove_test_err(snaps: FunctionsList, N: int, M: int, noise_value: float, reg_param: ndarray, idx_failed: list[int], mu_failure: int = 0, verbose=False)[source]
The PBDW online algorithm is used to reconstruct the snaps (FunctionsList), by solving the modified PBDW linear system: in particular, idx_failed measure is removed thus from \(\mathbf{y}\) the idx_failed row is deleted, from \(\mathbb{A}\) its idx_failed row and col are deleted and from \(\mathbb{K}\) its idx_failed row is deleted.
The interpolant is then defined as the sum over the obtained coefficients from the modified PBDW linear system, without idx_failed.
- Parameters:
snaps (FunctionsList) – Function to reconstruction
N (int) – Dimension of the reduced space
M (int) – Number of sensor to use
noise_value (float) – Standard deviation of the noise, modelled as a normal \(\mathcal{N}(0, \sigma^2)\)
reg_param (np.ndarray) – Regularising parameter \(\lambda\)
idx_failed (list[int]) – List of integers with the failed sensors
mu_failure (int, optional (default = 0)) – Index from which failure starts, typically time.
verbose (boolean, optional (default = False)) – If true, output is printed.
- Returns:
errors (np.ndarray) – Errors per each element in snaps (first column: absolute, second column: relative), measure in \(||\cdot ||_{L^2}\).
interps (FunctionsList) – Interpolant Field \(\mathcal{I}_M[u]\) of TR-GEIM.
resids (FunctionsList) – Residual Field \(r_M[u]\),
- class pyforce.online.failing_sensors.TRGEIM(magic_fun: FunctionsList, magic_sen: FunctionsList, mean_beta: ndarray, std_beta: ndarray, name: str)[source]
Bases:
object
This class can be used to perform the online phase of the TR-GEIM algorihtm for synthetic measures \(\mathbf{y}\in\mathbb{R}^M\) obtained as evaluations of the magic sensors on the snapshot \(u(\mathbf{x};\,\boldsymbol{\mu})\) as
\[y_m = v_m(u(\boldsymbol{\mu})) + \epsilon_m + \delta_{m} \qquad \qquad m = 1, \dots, M\]in which \(\epsilon_m\sim \mathcal{N}(0, \sigma^2)\) is random noise and \(\delta_m \sim \mathcal{N}(\kappa, \rho^2)\) acting on some measurements. This term is referred to as drift.
Two approaches are implemented:
Unregularised case: how the drifted measurement affect the reconstruction?
Remove measure case: the failed measure (once at a time) is removed as well as the correspondent magic function
- Parameters:
magic_fun (FunctionsList) – List of magic functions computed during the offline phase.
magic_sen (FunctionsList) – List of magic sensors computed during the offline phase.
mean_beta (np.ndarray) – Mean values \(\langle\beta_m\rangle\) of the training reduced coefficients
std_beta (np.ndarray) – Standard deviations \(\sigma_{\beta_m}\) of the training reduced coefficients
name (str) – Name of the snapshots (e.g., temperature T)
- compute_measure(snap: dolfinx.fem.Function, noise_value: float | None = None, M=None) ndarray [source]
Computes the measurement vector from the snap input, using the magic sensors stored to which synthetic random noise is added. If the dimension M is not given, the whole set of magic sensors is used.
\[y_m = v_m(u) +\epsilon_m \qquad \qquad m = 1, \dots, M\]If the dimension \(M\) is not given, the whole set of magic sensors is used.
- Parameters:
snap (Function) – Function from which measurements are to be extracted
noise_value (float, optional (Default = None)) – Standard deviation of the noise, modelled as a normal \(\mathcal{N}(0, \sigma^2)\)
M (int, optional (default = None)) – Maximum number of sensor to use (if None is set to the number of magic functions/sensors)
- Returns:
measure – Measurement vector \(\mathbf{y}\in\mathbb{R}^M\)
- Return type:
np.ndarray
- drift_test_err(snaps: FunctionsList, M: int, noise_value: float, reg_param: float, kappa: float, rho: float, idx_failed: list[int], num_rep_exp: int = 30, mu_failure: int = 0, verbose=False)[source]
The TR-GEIM algorithm is used to reconstruct the snaps (FunctionsList), by solving the TR-GEIM linear system
\[\left(\mathbb{B}^T\mathbb{B}+\lambda \mathbb{T}^T\mathbb{T}\right)\boldsymbol{\beta} = \mathbb{B}^T\mathbf{y}+\lambda \mathbb{T}^T\mathbb{T} \langle{\boldsymbol{\beta}}\rangle\]then the inteprolant and residual are computed and returned per each element of the list.
- Parameters:
snaps (FunctionsList) – Function to reconstruction
M (int) – Number of sensor to use
noise_value (float) – Standard deviation of the noise, modelled as a normal \(\mathcal{N}(0, \sigma^2)\)
reg_param (float) – Regularising parameter \(\lambda\)
kappa (float) – Mean value \(\kappa\) of the drift
rho (float) – Standard deviation \(\rho\) of the drift
idx_failed (list[int]) – List of integers with the failed sensors
num_rep_exp (int, optional (default = 30)) – Number of repeated experiments.
mu_failure (int, optional (default = 0)) – Index from which failure starts, typically time.
verbose (boolean, optional (default = False)) – If true, output is printed.
- Returns:
errors (np.ndarray) – Errors per each element in snaps (first column: absolute, second column: relative), measure in \(||\cdot ||_{L^2}\) and averaged for the numerical experiments.
interps (FunctionsList) – Interpolant Field \(\mathcal{I}_M[u]\) of TR-GEIM.
resids (FunctionsList) – Residual Field \(r_M[u]\),
- gpr_measure_test_err(snaps: FunctionsList, M: int, noise_value: float, reg_param: float, ext_sens: FunctionsList, surrogate_model: list, idx_failed: list[int], mu_failure: int = 0, verbose=False)[source]
The TR-GEIM algorithm is used to reconstruct the snaps (FunctionsList), by solving the TR-GEIM linear system with idx_failed measure. In order to retrieve information on the “failed measure”, a surrogate model (e.g., GPR) has been trained to learn the map from non-failed external measures and the one related to idx_failed.
The interpolant is then defined in the standard way.
- Parameters:
snaps (FunctionsList) – Functions to reconstruction
M (int) – Number of sensor to use
noise_value (float) – Standard deviation of the noise, modelled as a normal \(\mathcal{N}(0, \sigma^2)\)
reg_param (float) – Regularising parameter \(\lambda\)
ext_sens (FunctionsList) – Basis sensors adopted to compute the external measures, input of surrogate_model
surrogate_model (list) – List of all the trained surrogate models
idx_failed (list[int]) – List of integers with the failed sensors
mu_failure (int, optional (default = 0)) – Index from which failure starts, typically time.
verbose (boolean, optional (default = False)) – If true, output is printed.
- Returns:
errors (np.ndarray) – Errors per each element in snaps (first column: absolute, second column: relative), measure in \(||\cdot ||_{L^2}\).
interps (FunctionsList) – Interpolant Field \(\mathcal{I}_M[u]\) of TR-GEIM.
resids (FunctionsList) – Residual Field \(r_M[u]\),
- pure_remove_test_err(snaps: FunctionsList, M: int, noise_value: float, reg_param: float, idx_failed: list[int], mu_failure: int = 0, verbose=False)[source]
The TR-GEIM algorithm is used to reconstruct the snaps (FunctionsList), by solving the modified TR-GEIM linear system: in particular, idx_failed measure is removed thus from \(\mathbf{y}\) the idx_failed row is deleted, from \(\mathbb{T}\) and \(\mathbb{B}\) their idx_failed row and col are deleted.
The interpolant is then defined as the sum over the obtained coefficients from the modified TR-GEIM linear system, without idx_failed.
- Parameters:
snaps (FunctionsList) – Function to reconstruction
M (int) – Number of sensor to use
noise_value (float) – Standard deviation of the noise, modelled as a normal \(\mathcal{N}(0, \sigma^2)\)
reg_param (float) – Regularising parameter \(\lambda\)
idx_failed (list) – List of integers with the failed sensor
mu_failure (int, optional (default = 0)) – Index from which failure starts, typically time.
verbose (boolean, optional (default = False)) – If true, output is printed.
- Returns:
errors (np.ndarray) – Errors per each element in snaps (first column: absolute, second column: relative), measure in \(||\cdot ||_{L^2}\).
interps (FunctionsList) – Interpolant Field \(\mathcal{I}_M[u]\) of TR-GEIM.
resids (FunctionsList) – Residual Field \(r_M[u]\),
- pyforce.online.failing_sensors.compute_measure(snaps: FunctionsList, sensors: FunctionsList, noise_value: float | None = None, M=None) ndarray [source]
Computes the measurement matrix from the snaps input, using sensors given as input to which synthetic random noise is added. If the dimension M is not given, the whole set of magic sensors is used.
\[y_m = v_m(u) +\epsilon_m \qquad \qquad m = 1, \dots, M\]If the dimension \(M\) is not given, the whole set of magic sensors is used.
- Parameters:
snap (FunctionsList) – FunctionsList from which measurements are to be extracted
sensors (FunctionsList) – FunctionsList containing the sensors
noise_value (float, optional (Default = None)) – Standard deviation of the noise, modelled as a normal \(\mathcal{N}(0, \sigma^2)\)
M (int, optional (default = None)) – Maximum number of sensor to use (if None is set to the number of magic functions/sensors)
- Returns:
measure – Measurement vector \(\mathbf{y}\in\mathbb{R}^{M\times N_s}\)
- Return type:
np.ndarray
- pyforce.online.failing_sensors.remove_lin_combine(fun_list: FunctionsList, vec: ndarray, M: int, sensI_drifted: int)[source]
This auxiliary function is used to perform a linear combination of basis functions without sensI_drifted (i.e., \(j\)).
\[u = \sum_{i=1,i\neq j}^{M} \alpha_i \cdot \psi_i\]- Parameters:
fun_list (FunctionsList) – List of basis functions
vec (np.ndarray) – Iterable containing the coefficients \(\boldsymbol{\alpha}\in\mathbb{R}^{M-1}\) of the linear combination.
M (int) – Maximum number of basis functions without removal.
sensI_drifted (int) – Index of the drifted sensor and hence drifted coefficient \(\alpha_j\) .
- Returns:
combination – np.ndarray object storing the result of the linear combination
- Return type:
np.ndarray
pyforce.online.geim module
- class pyforce.online.geim.GEIM(magic_fun: FunctionsList, magic_sen: FunctionsList, name: str)[source]
Bases:
object
This class can be used to perform the online phase of the GEIM algorihtm for synthetic and real measures \(\mathbf{y}\in\mathbb{R}^M\) either obtained as evaluations of the magic sensors on the snapshot \(u(\mathbf{x};\,\boldsymbol{\mu})\) as
\[y_m = v_m(u)+\varepsilon_m \qquad \qquad m = 1, \dots, M\]given \(\varepsilon_m\) random noise (either present or not), or by real experimental data on the physical system.
- Parameters:
magic_fun (FunctionsList) – List of magic functions computed during the offline phase.
magic_sen (FunctionsList) – List of magic sensors computed during the offline phase.
name (str) – Name of the snapshots (e.g., temperature T)
- compute_measure(snap: dolfinx.fem.Function, M=None) ndarray [source]
Computes the measurement vector \(\mathbf{y}\in\mathbb{R}^M\) from the snap \(u\) input, using the magic sensors stored.
\[y_m = v_m(u) \qquad \qquad m = 1, \dots, M\]If the dimension \(M\) is not given, the whole set of magic sensors is used.
- Parameters:
snap (Function) – Function from which measurements are to be extracted
M (int, optional (default = None)) – Maximum number of sensor to use (if None is set to the number of magic functions/sensors)
- Returns:
measure – Measurement vector :math:mathbf{y}inmathbb{R}^M`
- Return type:
np.ndarray
- real_reconstruct(measure: ndarray)[source]
The interpolant given the measure vector \(\mathbf{y}\) input is computed, by solving the GEIM linear system
\[\mathbb{B}\boldsymbol{\beta} = \mathbf{y}\]then the interpolant is computed and returned
\[\mathcal{I}_M(\mathbf{x}) = \sum_{m=1}^M \beta_m[u] \cdot q_m(\mathbf{x})\]- Parameters:
measure (np.ndarray) – Measurement vector, shaped as \(M \times N_s\), given \(M\) the number of sensors used and \(N_s\) the number of parametric realisation.
- Returns:
interp (np.ndarray) – Interpolant Field \(\mathcal{I}_M\) of GEIM
computational_time (dict) – Dictionary with the CPU time of the most relevant operations during the online phase.
- reconstruct(snap: ndarray, M, noise_value=None)[source]
The interpolant for snap \(u\) input is computed, by solving the GEIM linear system
\[\mathbb{B}\boldsymbol{\beta} = \mathbf{y}\]then the inteprolant and residual are computed and returned
\[\mathcal{I}_M[u] = \sum_{m=1}^M \beta_m[u] \cdot q_m\qquad\qquad r_M = \left| u - \mathcal{I}_M[u]\right|\]- Parameters:
snap (Function as np.ndarray) – Snap to reconstruct, if a function is provided, the variable is reshaped.
M (int) – Number of sensor to use
noise_value (float, optional (default = None)) – Standard deviation of the noise, modelled as a normal \(\mathcal{N}(0, \sigma^2)\)
- Returns:
interp (np.ndarray) – Interpolant Field \(\mathcal{I}_M[u]\) of GEIM
resid (np.ndarray) – Residual Field \(r_M[u]\)
computational_time (dict) – Dictionary with the CPU time of the most relevant operations during the online phase.
- synt_test_error(snaps: FunctionsList, M=None, noise_value=None, verbose=False) namedtuple [source]
The absolute and relative error on the test set is computed, by solving the GEIM linear system
\[\mathbb{B}\boldsymbol{\beta} = \mathbf{y}\]- Parameters:
snaps (FunctionsList) – List of functions belonging to the test set to reconstruct
M (int, optional (default = None)) – Maximum number of magic functions to use (if None is set to the number of magic functions/sensors)
noise_value (float, optional (default = None)) – Standard deviation of the noise, modelled as a normal \(\mathcal{N}(0, \sigma^2)\)
verbose (boolean, optional (default = False)) – If true, output is printed.
- 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.
pyforce.online.indirect_recon module
- class pyforce.online.indirect_recon.PE(B: ndarray, coeff_maps: list, bnds: list)[source]
Bases:
object
A class to perform Parameter Estimation (PE) exploiting GEIM and numerical optimisation starting from a set of measurements \(\mathbf{y}\in\mathbb{R}^M\)
\[\hat{\boldsymbol{\mu}} = \text{arg}\,\min\limits_{\boldsymbol{\mu}\in\mathcal{D}} \mathcal{L}_{PE}(\boldsymbol{\mu})\]- Parameters:
B (np.ndarray) – Lower-triangular matrix \(\mathbb{B}\) of dimension \(M\times M\), arising from GEIM magic function and sensors.
coeff_maps (list) – List containing the map \(\mathcal{F}_m:\boldsymbol{\mu} \rightarrow \beta_m\).
bnds (list) – List of tuples, each element contains the mininum and maximum of the components of the parameter.
- optimise(measure: ndarray, use_brute=True, grid_elem=10)[source]
This auxiliary function performs the optimisation process given an input collection of measurements.
Two options are available:
brute force method for finding the first guess of the parameter estimation + Least squares
differential evolution method for finding the first guess of the parameter estimation + Least squares
- Parameters:
measure (np.ndarray) – Measurement vector of M elements, array-like.
use_brute (bool, optional (Default = True)) – brute force method for finding the first guess of the parameter estimation
grid_elem (int, optional (Default = 10)) – Number of elements in the grid for the brute force method
- Returns:
solution (np.ndarray) – Solution of the optimisation (after least squares)
guess (np.ndarray) – Solution of the optimisation (before least squares)
- synt_test_error(test_param: ndarray, test_snaps: FunctionsList, GEIM_msen: FunctionsList, Mmax: int, noise_value=None, use_brute=True, grid_elem=10, verbose=False)[source]
The absolute and relative error of the PE phase, using different measurements coming from the test set, are computed.
- Parameters:
test_param (np.ndarray) – np.ndarray with shape $(N_s, p)$ given $N_s$ the number of samples and $p$ the dimension of the parameter vector
test_snaps (FunctionsList) – List of functions belonging to the test set, used to generate the measurements
GEIM_msen (FunctionsList) – List of sensors to mimic the measurement process
Mmax (int) – Maximum number of sensor to use
noise_value (float, optional (Default = None)) – Standard deviation of the noise, modelled as a normal \(\mathcal{N}(0, \sigma^2)\).
use_brute (bool, optional (Default = True)) – brute force method for finding the first guess of the parameter estimation
grid_elem (int, optional (Default = 10)) – Number of elements in the grid for the brute force method
verbose (bool, optional (Default = False)) – If true, printing is produced
- Returns:
mean_abs_err (np.ndarray) – Average absolute error of the parameter estimation
mean_rel_err (np.ndarray) – Average relative error of the parameter estimation
computational_time (dict) – Dictionary with the CPU time of the most relevant operations during the online phase.
mu_PE (list) – List containing the estimated parameters after least squares at varying number of measurements
mu_PE_guess (list) – List containing the estimated parameters before least squares at varying number of measurements
- pyforce.online.indirect_recon.objective(mu: ndarray, measure: ndarray, B: ndarray, maps: list)[source]
This function evaluates the standard loss function \(\mathcal{L}_{PE}\) to be minimized during the Parameter Estimation phase
\[\mathcal{L}_{PE}(\boldsymbol{\mu}) = \|B\boldsymbol{\beta}(\boldsymbol{\mu}) - \mathbf{y}\|_2^2 \qquad\qquad \text{ given }\mathcal{F}_m(\boldsymbol{\mu}) = \beta_m(\boldsymbol{\mu})\]given the maps \(\mathcal{F}_m:\boldsymbol{\mu} \rightarrow \beta_m\) (\(m = 1, \dots, M\)).
- Parameters:
mu (np.ndarray) – Input parameter, array-like.
measure (np.ndarray) – Input measurements of M elements, array-like.
B (np.ndarray) – Lower-triangular matrix \(\mathbb{B}\) of dimension \(M\times M\).
maps (list) – List containing the map \(\mathcal{F}_m:\boldsymbol{\mu} \rightarrow \beta_m\).
pyforce.online.pbdw module
- class pyforce.online.pbdw.PBDW(basis_functions: FunctionsList, basis_sensors: FunctionsList, name: str, is_H1: bool = False)[source]
Bases:
object
A class implementing the online phase of the Parameterised-Background Data-Weak (PBDW) formulation, given a list of sensors’ Riesz representation \(\{g_m\}_{m=1}^M\) for the update space and basis functions \(\{\zeta_n\}_{n=1}^N\) for the reduced one. In particular, the following matrices are defined \((n,n' = 1,\dots,N)\) and \((m,m' = 1,\dots,M)\)
\[\mathbb{A}_{mm'}=\left(g_m,\,g_{m'}\right)_{\mathcal{U}} \qquad \qquad \mathbb{K}_{mn}=\left(g_m,\,\zeta_{n}\right)_{\mathcal{U}}\]given \(\mathcal{U}\) as the functional space.
- Parameters:
basis_functions (FunctionsList) – List of functions spanning the reduced space
basis_sensors (FunctionsList) – List of sensors representation spanning the update space
name (str) – Name of the snapshots (e.g., temperature T)
is_H1 (boolean, optional (Default= False)) – Boolean indicating whether to use scalar product in \(\mathcal{H}^1\) or \(L^2\).
- compute_measure(snap: dolfinx.fem.Function, noise_value: float, M=None) ndarray [source]
Computes the measurement vector from the snap input, using the basis sensors stored to which synthetic random noise is added. If the dimension M is not given, the whole set of basis sensors is used.
\[y_m = v_m(u) +\varepsilon_m \qquad \qquad m = 1, \dots, M\]If the dimension \(M\) is not given, the whole set of basis sensors is used.
- Parameters:
snap (Function) – Function from which measurements are to be extracted
noise_value (float) – Standard deviation of the noise, modelled as a normal \(\mathcal{N}(0, \sigma^2)\)
M (int, optional (default = None)) – Maximum number of sensor to use (if None is set to the number of basis sensors)
- Returns:
measure – Measurement vector :math:mathbf{y}inmathbb{R}^M`
- Return type:
np.ndarray
- hyperparameter_tuning(snaps: FunctionsList, noise_value: float, xi_lim=[-4, 5], n_samples: int = 10, num_rep_exp: int = 10, N: int | None = None, M: int | None = None, verbose: bool = False)[source]
The hyperparameter \(\xi\) of the PBDW statement is calibrated as the one minimising the absolute error in \(L^2\), namely
\[\hat{\xi} = \text{arg}\,\min\limits_{\xi\in\mathbb{R}^+} E_{M, \xi}\]given \(E_{M, \xi}\) the average absolute error in \(L^2\) with \(M\) sensors. The optimization problem is solved sampling \(\xi\) in a specific range \(\Xi \subset\mathbb{R}^+\).
- Parameters:
snaps (FunctionsList) – List of functions belonging to the validation set to reconstruct
noise_value (float) – Standard deviation of the noise, modelled as a normal \(\mathcal{N}(0, \sigma^2)\)
xi_lim (tuple of floats, optional (Default = [0, 6])) – Lower and upper bound (the input is the exponent for \(10^x\)) of \(\xi\), regularisation parameter of PBDW.
num_rep_exp (int, optional (Default = 10)) – Number of repeated numerical experiment to ensure statistical robustness
N (int, optional (Default = None)) – Number of basis function to use (if None is set to the number of basis functions)
M (int, optional (Default = None)) – Number of sensor to use (if None is set to the number of basis sensors)
verbose (boolean, optional (default = False)) – If true, output is printed.
- Returns:
xi_opt (float) – Optimal value of \(\hat{\xi}\) per each numerical experiment
xi_samples (np.ndarray) – Sample values of \(\xi\)
ave_abs_err (np.ndarray) – Average (also averaged wrt to the num. experiments) absolure reconstruction error associated to each output value of \(\xi_{sample}\)
- real_reconstruct(measure: ndarray, N: int | None = None, reg_param: float = 0.0)[source]
The state estimation given the measure vector \(\mathbf{y}\) input is computed, by solving the PBDW linear system
\[\begin{split}\left[ \begin{array}{ccc} \xi \cdot M \cdot \mathbb{I} + \mathbb{A} & & \mathbb{K} \\ & & \\ \mathbb{K}^T & & 0 \end{array} \right] \cdot \left[ \begin{array}{c} \boldsymbol{\alpha} \\ \\ \boldsymbol{\theta} \end{array} \right] = \left[ \begin{array}{c} \mathbf{y} \\ \\ \mathbf{0} \end{array} \right]\end{split}\]given \(\mathbf{y}\in\mathbb{R}^M\). Then, the full can state for the snapshot \(u\) can be written as
\[u(\mathbf{x};\boldsymbol{\mu}) \simeq z_N(\mathbf{x};\boldsymbol{\mu})+\eta_M(\mathbf{x};\boldsymbol{\mu}) = \sum_{n=1}^N\alpha_n(\boldsymbol{\mu})\cdot \zeta_n(\mathbf{x})+ \sum_{m=1}^M \theta_m(\boldsymbol{\mu})\cdot g_m(\mathbf{x})\]- Parameters:
measure (np.ndarray) – Measurement vector, shaped as \(M \times N_s\), given \(M\) the number of sensors used and \(N_s\) the number of parametric realisation.
N (int, optional (default = None)) – Maximum number of basis functions \(\zeta_n\) to use (if None is set to the number of basis functions)
reg_param (float, optional (default = 0.)) – Hyperparameter \(\xi\) weighting the importance of the model with respect to the measurements.
- Returns:
interp (np.ndarray) – Interpolant Field \(\mathcal{I}_M\) of GEIM
computational_time (dict) – Dictionary with the CPU time of the most relevant operations during the online phase.
- reconstruct(snap: ndarray, N: int, M: int, noise_value: float | None = None, reg_param: float = 0.0)[source]
The PBDW system is solved
\[\begin{split}\left[ \begin{array}{ccc} \xi \cdot M \cdot \mathbb{I} + \mathbb{A} & & \mathbb{K} \\ & & \\ \mathbb{K}^T & & 0 \end{array} \right] \cdot \left[ \begin{array}{c} \boldsymbol{\alpha} \\ \\ \boldsymbol{\theta} \end{array} \right] = \left[ \begin{array}{c} \mathbf{y} \\ \\ \mathbf{0} \end{array} \right]\end{split}\]given \(\mathbf{y}\in\mathbb{R}^M\). Then, the full can state for the snapshot \(u\) can be written as
\[u(\mathbf{x};\boldsymbol{\mu}) \simeq z_N(\mathbf{x};\boldsymbol{\mu})+\eta_M(\mathbf{x};\boldsymbol{\mu}) = \sum_{n=1}^N\alpha_n(\boldsymbol{\mu})\cdot \zeta_n(\mathbf{x})+ \sum_{m=1}^M \theta_m(\boldsymbol{\mu})\cdot g_m(\mathbf{x})\]- Parameters:
snap (Function as np.ndarray) – Snap to reconstruct, if a function is provided, the variable is reshaped.
N (int, optional (default = None)) – Maximum number of basis functions \(\zeta_n\) to use (if None is set to the number of basis functions)
M (int, optional (default = None)) – Maximum number of basis sensors \(g_m\) to use (if None is set to the number of basis sensors)
noise_value (float, optional (default = None)) – Standard deviation of the noise, modelled as a normal \(\mathcal{N}(0, \sigma^2)\)
reg_param (float, optional (default = 0.)) – Hyperparameter \(\xi\) weighting the importance of the model with respect to the measurements.
- Returns:
recon (Function) – Function containing the reconstruction using \(M\) sensors
resid (Function) – Function containing the residual field (absolute difference between interpolant and true field) using \(M\) sensors
computational_time (dict) – Dictionary with the CPU time of the most relevant operations during the online phase.
- synt_test_error(test_snap: FunctionsList, N: int | None = None, M: int | None = None, noise_value: float | None = None, reg_param: float = 0.0, verbose: bool = False) namedtuple [source]
The absolute and relative error on the test set is computed, by solving the PBDW system
\[\begin{split}\left[ \begin{array}{ccc} \xi \cdot M \cdot \mathbb{I} + \mathbb{A} & & \mathbb{K} \\ & & \\ \mathbb{K}^T & & 0 \end{array} \right] \cdot \left[ \begin{array}{c} \boldsymbol{\alpha} \\ \\ \boldsymbol{\theta} \end{array} \right] = \left[ \begin{array}{c} \mathbf{y} \\ \\ \mathbf{0} \end{array} \right]\end{split}\]given \(\mathbf{y}\in\mathbb{R}^M\). Then, the full can state for the snapshot \(u\) can be written as
\[u(\mathbf{x};\boldsymbol{\mu}) \simeq z_N(\mathbf{x};\boldsymbol{\mu})+\eta_M(\mathbf{x};\boldsymbol{\mu}) = \sum_{n=1}^N\alpha_n(\boldsymbol{\mu})\cdot \zeta_n(\mathbf{x})+ \sum_{m=1}^M \theta_m(\boldsymbol{\mu})\cdot g_m(\mathbf{x})\]- Parameters:
test_snap (FunctionsList) – List of functions belonging to the test set to reconstruct with PBDW
N (int, optional (default = None)) – Maximum number of basis functions \(\zeta_n\) to use (if None is set to the number of basis functions)
M (int, optional (default = None)) – Maximum number of basis sensors \(g_m\) to use (if None is set to the number of basis sensors)
noise_value (float, optional (default = None)) – Standard deviation of the noise, modelled as a normal \(\mathcal{N}(0, \sigma^2)\)
reg_param (float, optional (default = 0.)) – Hyperparameter \(\xi\) weighting the importance of the model with respect to the measurements.
verbose (boolean, optional (default = False)) – If true, output is printed.
- 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.
pyforce.online.pod_interpolation module
- class pyforce.online.pod_interpolation.PODI(modes: FunctionsList, maps: list, name: str)[source]
Bases:
object
A class to perform the online phase of the POD with Inteprolation (PODI) algorithm, in which the modal coefficients are found from suitable maps \(\mathcal{F}_n:\boldsymbol{\mu} \rightarrow \alpha_n\) (\(n = 1, \dots, N\)).
- Parameters:
modes (FunctionsList) – List of POD modes computed during the offline phase.
maps (list) – List of maps for the POD modal coefficients, they must be callable. If None, the reduced coefficient must be provided as input later!
name (str) – Name of the snapshots (e.g., temperature T)
- reconstruct(snap: ndarray, mu_estimated: ndarray, maxBasis: int, alpha_coeffs: ndarray | None = None)[source]
After the coefficients of the POD basis are obtained by interpolating using the maps or given as input, the snap is approximated using linear combination of the POD modes.
- Parameters:
snap (Function as np.ndarray) – Snap to reconstruct, if a function is provided, the variable is reshaped.
mu_estimated (np.ndarray) – Arrays with the estimated parameters from the Parameter Estimation phase, it must have dimension [1, p] in which p the number of parameters.
maxBasis (int) – Integer input indicating the maximum number of modes to use.
alpha_coeff (np.ndarray (optional, Default: None)) – Array with the estimated coefficients \(\alpha_n\), they will be used if the input alpha_coeffs is not None.
- Returns:
reconstruction (np.ndarray) – Reconstructed field using maxBasis POD modes.
resid (np.ndarray) – Residual field using maxBasis POD modes.
- synt_test_error(test_snap: FunctionsList, mu_estimated: ndarray, maxBasis: int, alpha_coeffs: ndarray | None = None, verbose=False) namedtuple [source]
The maximum absolute \(E_N\) and relative \(\varepsilon_N\) error on the test set is computed, by projecting it onto the reduced space in \(L^2\) with the coefficients estimated through callable maps or given as input.
\[E_N = \max\limits_{\boldsymbol{\mu}\in\Xi_{\text{test}}} \left\| u(\mathbf{x};\,\boldsymbol{\mu}) - \sum_{n=1}^N \alpha_n(\boldsymbol{\mu})\cdot \psi_n(\mathbf{x})\right\|_{L^2}\]\[\varepsilon_N = \max\limits_{\boldsymbol{\mu}\in\Xi_{\text{test}}} \frac{\left\| u(\mathbf{x};\,\boldsymbol{\mu}) - \sum_{n=1}^N \alpha_n(\boldsymbol{\mu})\cdot \psi_n(\mathbf{x})\right\|_{L^2}}{\left\| u(\mathbf{x};\,\boldsymbol{\mu})\right\|_{L^2}}\]The coefficients of the POD basis are obtained by interpolating using the maps.
- Parameters:
test_snap (FunctionsList) – List of snapshots onto which the test error of the POD basis is performed.
mu_estimated (np.ndarray) – Arrays with the estimated parameters from the Parameter Estimation phase, it must have dimension [Ns, p] in which Ns is the number of test snapshots and p the number of parameters. If None, the reduced coefficients alpha_coeffs must be given.
maxBasis (int) – Integer input indicating the maximum number of modes to use.
alpha_coeff (np.ndarray (optional, Default: None)) – Matrix with the estimated coefficients \(\alpha_n\), they will be used if the input alpha_coeffs is not None.
verbose (boolean, optional (Default = False)) – If True, print of the progress is enabled.
- 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.
pyforce.online.pod_projection module
- class pyforce.online.pod_projection.PODproject(modes: FunctionsList, name: str)[source]
Bases:
object
A class to perform the online phase of the POD with projection from true field, in which the modal coefficients are found from the projection of the snapshot onto the reduced space.
- Parameters:
modes (FunctionsList) – List of POD modes computed during the offline phase.
name (str) – Name of the snapshots (e.g., temperature T)
- project(snap: dolfinx.fem.Function, maxBasis: int)[source]
Project snap onto the reduced space of dimension maxBasis, by computing the modal coefficients \(\{\alpha_i\}\)
\[\alpha_i = (u, \psi_i)_{L^2} = \int_\Omega u\cdot \psi_i\,d\Omega\]- Parameters:
snap (Function) – Snap to project.
maxBasis (int) – Integer input indicating the maximum number of modes to use.
- Returns:
coeff – Modal coefficient obtain by projection with POD modes.
- Return type:
np.ndarray
- reconstruct(snap: ndarray, maxBasis: int)[source]
The coefficients of the POD basis are obtained by projection, the snap is approximated using linear combination of the POD modes.
- Parameters:
snap (Function as np.ndarray) – Snap to reconstruct, if a function is provided, the variable is reshaped.
maxBasis (int) – Integer input indicating the maximum number of modes to use.
- Returns:
reconstruction (np.ndarray) – Reconstructed field using maxBasis POD modes.
resid (np.ndarray) – Residual field using maxBasis POD modes.
computational_time (dict) – Dictionary with the CPU time of the most relevant operations during the online phase.
- synt_test_error(test_snap: FunctionsList, maxBasis: int, return_int=False, verbose=False) namedtuple [source]
The maximum absolute \(E_N\) and relative \(\varepsilon_N\) error on the test set is computed, by projecting it onto the reduced space in \(L^2\)
\[E_N = \max\limits_{\boldsymbol{\mu}\in\Xi_{\text{test}}} \left\| u(\mathbf{x};\,\boldsymbol{\mu}) - \sum_{n=1}^N \alpha_n(\boldsymbol{\mu})\cdot \psi_n(\mathbf{x})\right\|_{L^2}\]\[\varepsilon_N = \max\limits_{\boldsymbol{\mu}\in\Xi_{\text{test}}} \frac{\left\| u(\mathbf{x};\,\boldsymbol{\mu}) - \sum_{n=1}^N \alpha_n(\boldsymbol{\mu})\cdot \psi_n(\mathbf{x})\right\|_{L^2}}{\left\| u(\mathbf{x};\,\boldsymbol{\mu})\right\|_{L^2}}\]The coefficients of the POD basis are obtained by interpolating using the maps.
- Parameters:
test_snap (FunctionsList) – List of snapshots onto which the test error of the POD basis is performed.
maxBasis (int) – Integer input indicating the maximum number of modes to use.
verbose (boolean, optional (Default = False)) – If True, print of the progress is enabled.
- 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.
pyforce.online.tr_geim module
- class pyforce.online.tr_geim.TRGEIM(magic_fun: FunctionsList, magic_sen: FunctionsList, mean_beta: ndarray, std_beta: ndarray, name: str)[source]
Bases:
object
This class can be used to perform the online phase of the TR-GEIM algorihtm for synthetic and real measures \(\mathbf{y}\in\mathbb{R}^M\) obtained as evaluations of the magic sensors on the snapshot \(u(\mathbf{x};\,\boldsymbol{\mu})\) as
\[y_m = v_m(u)+\varepsilon_m \qquad \qquad m = 1, \dots, M\]given \(\varepsilon_m\) random noise, or by real experimental data on the physical system.
- Parameters:
magic_fun (FunctionsList) – List of magic functions computed during the offline phase.
magic_sen (FunctionsList) – List of magic sensors computed during the offline phase.
mean_beta (np.ndarray) – Mean values \(\langle\beta_m\rangle\) of the training reduced coefficients
std_beta (np.ndarray) – Standard deviations \(\sigma_{\beta_m}\) of the training reduced coefficients
name (str) – Name of the snapshots (e.g., temperature T)
- compute_measure(snap: dolfinx.fem.Function, noise_value: float, M=None) ndarray [source]
Computes the measurement vector from the snap input, using the magic sensors stored to which synthetic random noise is added. If the dimension M is not given, the whole set of magic sensors is used.
\[y_m = v_m(u) +\varepsilon_m \qquad \qquad m = 1, \dots, M\]If the dimension \(M\) is not given, the whole set of magic sensors is used.
- Parameters:
snap (Function) – Function from which measurements are to be extracted
noise_value (float) – Standard deviation of the noise, modelled as a normal \(\mathcal{N}(0, \sigma^2)\)
M (int, optional (default = None)) – Maximum number of sensor to use (if None is set to the number of magic functions/sensors)
- Returns:
measure – Measurement vector :math:mathbf{y}inmathbb{R}^M`
- Return type:
np.ndarray
- hyperparameter_tuning(snaps: FunctionsList, noise_value: float, lambda_lim=[10, 50], n_samples: int = 20, log_sampling: bool = False, num_repeats: int = 1, M: int | None = None, verbose=False)[source]
The regularising parameter \(\lambda\) of the TR-GEIM linear system is calibrated as the one minimising the absolute error in \(L^2\), namely
\[\hat{\lambda} = \text{arg}\,\min\limits_{\lambda\in\mathbb{R}^+} E_{M, \lambda}\]given \(E_{M, \lambda}\) the average absolute error in \(L^2\) with \(M\) sensors. The optimization problem is solved sampling \(\lambda\) in a specific range.
- Parameters:
snaps (FunctionsList) – List of functions belonging to the validation set to reconstruct
noise_value (float) – Standard deviation of the noise, modelled as a normal \(\mathcal{N}(0, \sigma^2)\)
lambda_lim (tuple of floats, optional (Default = [10, 50])) – Lower and upper bound for \(\lambda^*\) entering in the regularisation parameter of TR-GEIM as \(\lambda = \lambda^* \cdot \sigma^2\), given \(\sigma^2\) as the variance of the noise.
n_samples (int, optional (Default = 20)) – Number of samples for the hyperparameter \(\lambda\) to optimize.
log_sampling (bool, optional (Default = False)) – If True, the sampling of \(\lambda^*\) is logarithmic, otherwise it is linear.
num_repeats (int, optional (Default = 1)) – Number of repetitions of each numerical experiment.
M (int, optional (Default = None)) – Number of sensor to use (if None is set to the number of magic functions/sensors)
verbose (boolean, optional (default = False)) – If true, output is printed.
- Returns:
lambda_opt (float) – Optimal value of \(\hat{\lambda}\)
lambda_star_samples (np.ndarray) – Sample values of \(\lambda^\star\) entering the regularising parameter as \(\lambda=\lambda^\star \cdot \sigma^2\)
ave_abs_err (np.ndarray) – Average absolure reconstruction error associated to each output value of \(\lambda^\star\)
- real_reconstruct(measure: ndarray, reg_param: float)[source]
The interpolant given the measure vector \(\mathbf{y}\) input is computed, by solving the GEIM linear system
\[\left(\mathbb{B}^T\mathbb{B}+\lambda \mathbb{T}^T\mathbb{T}\right)\boldsymbol{\beta} = \mathbb{B}^T\mathbf{y}+\lambda \mathbb{T}^T\mathbb{T} \langle{\boldsymbol{\beta}}\rangle\]then the interpolant is computed and returned
\[\mathcal{I}_M(\mathbf{x}) = \sum_{m=1}^M \beta_m[u] \cdot q_m(\mathbf{x})\]- Parameters:
measure (np.ndarray) – Measurement vector, shaped as \(M \times N_s\), given \(M\) the number of sensors used and \(N_s\) the number of parametric realisation.
reg_param (float) – Regularising parameter \(\lambda\)
- Returns:
interp (np.ndarray) – Interpolant Field \(\mathcal{I}_M\) of GEIM
computational_time (dict) – Dictionary with the CPU time of the most relevant operations during the online phase.
- reconstruct(snap: ndarray, M: int, noise_value: float, reg_param: float) Tuple[dolfinx.fem.Function, dolfinx.fem.Function] [source]
The interpolant for snap \(u\) input is computed, by solving the TR-GEIM linear system
\[\left(\mathbb{B}^T\mathbb{B}+\lambda \mathbb{T}^T\mathbb{T}\right)\boldsymbol{\beta} = \mathbb{B}^T\mathbf{y}+\lambda \mathbb{T}^T\mathbb{T} \langle{\boldsymbol{\beta}}\rangle\]then the inteprolant and residual are computed and returned
\[\mathcal{I}_M[u] = \sum_{m=1}^M \beta_m[u] \cdot q_m\qquad\qquad r_M = \left| u - \mathcal{I}_M[u]\right|\]- Parameters:
snap (Function as np.ndarray) – Snap to reconstruct, if a function is provided, the variable is reshaped.
M (int) – Number of sensor to use
noise_value (float) – Standard deviation of the noise, modelled as a normal \(\mathcal{N}(0, \sigma^2)\)
reg_param (float) – Regularising parameter \(\lambda\)
- Returns:
interp (Function) – Interpolant Field \(\mathcal{I}_M[u]\) of TR-GEIM
resid (Function) – Residual Field \(r_M[u]\)
computational_time (dict) – Dictionary with the CPU time of the most relevant operations during the online phase.
coeff (np.ndarray) – Coefficients of the GEIM expansion \(\boldsymbol{\beta}(\boldsymbol{\mu})\)
- synt_test_error(snaps: FunctionsList, noise_value: float, reg_param: float, M=None, verbose=False) namedtuple [source]
The absolute and relative error on the test set is computed using the reconstruction obtained by solving the TR-GEIM linear system
\[\left(\mathbb{B}^T\mathbb{B}+\lambda \mathbb{T}^T\mathbb{T}\right)\boldsymbol{\beta} = \mathbb{B}^T\mathbf{y}+\lambda \mathbb{T}^T\mathbb{T} \langle{\boldsymbol{\beta}}\rangle\]- Parameters:
snaps (FunctionsList) – List of functions belonging to the test set to reconstruct
M (int, optional (default = None)) – Maximum number of magic functions to use (if None is set to the number of magic functions/sensors)
noise_value (float) – Standard deviation of the noise, modelled as a normal \(\mathcal{N}(0, \sigma^2)\)
reg_param (float) – Regularising parameter \(\lambda\)
verbose (boolean, optional (default = False)) – If true, output is printed.
- 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.
Module contents
pyforce/online.
Online phase of the pyforce library.