split up split and moments

This commit is contained in:
jan 2018-03-21 14:54:32 -07:00
commit acb86b8013

View file

@ -7,10 +7,11 @@ import numpy
from numpy import sin, cos, pi
from numpy.linalg import norm
from chop import pyr3d
from chop.moments import number_of_moments, get_moments
from chop.split import split, clip
from chop.shapes import build_tetrahedron, build_box, planes_from_tetrahedron, volume_of_tetrahedron
from chop.boundary import build_polyhedron, boundary_from_poly
from chop.util import check_poly, number_of_moments
from chop.util import check_poly
from chop.voxel import voxelize
logger = logging.getLogger(__name__)
@ -152,7 +153,7 @@ def test_split_tet_manual():
logger.debug(f'orient volume {volume_of_tetrahedron(tet["vertices"])}')
plane = numpy.array((-1,0,0)), 1
p_left, p_right = pyr3d.split(tet, plane)
p_left, p_right = split(tet, plane)
logger.debug(f'left {p_left}')
logger.debug(f'right {p_right}')
@ -161,16 +162,16 @@ def test_split_tet_manual():
check_poly(p_left)
check_poly(p_right)
om = pyr3d.reduce(tet, 0)
m1 = pyr3d.reduce(p_left, 0)
m2 = pyr3d.reduce(p_right, 0)
om = get_moments(tet, 0)
m1 = get_moments(p_left, 0)
m2 = get_moments(p_right, 0)
logger.debug(f'0th moments: tot {om}, left {m1}, right {m2}')
check(om, m1, m2)
om = pyr3d.reduce(tet, POLY_ORDER)
m1 = pyr3d.reduce(p_left, POLY_ORDER)
m2 = pyr3d.reduce(p_right, POLY_ORDER)
om = get_moments(tet, POLY_ORDER)
m1 = get_moments(p_left, POLY_ORDER)
m2 = get_moments(p_right, POLY_ORDER)
logger.debug(f'0th moments: tot {om}, left {m1}, right {m2}')
check(om, m1, m2)
@ -186,7 +187,7 @@ def test_split_tets_thru_centroid():
plane = thru_cent_3d(opoly[ntets//7])
# split them all about the same plane
poly12 = [pyr3d.split(o, plane) for o in opoly]
poly12 = [split(o, plane) for o in opoly]
poly1, poly2 = zip(*poly12)
for orig, p1, p2 in zip(opoly, poly1, poly2):
@ -195,9 +196,9 @@ def test_split_tets_thru_centroid():
check_poly(p2)
# reduce the original and its two parts
om = pyr3d.reduce(orig, POLY_ORDER)
m1 = pyr3d.reduce(p1, POLY_ORDER)
m2 = pyr3d.reduce(p2, POLY_ORDER)
om = get_moments(orig, POLY_ORDER)
m1 = get_moments(p1, POLY_ORDER)
m2 = get_moments(p2, POLY_ORDER)
check(om, m1, m2)
@ -212,7 +213,7 @@ def time_split():
# clip the first tet against the faces of the second
poly = polys[t]
for face in facesets[t]:
_poly = pyr3d.clip(poly, face)
_poly = clip(poly, face)
if (t+1) % 1000 == 0:
t_each = (perf_counter() - t0) / (t + 1)
logger.info(f'time per split for {t+1} splits: {t_each}')
@ -223,7 +224,7 @@ def time_split():
'neighbors': numpy.vstack(p['neighbors'] for p in polys) + numpy.arange(NUM_TRIALS).repeat(4)[:, None] * 4,
}
t1 = perf_counter()
poly = pyr3d.clip(bulk, facesets[0][0])
poly = clip(bulk, facesets[0][0])
logger.info(f'bulked split time per tet: {(perf_counter() - t1) / NUM_TRIALS}')
@ -244,15 +245,15 @@ def test_tet_tet_timing():
# clip the first tet against the faces of the second
for face in faces:
poly = pyr3d.clip(poly, face)
poly = clip(poly, face)
# find the moments (up to quadratic order) of the clipped poly
_om = pyr3d.reduce(poly, POLY_ORDER)
_om = get_moments(poly, POLY_ORDER)
if (t+1) % 1000 == 0:
t_each = (perf_counter() - t0) / (t + 1)
logger.info(f'time per split/reduce for {t+1} splits: {t_each}')
logger.info(f'done timing, time per split/reduce: {(perf_counter() - t0) / NUM_TRIALS}')
logger.info(f'time per split+get_moments for {t+1} iterations: {t_each}')
logger.info(f'done timing, time per split+get_moments: {(perf_counter() - t0) / NUM_TRIALS}')
def test_split_nonconvex():
@ -291,42 +292,42 @@ def test_split_nonconvex():
# split along the x-axis (two single connected components this direction)
plane = [numpy.array((1, 0, 0)),
-0.5 * (NZIGS - 1)]
poly1 = pyr3d.clip(opoly, plane)
poly1 = clip(opoly, plane)
plane[0] *= -1
plane[1] *= -1
poly2 = pyr3d.clip(opoly, plane)
poly2 = clip(opoly, plane)
om = pyr3d.reduce(opoly, POLY_ORDER)
m1 = pyr3d.reduce(poly1, POLY_ORDER)
m2 = pyr3d.reduce(poly2, POLY_ORDER)
om = get_moments(opoly, POLY_ORDER)
m1 = get_moments(poly1, POLY_ORDER)
m2 = get_moments(poly2, POLY_ORDER)
check(om, m1, m2)
# split along the z-axis (two single connected components this direction)
plane = [numpy.array((0, 0, 1)), -0.5]
poly1 = pyr3d.clip(opoly, plane)
poly1 = clip(opoly, plane)
plane[0] *= -1
plane[1] *= -1
poly2 = pyr3d.clip(opoly, plane)
poly2 = clip(opoly, plane)
om = pyr3d.reduce(opoly, POLY_ORDER)
m1 = pyr3d.reduce(poly1, POLY_ORDER)
m2 = pyr3d.reduce(poly2, POLY_ORDER)
om = get_moments(opoly, POLY_ORDER)
m1 = get_moments(poly1, POLY_ORDER)
m2 = get_moments(poly2, POLY_ORDER)
check(om, m1, m2)
# split along the y-axis (multiple connected components this direction)
plane = [numpy.array((0, 1, 0)),
-0.5 * (1.0 + ZOFF)]
poly1 = pyr3d.clip(opoly, plane)
poly1 = clip(opoly, plane)
plane[0] *= -1
plane[1] *= -1
poly2 = pyr3d.clip(opoly, plane)
poly2 = clip(opoly, plane)
om = pyr3d.reduce(opoly, POLY_ORDER)
m1 = pyr3d.reduce(poly1, POLY_ORDER)
m2 = pyr3d.reduce(poly2, POLY_ORDER)
om = get_moments(opoly, POLY_ORDER)
m1 = get_moments(poly1, POLY_ORDER)
m2 = get_moments(poly2, POLY_ORDER)
check(om, m1, m2)
@ -390,7 +391,7 @@ def test_voxelization():
poly = build_tetrahedron(verts)
# get its original moments for reference
moments = pyr3d.reduce(poly, POLY_ORDER)
moments = get_moments(poly, POLY_ORDER)
# voxelize it
dx = numpy.ones((3,)) / NGRID
@ -447,16 +448,16 @@ def stack_split(opoly, plane_fun=None, perturb=0):
# split the poly by making two copies of the original poly
# and them clipping them against the same plane, with one
# oriented oppositely
poly1 = pyr3d.clip(opoly, plane)
poly1 = clip(opoly, plane)
plane[0] *= -1
plane[1] *= -1
poly2 = pyr3d.clip(opoly, plane)
poly2 = clip(opoly, plane)
# reduce the original and its two parts
om = pyr3d.reduce(opoly, POLY_ORDER)
m1 = pyr3d.reduce(poly1, POLY_ORDER)
m2 = pyr3d.reduce(poly2, POLY_ORDER)
om = get_moments(opoly, POLY_ORDER)
m1 = get_moments(poly1, POLY_ORDER)
m2 = get_moments(poly2, POLY_ORDER)
check(om, m1, m2)
logger.debug(f'nstack = {len(stack)}, depth = {depth}, '