How can I plot the boundary of a subdomain?

Hi there,

I am new to FEniCS (2019.2.0.dev0) and I have encountered a simple problem (I think).

I have a square channel with a sphere (it is 2D, so circle is the right word, but let’s keep it as a sphere). I want to see how the sphere will deform subject to know differences on the two fluid properties. So basically they both move with the same background velocity, but I want to be able to track that and visualise it. I have not thought of yet the boundary conditions I need to impose there, but since it will move with the flow, the boundary velocity will be the same as the flow.

I have defined two geometries for that, the background flow (square) and the cell (sphere). I set the cell as the subdomain.

My question is how can I plot with e.g. red the outline/boundary of the cell domain? I can read the mesh coordinates but not sure you to plot both the background and the cell mesh with different colours.

At this point, I have to admit that I am very confused with the markers and MeshFunction commands…

Here is a sample of the code:

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

r = 0.25            # cell radius 

# Define geometries
domain = Rectangle(Point(-1,0),Point(1,1))
cell = Circle(Point(-0.5,0.5),r)

# Set subdomains
domain.set_subdomain(1,cell)

# Create mesh
mesh=generate_mesh(domain,32)

# Mesh coordinates
coor = mesh.coordinates()
xcoor, ycoor = coor[:,0], coor[:,1]

#Plot mesh
plt.figure()
plot(mesh)
#plt.plot(xcoor,ycoor) #That doesn't work, since it connects essentially all the points
plt.show()

Thank you :slight_smile:

Hello EvaAnton!

I tried to set the boundary in a MeshFunction and output the boundary like this.

xdmf = XDMFFile("boundaries.xdmf")
xdmf.write(mesh_function)
xdmf.close()

Finally, I successfully view the boundary in paraview!

Hi! Thank you for your reply.

What do you mean by setting the boundary in a MeshFunction? Maybe I can use to plot it.
I’d prefer to stay in Python and do all my visualisation there.

…this is a possible way to visualize subdomains within a python script with vedo:

from dolfin import *

class Structure(SubDomain):
    def inside(self, x, on_boundary):
        return x[0] > 1.4 - DOLFIN_EPS and x[0] < 1.6 \
            + DOLFIN_EPS and x[1] < 0.6 + DOLFIN_EPS

mesh = RectangleMesh(Point(0.0, 0.0), Point(3.0, 1.0), 60, 20)

# Create sub domain markers and mark everaything as 0
sub_domains = MeshFunction("size_t", mesh, mesh.topology().dim())
sub_domains.set_all(0)

# Mark structure domain as 1
structure = Structure()
structure.mark(sub_domains, 1)

# Extract sub meshes
fluid_mesh = SubMesh(mesh, sub_domains, 0)
structure_mesh = SubMesh(mesh, sub_domains, 1)

# Move structure mesh
for x in structure_mesh.coordinates():
    x[0] += 0.1*x[0]*x[1]

# Move fluid mesh according to structure mesh
ALE.move(fluid_mesh, structure_mesh)
fluid_mesh.smooth()

#############################################
from vedo.dolfin import plot

plot(fluid_mesh, interactive=False)
plot(structure_mesh, c='tomato', add=True)
plot()

image

1 Like