diff --git a/src/physics/thin_wall_hodlr.F90 b/src/physics/thin_wall_hodlr.F90 index 751b188e6..0279aa075 100644 --- a/src/physics/thin_wall_hodlr.F90 +++ b/src/physics/thin_wall_hodlr.F90 @@ -2194,9 +2194,10 @@ END SUBROUTINE fill_levels RECURSIVE SUBROUTINE subdivide_leaf(leaf,depth) TYPE(tw_oct_tree), TARGET, INTENT(inout) :: leaf INTEGER(4), INTENT(in) :: depth -INTEGER(4) :: j,k,kk,i,ii,npts,sep_max(1) +INTEGER(4) :: j,k,kk,i,ii,npts,sep_max(1),nabove INTEGER(4), ALLOCATABLE, DIMENSION(:) :: child_mark -REAL(8) :: xm,ym,zm,rleft(3),rdenom(3),locs(3),xs,extents(2,3),split_center(3) +REAL(8) :: rleft(3),rdenom(3),locs(3),xs,extents(2,3),split_center(3),split,vlow,vhigh +LOGICAL :: axis_tried(3) REAL(8), ALLOCATABLE, DIMENSION(:) :: vals REAL(8), PARAMETER :: pt_tol=0.d-6 TYPE(tw_oct_tree) :: child_tmp(2) @@ -2213,24 +2214,48 @@ RECURSIVE SUBROUTINE subdivide_leaf(leaf,depth) locs(j)=SUM((vals-split_center(j))**2) END DO DEALLOCATE(vals,child_mark) -!---Split cell in half in each direction -xm = split_center(1) !(leaf%bounds(1,1)+leaf%bounds(2,1))/2.d0 -ym = split_center(2) !(leaf%bounds(1,2)+leaf%bounds(2,2))/2.d0 -zm = split_center(3) !(leaf%bounds(1,3)+leaf%bounds(2,3))/2.d0 -DO k=1,leaf%npts - IF(ABS(active_pts(1,leaf%pts(k))-xm)<1.d-8)xm=xm+(leaf%bounds(2,1)-leaf%bounds(1,1))*1.d-4 - IF(ABS(active_pts(2,leaf%pts(k))-ym)<1.d-8)ym=ym+(leaf%bounds(2,2)-leaf%bounds(1,2))*1.d-4 - IF(ABS(active_pts(3,leaf%pts(k))-zm)<1.d-8)zm=zm+(leaf%bounds(2,3)-leaf%bounds(1,3))*1.d-4 -END DO -!---Split cell in half +!---Choose the split plane, trying axes in order of decreasing spread about the median +axis_tried=.FALSE. +DO ii=1,3 + sep_max=MAXLOC(locs,MASK=.NOT.axis_tried) + j=sep_max(1) + axis_tried(j)=.TRUE. + !---Median, moved upward off any vertex lying on it + split=split_center(j) + DO k=1,leaf%npts + IF(ABS(active_pts(j,leaf%pts(k))-split)<1.d-8)split=split+(leaf%bounds(2,j)-leaf%bounds(1,j))*1.d-4 + END DO + nabove=COUNT(active_pts(j,leaf%pts)>=split) + IF((nabove>0).AND.(nabovesplit_center(j)+1.d-8)vhigh=MIN(vhigh,xs) + END DO + IF(vlow>-1.d98)THEN + split=(vlow+split_center(j))/2.d0 + EXIT + ELSE IF(vhigh<1.d98)THEN + split=(split_center(j)+vhigh)/2.d0 + EXIT + END IF + !---All vertices lie on one plane normal to this axis, try the next one + j=0 +END DO +IF(j==0)THEN + !---Vertices coincide, so this block cannot be split further and is left as a leaf + CALL oft_warn("HODLR partitioning found a block of coincident vertices larger than the target size") + RETURN +END IF +!---Split cell in half at the plane (child 1: x_j >= split, child 2: x_j < split) child_tmp(1)%bounds=leaf%bounds child_tmp(2)%bounds=leaf%bounds -sep_max=MAXLOC(locs) -j=sep_max(1) -!WRITE(*,*)'Split',j,locs -locs=[xm,ym,zm] -child_tmp(1)%bounds(1,j)=locs(j) -child_tmp(2)%bounds(2,j)=locs(j) +child_tmp(1)%bounds(1,j)=split +child_tmp(2)%bounds(2,j)=split ! WRITE(*,*)0 ! WRITE(*,*)leaf%bounds(:,1) ! WRITE(*,*)leaf%bounds(:,2) diff --git a/src/tests/physics/test_ThinCurr_junctions.py b/src/tests/physics/test_ThinCurr_junctions.py index 9163347aa..76762acf9 100644 --- a/src/tests/physics/test_ThinCurr_junctions.py +++ b/src/tests/physics/test_ThinCurr_junctions.py @@ -503,6 +503,46 @@ def test_hodlr_coarse(): assert np.allclose(out, ref, rtol=0.0, atol=1.E-5*np.max(np.abs(ref))) +def run_hodlr_small_leaves(path, target_size, mp_q): + """Error of the HODLR L, applied to random vectors, relative to the dense L""" + result = False + try: + from OpenFUSIONToolkit import OFT_env + from OpenFUSIONToolkit.ThinCurr import ThinCurr + myOFT = OFT_env(nthreads=-1) + tw_dense = ThinCurr(myOFT) + tw_dense.setup_model(mesh_file=path) + tw_dense.compute_Lmat() + tw = ThinCurr(myOFT) + tw.setup_model(mesh_file=path) + tw.set_hodlr_options(target_size=target_size, L_svd_tol=1.E-12, L_aca_rel_tol=1.E-12) + tw.compute_Lmat(use_hodlr=True) + rng = np.random.default_rng(1) + errs = [] + for _ in range(3): + x = rng.standard_normal(tw.nelems) + ref = np.array(tw_dense.Lmat) @ x + errs.append(rel_err(tw.apply_Lmat(x), ref)) + print('HODLR L errors', errs) + result = max(errs) + except BaseException as e: + print(e) + mp_q.put(result) + + +@pytest.mark.parametrize("target_size", (30, 60)) +def test_hodlr_small_leaves(target_size): + """HODLR partitioning of the junction ring with leaves small enough that the median vertex of some blocks is tied + with more than half of the vertices at the largest coordinate along the split axis (flange plane z = 0)""" + r, lc, reg = ring() + files, _ = write_pair(r, lc, reg, 'junction_ring_leaves') + err = mp_run(run_hodlr_small_leaves, (files[0], target_size)) + # Same accuracy as with leaves large enough not to hit the tie (HODLR L differs from the dense L by ~3E-6 even + # for a single block) + assert err is not False + assert err < 1.E-5 + + def test_jumpers(tmp_path): from OpenFUSIONToolkit.ThinCurr.coils import ThinCurr_Icoil r, lc, reg, jumpers = tplate_jumpers()