Esempio n. 1
0
def check_error(params):
    error = np.zeros(N.size)

    for i in range(N.size):
        domain.N_p1 = int(N[i])
        domain.N_p2 = int(N[i])
        # Defining the physical system to be solved:
        system = physical_system(domain, boundary_conditions, params,
                                 initialize, advection_terms,
                                 collision_operator.BGK, moment_defs)

        # Declaring a linear system object which will evolve the defined physical system:
        nls = nonlinear_solver(system)
        N_g = nls.N_ghost_q

        # Time parameters:
        dt = 0.01 * 32 / nls.N_p1
        t_final = 0.2

        time_array = np.arange(dt, t_final + dt, dt)

        f_reference = nls.f

        for time_index, t0 in enumerate(time_array):
            nls.strang_timestep(dt)

        error[i] = af.mean(af.abs(nls.f - f_reference))

    return (error)
Esempio n. 2
0
def run_cases(q_dim, p_dim, charge_electron, tau):

    params.charge[0] = charge_electron
    params.tau = tau

    # Running the setup for all resolutions:
    for i in range(N.size):
        af.device_gc()
        domain.N_q1 = int(N[i])

        if (q_dim == 2):
            domain.N_q2 = int(N[i])
            params.k_q2 = 4 * np.pi

        if (p_dim >= 2):

            domain.N_p2 = 32
            domain.p2_start = -10
            domain.p2_end = 10

        if (p_dim == 3):

            domain.N_p3 = 32
            domain.p3_start = -10
            domain.p3_end = 10

        if (charge_electron != 0):
            domain.N_p1 = int(N[i])

            if (p_dim >= 2):
                domain.N_p2 = int(N[i])

            if (p_dim == 3):
                domain.N_p3 = int(N[i])

        params.p_dim = p_dim

        # Defining the physical system to be solved:
        system = physical_system(domain, boundary_conditions, params,
                                 initialize, advection_terms,
                                 collision_operator.BGK, moments)

        nls = nonlinear_solver(system)

        # Timestep as set by the CFL condition:
        dt = params.N_cfl * min(nls.dq1, nls.dq2) \
                          / max(domain.p1_end, domain.p2_end, domain.p3_end)

        time_array = np.arange(dt, params.t_final + dt, dt)
        # Checking that time array doesn't cross final time:
        if (time_array[-1] > params.t_final):
            time_array = np.delete(time_array, -1)

        for time_index, t0 in enumerate(time_array):
            nls.strang_timestep(dt)
            if (time_index % 25 == 0):
                nls.f = lowpass_filter(nls.f)

        nls.dump_distribution_function('dump_files/nlsf_' + str(N[i]))
Esempio n. 3
0
def check_error(params):
    error = np.zeros(N.size)

    for i in range(N.size):
        domain.N_p1 = int(N[i])
        domain.N_p2 = int(N[i])
        domain.N_p3 = int(N[i])

        # Defining the physical system to be solved:
        system = physical_system(domain, boundary_conditions, params,
                                 initialize, advection_terms,
                                 collision_operator.BGK, moments)

        # Declaring a linear system object which will evolve the defined physical system:
        nls = nonlinear_solver(system)

        # Time parameters:
        dt = 0.0001 * 32 / nls.N_p1
        t_final = 0.2

        time_array = np.arange(dt, t_final + dt, dt)

        if (time_array[-1] > t_final):
            time_array = np.delete(time_array, -1)

        # Finding final resting point of the blob:
        E1 = nls.fields_solver.cell_centered_EM_fields[0]
        E2 = nls.fields_solver.cell_centered_EM_fields[1]
        E3 = nls.fields_solver.cell_centered_EM_fields[2]

        B1 = nls.fields_solver.cell_centered_EM_fields[3]
        B2 = nls.fields_solver.cell_centered_EM_fields[4]
        B3 = nls.fields_solver.cell_centered_EM_fields[5]

        sol = odeint(dp_dt,
                     np.array([0, 0, 0]),
                     time_array,
                     args=(af.mean(E1), af.mean(E2), af.mean(E3), af.mean(B1),
                           af.mean(B2), af.mean(B3), af.sum(params.charge[0]),
                           af.sum(params.mass[0])),
                     atol=1e-12,
                     rtol=1e-12)

        f_reference = af.broadcast(initialize.initialize_f, nls.q1_center,
                                   nls.q2_center, nls.p1_center - sol[-1, 0],
                                   nls.p2_center - sol[-1, 1],
                                   nls.p3_center - sol[-1, 2], params)

        for time_index, t0 in enumerate(time_array):
            nls.strang_timestep(dt)

        error[i] = af.mean(af.abs(nls.f - f_reference))

    return (error)
Esempio n. 4
0
def check_error(params):
    error = np.zeros(N.size)

    for i in range(N.size):
        domain.N_p1 = int(N[i])
        domain.N_p2 = int(N[i])
        # Defining the physical system to be solved:
        system = physical_system(domain, boundary_conditions, params,
                                 initialize, advection_terms,
                                 collision_operator.BGK, moment_defs)

        # Declaring a linear system object which will evolve the defined physical system:
        nls = nonlinear_solver(system)
        N_g = nls.N_ghost_q

        # Time parameters:
        dt = 0.01 * 32 / nls.N_p1
        t_final = 0.2

        time_array = np.arange(dt, t_final + dt, dt)

        # Since only the p = 1 mode is excited:

        E1 = nls.cell_centered_EM_fields_at_n[0]
        E2 = nls.cell_centered_EM_fields_at_n[1]
        E3 = nls.cell_centered_EM_fields_at_n[2]

        B1 = nls.cell_centered_EM_fields_at_n[3]
        B2 = nls.cell_centered_EM_fields_at_n[4]
        B3 = nls.cell_centered_EM_fields_at_n[5]

        (A_p1, A_p2,
         A_p3) = af.broadcast(nls._A_p, nls.q1_center, nls.q2_center,
                              nls.p1_center, nls.p2_center, nls.p3_center, E1,
                              E2, E3, B1, B2, B3, nls.physical_system.params)

        f_analytic = af.broadcast(initialize.initialize_f, nls.q1_center,
                                  nls.q2_center,
                                  addition(nls.p1_center, -A_p1 * t_final),
                                  addition(nls.p2_center, -A_p2 * t_final),
                                  nls.p3_center, nls.physical_system.params)

        for time_index, t0 in enumerate(time_array):
            nls.strang_timestep(dt)

        error[i] = af.mean(af.abs(nls.f - f_analytic))

    return (error)
def test_check_maxwells_constraints():

    params = params_check_maxwells_contraints
    system = physical_system(domain,
                             boundary_conditions,
                             params,
                             initialize_check_maxwells_contraints,
                             advection_terms,
                             collision_operator.BGK,
                             moments
                            )

    dq1 = (domain.q1_end - domain.q1_start) / domain.N_q1
    dq2 = (domain.q2_end - domain.q2_start) / domain.N_q2
    
    q1, q2 = calculate_q_center(domain.q1_start, domain.q2_start,
                                domain.N_q1, domain.N_q2, domain.N_ghost,
                                dq1, dq2
                               )
    
    rho = (  params.pert_real * af.cos(  params.k_q1 * q1 
                                       + params.k_q2 * q2
                                      )
           - params.pert_imag * af.sin(  params.k_q1 * q1 
                                       + params.k_q2 * q2
                                      )
          )

    obj = fields_solver(system, 
                        rho, 
                        False
                       )

    # Checking for ∇.E = rho / epsilon
    rho_left_bot = 0.25 * (  rho 
                           + af.shift(rho, 0, 0, 0, 1)
                           + af.shift(rho, 0, 0, 1, 0)
                           + af.shift(rho, 0, 0, 1, 1)
                          ) 

    N_g = obj.N_g
    assert(af.mean(af.abs(obj.compute_divB()[:, :, N_g:-N_g, N_g:-N_g]))<1e-14)

    divE  = obj.compute_divE()
    rho_b = af.mean(rho_left_bot) # background

    assert(af.mean(af.abs(divE - rho_left_bot + rho_b)[:, :, N_g:-N_g, N_g:-N_g])<1e-6)
Esempio n. 6
0
def check_error(params):

    error = np.zeros(N.size)

    for i in range(N.size):
        af.device_gc()

        domain.N_q1 = int(N[i])
        domain.N_p1 = int(N[i])
 
        # Defining the physical system to be solved:
        system = physical_system(domain,
                                 boundary_conditions,
                                 params,
                                 initialize,
                                 advection_terms,
                                 collision_operator.BGK,
                                 moments
                                )

        # Declaring a linear system object which will evolve the defined physical system:
        nls = nonlinear_solver(system)
        N_g = nls.N_ghost

        # Time parameters:
        dt      = 0.001 * 32/nls.N_q1
        t_final = 0.1

        time_array  = np.arange(dt, t_final + dt, dt)
        f_reference = af.broadcast(initialize.initialize_f,
                                   af.broadcast(lambda a, b:a+b, nls.q1_center, - nls.p1_center * t_final), 
                                   nls.q2_center, nls.p1_center, nls.p2_center, nls.p3_center, params
                                  )

        for time_index, t0 in enumerate(time_array):
            nls.strang_timestep(dt)

        error[i] = af.mean(af.abs(  nls.f[:, :, N_g:-N_g, N_g:-N_g] 
                                  - f_reference[:, :, N_g:-N_g, N_g:-N_g]
                                 )
                          )

    return(error)
Esempio n. 7
0
    def __init__(self, N, initialize, params):

        domain.N_q1 = int(N)
        domain.N_q2 = int(N)

        N_g = domain.N_ghost
        system = physical_system(domain, boundary_conditions_mirror, params,
                                 initialize, advection_terms,
                                 collision_operator.BGK, moments)

        self.fields_solver = fields_solver(
            system,
            af.constant(0,
                        1,
                        1,
                        domain.N_q1 + 2 * N_g,
                        domain.N_q2 + 2 * N_g,
                        dtype=af.Dtype.f64))

        return
    def __init__(self, N):

        domain.N_q1 = int(N)
        domain.N_q2 = int(N)

        system = physical_system(domain,
                                 boundary_conditions,
                                 params_check_maxwells_contraints,
                                 initialize_check_maxwells_contraints,
                                 advection_terms,
                                 collision_operator.BGK,
                                 moments
                                )

        self.fields_solver = fields_solver(system, 
                                           None, 
                                           False
                                          )

        return
Esempio n. 9
0
def check_error(params):
    error = np.zeros(N.size)

    for i in range(N.size):
        domain.N_p1 = int(N[i])
        # Defining the physical system to be solved:
        system = physical_system(domain, boundary_conditions, params,
                                 initialize, advection_terms,
                                 collision_operator.BGK, moments)

        # Declaring a linear system object which will evolve the defined physical system:
        nls = nonlinear_solver(system)

        # Time parameters:
        dt = 0.01 * 32 / nls.N_p1
        t_final = 0.2

        time_array = np.arange(dt, t_final + dt, dt)

        (A_p1, A_p2,
         A_p3) = af.broadcast(nls._A_p, nls.f, 0, nls.q1_center, nls.q2_center,
                              nls.p1_center, nls.p2_center, nls.p3_center,
                              nls.fields_solver, nls.physical_system.params)

        f_analytic = af.broadcast(initialize.initialize_f, nls.q1_center,
                                  nls.q2_center,
                                  add(nls.p1_center, -A_p1 * t_final),
                                  add(nls.p2_center, -A_p2 * t_final),
                                  nls.p3_center, nls.physical_system.params)

        for time_index, t0 in enumerate(time_array):
            nls.strang_timestep(dt)

        error[i] = af.mean(af.abs(nls.f - f_analytic))

    return (error)
Esempio n. 10
0
from bolt.lib.physical_system import physical_system
from bolt.lib.nonlinear.nonlinear_solver import nonlinear_solver
from bolt.lib.linear.linear_solver import linear_solver

import input_files.domain as domain
import input_files.boundary_conditions as boundary_conditions
import input_files.params as params
import input_files.initialize as initialize

import bolt.src.nonrelativistic_boltzmann.advection_terms as advection_terms
import bolt.src.nonrelativistic_boltzmann.collision_operator as collision_operator
import bolt.src.nonrelativistic_boltzmann.moments as moments

# Defining the physical system to be solved:
system = physical_system(domain, boundary_conditions, params, initialize,
                         advection_terms, collision_operator.BGK, moments)

N_g_q = system.N_ghost_q

nls = nonlinear_solver(system)

# Timestep as set by the CFL condition:
dt = params.N_cfl * min(nls.dq1, nls.dq2) \
                  / max(domain.p1_end, domain.p2_end, domain.p3_end)

time_array = np.arange(0, params.t_final + dt, dt)

# Checking that time array doesn't cross final time:
if (time_array[-1] > params.t_final):
    time_array = np.delete(time_array, -1)
Esempio n. 11
0
# Horizontal
import domain_1
import boundary_conditions_1
import params_1
import initialize_1

# Vertical
import domain_2
import boundary_conditions_2
import params_2
import initialize_2

# Defining the physical system to be solved:
system_1 = physical_system(domain_1, boundary_conditions_1, params_1,
                           initialize_1, advection_terms,
                           collision_operator.RTA, moment_defs)

system_2 = physical_system(domain_2, boundary_conditions_2, params_2,
                           initialize_2, advection_terms,
                           collision_operator.RTA, moment_defs)
# Time parameters:
dt_1 = params_1.dt
t_final_1 = params_1.t_final
params_1.current_time = time_elapsed_1 = 0.0
params_1.time_step = time_step_1 = 0

dt_2 = params_2.dt
t_final_2 = params_2.t_final
params_2.current_time = time_elapsed_2 = 0.0
params_2.time_step = time_step_2 = 0
Esempio n. 12
0
def test_shear_y():

    t = np.random.rand(1)[0]
    N = 2**np.arange(5, 10)
    error = np.zeros(N.size)

    for i in range(N.size):
        domain.N_q1 = int(N[i])
        domain.N_q2 = int(N[i])

        # Defining the physical system to be solved:
        system = physical_system(domain, boundary_conditions_y, params,
                                 initialize_y, advection_terms,
                                 collision_operator.BGK, moment_defs)

        L_q1 = domain.q1_end - domain.q1_start
        L_q2 = domain.q2_end - domain.q2_start

        nls = nonlinear_solver(system)
        N_g = nls.N_ghost_q

        # For left:
        f_reference_bot = af.broadcast(
            initialize_y.initialize_f,
            af.select(
                nls.q1_center - t < domain.q1_start,  # Periodic domain
                nls.q1_center - t + L_q1,
                nls.q1_center - t),
            nls.q2_center,
            nls.p1_center,
            nls.p2_center,
            nls.p3_center,
            params)[:, N_g:-N_g, -2 * N_g:-N_g]

        # For right:
        f_reference_top = af.broadcast(
            initialize_y.initialize_f,
            af.select(nls.q1_center + t > domain.q1_end,
                      nls.q1_center + t - L_q1, nls.q1_center + t),
            nls.q2_center, nls.p1_center, nls.p2_center, nls.p3_center,
            params)[:, N_g:-N_g, N_g:2 * N_g]

        nls.time_elapsed = t
        nls._communicate_f()
        nls._apply_bcs_f()

        error[i] =   af.mean(af.abs(nls.f[:, N_g:-N_g, :N_g] - f_reference_bot)) \
                   + af.mean(af.abs(nls.f[:, N_g:-N_g, -N_g:] - f_reference_top))

    pl.loglog(N, error, '-o', label='Numerical')
    pl.loglog(N,
              error[0] * 32**3 / N**3,
              '--',
              color='black',
              label=r'$O(N^{-3})$')
    pl.xlabel(r'$N$')
    pl.ylabel('Error')
    pl.legend()
    pl.savefig('plot2.png')

    poly = np.polyfit(np.log10(N), np.log10(error), 1)
    assert (abs(poly[0] + 3) < 0.3)
Esempio n. 13
0
import h5py

from bolt.lib.physical_system import physical_system
from bolt.lib.nonlinear.nonlinear_solver import nonlinear_solver

import domain
import boundary_conditions
import params
import initialize

import bolt.src.coordinate_transformation.advection_terms as advection_terms
import bolt.src.coordinate_transformation.source_term as source_term
import bolt.src.coordinate_transformation.moments as moments

# Defining the physical system to be solved:
system = physical_system(domain, boundary_conditions, params, initialize,
                         advection_terms, source_term.source_term, moments)

# Declaring a linear system object which will evolve the defined physical system:
nls = nonlinear_solver(system)
N_g = nls.N_ghost

# Time parameters:
dt = 0.00025
t_final = 1.0

time_array = np.arange(dt, t_final + dt, dt)

n_nls = nls.compute_moments('density')

f_initial = nls.f
nls.dump_distribution_function('dump/0000')
Esempio n. 14
0
def check_error(params):
    error = np.zeros(N.size)

    for i in range(N.size):
        domain.N_p1 = int(N[i])
        domain.N_p2 = int(N[i])
        # Defining the physical system to be solved:
        system = physical_system(domain,
                                 boundary_conditions,
                                 params,
                                 initialize,
                                 advection_terms,
                                 collision_operator.BGK,
                                 moments
                                )

        nls = nonlinear_solver(system)

        # Time parameters:
        dt = 0.001 * 32/nls.N_p1
        
        # First, we check when the blob returns to (0, 0)
        E1 = nls.fields_solver.cell_centered_EM_fields[0]
        E2 = nls.fields_solver.cell_centered_EM_fields[1]
        B3 = nls.fields_solver.cell_centered_EM_fields[5]

        sol = odeint(dp_dt, np.array([0, 0]), time_array_odeint,
                     args = (af.mean(E1), af.mean(E2), af.mean(B3), 
                             af.sum(params.charge[0]),
                             af.sum(params.mass[0])
                            ),
                     atol = 1e-12, rtol = 1e-12
                    ) 

        dist_from_origin = abs(sol[:, 0]) + abs(sol[:, 1])
        
        # The time when the distance is minimum apart from the start is the time
        # when the blob returns back to the center:
        # However, this is an approximate solution. To get a more accurate solution, 
        # we provide this guess to our root finder scipy.optimize.root
        t_final_approx = time_array_odeint[np.argmin(dist_from_origin[1:])]
        t_final        = root(residual, t_final_approx, 
                              args = (af.mean(E1), af.mean(E2), af.mean(B3), 
                                      params.charge[0],
                                      params.mass[0]
                                     ),
                              method = 'lm', tol = 1e-12
                             ).x

        time_array  = np.arange(dt, float("{0:.3f}".format(t_final[0])) + dt, dt)

        if(time_array[-1]>t_final):
            time_array = np.delete(time_array, -1)
    
        f_reference = nls.f

        for time_index, t0 in enumerate(time_array):
            nls.strang_timestep(dt)

        error[i] = af.mean(af.abs(  nls.f
                                  - f_reference
                                 )
                          )

    return(error)
Esempio n. 15
0
def run_cases(q_dim, p_dim, charge_electron, tau):

    params.charge[0] = charge_electron
    params.tau       = tau

    # Running the setup for all resolutions:
    for i in range(N.size):
        af.device_gc()
        domain.N_q1 = int(N[i])

        if(q_dim == 2):
            domain.N_q2 = int(N[i])
            params.k_q2 = 4 * np.pi

        if(p_dim == 2):
         
            domain.N_p2     = 32
            domain.p2_start = -10
            domain.p2_end   = 10

        if(p_dim == 3):

            domain.N_p3     = 32
            domain.p3_start = -10
            domain.p3_end   = 10


        if(charge_electron != 0):
            domain.N_p1 = int(N[i])

            if(p_dim == 2):
                domain.N_p2 = int(N[i])

            if(p_dim == 3):
                domain.N_p3 = int(N[i])

        params.p_dim = p_dim
        dt           = 1e-3/(2**i)

        # Defining the physical system to be solved:
        system = physical_system(domain,
                                 boundary_conditions,
                                 params,
                                 initialize,
                                 advection_terms,
                                 collision_operator.BGK,
                                 moments
                                )
        
        # linearized_system = physical_system(domain,
        #                                     boundary_conditions,
        #                                     params,
        #                                     initialize,
        #                                     advection_terms,
        #                                     collision_operator.linearized_BGK,
        #                                     moments
        #                                    )

        # Declaring a linear system object which will 
        # evolve the defined physical system:
        nls = nonlinear_solver(system)
        ls  = linear_solver(system)

        time_array = np.arange(dt, t_final + dt, dt)

        for time_index, t0 in enumerate(time_array):
            nls.strang_timestep(dt)
            ls.RK4_timestep(dt)

        nls.dump_distribution_function('dump_files/nlsf_' + str(N[i]))
        ls.dump_distribution_function('dump_files/lsf_' + str(N[i]))