def charged_nucleation_in_2D(writer,
                             args,
                             R=30,
                             D=25,
                             weights=(0, -10, -8, 8)):
    dx = 2 * R / args.height
    x = (np.arange(args.width) - args.width // 2) * dx
    y = (np.arange(args.height) - args.height // 2) * dx
    x, y = np.meshgrid(x, y, indexing='ij')

    def source_G(t):
        amount = np.exp(-0.5 * (t - 5)**2)
        return (np.exp(-0.5 *
                       ((x - D)**2 + y * y)) * weights[0] + np.exp(-0.5 * (
                           (x + D)**2 + y * y)) * weights[1]) * amount

    def source_X(t):
        amount = np.exp(-0.5 * (t - 5)**2)
        return (np.exp(-0.5 *
                       ((x - D)**2 + y * y)) * weights[2] + np.exp(-0.5 * (
                           (x + D)**2 + y * y)) * weights[3]) * amount

    source_functions = {
        'G': source_G,
        'X': source_X,
    }

    noise_scale = 1e-4
    model_g = ModelG(
        bl_noise(x.shape) * noise_scale,
        bl_noise(x.shape) * noise_scale,
        bl_noise(x.shape) * noise_scale,
        dx,
        dt=args.dt,
        params=args.model_params,
        source_functions=source_functions,
    )

    print("Rendering 'Charged nucleation in 2D'")
    print("Lattice constant dx = {}, time step dt = {}".format(
        model_g.dx, model_g.dt))
    min_G = -4.672736908320116
    max_G = 0.028719261862332906
    min_X = -3.8935243721220334
    max_X = 1.2854028081816122
    min_Y = -0.7454193158963579
    max_Y = 4.20524950766914
    for n in progressbar.progressbar(range(args.num_frames)):
        model_g.step()
        if n % args.oversampling == 0:
            rgb = [
                6 * (-model_g.G + max_G) / (max_G - min_G),
                5 * (model_g.Y - min_Y) / (max_Y - min_Y),
                0.7 * (model_g.X - min_X) / (max_X - min_X),
            ]
            zero_line = 1 - tf.exp(-600 * model_g.Y**2)
            frame = make_video_frame([c * zero_line for c in rgb])
            writer.append_data(frame)
Exemple #2
0
def soliton_in_g_well_2D(writer, args, R=25, D=15):
    dx = 2 * R / args.height
    x = (np.arange(args.width) - args.width // 2) * dx
    y = (np.arange(args.height) - args.height // 2) * dx
    x, y = np.meshgrid(x, y, indexing='ij')

    def source_G(t):
        nucleator = -np.exp(-0.5 * (t - 5)**2)
        #potential = 0.015 * (np.tanh(t-25) + 1)
        #potential = 0.0003 * (np.tanh(t-25) + 1)   # gradient = (1+np.tanh(t-30)) * 0.0003 # see above

        #u = x / R
        #u = x/10 - 5  # see: "soliton_in_g_well_2D___1_seed__gradient_1__2a.mp4"
        #u = x/15 # see: "soliton_in_g_well_2D___1_seed__gradient_2__2b.mp4"
        #u = x/25
        return (
            #np.exp(-0.5*((x-D)**2+y*y)) * nucleator +
            #(u*u - 1) * potential
            np.exp(-0.5 * ((x)**2 + y * y)) * nucleator  #+ (x+8) * potential
            #(u*u - 1) * potential #+ (x+8) * gradient
        )

    source_functions = {
        'G': source_G,
    }

    noise_scale = 1e-4
    model_g = ModelG(
        bl_noise(x.shape) * noise_scale,
        bl_noise(x.shape) * noise_scale,
        bl_noise(x.shape) * noise_scale,
        dx,
        dt=args.dt,
        params=args.model_params,
        source_functions=source_functions,
    )

    print("Rendering 'Soliton in G-well in 2D'")
    print("Lattice constant dx = {}, time step dt = {}".format(
        model_g.dx, model_g.dt))
    G_scale = 0.02
    X_scale = 0.25
    Y_scale = 0.5
    for n in progressbar.progressbar(range(args.num_frames)):
        model_g.step()
        if n % args.oversampling == 0:
            rgb = [
                (G_scale * 0.5 - model_g.G) / G_scale,
                (model_g.Y - Y_scale * 0.5) / Y_scale,
                (model_g.X - X_scale * 0.5) / X_scale,
            ]
            zero_line = 1 - tf.exp(-600 * model_g.Y**2)
            frame = make_video_frame([c * zero_line for c in rgb])
            writer.append_data(frame)
def random_1D_fields():
    x = linspace(-20, 20, 512)
    dx = x[1] - x[0]
    noise_scale = 1.0

    fig, axs = subplots(2, 3)
    for ax_ in axs:
        for ax in ax_:
            while True:
                params = {
                    "A": rand() * 20,
                    "B": rand() * 20,
                    "k2": rand() * 2,
                    "k-2": rand() * 2,
                    "k5": rand() * 2,
                    "Dx": rand() * 4,
                    "Dy": rand() * 4,
                }
                model_g = ModelG(bl_noise(x.shape) * noise_scale,
                                 bl_noise(x.shape) * noise_scale,
                                 bl_noise(x.shape) * noise_scale,
                                 dx,
                                 params=params)

                while model_g.t < 20:
                    model_g.step()
                    if rand() < 0.05:
                        G, X, Y = model_g.numpy()
                        if abs(X).max() >= 100:
                            break

                G, X, Y = model_g.numpy()
                if abs(X).max() < 100:
                    break
                print("Rejected", params)

            print("Done", params)

            G /= abs(G).max()
            X /= abs(X).max()
            Y /= abs(Y).max()

            ax.plot(x, G)
            ax.plot(x, X)
            ax.plot(x, Y)
    show()
def self_stabilizing_soliton_2D():
    params = {
        "A": 4.2,
        "B": 18,
        "k2": 1.0,
        "k-2": 0.2,
        "k5": 0.9,
        "D_G": 1.0,
        "D_X": 1.0,
        "D_Y": 2.0,
    }

    x = np.linspace(-16, 16, 256)
    dx = x[1] - x[0]
    x, y = np.meshgrid(x, x)

    r2 = x * x + y * y
    model_g = ModelG(
        -np.exp(-0.1 * r2) * 1.0,
        np.exp(-r2) * 0.01,
        np.exp(-r2) * 0.01 + bl_noise(x.shape) * 0.02,
        dx,
        0.1 * dx,
        params,
    )

    def get_data():
        G, X, Y = model_g.numpy()
        x_scale = 0.2
        y_scale = 0.1
        return (
            G[64],
            X[64] * x_scale,
            Y[64] * y_scale,
        )

    G, X, Y = get_data()
    plots = []
    plots.extend(pylab.plot(x[0], G))
    plots.extend(pylab.plot(x[0], X))
    plots.extend(pylab.plot(x[0], Y))
    pylab.ylim(-0.03, 0.03)

    def update(frame):
        for _ in range(5):
            model_g.step()
        G, X, Y = get_data()
        plots[0].set_ydata(G)
        plots[1].set_ydata(X)
        plots[2].set_ydata(Y)
        return plots

    FuncAnimation(pylab.gcf(),
                  update,
                  frames=range(100),
                  init_func=lambda: plots,
                  blit=True,
                  repeat=True,
                  interval=20)
    pylab.show()

    G, X, Y = model_g.numpy()
    pylab.imshow(Y)
    pylab.show()
def random_3D():
    r = np.random.randn
    params = {
        "A": 2 + r() * 0.1,
        "B": 10 + r(),
        "k2": 1.0 + 0.1 * r(),
        "k-2": 0.1 + 0.01 * r(),
        "k5": 0.9 + 0.1 * r(),
        "D_G": 1.0,
        "D_X": 1.0 + 0.1 * r(),
        "D_Y": 2.0 + 0.1 * r(),
    }
    print(params)

    x = np.linspace(-16, 16, 128)
    dx = x[1] - x[0]
    x, y, z = np.meshgrid(x, x, x)

    def source_G(t):
        center = np.exp(-0.5 * (t - 20)**2) * 10
        gradient = (1 + np.tanh(t - 40)) * 0.0005
        print("t = {}\tcenter potential = {}\tx-gradient = {}".format(
            t, center, gradient))
        return -np.exp(-0.5 * (x * x + y * y + z * z)) * center + x * gradient

    source_functions = {
        'G': source_G,
    }

    model_g = ModelG(
        bl_noise(x.shape) * 0.01,
        bl_noise(x.shape) * 0.01,
        bl_noise(x.shape) * 0.01,
        dx,
        params,
        source_functions=source_functions,
    )

    def get_data():
        G, X, Y = model_g.numpy()
        x_scale = 0.1
        y_scale = 0.1
        return (
            G[64, 64],
            X[64, 64] * x_scale,
            Y[64, 64] * y_scale,
        )

    G, X, Y = get_data()
    plots = []
    plots.extend(pylab.plot(z[0, 0], G))
    plots.extend(pylab.plot(z[0, 0], X))
    plots.extend(pylab.plot(z[0, 0], Y))
    pylab.ylim(-0.5, 0.5)

    def update(frame):
        model_g.step()
        G, X, Y = get_data()
        plots[0].set_ydata(G)
        plots[1].set_ydata(X)
        plots[2].set_ydata(Y)
        return plots

    FuncAnimation(pylab.gcf(),
                  update,
                  frames=range(100),
                  init_func=lambda: plots,
                  blit=True,
                  repeat=True,
                  interval=20)
    pylab.show()

    G, X, Y = model_g.numpy()
    plots = [pylab.imshow(X[64])]

    def update(frame):
        model_g.step()
        G, X, Y = model_g.numpy()
        plots[0].set_data(X[64])
        return plots

    FuncAnimation(pylab.gcf(),
                  update,
                  frames=range(100),
                  init_func=lambda: plots,
                  blit=True,
                  repeat=True,
                  interval=20)
    pylab.show()

    fig = pylab.figure()
    ax = fig.add_subplot(111, projection='3d')

    G, X, Y = model_g.numpy()
    m = X.max() - (X.max() - X.min()) * 0.3
    points = []
    for _ in range(1000000):
        px = np.random.randint(x.shape[0])
        py = np.random.randint(y.shape[1])
        pz = np.random.randint(z.shape[2])

        c = X[px, py, pz]

        if c > m:
            points.append((px, py, pz, c))
            if len(points) > 20000:
                break

    xs, ys, zs, cs = zip(*points)
    ax.scatter3D(xs, ys, zs, c=cs)
    pylab.show()
def random_2D():
    r = np.random.randn
    params = {
        "A": 2 + r() * 0.1,
        "B": 10 + r(),
        "k2": 1.0 + 0.1 * r(),
        "k-2": 0.1 + 0.01 * r(),
        "k5": 0.9 + 0.1 * r(),
        "D_G": 1.0,
        "D_X": 1.0 + 0.1 * r(),
        "D_Y": 2.0 + 0.1 * r(),
    }
    print(params)

    x = np.linspace(-16, 16, 256)
    dx = x[1] - x[0]
    x, y = np.meshgrid(x, x)

    def source_G(t):
        center = np.exp(-0.5 * (t - 20)**2) * 10
        gradient = (1 + np.tanh(t - 40)) * 0.0005
        print("t = {}\tcenter potential = {}\tx-gradient = {}".format(
            t, center, gradient))
        return -np.exp(-0.5 * (x * x + y * y)) * center + x * gradient

    source_functions = {
        'G': source_G,
    }

    model_g = ModelG(
        bl_noise(x.shape) * 0.01,
        bl_noise(x.shape) * 0.01,
        bl_noise(x.shape) * 0.01,
        dx,
        0.1 * dx,
        params,
        source_functions=source_functions,
    )

    def get_data():
        G, X, Y = model_g.numpy()
        x_scale = 0.1
        y_scale = 0.1
        return (
            G[64],
            X[64] * x_scale,
            Y[64] * y_scale,
        )

    G, X, Y = get_data()
    plots = []
    plots.extend(pylab.plot(x[0], G))
    plots.extend(pylab.plot(x[0], X))
    plots.extend(pylab.plot(x[0], Y))
    pylab.ylim(-0.5, 0.5)

    def update(frame):
        model_g.step()
        G, X, Y = get_data()
        plots[0].set_ydata(G)
        plots[1].set_ydata(X)
        plots[2].set_ydata(Y)
        return plots

    FuncAnimation(pylab.gcf(),
                  update,
                  frames=range(100),
                  init_func=lambda: plots,
                  blit=True,
                  repeat=True,
                  interval=20)
    pylab.show()

    G, X, Y = model_g.numpy()
    plots = [pylab.imshow(X)]

    def update(frame):
        model_g.step()
        G, X, Y = model_g.numpy()
        plots[0].set_data(X)
        return plots

    FuncAnimation(pylab.gcf(),
                  update,
                  frames=range(100),
                  init_func=lambda: plots,
                  blit=True,
                  repeat=True,
                  interval=20)
    pylab.show()
def nucleation_and_motion_in_G_gradient_2D():
    params = {
        "A": 3.42,
        "B": 13.5,
        "k2": 1.0,
        "k-2": 0.1,
        "k5": 0.9,
        "D_G": 1.0,
        "D_X": 1.0,
        "D_Y": 1.95,
    }

    x = np.linspace(-16, 16, 128)
    dx = x[1] - x[0]
    x, y = np.meshgrid(x, x)

    def source_G(t):
        center = np.exp(-0.5 * (t - 5)**2) * 10
        gradient = (1 + np.tanh(t - 40)) * 0.0005
        # print("t = {}\tcenter potential = {}\tx-gradient = {}".format(t, center, gradient))
        return -np.exp(-0.5 * (x * x + y * y)) * center + (x + 8) * gradient

    source_functions = {
        'G': source_G,
    }

    r2 = x * x + y * y
    model_g = ModelG(
        -np.exp(-0.1 * r2) * 0,
        -np.exp(-r2) * 0.01 * 0,
        np.exp(-r2) * 0.01 * 0,
        dx,
        dt=0.1 * dx,
        params=params,
        source_functions=source_functions,
    )

    times = []
    locs = []

    def get_data():
        G, X, Y = model_g.numpy()

        loc = l2_location(X, x, y)
        times.append(model_g.t)
        locs.append(loc[0])
        print("t={}\tL2 location: {}".format(model_g.t, tuple(loc)))

        x_scale = 0.1
        y_scale = 0.1
        return (
            G[64],
            X[64] * x_scale,
            Y[64] * y_scale,
        )

    G, X, Y = get_data()
    plots = []
    plots.extend(pylab.plot(x[0], G))
    plots.extend(pylab.plot(x[0], X))
    plots.extend(pylab.plot(x[0], Y))
    pylab.ylim(-0.1, 0.1)

    def update(frame):
        for _ in range(20):
            model_g.step()
        G, X, Y = get_data()
        plots[0].set_ydata(G)
        plots[1].set_ydata(X)
        plots[2].set_ydata(Y)
        return plots

    FuncAnimation(pylab.gcf(),
                  update,
                  frames=range(100),
                  init_func=lambda: plots,
                  blit=True,
                  repeat=True,
                  interval=20)
    pylab.show()

    G, X, Y = model_g.numpy()
    pylab.imshow(X)
    pylab.show()

    pylab.plot(times, locs)
    pylab.show()
def nucleation_and_motion_in_G_gradient_1D():
    params = {
        "A": 14,
        "B": 29,
        "k2": 1.0,
        "k-2": 0.1,
        "k5": 0.9,
        "D_G": 1.0,
        "D_X": 1.0,
        "D_Y": 12,
    }

    x = np.linspace(-24, 24, 512)
    dx = x[1] - x[0]

    def source_G(t):
        center = -np.exp(-0.1 * (t - 5)**2) * 5
        gradient = (1 + np.tanh(t - 50)) * 0.0005
        print("t={}\tcenter={}\tgradient={}".format(t, center, gradient))
        return np.exp(-0.25 * x * x) * center + (x * 0.5 + 7) * gradient

    def source_X(t):
        center = np.exp(-0.1 * (t - 5)**2) * 5
        return -np.exp(-0.25 * x * x) * center

    def source_Y(t):
        center = np.exp(-0.1 * (t - 5)**2) * 0
        return np.exp(-0.25 * x * x) * center

    source_functions = {
        'G': source_G,
        'X': source_X,
        'Y': source_Y,
    }

    r2 = x * x
    model_g = ModelG(
        np.exp(-0.1 * r2) * 0,
        np.exp(-r2) * 0.01 * 0,
        np.exp(-r2) * 0.01 * 0,
        dx,
        dt=0.05 * dx,
        params=params,
        source_functions=source_functions,
    )

    def get_data():
        G, X, Y = model_g.numpy()
        x_scale = 0.1
        y_scale = 0.1
        return (
            G,
            X * x_scale,
            Y * y_scale,
        )

    G, X, Y = get_data()
    plots = []
    plots.extend(pylab.plot(x, G))
    plots.extend(pylab.plot(x, X))
    plots.extend(pylab.plot(x, Y))
    pylab.ylim(-0.1, 0.1)

    def update(frame):
        for _ in range(32):
            model_g.step()
        G, X, Y = get_data()
        plots[0].set_ydata(G)
        plots[1].set_ydata(X)
        plots[2].set_ydata(Y)
        return plots

    FuncAnimation(pylab.gcf(),
                  update,
                  frames=range(100),
                  init_func=lambda: plots,
                  blit=True,
                  repeat=True,
                  interval=20)
    pylab.show()

    G, X, Y = model_g.numpy()
    print("Total G={}, X={} Y={}".format(G.sum(), X.sum(), Y.sum()))
    print("Total G²={}, X²={} Y²={}".format((G**2).sum(), (X**2).sum(),
                                            (Y**2).sum()))