def create_component(ndim, dim): # each component is one dimension -> A[i] = B[i](A[i] - A[i-1]) + B[i+1](A[i+1] - A[i]) lower_diff = StencilComponent("mesh", SparseWeightArray({Vector.zero_vector(ndim): 1, -Vector.unit_vector(dim, ndim): -1})) lower_diff *= StencilComponent("beta_{}".format(dim), SparseWeightArray({Vector.zero_vector(ndim): 1})) upper_diff = StencilComponent("mesh", SparseWeightArray({Vector.zero_vector(ndim): -1, Vector.unit_vector(dim, ndim): 1})) upper_diff *= StencilComponent("beta_{}".format(dim), SparseWeightArray({Vector.unit_vector(dim, ndim): 1})) return lower_diff + upper_diff
def reference_vector(vec): first_nonzero_position = next(i for i in range(len(vec)) if vec[i]) return -vec * Vector.unit_vector(first_nonzero_position, len(vec))
def test_collision(): """ Test using Red Black. All of them should collide. """ dimensions = 3 # Red domains are formed by the origin plus all non-negative vectors with 1-norm 2. red_domain_starts = [Vector.zero_vector(dimensions)] + [i for i in Vector.von_neumann_vectors(dimensions, 2) if all(coord >= 0 for coord in i)] red_domain = DomainUnion([ RectangularDomain([(s, -1, 2) for s in start]) for start in red_domain_starts ]) # Black domains are formed by non-negative vectors with 1-norm 1. black_domain_starts = [i for i in Vector.von_neumann_vectors(dimensions, 1) if all(coord >= 0 for coord in i)] black_domain = DomainUnion([ RectangularDomain([(s, -1, 2) for s in start]) for start in black_domain_starts ]) star_neighborhood = {Vector.unit_vector(d, dimensions): 1 for d in range(dimensions)} star_neighborhood.update( {-Vector.unit_vector(d, dimensions): 1 for d in range(dimensions)} ) red = Stencil( StencilComponent( "mesh", SparseWeightArray( star_neighborhood ) ), "mesh", red_domain ) black = Stencil( StencilComponent( "mesh", SparseWeightArray( star_neighborhood ) ), "mesh", black_domain ) red_oop = Stencil( StencilComponent( "mesh", SparseWeightArray( star_neighborhood ) ), "output", red_domain ) black_oop = Stencil( StencilComponent( "mesh", SparseWeightArray( star_neighborhood ) ), "output", black_domain ) print("Testing with simulated 32^dimensions mesh") print("WRITE-READ", "has collision") print("RED - RED", stencil_conflict(red, red, {"mesh": (32,)*dimensions})) print("BLACK - BLACK", stencil_conflict(black, black, {"mesh": (32,)*dimensions})) print("RED - BLACK", stencil_conflict(red, black, {"mesh": (32,)*dimensions})) print("BLACK - RED", stencil_conflict(black, red, {"mesh": (32,)*dimensions})) print("REDOOP - BLACKOOP", stencil_conflict(red_oop, black_oop, {"mesh": (32,)*dimensions})) print("BLACKOOP - REDOOP", stencil_conflict(black_oop, red_oop, {"mesh": (32,)*dimensions})) print("INTRA-red is valid", validate_stencil(red)) print("INTRA-black is valid", validate_stencil(black))