My material is defined only with one material and not 2

hi all,

im trying to create a rod with to different E and its seems to be that my rod is being set with only the second E that i set
this is my mesh and materials definition

length, width, height = 1.0, 0.2, 0.2  # Dimensions of the rod
num_elements = 12  # Number of elements along each dimension
E1, E2 = 4E8, 8E6  # Young's moduli for the two halves of the rod

# Create a 3D mesh for the rod
mesh = BoxMesh(Point(0, 0, 0), Point(length, width, height), num_elements, num_elements, num_elements)

# Define boundary
class Omega_0(SubDomain):
    def inside(self, x, on_boundary):
        return x[0] <= length/2 + DOLFIN_EPS

class Omega_1(SubDomain):
    def inside(self, x, on_boundary):
        return x[0] > length/2 - DOLFIN_EPS

subdomain_0 = Omega_0()
subdomain_1 = Omega_1()
materials = MeshFunction('size_t', mesh, mesh.topology().dim())
subdomain_0.mark(materials, 1)
subdomain_1.mark(materials, 2)

this the userexperssion class

class K(fe.UserExpression):
    def __init__(self, materials, k_0, k_1, **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
            values[0] = self.k_1

    def value_shape(self):
        return ()

E_global = K(materials, E1, E2, degree=0)

and it seems to be that my entire rod is set with E2 , any idea why?

Don’t you here mean if self.materials[cell.index] == 1: as you have marked one subdomain with 1, the other with 2?

Please note that for further posts, please also include your import statements.

hi, what do you mean import statement? i have changed it to 1 and now if i use the code i got from chagpt to analyze the E on each point and it seems to be different on and not the values i gave it

E_scalar = FunctionSpace(mesh, 'P', 1)

# Project LAMBDA onto this function space
E_projected = project(E_global, E_scalar)

# Evaluate LAMBDA at the specified points
for pt in points:
    print(f"E at {pt}: {E_projected(pt)}")


You did not include how you imported DOLFIN, even if I could guess that you did

from dolfin import *
import fenics as fe

You are projecting a discontinuous function into a continuous space, and is hence seeing Gibbs phenomenon, as for instance shown in

hi thanks ,
yes this is my import

from dolfin import *
import numpy as np
import fenics as fe
import matplotlib.pyplot as plt

but i dont quit understand how can i over come my issue? what can i change in order to avoid this Gibbs phenomenon?

Use a Discontinuous Lagrange space to approximate E_scalar for visualization.

thank you so much! now its showing the correct data