I try to rotate exactly a vector defined on a mesh with RTE element (or Nedelec 1st kind) in 2D.
the analytic expression is v = (u[1], -u[0]). I do not want to use a projection as it would use a mass matrix. I just want to “assign” v = (u[1], -u[0]). The code below does not work
import matplotlib.pyplot as plt
from fenics import *
from dolfin import *
import numpy as np
from mshr import * #mesh
circle = Circle(dolfin.Point(0.0, 0.0), 1.0)
mesh = generate_mesh(circle,32) #element
P1 = FiniteElement(‘RTE’, cell=‘triangle’, degree=1) #space
FP1 = FunctionSpace(mesh,P1)
u= Function(FP1)
v= Function(FP1) #rotate a vector
assign(v,[u[1],-u[0]])
and generates the error below
AttributeError Traceback (most recent call last)
in
14 v= Function(FP1)
15 #
—> 16 assign(v,[u[1],-u[0]])
AttributeError: ‘Indexed’ object has no attribute ‘_cpp_object’
Thanks for replying. It seems to work to some extent indeed, e.g. one can plot v and u
but the internal structure of v is not the same as u
?v
Signature: v(arg, mapping=None, component=())
Type: ListTensor
String form: [f_14[1], -1 * f_14[0]]
Length: 2
File: /anaconda3/envs/fenicsproject/lib/python3.7/site-packages/ufl/tensors.py
Docstring: UFL operator type: Wraps a list of expressions into a tensor valued expression of one higher rank.
?u
Signature: u(*args, **kwargs)
Type: Function
String form: f_14
Length: 2
File: /anaconda3/envs/fenicsproject/lib/python3.7/site-packages/dolfin/function/function.py
Docstring:
Class docstring: UFL form argument type: Representation of a form coefficient.
Init docstring: Initialize Function.
for instance one cannot compute
norm(v)
TypeError Traceback (most recent call last)
in
----> 1 norm(v)
/anaconda3/envs/fenicsproject/lib/python3.7/site-packages/dolfin/fem/norms.py in norm(v, norm_type, mesh)
149 raise ValueError(“Unknown norm type {}”.format(str(norm_type)))
150 else:
→ 151 raise TypeError(“Do not know how to compute norm of {}”.format(str(v)))
152
153 # Assemble value and return
TypeError: Do not know how to compute norm of [f_14[1], -1 * f_14[0]]
Right. v is a ufl tensor whereas u is a dolfin function. In order to compute the norm, you could just use the definition of norm(?):
sqrt(abs(assemble(inner(v, v)*dx)))
Another way that should be equivalent (I cannot test this as of now) could be to use ufl.perp, namely
from ufl import perp
v = perp(u)
I have used FunctionAssigner like explained here to do subfunction assignment, never for Nedelec, or RT elements. My guess is that it shouldn’t work for these…
v= perp(u) gives also a ufl tensor instead of a dolfin function. So it is the same as
v = as_vector(-u[1], u[0]])
I also tried FunctionAssigner like explained here but for Nedelec or RT element which are vector element by construction I don’t see how to access the components of the FunctionSpace, I try something like
RuntimeError Traceback (most recent call last)
in
18 #v = as_vector([u[1], -u[0]])
19 #w=perp(u)
—> 20 FN10 = FN1.extract_sub_space([0])
/anaconda3/envs/fenicsproject/lib/python3.7/site-packages/dolfin/function/functionspace.py in extract_sub_space(self, component)
204
205 def extract_sub_space(self, component):
→ 206 V = self._cpp_object.extract_sub_space(component)
207 return FunctionSpace(V)
208
RuntimeError:
*** -------------------------------------------------------------------------
*** DOLFIN encountered an error. If you are not able to resolve this issue
*** using the information listed below, you can ask for help at
****** fenics-support@googlegroups.com
****** Remember to include the error message listed below and, if possible,
*** include a minimal running example to reproduce the error.
****** -------------------------------------------------------------------------
*** Error: Unable to extract subsystem of finite element.
*** Reason: There are no subsystems.
*** Where: This error was encountered inside FiniteElement.cpp.
*** Process: 0
*** *** DOLFIN version: 2019.1.0
*** Git changeset:
*** -------------------------------------------------------------------------
Yeah, that’s what I thought too. Are you encountering any problems using perp or as_vector ? Other than the fact that it is not a dolfin Function, can you create a MWE where you cannot work around it?