-
Notifications
You must be signed in to change notification settings - Fork 1
/
dolfin_to_nparrays.py
142 lines (97 loc) · 2.89 KB
/
dolfin_to_nparrays.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
from dolfin import *
import numpy as np
import scipy.sparse as sps
parameters.linear_algebra_backend = "uBLAS"
def get_sysNSmats( V, Q): # , velbcs ):
""" Assembles the system matrices for Stokes equation
in mixed FEM formulation, namely
[ A B' ] as [ Aa Grada ] : W -> W'
[ B 0 ] [ Diva 0 ]
for a given trial and test space W = V * Q and boundary conds.
Plus the velocity mass matrix M.
"""
u = TrialFunction(V)
p = TrialFunction(Q)
v = TestFunction(V)
q = TestFunction(Q)
ma = inner(u,v)*dx
mp = inner(p,q)*dx
aa = inner(grad(u), grad(v))*dx
grada = div(v)*p*dx
diva = q*div(u)*dx
# Assemble system
M = assemble(ma)
A = assemble(aa)
Grad = assemble(grada)
Div = assemble(diva)
MP = assemble(mp)
# Convert DOLFIN representation to numpy arrays
rows, cols, values = M.data()
Ma = sps.csr_matrix((values, cols, rows))
rows, cols, values = MP.data()
MPa = sps.csr_matrix((values, cols, rows))
rows, cols, values = A.data()
Aa = sps.csr_matrix((values, cols, rows))
rows, cols, values = Grad.data()
BTa = sps.csr_matrix((values, cols, rows))
rows, cols, values = Div.data()
Ba = sps.csr_matrix((values, cols, rows))
return Ma, Aa, BTa, Ba, MPa
def setget_rhs(V, Q, fv, fp, velbcs=None, t=None):
v = TestFunction(V)
q = TestFunction(Q)
fv = inner(fv,v)*dx
fp = inner(fp,q)*dx
fv = assemble(fv)
fp = assemble(fp)
fv = fv.array()
fv = fv.reshape(len(fv), 1)
fp = fp.array()
fp = fp.reshape(len(fp), 1)
return fv, fp
def get_curfv(V, fv, invinds, tcur):
"""get the fv at innernotes at t=tcur
"""
v = TestFunction(V)
fv.t = tcur
fv = inner(fv,v)*dx
fv = assemble(fv)
fv = fv.array()
fv = fv.reshape(len(fv), 1)
return fv[invinds,:]
def get_convvec(u0, V):
"""return the convection vector e.g. for explicit schemes
"""
v = TestFunction(V)
ConvForm = inner(grad(u0)*u0, v)*dx
ConvForm = assemble(ConvForm)
ConvVec = ConvForm.array()
ConvVec = ConvVec.reshape(len(ConvVec), 1)
return ConvVec
def condense_sysmatsbybcs(Ma,Aa,BTa,Ba,fv,fp,velbcs):
"""resolve the Dirichlet BCs and condense the sysmats
to the inner nodes"""
auxu = np.zeros((len(fv),1))
bcinds = []
for bc in velbcs:
bcdict = bc.get_boundary_values()
auxu[bcdict.keys(),0] = bcdict.values()
bcinds.extend(bcdict.keys())
# putting the bcs into the right hand sides
fvbc = - Aa*auxu # '*' is np.dot for csr matrices
fpbc = - Ba*auxu
# indices of the innernodes
innerinds = np.setdiff1d(range(len(fv)),bcinds).astype(np.int32)
# extract the inner nodes equation coefficients
Mc = Ma[innerinds,:][:,innerinds]
Ac = Aa[innerinds,:][:,innerinds]
fvbc= fvbc[innerinds,:]
Bc = Ba[:,innerinds]
BTc = BTa[innerinds,:]
bcvals = auxu[bcinds]
# removal of the indefiniteness in pressure via pi_0 !=! 0
# eeeh, better not here :/
# Bc = Ba[1:,innerinds]
# BTc = BTa[innerinds,1:]
# fp = fp[1:,0]
return Mc, Ac, BTc, Bc, fvbc, fpbc, bcinds, bcvals, innerinds