def test_jac_simpl(): model = Model("dxxU", "U") model_simp = Model("dxxU", "U", simplify=True) x = np.linspace(0, 2 * np.pi, 50, endpoint=False) U = np.cos(x) assert np.isclose( model.J(model.fields_template(x=x, U=U), dict(periodic=True)).todense(), model.J(model_simp.fields_template(x=x, U=U), dict(periodic=True)).todense(), ).all()
def test_model_api(compiler, periodic): model = Model( differential_equations=["k * dxxU + s"], dependent_variables="U", parameters="k", help_functions="s", compiler=compiler, ) assert set(model._args) == set( ["x", "U_m1", "U", "U_p1", "s_m1", "s", "s_p1", "k", "dx"]) with pytest.raises(NotImplementedError): Model("dxxxxxU", "U") with pytest.raises(ValueError): Model("dxxx(dx)", "U") x, dx = np.linspace(0, 10, 100, retstep=True, endpoint=False) U = np.cos(x * 2 * np.pi / 10) s = np.zeros_like(x) fields = model.fields_template(x=x, U=U, s=s) parameters = dict(periodic=periodic, k=1) model.F(fields, parameters) model.J(fields, parameters)
def test_upwind(compiler, uporder, vel, periodic): model = Model( differential_equations=["upwind(%s, U, %i)" % (vel, uporder)], dependent_variables="U", parameters="k", help_functions="s", compiler=compiler, ) x, dx = np.linspace(0, 10, 100, retstep=True, endpoint=False) U = np.cos(x * 2 * np.pi / 10) s = np.zeros_like(x) fields = model.fields_template(x=x, U=U, s=s) parameters = dict(periodic=periodic, k=1) model.F(fields, parameters) model.J(fields, parameters)
def test_fields_api(): model = Model(differential_equations=["dxxU1", "dxxU2"], dependent_variables=["U1", "U2"], help_functions='s') x = np.linspace(0, 1, 100) U1 = np.cos(x) U2 = np.sin(x) s = np.sin(x) fields = model.fields_template(x=x, U1=U1, U2=U2, s=s) print(fields) assert np.isclose(fields.uflat, np.vstack([U1, U2]).flatten('F')).all() assert np.isclose(fields.uarray.to_array(), np.vstack([U1, U2])).all() assert np.isclose(fields["x"], x).all() assert np.isclose(fields["U1"], U1).all() assert np.isclose(fields["U2"], U2).all() assert np.isclose(fields["s"], s).all() assert fields.size == x.size
def test_model_monovariate(func, var, par, k, compiler): model = Model(func, var, par, compiler=compiler) x, dx = np.linspace(0, 10, 100, retstep=True, endpoint=False) U = np.cos(x * 2 * np.pi / 10) fields = model.fields_template(x=x, U=U) parameters = dict(periodic=True, k=k) F = model.F(fields, parameters) J_sparse = model.J(fields, parameters) J_dense = model.J(fields, parameters, sparse=False) J_approx = model.F.diff_approx(fields, parameters) dxU = np.gradient(np.pad(U, 2, mode="wrap")) / dx dxxU = np.gradient(dxU) / dx dxU = dxU[2:-2] dxxU = dxxU[2:-2] assert np.isclose(F, k * dxxU, rtol=1E-2).all() assert np.isclose(J_approx, J_sparse.todense(), rtol=1E-2).all() assert np.isclose(J_approx, J_dense, rtol=1E-2).all()
def test_model_bivariate(): model = Model(["k1 * dxx(v)", "k2 * dxx(u)"], ["u", "v"], ["k1", "k2"]) x, dx = np.linspace(0, 10, 50, retstep=True, endpoint=False) u = np.cos(x * 2 * np.pi / 10) v = np.sin(x * 2 * np.pi / 10) fields = model.fields_template(x=x, u=u, v=v) parameters = dict(periodic=True, k1=1, k2=1) F = model.F(fields, parameters) J_sparse = model.J(fields, parameters) J_dense = model.J(fields, parameters, sparse=False) J_approx = model.F.diff_approx(fields, parameters, eps=1E-3) dxu = np.gradient(np.pad(u, 2, mode="wrap")) / dx dxxu = np.gradient(dxu) / dx dxu = dxu[2:-2] dxxu = dxxu[2:-2] dxv = np.gradient(np.pad(v, 2, mode="wrap")) / dx dxxv = np.gradient(dxv) / dx dxv = dxv[2:-2] dxxv = dxxv[2:-2] assert np.isclose(F, np.vstack([dxxv, dxxu]).flatten("F"), rtol=1E-2).all() assert np.isclose(J_approx, J_sparse.todense(), rtol=1E-4).all() assert np.isclose(J_approx, J_dense, rtol=1E-4).all()
import numpy as np import pylab as pl from triflow import Model, Simulation model = Model("k * dxxU - c * dxU", "U", ["k", "c"]) x, dx = np.linspace(0, 1, 200, retstep=True) U = np.cos(2 * np.pi * x * 5) fields = model.fields_template(x=x, U=U) 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,
#!/usr/bin/env python # coding=utf8 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:
def heat_model(): model = Model(differential_equations="k * dxxT", dependent_variables="T", parameters="k") return model
def model(): model = Model(differential_equations=["dxxU1", "dxxU2"], dependent_variables=["U1", "U2"], help_functions='s') return model