firedrakeproject / firedrake

Firedrake is an automated system for the portable solution of partial differential equations using the finite element method (FEM)
https://firedrakeproject.org
Other
482 stars 157 forks source link

"Too many indices for sum factorisation!" for calculating radius on extruded CubedSphereMesh. #3652

Open sghelichkhani opened 2 days ago

sghelichkhani commented 2 days ago

The following simply fails with the error in the title:

from firedrake import *

mesh2d = CubedSphereMesh(1, refinement_level=1)
mesh = ExtrudedMesh(mesh2d, layers=2, extrusion_type='radial')

X = SpatialCoordinate(mesh)
W = FunctionSpace(mesh, "CG", 1)
w = Function(W).interpolate(dot(X, X))

which is related to sum_factorise in tsfc not accepting sum_indices of more than 6 in https://github.com/firedrakeproject/tsfc/blob/72e5521f9c208094d2d5daf33e67cb2b7ad5f5a1/gem/optimise.py#L357. I was wondering if anyone has any insight on why that is, and how one can fix this?

Note: Simply by changing the last line to the equivalent:

w = Function(W).interpolate(sum([x**2 for x in X])))

works fine.

connorjward commented 2 days ago

I think that this is because the sum factorisation algorithm is NP-complete so TSFC has an arbitrary limit on the number of indices to avoid ballooning compilation time. Instead of imposing a hard limit we could perhaps allow this to be configurable in form_compiler_parameters or just spew a warning instead of failing.