class Test_limiter_conv(): mesh100 = mesh.unimesh(ncell=100, length=1.) mesh50 = mesh.unimesh(ncell=50, length=1.) mymodel = conv.model(1.) # periodic wave def init_sinperk(self, mesh, k): return np.sin(2 * k * np.pi / mesh.length * mesh.centers()) def test_0_tvd(self, limiter): slope = limiter(-2., 1.) # tvd: 0 if opposite signs assert slope == 0. slope = limiter(3., 3.) # consistency assert slope == 3. slope = limiter(1., 10.) # tvd: max is double of smallest assert slope <= 2. def test_muscl_limiters(self, limiter): curmesh = self.mesh50 endtime = 5 cfl = .8 # extrapol1(), extrapol2()=extrapolk(1), centered=extrapolk(-1), extrapol3=extrapolk(1./3.) xnum = muscl(limiter) finit = field.fdata(self.mymodel, curmesh, [self.init_sinperk(curmesh, k=4)]) rhs = modeldisc.fvm(self.mymodel, curmesh, xnum) solver = rk3ssp(curmesh, rhs) fsol = solver.solve(finit, cfl, [endtime]) assert not fsol[-1].isnan() avg, var = fsol[-1].stats('q') #varref = { } assert avg == pytest.approx(0., abs=1.e-12)
class integration_data(): curmesh = mesh.unimesh(ncell=50, length=1.) convmodel = conv.model(convcoef=1.) def init_sinperk(self, mesh, k): return np.sin(2 * k * np.pi / mesh.length * mesh.centers())
class monitor_data(): mesh50 = mesh.unimesh(ncell=50, length=1.) convmodel = conv.model(convcoef=1.) eulermodel = euler.model() xsch = xnum.extrapol3() def init_sinperk(self, mesh, k): return np.sin(2 * k * np.pi / mesh.length * mesh.centers())
class Test_fdataclass_scalar(): convmodel = conv.model(convcoef=1.) curmesh = mesh.unimesh(ncell=50, length=1.) def test_init_empty(self): f = field.fdata(self.convmodel, self.curmesh, []) assert f.time == 0. # default value assert f.it == -1 # default value assert f.data == [] f.set_time(10.) assert f.time == 10. def test_reset(self): f = field.fdata(self.convmodel, self.curmesh, []) f.set_time(10.) f.reset(t=5.) assert f.time == 5. assert f.it == -1 # default value f.reset(it=20) assert f.time == 0. # default value assert f.it == 20 def test_init_expand(self): f = field.fdata(self.convmodel, self.curmesh, [1.]) assert f.time == 0. # default value assert np.size(f.data[0]) == self.curmesh.ncell assert np.average(f.data[0]) == 1. @pytest.mark.mpl_image_compare def test_plotdata_sca(self): def fn(x): return np.exp(-2 * np.square(x - .5)) * np.sin(20 * (x - .5)) f = field.fdata(self.convmodel, self.curmesh, [fn(self.curmesh.centers())]) fig, ax = plt.subplots(1, 1) f.plot('q') return fig
import pytest # import numpy as np import flowdyn.mesh as mesh import flowdyn.modelphy.convection as conv import flowdyn.modeldisc as modeldisc import flowdyn.field as field from flowdyn.xnum import * from flowdyn.integration import * mesh100 = mesh.unimesh(ncell=100, length=1.) mesh50 = mesh.unimesh(ncell=50, length=1.) mymodel = conv.model(1.) # periodic wave def init_sinperk(mesh, k): return np.sin(2*k*np.pi/mesh.length*mesh.centers()) def test_mesh(): endtime = 5 cfl = .5 # extrapol1(), extrapol2()=extrapolk(1), centered=extrapolk(-1), extrapol3=extrapolk(1./3.) xnum = extrapol1() # explicit, rk2, rk3ssp, rk4, implicit, trapezoidal=cranknicolson tnum = explicit for curmesh in [ mesh50, mesh100]: finit = field.fdata(mymodel, curmesh, [ init_sinperk(curmesh, k=2) ] ) rhs = modeldisc.fvm(mymodel, curmesh, xnum) solver = tnum(curmesh, rhs) fsol = solver.solve(finit, cfl, [endtime])
""" import time from pylab import * from flowdyn.mesh import * import flowdyn.modelphy.convection as convection import flowdyn.modeldisc as modeldisc from flowdyn.field import * from flowdyn.xnum import * from flowdyn.integration import * mesh100 = unimesh(ncell=100, length=1.) mesh50 = unimesh(ncell=50, length=1.) mymodel = convection.model(1.) # TODO : make init method for scafield # sinus packet def init_sinpack(mesh): return sin(2 * 2 * pi / mesh.length * mesh.centers()) * ( 1 + sign(-(mesh.centers() / mesh.length - .25) * (mesh.centers() / mesh.length - .75))) / 2 # periodic wave def init_sinper(mesh): k = 2 # nombre d'onde return sin(2 * k * pi / mesh.length * mesh.centers())
class Test_fieldlistclass(): convmodel = conv.model(convcoef=1.) curmesh = mesh.unimesh(ncell=50, length=1.) def test_append_scalararray(self): flist = field.fieldlist() f1 = field.fdata(self.convmodel, self.curmesh, [1.]) flist.append(f1) f2 = f1.copy() f2.set_time(10.) flist.append(f2) assert len(flist) == 2 assert flist[0].time == 0. assert flist[-1].time == 10. assert flist.time_array() == [0., 10.] def test_extend_scalararray(self): flist = field.fieldlist() f1 = field.fdata(self.convmodel, self.curmesh, [1.]) flist.append(f1) f2 = f1.copy() f2.set_time(10.) flist.append(f2) newlist = field.fieldlist() f3 = f2.copy() newlist.append(f3) f3.set_time(20.) newlist.append(f2) flist.extend(newlist) f2.reset(t=100., it=5) assert len(flist) == 4 # f1, f2, f3, f2 assert flist.time_array() == [0., 100., 20., 100.] assert flist.it_array() == [-1, 5, -1, 5] @pytest.mark.mpl_image_compare def test_flist_plotxtcontour(self): def fn(x, t): return np.exp(-2 * np.square(x - .5 + .2 * np.sin(10 * t))) times = np.linspace(0., 5., 11, endpoint=True) flist = field.fieldlist() for t in times: f = field.fdata(self.convmodel, self.curmesh, [fn(self.curmesh.centers(), t)]) f.set_time(t) flist.append(f) fig, ax = plt.subplots(1, 1) flist.xtcontour('q') return fig @pytest.mark.mpl_image_compare def test_flist_plotxtcontourf(self): def fn(x, t): return np.exp(-2 * np.square(x - .5 + .2 * np.sin(10 * t))) times = np.linspace(0., 5., 11, endpoint=True) flist = field.fieldlist() for t in times: f = field.fdata(self.convmodel, self.curmesh, [fn(self.curmesh.centers(), t)]) f.set_time(t) flist.append(f) fig, ax = plt.subplots(1, 1) flist.xtcontourf('q') return fig
import numpy as np import flowdyn.mesh as mesh import flowdyn.modelphy.convection as conv import flowdyn.modeldisc as modeldisc import flowdyn.field as field import flowdyn.xnum as xnum import flowdyn.integration as tnum curmesh = mesh.unimesh(ncell=50, length=1.) convmodel = conv.model(convcoef=1.) def init_sinperk(mesh, k): return np.sin(2*k*np.pi/mesh.length*mesh.centers()) xsch = xnum.extrapol3() tottime = 10. breaktime = 5. cfl = .5 finit = field.fdata(convmodel, curmesh, [ init_sinperk(curmesh, k=4) ] ) rhs = modeldisc.fvm(convmodel, curmesh, xsch) solver = tnum.rk4(curmesh, rhs) nsol = 10+1 # every second tsave = np.linspace(0, tottime, nsol, endpoint=True) # stop_directive = { 'tottime': breaktime } fsol0 = solver.solve(finit, cfl, tsave, stop=stop_directive) assert len(fsol0) < nsol # end before expected by tsave assert not fsol0[-1].isnan() assert fsol0[-1].time == breaktime