Compute function maximum/minimum on subdomains

The following seems snappy enough

from dolfin import *
import numpy as np


def minimum(f, subdomains, subd_id):
    '''Minimum of f over subdomains cells marked with subd_id'''
    V = f.function_space()
    
    dm = V.dofmap()

    subd_dofs = np.unique(np.hstack(
        [dm.cell_dofs(c.index()) for c in SubsetIterator(subdomains, subd_id)]))
    
    return np.min(f.vector().get_local()[subd_dofs])

# --------------------------------------------------------------------

if __name__ == '__main__':
    
    domain = CompiledSubDomain('x[1] > 0.5 - DOLFIN_EPS')
    f = Expression('x[0]+x[1]', degree=1)
    
    # Check scaling
    for n in (8, 16, 32, 64, 128, 256):
        
        mesh = UnitSquareMesh(n, n)
        V = FunctionSpace(mesh, 'P', 1)
        u = interpolate(f, V)

        mf = MeshFunction('size_t', mesh, 2, 0)
        domain.mark(mf, 1)

        t = Timer('minimum')
        min_value = minimum(u, mf, 1)
        dt = t.stop()

        print dt, min_value

3 Likes