File size: 1,430 Bytes
4aa7bc6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
"""Independent conforming P1 finite-element regressions (not proof certificates)."""
from __future__ import annotations
import numpy as np
from scipy.sparse import coo_matrix
from scipy.sparse.linalg import eigsh


def eigenvalues(edges, dirichlet=(), count=20, density=60):
    vertices=sorted({e.u for e in edges}|{e.v for e in edges})
    ids={v:i for i,v in enumerate(vertices)}; total=len(ids)
    entries=[]
    for e in edges:
        length=float(e.length)
        ne=max(8,int(np.ceil(density*length)))
        nodes=[ids[e.u]]+list(range(total,total+ne-1))+[ids[e.v]]
        total+=ne-1
        h=length/ne
        for a,b in zip(nodes[:-1],nodes[1:]):
            for i,gi in enumerate((a,b)):
                for j,gj in enumerate((a,b)):
                    entries.append((gi,gj,(1 if i==j else -1)/h,h*(2 if i==j else 1)/6))
    rows,cols,kdata,mdata=map(np.array,zip(*entries))
    K=coo_matrix((kdata,(rows.astype(int),cols.astype(int))),shape=(total,total)).tocsr()
    M=coo_matrix((mdata,(rows.astype(int),cols.astype(int))),shape=(total,total)).tocsr()
    excluded={ids[v] for v in dirichlet}
    keep=[v for v in range(total) if v not in excluded]
    K=K[keep,:][:,keep]; M=M[keep,:][:,keep]
    if count>=len(keep):
        raise ValueError('Too many requested eigenvalues for this mesh.')
    vals=eigsh(K,k=count,M=M,sigma=-1e-5,which='LM',return_eigenvectors=False,tol=1e-10)
    return np.sort(vals)