print(int(100 * n / N), "%")
    for m in range(1,len(x)-1):
        s_next = s(u,m,n)
        s_next_p1 = s(u, m+1, n)
        f_next = f_u(u,m)
        f_next_p1 = f_u(u,m+1)

        u_half[0,m] = ((u[0,m] + u[0,m+1])/2) -(k/(2*h))*(f_next_p1[0]-f_next[0])+((k/2)*(s_next[0]+s_next_p1[0]))
        u_half[1,m] = ((u[1,m] + u[1,m+1])/2) -(k/(2*h))*(f_next_p1[1]-f_next[1])+((k/2)*(s_next[1]+s_next_p1[1]))
        f_half_p1 = f_u(u_half,m)
        f_half_m1 = f_u(u_half,m-1)
        s_half_p1 = s(u_half, m, n)
        s_half_m1 = s(u_half, m-1, n)
        u_next[0,m]= u[0,m] -(k/(2*h))*(f_half_p1[0]-f_half_m1[0])+((k/2)*(s_half_p1[0]+s_half_m1[0]))
        u_next[1,m] = u[1,m] -(k/(2*h))*(f_half_p1[1]-f_half_m1[1])+((k/2)*(s_half_p1[1]+s_half_m1[1]))

    u_next[0,0] = u_next[0,1] -(u_next[0,2]-u_next[0,1])
    u_next[1,0] = u_next[1,1] - (u_next[1,2]-u_next[1,1])
    u_next[0, len(x)] = u_next[0, len(x)-1]+(u_next[0,len(x)-1]-u_next[0, len(x)-2])
    u_next[1, len(x)] = u_next[1, len(x) - 1] + (u_next[1, len(x) - 1] - u_next[1, len(x) - 2])
    u = u_next



if __name__ == "__main__":
    rw.write_data(u, "Reference solution, Lax-Wendroff, x-convergence.txt") # Writing the reference solution to file
    a = rw.read_data("Reference solution, Lax-Wendroff, x-convergence.txt")
    print(a)


Exemplo n.º 2
0
        u_half[0, m] = ((u[0, m] + u[0, m + 1]) /
                        2) - (k / (2 * h)) * (f_next_p1[0] - f_next[0]) + (
                            (k / 2) * (s_next[0] + s_next_p1[0]))
        u_half[1, m] = ((u[1, m] + u[1, m + 1]) /
                        2) - (k / (2 * h)) * (f_next_p1[1] - f_next[1]) + (
                            (k / 2) * (s_next[1] + s_next_p1[1]))
        f_half_p1 = f_u(u_half, m)  #Tror ikke det skal være m+1
        f_half_m1 = f_u(u_half, m - 1)
        s_half_p1 = s(u_half, m, n)
        s_half_m1 = s(u_half, m - 1, n)
        u_next[0,
               m] = u[0, m] - (k / (2 * h)) * (f_half_p1[0] - f_half_m1[0]) + (
                   (k / 2) * (s_half_p1[0] + s_half_m1[0]))
        u_next[1,
               m] = u[1, m] - (k / (2 * h)) * (f_half_p1[1] - f_half_m1[1]) + (
                   (k / 2) * (s_half_p1[1] + s_half_m1[1]))

        #print(((u[0, m - 1] + u[0, m + 1]) / 2), (k / (2 * h)) * (f_next_p1[0] - f_next_m1[0]), (k * s_next[0]))
        #print(((u[1,m-1]+ u[1,m+1])/2),(k/(2*h))*(f_next_p1[1]-f_next_m1[1]),(k*s_next[1]))
    u_next[0, 0] = rho_up
    u_next[1, 0] = initial_velocity
    u_next[:, len(x)] = u_next[:, len(x) - 1]
    u = u_next
    #u[:, len(x)] = u[:, len(x) - 1]
    #u[:, 0] = u[:, 1]
readwrite.write_data(u, "data.txt")
plt.plot(x, u[0][:-1])
plt.ylabel("Tetthet av biler")
plt.xlabel("x")
plt.show()
Exemplo n.º 3
0
    f = f_u(u, range(0, len(x) + 1))  # length: L/h
    s_next = s(u, range(0, len(x) - 1), n)  # length: L/h
    #print("length of x:",len(x))
    #print("length of u:",len(u[0]))
    for m in range(1, len(x) - 1):
        u_next[0, m] = (
            (u[0, m - 1] + u[0, m + 1]) /
            2) - (k /
                  (2 * h)) * (f[0, m + 1] - f[0, m - 1]) + (k * s_next[0, m])
        u_next[1, m] = ((u[1, m - 1] + u[1, m + 1]) / 2) - (k / (2 * h)) * (
            f[1, m + 1] - f[1, m - 1]) + (k * s_next[1, m]) + (
                (k / (h**2)) * (u[1, m + 1] - 2 * u[1, m] + u[1, m - 1]))
    u_next[0, 0] = rho_up
    u_next[1, 0] = initial_velocity
    u_next[:, len(x)] = u_next[:, len(x) - 1]
    u = u_next

    #print(n)

    if (n % (N / 10) == 0):
        plt.plot(x, u[0][:-1])
        plt.show()

if __name__ == "__main__":
    print("hei")
    rw.write_data(u, "u_lax_friedrich_x3.txt")
    a = rw.read_data("u_lax_friedrich_x3.txt")
    print(a)

    #plt.plot(x,u[0][:-1])
    #plt.show()
u[0, :] = rho_up
u[1, :] = initial_velocity
u_next[0, :] = rho_up
u_next[1, :] = initial_velocity

for n in range(N):
    for m in range(1, len(x) - 1):
        s_next = s(u, m, n)
        fm = f_u(u, m - 1)
        fp = f_u(u, m + 1)
        u_next[0, m] = ((u[0, m - 1] + u[0, m + 1]) /
                        2) - (k / (2 * h)) * (fp[0] - fm[0]) + (k * s_next[0])
        u_next[1, m] = (
            (u[1, m - 1] + u[1, m + 1]) /
            2) - (k / (2 * h)) * (fp[1] - fm[1]) + (k * s_next[1]) + (
                (k / (h**2)) *
                (u[1, m + 1] - 2 * u[1, m] + u[1, m - 1])) * (mu / u[0, m])
    u_next[0, 0] = rho_up
    u_next[1, 0] = initial_velocity
    u_next[0, len(x)] = u_next[0, len(x) - 1] + (u_next[0, len(x) - 1] -
                                                 u_next[0, len(x) - 2])
    u_next[1, len(x)] = u_next[1, len(x) - 1] + (u_next[1, len(x) - 1] -
                                                 u_next[1, len(x) - 2])
    u = u_next

if __name__ == "__main__":
    rw.write_data(u, "Reference solution, Lax-Friedrich, x-convergence.txt"
                  )  # Writing the reference solution to file
    a = rw.read_data("Reference solution, Lax-Friedrich, x-convergence.txt")
    print(a)
Exemplo n.º 5
0
import matplotlib.pyplot as plt
from readwrite import read_data, write_data
from sklearn.metrics import plot_confusion_matrix

train_images, train_labels, test_images, validation_images, validation_labels = read_data(
)
classifier = MLPClassifier(hidden_layer_sizes=(100, ),
                           activation='relu',
                           solver='adam',
                           alpha=0.0001,
                           batch_size='auto',
                           learning_rate='constant',
                           power_t=0.5,
                           max_iter=10,
                           shuffle=True,
                           random_state=None,
                           tol=0.0001,
                           momentum=0.9,
                           early_stopping=False,
                           validation_fraction=0.1,
                           n_iter_no_change=10)

classifier.fit(train_images, train_labels)

plot_confusion_matrix(classifier, validation_images, validation_labels)
plt.show()
plt.savefig('mlp.png')

prediction = classifier.predict(test_images)
write_data(prediction)
Exemplo n.º 6
0
initial_velocity = V_ro(rho_up)
u[0, :] = rho_up
u[1, :] = initial_velocity
u_next[0, :] = rho_up
u_next[1, :] = initial_velocity
for n in range(N):

    f = f_u(u, range(1, len(x) + 1))
    s_next = s(u, range(1, len(x) + 1), n)

    for m in range(1, len(x) - 1):
        #s_next = s(u,m,n)
        u_next[0, m] = (
            (u[0, m - 1] + u[0, m + 1]) /
            2) - (k /
                  (2 * h)) * (f[0, m + 1] - f[0, m - 1]) + (k * s_next[0, m])
        u_next[1, m] = ((u[1, m - 1] + u[1, m + 1]) / 2) - (k / (2 * h)) * (
            f[1, m + 1] - f[1, m - 1]) + (k * s_next[1, m]) + (
                (k / (h**2)) *
                (u[1, m + 1] - 2 * u[1, m] + u[1, m - 1])) * (mu / u[0, m])
    u_next[0, 0] = rho_up  #u_next[0,1] -(u_next[0,2]-u_next[0,1])
    u_next[1, 0] = initial_velocity  #u_next[1,1] - (u_next[1,2]-u_next[1,1])
    u_next[0, len(x)] = u_next[0, len(x) - 1] + (u_next[0, len(x) - 1] -
                                                 u_next[0, len(x) - 2])
    u_next[1, len(x)] = u_next[1, len(x) - 1] + (u_next[1, len(x) - 1] -
                                                 u_next[1, len(x) - 2])
    u = u_next

rw.write_data(u, "L_F_100__150_0.00001_100000")
plt.plot(x, u[0][:-1])
plt.show()
Exemplo n.º 7
0
        f_next_p1 = f_u(u,m+1) #m+1

        u_half[0,m] = ((u[0,m] + u[0,m+1])/2) -(k/(2*h))*(f_next_p1[0]-f_next[0])+((k/2)*(s_next[0]+s_next_p1[0]))
        u_half[1,m] = ((u[1,m] + u[1,m+1])/2) -(k/(2*h))*(f_next_p1[1]-f_next[1])+((k/2)*(s_next[1]+s_next_p1[1]))
        f_half_p1 = f_u(u_half,m)
        f_half_m1 = f_u(u_half,m-1)
        s_half_p1 = s(u_half, m, n)
        s_half_m1 = s(u_half, m-1, n)
        u_next[0,m]= u[0,m] -(k/(2*h))*(f_half_p1[0]-f_half_m1[0])+((k/2)*(s_half_p1[0]+s_half_m1[0]))
        u_next[1,m] = u[1,m] -(k/(2*h))*(f_half_p1[1]-f_half_m1[1])+((k/2)*(s_half_p1[1]+s_half_m1[1]))

        #print(((u[0, m - 1] + u[0, m + 1]) / 2), (k / (2 * h)) * (f_next_p1[0] - f_next_m1[0]), (k * s_next[0]))
        #print(((u[1,m-1]+ u[1,m+1])/2),(k/(2*h))*(f_next_p1[1]-f_next_m1[1]),(k*s_next[1]))
    u_next[0,0] = u_next[0,1] -(u_next[0,2]-u_next[0,1])
    u_next[1,0] = u_next[1,1] - (u_next[1,2]-u_next[1,1])
    u_next[0, len(x)] = u_next[0, len(x)-1]+(u_next[0,len(x)-1]-u_next[0, len(x)-2])
    u_next[1, len(x)] = u_next[1, len(x) - 1] + (u_next[1, len(x) - 1] - u_next[1, len(x) - 2])
    u = u_next

    #if (n % (N / 10) == 0):
    #    plt.plot(x, u[0][:-1])
    #    plt.show()

if __name__ == "__main__":
    rw.write_data(u, "u_lax_wendroff_x2.txt")
    a = rw.read_data("u_lax_wendroff_x2.txt")
    print(a)

    plt.plot(x,ref_sol[0])
    plt.show()
    u[1, :] = initial_velocity
    u_next[0, :] = rho_up
    u_next[1, :] = initial_velocity
    for n in range(N):
        f = f_u(u, range(1, len(x) + 1))
        s_next = s(u, range(1, len(x) + 1), n)
        for m in range(1, len(x) - 1):
            u_next[0, m] = ((u[0, m - 1] + u[0, m + 1]) / 2) - (
                k / (2 * h)) * (f[0, m + 1] - f[0, m - 1]) + (k * s_next[0, m])
            u_next[1, m] = ((u[1, m - 1] + u[1, m + 1]) / 2) - (
                k /
                (2 * h)) * (f[1, m + 1] - f[1, m - 1]) + (k * s_next[1, m]) + (
                    (k / (h**2)) *
                    (u[1, m + 1] - 2 * u[1, m] + u[1, m - 1])) * mu / (u[0, m])
        u_next[0, 0] = rho_up
        u_next[1, 0] = initial_velocity
        u_next[:, len(x)] = u_next[:, len(x) - 1]
        u = u_next

    plt.plot(x, u[0][:-1])
    plt.show()
    rw.write_data(u, "ut1.txt")
    a = rw.read_data("ut1.txt")
    error[index] = np.linalg.norm(np.subtract(ref[0, :], u[0, :]),
                                  2)**np.sqrt(h)
    k_vec[index] = k
    index += 1
plt.figure()
plt.loglog(k_vec, error)
plt.show()