A confused problem

Hello, everyone
I meet a confuxed problem when i work in fenics 2019.1.
A mini example code as follows:

from fenics import *
import numpy as np
import mshr
dx = 0.1
# mesh_f = RectangleMesh(Point(0.0, 0.0), Point(1.0, 1.0), 10, 10)
domain_f = mshr.Rectangle(Point(0.0, 0.0), Point(1.0, 1.0))
mesh_f = mshr.generate_mesh(domain_f, dx)

n_f = FacetNormal(mesh_f)
tor_f = as_vector((-n_f[1], n_f[0]))
V_f = VectorFunctionSpace(mesh_f, "CG", 2)
f = Constant((0.0, 0.0))

u , v = TrialFunction(V_f), TestFunction(V_f)

a1 = dot(u, v)*dx + dot(u, n_f)*dot(v, n_f)*ds \
    + dot(u, tor_f)*dot(v, tor_f)*ds
L1 = dot(f, v)*dx


print("----")

There is an error message:

a1 = dot(u, v)*dx + dot(u, n_f)*dot(v, n_f)*ds \
         ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
TypeError: unsupported operand type(s) for +: 'Product' and 'Form'

Can you try adding some parentheses?

a1 = dot(u, v)*dx + (dot(u, n_f)*dot(v, n_f))*ds

Hi.

Please note that from fenics import * is a wildcard import that also import UFL and hence the measure dx. You cannot define dx=0.1 without overlapping. Try not to define variables that might overwrite the FEniCS defined function and classes.

A simple modification hh=0.1 instead of dx=0.1 solves the problem.

from fenics import *
import numpy as np
import mshr
hh = 0.1
# mesh_f = RectangleMesh(Point(0.0, 0.0), Point(1.0, 1.0), 10, 10)
domain_f = mshr.Rectangle(Point(0.0, 0.0), Point(1.0, 1.0))
mesh_f = mshr.generate_mesh(domain_f, hh)

n_f = FacetNormal(mesh_f)
tor_f = as_vector((-n_f[1], n_f[0]))
V_f = VectorFunctionSpace(mesh_f, "CG", 2)
f = Constant((0.0, 0.0))

u, v = TrialFunction(V_f), TestFunction(V_f)

a1 = dot(u, v)*dx + dot(u, n_f)*dot(v, n_f)*ds \
    + dot(u, tor_f)*dot(v, tor_f)*ds
L1 = dot(f, v)*dx


print("----")

Cheers.

2 Likes

Oh, I made such a low-level mistake. Thank you for your reply. It works now!