Example #1
0
    def plot_error_time(self, start, end, x_end, t_end, t_dir=True, reference_order=1, ref_ana=True, expo=10):
        points = end - start
        errors = np.zeros(points)
        step_vector = np.zeros(points)
        reference_initial_electric = self.solutions.plot_analytical(0, x_end, 2 ** start, 0, electric=True,
                                                                    get_field=True)
        reference_initial_magnetic = self.solutions.plot_analytical(0, x_end, 2 ** start, 0, electric=False,
                                                                    get_field=True)
        reference_initial = reference_initial_electric, reference_initial_magnetic
        reference_field_full = np.zeros(2 ** expo)
        for p in range(2 ** expo):
            dummy = yee_1d(2 ** start, 2 ** expo, t0=p * t_end / 2 ** expo, run_time_steps=1, t_end=t_end,
                           x_end=x_end, initial_fields=reference_initial, boundary="periodic")
            reference_field_full[p] = dummy[0][2 ** start - 1]
            reference_initial = dummy
        for i in range(points):
            n = 2 ** (start + i)
            m = 2 ** start
            k = t_end / n
            end_field = np.zeros(n)
            initial_field_electric = np.copy(self.solutions.plot_analytical(0, x_end, m, 0, electric=True,
                                                                            get_field=True))
            initial_field_magnetic = np.copy(self.solutions.plot_analytical(0, x_end, m, 0, electric=False,
                                                                            get_field=True))
            fields_initial = initial_field_electric, initial_field_magnetic
            if ref_ana:
                reference_field = self.solutions.electric_along_time(0, t_end, n, x_end)
            else:
                reference_field = reference_field_full[::2 ** expo / n]
            for j in range(n):
                field_numerical = yee_1d(m, n, t0=j * t_end / n, run_time_steps=1, t_end=t_end,
                                         x_end=x_end, initial_fields=fields_initial, boundary="periodic")
                end_field[j] = field_numerical[0][m - 1]
                fields_initial = field_numerical
            step_vector[i] = k
            errors[i] = np.sqrt(k) * np.linalg.norm(end_field - reference_field)
            #plt.plot(np.linspace(0, t_end, n), end_field)
            #plt.plot(np.linspace(0, t_end, n), reference_field)
            #plt.show()

        plt.loglog(step_vector, errors, "-o")
        if reference_order is not None:
            plt.loglog(step_vector, 10e10 * step_vector ** reference_order, linestyle=":")
            plt.loglog(step_vector, 1 * step_vector ** 1, linestyle=":")
            plt.legend(('Error', 'Order 2', 'Order 1'), loc="best")
        print(np.log(errors[points - 1] / errors[0]) / np.log(step_vector[points - 1] / step_vector[0]))
        plt.ylabel('Error, 2 - norm')
        plt.xlabel('Temporal step size')
        plt.show()
def generate_time_domain_array_pulse(boundary):
    Ez = np.zeros((M, N + 1))
    Ez_t = np.zeros(M)
    By_t = np.zeros(M)

    for t in range(N):
        Ez_t, By_t = yee_1d(M,
                            N,
                            t0=t,
                            run_time_steps=1,
                            initial_fields=(Ez_t, By_t),
                            boundary=boundary,
                            pulse=True)
        Ez[:, t + 1] = Ez_t
    return Ez
Example #3
0
    def plot_error_space(self, start, end, x_end, t_end, reference_order=2, x_dir=True):
        points = end - start
        errors = np.zeros(points)
        step_vector = np.zeros(points)

        for i in range(points):
            print(i)
            if x_dir:
                m = 2 ** (start + i)
                n = 2 ** end
                h = x_end / m
            else:
                m = 2 ** start
                n = 2 ** (start + i)
                h = x_end / m
                k = t_end / n
            initial_field_electric = self.solutions.plot_analytical(0, x_end, m, 0, electric=True,
                                                                    get_field=True)
            initial_field_magnetic = self.solutions.plot_analytical(0, x_end, m, t_end / (2 * n), electric=False,
                                                                    get_field=True)
            fields_initial = initial_field_electric, initial_field_magnetic
            field_analytical = self.solutions.plot_analytical(0, x_end, m, t_end, electric=True, get_field=True)
            field_numerical = yee_1d(m, n, t_end=t_end, x_end=x_end, initial_fields=fields_initial,
                                     boundary="periodic")[0]
            if x_dir:
                step_vector[i] = h
            else:
                step_vector[i] = k
            errors[i] = np.sqrt(h) * np.linalg.norm(field_numerical - field_analytical)
            # plt.plot(np.linspace(0, x_end, m), field_numerical)
            # plt.plot(np.linspace(0, x_end, m), field_analytical)
            # plt.show()

        plt.loglog(step_vector, errors, "-o")
        if reference_order is not None:
            plt.loglog(step_vector, 2 * step_vector ** reference_order, linestyle=":")
            # plt.loglog(step_vector, 0.1 * step_vector ** 1, linestyle=":")
            plt.legend(('Error', 'Order 2'), loc="best")
        print(np.log(errors[points - 1] / errors[0]) / np.log(step_vector[points - 1] / step_vector[0]))
        plt.ylabel('Error, 2 - norm')
        if x_dir:
            plt.xlabel('Stepsize [m]')
        else:
            plt.xlabel('Temporal step size [m]')
        # plt.title(function)
        plt.show()
def generate_time_domain_array_periodic(standing_wave=False):
    c = 299792458
    t_end = 1 / c
    x_end = 1

    Ez = np.zeros((M, N + 1))
    Ez_t = np.sin(2 * np.pi * np.linspace(0, x_end, M))
    if standing_wave:
        By_t = np.zeros(M)
    else:
        By_t = -np.sin(2 * np.pi * (np.linspace(0, x_end, M) + c * t_end /
                                    (2 * M)))

    Ez[:, 0] = Ez_t

    for t in range(N):
        Ez_t, By_t = yee_1d(M,
                            N,
                            t0=t,
                            run_time_steps=1,
                            initial_fields=(Ez_t, By_t),
                            boundary='periodic')
        Ez[:, t + 1] = Ez_t
    return Ez
    return np.sin(2 * np.pi * (x - c * t))


start = 3
stop = 14
N_const = 2**stop
M_const = 2**start
h_M_const = 1 / (M_const - 1)

Ez0_M_const = np.sin(2 * np.pi * np.linspace(0, x_end, M_const))
By0_M_const = -np.sin(2 * np.pi * (np.linspace(0, x_end, M_const) + c * t_end /
                                   (2 * M_const)))
ref_num_M_const = yee_1d(M_const,
                         2**(stop + 1),
                         t_end=t_end,
                         x_end=x_end,
                         initial_fields=(np.copy(Ez0_M_const),
                                         np.copy(By0_M_const)),
                         boundary='periodic')[0]

error_N_const = np.zeros(stop - start + 1)
error_M_const = np.zeros(stop - start + 1)
h = 1 / (np.logspace(start, stop, stop - start + 1, base=2) - 1)  # unit x_end
k = 1 / np.logspace(start, stop, stop - start + 1, base=2)  # unit t_end
x_M_const = np.linspace(0, x_end, M_const)

for i in range(start, stop + 1):
    # Constant N
    M = 2**i
    Ez_N_const = np.sin(2 * np.pi * np.linspace(0, x_end, M))
    By_N_const = -np.sin(2 * np.pi * (np.linspace(0, x_end, M) + c * t_end /
Example #6
0
OneDimErrors.plot_error_space(4, 10, 1, 1 / (1 * speed_of_light))
OneDimErrors.plot_error_time(4,
                             10,
                             1,
                             1 / (1 * speed_of_light),
                             ref_ana=False,
                             expo=12,
                             reference_order=2)

# Running other scripts to produce plots as in the report

import plot_convergence_1d
import plot_convergence_3d

# Plotting the numerical solution to a one-dimensional problem along with the analytical solution.

plt.plot(
    np.linspace(0, 1, m),
    yee_1d(m,
           n,
           t_end=1 / (1 * speed_of_light),
           x_end=1,
           initial_fields=init_fields)[0])
OneDimSolutions.plot_analytical(0, 1, m, 1 / (1 * speed_of_light))
plt.show()

#The following script saves the data generated in Python in a format that can be read and plotted in Matlab.
#Running the script plot_surface.m in Matlab after running this script plots the data.

import plot_3d