Ejemplo n.º 1
0
def test_simul_already_ended(heat_model):
    x, dx = np.linspace(0, 10, 50, retstep=True, endpoint=False)
    T = np.cos(x * 2 * np.pi / 10)
    initial_fields = heat_model.fields_template(x=x, T=T)
    parameters = dict(periodic=True, k=1)

    simul = Simulation(heat_model, initial_fields, parameters,
                       dt=1, tol=1E-1, tmax=10)
    simul.run()
    simul.run()
Ejemplo n.º 2
0
def test_simul_runtime_error(heat_model):
    x, dx = np.linspace(0, 10, 50, retstep=True, endpoint=False)
    T = np.cos(x * 2 * np.pi / 10)
    initial_fields = heat_model.fields_template(x=x, T=T)
    parameters = dict(periodic=True, k=1)

    simul = Simulation(heat_model, initial_fields, parameters,
                       dt=1, tol=1E-1, max_iter=2)
    with pytest.raises(RuntimeError):
        for t, fields in simul:
            pass

    simul = Simulation(heat_model, initial_fields, parameters,
                       dt=1, tol=1E-1, dt_min=.1)
    with pytest.raises(RuntimeError):
        for t, fields in simul:
            pass
Ejemplo n.º 3
0
def simul(heat_model, fields):
    parameters = dict(periodic=True, k=1)
    simul = Simulation(heat_model,
                       fields.copy(),
                       parameters,
                       dt=.5,
                       tmax=2,
                       tol=1E-1,
                       id="test_triflow_containers")
    return simul
Ejemplo n.º 4
0
def test_simul_heat_eq(heat_model, scheme):
    x, dx = np.linspace(0, 10, 50, retstep=True, endpoint=False)
    T = np.cos(x * 2 * np.pi / 10)
    initial_fields = heat_model.fields_template(x=x, T=T)
    parameters = dict(periodic=True, k=1)
    for i, (t, fields) in enumerate(Simulation(heat_model, initial_fields,
                                               parameters, scheme=scheme,
                                               dt=1, tmax=100, tol=1E-1)):
        continue
    assert t == 100
    assert np.isclose(fields["T"].values.mean(), 0)
Ejemplo n.º 5
0
def simul(heat_model):
    x, dx = np.linspace(0, 10, 50, retstep=True, endpoint=False)
    T = np.cos(x * 2 * np.pi / 10)
    initial_fields = heat_model.fields_template(x=x, T=T)
    parameters = dict(periodic=True, k=1)
    simul = Simulation(heat_model,
                       initial_fields,
                       parameters,
                       dt=.5,
                       tmax=2,
                       tol=1E-1)
    return simul
Ejemplo n.º 6
0
def test_simul_pprocess(heat_model):
    x, dx = np.linspace(0, 10, 50, retstep=True, endpoint=False)
    T = np.cos(x * 2 * np.pi / 10)
    initial_fields = heat_model.fields_template(x=x, T=T)
    parameters = dict(periodic=True, k=1)

    simul = Simulation(heat_model, initial_fields, parameters,
                       dt=1, tol=1E-1, tmax=10)

    def compute_grad(simul):
        simul.fields["grad"] = np.gradient(simul.fields["T"].values)
        return simul

    simul.add_post_process("grad", compute_grad)
    simul.run()
    simul.remove_post_process("grad")
    assert simul.post_processes == []
Ejemplo n.º 7
0
def test_simul_heat_eq_dirichlet(heat_model, scheme):
    x, dx = np.linspace(0, 10, 50, retstep=True, endpoint=False)
    T = np.cos(x * 2 * np.pi / 10)
    initial_fields = heat_model.fields_template(x=x, T=T)
    parameters = dict(periodic=False, k=1)

    def dirichlet_bdc(t, fields, parameters):
        fields["T"][0] = 1
        fields["T"][-1] = 1
        return fields, parameters

    simul = Simulation(heat_model, initial_fields, parameters,
                       hook=dirichlet_bdc, scheme=scheme,
                       dt=.1, tmax=100, tol=1E-1)

    for i, (t, fields) in enumerate(simul):
        pass
    assert np.isclose(t, 100)
    assert np.isclose(fields["T"], 1, atol=1E-1).all()
parameters = dict(c=.03, k=.001, dx=dx, periodic=False)

t = 0
dt = 5E-1
tmax = 2.5

pl.plot(fields.x, fields.U, label='t: %g' % t)


def dirichlet_condition(t, fields, pars):
    fields.U[0] = 1
    fields.U[-1] = 0
    return fields, pars


simul = Simulation(model,
                   fields,
                   parameters,
                   dt,
                   hook=dirichlet_condition,
                   tmax=tmax)

for i, (t, fields) in enumerate(simul):
    print("iteration: %i\t" % i, "t: %g" % t, end='\r')
    pl.plot(fields.x, fields.U, label='t: %g' % t)

pl.xlim(0, 1)
legend = pl.legend(loc='best')

pl.show()
Ejemplo n.º 9
0
import numpy as np

from triflow import Model, Simulation

# We initialize the model dtU = k * dxxU and we precise
# the variable and the parameters
model = Model("k * dxxU", "U", "k")

# We discretize our spatial domain between 0 and 100 with 500 nodes.
# retstep=True ask to return the spatial step. We want periodic condition,
# so endpoint=True exclude the final node (which will be redondant with the
# first node, x=0 and x=100 are merged)
x, dx = np.linspace(0, 100, 500, retstep=True, endpoint=False)

# We initialize with a sinusoidal initial condition
U = np.cos(2 * np.pi * x / 100 * 10)
# We fill the fields container
fields = model.fields_template(x=x, U=U)
# We precise our parameters. The default scheme provide an automatic
# time_stepping, we have to precise the tolerance. We set a periodic
# simulation.
parameters = dict(k=1e-1, periodic=True)

# We initialize the simulation
simulation = Simulation(model, fields, parameters, dt=5, tol=1E-1, tmax=50)

# We iterate on the simulation until the end.
for t, fields in simulation:
    print("t: %g, mean value of U: %g" % (t, fields.U.mean().values))