# My material is defined only with one material and not 2

**URL:** <https://fenicsproject.discourse.group/t/my-material-is-defined-only-with-one-material-and-not-2/13264>\
**Category:** Legacy DOLFIN\
**Tags:** material-properties\
**Created:** [January 9, 2024, 7:43pm UTC](https://fenicsproject.discourse.group/t/my-material-is-defined-only-with-one-material-and-not-2/13264 "2024-01-09T19:43:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mirialex](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/mirialex/32/5890_2.png) [@Mirialex](https://fenicsproject.discourse.group/u/Mirialex)\
**Post date:** [January 9, 2024, 7:43pm UTC](https://fenicsproject.discourse.group/t/my-material-is-defined-only-with-one-material-and-not-2/13264/1 "2024-01-09T19:43:10Z")

</div>

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

```auto
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())
materials.set_all(0)
subdomain_0.mark(materials, 1)
subdomain_1.mark(materials, 2)
'''

```

this the userexperssion class

```auto
class K(fe.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 ()

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

```

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

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [January 9, 2024, 8:20pm UTC](https://fenicsproject.discourse.group/t/my-material-is-defined-only-with-one-material-and-not-2/13264/2 "2024-01-09T20:20:30Z")

</div>

> [@Mirialex](#):
>
> ` if self.materials[cell.index] == 0:`

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.

---

<div class="post-metadata">

**Author:** ![Mirialex](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/mirialex/32/5890_2.png) [@Mirialex](https://fenicsproject.discourse.group/u/Mirialex)\
**Post date:** [January 9, 2024, 8:52pm UTC](https://fenicsproject.discourse.group/t/my-material-is-defined-only-with-one-material-and-not-2/13264/3 "2024-01-09T20:52:53Z")

</div>

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

```auto
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)}")

```

![Cap](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/7/731a4ee8d69e698dffad2dd0540a3d32a4363c8b.png)

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [January 9, 2024, 9:27pm UTC](https://fenicsproject.discourse.group/t/my-material-is-defined-only-with-one-material-and-not-2/13264/4 "2024-01-09T21:27:49Z")

</div>

> [@Mirialex](#):
>
> hi, what do you mean import statement?

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

```auto
from dolfin import *
import fenics as fe

```

> [@Mirialex](#):
>
> `E_scalar = FunctionSpace(mesh, 'P', 1)`

You are projecting a discontinuous function into a continuous space, and is hence seeing Gibbs phenomenon, as for instance shown in  
[http://jsdokken.com/FEniCS23-tutorial/src/approximations.html](http://jsdokken.com/FEniCS23-tutorial/src/approximations.html)

---

<div class="post-metadata">

**Author:** ![Mirialex](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/mirialex/32/5890_2.png) [@Mirialex](https://fenicsproject.discourse.group/u/Mirialex)\
**Post date:** [January 11, 2024, 4:33pm UTC](https://fenicsproject.discourse.group/t/my-material-is-defined-only-with-one-material-and-not-2/13264/5 "2024-01-11T16:33:42Z")

</div>

hi thanks ,  
yes this is my import

```auto
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?

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [January 21, 2024, 2:34pm UTC](https://fenicsproject.discourse.group/t/my-material-is-defined-only-with-one-material-and-not-2/13264/6 "2024-01-21T14:34:28Z")

</div>

Use a Discontinuous Lagrange space to approximate `E_scalar` for visualization.

---

<div class="post-metadata">

**Author:** ![Mirialex](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/mirialex/32/5890_2.png) [@Mirialex](https://fenicsproject.discourse.group/u/Mirialex)\
**Post date:** [January 26, 2024, 1:58pm UTC](https://fenicsproject.discourse.group/t/my-material-is-defined-only-with-one-material-and-not-2/13264/7 "2024-01-26T13:58:58Z")

</div>

thank you so much! now its showing the correct data
