How to define SubDomain in dolfin-x?

Hello,

I am trying to use SubDomain in dolfin-x. Below, a simple code is presented in dolfin which I can not represent it in dolfin-x.
The proposed simple code in dolfin is:

from dolfin import *
import matplotlib.pyplot as plt

mesh = RectangleMesh(Point(0, 0), Point(1, 1), 10, 10)

V = FunctionSpace(mesh, “Lagrange”, 1)
v = TestFunction(V)
u = TrialFunction(V)

s=dot(grad(u),grad(v))*dx

S = PETScMatrix()
assemble(s, tensor=S)

class MW(SubDomain):
def inside(self, x, on_boundary):
return on_boundary

ew=DirichletBC(V,(“0”),MW())
ew.apply(S)

esolver = SLEPcEigenSolver(S)
esolver.solve()

and I have a problem how to define this part in dolfin-x:

class MW(SubDomain):
def inside(self, x, on_boundary):
return on_boundary

ew=DirichletBC(V,(“0”),MW())
ew.apply(S)

and this is the proposed dolfin-x code:

import numpy as np
import matplotlib.pyplot as plt
from dolfinx.plotting import plot
from mpi4py import MPI
from dolfinx import Function, FunctionSpace, UnitSquareMesh, RectangleMesh
from dolfinx.fem import assemble_matrix, Form
from ufl import TrialFunction, TestFunction, grad, dx, dot
from slepc4py import SLEPc
from dolfinx.cpp.mesh import CellType
import dolfinx

mesh = RectangleMesh(
MPI.COMM_WORLD,
[np.array([0, 0, 0]), np.array([1, 1, 0])], [10, 10],
cell_type=CellType.triangle)

V = FunctionSpace(mesh, (“CG”, 1))

u = TrialFunction(V)
v = TestFunction(V)
a = dot(grad(u), grad(v))*dx

A = assemble_matrix(a, )
A.assemble()

eigensolver = SLEPc.EPS().create(MPI.COMM_WORLD)
eigensolver.setDimensions(6)
eigensolver.setWhichEigenpairs(2)
eigensolver.setOperators(A)

eigensolver.solve()
vr, vi = A.createVecs()

Thanks in advance for your help.

Consider the Poisson demo where the lambda function is replaced with

def boundary(x):
            return np.full(x.shape[1], True)

Similar to this test

1 Like

Thanks for your reply dokken.
It was really helpful. but there is still an ambiguity how to write this part of code in dolfin-x after defining the boundary condition?

ew.apply(S)

Thank you

Thanks a lot dokken Now, it runs properly.
and thanks for your suggestion. I am new to dolfin-x and I am trying to solve some of my previous dolfin problems by using dolfin-x. I would keep your suggestion in my mind.

Thank you again.