How to change function values of subdomain

Hi everyone. I want to ask about the best way to change function values in a subdomain. I am solving a PDE on a domain \Omega and after convergence, how can I change values on subdomain marked with subdomain id 1 to 5.0 and leave all other values of the domain the same.

You first need to specify what version of fenics you are using;

  • legacy fenics
  • dolfinx

I am using the legacy fenics

Consider the following MWE:

import dolfin

mesh = dolfin.UnitSquareMesh(10, 10)

class Left(dolfin.SubDomain):
    def inside(self, x, on_boundary):
        return x[0] <= 0.5 + 1e2*dolfin.DOLFIN_EPS

class Top(dolfin.SubDomain):
    def inside(self, x, on_boundary):
        return x[1] <= 0.5 + 1e2*dolfin.DOLFIN_EPS

cells = dolfin.MeshFunction("size_t", mesh, mesh.topology().dim(), 0)
Top().mark(cells, 2)
Left().mark(cells, 1)
dolfin.File("subdomain_org.pvd") << cells

locs = cells.where_equal(1)
cells.array()[locs]=5
dolfin.File("subdomain_new.pvd") << cells

resulting in the two domains