Commit 8a381715 authored by dboe's avatar dboe
Browse files

new tests

parent 24e39747
import unittest
import tfields
import numpy as np
class BoundingBox_Test(unittest.TestCase):
def setUp(self):
self._mesh = tfields.Mesh3D.grid((5.6, 6.2, 3),
(-0.25, 0.25, 4),
(-1, 1, 10))
self._cuts = {'x': [5.7, 6.1],
'y': [-0.2, 0, 0.2],
'z': [-0.5, 0.5]}
def test_tree(self):
# test already in doctests.
tree = tfields.bounding_box.Node(self._mesh,
self._cuts,
at_intersection='keep')
leaves = tree.leaves()
leaves = tfields.bounding_box.Node.sort_leaves(leaves)
meshes = [leaf.mesh for leaf in leaves]
templates = [leaf.template for leaf in leaves]
special_leaf = tree.find_leaf([5.65, -0.21, 0])
class Searcher_Test(unittest.TestCase):
def setUp(self):
self._mesh = tfields.Mesh3D.grid((0, 1, 2), (1, 2, 2), (2, 3, 2))
print(self._mesh, self._mesh.maps)
def test_tree(self):
tree = tfields.bounding_box.Searcher(self._mesh, n_sections=[5, 5, 5])
points = tfields.Tensors([[0.5, 1, 2.1],
[0.5, 0, 0],
[0.5, 2, 2.1],
[0.5, 1.5, 2.5]])
box_res = tree.in_faces(points, delta=0.0001)
usual_res = self._mesh.in_faces(points, delta=0.0001)
# print(box_res)
# print(usual_res)
self.assertTrue(np.array_equal(box_res, usual_res))
if __name__ == '__main__':
unittest.main()
import unittest
import tfields
from tempfile import NamedTemporaryFile
class IO_Test(unittest.TestCase):
def test_npz(self):
p = tfields.Points3D([[1., 2., 3.], [4., 5., 6.], [1, 2, -6]],
name='my_points')
scalars = tfields.Tensors([0, 1, 2], name=42)
vectors = tfields.Tensors([[0, 0, 0], [0, 0, 1], [0, -1, 0]])
maps = [tfields.TensorFields([[0, 1, 2], [0, 1, 2]], [42, 21]),
tfields.TensorFields([[1], [2]], [-42, -21])]
m = tfields.TensorMaps(vectors, scalars,
maps=maps)
out_file = NamedTemporaryFile(suffix='.npz')
# Simply give the file name to save
p.save(out_file.name)
_ = out_file.seek(0) # this is only necessary in the test
p1 = tfields.Points3D.load(out_file.name)
self.assertTrue(p.equal(p1))
self.assertEqual(p.coord_sys, p1.coord_sys)
# The fully nested structure of a TensorMaps object is reconstructed
out_file_maps = NamedTemporaryFile(suffix='.npz')
m.save(out_file_maps.name)
_ = out_file_maps.seek(0)
m1 = tfields.TensorMaps.load(out_file_maps.name,
allow_pickle=True)
self.assertTrue(m.equal(m1))
self.assertEqual(m.maps[3].dtype, m1.maps[3].dtype)
# Names are preserved
self.assertEqual(p.name, 'my_points')
self.assertEqual(m.names, [42])
if __name__ == '__main__':
unittest.main()
...@@ -391,7 +391,8 @@ class Searcher(Node): ...@@ -391,7 +391,8 @@ class Searcher(Node):
values = np.linspace(minima[i], maxima[i], n_cuts)[1:-1] values = np.linspace(minima[i], maxima[i], n_cuts)[1:-1]
cut[key] = values cut[key] = values
return super(Searcher, self).__init__(mesh, cut, at_intersection='keep', return super(Searcher, self).__init__(mesh, cut,
at_intersection='keep',
delta=delta) delta=delta)
def in_faces(self, tensors, delta=-1, assign_multiple=False): def in_faces(self, tensors, delta=-1, assign_multiple=False):
...@@ -432,7 +433,8 @@ class Searcher(Node): ...@@ -432,7 +433,8 @@ class Searcher(Node):
continue continue
if leaf.template.nfaces() == 0: if leaf.template.nfaces() == 0:
continue continue
leaf_mask = leaf.template.triangles()._in_triangles(point, delta) leaf_mask = leaf.template.triangles()._in_triangles(point,
delta)
original_face_indices = leaf.template.maps[3].fields[0][leaf_mask] original_face_indices = leaf.template.maps[3].fields[0][leaf_mask]
if not assign_multiple and len(original_face_indices) > 0: if not assign_multiple and len(original_face_indices) > 0:
original_face_indices = original_face_indices[:1] original_face_indices = original_face_indices[:1]
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment