Exemple #1
0
    def __init__(self, setup):
        self.setup = setup

        sigma2 = pow(setup.sigma, 2)
        dx_opt = abs(setup.C_opt / (.5 * sigma2 - setup.r) * setup.l2_opt *
                     sigma2)
        dt_opt = pow(dx_opt, 2) / sigma2 / setup.l2_opt

        # adjusting dt so that nt is integer
        self.dt = setup.T
        self.nt = 0
        while self.dt > dt_opt:
            self.nt += 1
            self.dt = setup.T / self.nt

        # adjusting dx to match requested l^2
        dx = np.sqrt(setup.l2_opt * self.dt) * setup.sigma

        # calculating actual u number and lambda
        self.C = -(.5 * sigma2 - setup.r) * (-self.dt) / dx
        self.l2 = dx * dx / sigma2 / self.dt

        # adjusting nx and setting S_beg, S_end
        S_beg = setup.S_match
        self.nx = 1
        while S_beg > setup.S_min:
            self.nx += 1
            S_beg = np.exp(np.log(setup.S_match) - self.nx * dx)

        self.ix_match = self.nx

        S_end = setup.S_match
        while S_end < setup.S_max:
            self.nx += 1
            S_end = np.exp(np.log(S_beg) + (self.nx - 1) * dx)

        # asset price
        self.S = np.exp(np.log(S_beg) + np.arange(self.nx) * dx)

        self.mu_coeff = 0.5 / self.l2
        self.solvers = {}
        self.solvers[1] = Factories.advection_diffusion_1d(
            advectee=setup.payoff(self.S),
            advector=self.C,
            options=Options(n_iters=1, non_zero_mu_coeff=True),
            boundary_conditions=Extrapolated())
        self.solvers[2] = Factories.advection_diffusion_1d(
            advectee=setup.payoff(self.S),
            advector=self.C,
            options=Options(**OPTIONS),
            boundary_conditions=Extrapolated())
    def __init__(self, particles_builder: ParticlesBuilder, dt, grid, size,
                 stream_function, field_values, rhod_of, mpdata_iters,
                 mpdata_iga, mpdata_fct, mpdata_tot):
        super().__init__(particles_builder, dt, Mesh(grid, size), [])

        self.__rhod_of = rhod_of

        grid = self.mesh.grid
        rhod = np.repeat(rhod_of(
            (np.arange(grid[1]) + 1 / 2) / grid[1]).reshape((1, grid[1])),
                         grid[0],
                         axis=0)

        self.__GC, self.__mpdatas = Factories.stream_function_2d(
            grid=self.mesh.grid,
            size=self.mesh.size,
            dt=self.dt,
            stream_function=stream_function,
            field_values=dict((key, np.full(grid, value))
                              for key, value in field_values.items()),
            g_factor=rhod,
            options=Options(n_iters=mpdata_iters,
                            infinite_gauge=mpdata_iga,
                            flux_corrected_transport=mpdata_fct,
                            third_order_terms=mpdata_tot))

        rhod = particles_builder.particles.backend.from_ndarray(rhod.ravel())
        self._values["current"]["rhod"] = rhod
        self._tmp["rhod"] = rhod
        self.asynchronous = False
        self.thread: Thread = None

        super().sync()
        self.notify()
Exemple #3
0
def test_timing_2d(benchmark, options):
    setup = Setup(n_rotations=6)
    _, __, z = from_pdf_2d(setup.pdf,
                           xrange=setup.xrange,
                           yrange=setup.yrange,
                           gridsize=setup.grid)
    mpdata = Factories.constant_2d(data=z, C=(-.5, .25), options=options)

    def set_z():
        mpdata.curr.get()[:] = z

    benchmark.pedantic(mpdata.advance, (setup.nt, ),
                       setup=set_z,
                       warmup_rounds=1,
                       rounds=3)
    state = mpdata.curr.get()

    print(np.amin(state), np.amax(state))
    if options.n_iters == 1:
        assert np.amin(state) >= h0
    assert np.amax(state) < 10 * h

    if False:
        pyplot.imshow(state)
        pyplot.colorbar()
        pyplot.show()
    def __init__(self, dt, grid, size, stream_function, field_values, rhod_of,
                 mpdata_iters, mpdata_iga, mpdata_fct, mpdata_tot):
        super().__init__(dt, Mesh(grid, size), [])

        self.__rhod_of = rhod_of

        grid = self.mesh.grid
        self.rhod = np.repeat(
            rhod_of(
                (np.arange(grid[1]) + 1 / 2) / grid[1]
            ).reshape((1, grid[1])),
            grid[0],
            axis=0
        )

        self.__GC, self.__mpdatas = Factories.stream_function_2d(
            grid=self.mesh.grid, size=self.mesh.size, dt=self.dt,
            stream_function=stream_function,
            field_values=dict((key, np.full(grid, value)) for key, value in field_values.items()),
            g_factor=self.rhod,
            options=Options(
                n_iters=mpdata_iters,
                infinite_gauge=mpdata_iga,
                flux_corrected_transport=mpdata_fct,
                third_order_terms=mpdata_tot
            )
        )

        self.asynchronous = False
        self.thread: (Thread, None) = None
Exemple #5
0
    def __init__(self, setup: Setup, options: Options):

        x, state = from_cdf_1d(setup.cdf, setup.x_min, setup.x_max, setup.nx)

        self.stepper = Factories.constant_1d(
            state,
            setup.C,
            options
        )
        self.nt = setup.nt
Exemple #6
0
def test_upwind_1d():
    state = np.array([0, 1, 0])
    C = 1

    mpdata = Factories.constant_1d(state, C, Options(n_iters=1))
    nt = 5

    conserved = np.sum(mpdata.advectee.get())
    mpdata.advance(nt)

    assert np.sum(mpdata.advectee.get()) == conserved
Exemple #7
0
    def __init__(self, setup, grid_layout, psi_coord, opts, GC_max):
        self.setup = setup
        self.psi_coord = psi_coord

        # units of calculation
        self.__t_unit = self.setup.si.seconds
        self.__r_unit = self.setup.si.micrometre
        self.__n_unit = self.setup.si.centimetres ** -3 / self.setup.si.micrometre

        self.solver, self.__r, self.__rh, self.dx, dt = Factories.condensational_growth(
            self.setup.nr,
            self.__mgn(self.setup.r_min, self.__r_unit),
            self.__mgn(self.setup.r_max, self.__r_unit),
            GC_max,
            grid_layout,
            psi_coord,
            lambda r: self.__mgn(self.setup.pdf(r * self.__r_unit), self.__n_unit),
            lambda r: self.__mgn(self.setup.drdt(r * self.__r_unit), self.__r_unit / self.__t_unit),
            opts
        )

        self.out_steps = Simulation.find_out_steps(setup=self.setup, dt=dt)
        self.dt = dt * self.__t_unit
def test_single_timestep(options):
    # Arrange
    grid = (75, 75)
    size = (1500, 1500)
    dt = 1
    w_max = .6

    def stream_function(xX, zZ):
        X = size[0]
        return - w_max * X / np.pi * np.sin(np.pi * zZ) * np.cos(2 * np.pi * xX)

    rhod_of = lambda z: 1 - z * 1e-4
    rhod = np.repeat(
        rhod_of(
            (np.arange(grid[1]) + 1 / 2) / grid[1]
        ).reshape((1, grid[1])),
        grid[0],
        axis=0
    )

    GC, mpdatas = Factories.stream_function_2d(
        grid=grid, size=size, dt=dt,
        stream_function=stream_function,
        field_values={'th': np.full(grid, 300), 'qv': np.full(grid, .001)},
        g_factor=rhod,
        options=options
    )

    # Plot

    # Act
    for mpdata in mpdatas.values():
        mpdata.advance(1)

    # Assert
    for k, v in mpdatas.items():
        assert np.isfinite(v.curr.get()).all()
Exemple #9
0
 def __init__(self, setup: Setup, options: Options):
     x, y, z = from_pdf_2d(setup.pdf, xrange=setup.xrange, yrange=setup.yrange, gridsize=setup.grid)
     self.mpdata = Factories.stream_function_2d_basic(setup.grid, setup.size, setup.dt, setup.stream_function, z, options)
     self.nt = setup.nt