0
from fenics import *

mesh1 = RectangleMesh(Point(0, 0), Point(1, 1), 10, 10)

tol = 1E-14
class Omega_0(SubDomain):
    def inside(self, x, on_boundary):
        return x[1] <= 0.5 + tol

class Omega_1(SubDomain):
    def inside(self, x, on_boundary):
        return x[1] >= 0.5 - tol

materials = MeshFunction('size_t', mesh1, mesh1.topology().dim())    #定义网格函数
subdomain_0 = Omega_0()
subdomain_1 = Omega_1()
subdomain_0.mark(materials, 0)
subdomain_1.mark(materials, 1)

class K(UserExpression):
    def __init__(self, materials, k_0, k_1, **kwargs):
        super().__init__(**kwargs)
        self.materials = materials
        self.k_0 = k_0
        self.k_1 = k_1
        
    def eval_cell(self, values, x, cell):
        if self.materials[cell.index] == 0:
            values[0] = self.k_0
        else:
            values[0] = self.k_1
kappa = K(materials, 0.1, 1)

I am a beginner, and the above code is based on the code in the help documentation,K(UserExpression):refer to the https://fenicsproject.discourse.group/t/recursionerror-maximum-recursion-depth-exceeded-in-the-f t11-magnetostatics-py-example-of-the-solving-pdes-in-python-the-fenics-tutorial-part-1/1559, when I run the above code, I get the following error, I may not understand K(UserExpression): how to define:

WARNING: user expression has not supplied value_shape method or an element. Assuming scalar element. 

1 Answers1

0

The Warning you see is because you did not define the value_shape method in your K class. The whole class should look like this:

class K(UserExpression):
    def __init__(self, materials, k_0, k_1, **kwargs):
        super().__init__(**kwargs)
        self.materials = materials
        self.k_0 = k_0
        self.k_1 = k_1
    
    def eval_cell(self, values, x, cell):
        if self.materials[cell.index] == 0:
            values[0] = self.k_0
        else:
            values[0] = self.k_1
    def value_shape(self):
        return ()
pbit24
  • 418
  • 4
  • 12