piecewise constant parameter in 2018.1.0 version


72
views
0
5 weeks ago by
Hi,

the following MWE works fine in Fenics 2017.2.0 but not in 2018.1.0 version... Any ideas why?
I'm using this to define a piecewise physical parameter in heat equation.

I'm using Docker in OSX 10.13.5.

from dolfin import *

# Reading a h5 mesh generated by gmsh and dolfin-convert
mesh = Mesh()
hdf = HDF5File(mesh.mpi_comm(), "mesh/earth3d.h5", "r")
hdf.read(mesh, "/mesh", False)

#definition of subdomains
subdomains = MeshFunction("size_t", mesh,mesh.topology().dim())
hdf.read(subdomains, "/domains")

# Class to define a piecewise constant by subdomain
class Conductivity(UserExpression):
    def __init__(self, subdomains, c_0, c_1, **kwargs):
        self.subdomains = subdomains
        self.c_0 = c_0
        self.c_1 = c_1

    def eval_cell(self, values, x, ufl_cell):
        if self.subdomains[ufl_cell.index] == 1:
            values[0] = self.c_0
        else:
            values[0] = self.c_1

# Thermal conductivity:
kappa =  Conductivity(subdomains, 1.0, 0.1, degree=0)  

# FEM space:
Hh = FunctionSpace(mesh,"Lagrange", 1)
uh = TrialFunction(Hh)
vh = TestFunction(Hh)

# Bilinear form with problems!!!
a = inner(kappa*grad(uh), grad(vh))*dx  ​


In Fenics 2017.2.0 it's ok but in Fenics 2018.1.0 I get the following error:

File "toto.py", line 34, in <module>
a = inner(kappa*grad(uh), grad(vh))*dx
File "/usr/local/lib/python3.5/dist-packages/ufl/exproperators.py", line 193, in _mul
return _mult(self, o)
File "/usr/local/lib/python3.5/dist-packages/ufl/exproperators.py", line 124, in _mult
s1, s2 = a.ufl_shape, b.ufl_shape
File "/usr/local/lib/python3.5/dist-packages/ufl/coefficient.py", line 73, in ufl_shape
return self._ufl_shape
AttributeError: 'Conductivity' object has no attribute '_ufl_shape'

Thanks in advance,

Rodolfo


Community: FEniCS Project

2 Answers


1
5 weeks ago by
Calling the superclass constructor in the __init__() method seems to help.  The following runs for me (with an innocuous-looking warning) in dev:latest.

from dolfin import *

# Reading a h5 mesh generated by gmsh and dolfin-convert
#mesh = Mesh()
#hdf = HDF5File(mesh.mpi_comm(), "mesh/earth3d.h5", "r")
#hdf.read(mesh, "/mesh", False)
mesh = UnitSquareMesh(10,10)

#definition of subdomains
#subdomains = MeshFunction("size_t", mesh,mesh.topology().dim())
#hdf.read(subdomains, "/domains")

# Class to define a piecewise constant by subdomain
class Conductivity(UserExpression):
    def __init__(self, subdomains, c_0, c_1, **kwargs):

        ############################################
        super(Conductivity, self).__init__(**kwargs)
        ############################################
        
        self.subdomains = subdomains
        self.c_0 = c_0
        self.c_1 = c_1

    def eval_cell(self, values, x, ufl_cell):
        #if self.subdomains[ufl_cell.index] == 1:
        #    values[0] = self.c_0
        #else:
        #    values[0] = self.c_1
        values[0] = 1.0

# Thermal conductivity:
subdomains = 'asdf'
kappa =  Conductivity(subdomains, 1.0, 0.1, degree=0)

# FEM space:
Hh = FunctionSpace(mesh,"Lagrange", 1)
uh = TrialFunction(Hh)
vh = TestFunction(Hh)

# Bilinear form with problems!!!
a = inner(kappa*grad(uh), grad(vh))*dx
​


(Interestingly, this does not work in 2017.2.)

0
5 weeks ago by
It works with Fenics 2018.1.0 but not with 2017.2.0....

Anyways, many thanks David!!!

Rodolfo
Please login to add an answer/comment or follow this question.

Similar posts:
Search »