nodes,faces=load("ex2fin.msh","./",seltype=2)
dist=np.sqrt(nodes[:,0]**2+nodes[:,1]**2)
amax=dist.max()
edgsel=dist>amax-0.01
#amax=nodes[:,0].max()
#edgsel=nodes[:,0]>amax-0.01*scal
edgval=5 #okrajova teplota
conduc=2. #faktor vodivosti
influx=10 #
dist=np.sqrt(nodes[:,0]**2+(nodes[:,1]+0.2)**2)
heasel=dist<0.03 #heat source
#testovani "nepripojenych" uzlu
from collections import Counter
cc=Counter(list(faces[-3:].ravel()-1))
ocnt=np.zeros(len(nodes))
ocnt[cc.keys()]=cc.values()
print("nepripojeno %i bodu"%sum(ocnt==0))
faces[2:]-=1 #opravime na indexovani od 0, nikoli od 1
edgsel[ocnt==0]=False #nepripojene vyradime
nint=sum(ocnt>0)-sum(edgsel)
nodind=np.zeros(len(nodes),dtype='int') #cislovani bodu zvlast pro interni a externi (okrajove) vrcholy
nodind[(edgsel==False)*(ocnt>0)]=np.r_[:nint]
nodind[edgsel]=np.r_[:sum(edgsel)]
#----------------
matI=np.zeros((nint,nint))
matX=np.zeros((nint,sum(edgsel)))
stat=[]
nnint=1./(2*3*4) #integral ni nj
for fx in faces[2:].T:
fwk=nodes[fx].copy()
fwk[:,2]=1
surf=abs(np.linalg.det(fwk))
alpha,beta=-np.roll((np.roll(fwk[:,:2],1,0)-fwk[:,:2]),1,0).T/surf
stat.append(list(alpha)+list(beta)+[surf])
inter=edgsel[fx]==False #
for i in np.r_[:3][inter]:
ni=nodind[fx[i]]
matI[ni,ni]+=surf/6*(nnint-(alpha[i]**2+beta[i]**2)/2.*conduc)
for j in [(i+1)%3,(i+2)%3]:
val=surf/6*(nnint-(alpha[i]*alpha[j]+beta[i]*beta[j])/2.*conduc) #grad ni grad nj
if inter[j]: matI[ni,nodind[fx[j]]]+=val
else: matX[ni,nodind[fx[j]]]+=val
Rside=-matX.dot(np.ones(sum(edgsel))*edgval)
Rside[nodind[heasel]]-=influx #influx
from scipy import sparse as sp
from scipy.sparse import linalg
matS=sp.csc_matrix(matI)
try:
Csol=linalg.splu(matS)
Cred=Csol.solve(rhs=Rside)
except:
dia=matI.diagonal()
print("diagonala:",dia.min(),dia.max(),len(dia))
ix,iy=matS.nonzero()
iix=ix[ix!=iy]
iiy=iy[ix!=iy]
qsel=matS[iix,iiy]
print("mimo ni:",qsel.max(),qsel.min(),len(iix))
else:
#--------------------------------------
dx,dy=nodes[(ocnt>0),:2].T
tmpall=np.zeros(dx.shape)
tmpall[edgsel[ocnt>0]==False]=Cred
tmpall[edgsel[ocnt>0]]=edgval
pl.scatter(dx,dy,c=tmpall)
pl.colorbar()
#innd=nodes[edgsel==False,:2]