コード例 #1
0
def test_steady_state_tracker():
    """ test the SteadyStateTracker """
    storage = MemoryStorage()
    c0 = ScalarField.random_uniform(UnitGrid([5]))
    pde = DiffusionPDE()
    tracker = trackers.SteadyStateTracker(atol=1e-2, rtol=1e-2, progress=True)
    pde.solve(c0, 1e3, dt=0.1, tracker=[tracker, storage.tracker(interval=1e2)])
    assert len(storage) < 9  # finished early
コード例 #2
0
def integrate(tfinal, u, u2, udiff, IC):
    grid = CartesianGrid([[0, 200]], 200)
    field = ScalarField(grid, IC)
    storage = MemoryStorage()
    eq = libraryPDE(u, u2, udiff)  #define PDE with custom parameters
    return eq.solve(field,
                    t_range=tfinal,
                    dt=0.1,
                    tracker=storage.tracker(0.1)).data
コード例 #3
0
def test_small_tracker_dt():
    """test the case where the dt of the tracker is smaller than the dt
    of the simulation"""
    storage = MemoryStorage()
    pde = DiffusionPDE()
    c0 = ScalarField.random_uniform(UnitGrid([4, 4]), 0.1, 0.2)
    pde.solve(
        c0, 1e-2, dt=1e-3, method="explicit", tracker=storage.tracker(interval=1e-4)
    )
    assert len(storage) == 11
コード例 #4
0
def test_pde_time_dependent_bcs(backend):
    """test PDE with time-dependent BCs"""
    field = ScalarField(grids.UnitGrid([3]))

    eq = PDE({"c": "laplace(c)"}, bc={"value_expression": "Heaviside(t - 1.5)"})

    storage = MemoryStorage()
    eq.solve(field, t_range=10, dt=1e-2, backend=backend, tracker=storage.tracker(1))

    np.testing.assert_allclose(storage[1].data, 0)
    np.testing.assert_allclose(storage[-1].data, 1, rtol=1e-3)
コード例 #5
0
def main():
    xstep=5
    tstep=1
    v=73.82/60#Unit:cm/s
    tmin=0
    tmax=500
    bc_T=f'-1*{llambda}/{h}*c.diff(x)+(Piecewise(\
        ((25+{T_Kelvin}),tt<=20/{v}),\
        ((25+150/{v}*(tt-20/{v})+{T_Kelvin}),And(20/{v}<tt,tt<=25/{v})),\
        ((175+{T_Kelvin}),And(25/{v}<tt,tt<=197.5/{v})),\
        ((175+20/{v}*(tt-197.5/{v})+{T_Kelvin}),And(197.5/{v}<tt,tt<=202.5/{v})),\
        ((195+{T_Kelvin}),And(202.5/{v}<tt,tt<=233/{v})),\
        ((195+40/{v}*(tt-233/{v})+{T_Kelvin}),And(233/{v}<tt,tt<=238/{v})),\
        ((235+{T_Kelvin}),And(238/{v}<tt,tt<=268.5/{v})),\
        ((235+20/{v}*(tt-268.5/{v})+{T_Kelvin}),And(268.5/{v}<tt,tt<=273.5/{v})),\
        ((255+{T_Kelvin}),And(273.5/{v}<tt,tt<=344.5/{v})),\
        ((255-230/{v}*(tt-344.5/{v})+{T_Kelvin}),And(344.5/{v}<tt,tt<=435.5/{v})),\
        ((25+{T_Kelvin}),true)).subs(tt,t))' 
        #ture需要小写,f'string'string里面有{}可以传递全局参数参数
    grid = CartesianGrid([[0, x_max]], [int(xstep)],periodic=False)
    state=ScalarField(grid=grid,data=T_origin+T_Kelvin)

    eq = DiffusionPDE(diffusivity=alpha, bc={'value_expression':bc_T},)

    storage = MemoryStorage()
    eq.solve(state, t_range=(tmin,tmax),dt=1e-3, tracker=storage.tracker(tstep))

    def temp_curve():
        p=[]
        for i in range(int((tmax-tmin)/tstep)):
            p.append(storage.data[i][-1])
    
        q=np.asarray(p)
        plt.plot(np.linspace(tmin,tmax,int((tmax-tmin)/tstep),endpoint=0),q,label='Temperature')
        plt.xlabel('Time')
        plt.ylabel('Temprature')
        plt.show()

    def plotky():
        plot_kymograph(storage)

    #temp_curve()
    plotky()
コード例 #6
0
def test_solvers_simple_ode(scheme, adaptive):
    """test explicit solvers with a simple ODE"""
    grid = UnitGrid([1])
    field = ScalarField(grid, 1)
    eq = PDE({"y": "2*sin(t) - y"})

    dt = 1e-3 if scheme == "euler" else 1e-2

    storage = MemoryStorage()
    solver = ExplicitSolver(eq, scheme=scheme, adaptive=adaptive)
    controller = Controller(solver, t_range=20.0, tracker=storage.tracker(1))
    controller.run(field, dt=dt)

    ts = np.ravel(storage.times)
    expect = 2 * np.exp(-ts) - np.cos(ts) + np.sin(ts)
    np.testing.assert_allclose(np.ravel(storage.data), expect, atol=0.05)

    if adaptive:
        assert solver.info["steps"] < 20 / dt
    else:
        assert solver.info["steps"] == pytest.approx(20 / dt, abs=1)
コード例 #7
0
def main():
        #ture需要小写,f'string'string里面有{}可以传递全局参数参数
    eq = Heat_trans()

    storage = MemoryStorage()
    eq.solve(state, t_range=(tmin,tmax),dt=1e-3, tracker=storage.tracker(tstep))

    def temp_curve():
        p=[]
        for i in range(int((tmax-tmin)/tstep)):
            p.append(storage.data[i][-1])
    
        q=np.asarray(p)
        plt.plot(np.linspace(tmin,tmax,int((tmax-tmin)/tstep),endpoint=0),q,label='Temperature')
        plt.xlabel('Time')
        plt.ylabel('Temprature')
        plt.show()

    def plotky():
        plot_kymograph(storage)

    #temp_curve()
    plotky()
コード例 #8
0
using the :class:`~pde.pdes.pde.PDE` class. In particular, we consider
:math:`D(x) = 1.01 + \tanh(x)`, which gives a low diffusivity on the left side of the
domain.

Note that the naive implementation,
:code:`PDE({"c": "divergence((1.01 + tanh(x)) * gradient(c))"})`, has numerical
instabilities. This is because two finite difference approximations are nested. To
arrive at a more stable numerical scheme, it is advisable to expand the divergence, 

.. math::
    \partial_t c = D \nabla^2 c + \nabla D . \nabla c
"""

from pde import PDE, CartesianGrid, MemoryStorage, ScalarField, plot_kymograph

# Expanded definition of the PDE
diffusivity = "1.01 + tanh(x)"
term_1 = f"({diffusivity}) * laplace(c)"
term_2 = f"dot(gradient({diffusivity}), gradient(c))"
eq = PDE({"c": f"{term_1} + {term_2}"}, bc={"value": 0})

grid = CartesianGrid([[-5, 5]], 64)  # generate grid
field = ScalarField(grid, 1)  # generate initial condition

storage = MemoryStorage()  # store intermediate information of the simulation
res = eq.solve(field, 100, dt=1e-3,
               tracker=storage.tracker(1))  # solve the PDE

plot_kymograph(storage)  # visualize the result in a space-time plot
コード例 #9
0
grid = CartesianGrid([[0, x_max]], [int(xstep)])
state = ScalarField(grid=grid, data=T_origin + T_Kelvin)

eq = PDE(
    rhs={'c': f'{alpha}*laplace(c)'},
    bc={
        'type': 'mixed',
        'value': -1 * h / llambda,
        'const': bc_T
    },
)
#bc_ops={})

storage = MemoryStorage()
eq.solve(state, t_range=(tmin, tmax), dt=1e-3, tracker=storage.tracker(tstep))


def temp_curve():
    p = []
    for i in range(int((tmax - tmin) / tstep)):
        p.append(storage.data[i][-1])

    q = np.asarray(p)
    plt.plot(np.linspace(tmin, tmax, int((tmax - tmin) / tstep), endpoint=0),
             q,
             label='Temperature')
    plt.xlabel('Time')
    plt.ylabel('Temprature')
    plt.show()
コード例 #10
0
ファイル: make_movie.py プロジェクト: xuanxu/py-pde
#!/usr/bin/env python3

from pde import UnitGrid, ScalarField, DiffusionPDE, MemoryStorage, movie_scalar

eq = DiffusionPDE()  # define the physics
grid = UnitGrid([16, 16])  # generate grid
state = ScalarField.random_uniform(grid, 0.2,
                                   0.3)  # generate initial condition

storage = MemoryStorage()  # create storage
tracker = storage.tracker(interval=1)  # create associated tracker

eq.solve(state, t_range=2, dt=0.005, tracker=tracker)

# create movie from stored data
movie_scalar(storage, '/tmp/diffusion.mov')
コード例 #11
0
    \partial_t \phi = 6 \phi \partial_x \phi - \partial_x^2 \phi
    
which we implement using a custom PDE class below.
"""

from math import pi

from pde import CartesianGrid, MemoryStorage, PDEBase, ScalarField, plot_kymograph


class KortewegDeVriesPDE(PDEBase):
    """ Korteweg-de Vries equation """
    def evolution_rate(self, state, t=0):
        """ implement the python version of the evolution equation """
        assert state.grid.dim == 1  # ensure the state is one-dimensional
        grad = state.gradient("natural")[0]
        return 6 * state * grad - grad.laplace("natural")


# initialize the equation and the space
grid = CartesianGrid([[0, 2 * pi]], [32], periodic=True)
state = ScalarField.from_expression(grid, "sin(x)")

# solve the equation and store the trajectory
storage = MemoryStorage()
eq = KortewegDeVriesPDE()
eq.solve(state, t_range=3, tracker=storage.tracker(0.1))

# plot the trajectory as a space-time plot
plot_kymograph(storage)
コード例 #12
0
ファイル: pde_1d.py プロジェクト: xuanxu/py-pde
from math import pi

from pde import (CartesianGrid, ScalarField, PDEBase, MemoryStorage,
                 plot_kymograph)


class KortewegDeVriesPDE(PDEBase):
    """ Korteweg-de Vries equation
    
    See https://en.wikipedia.org/wiki/Korteweg–de_Vries_equation
    """
    def evolution_rate(self, state, t=0):
        """ implement the python version of the evolution equation """
        assert state.grid.dim == 1  # ensure the state is one-dimensional
        grad = state.gradient('natural')[0]
        return 6 * state * grad - grad.laplace('natural')


# initialize the equation and the space
grid = CartesianGrid([[0, 2 * pi]], [32], periodic=True)
state = ScalarField.from_expression(grid, "sin(x)")
eq = KortewegDeVriesPDE()

# solve the equation and store the trajectory
storage = MemoryStorage()
eq.solve(state, t_range=3, tracker=['progress', storage.tracker(.1)])

# plot the trajectory as a space-time plot
plot_kymograph(storage, show=True)
コード例 #13
0
This example implements a complex PDE using the :class:`~pde.pdes.pde.PDE`. We here
chose the `Schrödinger equation <https://en.wikipedia.org/wiki/Schrödinger_equation>`_ 
without a spatial potential in non-dimensional form:

.. math::
    i \partial_t \psi = -\nabla^2 \psi
    
Note that the example imposes Neumann conditions at the wall, so the wave packet is
expected to reflect off the wall.
"""

from math import sqrt

from pde import PDE, CartesianGrid, MemoryStorage, ScalarField, plot_kymograph

grid = CartesianGrid([[0, 20]], 128, periodic=False)  # generate grid

# create a (normalized) wave packet with a certain form as an initial condition
initial_state = ScalarField.from_expression(
    grid, "exp(I * 5 * x) * exp(-(x - 10)**2)")
initial_state /= sqrt(initial_state.to_scalar("norm_squared").integral.real)

eq = PDE({"ψ": f"I * laplace(ψ)"})  # define the pde

# solve the pde and store intermediate data
storage = MemoryStorage()
eq.solve(initial_state, t_range=2.5, dt=1e-5, tracker=[storage.tracker(0.02)])

# visualize the results as a space-time plot
plot_kymograph(storage, scalar="norm_squared")
コード例 #14
0
#!/usr/bin/env python3

from pde import (DiffusionPDE, UnitGrid, ScalarField, MemoryStorage,
                 PlotTracker, PrintTracker, RealtimeIntervals)

eq = DiffusionPDE()  # define the pde
grid = UnitGrid([16, 16])  # generate grid
state = ScalarField.random_uniform(grid, 0.2,
                                   0.3)  # generate initial condition

storage = MemoryStorage()

trackers = [
    'progress',  # show progress bar during simulation
    'steady_state',  # abort when steady state is reached
    storage.tracker(interval=1),  # store data every simulation time unit
    PlotTracker(show=True),  # show images during simulation
    # print some output every 5 real seconds:
    PrintTracker(interval=RealtimeIntervals(duration=5))
]

eq.solve(state, 10, dt=0.1, tracker=trackers)

storage[0].plot(show=True)
コード例 #15
0
"""
Time-dependent boundary conditions
==================================

This example solves a simple diffusion equation in one dimensions with time-dependent
boundary conditions.
"""

from pde import PDE, CartesianGrid, MemoryStorage, ScalarField, plot_kymograph

grid = CartesianGrid([[0, 10]], [64])  # generate grid
state = ScalarField(grid)  # generate initial condition

eq = PDE({"c": "laplace(c)"}, bc={"value_expression": "sin(t)"})

storage = MemoryStorage()
eq.solve(state, t_range=20, dt=1e-4, tracker=storage.tracker(0.1))

# plot the trajectory as a space-time plot
plot_kymograph(storage)
コード例 #16
0
    (195+8*tt+273.15,And(233<tt,tt<=238)), \
    (235+273.15,And(238<tt,tt<=268.5)), \
    (235+4*tt+273.15,And(268.5<tt,tt<=273.5)), \
    (255+273.15,And(273.5<tt,tt<=344.5)), \
    (255-(230/91)*tt+273.15,And(344.5<tt,tt<=435.5)), \
    (25+273.15,And(435.5<tt,tt<=500))).subs(tt,t))'

grid = CartesianGrid([[0, x_max]], [int(xstep)])
state=ScalarField(grid=grid,data=T_origin+T_Kelvin)

eq = PDE(rhs={'c': 'a*laplace(c)'}, 
    bc={'value_expression': bc_T},
    consts={'a':alpha})

storage = MemoryStorage()
eq.solve(state, t_range=tmax, tracker=storage.tracker(tstep))

def kymograph_pic():
    plot_kymograph(storage)

def temp_curve():
    p=[]
    for i in range(int(tmax/tstep)):
        p.append(storage.data[i][-1])
    
    q=np.asarray(p)
    plt.plot(np.linspace(0,tmax,int(tmax/tstep),endpoint=1),q,label='Temperature')
    plt.xlabel('Time')
    plt.ylabel('Temprature')
    plt.show()
コード例 #17
0
"""
Stochastic simulation
=====================

This example illustrates how a stochastic simulation can be done.
"""

from pde import (KPZInterfacePDE, UnitGrid, ScalarField, MemoryStorage,
                 plot_kymograph)

grid = UnitGrid([64])  # generate grid
state = ScalarField.random_harmonic(grid)  # generate initial condition

eq = KPZInterfacePDE(noise=1)  # define the SDE
storage = MemoryStorage()
eq.solve(state, t_range=10, dt=0.01, tracker=storage.tracker(0.5))
plot_kymograph(storage)