dokken
September 10, 2024, 5:39am
5
Swani:
Furthermore, while checking your reply, I have some additional questions.
a. In the code, you showed the Hessian, shouldn’t the Hessian( \nabla^{2}u_{elem}(x,y) ∇2uelem(x,y) \nabla^{2}u_{elem}(x,y) ) be viewed differently from the partial derivative of the gradient( {\partial\nabla{u_{elem}(x,y)}\over\partial{x} \ or \ \partial{y}} ∂∇uelem(x,y)∂x or ∂y{\partial\nabla{u_{elem}(x,y)}\over\partial{x} \ or \ \partial{y}} )?
(If my knowledge is wrong, please correct me.)
You can do that by callinggrad_u.dx(0)
or grad_u.dx(1)
.
Swani:
b. Since grad(u)
is a UFL form, vector array extraction seems to be difficult, so I tried vector array extraction with as_vector
, but with difficulty. Is there a way to extract either grad_u
or grad_u_ex
as a vector array? As far as I know, there doesn’t seem to be a way.
While this is an important example for noticing that the two results are not different, I think there is a difficulty in utilising the gradient(or Hessian) array.
(The reason I want to extract the values into a vector array is to compare them later with other operations).
Please be clear on how you want to do this comparison.
As the examples you have shown has been simple, analytic expressions, you could simply interpolate the analytical expression into the function space you want to work with. See for instance
Legacy dolfin doesn’t really have complex support.
You can use sympy for this though:
from dolfin import *
mesh = UnitSquareMesh(10, 10)
import sympy
x, y, t = sympy.symbols("x[0], x[1] t", real=True)
v_in = y * (1 - y) * sympy.exp(1j * t)
v_real, _ = v_in.as_real_imag()
expr = Expression(str(v_real), degree=2, t=0.0)
print(assemble(expr * dx(domain=mesh)))
expr.t = 2
print(assemble(expr * dx(domain=mesh)))
Using strings and eval:
import sympy
x = sympy.Symbol("x")
cos = sympy.cos
sympy_input = cos(x) + x ** 2
import fenics
mesh = fenics.UnitIntervalMesh(10)
cos = fenics.cos
x = fenics.SpatialCoordinate(mesh)[0]
ufl_output = eval(str(sympy_input))
print(fenics.assemble(ufl_output*fenics.dx))
However, is there a specfic reason for using sympy?
UFL-forms can be differentiated etc, so in most use cases there is no need for using sympy.
EDIT: You can also use: https://github.com/MiroK/ulfy
As pointed out in the previous part of the question, what I have described gives you a symbolic expression that you can evaluate when integrating. Thus I chose an L2 norm, as it Seems natural in this setting
1 Like