This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

How to plot just the values in a SubDomain?

0 votes

Hello,

I'm working on a mixed problem in which the values of the variables are in different order of magnitude for different subdomains so it is not possible to visualize the lower values with regular plot.

I would like to know if there is a easy way to plot just the values of a single subdomain. For example, if I can use SubDomain objects marked by CellFunction to select which points should be ploted.

Thanks in advance,
Best regards

asked May 8, 2016 by EduardoMS FEniCS Novice (270 points)

2 Answers

+2 votes
 
Best answer

Also you can "extract" your solution only at the dofs that belongs to a given cell. Something like this:

from dolfin import *

mesh = UnitSquareMesh(100, 100, "crossed")
V = FunctionSpace(mesh, "CG", 2)
u = interpolate(Expression("x[0]*x[0] + x[1]*x[1]"), V)

class half(SubDomain):
  def inside(self, x, on_boundary):
    return x[0] > 0.5

cell_f = CellFunction("size_t", mesh)
cell_f.set_all(0)
half = half()

# mark subdomains
for cell in cells(mesh):
  if half.inside(cell.midpoint(), True):
    cell_f[cell.index()] = 1

def extract_values(u, cell_function, subdomain_id, V):
  dofmap = V.dofmap()
  mesh = V.mesh()
  for cell in cells(mesh):
    # Preserve only the dofs in cells marked as subdomain_id
    if cell_function[cell.index()] != subdomain_id:
      dofs = dofmap.cell_dofs(cell.index())
      for dof in dofs:
        u.vector()[dof] = 0.0
  return u


u_left  = Function(V); u_left.vector()[:]  = u.vector()
u_right = Function(V); u_right.vector()[:] = u.vector()
u_left  = extract_values(u_left, cell_f, 0, V)
u_right = extract_values(u_right, cell_f, 1, V)

plot(u_left, title="left")
plot(u_right, title="right")
interactive()
answered May 9, 2016 by hernan_mella FEniCS Expert (19,460 points)
edited May 10, 2016 by hernan_mella

Thank you! This really solved my problem.

For those interested in extracting the solution on a set of nodes defined as a facet mesh function, see MiroK's answer (thanks again to him) to this question: https://fenicsproject.org/qa/11876/extract-solution-at-a-set-of-nodes. Martin

0 votes

Unfortunately, this is not possible today. However, you can adjust the value range plotted like this:

plot(u, range_min=0., range_max=1.)
answered May 9, 2016 by Øyvind Evju FEniCS Expert (17,700 points)

Thank you for attention! But this doesn't remove the undesired points. It just set the value of them to range_max when they are greater than range_max.

...