3.2.9. eigen Command
- eigen <$type> <$solver> <-findLargest> $numEigenvalues
eigenvalues = ops.eigen(type, solver, '-findLargest', numEigenvalues)
Argument |
Type |
Description |
|---|---|---|
$numEigenvalues |
integer |
number of eigenvalues to compute |
$type |
string |
type of eigenvalue problem: -generalized (also -frequency), \(\mathbf{K} \Phi = \lambda \mathbf{M} \Phi\), or -standard, \(\mathbf{K} \Phi = \lambda \Phi\) (optional: default = -generalized). The dash may be left out |
$solver |
string |
eigen solver: -genBandArpack, -fullGenLapack, -symmBandLapack or -symmGenLapack, and in Python also PythonSparse (see below) (optional: default = -genBandArpack) |
-findLargest |
string |
compute the largest eigenvalues instead of the smallest (optional) |
The command returns a list of the eigenvalues \(\lambda\).
Discussion
This command is used to perform an eigenvalue analysis of the model, e.g. to obtain its natural frequencies and mode shapes.
The generalized eigenvalue problem, for the stiffness matrix \(\mathbf{K}\) and mass matrix \(\mathbf{M}\) of size \(n \times n\), is
where \(\lambda\) is an eigenvalue and \(\Phi\) the associated eigenvector (mode shape). For a structural model, \(\lambda = \omega^2\), where \(\omega\) is the circular natural frequency, so the period of a mode is \(T = 2 \pi / \sqrt{\lambda}\).
The standard eigenvalue problem, \(\left( \mathbf{K} - \lambda \mathbf{I} \right) \Phi = 0\), uses the stiffness matrix only.
Note
The eigenvectors are stored at the nodes. They can be obtained with the nodeEigenvector Command command, recorded with the
eigenresponse of the Node Recorder, or printed with the printModel Command command.The model must have mass for a generalized eigenvalue analysis.
The default solver, -genBandArpack, uses ARPACK, an iterative solver. It can compute at most \(N - 1\) eigenvalues, where \(N\) is the number of degrees-of-freedom with mass. To compute all of them, use -fullGenLapack.
-fullGenLapack stores the full matrices and computes all the eigenvalues with LAPACK. It is accurate, but slow and memory-intensive for large models.
-symmBandLapack only solves the standard eigenvalue problem of the stiffness matrix, \(\mathbf{K} \Phi = \lambda \Phi\).
-symmGenLapack solves the generalized eigenvalue problem with the LAPACK symmetric generalized eigen solver. It is not available on Windows, and in the Tcl interpreter of versions up to 3.8.0.
The eigenvalues and eigenvectors are used by the modalDamping Command command.
-findLargest is only meaningful if every degree-of-freedom has mass. Degrees-of-freedom with no mass, e.g. rotations, have infinite eigenvalues, and the command then returns zeros.
Warning
The default eigen solver uses ARPACK, an iterative eigenvalue solver. Like other iterative methods, ARPACK can struggle to compute the eigenvectors accurately when the model has repeated (degenerate) eigenvalues, or clusters of very closely spaced eigenvalues, as it is then hard to form an orthonormal basis for the eigenspace numerically.
In such cases ARPACK may fail to find all the requested eigenvalues, or may return the correct eigenvalues but incorrect eigenvectors. When modalDamping Command is used, incorrect eigenvectors give incorrect damping forces. If you are unsure whether this affects your model, change the solver and compare the results: when the problem exists, the eigenvectors change greatly with the solver.
Small changes to the model can avoid the problem. In practice, material properties and nodal masses are rarely exactly the same, and introducing slight variations in the model helps ARPACK form the basis correctly.
As an illustration, consider the following script (provided by Michael H. Scott), which has two identical, uncoupled two-mass systems, and so two equal eigenvalues:
model Basic -ndm 1 -ndf 1
node 0 0; fix 0 1
node 1 0; mass 1 1.0
node 2 0; mass 2 1.0
node 3 0; mass 3 1.0
node 4 0; mass 4 1.0
set k 610
uniaxialMaterial Elastic 1 $k
element zeroLength 1 0 1 -mat 1 -dir 1
element zeroLength 2 1 2 -mat 1 -dir 1
element zeroLength 3 0 3 -mat 1 -dir 1
element zeroLength 4 3 4 -mat 1 -dir 1
system ProfileSPD
numberer RCM
set nModes 2
set eigV [eigen $nModes]
puts "Eigenvalues: $eigV"
# print eigenvectors
for {set i 1} {$i <= 4} {incr i 1} {
puts "$i [nodeEigenvector $i 1] [nodeEigenvector $i 2]"
}
With the default ARPACK solver, the two mode shapes found mix the two uncoupled systems:
Eigenvalues: 232.99926686256409880116 232.99926686256409880116
1 0.50235827201282656773 -0.15501409223134487991
2 0.81283275864641846287 -0.25081806996552685751
3 0.15501409223134465787 0.50235827201282678978
4 0.25081806996552685751 0.81283275864641868491
Changing the mass at node 4 slightly, with node 4 0; mass 4 1.0000000001, separates the two eigenvalues, and ARPACK then finds mode shapes that each involve only one of the two systems.
For small problems, e.g. in an academic setting, the -fullGenLapack solver gives the correct eigenvalues and eigenvectors of the original problem:
set eigV [eigen -fullGenLapack $nModes]
gives:
Eigenvalues: 232.99926686256412722287 232.99926686256412722287
1 0.00000000000000000000 -0.52573111211913370333
2 0.00000000000000000000 -0.85065080835203987775
3 0.52573111211913370333 0.00000000000000000000
4 0.85065080835203987775 0.00000000000000000000
Example:
The following example computes 10 eigenvalues with the default solver, then with the -fullGenLapack solver, then 10 eigenvalues of the stiffness matrix only, and finally computes the periods of the modes.
# 10 eigenvalues using the default solver (-genBandArpack)
set eigenvalues [eigen 10]
# 10 eigenvalues using the -fullGenLapack solver
set eigenvalues [eigen -fullGenLapack 10]
# 10 eigenvalues of the stiffness matrix
set eigenvalues [eigen -standard -symmBandLapack 10]
# periods of the modes of the generalized problem
set eigenvalues [eigen 10]
foreach lambda $eigenvalues {
puts "T = [expr 2.0*acos(-1.0)/sqrt($lambda)]"
}
import math
# 10 eigenvalues using the default solver (-genBandArpack)
eigenvalues = ops.eigen(10)
# 10 eigenvalues using the -fullGenLapack solver
eigenvalues = ops.eigen('-fullGenLapack', 10)
# 10 eigenvalues of the stiffness matrix
eigenvalues = ops.eigen('-standard', '-symmBandLapack', 10)
# periods of the modes of the generalized problem
eigenvalues = ops.eigen(10)
for lam in eigenvalues:
print('T =', 2.0*math.pi/math.sqrt(lam))
# PythonSparse: use a custom Python solver (e.g. SciPy eigsh)
eigenvalues = ops.eigen('PythonSparse', 10, {'solver': solver, 'scheme': 'CSR'})
Code Developed by: fmk
3.2.9.1. PythonSparse Eigenvalue Solver
The PythonSparse eigen solver delegates the generalized eigenvalue problem \(\mathbf{K}\phi = \lambda \mathbf{M}\phi\) to user-defined Python objects. Sparse stiffness (K) and mass (M) matrix buffers are exposed via zero-copy memoryviews, enabling the use of SciPy, CuPy, or custom eigensolvers without recompiling OpenSees. See example below for a SciPy eigsh implementation.
eigenvalues = ops.eigen('PythonSparse', numEigenvalues, {'solver': solver, 'scheme': scheme})
Perform the eigenvalue analysis using a user-defined Python solver. The dictionary is the solver configuration; its keys are given below.
Argument |
Type |
Description |
|---|---|---|
$numEigenvalues |
int |
Number of eigenvalues (and eigenvectors) to compute. |
dict |
dict |
Solver configuration; see dict options table below. |
Key |
Type |
Description |
|---|---|---|
solver |
Python object |
A Python object with a |
scheme |
string |
Sparse format: |
Note
The PythonSparse eigen interface is available only when using the Python interpreter (OpenSeesPy).
The solver object must implement a solve(**kwargs) method. OpenSees passes the matrix data as keyword arguments: memoryviews (buffer-like objects) for arrays, and scalars for metadata. Use numpy.frombuffer to interpret memoryviews as NumPy arrays without copying. The structure format depends on scheme:
CSR/CSC format — compressed storage uses index_ptr and indices:
Keyword |
Type |
Description |
|---|---|---|
index_ptr |
memoryview |
Row pointers (CSR) or column pointers (CSC), int32. |
indices |
memoryview |
Column indices (CSR) or row indices (CSC), int32. |
k_values |
memoryview |
Stiffness matrix K coefficients (float64). |
m_values |
memoryview |
Mass matrix M coefficients (float64). |
eigenvalues |
memoryview |
Output buffer for eigenvalues (float64, writable). Write in place. |
eigenvectors |
memoryview |
Output buffer for eigenvectors (float64, writable). Write in place, row-major: mode 0, mode 1, … |
num_eqn |
int |
Number of equations. |
nnz |
int |
Number of nonzeros (K and M share sparsity pattern). |
matrix_status |
string |
|
storage_scheme |
string |
|
num_modes |
int |
Number of modes requested. |
generalized |
bool |
True for generalized (K, M); False for standard (K only). |
find_smallest |
bool |
True: smallest eigenvalues (e.g., frequency); False: largest. |
COO format — coordinate storage uses row_indices and col_indices instead of index_ptr/indices:
Keyword |
Type |
Description |
|---|---|---|
row_indices |
memoryview |
Row indices for each nonzero, int32 (COO only). |
col_indices |
memoryview |
Column indices for each nonzero, int32 (COO only). |
k_values |
memoryview |
Stiffness matrix K coefficients (float64). |
m_values |
memoryview |
Mass matrix M coefficients (float64). |
eigenvalues |
memoryview |
Output buffer for eigenvalues (float64, writable). Write in place. |
eigenvectors |
memoryview |
Output buffer for eigenvectors (float64, writable). Write in place. |
num_eqn |
int |
Number of equations. |
nnz |
int |
Number of nonzeros. |
matrix_status |
string |
|
storage_scheme |
string |
|
num_modes |
int |
Number of modes requested. |
generalized |
bool |
True for generalized (K, M); False for standard (K only). |
find_smallest |
bool |
True: smallest eigenvalues; False: largest. |
The solve method should return None on success, or raise an exception on failure.
Warning
In-place output required
You must write eigenvalues and eigenvectors directly into the eigenvalues and eigenvectors buffers. OpenSees reads from these buffers; it does not use return values.
Do: Use in-place assignment, e.g. np.frombuffer(eigenvalues, ...)[:] = eigvals and np.frombuffer(eigenvectors, ...)[:] = eigvecs.T.flatten()
Do not: Return the results or assign to new arrays without copying back into the buffers. OpenSees will use uninitialized or stale data.
Example — SciPy Generalized Eigenvalue Solver
This example uses SciPy and its eigsh (ARPACK symmetric eigensolver) to solve the generalized eigenvalue problem:
import numpy as np
import scipy.sparse as sp
import scipy.sparse.linalg as sp_linalg
import openseespy.opensees as ops
class SciPyGeneralizedEigenSolver:
def __init__(self, maxiter=None, tol=0.0):
self.maxiter = maxiter
self.tol = tol
self._k_matrix = None
self._m_matrix = None
def solve(self, **kwargs):
index_ptr = kwargs['index_ptr']
indices = kwargs['indices']
k_values = kwargs['k_values']
m_values = kwargs['m_values']
eigenvalues = kwargs['eigenvalues']
eigenvectors = kwargs['eigenvectors']
num_eqn = kwargs['num_eqn']
nnz = kwargs['nnz']
matrix_status = kwargs['matrix_status']
num_modes = kwargs['num_modes']
find_smallest = kwargs['find_smallest']
indptr = np.frombuffer(index_ptr, dtype=np.int32, count=num_eqn + 1)
idx = np.frombuffer(indices, dtype=np.int32, count=nnz)
k_data = np.frombuffer(k_values, dtype=np.float64, count=nnz)
m_data = np.frombuffer(m_values, dtype=np.float64, count=nnz)
if matrix_status == 'STRUCTURE_CHANGED' or self._k_matrix is None:
self._k_matrix = sp.csr_matrix((k_data, idx, indptr),
shape=(num_eqn, num_eqn))
self._m_matrix = sp.csr_matrix((m_data, idx, indptr),
shape=(num_eqn, num_eqn))
elif matrix_status == 'COEFFICIENTS_CHANGED':
self._k_matrix.data[:] = k_data
self._m_matrix.data[:] = m_data
eigsh_kwargs = {
'k': num_modes,
'M': self._m_matrix,
'which': 'LM',
}
if find_smallest:
eigsh_kwargs['sigma'] = 0.0
if self.maxiter is not None:
eigsh_kwargs['maxiter'] = self.maxiter
if self.tol > 0.0:
eigsh_kwargs['tol'] = self.tol
eigvals, eigvecs = sp_linalg.eigsh(self._k_matrix, **eigsh_kwargs)
np.frombuffer(eigenvalues, dtype=np.float64,
count=num_modes)[:] = eigvals[:num_modes]
np.frombuffer(eigenvectors, dtype=np.float64,
count=num_modes * num_eqn)[:] = eigvecs.T.flatten()
return None
solver = SciPyGeneralizedEigenSolver()
eigenvalues = ops.eigen('PythonSparse', 10, {'solver': solver, 'scheme': 'CSR'})
See also
PythonSparse System — PythonSparse interface for linear systems
The EXAMPLES/SolverBenchmark script demonstrates the PythonSparse eigen solver.
Code Developed by: fmk