Esempio n. 1
0
def main():
    width = 300
    height = 300

    lb.set_random_seed(randint(0, 32768))
    
    def end_condition(ix, iy):
        return ix == 0 or ix == width - 1 or iy == 0 or iy == height - 1

    def loop(current_loop, max_loop, cells):
        if current_loop % 100 == 0:
            print(f'loop:{current_loop}/{max_loop}')
        return False

    # Initialize a break model
    min_guarantee = 0.0
    eta = 6.0
    model = lb.FastDBM(width, height, min_guarantee=min_guarantee, eta=eta)

    # Simulate
    sim = lb.Simulator(width, height, model)
    sim.breakdown(width // 2, height // 2)
    sim.simulate(max_loop=10000, callback_on_break=end_condition, callback_on_loop=loop)

    # Output
    save(sim.cells, Path(__file__).stem,
         output_binary=True, output_mono=True, output_gray=True)
Esempio n. 2
0
def main():
    width = 300
    height = 300

    lb.set_random_seed(randint(0, 32768))

    def loop(current_loop, max_loop, cells):
        if current_loop % 100 == 0:
            print(f'loop:{current_loop}/{max_loop}')
        return False

    # Initialize a break model
    num_particle = 15000
    model = lb.DLABreakModel(width, height, num_particle=num_particle)

    # Simulate
    sim = lb.Simulator(width, height, model)
    sim.breakdown(width // 2, height // 2)
    sim.simulate(max_loop=10000, callback_on_loop=loop)

    # Output
    save(sim.cells,
         Path(__file__).stem,
         output_binary=True,
         output_mono=True,
         output_gray=True)
Esempio n. 3
0
def main():
    # Basic Simulation
    width = 300
    height = 300
    lb.set_random_seed(randint(0, 65535))  # Set the random seed of C/C++ standard library
    sim = lb.Simulator(width, height)
    sim.breakdown(150, 150)  # First broken cell is (x,y)=(150, 150)
    sim.simulate(max_loop=2000)

    # Save to PNG
    save(sim.cells, Path(__file__).stem, output_gray=True)  # See lichtenberg/archive.py
Esempio n. 4
0
def main():
    width = 200
    height = 200
    lb.set_random_seed(randint(0, 65535))
    sim = lb.Simulator(width, height)
    sim.breakdown(100, 50)
    sim.breakdown(50, 100)
    sim.breakdown(100, 150)
    sim.breakdown(150, 100)
    sim.simulate(max_loop=1000)
    save(sim.cells, Path(__file__).stem, output_gray=True)
Esempio n. 5
0
 def draw(self):
     last_image = self.image
     image = Image.new("RGB", self.image_size)
     lb.set_random_seed(self.seed)
     if len(self.points) >= 2:
         blur_params = [(0, 1), (1, 4), (1, 8), (2, 8)]
         draw_blur(image, self.points, blur_params, self.weight, self.color,
                   self.seed)
     self.image = image
     if self.callback:
         self.callback(last_image, image)  # notify update
Esempio n. 6
0
def main():
    width = 100
    height = 100
    lb.set_random_seed(156)
    save_GIF = True
    GIF_frames: List[Image] = []

    # Initialize the field of simulation (width+2) x (height+2)
    grid = []
    for y in range(height+2):
        row = []
        for x in range(width+2):
            c = lb.DBMCell()
            c.potential = 0.0
            c.lock = False
            row.append(c)
        grid.append(row)
    # Initial Conditions:
    # - Top row has a minus potentials(attracted)
    # - Bottom row has a plus potentials(attractor)
    top, bottom = 0, height+1
    for x in range(width+2):
        grid[top][x].lock = True
        grid[bottom][x].lock = True
        grid[bottom][x].potential = 1.0
    left, right = 0, width+1
    for y in range(height+2):
        grid[y][left].lock = True
        grid[y][right].lock = True

    def end_condition(ix, iy):
        """
        If you return True, the simulation will be interrupted.
        """
        end = (iy == height-1)
        if end:
            print("Reached the end line. Break.")
        return end

    # Initialize a break model
    eta = 1.0  # Detail level of branching
    min_guarantee = 0.005
    model = lb.DielectricBreakModel(width, height, grid,
                                    min_guarantee=min_guarantee, eta=eta)

    def loop(current_loop, max_loop, cells):
        """
        If you return True, the simulation will be interrupted.
        """
        # Progress Indicator
        print(f"loop:{current_loop}/{max_loop}")

        # True to generate animation frames
        if save_GIF:
            if current_loop % 10 == 0:
                frame = visualize_field(width, height, cells, model)
                GIF_frames.append(frame)
        return False

    # Simulate
    sim = lb.Simulator(width, height, model, up=False)
    sim.breakdown(width // 2, 0)
    sim.simulate(max_loop=10000, callback_on_break=end_condition, callback_on_loop=loop)

    # Output
    gamma = 1.0/1.8
    scale = 1.0
    save(sim.cells, Path(__file__).stem,
         output_binary=True, output_mono=True, output_gray=True,
         gamma=gamma, scale=scale)

    if save_GIF:
        GIF_frames[0].save(Path(__file__).stem + ".gif",
                           save_all=True,
                           append_images=GIF_frames[1:],
                           optimize=False, duration=100, loop=0)
Esempio n. 7
0
def main():
    width = 300
    height = 300
    seed = 49057  # randint(0, 65535)
    print(f"seed={seed}")
    lb.set_random_seed(seed)

    # Simulate
    sim = lb.Simulator(width, height)
    sim.insulate_circle(75, 225, 150)
    sim.breakdown(10, 10)
    sim.simulate(max_loop=2000)

    # Get Longest Path
    tree = lb.Tree(sim.cells)
    leaves = tree.get_leaves()
    leaves = sorted(leaves, key=lambda lf: lf.count, reverse=True)
    longest_leaf = leaves[0]
    longest_points = []
    node = longest_leaf.node
    while node is not None:
        x, y, _ = node.point
        longest_points.append((x, y))
        node = node.parent

    # Generate Potential-Points on the path
    n = len(longest_points)
    step = 1
    potentials = []
    potential_curve = func_potential()
    for i in range(0, n, step):
        t = i / n
        x, y = longest_points[i]
        p = potential_curve(t)
        potentials.append((x, y, p))

    # Output
    margin = 50
    field_width = width + margin * 2
    field_height = height + margin * 2
    time_factors = [1.0, 0.5, 0.1]
    for i, t in enumerate(time_factors):
        print(f"{i + 1}/{len(time_factors)}")

        # Simulate potential field
        flares: List[FlarePoint] = []
        for x, y, p in potentials:
            flares.append((margin + x, margin + y, p * t))
        field = lb.make_electric_field(field_width, field_height, flares)

        # Make Images
        gamma = 1.0
        img = Image.new("L", (width, height))
        for y in range(height):
            for x in range(width):
                p = field[margin + y][margin + x]
                p = p**gamma
                c = int(p * 255)
                img.putpixel((x, y), (c, ))
        img.save(Path(__file__).stem + f"_{i:02}.png")

    img = Image.new("L", (width, height))
    draw = ImageDraw.Draw(img)
    n = len(potentials)
    for i in range(n - 1):
        x1, y1, _ = potentials[i]
        x2, y2, _ = potentials[i + 1]
        draw.line((x1, y1, x2, y2), fill=(255, ), width=10)
    img.save(Path(__file__).stem + "_line.png")