pmgbergen / porepy

Python Simulation Tool for Fractured and Deformable Porous Media

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Implement hash functions in Ad operators

keileg opened this issue · comments

Overview

We should implement hash functions for all Ad operators. The motivation is primarily to allow us to identify operators that are identical and thereby save time and memory during operator tree parsing. The hash will also allow us to store values of operators.

The implementation itself should be easy, see below. The potentially hard part will be to decide a way to define a suitable hash key for all types of operators.

Prototype implementation

A few ideas are sketched below, but the code may well contain logical flaws

class MergedOperator:

    def __key(self):
      tmp = [g.id for g in self.domain]
      tmp += [self.key]   # self.key and self.__key() is problematic
      tmp += [self.discr.keyword]
      return tuple(tmp)

    def __hash__(self):
        return hash(self.__key())

class Operator:

    def __key(self):
      tmp = [child for child in self.children]
      tmp += [self.operation]
      return tuple(tmp)

    def __hash__(self):
        return hash(self.__key())

Task: Identify a suitable hash function for all Ad operators, with identical values iff evaluation can be stored and reused. Implement. Design suggestions are available in #1179. Proper test coverage is critical.

Outcome: Implementation of hash functions, with testing. Design principles are available for new classes of Ad operators.