5import matplotlib.pyplot
as plt
9from sympy.printing
import cxxcode
12 from sympy.printing.cxx
import CXX11CodePrinter
14 from sympy.printing.cxxcode
import CXX11CodePrinter
16from mpi4py.MPI
import COMM_WORLD
as mpi
23 largs = np.array_split(args, msize)[mrank]
24 if len(largs.shape) == 1:
25 lres = [expr(arg)
for arg
in largs]
27 lres = [expr(*arg)
for arg
in largs]
28 res = mpi.gather(lres, root=0)
30 return np.concatenate(res)
34 args = [(i,j)
for j
in range(12)
for i
in range(j+1)]
37 matrix = sym.Matrix(np.zeros((12, 12)))
38 for (i,j), val
in zip(args, vals):
49 vector = sym.Matrix(np.zeros(12))
50 for i, val
in zip(args, vals):
56 indices = expr.indices
59 return f
"{self._print(expr.base.label)}{self._print(indices[0])}"
62 return f
"{self._print(expr.base.label)}{''.join(self._print(i) for i in indices)}"
63CXX11CodePrinter._print_Indexed = _print_Indexed
67 s = cxxcode(sym.simplify(v), standard=
'C++11')
70 s = re.sub(
r'std::pow\(([^)]+), (\d+)\)',
r'std::pow(\1,\2)', s)
71 s = re.sub(
r'std::pow\(([^)]+),2\)',
r'(\1*\1)', s)
72 s = re.sub(
r'std::pow\(([^)]+),3\)',
r'(\1*\1*\1)', s)
73 s = re.sub(
r'\b(X|Y)\b',
r'e.\1', s)
74 s = re.sub(
r'\bU(\d\d)\b',
r'U[e.i\1]', s)
75 s = re.sub(
r'\bJ(\d\d)\b',
r'J[e.n\1]', s)
76 s = re.sub(
r'\bP(\d\d)(\d)\b',
r'P[e.n\1].c\2\2', s)
77 s = re.sub(
r'(d?G)(\d)\b',
r'\1.c\2\2', s)
81sidx =
lambda i:
"e.i{}{}".format(*idx[i])
84 for (i,j), kval
in zip(kij, kvals):
85 print(f
"K({sidx(i)}, {sidx(j)}) += {kval};", file=file)
86 for i, fval
in enumerate(fvals):
87 print(f
"F[{sidx(i)}] += {fval};", file=file)
91 kij = [(i,j)
for j
in range(12)
for i
in range(j+1)]
92 kvals =
evaluate(
lambda i,j:
' + '.join(
cpp(K[i,j])
for K
in KK), kij)
93 fvals =
evaluate(
lambda i:
' + '.join(
cpp(F[i])
for F
in FF), range(12))
98 with open(fname,
'w')
as file:
104A, B, C, D, = sym.symbols(
'A B C D')
106x, y = sym.symbols(
'x y')
107X, Y = sym.symbols(
'X Y')
112Φ = np.linalg.inv(np.array([[1, 0, 0, 0], [0, 1, 0, 0], [1, 1, 1, 1], [0, 1, 2, 3]])).T
114φx = [sum(int(c) * ξ**i
for i, c
in enumerate(p))
for p
in Φ]
117φx = np.array([sym.expand(a)
for a
in φx])
119φy = [sum(int(c) * ζ**i
for i, c
in enumerate(p))
for p
in Φ]
122φy = np.array([sym.expand(a)
for a
in φy])
124idx = [(0,0), (0,1), (1,0), (0,2), (0,3), (1,2), (2,0), (2,1), (3,0), (2,2), (2,3), (3,2)]
125φ = np.array([φx[idx[i][0]] * φy[idx[i][1]]
for i
in range(12)])
127dφx = np.array([sym.expand(sym.diff(a, x))
for a
in φ])
128dφy = np.array([sym.expand(sym.diff(a, y))
for a
in φ])
130U = sym.IndexedBase(
'U', shape=(4, 4))
131u0 = sum(U[idx[k]] * φ[k]
for k
in range(12))
135 print(
" return ",
cpp(u0).replace(
'U',
'active.U'),
';', sep=
'')
140J = sym.IndexedBase(
'J', shape=(2, 2))
145 for j
in range(len(z)):
147 res *= (x - z[j]) / (z[i] - z[j])
151Jxy = sym.simplify(sum(J[i,j] *
La(i, x, zx) *
La(j, y, zy)
for i
in range(len(zx))
for j
in range(len(zy))))
155 sym.simplify(sym.integrate(sym.integrate(D * (dφx[i] * dφx[j] + dφy[i] * dφy[j]), (x, 0, X)), (y, 0, Y)))
157if mrank == 0: print(
'KD = sym.', KD, end=
'\n\n')
160 sym.simplify(sym.integrate(sym.integrate(A * φ[i] * φ[j], (x, 0, X)), (y, 0, Y)))
162if mrank == 0: print(
'KA = sym.', KA, end=
'\n\n')
165 sym.simplify(sym.integrate(sym.integrate(2 * B * u0 * φ[i] * φ[j], (x, 0, X)), (y, 0, Y)))
167if mrank == 0: print(
'KB = sym.', KB, end=
'\n\n')
169u02 = sym.expand(u0**2)
170u03 = sym.expand(u0**3)
173 sym.simplify(sym.integrate(sym.simplify(sym.integrate(3 * C * u02 * φ[i] * φ[j], (x, 0, X))), (y, 0, Y)))
175if mrank == 0: print(
'KC = sym.', KC, end=
'\n\n')
181KK = mpi.bcast(KK, root=0)
184 sym.simplify(sym.integrate(sym.simplify(sym.integrate(B * u02 * φ[i], (x, 0, X))), (y, 0, Y)))
186if mrank == 0: print(
'FB = sym.', FB, end=
'\n\n')
189 sym.simplify(sym.integrate(sym.simplify(sym.integrate(2 * C * u03 * φ[i], (x, 0, X))), (y, 0, Y)))
191if mrank == 0: print(
'FC = sym.', FC, end=
'\n\n')
194 sym.simplify(sym.integrate(sym.integrate(Jxy * φ[i], (x, 0, X)), (y, 0, Y)))
196if mrank == 0: print(
'F0 = sym.', F0, end=
'\n\n')
202FF = mpi.bcast(FF, root=0)
209P = sym.IndexedBase(
'P', shape=(2,2,2))
210G = sym.IndexedBase(
'G', shape=(2,))
211dG = sym.IndexedBase(
'dG', shape=(2,))
212ug = sym.symbols(
'Ug')
214Pxy = [sym.simplify(sum(P[i, j, c] *
La(i, x, zx) *
La(j, y, zy)
for i
in range(len(zx))
for j
in range(len(zy))))
for c
in range(2)]
217 sym.simplify(sym.integrate(sym.integrate(sum(Pxy[i] * dG[i]
for i
in range(2)) * φ[i] * φ[j], (x, 0, X)), (y, 0, Y)))
219if mrank == 0: print(
'KL = sym.', KL, end=
'\n\n')
222 sym.simplify(sym.integrate(sym.integrate(sum(Pxy[i] * (dG[i] * ug - G[i])
for i
in range(2)) * φ[i], (x, 0, X)), (y, 0, Y)))
224if mrank == 0: print(
'FL = sym.', FL, end=
'\n\n')
226KL = mpi.bcast(KL, root=0)
227FL = mpi.bcast(FL, root=0)
229print_cpp((KL,), (FL,),
'diffusion3d-eval-shb.ipp')