Topology optimization for heat conduction using dolfin adjoint

Dear guys,

Since I want to replicate the problem in Bendsoe, M.P. and Sigmund, O. (2004) as follows (the 91 lines code for heat conduction) by FEniCS and dolfin-adjoint, I have spent so much time but it is really difficult for me by following the official example,

I have three tasks recent,

  1. replicate the general problem above
  2. add a mesh-dependent Helmholtz filter
  3. implement MMA

Could anyone help me, please? Thank you very much.


Dear @wqabc2008,

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.

Dear dokken,

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), 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.

Thank you so much for your help.


So how have you tried to create the zero Dirichlet condition in this part of the domain? May I ask you to post your code here encapsulated with ```

Hi dokken,

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

Is that right?

Thank you so much.

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

and open the pvd-file in paraview.

1 Like

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.

With regards


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.

Hope this helps,



Thank you so much for your helpful information. This dose help me a lot.

Good luck with your work.

With regards


did you manage to solve? could you make the code available?

Dear, @wqabc2008

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

Thanks in advance

This can rather easy be modified.


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/
/home/mhh/.local/lib/python3.10/site-packages/matplotlib/projections/ 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/”, line 97, in
V0 = FunctionSpace(mesh, “DG”, 0) # Discontinous function space
File “/usr/lib/petsc/lib/python3/dist-packages/dolfin/function/”, line 33, in init
self._init_convenience(*args, **kwargs)
File “/usr/lib/petsc/lib/python3/dist-packages/dolfin/function/”, 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/”, 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/”, line 50, in mpi_jit
return local_jit(*args, **kwargs)
File “/usr/lib/petsc/lib/python3/dist-packages/dolfin/jit/”, line 100, in ffc_jit
return ffc.jit(ufl_form, parameters=p)
File “/home/mhh/.local/lib/python3.10/site-packages/ffc/”, line 214, in jit
kind, module_name = compute_jit_prefix(ufl_object, parameters)
File “/home/mhh/.local/lib/python3.10/site-packages/ffc/”, line 156, in compute_jit_prefix
error(“Unknown ufl object type %s” % (,))
File “”, line 1, in
File “/home/mhh/.local/lib/python3.10/site-packages/ufl/”, line 172, in error
raise self._exception_type(self._format_raw(*message))
Exception: Unknown ufl object type FiniteElement

Process finished with exit code 1