I am trying to solve for elasticity but it is giving me this error:

def elasticity_solve_with_expression(u_sol, p_sol, E, nu, alpha, bcs_u):
    """
    Solve the elasticity PDE:
      -div(sigma(u))=0,
      sigma=2*mu*eps(u)+lambda*tr(eps(u))*I - alpha*p_expr*I
    where p_expr references p_sol via a Python callback.
    """
    V_u = u_sol.function_space
    mesh_ = V_u.mesh
    dx = ufl.Measure("dx", domain=mesh_)

    # Callback referencing p_sol
    def p_known_callback(x):
        # x: shape=(gdim, N)
        # Return shape=(1, N) for a scalar
        values = np.zeros((1, x.shape[1]), dtype=np.float64)
        for j in range(x.shape[1]):
            pt = x[:, j]
            # In older Dolfinx, p_sol.eval(...) may not exist => custom interpolation needed
            val = p_sol.eval(pt)  
            values[0, j] = val[0]
        return values

    # Provide the raw C++ element object
    # For a scalar pressure, we do p_sol.function_space.element._cpp_object
    V_p = p_sol.function_space
    cpp_element = V_p.element._cpp_object


    # Construct Expression with that low-level object
    p_expr = fem.Expression(p_known_callback, cpp_element)

    # Material
    mu_value  = E/(2*(1+nu))
    lam_value = E*nu/((1+nu)*(1-2*nu))

    u = ufl.TrialFunction(V_u)
    v = ufl.TestFunction(V_u)

    def eps(u_):
        return ufl.sym(ufl.grad(u_))

    sigma_expr = (2*mu_value*eps(u)
                  + lam_value*ufl.tr(eps(u))*ufl.Identity(2)
                  - alpha * p_expr * ufl.Identity(2))

    a = ufl.inner(sigma_expr, ufl.sym(ufl.grad(v))) * dx
    L = ufl.as_ufl(0.0)*dx

    problem = LinearProblem(a, L, bcs=bcs_u)
    u_new = problem.solve()
    u_sol.x.array[:] = u_new.x.array[:]

line 69, in elasticity_solve_with_expression
cpp_element = V_p.element._cpp_object
^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: ‘dolfinx.cpp.fem.FiniteElement_float64’ object has no attribute ‘_cpp_object’

Please properly format the code with

```python
# add code here
```

and make sure that the code you add can reproduce the error

Thank you for the suggestions. I have edited the post.

This is not the correct way of constructing an Expression. See
https://docs.fenicsproject.org/dolfinx/v0.9.0/python/generated/dolfinx.fem.html#dolfinx.fem.Expression
and for instance
https://docs.fenicsproject.org/dolfinx/v0.9.0/python/demos/demo_elasticity.html#post-processing
on usage.

However, I don’t understand the point of using

What are you trying to achieve her?

On what grid does p_sol live? If it lives on the same grid, why not use p_sol within the variational form?

1 Like

The goal is to iteratively solve a coupled hydro-mechanical problem involving the interaction of elasticity and fluid flow over a time-dependent simulation. Specifically, p_sol represents the pressure field that evolves over the same grid (mesh) as the displacement field u_sol.

Why not just leave p_sol as it is in the variational form?

I tried. I worked, thanks a lot for the suggestion.