Regarding the issue in updating the expression

Hello everyone,

I am using an UFL expression to define a variable that depends on two parameters. After some steps of the solution, I want to update one of the parameters with a location dependent field. I tried it with the following approach but it is throwing an error,

The minimal working code is as follows,

from fenics import *            
from mshr import *              

E = 210000.0
nu = 0.3
mu = Expression('E_1/(2.0*(1.0 + nu_1))', E_1 = E, nu_1 = nu,degree = 0)


#Define and plot mesh
domain = Rectangle(Point(0.0, 0.0), Point(100.0, 100.0))
mesh = generate_mesh(domain, 32)

# Define function space 
V = VectorFunctionSpace(mesh, 'CG', 1)
V_alpha = FunctionSpace(mesh, 'CG', 1)

class Stiffness_function(UserExpression):
    def __init__(self, E_sol, **kwargs):
        super().__init__(**kwargs)
        self.E_sol = E_sol
        
    def eval(self, value, x):
        x_c = x[0]
        y_c = x[1]
        value[0]= (self.E_c_sol(x_c,y_c))*sqrt(x_c**2 + y_c**2)
   
    def value_shape(self):
        return (1,)
        
for n in range(3):
    if n == 2:
        mu.E_1 = 150000.
    else:
        New_E = Stiffness_function(E_sol = E, element = V_alpha.ufl_element())  
        mu.E_1 = New_E

and the error is

Please guide me, how I can update this expression.

Thank you,
Manish

The error is quite straightforward. Your class has a variable E_sol while you are assigning to E_1

@dokken Thank you for helping me. I replace the variable E_sol in class with E_1 and updated code is as follows,

class Stiffness_function(UserExpression):
    def __init__(self, E_1, **kwargs):
        super().__init__(**kwargs)
        self.E_1 = E_1
        
    def eval(self, value, x):
        x_c = x[0]
        y_c = x[1]
        value[0]= (self.E_1)*sqrt(x_c**2 + y_c**2)

    def value_shape(self):
        return (1,)
        
for n in range(3):
    if n == 1:
        New_E = Stiffness_function(E_1 = E, element = V_alpha.ufl_element())  
        mu.E_1 = New_E

But it is still giving me the same error.

Thank you
Manish

You should never mix Expressions with UserExpressions. I would suggest generating a single UserExpression for the combined expression

Thank you. I will try it with a single UserExpression.

Manish