예제 #1
0
def run(kn=1.0, dt=0.01, nt=1000, coll="linear", scheme="Euler"):
    config = collision.utils.CollisionConfig.from_json(
        "./examples/linear_transport/configs/" + "linear" + ".json"
    )

    vmesh = collision.CartesianMesh(config)
    if coll == "linear":
        coll_op = collision.LinearCollision(config, vmesh)
    elif coll == "rbm":
        coll_op = collision.RandomBatchLinearCollision(config, vmesh)
    else:
        raise NotImplementedError(
            "Collision method {} is not implemented.".format(coll)
        )

    solver = pykinetic.BoltzmannSolver0D(collision_operator=coll_op, kn=kn)
    if "RK" in scheme:
        solver.time_integrator = "RK"
        solver.a = rkcoeff[scheme]["a"]
        solver.b = rkcoeff[scheme]["b"]
        solver.c = rkcoeff[scheme]["c"]
    else:
        solver.time_integrator = scheme
    solver.dt = dt

    domain = pykinetic.Domain([])
    state = pykinetic.State(domain, vmesh, 1)

    qinit(state, vmesh)

    sol = pykinetic.Solution(state, domain)

    output_dict = {}
    sol_frames, macro_frames, ts, l2_errs = (
        [copy.deepcopy(sol)],
        [compute_rho(sol.state, vmesh)],
        [0.0],
        l2_err(sol, vmesh),
    )
    pbar = Progbar(nt)
    for t in range(nt):
        solver.evolve_to_time(sol)
        l2_errs.append(l2_err(sol, vmesh))
        sol_frames.append([copy.deepcopy(sol), l2_err])
        macro_frames.append(compute_rho(sol.state, vmesh))
        ts.append(0.0 + (t + 1) * dt)
        pbar.update(t + 1, finalize=False)
    pbar.update(nt, finalize=True)

    output_dict["macro_frames"] = macro_frames
    output_dict["t"] = ts

    return output_dict
예제 #2
0
def run(
    kn=lambda x, y: 1.0,
    sigma_s=lambda x, y: 1.0,
    sigma_a=lambda x, y: 0.0,
    Q=lambda x, y: 0.0,
    xmin=[0.0, 0.0],
    xmax=[1.0, 1.0],
    nx=40,
    dt=0.01,
    nt=1000,
    coll="linear",
    scheme="Euler",
    BC="periodic",
    f_l=lambda vx, vy: 0.0,
    f_b=lambda vx, vy: 0.0,
    f_r=lambda vx, vy: 0.0,
    f_t=lambda vx, vy: 0.0,
    init_func=lambda vmesh, u, T, rho: 0.0,
):
    # Load config
    config = collision.utils.CollisionConfig.from_json(
        "./examples/linear_transport/configs/" + "parity_2d" + ".json")

    # Collision
    vmesh = collision.PolarMesh(config)
    # print(vmesh.centers[0], vmesh.weights)
    if coll == "linear":
        coll_op = collision.LinearCollision(config, vmesh)
    elif coll == "rbm":
        coll_op = collision.RandomBatchLinearCollision(config, vmesh)
    elif coll == "rbm_symm":
        coll_op = collision.SymmetricRBMLinearCollision(config, vmesh)
    else:
        raise NotImplementedError(
            "Collision method {} is not implemented.".format(coll))

    # x domian
    x = pykinetic.Dimension(xmin[0], xmax[0], nx, name="x")
    y = pykinetic.Dimension(xmin[1], xmax[1], nx, name="y")
    # print(x.centers_with_ghost(2))
    domain = pykinetic.Domain([x, y])

    # Riemann solver
    rp = pykinetic.riemann.parity_2D
    solver = APNeutronTransportSolver2D(
        rp,
        [coll_op],
        kn=kn(x.centers, y.centers),
        sigma_s=sigma_s(x.centers, y.centers),
        sigma_a=sigma_a(x.centers, y.centers),
        Q=Q(x.centers, y.centers),
    )
    # print(solver.kn)
    solver.order = 1
    # solver.lim_type = -1
    solver.time_integrator = scheme
    solver.dt = dt
    print("dt is {}".format(solver.dt))

    # sigma = sigma_s(x.centers, y.centers) + kn(
    #     x.centers, y.centers
    # ) ** 2 * sigma_a(x.centers, y.centers)
    # sigma_l, sigma_r = None, None
    # if isinstance(sigma, np.ndarray):
    #     sigma_l, sigma_r = sigma[0], sigma[-1]
    # else:
    #     sigma_l = sigma_r = sigma
    # kn_l, kn_r = None, None
    # if isinstance(solver.kn, np.ndarray):
    #     kn_l, kn_r = solver.kn[0], solver.kn[-1]
    # else:
    #     kn_l = kn_r = solver.kn

    # Boundary conditions
    def dirichlet_lower_BC(state, dim, t, qbc, auxbc, num_ghost):
        vx, vy = state.problem_data["v"]
        if dim.name == "x":
            for i in range(num_ghost):
                qbc[::2, i] = -qbc[::2, num_ghost + i]
                qbc[1::2, i] = (
                    2 * (-vx * 2 * qbc[::2, num_ghost] / state.grid.delta[0]) -
                    qbc[1::2, num_ghost + i])
        elif dim.name == "y":
            for i in range(num_ghost):
                qbc[::2, :, i] = -qbc[::2, :, num_ghost + i]
                qbc[1::2, :, i] = (
                    2 *
                    (-vy * 2 * qbc[::2, :, num_ghost] / state.grid.delta[1]) -
                    qbc[1::2, :, num_ghost + i])
                qbc[1, :, i] = -qbc[1, :, i]
        else:
            raise ValueError("Dim could be only x or y.")

    def dirichlet_upper_BC(state, dim, t, qbc, auxbc, num_ghost):
        vx, vy = state.problem_data["v"]
        if dim.name == "x":
            for i in range(num_ghost):
                qbc[::2, -i - 1] = -qbc[::2, -2 * num_ghost + i]
                qbc[1::2, -i - 1] = (
                    2 *
                    (vx * 2 * qbc[::2, -num_ghost - 1] / state.grid.delta[0]) -
                    qbc[1::2, -2 * num_ghost + i])
        elif dim.name == "y":
            for i in range(num_ghost):
                qbc[::2, :, -i - 1] = -qbc[::2, :, -2 * num_ghost + i]
                qbc[1::2, :,
                    -i - 1] = (2 * (vy * 2 * qbc[::2, :, -num_ghost - 1] /
                                    state.grid.delta[1]) -
                               qbc[1::2, :, -2 * num_ghost + i])
                qbc[1, :, -i - 1] = -qbc[1, :, -i - 1]
        else:
            raise ValueError("Dim could be only x or y.")

    if BC == "periodic":
        solver.all_bcs = pykinetic.BC.periodic
    elif BC == "dirichlet":
        solver.all_bcs = pykinetic.BC.custom
        solver.user_bc_lower = dirichlet_lower_BC
        solver.user_bc_upper = dirichlet_upper_BC
    else:
        raise ValueError("Given BC type is not avaliable!")

    state = pykinetic.State(domain, vmesh, 4)
    state.problem_data["v"] = vmesh.centers
    state.problem_data["phi"] = phi(solver.kn)
    state.problem_data["sqrt_phi"] = np.sqrt(phi(solver.kn))

    qinit(state, vmesh, solver.kn, init_func)
    sol = pykinetic.Solution(state, domain)

    output_dict = {}
    sol_frames, macro_frames, ts = (
        [copy.deepcopy(sol)],
        [compute_rho(sol.state, vmesh)],
        [0.0],
    )
    pbar = Progbar(nt)
    for t in range(nt):
        solver.evolve_to_time(sol)
        sol_frames.append(copy.deepcopy(sol))
        macro_frames.append(compute_rho(sol.state, vmesh))
        ts.append(0.0 + (t + 1) * dt)
        pbar.update(t + 1, finalize=False)
    pbar.update(nt, finalize=True)

    output_dict["macro_frames"] = macro_frames
    output_dict["mesh"] = state.c_centers
    output_dict["t"] = ts

    return output_dict
예제 #3
0
파일: rbm_1d.py 프로젝트: mazhengcn/kipack
def run(
    kn=1.0,
    sigma_s=lambda x: 1.0,
    sigma_a=lambda x: 0.0,
    Q=lambda x: 0.0,
    xmin=0.0,
    xmax=1.0,
    nx=40,
    dt=0.01,
    nt=1000,
    coll="linear",
    scheme="Euler",
    BC="periodic",
    f_l=lambda v: 1.0,
    f_r=lambda v: 0.0,
    init_func=lambda vmesh, u, T, rho: 0.0,
):
    # Load config
    config = collision.utils.CollisionConfig.from_json(
        "./linear_transport/configs/" + "linear" + ".json")

    # Collision
    vmesh = collision.CartesianMesh(config)
    if coll == "linear":
        coll_op = collision.LinearCollision(config, vmesh)
    elif coll == "rbm":
        coll_op = collision.RandomBatchLinearCollision(config, vmesh)
    elif coll == "rbm_symm":
        coll_op = collision.SymmetricRBMLinearCollision(config, vmesh)
    else:
        raise NotImplementedError(
            "Collision method {} is not implemented.".format(coll))

    # x domian
    x = pykinetic.Dimension(xmin, xmax, nx, name="x")
    domain = pykinetic.Domain([x])

    # Riemann solver
    rp = pykinetic.riemann.advection_1D
    solver = DiffusiveRegimeSolver1D(
        rp,
        [coll_op],
        kn=kn(x.centers),
        sigma_s=sigma_s(x.centers),
        sigma_a=sigma_a(x.centers),
        Q=Q(x.centers),
    )
    solver.order = 1
    # solver.lim_type = 2
    # Time integrator
    if "RK" in scheme:
        solver.time_integrator = "RK"
        solver.a = rkcoeff[scheme]["a"]
        solver.b = rkcoeff[scheme]["b"]
        solver.c = rkcoeff[scheme]["c"]
    else:
        solver.time_integrator = scheme
    solver.dt = dt
    print("dt is {}".format(solver.dt))

    # Boundary condition
    def dirichlet_lower_BC(state, dim, t, qbc, auxbc, num_ghost):
        v = state.problem_data["v"][0]
        for i in range(num_ghost):
            qbc[0, i, v > 0] = f_l(v[v > 0])

    def dirichlet_upper_BC(state, dim, t, qbc, auxbc, num_ghost):
        v = state.problem_data["v"][0]
        for i in range(num_ghost):
            qbc[0, -i - 1, v < 0] = f_r(v[v < 0])

    if BC == "periodic":
        solver.bc_lower[0] = pykinetic.BC.periodic
        solver.bc_upper[0] = pykinetic.BC.periodic
    elif BC == "dirichlet":
        solver.bc_lower[0] = pykinetic.BC.custom
        solver.bc_upper[0] = pykinetic.BC.custom
        solver.user_bc_lower = dirichlet_lower_BC
        solver.user_bc_upper = dirichlet_upper_BC
    else:
        raise ValueError("Given BC type is not avaliable!")

    state = pykinetic.State(domain, vmesh, 1)
    state.problem_data["v"] = vmesh.centers
    qinit(state, vmesh, init_func)
    sol = pykinetic.Solution(state, domain)

    output_dict = {}
    sol_frames, macro_frames, ts = (
        [copy.deepcopy(sol.q)],
        [compute_rho(sol.state, vmesh)],
        [0.0],
    )
    pbar = Progbar(nt)
    for t in range(nt):
        solver.evolve_to_time(sol)
        if (t + 1) % 1 == 0:
            sol_frames.append(copy.deepcopy(sol.q))
            macro_frames.append(compute_rho(sol.state, vmesh))
            ts.append(sol.t)
        pbar.update(t + 1, finalize=False)
    pbar.update(nt, finalize=True)

    output_dict["f_frames"] = np.asarray(sol_frames)[:, 0]
    output_dict["macro_frames"] = np.asarray(macro_frames)
    output_dict["x"] = np.asarray(x.centers)
    output_dict["t"] = np.asarray(ts)
    output_dict["v"] = np.asarray(vmesh.centers[0])

    return output_dict
예제 #4
0
def run(
    kn=lambda x: 1.0,
    sigma_s=lambda x: 1.0,
    sigma_a=lambda x: 0.0,
    Q=lambda x: 0.0,
    xmin=0.0,
    xmax=1.0,
    nx=40,
    dt=0.01,
    nt=1000,
    coll="linear",
    scheme="Euler",
    BC="periodic",
    f_l=lambda v: 1.0,
    f_r=lambda v: 0.0,
    init_func=lambda vmesh, u, T, rho: 0.0,
):
    # Load config
    config = collision.utils.CollisionConfig.from_json(
        "./linear_transport/configs/" + "parity" + ".json")

    # Collision
    vmesh = collision.CartesianMesh(config)
    # print(vmesh.centers[0], vmesh.weights)
    if coll == "linear":
        coll_op = collision.LinearCollision(config, vmesh)
    elif coll == "rbm":
        coll_op = collision.RandomBatchLinearCollision(config, vmesh)
    elif coll == "rbm_symm":
        coll_op = collision.SymmetricRBMLinearCollision(config, vmesh)
    else:
        raise NotImplementedError(
            "Collision method {} is not implemented.".format(coll))

    # x domian
    x = pykinetic.Dimension(xmin, xmax, nx, name="x")
    # print(x.centers_with_ghost(2))
    domain = pykinetic.Domain([x])

    # Riemann solver
    rp = pykinetic.riemann.parity_1D
    solver = APNeutronTransportSolver1D(
        rp,
        [coll_op],
        kn=kn(x.centers),
        sigma_s=sigma_s(x.centers),
        sigma_a=sigma_a(x.centers),
        Q=Q(x.centers),
    )
    # print(solver.kn)
    solver.order = 1
    # solver.lim_type = -1
    solver.time_integrator = scheme
    solver.dt = dt
    print("dt is {}".format(solver.dt))

    sigma = sigma_s(x.centers) + kn(x.centers)**2 * sigma_a(x.centers)
    sigma_l, sigma_r = None, None
    if isinstance(sigma, np.ndarray):
        sigma_l, sigma_r = sigma[0], sigma[-1]
    else:
        sigma_l = sigma_r = sigma
    kn_l, kn_r = None, None
    if isinstance(solver.kn, np.ndarray):
        kn_l, kn_r = solver.kn[0], solver.kn[-1]
    else:
        kn_l = kn_r = solver.kn

    # Boundary conditions
    def dirichlet_lower_BC(state, dim, t, qbc, auxbc, num_ghost):
        v = state.problem_data["v"][0] / sigma_l
        kn_dx = kn_l / state.grid.delta[0]
        for i in range(num_ghost):
            qbc[0,
                i, :] = (f_l(v) - (0.5 - kn_dx * v) * qbc[0, num_ghost, :]) / (
                    0.5 + kn_dx * v)
        for i in range(num_ghost):
            qbc[1,
                i, :] = (2 * f_l(v) -
                         (qbc[0, num_ghost - 1, :] +
                          qbc[0, num_ghost, :])) / kn_l - qbc[1, num_ghost, :]

    def dirichlet_upper_BC(state, dim, t, qbc, auxbc, num_ghost):
        v = state.problem_data["v"][0] / sigma_r
        kn_dx = kn_r / state.grid.delta[0]
        for i in range(num_ghost):
            qbc[0, -i -
                1, :] = (f_r(v) -
                         (0.5 - kn_dx * v) * qbc[0, -num_ghost - 1, :]) / (
                             0.5 + kn_dx * v)
        for i in range(num_ghost):
            qbc[1, -i -
                1, :] = ((qbc[0, -num_ghost, :] + qbc[0, -num_ghost - 1, :]) -
                         2 * f_r(v)) / kn_r - qbc[1, -num_ghost - 1, :]

    if BC == "periodic":
        solver.bc_lower[0] = pykinetic.BC.periodic
        solver.bc_upper[0] = pykinetic.BC.periodic
    elif BC == "dirichlet":
        solver.bc_lower[0] = pykinetic.BC.custom
        solver.bc_upper[0] = pykinetic.BC.custom
        solver.user_bc_lower = dirichlet_lower_BC
        solver.user_bc_upper = dirichlet_upper_BC
    else:
        raise ValueError("Given BC type is not avaliable!")

    state = pykinetic.State(domain, vmesh, 2)
    state.problem_data["v"] = vmesh.centers
    state.problem_data["phi"] = phi(solver.kn)
    state.problem_data["sqrt_phi"] = np.sqrt(phi(solver.kn))

    qinit(state, vmesh, solver.kn, init_func)
    sol = pykinetic.Solution(state, domain)

    output_dict = {}
    sol_frames, macro_frames, ts = (
        [copy.deepcopy(sol.q)],
        [compute_rho(sol.state, vmesh)],
        [0.0],
    )
    pbar = Progbar(nt)
    for t in range(nt):
        solver.evolve_to_time(sol)
        sol_frames.append(copy.deepcopy(sol.q))
        macro_frames.append(compute_rho(sol.state, vmesh))
        # Test
        # qbc = solver.qbc
        # print(
        #     np.max(
        #         np.abs(
        #             5.0 * np.sin(vmesh.centers[0])
        #             - (
        #                 0.5 * (qbc[0, 1] + qbc[0, 2])
        #                 + kn * 0.5 * (qbc[1, 1] + qbc[1, 2])
        #             )
        #         )
        #     )
        # )
        ts.append(0.0 + (t + 1) * dt)
        pbar.update(t + 1, finalize=False)
    pbar.update(nt, finalize=True)

    output_dict["f_frames"] = np.asarray(sol_frames)
    output_dict["macro_frames"] = macro_frames
    output_dict["x"] = x.centers
    output_dict["t"] = ts
    output_dict["v"] = np.asarray(vmesh.centers[0])

    return output_dict
예제 #5
0
def run(
    kn=1.0,
    sigma_s=lambda x, y: 1.0,
    sigma_a=lambda x, y: 0.0,
    Q=lambda x, y: 0.0,
    xmin=[0.0, 0.0],
    xmax=[1.0, 1.0],
    nx=40,
    dt=0.01,
    nt=1000,
    coll="linear",
    scheme="Euler",
    BC="periodic",
    f_l=lambda vx, vy: 0.0,
    f_b=lambda vx, vy: 0.0,
    f_r=lambda vx, vy: 0.0,
    f_t=lambda vx, vy: 0.0,
    init_func=lambda vmesh, u, T, rho: 0.0,
):
    # Load config
    config = collision.utils.CollisionConfig.from_json(
        "./examples/linear_transport/configs/" + "linear_2d" + ".json"
    )

    # Collision
    vmesh = collision.PolarMesh(config)
    if coll == "linear":
        coll_op = collision.LinearCollision(config, vmesh)
    elif coll == "rbm":
        coll_op = collision.RandomBatchLinearCollision(config, vmesh)
    elif coll == "rbm_symm":
        coll_op = collision.SymmetricRBMLinearCollision(config, vmesh)
    else:
        raise NotImplementedError(
            "Collision method {} is not implemented.".format(coll)
        )

    # x domian
    x = pykinetic.Dimension(xmin[0], xmax[0], nx, name="x")
    y = pykinetic.Dimension(xmin[1], xmax[1], nx, name="y")
    domain = pykinetic.Domain([x, y])

    # Riemann solver
    rp = pykinetic.riemann.advection_2D
    solver = DiffusiveRegimeSolver2D(
        rp,
        coll_op,
        kn=kn(x.centers, y.centers),
        sigma_s=sigma_s(x.centers, y.centers),
        sigma_a=sigma_a(x.centers, y.centers),
        Q=Q(x.centers, y.centers),
    )
    solver.order = 1
    # solver.lim_type = 2
    # Time integrator
    solver.time_integrator = scheme
    solver.dt = dt
    print("dt is {}".format(solver.dt))

    # Boundary condition
    def dirichlet_lower_BC(state, dim, t, qbc, auxbc, num_ghost):
        vx, vy = state.problem_data["v"]
        if dim.name == "x":
            for i in range(num_ghost):
                qbc[0, i, :, (vx > 0)[0], :] = f_l(vx[(vx > 0)[0]], vy)
        elif dim.name == "y":
            for i in range(num_ghost):
                qbc[0, :, i, :, (vy > 0)[:, 0]] = f_b(vx, vy[(vy > 0)[:, 0]])
        else:
            raise ValueError("Dim could be only x or y.")

    def dirichlet_upper_BC(state, dim, t, qbc, auxbc, num_ghost):
        vx, vy = state.problem_data["v"]
        if dim.name == "x":
            for i in range(num_ghost):
                qbc[0, -i - 1, :, (vx < 0)[0], :] = f_r(vx[(vx < 0)[0]], vy)
        elif dim.name == "y":
            for i in range(num_ghost):
                qbc[0, :, -i - 1, :, (vy < 0)[:, 0]] = f_t(vx, vy[(vy < 0)[:, 0]])
        else:
            raise ValueError("Dim could be only 'x' or 'y'.")

    if BC == "periodic":
        solver.all_bcs = pykinetic.BC.periodic
    elif BC == "dirichlet":
        solver.all_bcs = pykinetic.BC.custom
        solver.user_bc_lower = dirichlet_lower_BC
        solver.user_bc_upper = dirichlet_upper_BC
    else:
        raise ValueError("Given BC type is not avaliable!")

    state = pykinetic.State(domain, vmesh, 1)
    state.problem_data["v"] = vmesh.centers
    qinit(state, vmesh, init_func)
    sol = pykinetic.Solution(state, domain)

    output_dict = {}
    sol_frames, macro_frames, ts = (
        [copy.deepcopy(sol)],
        [compute_rho(sol.state, vmesh)],
        [0.0],
    )
    pbar = Progbar(nt)
    for t in range(nt):
        solver.evolve_to_time(sol)
        sol_frames.append(copy.deepcopy(sol))
        macro_frames.append(compute_rho(sol.state, vmesh))
        ts.append(0.0 + (t + 1) * dt)
        pbar.update(t + 1, finalize=False)
    pbar.update(nt, finalize=True)

    output_dict["macro_frames"] = macro_frames
    output_dict["mesh"] = state.c_centers
    output_dict["t"] = ts

    return output_dict