예제 #1
0
def main():
    def pde(x, y):
        dy_r = dde.grad.jacobian(y, x, i=0, j=0)
        dy_rr = dde.grad.hessian(y, x, i=0, j=0)
        dy_thetatheta = dde.grad.hessian(y, x, i=1, j=1)
        return x[:, 0:1] * dy_r + x[:, 0:1]**2 * dy_rr + dy_thetatheta

    def solution(x):
        r, theta = x[:, 0:1], x[:, 1:]
        return r * np.cos(theta)

    geom = dde.geometry.Rectangle(xmin=[0, 0], xmax=[1, 2 * np.pi])
    bc_rad = dde.DirichletBC(
        geom,
        lambda x: np.cos(x[:, 1:2]),
        lambda x, on_boundary: on_boundary and np.isclose(x[0], 1),
    )
    data = dde.data.PDE(geom,
                        pde,
                        bc_rad,
                        num_domain=2540,
                        num_boundary=80,
                        solution=solution)

    net = dde.maps.FNN([2] + [20] * 3 + [1], "tanh", "Glorot normal")
    # Use [r*sin(theta), r*cos(theta)] as features,
    # so that the network is automatically periodic along the theta coordinate.
    net.apply_feature_transform(lambda x: tf.concat(
        [x[:, 0:1] * tf.sin(x[:, 1:2]), x[:, 0:1] * tf.cos(x[:, 1:2])], axis=1)
                                )

    model = dde.Model(data, net)
    model.compile("adam", lr=1e-3, metrics=["l2 relative error"])
    losshistory, train_state = model.train(epochs=15000)
    dde.saveplot(losshistory, train_state, issave=True, isplot=True)
예제 #2
0
def main():
    def pde(x, y):
        dy_x = tf.gradients(y, x)[0]
        dy_x, dy_y = dy_x[:, 0:1], dy_x[:, 1:]
        dy_xx = tf.gradients(dy_x, x)[0][:, 0:1]
        dy_yy = tf.gradients(dy_y, x)[0][:, 1:]
        return -dy_xx - dy_yy - 1

    def boundary(_, on_boundary):
        return on_boundary

    def func(x):
        return np.zeros([len(x), 1])

    geom = dde.geometry.Polygon([[0, 0], [1, 0], [1, -1], [-1, -1], [-1, 1], [0, 1]])
    bc = dde.DirichletBC(geom, func, boundary)

    data = dde.data.PDE(geom, pde, bc, num_domain=1200, num_boundary=120, num_test=1500)
    net = dde.maps.FNN([2] + [50] * 4 + [1], "tanh", "Glorot uniform")
    model = dde.Model(data, net)

    model.compile("adam", lr=0.001)
    model.train(epochs=50000)
    model.compile("L-BFGS-B")
    losshistory, train_state = model.train()
    dde.saveplot(losshistory, train_state, issave=True, isplot=True)
예제 #3
0
def main():
    def pde(x, y):
        dy_x = tf.gradients(y, x)[0]
        dy_xx = tf.gradients(dy_x, x)[0]
        return dy_xx - 2

    def boundary_l(x, on_boundary):
        return on_boundary and np.isclose(x[0], -1)

    def boundary_r(x, on_boundary):
        return on_boundary and np.isclose(x[0], 1)

    def func(x):
        return (x + 1)**2

    geom = dde.geometry.Interval(-1, 1)
    bc_l = dde.DirichletBC(geom, func, boundary_l)
    bc_r = dde.RobinBC(geom, lambda X, y: y, boundary_r)
    data = dde.data.PDE(geom,
                        pde, [bc_l, bc_r],
                        16,
                        2,
                        solution=func,
                        num_test=100)

    layer_size = [1] + [50] * 3 + [1]
    activation = "tanh"
    initializer = "Glorot uniform"
    net = dde.maps.FNN(layer_size, activation, initializer)

    model = dde.Model(data, net)
    model.compile("adam", lr=0.001, metrics=["l2 relative error"])
    losshistory, train_state = model.train(epochs=10000)

    dde.saveplot(losshistory, train_state, issave=True, isplot=True)
def main():
    def pde(x, y):
        dy_xx = dde.grad.hessian(y, x, i=0, j=0)
        dy_yy = dde.grad.hessian(y, x, i=1, j=1)
        return -dy_xx - dy_yy - 1

    def boundary(_, on_boundary):
        return on_boundary

    geom = dde.geometry.Polygon([[0, 0], [1, 0], [1, -1], [-1, -1], [-1, 1],
                                 [0, 1]])
    bc = dde.DirichletBC(geom, lambda x: 0, boundary)

    data = dde.data.PDE(geom,
                        pde,
                        bc,
                        num_domain=1200,
                        num_boundary=120,
                        num_test=1500)
    net = dde.maps.FNN([2] + [50] * 4 + [1], "tanh", "Glorot uniform")
    model = dde.Model(data, net)

    model.compile("adam", lr=0.001)
    model.train(epochs=50000)
    model.compile("L-BFGS-B")
    losshistory, train_state = model.train()
    dde.saveplot(losshistory, train_state, issave=True, isplot=True)
예제 #5
0
def main():
    geom = dde.geometry.TimeDomain(0, 5)
    ic = dde.IC(geom, func, lambda _, on_initial: on_initial)

    quad_deg = 20
    data = dde.data.IDE(
        geom,
        ide,
        ic,
        quad_deg,
        kernel=kernel,
        num_domain=10,
        num_boundary=2,
        train_distribution="uniform",
    )

    layer_size = [1] + [20] * 3 + [1]
    activation = "tanh"
    initializer = "Glorot uniform"
    net = dde.maps.FNN(layer_size, activation, initializer)

    model = dde.Model(data, net)
    model.compile("L-BFGS-B")
    model.train()

    X = geom.uniform_points(100)
    y_true = func(X)
    y_pred = model.predict(X)
    print("L2 relative error:", dde.metrics.l2_relative_error(y_true, y_pred))

    plt.figure()
    plt.plot(X, y_true, "-")
    plt.plot(X, y_pred, "o")
    plt.show()
    np.savetxt("test.dat", np.hstack((X, y_true, y_pred)))
예제 #6
0
def main():
    def func(x):
        """
        x: array_like, N x D_in
        y: array_like, N x D_out
        """
        return x * np.sin(5 * x)

    geom = dde.geometry.Interval(-1, 1)
    num_train = 10
    num_test = 1000
    data = dde.data.Function(geom, func, num_train, num_test)

    layer_size = [1] + [50] * 3 + [1]
    activation = "tanh"
    initializer = "Glorot uniform"
    regularization = ["l2", 1e-5]
    dropout_rate = 0.01
    net = dde.maps.FNN(
        layer_size,
        activation,
        initializer,
        regularization=regularization,
        dropout_rate=dropout_rate,
    )

    model = dde.Model(data, net)
    model.compile("adam", lr=0.001, metrics=["l2 relative error"])
    losshistory, train_state = model.train(epochs=30000, uncertainty=True)

    dde.saveplot(losshistory, train_state, issave=True, isplot=True)
def main():
    geom = dde.geometry.Disk([0, 0], 1)

    observe_x = geom.random_points(30)
    observe_y = dde.PointSetBC(observe_x, func(observe_x))

    data = dde.data.FPDE(
        geom,
        fpde,
        alpha,
        observe_y,
        [8, 100],
        num_domain=64,
        anchors=observe_x,
        solution=func,
    )

    net = dde.maps.FNN([2] + [20] * 4 + [1], "tanh", "Glorot normal")
    net.apply_output_transform(
        lambda x, y: (1 - tf.reduce_sum(x**2, axis=1, keepdims=True)) * y)

    model = dde.Model(data, net)
    model.compile("adam", lr=1e-3, loss_weights=[1, 100])
    variable = dde.callbacks.VariableValue(alpha, period=1000)
    losshistory, train_state = model.train(epochs=10000, callbacks=[variable])
    dde.saveplot(losshistory, train_state, issave=True, isplot=True)
예제 #8
0
def solve_poisson_with_dl(lightweight=False):
    # geom = dde.geometry.Polygon([[0, 0], [1, 0], [1, -1], [-1, -1], [-1, 1], [0, 1]])
    geom = dde.geometry.Rectangle(xmin=[0, 0], xmax=[1, 1])

    # bc = dde.DirichletBC(geom, lambda x: 0, boundary)
    bc = dde.DirichletBC(geom, _func, lambda _, on_boundary: on_boundary)

    data = dde.data.PDE(
        geom,
        _pde,
        bc,
        num_domain=1200,
        num_boundary=120,
        num_test=10000,
        # solution=_func,
    )

    # NN
    layer_size = [2] + [128] + [256] + [512] + [256] + [128] + [1]
    activation = 'relu'
    initializer = 'Glorot uniform'
    net = dde.maps.FNN(layer_size, activation, initializer)
    model = dde.Model(data, net)

    # Train NN
    model.compile('adam', lr=0.0005)

    losshistory, train_state = model.train(epochs=10000)

    if not lightweight:
        dde.saveplot(losshistory, train_state, issave=True, isplot=True)
        save_contour_from_model(model, 1, 1, 'poisson')

    return model
예제 #9
0
def main():
    geom = dde.geometry.Interval(0, 1)
    bc = dde.DirichletBC(geom, func, lambda _, on_boundary: on_boundary)
    data = dde.data.PDE(
        geom,
        pde,
        bc,
        1280,
        2,
        train_distribution="pseudo",
        solution=func,
        num_test=10000,
    )

    layer_size = [1] + [100] * 3 + [1]
    activation = "tanh"
    initializer = "Glorot uniform"
    net = dde.maps.MsFFN(layer_size, activation, initializer, sigmas=[1, 10])

    model = dde.Model(data, net)
    model.compile(
        "adam",
        lr=0.001,
        metrics=["l2 relative error"],
        decay=("inverse time", 2000, 0.9),
    )

    pde_residual_resampler = dde.callbacks.PDEResidualResampler(period=1)
    model.train(epochs=20000, callbacks=[pde_residual_resampler])

    dde.saveplot(model.losshistory, model.train_state, issave=True, isplot=True)
예제 #10
0
def main():
    """
    main program
    haotian song 2020/12/23 
    """
    geom = dde.geometry.TimeDomain(0, 3)
    y0 = InitialCondition()
    a = dde.config.real.set_complex128()
    ic = []
    for j in range(0, len(y0)):
        ic.append(dde.IC(geom, lambda X: y0, boundary, component=j))
    # BoundaryCondition = dde.OperatorBC(geom,boundaryFunction,on_boundary=on_boundary)
    data = dde.data.PDE(geom,
                        SLE_DL,
                        ic,
                        num_domain=400,
                        num_boundary=2,
                        num_test=100)
    layer_size = [1] + [50] * 3 + [1]
    activation = "tanh"
    initializer = "Glorot uniform"
    net = dde.maps.FNN(layer_size, activation, initializer)
    model = dde.Model(data, net)
    model.compile("adam", lr=0.001)
    variable = dde.callbacks.VariableValue([C1, C2, C3],
                                           period=600,
                                           filename="variables.dat")
    losshistory, train_state = model.train(epochs=60000, callbacks=[variable])
    dde.saveplot(losshistory, train_state, issave=True, isplot=True)
def main():
    kf = tf.Variable(0.05)
    D = tf.Variable(1.0)

    def pde(x, y):
        ca, cb = y[:, 0:1], y[:, 1:2]
        dca_t = dde.grad.jacobian(y, x, i=0, j=1)
        dca_xx = dde.grad.hessian(y, x, component=0, i=0, j=0)
        dcb_t = dde.grad.jacobian(y, x, i=1, j=1)
        dcb_xx = dde.grad.hessian(y, x, component=1, i=0, j=0)
        eq_a = dca_t - 1e-3 * D * dca_xx + kf * ca * cb ** 2
        eq_b = dcb_t - 1e-3 * D * dcb_xx + 2 * kf * ca * cb ** 2
        return [eq_a, eq_b]

    def fun_bc(x):
        return 1 - x[:, 0:1]

    def fun_init(x):
        return np.exp(-20 * x[:, 0:1])

    geom = dde.geometry.Interval(0, 1)
    timedomain = dde.geometry.TimeDomain(0, 10)
    geomtime = dde.geometry.GeometryXTime(geom, timedomain)

    bc_a = dde.DirichletBC(
        geomtime, fun_bc, lambda _, on_boundary: on_boundary, component=0
    )
    bc_b = dde.DirichletBC(
        geomtime, fun_bc, lambda _, on_boundary: on_boundary, component=1
    )
    ic1 = dde.IC(geomtime, fun_init, lambda _, on_initial: on_initial, component=0)
    ic2 = dde.IC(geomtime, fun_init, lambda _, on_initial: on_initial, component=1)

    observe_x, Ca, Cb = gen_traindata()
    ptset = dde.bc.PointSet(observe_x)
    observe_y1 = dde.DirichletBC(
        geomtime, ptset.values_to_func(Ca), lambda x, _: ptset.inside(x), component=0
    )
    observe_y2 = dde.DirichletBC(
        geomtime, ptset.values_to_func(Cb), lambda x, _: ptset.inside(x), component=1
    )

    data = dde.data.TimePDE(
        geomtime,
        pde,
        [bc_a, bc_b, ic1, ic2, observe_y1, observe_y2],
        num_domain=2000,
        num_boundary=100,
        num_initial=100,
        anchors=observe_x,
        num_test=50000,
    )
    net = dde.maps.FNN([2] + [20] * 3 + [2], "tanh", "Glorot uniform")
    model = dde.Model(data, net)
    model.compile("adam", lr=0.001)
    variable = dde.callbacks.VariableValue(
        [kf, D], period=1000, filename="variables.dat"
    )
    losshistory, train_state = model.train(epochs=80000, callbacks=[variable])
    dde.saveplot(losshistory, train_state, issave=True, isplot=True)
예제 #12
0
파일: Burgers.py 프로젝트: lululxvi/deepxde
def main():
    geom = dde.geometry.Interval(-1, 1)
    timedomain = dde.geometry.TimeDomain(0, 0.99)
    geomtime = dde.geometry.GeometryXTime(geom, timedomain)

    bc = dde.DirichletBC(geomtime, lambda x: 0, lambda _, on_boundary: on_boundary)
    ic = dde.IC(
        geomtime, lambda x: -np.sin(np.pi * x[:, 0:1]), lambda _, on_initial: on_initial
    )

    data = dde.data.TimePDE(
        geomtime, pde, [bc, ic], num_domain=2540, num_boundary=80, num_initial=160
    )
    net = dde.maps.FNN([2] + [20] * 3 + [1], "tanh", "Glorot normal")
    model = dde.Model(data, net)

    model.compile("adam", lr=1e-3)
    model.train(epochs=15000)
    model.compile("L-BFGS-B")
    losshistory, train_state = model.train()
    dde.saveplot(losshistory, train_state, issave=True, isplot=True)

    X, y_true = gen_testdata()
    y_pred = model.predict(X)
    f = model.predict(X, operator=pde)
    print("Mean residual:", np.mean(np.absolute(f)))
    print("L2 relative error:", dde.metrics.l2_relative_error(y_true, y_pred))
    np.savetxt("test.dat", np.hstack((X, y_true, y_pred)))
예제 #13
0
파일: wave_1d.py 프로젝트: fabyayu/deepxde
def main():
    def pde(x, y):
        dy_tt = dde.grad.hessian(y, x, i=1, j=1)
        dy_xx = dde.grad.hessian(y, x, i=0, j=0)
        return dy_tt - C**2 * dy_xx

    def func(x):
        x, t = np.split(x, 2, axis=1)
        return np.sin(np.pi * x) * np.cos(C * np.pi * t) + np.sin(
            A * np.pi * x) * np.cos(A * C * np.pi * t)

    geom = dde.geometry.Interval(0, 1)
    timedomain = dde.geometry.TimeDomain(0, 1)
    geomtime = dde.geometry.GeometryXTime(geom, timedomain)

    bc = dde.DirichletBC(geomtime, func, lambda _, on_boundary: on_boundary)
    ic_1 = dde.IC(geomtime, func, lambda _, on_initial: on_initial)
    # do not use dde.NeumannBC here, since `normal_derivative` does not work with temporal coordinate.
    ic_2 = dde.OperatorBC(
        geomtime,
        lambda x, y, _: dde.grad.jacobian(y, x, i=0, j=1),
        lambda x, _: np.isclose(x[1], 0),
    )
    data = dde.data.TimePDE(
        geomtime,
        pde,
        [bc, ic_1, ic_2],
        num_domain=360,
        num_boundary=360,
        num_initial=360,
        solution=func,
        num_test=10000,
    )

    layer_size = [2] + [100] * 3 + [1]
    activation = "tanh"
    initializer = "Glorot uniform"
    net = dde.maps.STMsFFN(layer_size,
                           activation,
                           initializer,
                           sigmas_x=[1],
                           sigmas_t=[1, 10])
    net.apply_feature_transform(lambda x: (x - 0.5) * 2 * np.sqrt(3))

    model = dde.Model(data, net)
    initial_losses = get_initial_loss(model)
    loss_weights = 5 / initial_losses
    model.compile(
        "adam",
        lr=0.001,
        metrics=["l2 relative error"],
        loss_weights=loss_weights,
        decay=("inverse time", 2000, 0.9),
    )
    pde_residual_resampler = dde.callbacks.PDEResidualResampler(period=1)
    losshistory, train_state = model.train(epochs=10000,
                                           callbacks=[pde_residual_resampler],
                                           display_every=500)

    dde.saveplot(losshistory, train_state, issave=True, isplot=True)
예제 #14
0
def main():
    fname_lo_train = "dataset/mf_lo_train.dat"
    fname_hi_train = "dataset/mf_hi_train.dat"
    fname_hi_test = "dataset/mf_hi_test.dat"

    data = dde.data.MfDataSet(
        fname_lo_train=fname_lo_train,
        fname_hi_train=fname_hi_train,
        fname_hi_test=fname_hi_test,
        col_x=(0, ),
        col_y=(1, ),
    )

    activation = "tanh"
    initializer = "Glorot uniform"
    regularization = ["l2", 0.01]
    net = dde.maps.MfNN(
        [1] + [20] * 4 + [1],
        [10] * 2 + [1],
        activation,
        initializer,
        regularization=regularization,
    )

    model = dde.Model(data, net)
    model.compile("adam", lr=0.001, metrics=["l2 relative error"])
    losshistory, train_state = model.train(epochs=80000)

    dde.saveplot(losshistory, train_state, issave=True, isplot=True)
예제 #15
0
def mfnn(data):
    x_dim, y_dim = 3, 1
    activation = "selu"
    initializer = "LeCun normal"
    regularization = ["l2", 0.01]
    net = dde.maps.MfNN(
        [x_dim] + [128] * 2 + [y_dim],
        [8] * 2 + [y_dim],
        activation,
        initializer,
        regularization=regularization,
        residue=True,
        trainable_low_fidelity=True,
        trainable_high_fidelity=True,
    )

    model = dde.Model(data, net)
    model.compile("adam", lr=0.0001, loss="MAPE", metrics=["MAPE", "APE SD"])
    losshistory, train_state = model.train(epochs=30000)
    # checker = dde.callbacks.ModelCheckpoint(
    #     "model/model.ckpt", verbose=1, save_better_only=True, period=1000
    # )
    # losshistory, train_state = model.train(epochs=30000, callbacks=[checker])
    # losshistory, train_state = model.train(epochs=5000, model_restore_path="model/model.ckpt-28000")

    dde.saveplot(losshistory, train_state, issave=True, isplot=False)
    return (
        train_state.best_metrics[1],
        train_state.best_metrics[3],
        train_state.best_y[1],
    )
예제 #16
0
파일: mf_func.py 프로젝트: zzjzzjzp/deepxde
def main():
    def func_lo(x):
        A, B, C = 0.5, 10, -5
        return A * (6 * x - 2)**2 * np.sin(12 * x - 4) + B * (x - 0.5) + C

    def func_hi(x):
        return (6 * x - 2)**2 * np.sin(12 * x - 4)

    geom = dde.geometry.Interval(0, 1)
    num_test = 1000
    data = dde.data.MfFunc(geom, func_lo, func_hi, 100, 6, num_test)

    activation = "tanh"
    initializer = "Glorot uniform"
    regularization = ["l2", 0.01]
    net = dde.maps.MfNN(
        [1] + [20] * 4 + [1],
        [10] * 2 + [1],
        activation,
        initializer,
        regularization=regularization,
    )

    model = dde.Model(data, net)
    model.compile("adam", lr=0.001, metrics=["l2 relative error"])
    losshistory, train_state = model.train(epochs=80000)

    dde.saveplot(losshistory, train_state, issave=True, isplot=True)
예제 #17
0
    def define_pinn(self, geomtime, input_data, observe_train):

        ## Define the network
        self.net = dde.maps.FNN(
            [self.input] + [self.hidden_layer_size] * self.num_hidden_layers +
            [self.output], "tanh", "Glorot uniform")

        ## Select relevant PDE (Dim, Heterogeneity, forward/inverse)
        if self.dim == 1:
            pde = self.dynamics.pde_1D
        elif self.dim == 2 and self.heter:
            if self.inverse and 'd' in self.inverse:
                pde = self.dynamics.pde_2D_heter
                self.net.apply_output_transform(self.dynamics.modify_inv_heter)
            else:
                pde = self.dynamics.pde_2D_heter_forward
                self.net.apply_output_transform(self.dynamics.modify_heter)
        elif self.dim == 2 and not self.heter:
            pde = self.dynamics.pde_2D

        ## Define PINN model
        self.pde_data = dde.data.TimePDE(geomtime,
                                         pde,
                                         input_data,
                                         num_domain=self.num_domain,
                                         num_boundary=self.num_boundary,
                                         anchors=observe_train,
                                         num_test=self.num_test)
        self.model = dde.Model(self.pde_data, self.net)
        self.model.compile("adam", lr=self.lr)
        return 0
예제 #18
0
def run(m, net, lr, epochs):
    d = np.load("train.npz")
    X_train, y_train = (d["X_train0"], d["X_train1"]), d["y_train"]
    d = np.load("test.npz")
    X_test, y_test = (d["X_test0"], d["X_test1"]), d["y_test"]

    X_test_trim = trim_to_65535(X_test)[0]
    y_test_trim = trim_to_65535(y_test)[0]
    data = dde.data.OpDataSet(
        X_train=X_train, y_train=y_train, X_test=X_test_trim, y_test=y_test_trim
    )

    model = dde.Model(data, net)
    model.compile("adam", lr=lr, metrics=[mean_squared_error_outlier])
    checker = dde.callbacks.ModelCheckpoint(
        "model/model.ckpt", save_better_only=True, period=1000
    )
    losshistory, train_state = model.train(epochs=epochs, callbacks=[checker])
    dde.saveplot(losshistory, train_state, issave=False, isplot=True)

    model.restore("model/model.ckpt-" + str(train_state.best_step), verbose=1)
    safe_test(model, data, X_test, y_test)

    for i in range(10):
        d = np.load("example{}.npz".format(i))
        X_test, y_test = (d["X_test0"], d["X_test1"]), d["y_test"]
        safe_test(model, data, X_test, y_test, fname="example{}.dat".format(i))
예제 #19
0
def main():
    geom = dde.geometry.Sphere([0, 0, 0], 1)
    bc = dde.DirichletBC(geom, func, lambda _, on_boundary: on_boundary)

    data = dde.data.FPDE(
        geom,
        fpde,
        alpha,
        bc,
        [8, 8, 100],
        num_domain=256,
        num_boundary=1,
        solution=func,
    )

    net = dde.maps.FNN([3] + [20] * 4 + [1], "tanh", "Glorot normal")
    net.apply_output_transform(
        lambda x, y: (1 - tf.reduce_sum(x**2, axis=1, keepdims=True)) * y)

    model = dde.Model(data, net)
    model.compile("adam", lr=1e-3)
    losshistory, train_state = model.train(epochs=10000)
    dde.saveplot(losshistory, train_state, issave=False, isplot=True)

    X = geom.random_points(10000)
    y_true = func(X)
    y_pred = model.predict(X)
    print("L2 relative error:", dde.metrics.l2_relative_error(y_true, y_pred))
    np.savetxt("test.dat", np.hstack((X, y_true, y_pred)))
예제 #20
0
def main():
    geom = dde.geometry.Interval(-1, 1)
    num_train = 10
    num_test = 1000
    data = dde.data.Function(geom, func, num_train, num_test)

    layer_size = [1] + [50] * 3 + [1]
    activation = "tanh"
    initializer = "Glorot uniform"
    regularization = ["l2", 1e-5]
    dropout_rate = 0.01
    net = dde.maps.FNN(
        layer_size,
        activation,
        initializer,
        regularization=regularization,
        dropout_rate=dropout_rate,
    )

    model = dde.Model(data, net)
    uncertainty = dde.callbacks.DropoutUncertainty(period=1000)
    model.compile("adam", lr=0.001, metrics=["l2 relative error"])
    losshistory, train_state = model.train(epochs=30000,
                                           callbacks=[uncertainty])

    dde.saveplot(losshistory, train_state, issave=True, isplot=True)
예제 #21
0
def Poisson_PoliANN(Nd, Nb, Nh, Nl, sigma, opt, initializer):

    # PDE Definition using the TensorFlow notation
    def pde(x, y):

        f = 2 * (np.pi**2) * tf.sin(np.pi * x[:, 0:1]) \
            * tf.sin(np.pi * x[:, 1:2])

        # Definition of the spatial derivatives
        dy_x = tf.gradients(y, x)[0]
        dy_x, dy_y = dy_x[:, 0:1], dy_x[:, 1:]
        dy_xx = tf.gradients(dy_x, x)[0][:, 0:1]
        dy_yy = tf.gradients(dy_y, x)[0][:, 1:]

        # Definition of the Poisson equation
        return dy_xx + dy_yy + f

    # Definition of the boundary
    def boundary(_, on_boundary):
        return on_boundary

    # Definition of the homogeneous Dirichlet
    # boundary conditions
    def func(x):
        return np.zeros([len(x), 1])

    # Geometry definition

    geom = dde.geometry.Rectangle([0, 0], [1, 1])

    # Imposition of the Dirichlet boundary condition
    bc = dde.DirichletBC(geom, func, boundary)

    # ANN model definition
    data = dde.data.PDE(geom, pde, bc, num_domain=Nd, num_boundary=Nb)
    net = dde.maps.FNN([2] + [Nl] * Nh + [1], sigma, initializer)

    # Strict enforcement of the BCs for the unit square domain
    # net.apply_output_transform(lambda x, y: \
    #                           x[:,0:1] * (1 - x[:, 0:1]) * \
    #                           x[:,1:2] * (1 - x[:, 1:2]) * y)

    model = dde.Model(data, net)

    # ANN model training
    if opt == 'adam':
        model.compile(opt, lr=0.001)
        losshistory, train_state = model.train(epochs=10000)
    elif opt == 'L-BFGS-B':
        model.compile(opt)
        losshistory, train_state = model.train()
    elif opt == 'mixed':
        model.compile('adam', lr=0.001)
        model.train(epochs=5000)
        model.compile('L-BFGS-B')
        losshistory, train_state = model.train()
        dde.saveplot(losshistory, train_state, issave=True, isplot=True)

    return model
def main():
    alpha0 = 1.8
    alpha = tf.Variable(1.5)

    def fpde(x, y, int_mat):
        """(D_{0+}^alpha + D_{1-}^alpha) u(x)
        """
        if isinstance(int_mat, (list, tuple)) and len(int_mat) == 3:
            int_mat = tf.SparseTensor(*int_mat)
            lhs = tf.sparse_tensor_dense_matmul(int_mat, y)
        else:
            lhs = tf.matmul(int_mat, y)
        lhs /= 2 * tf.cos(alpha * np.pi / 2)
        rhs = gamma(alpha0 + 2) * x
        return lhs - rhs[: tf.size(lhs)]

    def func(x):
        return x * (np.abs(1 - x ** 2)) ** (alpha0 / 2)

    geom = dde.geometry.Interval(-1, 1)

    observe_x = np.linspace(-1, 1, num=20)[:, None]
    observe_y = dde.PointSetBC(observe_x, func(observe_x))

    # Static auxiliary points
    # data = dde.data.FPDE(
    #     geom,
    #     fpde,
    #     alpha,
    #     observe_y,
    #     [101],
    #     meshtype="static",
    #     anchors=observe_x,
    #     solution=func,
    # )
    # Dynamic auxiliary points
    data = dde.data.FPDE(
        geom,
        fpde,
        alpha,
        observe_y,
        [100],
        meshtype="dynamic",
        num_domain=20,
        anchors=observe_x,
        solution=func,
        num_test=100,
    )

    net = dde.maps.FNN([1] + [20] * 4 + [1], "tanh", "Glorot normal")
    net.apply_output_transform(lambda x, y: (1 - x ** 2) * y)

    model = dde.Model(data, net)

    model.compile("adam", lr=1e-3, loss_weights=[1, 100])
    variable = dde.callbacks.VariableValue(alpha, period=1000)
    losshistory, train_state = model.train(epochs=10000, callbacks=[variable])
    dde.saveplot(losshistory, train_state, issave=True, isplot=True)
예제 #23
0
def main():
    spatial_domain = dde.geometry.Rectangle(xmin=[-0.5, -0.5], xmax=[1, 1.5])

    boundary_condition_u = dde.DirichletBC(spatial_domain,
                                           u_func,
                                           lambda _, on_boundary: on_boundary,
                                           component=0)
    boundary_condition_v = dde.DirichletBC(spatial_domain,
                                           v_func,
                                           lambda _, on_boundary: on_boundary,
                                           component=1)
    boundary_condition_right_p = dde.DirichletBC(spatial_domain,
                                                 p_func,
                                                 boundary_outflow,
                                                 component=2)

    data = dde.data.TimePDE(
        spatial_domain,
        pde,
        [
            boundary_condition_u, boundary_condition_v,
            boundary_condition_right_p
        ],
        num_domain=2601,
        num_boundary=400,
        num_test=100000,
    )

    net = dde.maps.FNN([2] + 4 * [50] + [3], "tanh", "Glorot normal")

    model = dde.Model(data, net)

    model.compile("adam", lr=1e-3)
    model.train(epochs=30000)
    model.compile("L-BFGS-B")
    losshistory, train_state = model.train()

    X = spatial_domain.random_points(100000)
    output = model.predict(X)
    u_pred = output[:, 0]
    v_pred = output[:, 1]
    p_pred = output[:, 2]

    u_exact = u_func(X).reshape(-1)
    v_exact = v_func(X).reshape(-1)
    p_exact = p_func(X).reshape(-1)

    f = model.predict(X, operator=pde)

    l2_difference_u = dde.metrics.l2_relative_error(u_exact, u_pred)
    l2_difference_v = dde.metrics.l2_relative_error(v_exact, v_pred)
    l2_difference_p = dde.metrics.l2_relative_error(p_exact, p_pred)
    residual = np.mean(np.absolute(f))

    print("Mean residual:", residual)
    print("L2 relative error in u:", l2_difference_u)
    print("L2 relative error in v:", l2_difference_v)
    print("L2 relative error in p:", l2_difference_p)
예제 #24
0
def visualize_solution(nu):
    # Build pseudo model
    def pde(x, u):
        u_x = dde.grad.jacobian(u, x, i=0, j=0)
        u_t = dde.grad.jacobian(u, x, i=0, j=1)
        u_xx = dde.grad.hessian(u, x, i=0, j=0)
        return u_t + u * u_x - nu * u_xx

    spatial_domain = dde.geometry.Interval(x_min, x_max)
    temporal_domain = dde.geometry.TimeDomain(t_min, t_max)
    spatio_temporal_domain = dde.geometry.GeometryXTime(
        spatial_domain, temporal_domain)

    boundary_condition = dde.DirichletBC(spatio_temporal_domain, lambda x: 0,
                                         lambda _, on_boundary: on_boundary)
    initial_condition = dde.IC(spatio_temporal_domain,
                               lambda x: -np.sin(np.pi * x[:, 0:1]),
                               lambda _, on_initial: on_initial)

    data = dde.data.TimePDE(spatio_temporal_domain,
                            pde, [boundary_condition, initial_condition],
                            num_domain=domain_points,
                            num_boundary=boundary_points,
                            num_initial=initial_points,
                            num_test=test_points)

    net = dde.maps.FNN([2] + hidden_layers * [hidden_units] + [1], "tanh",
                       "Glorot normal")

    model = dde.Model(data, net)

    # Load reference solution
    file_name = 'Reference_Solutions/u_exact_nu_{}.mat'.format(nus[i])
    data = scipy.io.loadmat(file_name)
    u_exact = data['usol'].T
    x_test, t_test = np.meshgrid(np.linspace(x_min, x_max, test_points_x),
                                 np.linspace(t_min, t_max, test_points_t))
    X = np.vstack((np.ravel(x_test), np.ravel(t_test))).T

    # Reload model and make predictions
    model_name = 'Neural_Networks/nu_{}/Burger_Equation_Source_Model_nu_{}-{}'.format(
        nus[i], nus[i], epochs_source[i])
    model.compile("adam", lr=learning_rate)
    model.train(model_restore_path=model_name, epochs=0)

    u_pred = model.predict(X).reshape(test_points_t, test_points_x)
    f = model.predict(X, operator=pde)

    ax = plt.axes(projection="3d")
    ax.plot_wireframe(x_test, t_test, u_pred)
    ax.set_xlabel('location x')
    ax.set_ylabel('time t')
    ax.set_zlabel('u')
    plt.tight_layout()
    plt.savefig('Predictions/Predicted_solution_nu_{}.png'.format(nus[i]),
                dpi=300)

    return
예제 #25
0
def main():
    def pde(x, y):
        dy_x = tf.gradients(y, x)[0]
        dy_x, dy_t = dy_x[:, 0:1], dy_x[:, 1:2]
        dy_xx = tf.gradients(dy_x, x)[0][:, 0:1]
        return dy_t + y * dy_x - 0.01 / np.pi * dy_xx

    geom = dde.geometry.Interval(-1, 1)
    timedomain = dde.geometry.TimeDomain(0, 0.99)
    geomtime = dde.geometry.GeometryXTime(geom, timedomain)

    bc = dde.DirichletBC(geomtime, lambda x: np.zeros((len(x), 1)),
                         lambda _, on_boundary: on_boundary)
    ic = dde.IC(geomtime, lambda x: -np.sin(np.pi * x[:, 0:1]),
                lambda _, on_initial: on_initial)

    data = dde.data.TimePDE(geomtime,
                            1,
                            pde, [bc, ic],
                            num_domain=2500,
                            num_boundary=100,
                            num_initial=160)
    net = dde.maps.FNN([2] + [20] * 3 + [1], "tanh", "Glorot normal")
    model = dde.Model(data, net)

    model.compile("adam", lr=1.0e-3)
    model.train(epochs=10000)
    model.compile("L-BFGS-B")
    model.train()

    X = geomtime.random_points(100000)
    err = 1
    while err > 0.005:
        f = model.predict(X, operator=pde)
        err_eq = np.absolute(f)
        err = np.mean(err_eq)
        print("Mean residual: %.3e" % (err))

        x_id = np.argmax(err_eq)
        print("Adding new point:", X[x_id], "\n")
        data.add_anchors(X[x_id])
        early_stopping = dde.callbacks.EarlyStopping(min_delta=1e-4,
                                                     patience=2000)
        model.compile("adam", lr=1e-3)
        model.train(epochs=10000,
                    disregard_previous_best=True,
                    callbacks=[early_stopping])
        model.compile("L-BFGS-B")
        losshistory, train_state = model.train()
    dde.saveplot(losshistory, train_state, issave=True, isplot=True)

    X, y_true = gen_testdata()
    y_pred = model.predict(X)
    print("L2 relative error:", dde.metrics.l2_relative_error(y_true, y_pred))
    np.savetxt("test.dat", np.hstack((X, y_true, y_pred)))
예제 #26
0
파일: stokes.py 프로젝트: lululxvi/hpinn
def main():
    geom = dde.geometry.Rectangle([0, 0], [1, 1])

    net = dde.maps.PFNN([2] + [[64] * 4] * 4 + [4], "tanh",
                        "Glorot normal")  # ?
    net.apply_output_transform(output_transform)

    losses = [
        dde.OperatorBC(geom, volume, lambda x, _: not geom.on_boundary(x)),
        dde.OperatorBC(
            geom, volume,
            lambda x, _: not geom.on_boundary(x)),  # augmented Lagrangian
        dde.OperatorBC(geom, dissipated_power,
                       lambda x, _: not geom.on_boundary(x)),
    ]

    dx = 0.01
    data = dde.data.PDE(
        geom,
        pde,
        losses,
        num_domain=int(geom.area / dx**2),
        num_boundary=int(geom.perimeter / dx),
    )
    model = dde.Model(data, net)

    mu_PDE, mu_V = 0.1, 1e4  # ?
    print("-" * 80)
    print(f"Iteration 0: mu = {mu_PDE}, {mu_V}\n")
    # loss_weights = [mu_PDE / 3] * 3 + [mu_V] + [1]  # penalty
    # loss = ["MSE", "MSE", "MSE", loss_volume, loss_power]
    loss_weights = [mu_PDE / 3] * 3 + [0] * 3 + [mu_V, 0] + [
        1
    ]  # augmented Lagrangian
    loss = ["MSE"] * 3 + ["zero"] * 3 + [loss_volume, "zero", loss_power]
    model.compile(
        "adam",
        lr=0.0001,
        loss=loss,
        loss_weights=loss_weights,
    )
    losshistory, train_state = model.train(epochs=20000)
    # save_solution(geom, model, "solution")
    # return
    model.compile(
        "L-BFGS-B",
        loss=loss,
        loss_weights=loss_weights,
    )
    losshistory, train_state = model.train()
    save_solution(geom, model, "solution0")

    augmented_Lagrangian(model, geom, mu_PDE, mu_V, 2)

    dde.saveplot(losshistory, train_state, issave=True, isplot=False)
예제 #27
0
def main():
    C1 = tf.Variable(1.0)
    C2 = tf.Variable(1.0)
    C3 = tf.Variable(1.0)

    def Lorenz_system(x, y):
        """Lorenz system.
        dy1/dx = 10 * (y2 - y1)
        dy2/dx = y1 * (28 - y3) - y2
        dy3/dx = y1 * y2 - 8/3 * y3
        """
        y1, y2, y3 = y[:, 0:1], y[:, 1:2], y[:, 2:]
        dy1_x = dde.grad.jacobian(y, x, i=0)
        dy2_x = dde.grad.jacobian(y, x, i=1)
        dy3_x = dde.grad.jacobian(y, x, i=2)
        return [
            dy1_x - C1 * (y2 - y1),
            dy2_x - y1 * (C2 - y3) + y2,
            dy3_x - y1 * y2 + C3 * y3,
        ]

    def boundary(_, on_initial):
        return on_initial

    geom = dde.geometry.TimeDomain(0, 3)

    # Initial conditions
    ic1 = dde.IC(geom, lambda X: -8, boundary, component=0)
    ic2 = dde.IC(geom, lambda X: 7, boundary, component=1)
    ic3 = dde.IC(geom, lambda X: 27, boundary, component=2)

    # Get the train data
    observe_t, ob_y = gen_traindata()
    observe_y0 = dde.PointSetBC(observe_t, ob_y[:, 0:1], component=0)
    observe_y1 = dde.PointSetBC(observe_t, ob_y[:, 1:2], component=1)
    observe_y2 = dde.PointSetBC(observe_t, ob_y[:, 2:3], component=2)

    data = dde.data.PDE(
        geom,
        Lorenz_system,
        [ic1, ic2, ic3, observe_y0, observe_y1, observe_y2],
        num_domain=400,
        num_boundary=2,
        anchors=observe_t,
    )

    net = dde.maps.FNN([1] + [40] * 3 + [3], "tanh", "Glorot uniform")
    model = dde.Model(data, net)
    model.compile("adam", lr=0.001)
    variable = dde.callbacks.VariableValue([C1, C2, C3],
                                           period=600,
                                           filename="variables.dat")
    losshistory, train_state = model.train(epochs=60000, callbacks=[variable])
    dde.saveplot(losshistory, train_state, issave=True, isplot=True)
예제 #28
0
def main():
    
    def pde(x, y):
        dy_t = dde.grad.jacobian(y, x,i=0, j=4)
        dy_xx = dde.grad.hessian(y, x,component=0 , j=0)
        # dy_xx = dde.grad.hessian(y, x , j=0)
        return (
            dy_t
            - dy_xx
            + tf.exp(-x[:, 4:])
            * (tf.sin(np.pi * x[:, 0:1]) - np.pi ** 2 * tf.sin(np.pi * x[:, 0:1])),
            x[:, 0:1] * 0,
        )

    def func(x):
        return np.sin(np.pi * x[:, 0:1]) * np.exp(-x[:, 4:])
    def func2(x):
        return np.sin(np.pi * x[:, 0:1]) * np.exp(-x[:, 4:]),0

    # geom = dde.geometry.Interval(-1, 1)
    geom = dde.geometry.Rectangle
    geom = dde.geometry.Hypercube([-1]*4,[1]*4)
    timedomain = dde.geometry.TimeDomain(0, 1)
    geomtime = dde.geometry.GeometryXTime(geom, timedomain)

    bc = dde.DirichletBC(geomtime, func, lambda _, on_boundary: on_boundary,component=0)
    ic = dde.IC(geomtime, func, lambda _, on_initial: on_initial,component=0)
    ic2 = dde.IC(geomtime,lambda shit:  1, lambda _, on_initial: on_initial,component=1)
    # observe_x = np.vstack((np.linspace(-1, 1, num=10), np.full((10), 1))).T
    # ptset = dde.bc.PointSet(observe_x)
    # observe_y = dde.DirichletBC(
    #     geomtime, ptset.values_to_func(func(observe_x)), lambda x, _: ptset.inside(x)
    # )

    data = dde.data.TimePDE(
        geomtime,
        pde,
        [bc, ic,ic2],
        num_domain=4000,
        num_boundary=1,
        num_initial=100,
    )
    
    layer_size = [5] + [32] * 3 + [2]
    activation = "tanh"
    initializer = "Glorot uniform"
    net = dde.maps.FNN(layer_size, activation, initializer)

    model = dde.Model(data, net)

    model.compile("adam", lr=0.001, metrics=["l2 relative error"])
    losshistory, train_state = model.train(epochs=10000)

    dde.saveplot(losshistory, train_state, issave=True, isplot=True)
예제 #29
0
def main():
    #sir params
    beta = 1  #infection
    gamma = .2  #removal
    N = 150  #population
    initial_infected = 20

    #x is time, y is odes
    def odes(x, y):
        s, i, r = y[:, 0:1], y[:, 1:2], y[:, 2:3]
        ds_x = tf.gradients(s, x)[0]
        di_x = tf.gradients(i, x)[0]
        dr_x = tf.gradients(r, x)[0]
        de1 = ds_x + beta * i * s / N
        de2 = di_x - (beta * i * s / N) + (gamma * i)
        de3 = dr_x - gamma * i
        # de1 = tf.gradients(ds_x, x)[0] - s
        # de2 = tf.gradients(di_x, x)[0] - i
        # de3 = tf.gradients(dr_x, x)[0] - r
        return [de1, de2, de3]

    def boundary(_, on_initial):
        return on_initial

    def func(x):
        return np.hstack((0 * np.cos(x), 0 * np.cos(x), 0 * np.cos(x)))
        #return np.hstack(((N-initial_infected)*np.cos(x), initial_infected*np.cos(x), 0*np.cos(x)))

    geom = dde.geometry.TimeDomain(0, 15)
    ic1 = dde.IC(geom,
                 lambda x: (N - initial_infected) * np.ones(x.shape),
                 boundary,
                 component=0)
    ic2 = dde.IC(geom,
                 lambda x: initial_infected * np.ones(x.shape),
                 boundary,
                 component=1)
    ic3 = dde.IC(geom, lambda x: 0 * np.ones(x.shape), boundary, component=2)
    data = dde.data.PDE(geom, odes, [ic1, ic2, ic3], 1000, 3)

    layer_size = [1] + [50] * 3 + [3]
    activation = "tanh"
    initializer = "Glorot uniform"
    net = dde.maps.FNN(layer_size, activation, initializer)

    model = dde.Model(data, net)

    model.compile("adam", lr=0.001)
    losshistory, train_state = model.train(epochs=20000)

    #dde.saveplot(losshistory, train_state, issave=True, isplot=True)
    plot_best_state(train_state)
    plot_loss_history(losshistory)
    plt.show()
def train_source_model(nu):
  path = Path('Neural_Networks', 'nu_{}'.format(nus[i]))

  if path.exists() and path.is_dir():
    shutil.rmtree(path)

  os.mkdir(path)
  
  def pde(x, u):
    du_xx = dde.grad.hessian(u, x)
    return du_xx + nu*np.pi ** 2 * tf.sin(np.pi * x)
  
  def func(x):
    return nu*np.sin(np.pi * x)

  spatial_domain = dde.geometry.Interval(x_min, x_max)

  boundary_condition = dde.DirichletBC(spatial_domain, lambda x: 0, lambda _, on_boundary: on_boundary)


  data = dde.data.PDE(spatial_domain, pde, [boundary_condition],
                          num_domain=domain_points, num_boundary=boundary_points, solution = func, num_test=test_points)

  net = dde.maps.FNN([1] + hidden_layers * [hidden_units] + [1], "tanh", "Glorot normal")

  model = dde.Model(data, net)

  model_name = 'Neural_Networks/nu_{}/Poisson_Equation_Source_Model_nu_{}'.format(nus[i], nus[i])

  start = time.time()

  model.compile("adam", lr=1e-3)
  history, train_state = model.train(epochs=number_of_epochs, model_save_path=model_name)

  end = time.time()
  length = end - start

  X = np.linspace(x_min, x_max, test_points).reshape(-1, 1)
  u_pred = model.predict(X)
  u_exact = func(X)
  f = model.predict(X, operator=pde)

  figure_name = 'Predictions/Predicted_solution_nu_{}'.format(nu)
  plt.plot(X, u_exact, color = 'blue', label = 'exact solution')
  plt.plot(X, u_pred, color = 'red', linestyle ='--', label = 'predicted solution')
  plt.xlabel(r'location $x$')
  plt.ylabel(r'$u$')
  plt.legend(loc="upper left")
  plt.tight_layout()
  plt.savefig(figure_name, dpi = 600)

  residual = np.mean(np.absolute(f))
  l2_difference = dde.metrics.l2_relative_error(u_exact, u_pred)