Ai
3 Star 0 Fork 0

mirrors_lepy/lscm-python

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
solveLinearSystem.py 1.81 KB
一键复制 编辑 原始数据 按行查看 历史
xinzhuohuZJU 提交于 2021-09-25 22:09 +08:00 . first update
"""
Created on 2021-07-29 14:45
Last Update Time: 2021-07-29
@author: Xinzhuo Hu
"""
import numpy as np
import pymesh
from scipy import sparse
from scipy.sparse.linalg import spsolve_triangular
# Ku = f with 2 constrained vertices
def solveLSCMsystem(K, fixedVars, fixedVarValues):
# number of vertices
nv = int(K.shape[0]/2)
n = K.shape[0]
# full matrix Kf
Kind_lower = np.tril_indices(n,-1)
Kf = np.copy(K)
Kf[Kind_lower] = Kf.T[Kind_lower]
# lower triangle matrix Kl
Kl = np.zeros((n,n),dtype=float)
Kl[Kind_lower] = K.T[Kind_lower]
# build right hand f
# original f
fori = np.zeros([2*nv, ],dtype=float)
c = np.zeros([2*nv, ],dtype=float)
c[fixedVars,] = fixedVarValues
# true f
# K is a upper triangle symmetric sparse matrix
f = np.dot(Kf,c)
ft = np.copy(f)
ft = fori - f
ft[fixedVars] = fixedVarValues
# test
ft_ind = np.where(ft!=0)
# Modify LSCM Matrix
num_fixedvars = fixedVars.shape[0]
for i in range(num_fixedvars):
idx = fixedVars[i]
K[idx,:] = 0
K[:,idx] = 0
K[idx,idx] = 1
# try Kf
Kf[idx,:] = 0
Kf[:,idx] = 0
Kf[idx,idx] = 1
# try Kl
Kl[idx,:] = 0
Kl[:,idx] = 0
Kl[idx,idx] = 1
# convert to sparse matrix
# sK = sparse.csr_matrix(K)
# x = spsolve_triangular(sK, ft, lower=False)
# x_ind = np.where(x!=0)
# print(np.allclose(sK.dot(x), ft))
# PyMesh solvers
sKf = sparse.csc_matrix(Kf)
solver = pymesh.SparseSolver.create("LDLT")
solver.compute(sKf)
uv_vec = solver.solve(ft)
print('Constrained Values Test: ',uv_vec[fixedVars, 0])
print(np.allclose(sKf.dot(uv_vec), ft))
# assemble uv
u = uv_vec[0:nv]
v = uv_vec[nv:2*nv]
uv = np.hstack((u,v))
return uv
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors_lepy/lscm-python.git
git@gitee.com:mirrors_lepy/lscm-python.git
mirrors_lepy
lscm-python
lscm-python
main

搜索帮助