def setUpClass(cls): """ Add some custom terminals. """ Expression.clear_terminals() Expression.subscribe_terminal('foo') Expression.subscribe_terminal('bar')
def parse(self, conf): """ Parse the given config section into an instantiated Mesh. Args: conf (configparser section): The config section for the mesh. Raises: AttributeError: If no mesh type is defined. """ if 'type' not in conf: raise AttributeError('Must define a mesh type.') mesh_type = conf.pop('type') if mesh_type.lower() == 'file': mesh_type = 'Mesh' if not mesh_type.endswith('Mesh'): mesh_type += 'Mesh' mesh_cls = getattr(firedrake, mesh_type) element = conf.pop('element') order = conf.pop('order') try: order = int(order) except ValueError as e: raise TypeError from e processed_args = [] args = conf.pop('params', None) if args is not None: args = Expression(args) processed_args = args.evaluate(None) kwargs = {} for k, v in conf.items(): expr = Expression(v) kwargs[k] = expr.evaluate(None) self.mesh = mesh_cls(*processed_args, **kwargs) self.func_space = firedrake.FunctionSpace(self.mesh, element, order)
def test_with_two_terminal(self): """ Test used terminals is populated when multiple terminals are used. """ expr = Expression('foo * (1 + bar)') self.assertListEqual(['foo', 'bar'], expr.used_terminals)
def test_with_one_terminal(self): """ Test used terminals is populated when a terminal is used. """ expr = Expression('foo * 2') self.assertListEqual(['foo'], expr.used_terminals)
def test_no_terminals(self): """ Test used terminals is empty when no terminals are used. """ expr = Expression('1 + 2') self.assertListEqual([], expr.used_terminals)
def tearDownClass(cls): """ Clear the terminals. """ Expression.clear_terminals()
def test_expression_str(expr_str, expr_val): """ Test that a variety of expressions are correctly parsed. """ expr = Expression(expr_str) assert str(expr) == expr_val
def test_expressions(expr_str, expr_val): """ Test that a variety of expressions are correctly parsed. """ expr = Expression(expr_str).evaluate() assert isclose(expr, expr_val)