Read about our upcoming Code of Conduct on this issue

run.py 2.11 KB
Newer Older
1
#!/usr/bin/env python
2
3
4
5
6
7
8
9
10
11
12
13
14
"""Simulate Burgers equation with a sine-wave as an initial condition.

Notes
-----
- A stark difference in the final energy is observed between the convective and
  skew symmetric Burgers solver is evident a nx=101 is chosen.
- This contrast is less evident if nx=64, 128 etc. which is indeed puzzling.
- As suggested in the reference below the foolproof solution is to apply
  dealiasing which guarantees energy conservation.

.. seealso: https://kth-nek5000.github.io/kthNekBook/_notebooks/burgers.html

"""
15
16
17
18
19
import click
import numpy as np
import matplotlib.pyplot as plt


20
def solve(Simul, nx, coef_dealiasing):
21
22
23
24
25
26
    params = Simul.create_default_params()
    params.output.sub_directory = "bench_skew_sym"

    params.output.periods_save.phys_fields = 1.0
    params.short_name_type_run = f"test_coef_dealias={coef_dealiasing:.2f}"
    params.oper.Lx = 2 * np.pi
27
    params.oper.nx = nx
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
    params.oper.coef_dealiasing = coef_dealiasing
    params.nu_2 = 0.0
    params.time_stepping.USE_CFL = False
    params.time_stepping.deltat0 = 1e-2
    params.time_stepping.t_end = 1.0
    params.time_stepping.type_time_scheme = "RK2"
    params.init_fields.type = "in_script"

    params.output.periods_print.print_stdout = 0.5

    sim = Simul(params)

    # Initialize
    x = sim.oper.x
    Lx = sim.oper.Lx

    u = np.sin(x)
    u_fft = sim.oper.fft(u)

    sim.state.init_statephys_from(u=u)
    sim.state.init_statespect_from(u_fft=u_fft)

    # Plot once
    sim.output.init_with_initialized_state()
    sim.output.phys_fields.plot(field="u")
    ax = plt.gca()

    sim.time_stepping.start()

    ax.plot(x, sim.state.state_phys.get_var("u"))
    plt.show()


@click.command()
@click.option("--solver", type=click.Choice(["conv", "skew_sym"]))
63
@click.option("--nx", type=int, default=101)
64
@click.option("--dealias/--no-dealias", type=bool, default=False)
65
def run(solver, nx, dealias):
66
67
68
69
70
    if solver == "conv":
        from fluidsim.solvers.burgers1d.solver import Simul
    else:
        from fluidsim.solvers.burgers1d.skew_sym.solver import Simul

71
    solve(Simul, nx, 2./3 if dealias else 1.0)
72
73
74
75


if __name__ == "__main__":
    run()