# Plotting a rod with two different materials (different modulues young) to paraview

**URL:** <https://fenicsproject.discourse.group/t/plotting-a-rod-with-two-different-materials-different-modulues-young-to-paraview/13087>\
**Category:** Legacy DOLFIN\
**Tags:** visualization\
**Created:** [December 18, 2023, 8:51pm UTC](https://fenicsproject.discourse.group/t/plotting-a-rod-with-two-different-materials-different-modulues-young-to-paraview/13087 "2023-12-18T20:51:43Z")\
**Posts on this page:** 12\
**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:** [December 18, 2023, 8:51pm UTC](https://fenicsproject.discourse.group/t/plotting-a-rod-with-two-different-materials-different-modulues-young-to-paraview/13087/1 "2023-12-18T20:51:43Z")

</div>

hi all,

im trying to create a laminated rode that is being defined by two subdomains, each domain for each material. those materials are defer by their moduoel young. this rode is being stretched on the left side of the rode. i want to represent this rode in paraview and see in different color each material. how can i achieve that part? what i specifically need in the part of the two color plotting , the rest i got

the relevant parts of the subdomains  
class Omega\_0(SubDomain):  
def inside(self, x, on\_boundary):  
return x[1] \<= 0.5 + DOLFIN\_EPS

class Omega\_1(SubDomain):  
def inside(self, x, on\_boundary):  
return x[1] \> 0.5 - DOLFIN\_EPS

subdomain\_0 = Omega\_0()  
subdomain\_1 = Omega\_1()  
materials = MeshFunction(‘size\_t’, mesh, mesh.topology().dim())  
materials.set\_all(0)  
subdomain\_1.mark(materials, 1)

class Boundaryleft(SubDomain):  
def inside(self, x, on\_boundary):  
return on\_boundary and near(x[0], 0, DOLFIN\_EPS)

class Boundaryright(SubDomain):  
def inside(self, x, on\_boundary):  
return on\_boundary and near(x[0], 1, DOLFIN\_EPS)

# ADDED

facets = MeshFunction(“size\_t”, mesh, 2)  
facets.set\_all(0)  
Boundaryleft().mark(facets, 1)  
Boundaryright().mark(facets, 2)

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

```

kappa = K(materials, 1.0e7, 2.0e4, degree=0)

File(“Materials.pvd”)\<\< materials  
vtkfile\_subdomains = File(‘subdomains.pvd’)  
vtkfile\_subdomains \<\< materials

---

<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:** [December 18, 2023, 9:26pm UTC](https://fenicsproject.discourse.group/t/plotting-a-rod-with-two-different-materials-different-modulues-young-to-paraview/13087/2 "2023-12-18T21:26:11Z")

</div>

Please format your code with 3x` syntax, ie

````python
```python
# add code here

```

````

Please also explain what is not working with your current code, as it is unclear what is not working for you.

---

<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:** [December 19, 2023, 7:41pm UTC](https://fenicsproject.discourse.group/t/plotting-a-rod-with-two-different-materials-different-modulues-young-to-paraview/13087/3 "2023-12-19T19:41:23Z")

</div>

```auto
# Subdomains
class Omega_0(SubDomain):
    def inside(self, x, on_boundary):
        return x[1] <= 0.5 + DOLFIN_EPS

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

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

# Function space
V = VectorFunctionSpace(mesh, 'Lagrange', 2)

# Boundary conditions
class Boundaryleft(SubDomain):
    def inside(self, x, on_boundary):
        return on_boundary and near(x[0], 0, DOLFIN_EPS)

class Boundaryright(SubDomain):
    def inside(self, x, on_boundary):
        return on_boundary and near(x[0], 1, DOLFIN_EPS)

# ADDED
facets = MeshFunction("size_t", mesh, 2)
facets.set_all(0)
Boundaryleft().mark(facets, 1)
Boundaryright().mark(facets, 2)

'''
boundaryl = Boundaryleft()
boundaryr = Boundaryright()
bc1 = DirichletBC(V, Constant((0, 0, 0)), boundaryl)
bc2 = DirichletBC(V, Constant((1, 0, 0)), boundaryr)
'''
bc1 = DirichletBC(V.sub(0), Constant(0), facets, 1)
bc2 = DirichletBC(V.sub(0), Constant(1), facets, 2)

bc = [bc1, bc2]

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

kappa = K(materials, 1.0e7, 2.0e4, degree=0)

File("Materials.pvd")<< materials
vtkfile_subdomains = File('subdomains.pvd')
vtkfile_subdomains << materials

```

---

<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:** [December 19, 2023, 7:43pm UTC](https://fenicsproject.discourse.group/t/plotting-a-rod-with-two-different-materials-different-modulues-young-to-paraview/13087/4 "2023-12-19T19:43:02Z")

</div>

i want to see my rode in para view, each subdomain in different color, but im not able to doit, once i upload the subdomin file to paraview, im not able to find the material file in the coloring section  
seems like there is no scalar defined or somrthing like that, so iwanted to know what im missing

 ![image](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/a/a1e0edee6025b9d0e435fa87d5dbad4c70acd0d9.png)

---

<div class="post-metadata">

**Author:** ![violetus](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/violetus/32/5122_2.png) [@violetus](https://fenicsproject.discourse.group/u/violetus)\
**Post date:** [December 19, 2023, 9:56pm UTC](https://fenicsproject.discourse.group/t/plotting-a-rod-with-two-different-materials-different-modulues-young-to-paraview/13087/5 "2023-12-19T21:56:38Z")

</div>

It is looking fine to me. What are you expecting ?

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

---

<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:** [December 23, 2023, 8:42pm UTC](https://fenicsproject.discourse.group/t/plotting-a-rod-with-two-different-materials-different-modulues-young-to-paraview/13087/6 "2023-12-23T20:42:45Z")

</div>

hi, how did you do it? what did you set in paraview, im not able to see it like that?

 ![Capture2](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/9/90de945efaef4676007fb1eb9cd9671579159308.png)  
this is what i see

---

<div class="post-metadata">

**Author:** ![violetus](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/violetus/32/5122_2.png) [@violetus](https://fenicsproject.discourse.group/u/violetus)\
**Post date:** [December 23, 2023, 9:45pm UTC](https://fenicsproject.discourse.group/t/plotting-a-rod-with-two-different-materials-different-modulues-young-to-paraview/13087/7 "2023-12-23T21:45:06Z")

</div>

![image](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/8/8b3cfb4c4cd524a37e006a908d9eee78d5f04c7f.png)  
As you can see that the max limit is no different than 0, therefore in order to change the max limit, go to Rescale to Custom Data Range and set the max limit to 1 and click rescale in order to see changes.  
 ![image](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/a/a7d05ae1f1ff6a9e3890e503cb8fb6e26cce76f7.png)

---

<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:** [December 25, 2023, 8:42pm UTC](https://fenicsproject.discourse.group/t/plotting-a-rod-with-two-different-materials-different-modulues-young-to-paraview/13087/8 "2023-12-25T20:42:03Z")

</div>

unfortunately still not working, any idea why? maybe its how you defined the mesh?

 ![123](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/0/01252d15a46fb363dd188aae67b8bf455172f4d8.png)

---

<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:** [December 25, 2023, 8:42pm UTC](https://fenicsproject.discourse.group/t/plotting-a-rod-with-two-different-materials-different-modulues-young-to-paraview/13087/9 "2023-12-25T20:42:14Z")

</div>

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

length, width, height = 1.0, 0.2, 0.2 # Dimensions of the rod
num_elements = 10 # Number of elements along each dimension
E1, E2 = 1E6, 2E6 # 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)

# Subdomains
class Omega_0(SubDomain):
    def inside(self, x, on_boundary):
        return x[1] <= 0.5 + DOLFIN_EPS

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

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

# Function space
V = VectorFunctionSpace(mesh, 'Lagrange', 2)

# Boundary conditions
class Boundaryleft(SubDomain):
    def inside(self, x, on_boundary):
        return on_boundary and near(x[0], 0, DOLFIN_EPS)

class Boundaryright(SubDomain):
    def inside(self, x, on_boundary):
        return on_boundary and near(x[0], 1, DOLFIN_EPS)

# ADDED
facets = MeshFunction("size_t", mesh, 2)
facets.set_all(0)
Boundaryleft().mark(facets, 1)
Boundaryright().mark(facets, 2)

'''
boundaryl = Boundaryleft()
boundaryr = Boundaryright()
bc1 = DirichletBC(V, Constant((0, 0, 0)), boundaryl)
bc2 = DirichletBC(V, Constant((1, 0, 0)), boundaryr)
'''
bc1 = DirichletBC(V.sub(0), Constant(0), facets, 1)
bc2 = DirichletBC(V.sub(0), Constant(1), facets, 2)

bc = [bc1, bc2]

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

kappa = K(materials, 1.0e7, 2.0e4, degree=0)

File("Materials.pvd")<< materials
vtkfile_subdomains = File('subdomains.pvd')
vtkfile_subdomains << materials

```

---

<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:** [December 25, 2023, 9:04pm UTC](https://fenicsproject.discourse.group/t/plotting-a-rod-with-two-different-materials-different-modulues-young-to-paraview/13087/10 "2023-12-25T21:04:01Z")

</div>

ok i noticed that i had a mistake in the subdomain, now it working  
thank you so much!

---

<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:** [December 29, 2023, 11:08am UTC](https://fenicsproject.discourse.group/t/plotting-a-rod-with-two-different-materials-different-modulues-young-to-paraview/13087/11 "2023-12-29T11:08:25Z")

</div>

if i want the colors of the subdomain to also be reflected in the xdmf file, how can i do it? i want to see after applying the load from the boundary how each side deformed

---

<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:** [December 29, 2023, 5:31pm UTC](https://fenicsproject.discourse.group/t/plotting-a-rod-with-two-different-materials-different-modulues-young-to-paraview/13087/12 "2023-12-29T17:31:40Z")

</div>

You would have to store the `MeshFunction` and the `Function` to the same file, then you can apply Filters such as `Extract Block`, `Warp by Vector` etc.
