burgers.pyΒΆ

In this script we solve the Burgers equation on a 1D or 2D periodic domain, starting from a centered Gaussian and convecting in the positive direction of the first coordinate.

 7from nutils import mesh, function, solver, export, cli, testing
 8import numpy, treelog
 9
10def main(nelems:int, ndims:int, degree:int, timescale:float, newtontol:float, endtime:float):
11  '''
12  Burgers equation on a 1D or 2D periodic domain.
13
14  .. arguments::
15
16     nelems [20]
17       Number of elements along a single dimension.
18     ndims [1]
19       Number of spatial dimensions.
20     degree [1]
21       Polynomial degree for discontinuous basis functions.
22     timescale [.5]
23       Fraction of timestep and element size: timestep=timescale/nelems.
24     newtontol [1e-5]
25       Newton tolerance.
26     endtime [inf]
27       Stopping time.
28  '''
29
30  domain, geom = mesh.rectilinear([numpy.linspace(0,1,nelems+1)]*ndims, periodic=range(ndims))
31
32  ns = function.Namespace()
33  ns.x = geom
34  ns.basis = domain.basis('discont', degree=degree)
35  ns.u = 'basis_n ?lhs_n'
36  ns.f = '.5 u^2'
37  ns.C = 1
38
39  res = domain.integral('-basis_n,0 f d:x' @ ns, degree=5)
40  res += domain.interfaces.integral('-[basis_n] n_0 ({f} - .5 C [u] n_0) d:x' @ ns, degree=degree*2)
41  inertia = domain.integral('basis_n u d:x' @ ns, degree=5)
42
43  sqr = domain.integral('(u - exp(-?y_i ?y_i)(y_i = 5 (x_i - 0.5_i)))^2 d:x' @ ns, degree=5)
44  lhs0 = solver.optimize('lhs', sqr)
45
46  timestep = timescale/nelems
47  bezier = domain.sample('bezier', 7)
48  with treelog.iter.plain('timestep', solver.impliciteuler('lhs', res, inertia, timestep=timestep, lhs0=lhs0, newtontol=newtontol)) as steps:
49    for itime, lhs in enumerate(steps):
50      x, u = bezier.eval(['x_i', 'u'] @ ns, lhs=lhs)
51      export.triplot('solution.png', x, u, tri=bezier.tri, hull=bezier.hull, clim=(0,1))
52      if itime * timestep >= endtime:
53        break
54
55  return lhs

If the script is executed (as opposed to imported), nutils.cli.run() calls the main function with arguments provided from the command line. For example, to simulate until 0.5 seconds run python3 burgers.py endtime=0.5 (view log).

62if __name__ == '__main__':
63  cli.run(main)

Once a simulation is developed and tested, it is good practice to save a few strategic return values for regression testing. The nutils.testing module, which builds on the standard unittest framework, facilitates this by providing nutils.testing.TestCase.assertAlmostEqual64() for the embedding of desired results as compressed base64 data.

71class test(testing.TestCase):
72
73  @testing.requires('matplotlib')
74  def test_1d_p0(self):
75    lhs = main(ndims=1, nelems=10, timescale=.1, degree=0, endtime=.01, newtontol=1e-5)
76    self.assertAlmostEqual64(lhs, '''
77      eNrz1ttqGGOiZSZlrmbuZdZgcsEwUg8AOqwFug==''')
78
79  @testing.requires('matplotlib')
80  def test_1d_p1(self):
81    lhs = main(ndims=1, nelems=10, timescale=.1, degree=1, endtime=.01, newtontol=1e-5)
82    self.assertAlmostEqual64(lhs, '''
83      eNrbocann6u3yqjTyMLUwfSw2TWzKPNM8+9mH8wyTMNNZxptMirW49ffpwYAI6cOVA==''')
84
85  @testing.requires('matplotlib')
86  def test_1d_p2(self):
87    lhs = main(ndims=1, nelems=10, timescale=.1, degree=2, endtime=.01, newtontol=1e-5)
88    self.assertAlmostEqual64(lhs, '''
89      eNrr0c7SrtWfrD/d4JHRE6Ofxj6mnqaKZofNDpjZmQeYB5pHmL8we23mb5ZvWmjKY/LV6KPRFIMZ+o36
90      8dp92gCxZxZG''')
91
92  @testing.requires('matplotlib')
93  def test_2d_p1(self):
94    lhs = main(ndims=2, nelems=4, timescale=.1, degree=1, endtime=.01, newtontol=1e-5)
95    self.assertAlmostEqual64(lhs, '''
96      eNoNyKENhEAQRuGEQsCv2SEzyQZHDbRACdsDJNsBjqBxSBxBHIgJ9xsqQJ1Drro1L1/eYBZceGz8njrR
97      yacm8UQLBvPYCw1airpyUVYSJLhKijK4IC01WDnqqxvX8OTl427aU73sctPGr3qqceBnRzOjo0xy9JpJ
98      R73m6R6YMZo/Q+FCLQ==''')