def compute_sol(self, stop, monitors): rhs = modeldisc.fvm(self.eulermodel, self.mesh50, self.xsch, bcL=self.bcL, bcR=self.bcR) finit = rhs.fdata_fromprim([1., 0., 1.]) # rho, u, p solver = tnum.rk4(self.mesh50, rhs) fsol = solver.solve(finit, self.cfl, stop=stop, monitors=monitors) return fsol
def test_numscheme(): curmesh = mesh50 endtime = 1 cfl = .5 tnum = rk3ssp for xnum in [ extrapol1(), extrapol2(), extrapol3(), muscl(minmod), muscl(vanalbada) ]: finit = field.fdata(mymodel, curmesh, [ init_sinperk(curmesh, k=5) ] ) rhs = modeldisc.fvm(mymodel, curmesh, xnum) solver = tnum(curmesh, rhs) solver.solve(finit, cfl, [endtime]) assert 1
def test_wavelength(k): curmesh = mesh50 endtime = 5 cfl = .8 # extrapol1(), extrapol2()=extrapolk(1), centered=extrapolk(-1), extrapol3=extrapolk(1./3.) xnum = extrapol1() # explicit, rk2, rk3ssp, rk4, implicit, trapezoidal=cranknicolson tnum = explicit finit = field.fdata(mymodel, curmesh, [ init_sinperk(curmesh, k=k) ] ) rhs = modeldisc.fvm(mymodel, curmesh, xnum) solver = tnum(curmesh, rhs) fsol = solver.solve(finit, cfl, [endtime]) assert not fsol[-1].isnan()
def test_integrators(tnum): curmesh = mesh50 endtime = 1 cfl = .8 # extrapol1(), extrapol2()=extrapolk(1), centered=extrapolk(-1), extrapol3=extrapolk(1./3.) xnum = extrapol1() # explicit, rk2, rk3ssp, rk4, implicit, trapezoidal=cranknicolson 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]) assert not fsol[-1].isnan() assert fsol[-1].average('q') < 1.e-12
def test_checkend_maxit_noendtime(self): endtime = 5. cfl = .5 maxit = 100 stop_directive = {'maxit': maxit} finit = field.fdata(self.convmodel, self.curmesh, [self.init_sinperk(self.curmesh, k=4)]) rhs = modeldisc.fvm(self.convmodel, self.curmesh, self.xsch) solver = tnum.rk4(self.curmesh, rhs) fsol = solver.solve(finit, cfl, stop=stop_directive) assert len(fsol) == 1 # end before expected by tsave assert not fsol[-1].isnan() assert solver.nit() == 100
def test_mesh_refined(lratio): lmesh = mesh.refinedmesh(ncell=50, length=1., ratio=2., nratioa=lratio) endtime = 5 cfl = 1. # extrapol1(), extrapol2()=extrapolk(1), centered=extrapolk(-1), extrapol3=extrapolk(1./3.) xnum = extrapol3() # explicit, rk2, rk3ssp, rk4, implicit, trapezoidal=cranknicolson tnum = rk3ssp finit = field.fdata(mymodel, lmesh, [init_sinperk(lmesh, k=2)]) rhs = modeldisc.fvm(mymodel, lmesh, xnum) solver = tnum(lmesh, rhs) fsol = solver.solve(finit, cfl, [endtime]) assert not fsol[-1].isnan()
def test_integrators(): curmesh = mesh50 endtime = 1 cfl = .8 # extrapol1(), extrapol2()=extrapolk(1), centered=extrapolk(-1), extrapol3=extrapolk(1./3.) xnum = extrapol1() # explicit, rk2, rk3ssp, rk4, implicit, trapezoidal=cranknicolson for tnum in [ explicit,rk2, rk3ssp, implicit, cranknicolson ]: finit = field.fdata(mymodel, curmesh, [ init_sinperk(curmesh, k=2) ] ) rhs = modeldisc.fvm(mymodel, curmesh, xnum) solver = tnum(curmesh, rhs) solver.solve(finit, cfl, [endtime]) assert 1
def test_checkend_tottime(self): endtime = 5. cfl = .5 stop_directive = {'tottime': endtime} finit = field.fdata(self.convmodel, self.curmesh, [self.init_sinperk(self.curmesh, k=4)]) rhs = modeldisc.fvm(self.convmodel, self.curmesh, self.xsch) solver = tnum.rk4(self.curmesh, rhs) nsol = 11 tsave = np.linspace(0, 2 * endtime, nsol, endpoint=True) fsol = solver.solve(finit, cfl, tsave, stop=stop_directive) assert len(fsol) < nsol # end before expected by tsave assert not fsol[-1].isnan() assert fsol[-1].time < 2 * endtime
def test_compression_centered(): endtime = 2. cfl = .5 xnum = muscl(minmod) tnum = integ.rk3ssp thismesh = mesh100 thisprim = [init_step(thismesh, 2, -1.)] thiscons = field.fdata(mymodel, thismesh, thisprim) bcL = { 'type': 'dirichlet', 'prim': thisprim[0] } bcR = { 'type': 'dirichlet', 'prim': thisprim[-1] } rhs = modeldisc.fvm(mymodel, thismesh, xnum, bcL=bcL, bcR=bcR) solver = tnum(thismesh, rhs) solver.solve(thiscons, cfl, [endtime]) assert 1
def test_sin_bcper(): endtime = 2. cfl = 0.5 xnum = muscl(minmod) tnum = integ.rk3ssp thismesh = mesh100 thisprim = [ init_sin(thismesh) ] thiscons = field.fdata(mymodel, thismesh, thisprim) bcL = { 'type': 'per' } bcR = { 'type': 'per' } rhs = modeldisc.fvm(mymodel, thismesh, xnum, bcL=bcL, bcR=bcR) solver = tnum(thismesh, rhs) solver.solve(thiscons, cfl, [endtime]) assert 1
def test_conv_it_perf(self, tmeth): endtime = 5 cfl = .8 xsch = xnum.extrapol3() finit = field.fdata(self.convmodel, self.curmesh, [self.init_sinperk(self.curmesh, k=4)]) rhs = modeldisc.fvm(self.convmodel, self.curmesh, xsch) solver = tmeth(self.curmesh, rhs) fsol = solver.solve(finit, cfl, [endtime]) assert solver.nit() > 300 assert solver.nit() < 400 maxperf = 40. if tmeth in tnum.List_Explicit_Integrators else 100. assert solver.perf_micros() < maxperf assert not fsol[-1].isnan()
def test_acousticpacket_sym(flux): endtime = 2. cfl = 0.6 xnum = muscl(minmod) tnum = integ.rk3ssp meshsim = mesh100 xc = meshsim.centers() bcL = {'type': 'sym'} bcR = {'type': 'sym'} rhs = modeldisc.fvm(model, meshsim, xnum, numflux=flux, bcL=bcL, bcR=bcR) finit = rhs.fdata(model.prim2cons([frho(xc), fu(xc), fp(xc)])) # rho, u, p solver = tnum(meshsim, rhs) fsol = solver.solve(finit, cfl, [endtime]) assert not fsol[-1].isnan()
def test_sin_bcper(self): endtime = 2. cfl = 0.5 xnum = muscl(minmod) tnum = integ.rk3ssp thismesh = self.mesh100 thisprim = [ self.init_sin(thismesh) ] thiscons = field.fdata(self.mymodel, thismesh, thisprim) bcL = { 'type': 'per' } bcR = { 'type': 'per' } rhs = modeldisc.fvm(self.mymodel, thismesh, xnum, bcL=bcL, bcR=bcR) solver = tnum(thismesh, rhs) fsol = solver.solve(thiscons, cfl, [endtime]) assert not fsol[-1].isnan()
def test_expansion(self): endtime = 2. cfl = 0.5 xnum = muscl(minmod) tnum = integ.rk3ssp thismesh = self.mesh100 thisprim = [self.init_step(thismesh, 1., 2.)] thiscons = field.fdata(self.mymodel, thismesh, thisprim) bcL = { 'type': 'dirichlet', 'prim': thisprim[0] } bcR = { 'type': 'dirichlet', 'prim': thisprim[-1] } rhs = modeldisc.fvm(self.mymodel, thismesh, xnum, bcL=bcL, bcR=bcR) solver = tnum(thismesh, rhs) fsol = solver.solve(thiscons, cfl, [endtime]) assert not fsol[-1].isnan()
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)
def test_rayleigh(self): endtime = 100. cfl = 1.2 xnum = muscl(minmod) tnum = integ.rk4 meshsim = self.mesh20 modelrayleigh = euler.model(source=[None, None, lambda x, q: 2.]) bcL = {'type': 'insub', 'ptot': 1.4, 'rttot': 1.} bcR = {'type': 'outsub', 'p': 1.} rhs = modeldisc.fvm(modelrayleigh, meshsim, xnum, bcL=bcL, bcR=bcR) finit = rhs.fdata_fromprim([1., 0., 1.]) # rho, u, p solver = tnum(meshsim, rhs) fsol = solver.solve(finit, cfl, [endtime]) assert not fsol[-1].isnan() avg, var = fsol[-1].stats('rttot') assert avg == pytest.approx(1.6139, rel=1.e-4) assert var == pytest.approx(0.1242, rel=1.e-2)
def test_interpol_t(self): endtime = 1. / self.curmesh.ncell cfl = 5. # unstable but no risk with 1 it maxit = 1 stop_directive = {'maxit': maxit} finit = field.fdata(self.convmodel, self.curmesh, [self.init_sinperk(self.curmesh, k=4)]) rhs = modeldisc.fvm(self.convmodel, self.curmesh, self.xsch) solver = tnum.explicit(self.curmesh, rhs) fsol2 = solver.solve(finit, cfl, [2 * endtime], stop=stop_directive) fsol1 = solver.solve(finit, cfl, [endtime], stop=stop_directive) assert not fsol2[-1].isnan() assert 2 * fsol1[-1].time == pytest.approx(fsol2[-1].time, abs=1.e-12) fi = finit.interpol_t(fsol2[-1], endtime) diff = fi.diff(fsol1[-1]) for d in diff.data: assert np.average(np.abs(d)) == pytest.approx(0., abs=1.e-6)
def test_flow_sub(self, bcin, bcout): endtime = 100. cfl = 1.2 xnum = muscl(minmod) tnum = integ.rk4 meshsim = self.mesh20 bcL = {'type': bcin, 'ptot': 1.4, 'rttot': 1.} bcR = {'type': bcout, 'p': 1.} rhs = modeldisc.fvm(self.model, meshsim, xnum, bcL=bcL, bcR=bcR) finit = rhs.fdata_fromprim([1., 0., 1.]) # rho, u, p solver = tnum(meshsim, rhs) fsol = solver.solve(finit, cfl, [endtime]) assert not fsol[-1].isnan() mach_th = np.sqrt(((bcL['ptot'] / bcR['p'])**(1. / 3.5) - 1.) / .2) error = np.sqrt( np.sum((fsol[-1].phydata('mach') - mach_th)**2) / meshsim.ncell) / mach_th assert error < 1.e-8
def test_shocktube_sodsup(flux): model = euler.model() sod = solR.Sod_supersonic(model) # sol.Sod_supersonic(model) # bcL = { 'type': 'dirichlet', 'prim': sod.bcL() } bcR = { 'type': 'dirichlet', 'prim': sod.bcR() } xnum = muscl(minmod) # rhs = modeldisc.fvm(model, meshref, xnum, numflux=flux, bcL=bcL, bcR=bcR) solver = integ.rk3ssp(meshref, rhs) # computation # endtime = 2. cfl = 1. finit = sod.fdata(meshref) fsol = solver.solve(finit, cfl, [endtime]) fref = sod.fdata(meshref, endtime) # for name in ['density', 'pressure', 'mach']: error = np.sqrt(np.sum((fsol[-1].phydata(name)-fref.phydata(name))**2))/np.sum(np.abs(fref.phydata(name))) assert error < 5.e-3
def test_sym_flux(flux): endtime = 1. cfl = 0.6 xnum = muscl(minmod) tnum = integ.rk3ssp meshsim = mesh.unimesh(ncell=100, length=1.) xc = meshsim.centers() bcL = { 'type': 'sym'} bcR = { 'type': 'sym'} model = shw.shallowwater1d() model10 = shw.shallowwater1d(g=10.) rhs = modeldisc.fvm(model, meshsim, xnum, numflux=flux, bcL=bcL, bcR=bcR) finit = rhs.fdata(model.prim2cons(waterdrop(xc, .01))) # rho, u, p solver = tnum(meshsim, rhs) fsol = solver.solve(finit, cfl, [endtime]) assert not fsol[-1].isnan() avg, var = fsol[-1].stats('height') assert avg == pytest.approx(0.098232, rel=1.e-4) assert var >= 6.5e-6
def test_interpolation_regression(self): endtime = 5 cfl = .5 finit = field.fdata(self.convmodel, self.curmesh, [self.init_sinperk(self.curmesh, k=4)]) rhs = modeldisc.fvm(self.convmodel, self.curmesh, self.xsch) solver = tnum.rk4(self.curmesh, rhs) nsol = 5 tsave = np.linspace(0, endtime, nsol, endpoint=True) fsol = solver.solve(finit, cfl, tsave) fleg = solver.solve_legacy(finit, cfl, tsave) assert len(fsol) == nsol assert len(fleg) == nsol for fs, fl in zip(fsol, fleg): assert not fs.isnan() diff = fs.diff(fl) assert diff.time == pytest.approx(0., abs=1.e-12) for d in diff.data: assert np.average(np.abs(d)) == pytest.approx(0., abs=1.e-6)
def test_frequency(self, montype): endtime = 5. cfl = .5 finit = field.fdata(self.convmodel, self.mesh50, [self.init_sinperk(self.mesh50, k=4)]) rhs = modeldisc.fvm(self.convmodel, self.mesh50, self.xsch) maxit = 500 stop_directive = {'maxit': maxit} solver = tnum.rk4(self.mesh50, rhs) mondict = {} for f in 1, 2, 10, 50, 100: monitors = {montype: {'frequency': f}} fsol = solver.solve(finit, cfl, [endtime], stop=stop_directive, monitors=monitors) mondict[f] = monitors[montype]['output'] assert not fsol[-1].isnan() assert mondict[f]._it[-1] == maxit assert (len(mondict[f]._it) - 1) * f == maxit
def test_restart(self): tottime = 10. breaktime = 5. cfl = .5 finit = field.fdata(self.convmodel, self.curmesh, [self.init_sinperk(self.curmesh, k=4)]) rhs = modeldisc.fvm(self.convmodel, self.curmesh, self.xsch) solver = tnum.rk4(self.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) == 6 # end before expected by tsave assert not fsol0[-1].isnan() assert fsol0[-1].time == breaktime # fsol = solver.restart(fsol0[-1], cfl, tsave) assert len(fsol) == 6 # only last snapshots ; time 5. is saved again assert not fsol[-1].isnan() assert fsol[-1].time == tottime
def test_acousticpacket_bcout(self, bcout): endtime = 2. cfl = 0.6 xnum = muscl(minmod) tnum = integ.rk3ssp meshsim = self.mesh100 xc = meshsim.centers() bcL = {'type': 'sym'} bcR = {'type': bcout, 'p': 1.} # 'p' will be ignored if unused rhs = modeldisc.fvm(self.model, meshsim, xnum, numflux='hllc', bcL=bcL, bcR=bcR) finit = rhs.fdata( self.model.prim2cons([self.frho(xc), self.fu(xc), self.fp(xc)])) # rho, u, p solver = tnum(meshsim, rhs) fsol = solver.solve(finit, cfl, [endtime]) assert not fsol[-1].isnan()
def test_nozzle(NPR): def S(x): # section law, throat is at x=5 return 1. - .5 * np.exp(-.5 * (x - 5.)**2) model = euler.nozzle(sectionlaw=S) meshsim = mesh.unimesh(ncell=50, length=10.) nozz = solN.nozzle(model, S(meshsim.centers()), NPR=NPR) fref = nozz.fdata(meshsim) # BC bcL = {'type': 'insub', 'ptot': NPR, 'rttot': 1.} bcR = {'type': 'outsub', 'p': 1.} # rhs = modeldisc.fvm(model, meshsim, muscl(vanleer), bcL=bcL, bcR=bcR) solver = integ.rk3ssp(meshsim, rhs) # computation endtime = 100. cfl = .8 finit = rhs.fdata_fromprim([1., 0.1, 1.]) # rho, u, p fsol = solver.solve(finit, cfl, [endtime]) # error error = np.sqrt( np.sum((fsol[-1].phydata('mach') - fref.phydata('mach'))**2)) / np.sum( np.abs(fref.phydata('mach'))) assert error < 5.e-2
def test_variables(self): bcL = {'type': 'insub', 'ptot': 1.4, 'rttot': 1.} bcR = {'type': 'outsub', 'p': 1.} meshsim = self.mesh100 rhs = modeldisc.fvm(self.model, meshsim, extrapol1(), bcL=bcL, bcR=bcR) xc = meshsim.centers() finit = rhs.fdata_fromprim([self.frho(xc), self.fu(xc), self.fp(xc)]) # rho, u, p data = finit.phydata('asound') * finit.phydata('mach') - finit.phydata( 'velocity') error = np.sum(np.abs(data)) assert error < 1.e-12 data = finit.phydata('velocity') * finit.phydata( 'density') - finit.phydata('massflow') error = np.sum(np.abs(data)) assert error < 1.e-12 data = finit.phydata('htot') - finit.phydata( 'enthalpy') - finit.phydata('kinetic-energy') - finit.phydata( 'massflow') error = np.sum(np.abs(data)) # entropy should be constant (variance = 0) __, error = finit.stats('entropy') assert error < 1.e-12
def init_rupturebarrage(mesh): h0_vect = np.zeros(len(mesh.centers())) + 0.5 for i in range(len(mesh.centers())): if mesh.centers()[i] < 1: h0_vect[i] = 1 return h0_vect #h0_vect = init_rupturebarrage(mesh) u0_vect = .5 + np.zeros((ncell)) w_init = [h0_vect, u0_vect * h0_vect] # Define RHS rhs = modeldisc.fvm(model, mesh, xmeth, numflux=numflux, bcL=bcL, bcR=bcR) field0 = rhs.fdata_fromprim(w_init) # Define the solver solver = tmeth(mesh, rhs) # Solution w_sol = solver.solve(field0, cfl, tsave) # VERIFIED : w_sol contains solutions in conservative variables [h,q=h*u] solver.show_perf() fig2, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 7)) ax1.set_xlabel('$x$'), ax1.set_ylabel('$h(t,x)$'), ax1.set_title( 'Hauteur de fluide au temps t = ' + str(endtime) + '\n (CFL ' + str(cfl) + ', flux : ' + numflux + ', ' + xmethstr + ', ' + tmethstr + ')') ax1.set(xlim=(-0.1, mesh.centers()[-1] + 0.1), ylim=(-0.0, .2)) ax1.grid(linestyle='--', color='0.5') line1, = ax1.plot(mesh.centers(), w_sol[-1].data[0])
import flowdyn.integration as integ #meshsim = mesh.unimesh(ncell=200, length=10., x0=-4.) #meshref = mesh.unimesh(ncell=1000, length=10., x0=-4.) @pytest.mark.parametrize("case, endtime", [(solR.Sod_subsonic, 2.8), (solR.Sod_supersonic, 2.)]) def test_shocktube(case, endtime): meshsim = mesh.unimesh(ncell=200, length=10., x0=-4.) model = euler.euler1d() sod = case(model) # sol.Sod_supersonic(model) # bcL = {'type': 'dirichlet', 'prim': sod.bcL()} bcR = {'type': 'dirichlet', 'prim': sod.bcR()} xnum = muscl(minmod) # rhs = modeldisc.fvm(model, meshsim, xnum, numflux='hllc', bcL=bcL, bcR=bcR) solver = integ.rk3ssp(meshsim, rhs) # computation # cfl = 1. finit = sod.fdata(meshsim) fsol = solver.solve(finit, cfl, [endtime]) fref = sod.fdata(meshsim, endtime) # for name in ['density', 'pressure', 'mach']: error = np.sqrt( np.sum((fsol[-1].phydata(name) - fref.phydata(name))**2)) / np.sum( np.abs(fref.phydata(name))) assert error < 1.e-2
import flowdyn.modeldisc as modeldisc import flowdyn.solution.euler_riemann as sol meshsim = unimesh(ncell=200, length=10., x0=-4.) meshref = unimesh(ncell=1000, length=10., x0=-4.) model1 = euler.model() model2 = euler.model() sod = sol.Sod_subsonic(model1) # sol.Sod_supersonic(model1) # bcL = { 'type': 'dirichlet', 'prim': sod.bcL() } bcR = { 'type': 'dirichlet', 'prim': sod.bcR() } xnum1 = muscl(minmod) # xnum2 = muscl(vanalbada) # rhs1 = modeldisc.fvm(model1, meshsim, xnum1, numflux='hlle', bcL=bcL, bcR=bcR) solver1 = tnum.rk3ssp(meshsim, rhs1) rhs2 = modeldisc.fvm(model2, meshsim, xnum2, numflux='hlle', bcL=bcL, bcR=bcR) solver2 = tnum.rk3ssp(meshsim, rhs2) # computation # endtime = 2.8 # 2.8 for subsonic, 2.8 for supersonic cfl = 1. finit = sod.fdata(meshsim) fsol1 = solver1.solve(finit, cfl, [endtime]) solver1.show_perf() fsol2 = solver2.solve(finit, cfl, [endtime]) solver2.show_perf()
#from flowdyn.field import * from flowdyn.xnum import * import flowdyn.integration as tnum import flowdyn.modelphy.euler as euler import flowdyn.modeldisc as modeldisc #import flowdyn.solution.euler_riemann as sol nx = 50 meshsim = unimesh(ncell=nx, length=10.) model = euler.model() bcL = {'type': 'insub', 'ptot': 1.4, 'rttot': 1.} bcR = {'type': 'outsub', 'p': 1.} rhs = modeldisc.fvm(model, meshsim, muscl(minmod), bcL=bcL, bcR=bcR) solver1 = tnum.rk3ssp(meshsim, rhs) solver2 = tnum.gear(meshsim, rhs) # computation # endtime = 100. cfl1 = .8 cfl2 = 20. finit = rhs.fdata_fromprim([1., 0.1, 1.]) # rho, u, p fsol1 = solver1.solve(finit, cfl1, [endtime]) solver1.show_perf()