Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
agrayver committed Dec 15, 2019
1 parent 8fb990a commit 106f617
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 30 deletions.
2 changes: 1 addition & 1 deletion contrib/python-bindings/include/cell_accessor_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ namespace python
get_barycenter() const;

/**
* Get the barycenter of the cell.
* Get the center of the cell.
*/
PointWrapper
get_center(const bool respect_manifold = false,
Expand Down
7 changes: 6 additions & 1 deletion contrib/python-bindings/include/mapping_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ namespace python
*/
MappingQGenericWrapper(const MappingQGenericWrapper &other);

/**
* Default constructor.
*/
MappingQGenericWrapper();

/**
* Constructor.
*/
Expand All @@ -62,7 +67,7 @@ namespace python
transform_real_to_unit_cell(CellAccessorWrapper &cell, PointWrapper &point);

/**
* Get the underlying mapping
* Get the underlying mapping.
*/
void *
get_mapping();
Expand Down
9 changes: 5 additions & 4 deletions contrib/python-bindings/include/triangulation_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -348,12 +348,13 @@ namespace python

/**
* Find and return an active cell that surrounds a given point p.
* The mapping used to determine whether the given point is inside a given
* cell.
* The mapping is used to determine whether the given point is inside a
* given cell.
*/
CellAccessorWrapper
find_active_cell_around_point(PointWrapper & p,
MappingQGenericWrapper &mapping);
find_active_cell_around_point(
PointWrapper & p,
MappingQGenericWrapper mapping = MappingQGenericWrapper());

/**
* Assign a manifold object to a certain part of the triangulation.
Expand Down
2 changes: 2 additions & 0 deletions contrib/python-bindings/source/cell_accessor_wrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ namespace python
return PointWrapper(barycenter_list);
}



template <int dim, int spacedim>
PointWrapper
get_center(const bool respect_manifold,
Expand Down
10 changes: 8 additions & 2 deletions contrib/python-bindings/source/export_triangulation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ namespace python
distort_random,
1,
2)
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(
find_active_cell_around_point_overloads,
find_active_cell_around_point,
1,
2)

const char n_active_cells_docstring[] =
"Return the number of active cells \n";
Expand Down Expand Up @@ -536,8 +541,9 @@ namespace python
boost::python::args("self", "transformation"))
.def("find_active_cell_around_point",
&TriangulationWrapper::find_active_cell_around_point,
find_active_cell_around_point_docstring,
boost::python::args("self", "point", "mapping"))
find_active_cell_around_point_overloads(
boost::python::args("self", "point", "mapping"),
find_active_cell_around_point_docstring))
.def("refine_global",
&TriangulationWrapper::refine_global,
refine_global_docstring,
Expand Down
9 changes: 9 additions & 0 deletions contrib/python-bindings/source/mapping_wrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ namespace python



MappingQGenericWrapper::MappingQGenericWrapper()
: dim(-1)
, spacedim(-1)
, degree(-1)
, mapping_ptr(nullptr)
{}



MappingQGenericWrapper::MappingQGenericWrapper(const int dim,
const int spacedim,
const int degree)
Expand Down
45 changes: 24 additions & 21 deletions contrib/python-bindings/source/triangulation_wrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -539,14 +539,22 @@ namespace python

Point<spacedim> point = *(static_cast<Point<spacedim> *>(p.get_point()));

const MappingQGeneric<dim, spacedim> *mapping =
static_cast<const MappingQGeneric<dim, spacedim> *>(
mapping_wrapper.get_mapping());

auto cell_pair =
GridTools::find_active_cell_around_point(*mapping, *tria, point);

return std::make_pair(cell_pair.first->level(), cell_pair.first->index());
if (mapping_wrapper.get_mapping() != nullptr)
{
const MappingQGeneric<dim, spacedim> *mapping =
static_cast<const MappingQGeneric<dim, spacedim> *>(
mapping_wrapper.get_mapping());

auto cell_pair =
GridTools::find_active_cell_around_point(*mapping, *tria, point);
return std::make_pair(cell_pair.first->level(),
cell_pair.first->index());
}
else
{
auto cell = GridTools::find_active_cell_around_point(*tria, point);
return std::make_pair(cell->level(), cell->index());
}
}


Expand Down Expand Up @@ -1280,17 +1288,12 @@ namespace python
ExcMessage(
"The output Triangulation must be of dimension three"));

if ((dim == 2) && (spacedim == 2))
{
Triangulation<2, 2> *tria =
static_cast<Triangulation<2, 2> *>(triangulation);
Triangulation<3, 3> *tria_out = static_cast<Triangulation<3, 3> *>(
triangulation_out.get_triangulation());
GridGenerator::extrude_triangulation(*tria,
n_slices,
height,
*tria_out);
}

Triangulation<2, 2> *tria =
static_cast<Triangulation<2, 2> *>(triangulation);
Triangulation<3, 3> *tria_out =
static_cast<Triangulation<3, 3> *>(triangulation_out.get_triangulation());
GridGenerator::extrude_triangulation(*tria, n_slices, height, *tria_out);
}


Expand Down Expand Up @@ -1324,8 +1327,8 @@ namespace python

CellAccessorWrapper
TriangulationWrapper::find_active_cell_around_point(
PointWrapper & p,
MappingQGenericWrapper &mapping)
PointWrapper & p,
MappingQGenericWrapper mapping)
{
std::pair<int, int> level_index_pair;
if ((dim == 2) && (spacedim == 2))
Expand Down
30 changes: 29 additions & 1 deletion contrib/python-bindings/tests/triangulation_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import unittest
from PyDealII.Debug import *


class TestTriangulationWrapper(unittest.TestCase):

def setUp(self):
Expand Down Expand Up @@ -375,6 +374,35 @@ def test_refine_global(self):
else:
self.assertEqual(n_cells, 64)


def test_transform(self):
for dim in self.dim:
triangulation_1 = self.build_hyper_cube_triangulation(dim)
triangulation_1.refine_global(1)
triangulation_2 = self.build_hyper_cube_triangulation(dim)
triangulation_2.refine_global(1)

triangulation_1.transform(lambda p: [v + 1. for v in p])

if dim[1] == '3D':
offset = Point([1., 1., 1.])
else:
offset = Point([1., 1.])

for (cell_1, cell_2) in zip(triangulation_1.active_cells(), triangulation_2.active_cells()):
self.assertTrue(cell_1.center().distance(cell_2.center() + offset) < 1e-8)


def test_find_active_cell_around_point(self):
for dim in self.dim:
triangulation = self.build_hyper_cube_triangulation(dim)
triangulation.refine_global(2)

for cell in triangulation.active_cells():
cell_ret = triangulation.find_active_cell_around_point(cell.center())
self.assertTrue(cell.center().distance(cell_ret.center()) < 1e-8)


def test_save_load(self):
for dim in self.dim:
triangulation_1 = self.build_hyper_cube_triangulation(dim)
Expand Down

0 comments on commit 106f617

Please sign in to comment.