Classes and methods
KrylovSolver
This class performs FEM with Krylov iteration: discretizes the solution space, computes the discretization matrices and solves for its eigenpairs using Krylov iteration.
__init__(self, s: Callable[[ProxyFunction, ProxyFunction], CoefficientFunction], m: Callable[[ProxyFunction, ProxyFunction], CoefficientFunction], mesh: comp.Mesh, L: int, tau: float, alpha: Filter, m_min: int = 2, m_max: int = 30)
Parameters:
s: Callable[[ngsolve.comp.ProxyFunction, ngsolve.comp.ProxyFunction], ngsolve.fem.CoefficientFunction]Left-hand-site of the weak formulation of the problem to solve.
m : Callable[[ngsolve.comp.ProxyFunction, ngsolve.comp.ProxyFunction], ngsolve.fem.CoefficientFunction]Right-hand-site of the weak formulation of the problem to solve.
mesh : ngsolve.comp.MeshA
Meshobject of the discretized domain.L : int
L > 0. Number of time-steps in each iteation.tau : tau
tau > 0. Size of each time-step.alpha : FilterA
Filterobject representing discrete filter function (dff).m_min : int, optionalFrom this iteration onwards the results are saved in
KrylovSolver.results. The default is 2.m_max : int, optionalMaximal number of Krylov iterations. The default is 30.
discretize(self, **kwargs)
This method discretizes the problem: creates solution space with its basis, prepares matrices \(M\) and \(S\).
Parameters:
**kwargskwargs for generation of
ngsolve.H1solution space.
solve(self)
Core method, that performs the Krylov iteration to compute eigenvalues \(\omega^2\) and
m_maxin theKrylovSolver.resultsproperty.
compute_true_eigvals(self)
Computes true eigenvalues of the \(M^{-1} S\) matrix. If this method has been called, true eigenvalues are added to plots in
KrylovSolver.plot_results()method.NOTE: This method should be used in small-scale examples for comparison of the results of the Krylov iteration with true eigenvalues only! In large-scale examples it ruins the performance of the whole method, since it uses direct solver on large matrices \(S\) and \(M\).
plot(self, start: float, end: float, title: str="", plot_filter: bool=True, label_om: str=r"$\omega$", label_step: str=r"$k$", label_filter: str=r"$|\tilde{\beta}_{\vec{\alpha}}(\omega)|$", ev_marker: str="x", ev_color: str="blue", filter_plot_kwargs: dict={"color":"red"})
Generates plot presenting obtained resonances (\(\omega\)). The resonance-axis is scaled in \(\omega\) (presents square roots of eigenvalues).
Parameters:
start : floatstart point of the plot.
end : floatend point of the plot.
title : str, optionalTitle of the plot. The default is
"".plot_filter : bool, optionalIf
True, plot of the filter function is appended to the plot. The default isTrue.label_om : str, optionalLabel on the omega (horizontal)-axis. The default is
r"$\\omega$".label_step : str, optionalLabel on the iteration-step
k(vertical left)-axis. The default isr"$k$".label_filter : str, optionalLabel on the filter function (vertical right)-axis. The default is
r"$|\\tilde{\\beta}_{\\vec{\\alpha}}(\\omega)|$".ev_marker : str, optionalMarker for omegas. The default is
"x".ev_color : str, optionalColor of eigenvalues. The default is
"blue".filter_plot_kwargs : dict, optionalDictionary with kwargs for the plot of the filter function. All kwargs of the
plt.plot()method are supported. The default is{"color":"red"}.
- Raises:
RuntimeErrorIf there are no results in the
KrylovSolver. Usesolve()method and try again.
plot2(self, start: float, end: float, title: str="", plot_filter: bool=True, label_om: str=r"$\omega^2$", label_step: str=r"$k$", label_filter: str=r"$|\tilde{\beta}_{\vec{\alpha}}(\omega^2)|$", ev_marker: str="x", ev_color: str="blue", filter_plot_kwargs: dict={"color":"red"})
Generates plot presenting obtained eigenvalues (\(\omega^2\)). The eigenvalue-axis is scaled in \(\omega^2\) (presents eigenvalues).
- Parameters
see method
KrylovSolver.plot()- Raises
see method
KrylovSolver.plot()
get_single_result(self, ev: float, k: int=-1) -> tuple[float, np.array]
Returns computed eigenvalue closest to given ev with its eigenvector after the
k-th step of the Krylov iteration.Parameters:
ev : floatEigenvalue (\(\omega^2\)), to which closest value should be returned.
k : int, optionalStep of the itereation. Use -1 for last iteration. The default is -1.
- Raises:
RuntimeErrorIf there are no results in the KrylovSolver. Use solve() method and try again.
ValueErrorIf given step k is not in stored results.
- Returns:
floatEigenvalue (\(\omega^2\)) in results of the k-th step closest to ev.
np.arrayEigenvector to the sought eigenvalue.
Results
A simple dictionary-like class to store results of the Krylov iteration.
Key
kis the number of iteration betweenm_minandm_max(-1 refers to the last iteration).Value is a
Tuple[np.ndarray, np.ndarray]]. The first array (eigvals) contains obtained eigenvalues (\(\omega^2\) in this step). The second one (eigvecs) contains eigenvectors in columns.eigvecs[:,i]is an eigenvector toeigvals[i].
FilterGenerator
This dataclass contains methods, that generate weights (\(\alpha\)) in standard way: by \(L_2\) minimization or collocation / least-squares in Chebyshev nodes.
- Parameters:
_L: intnumber of time-steps
_tau: floattime-step
_om_min: floatstart of the target interval
_om_max: floatend of the target interval
_om_end: floatend of the controlled interval
chebyshev(self, K: int) -> Filter
Returns weights (as a
Filter) obtained by the collocation or least-squares fitting in Chebyshev nodes in \(\omega^2\).Parameters:
K : intNumber of nodes.
- Returns:
FilterA
Filterobject with computed weights.
l2(self, K: Optional[int] = 20) -> Filter
Returns weights (as a
Filter) obtained by \(L_2\) minimization.
- Parameters:
K : Optional[int], optionalNumber of sample points for numerical quadrature in each unit. The default is 20.
- Returns:
FilterA
Filterobject with computed weights.
fourier(self) -> Filter
Returns weights (as Filter) obtained by inverse Fourier transform. Note: this method works for negative Laplacian problem only!
- Returns:
FilterA
Filterobject with computed weights.
plot_chebyshev_nodes(self, N: int, ax: Optional[Axes] = None, marker="x", **kwargs) -> Axes
Plots
NChebyshev nodes in \(\omega^2\) on \(omega\)-scaled axis.
- Parameters:
N : intNumber of nodes.
ax : Optional[Axes], optionalAn
Axesobject, where nodes should be plotted. IfNone, plot is on a new axis. The default isNone.marker : str, optionalA
matplotlibmarker. The default is"x".**kwargskwargs for
matplotlib.axes.Axes.plot()method.- Returns:
Axes
Axesobject with plotted nodes.
Filter
Class to store filter as a numpy ndarray (actually evaluation of weights \(\alpha\) at points \(0, \tau, 2\tau, ..., \tau (L-1)\) with its parameters: time-step tau, omega_end, number of time-steps L and derivation method of the filter (FilterType).
__new__(cls, array_input, filter_type, om_end: float, tau: float)
Constructor of a new filter.
- Parameters:
array_inputEvaluation of weights alpha at points \(0, \tau, 2\tau, ..., \tau (L-1)\) as a
list,tupleor anything that can be casted to a numpyndarray.filter_type : FilterTypeFilter generation method.
om_end : float\(\omega_{\mathrm{end}}\),
om_end > 0.tau : floatTime-step,
tau > 0.- Returns:
objA new
Filterobject.
plot(self, start: Optional[float] = 0, end: Optional[float] = None, ax: Optional[Axes] = None, num: Optional[int] = 10000, **kwargs) -> Axes
This method plots filter function \(\beta(\omega)\). The method creates new axis or creates plot on given one.
- Parameters:
start : Optional[float], optionalStart of the plot. The default is 0.
end : Optional[float], optionalEnd of the plot. The default is
None: in this caseend = om_end.ax : Optional[Axes], optionalAn
Axesobject, where the plot is created, if not None. Otherwise the method plots on a new axis. The default isNone.num : Optional[int], optionalFineness of the plot, i.e., number of sample points in the interval (
start,end). The default is 10000.**kwargsKwargs for
matplotlib.axes.Axes.plot()method.- Returns:
Axes
Axesobject with the plot.
plot2(self, start: Optional[int] = 0, end: Optional[int] = None, ax: Optional[Axes] = None, num: Optional[int] = 10000, **kwargs) -> Axes:
Copy of the function Filter.plot(). The only exception is that it plots \(\beta(\omega^2)\), not \(\beta(\omega)\).
- Parameters:
see
Filter.plot().- Returns:
see
Filter.plot().
FilterType
A simple Enum to distinguish types of filter functions.