For people to be able to help you, you need to supply a minimal working example highlight were your issues are. Especially since you are linking to a well documented demo, it is not clear to me were your issues with modifiying this example is.
In particular, you need to supply the mathematical formulation of the Bendsoe problem and of each of the steps you would like to implement.

Hope you are well. Thank you so much for your quick reply.

I am sorry for that because this is my first time using this forum and I don’t know how to input formulations here. I have tried using

$$\int_{\Omega} fT + \alpha \int_{\Omega} \nabla a \cdot \nabla a$$

but it does not work well in preview.

I just want to replicate the topology optimization of the simple and general heat sink. The mathematical formulation of this problem as well as all the constant values are the same as the example here (particularly f=0.01 and V=0.4) http://www.dolfin-adjoint.org/en/latest/documentation/poisson-topology/poisson-topology.html, but the only difference is that the position of the Dirichlet boundary condition (T=0) in my case is shown as follows:

I have tried to modify the example’s code to replicate the problem above but failed, and I also want to know if anybody knows how to add a mesh-dependent Helmholtz filter and if we can implement by MMA.

I am sorry for asking such a simple question you may think.

The code I modified for the Zero Dirichlet boundary condition is

class Bot(SubDomain):
“The top and left boundary of the unitsquare, used to enforce the Dirichlet boundary condition.”
def inside(self, x, on_boundary):
return (1/2-2/15 < x[0] < 1/2+2/15 and x[1] == 0) and on_boundary

It seems to be correct, and you can verify this with the following code:

class Bot(SubDomain):
"The top and left boundary of the unitsquare, used to enforce the Dirichlet boundary condition."
def inside(self, x, on_boundary):
return (1/2-2/15 < x[0] < 1/2+2/15 and x[1] == 0) and on_boundary
mf = MeshFunction("size_t", mesh, mesh.topology().dim()-1, 0)
Bot().mark(mf, 2)
File("mf.pvd") << mf

Hi dokken, it works! Thank you so much for your help. Next, I need to add a mesh-independent Helmholtz filter and try MMA instead of IPOPT. Hopefully, it will work.

Regarding the Helmnotz filter, this paper from 2020 (de Souza, E.M., Silva, E.C.N. Topology optimization applied to the design of actuators driven by pressure loads. Struct Multidisc Optim (2020). Link2Publisher ) proposes an implementation of it with dolfin_adjoint.
Here is the corresponding code:

def helmnotz_filter(rho_n, r_min, annotate=False, name="Filtered"):
V = rho_n.function_space()
rho = TrialFunction(V)
w = TestFunction(V)
a = (r_min**2)*inner(grad(rho), grad(w))*dx + rho*w*dx
L = rho_n*w*dx
A, b = assemble_system(a, L)
rho = Function(V, name=name)
solve(A, rho.vector(), b, annotate=annotate)
return rho

The authors recommend to have the input field discretized by nodal first order elements as the filter weak equation presents gradient of it. Please cite their work if you publish your research using their code.

Regarding MMA I haven’t found bindings or examples with dolfin_adjoint but some already implemented it in Python (here and here). This may be worth it for you to investigate it if you absolutely want MMA but I personally use IPOPT.

It would be nice if you could share a resulting code with me, because I’m quite interested in this topic, but I have a couple of issues that you can help me to deal with

Dear @Rajak18
I run these commands in the code to install fenics. then, I have opened Pycharm, but when I run the code I face this error, Please help me:
/usr/bin/python3.10 /home/mhh/Downloads/11heatSinkPCM-main/heatSinkPCM-main/heatSinkPCM-main/TOPCMheatsink.py
/home/mhh/.local/lib/python3.10/site-packages/matplotlib/projections/init.py:63: UserWarning: Unable to import Axes3D. This may be due to multiple versions of Matplotlib being installed (e.g. as a system package and as a pip package). As a result, the 3D projection is not available.
warnings.warn("Unable to import Axes3D. This may be due to multiple versions of "
Unknown ufl object type FiniteElement
Traceback (most recent call last):
File “/home/mhh/Downloads/11heatSinkPCM-main/heatSinkPCM-main/heatSinkPCM-main/TOPCMheatsink.py”, line 97, in
V0 = FunctionSpace(mesh, “DG”, 0) # Discontinous function space
File “/usr/lib/petsc/lib/python3/dist-packages/dolfin/function/functionspace.py”, line 33, in init
self._init_convenience(*args, **kwargs)
File “/usr/lib/petsc/lib/python3/dist-packages/dolfin/function/functionspace.py”, line 100, in _init_convenience
self._init_from_ufl(mesh, element, constrained_domain=constrained_domain)
File “/usr/lib/petsc/lib/python3/dist-packages/dolfin/function/functionspace.py”, line 42, in _init_from_ufl
ufc_element, ufc_dofmap = ffc_jit(element, form_compiler_parameters=None,
File “/usr/lib/petsc/lib/python3/dist-packages/dolfin/jit/jit.py”, line 50, in mpi_jit
return local_jit(*args, **kwargs)
File “/usr/lib/petsc/lib/python3/dist-packages/dolfin/jit/jit.py”, line 100, in ffc_jit
return ffc.jit(ufl_form, parameters=p)
File “/home/mhh/.local/lib/python3.10/site-packages/ffc/jitcompiler.py”, line 214, in jit
kind, module_name = compute_jit_prefix(ufl_object, parameters)
File “/home/mhh/.local/lib/python3.10/site-packages/ffc/jitcompiler.py”, line 156, in compute_jit_prefix
error(“Unknown ufl object type %s” % (ufl_object.class.name,))
File “”, line 1, in
File “/home/mhh/.local/lib/python3.10/site-packages/ufl/log.py”, line 172, in error
raise self._exception_type(self._format_raw(*message))
Exception: Unknown ufl object type FiniteElement