# Path: sucemfem/Testing/Meshes.py
# class InscribedTet(object):
#     def __init__(self):
#         self.coordinates = N.array(
#             [[-1./2 ,     1./2 ,    -1./2],
#              [ 1./2 ,     1./2 ,     1./2],
#              [ 1./2 ,    -1./2 ,    -1./2],
#              [-1./2 ,    -1./2 ,     1./2],  
#              [ 1./6 ,     1./6 ,    -1./6],
#              [-1./6 ,     1./6 ,     1./6],
#              [-1./6 ,    -1./6 ,    -1./6],
#              [ 1./6 ,    -1./6 ,     1./6]],
#             N.float64)
#         self.element_nodes = N.array(
#             [[1, 4, 6, 7],
#              [1, 3, 5, 7],          
#              [3, 4, 7, 8],          
#              [1, 2, 5, 6],          
#              [2, 3, 5, 8],          
#              [2, 4, 6, 8],          
#              [4, 6, 7, 8],          
#              [1, 5, 6, 7],          
#              [3, 5, 7, 8],          
#              [2, 5, 6, 8],          
#              [5, 6, 7, 8]], N.uint32) - 1
# 
#     def get_dolfin_mesh(self):
#         mesh = dolfin.Mesh()
#         me = dolfin.MeshEditor()
#         me.open(mesh,'tetrahedron', 3, 3)
#         me.init_vertices(len(self.coordinates))
#         me.init_cells(len(self.element_nodes))
#         for i, coord in enumerate(self.coordinates):
#             me.add_vertex(N.uint(i), *coord)
#         for i, el_nodes in enumerate(self.element_nodes):
#             me.add_cell(i, *el_nodes)
#         me.close()
#         return mesh
# 
# Path: sucemfem/Geometry.py
# class EnsureInitialised(object):
# class BoundaryEdges(object):
# class BoundaryEdgeCells(object):
# class CellsConnected2Edges(object):
#     def __init__(self, dolfin_mesh):
#     def __call__(self, dim0, dim1):
#     def __init__(self, mesh, boundary_facefun=None, boundary_value=1):
#     def mark(self, edge_meshfun, value):
#     def __init__(self, mesh):
#     def mark(self, cell_meshfun, value):
#     def __init__(self, boundary_edgefun, boundary_value):
#     def mark(self, cell_meshfun, value):


import unittest
import dolfin
import numpy as np
from sucemfem.Testing.Meshes import InscribedTet
from sucemfem import Geometry 
        self.DUT = Geometry.BoundaryEdges(self.mesh)
        
    def test_mark(self):
        edge_meshfunc = dolfin.EdgeFunction('uint', self.mesh)
        edge_meshfunc.set_all(0)
        test_val = 11
        self.DUT.mark(edge_meshfunc, test_val)
        no_edges = len(edge_meshfunc.array())
        self.assertTrue(
            sum(edge_meshfunc.array() == test_val) == self.no_boundary_edges)
        for edge in dolfin.SubsetIterator(edge_meshfunc, test_val):
            self.assertTrue(self._check_on_boundary(edge))

    def _check_on_boundary(self, entity):
        """Check if an entity is on boundary using numerical node positions

        For the unit cube geometry an entity is on a boundary face if
        all the nodes have one coordinate value that is equal to 0 or
        one. Eg. if all the x values are equal to 1 that means the
        entity lies on the x=0 boundary face.
        """
        node_coords = entity.mesh().coordinates()[entity.entities(0)]
        for coordvals in node_coords.T:
            if np.allclose(coordvals,0) or np.allclose(coordvals, 1):
                return True
        return False

class test_BoundaryEdgeCells(unittest.TestCase):
    def setUp(self):
        self.mesh = InscribedTet().get_dolfin_mesh()

# Path: sucemfem/Testing/Meshes.py
# class InscribedTet(object):
#     def __init__(self):
#         self.coordinates = N.array(
#             [[-1./2 ,     1./2 ,    -1./2],
#              [ 1./2 ,     1./2 ,     1./2],
#              [ 1./2 ,    -1./2 ,    -1./2],
#              [-1./2 ,    -1./2 ,     1./2],  
#              [ 1./6 ,     1./6 ,    -1./6],
#              [-1./6 ,     1./6 ,     1./6],
#              [-1./6 ,    -1./6 ,    -1./6],
#              [ 1./6 ,    -1./6 ,     1./6]],
#             N.float64)
#         self.element_nodes = N.array(
#             [[1, 4, 6, 7],
#              [1, 3, 5, 7],          
#              [3, 4, 7, 8],          
#              [1, 2, 5, 6],          
#              [2, 3, 5, 8],          
#              [2, 4, 6, 8],          
#              [4, 6, 7, 8],          
#              [1, 5, 6, 7],          
#              [3, 5, 7, 8],          
#              [2, 5, 6, 8],          
#              [5, 6, 7, 8]], N.uint32) - 1
# 
#     def get_dolfin_mesh(self):
#         mesh = dolfin.Mesh()
#         me = dolfin.MeshEditor()
#         me.open(mesh,'tetrahedron', 3, 3)
#         me.init_vertices(len(self.coordinates))
#         me.init_cells(len(self.element_nodes))
#         for i, coord in enumerate(self.coordinates):
#             me.add_vertex(N.uint(i), *coord)
#         for i, el_nodes in enumerate(self.element_nodes):
#             me.add_cell(i, *el_nodes)
#         me.close()
#         return mesh
# 
# Path: sucemfem/Geometry.py
# class EnsureInitialised(object):
# class BoundaryEdges(object):
# class BoundaryEdgeCells(object):
# class CellsConnected2Edges(object):
#     def __init__(self, dolfin_mesh):
#     def __call__(self, dim0, dim1):
#     def __init__(self, mesh, boundary_facefun=None, boundary_value=1):
#     def mark(self, edge_meshfun, value):
#     def __init__(self, mesh):
#     def mark(self, cell_meshfun, value):
#     def __init__(self, boundary_edgefun, boundary_value):
#     def mark(self, cell_meshfun, value):


# Path: sucemfem/tests/test_Geometry.py
import unittest
import dolfin
import numpy as np
from sucemfem.Testing.Meshes import InscribedTet
from sucemfem import Geometry 

        self.DUT = Geometry.BoundaryEdges(self.mesh)
        
    def test_mark(self):
        edge_meshfunc = dolfin.EdgeFunction('uint', self.mesh)
        edge_meshfunc.set_all(0)
        test_val = 11
        self.DUT.mark(edge_meshfunc, test_val)
        no_edges = len(edge_meshfunc.array())
        self.assertTrue(
            sum(edge_meshfunc.array() == test_val) == self.no_boundary_edges)
        for edge in dolfin.SubsetIterator(edge_meshfunc, test_val):
            self.assertTrue(self._check_on_boundary(edge))

    def _check_on_boundary(self, entity):
        """Check if an entity is on boundary using numerical node positions

        For the unit cube geometry an entity is on a boundary face if
        all the nodes have one coordinate value that is equal to 0 or
        one. Eg. if all the x values are equal to 1 that means the
        entity lies on the x=0 boundary face.
        """
        node_coords = entity.mesh().coordinates()[entity.entities(0)]
        for coordvals in node_coords.T:
            if np.allclose(coordvals,0) or np.allclose(coordvals, 1):
                return True
        return False

class test_BoundaryEdgeCells(unittest.TestCase):
    def setUp(self):
        self.mesh = InscribedTet().get_dolfin_mesh()

        self.DUT = Geometry.BoundaryEdgeCells(self.mesh)