Example #1
0
    def evaluate_ignoring_errors(self):
        """
        Evaluates the expression. If a node exists in the tree that cannot be evaluated
        as a scalar or vector (e.g. Parameter, Variable, StateVector, InputParameter),
        then None is returned. Otherwise the result of the evaluation is given

        See Also
        --------
        evaluate : evaluate the expression

        """
        try:
            result = self.evaluate(t=0, u="shape test")
        except NotImplementedError:
            # return None if NotImplementedError is raised
            # (there is a e.g. Parameter, Variable, ... in the tree)
            return None
        except TypeError as error:
            # return None if specific TypeError is raised
            # (there is a e.g. StateVector in the tree)
            if error.args[0] == "StateVector cannot evaluate input 'y=None'":
                return None
            else:
                raise error
        except ValueError as e:
            raise pybamm.ShapeError(
                "Cannot find shape (original error: {})".format(e))
        return result
Example #2
0
 def concatenation(self, disc_children):
     """Discrete concatenation, taking `edge_to_node` for children that evaluate on
     edges.
     See :meth:`pybamm.SpatialMethod.concatenation`
     """
     for idx, child in enumerate(disc_children):
         n_nodes = sum(
             len(mesh.nodes) for mesh in self.mesh.combine_submeshes(*child.domain)
         )
         n_edges = sum(
             len(mesh.edges) for mesh in self.mesh.combine_submeshes(*child.domain)
         )
         child_size = child.size
         if child_size != n_nodes:
             # Average any children that evaluate on the edges (size n_edges) to
             # evaluate on nodes instead, so that concatenation works properly
             if child_size == n_edges:
                 disc_children[idx] = self.edge_to_node(child)
             else:
                 raise pybamm.ShapeError(
                     """
                     child must have size n_nodes (number of nodes in the mesh)
                     or n_edges (number of edges in the mesh)
                     """
                 )
     return pybamm.DomainConcatenation(disc_children, self.mesh)
Example #3
0
    def test_shape(self):
        """
        Check that the discretised self has a pybamm `shape`, i.e. can be evaluated

        Raises
        ------
        pybamm.ShapeError
            If the shape of the object cannot be found
        """
        try:
            self.shape_for_testing
        except ValueError as e:
            raise pybamm.ShapeError("Cannot find shape (original error: {})".format(e))