37 lines
2.1 KiB
Python
37 lines
2.1 KiB
Python
import unittest,itertools
|
|
import numpy as np
|
|
from scipy.spatial import ConvexHull
|
|
from phase2_b14_math import below_fraction,band_fraction,layers,manifold_edge,channels,families
|
|
class ChannelTests(unittest.TestCase):
|
|
def test_clip_against_convex_hull(self):
|
|
rng=np.random.default_rng(17);x=np.vstack([np.zeros(3),np.eye(3)])
|
|
for k in range(80):
|
|
f=rng.uniform(-1,1,4);cut=float(rng.uniform(f.min(),f.max()));pts=list(x[f<cut])
|
|
for i,j in itertools.combinations(range(4),2):
|
|
if (f[i]<cut)!=(f[j]<cut):pts.append(x[i]+(cut-f[i])/(f[j]-f[i])*(x[j]-x[i]))
|
|
vol=ConvexHull(np.array(pts)).volume*6;self.assertAlmostEqual(float(below_fraction(f[None],cut)[0]),vol,places=12)
|
|
def test_constant_strict_boundary(self):
|
|
f=np.array([[1,1,1,1],[.5,.5,.5,.5],[0,0,1,1]],float)
|
|
np.testing.assert_allclose(band_fraction(f,.5,1),[0,0,.5])
|
|
def test_partition_volume(self):
|
|
f=np.array([[0,.1,.7,1],[0,0,1,1]],float)
|
|
total=sum(band_fraction(f,a,b) for a,b in zip([0,.2,.5,.7],[.2,.5,.7,1]))
|
|
np.testing.assert_allclose(total,1,atol=1e-14)
|
|
def test_binary_layers(self):
|
|
np.testing.assert_array_equal(layers(np.array([0,.25,.5,1.])),[0,.125,.25,.5])
|
|
def test_edge_link_point_contact_and_fork(self):
|
|
self.assertFalse(manifold_edge([[0,1,2,3],[0,1,4,5]],0,1))
|
|
self.assertFalse(manifold_edge([[0,1,2,3],[0,1,3,4],[0,1,3,5]],0,1))
|
|
self.assertTrue(manifold_edge([[0,1,2,3],[0,1,3,4]],0,1))
|
|
def test_single_tet_witness_and_mismatch(self):
|
|
x=np.vstack([np.zeros(3),np.eye(3)]);t=np.array([[0,1,2,3]]);f=np.array([0,1,1,1.]);d=np.array([0,1,1,1.]);mass=np.ones(4)/24
|
|
e,r,g=channels(x,t,f,d,mass,np.array([1/6]),np.ones(1),np.array([0]),lambda *a:None)
|
|
np.testing.assert_allclose(e,np.tile([1,0],(4,1)))
|
|
e,r,g=channels(x,t,f,d*.8,mass,np.array([1/6]),np.ones(1),np.array([0]),lambda *a:None)
|
|
self.assertTrue(np.isnan(e[1:]).all());np.testing.assert_array_equal(e[0],[1,0])
|
|
def test_disconnected_tets_no_air_family(self):
|
|
t=np.array([[0,1,2,3],[4,5,6,7]]);f=np.tile([0,1,1,1.],2)
|
|
label,v,frac,bad=families(t,f,np.array([1.,3.]),.5)
|
|
self.assertEqual(len(v),2);self.assertAlmostEqual(v.max()/v.min(),3)
|
|
if __name__=='__main__':unittest.main()
|