Fast cell-based material allocation

Hey all,

I am modeling a highly inhomogeneous 3D problem and therefore need a fast cell-based material allocation.

The material is stored in cell_material, where cell_material[j] corresponds to the material in cell j. Is it possible to create a coefficient function that is used in the bilinear form with the following mapping property:

coefficient function: index of the currently integrated cell ā†’ material parameter within this cell ?

There is an alternative that uses an auxiliary finite element space with constant basis function to assign the material to each cell, but Iā€™m not convinced of its speed:

material_fes = L2(mesh, order=0) # constant function in every cell
material_gf = GridFunction(material_fes)

for el in mesh.Elements():
    j = el.nr
    material_gf.vec.data[j] = cell_material[j]

material_cf = CoefficientFunction(material_gf).Compile()

The approach I asked for should be much faster than the example I gave before. Maybe someone has information about what possibilities ngsolve offers.

I am very grateful for help.

you can skip the python loop

material_gf.vec.FV().NumPy()[:] = cell_material

if cell_material is a numpy array. But what should be faster than this?

Thank you for your answer. You are right with your point, but I meant the speed of assembly of the system matrix.

If the cell material is known from the index of the cell it should be much faster compared to a coefficient function that is defined based on a grid function. Let me explain this in detail:

I assume that the evaluation of a grid function at a certain point in space requires two steps:

  1. Find the cell that contains the point of interest
  2. Evaluate the function value inside this cell at the point of interest

I would like to know if there is an option to avoid the first step, by using a special coefficient function that only uses the cell index of the currently integrated cell and maps it to the material defined inside this cell.

The gridfunction doesnt need to find the element. The fespace knows which dofs are associated with which elements.
Integrate/assemble loops over elements and this info is used.
Best Christopher

1 Like

Thank you very much for the information,

I have found out that it is not the system matrix that takes so long to set up, but my preconditioner.