_count attribute duplication by Function objects

Issue #323 wontfix
James R. Maddison created an issue

Function objects pass a count keyword to the Coefficient constructor. This can conflict with the counts of existing Coefficients, which can then lead to Coefficient.__eq__ eroneously returning True. This leads to obscure bugs with Python collections.

Example:

from dolfin import *
from ufl import Coefficient

space = FunctionSpace(UnitSquareMesh(1, 1), "CG", 1)
e = space.ufl_element()

c = [Coefficient(e) for i in xrange(10)]
F = [Function(space) for i in xrange(10)]
for i in xrange(len(c)):
  for j in xrange(len(F)):
    if c[i] == F[j]:
      print("Coefficient %i count = %i, Function %i count = %i" % (i, c[i]._count, j, F[j]._count))

Comments (7)

  1. James R. Maddison reporter

    To check correct usage: When using dolfin, classes which inherit from Coefficient should pass their id to the optional count keyword argument

  2. Martin Sandve Alnæs

    Yes that is the correct behaviour. Each Coefficient needs a unique count which must be preserved when a Function moves from python to C++ and back, and using the id() is the perfect solution. Since it's inherited from Variable it's globally unique in a dolfin program across classes and monotonously increasing, which gives deterministic behaviour (a random number would not allow stable signature computation the same way).

  3. Log in to comment