ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 29 Nov 2018 10:51:09 +0100How to use the divergence of a finite function as the source term of another problemhttps://ask.sagemath.org/question/44492/how-to-use-the-divergence-of-a-finite-function-as-the-source-term-of-another-problem/ I use RT element to solve problem A and denote the finite element solution by sigma, sigma is a matrix-valued finite element function. I need to use the divergence of sigma to be the source term of another problem, the trial function space is vector-valued functions. But it doesn't work. The code is
from __future__ import print_function
from fenics import *
import matplotlib.pyplot as plt
mesh = UnitSquareMesh(8,8)
RT = VectorElement("RT",mesh.ufl_cell(), 1,2)
Q = FunctionSpace(mesh, RT)
V = VectorFunctionSpace(mesh, 'CG',1)
f = Expression((('x[0]*x[1]','x[0]'),('x[0]','x[0]*x[1]')),degree=2)
sigma = TrialFunction(Q)
tau = TestFunction(Q)
a = inner(sigma, tau)*dx
L = inner(f,tau)*dx
sigma = Function(Q)
solve(a == L, sigma)
def boundary(x, on_boundary):
return on_boundary
bc = DirichletBC(V, Constant((0.0, 0.0)), boundary)
u = TrialFunction(V)
v = TestFunction(V)
au = inner(grad(u), grad(v))*dx
Lu = inner(sigma,grad(v))*dx
u = Function(V)
solve(au == Lu, u, bc)Wed, 28 Nov 2018 19:41:43 +0100https://ask.sagemath.org/question/44492/how-to-use-the-divergence-of-a-finite-function-as-the-source-term-of-another-problem/Comment by Iguananaut for <p>I use RT element to solve problem A and denote the finite element solution by sigma, sigma is a matrix-valued finite element function. I need to use the divergence of sigma to be the source term of another problem, the trial function space is vector-valued functions. But it doesn't work. The code is </p>
<pre><code>from __future__ import print_function
from fenics import *
import matplotlib.pyplot as plt
mesh = UnitSquareMesh(8,8)
RT = VectorElement("RT",mesh.ufl_cell(), 1,2)
Q = FunctionSpace(mesh, RT)
V = VectorFunctionSpace(mesh, 'CG',1)
f = Expression((('x[0]*x[1]','x[0]'),('x[0]','x[0]*x[1]')),degree=2)
sigma = TrialFunction(Q)
tau = TestFunction(Q)
a = inner(sigma, tau)*dx
L = inner(f,tau)*dx
sigma = Function(Q)
solve(a == L, sigma)
def boundary(x, on_boundary):
return on_boundary
bc = DirichletBC(V, Constant((0.0, 0.0)), boundary)
u = TrialFunction(V)
v = TestFunction(V)
au = inner(grad(u), grad(v))*dx
Lu = inner(sigma,grad(v))*dx
u = Function(V)
solve(au == Lu, u, bc)
</code></pre>
https://ask.sagemath.org/question/44492/how-to-use-the-divergence-of-a-finite-function-as-the-source-term-of-another-problem/?comment=44497#post-id-44497Hello--regretfully I don't have the knowledge to answer your question, though I'm sure someone will be along who can. Just as a side-note, when posting code please make sure it's properly marked as code. You can do this by selecting the code block and clicking the "format as code" button in the post editor, or by pressing Ctrl-K. I've gone ahead and taken care of it for you this time.Thu, 29 Nov 2018 10:51:09 +0100https://ask.sagemath.org/question/44492/how-to-use-the-divergence-of-a-finite-function-as-the-source-term-of-another-problem/?comment=44497#post-id-44497