Path: blob/main/docs/literate/src/files/first_steps/getting_started.jl
5593 views
#src # Getting started12# Trixi.jl is a numerical simulation framework for conservation laws and3# is written in the [Julia programming language](https://julialang.org/).4# This tutorial is intended for beginners in Julia and Trixi.jl.5# After reading it, you will know how to install Julia and Trixi.jl on your computer,6# and you will be able to download setup files from our GitHub repository, modify them,7# and run simulations.89# The contents of this tutorial:10# - [Julia installation](@ref Julia-installation)11# - [Trixi.jl installation](@ref Trixi.jl-installation)12# - [Running a simulation](@ref Running-a-simulation)13# - [Getting an existing setup file](@ref Getting-an-existing-setup-file)14# - [Modifying an existing setup](@ref Modifying-an-existing-setup)1516# ## Julia installation1718# Trixi.jl is compatible with the latest stable release of Julia. Additional details regarding Julia19# support can be found in the [`README.md`](https://github.com/trixi-framework/Trixi.jl#installation)20# file. After installation, the current default Julia version can be managed through the command21# line tool `juliaup`. You may follow our concise installation guidelines for Windows, Linux, and22# MacOS provided below. In the event of any issues during the installation process, please consult23# the official [Julia installation instruction](https://julialang.org/downloads/).2425# ### Windows2627# - Open a terminal by pressing `Win+r` and entering `cmd` in the opened window.28# - To install Julia, execute the following command in the terminal:29# ```shell30# winget install julia -s msstore31# ```32# Note: For this installation an MS Store account is necessary to proceed.33# - Verify the successful installation of Julia by executing the following command in the terminal:34# ```shell35# julia36# ```37# To exit Julia, execute `exit()` or press `Ctrl+d`.3839# ### Linux and MacOS4041# - To install Julia, run the following command in a terminal:42# ```shell43# curl -fsSL https://install.julialang.org | sh44# ```45# Follow the instructions displayed in the terminal during the installation process.46# - If an error occurs during the execution of the previous command, you may need to install47# `curl`. On Ubuntu-type systems, you can use the following command:48# ```shell49# sudo apt install curl50# ```51# After installing `curl`, repeat the first step once more to proceed with Julia installation.52# - Verify the successful installation of Julia by executing the following command in the terminal:53# ```shell54# julia55# ```56# To exit Julia, execute `exit()` or press `Ctrl+d`.5758# ## Trixi.jl installation5960# Trixi.jl and its related tools are registered Julia packages, thus their installation61# happens inside Julia.62# For a smooth workflow experience with Trixi.jl, you need to install63# [Trixi.jl](https://github.com/trixi-framework/Trixi.jl),64# [Plots.jl](https://github.com/JuliaPlots/Plots.jl), and sub-packages of65# [OrdinaryDiffEq.jl](https://github.com/SciML/OrdinaryDiffEq.jl).6667# - Open a terminal and start Julia.68# - Execute the following commands to install all mentioned packages. Please note that the69# installation process involves downloading and precompiling the source code, which may take70# some time depending on your machine.71# ````julia72# import Pkg73# Pkg.add(["OrdinaryDiffEqLowStorageRK", "OrdinaryDiffEqSSPRK", "Plots", "Trixi"])74# ````75# - On Windows, the firewall may request permission to install packages.7677# Besides Trixi.jl you have now installed additional packages:78# sub-packages of [OrdinaryDiffEq.jl](https://github.com/SciML/OrdinaryDiffEq.jl) provide time79# integration schemes used by Trixi.jl and [Plots.jl](https://github.com/JuliaPlots/Plots.jl)80# can be used to directly visualize Trixi.jl results from the Julia REPL.8182# ## Usage8384# ### Running a simulation8586# To get you started, Trixi.jl has a large set87# of [example setups](https://github.com/trixi-framework/Trixi.jl/tree/main/examples), that can be88# taken as a basis for your future investigations. In Trixi.jl, we call these setup files89# "elixirs", since they contain Julia code that takes parts of Trixi.jl and combines them into90# something new.9192# Any of the examples can be executed using the [`trixi_include`](@ref)93# function. `trixi_include(...)` expects94# a single string argument with a path to a file containing Julia code.95# For convenience, the [`examples_dir`](@ref) function returns a path to the96# [`examples`](https://github.com/trixi-framework/Trixi.jl/tree/main/examples)97# folder, which has been locally downloaded while installing Trixi.jl.98# `joinpath(...)` can be used to join path components into a full path.99100# Let's execute a short two-dimensional problem setup. It approximates the solution of101# the compressible Euler equations in 2D for an ideal gas ([`CompressibleEulerEquations2D`](@ref))102# with a weak blast wave as the initial condition and periodic boundary conditions.103104# The compressible Euler equations in two spatial dimensions are given by105# ```math106# \frac{\partial}{\partial t}107# \begin{pmatrix}108# \rho \\ \rho v_1 \\ \rho v_2 \\ \rho e_{\text{total}}109# \end{pmatrix}110# +111# \frac{\partial}{\partial x}112# \begin{pmatrix}113# \rho v_1 \\ \rho v_1^2 + p \\ \rho v_1 v_2 \\ (\rho e_{\text{total}} + p) v_1114# \end{pmatrix}115# +116# \frac{\partial}{\partial y}117# \begin{pmatrix}118# \rho v_2 \\ \rho v_1 v_2 \\ \rho v_2^2 + p \\ (\rho e_{\text{total}} + p) v_2119# \end{pmatrix}120# =121# \begin{pmatrix}122# 0 \\ 0 \\ 0 \\ 0123# \end{pmatrix},124# ```125# for an ideal gas with the specific heat ratio ``\gamma``.126# Here, ``\rho`` is the density, ``v_1`` and ``v_2`` are the velocities, ``e_{\text{total}}`` is the specific127# total energy, and128# ```math129# p = (\gamma - 1) \left( \rho e_{\text{total}} - \frac{1}{2} \rho (v_1^2 + v_2^2) \right)130# ```131# is the pressure.132133# The [`initial_condition_weak_blast_wave`](@ref) is specified in134# [`compressible_euler_2d.jl`](https://github.com/trixi-framework/Trixi.jl/blob/main/src/equations/compressible_euler_2d.jl)135136# Start Julia in a terminal and execute the following code:137138# ````julia139# using Trixi, OrdinaryDiffEq140# trixi_include(joinpath(examples_dir(), "tree_2d_dgsem", "elixir_euler_ec.jl"))141# ````142using Trixi, OrdinaryDiffEqLowStorageRK #hide #md143trixi_include(@__MODULE__, joinpath(examples_dir(), "tree_2d_dgsem", "elixir_euler_ec.jl")); #hide #md144145# The output contains a recap of the setup and various information about the course of the simulation.146# For instance, the solution was approximated over the [`TreeMesh`](@ref) with 1024 effective cells using147# the `CarpenterKennedy2N54` ODE148# solver. Further details about the ODE solver can be found in the149# [documentation of OrdinaryDiffEq.jl](https://docs.sciml.ai/DiffEqDocs/stable/solvers/ode_solve/#Low-Storage-Methods)150151# To analyze the result of the computation, we can use the Plots.jl package and the function152# `plot(...)`, which creates a graphical representation of the solution. `sol` is a variable153# defined in the executed example and it contains the solution after the simulation154# finishes. `sol.u` holds the vector of values at each saved timestep, while `sol.t` holds the155# corresponding times for each saved timestep. In this instance, only two timesteps were saved: the156# initial and final ones. The plot depicts the distribution of the weak blast wave at the final moment157# of time, showing the density, velocities, and pressure of the ideal gas across a 2D domain.158159using Plots160plot(sol)161162# ### Getting an existing setup file163164# To obtain a list of all Trixi.jl elixirs execute165# [`get_examples`](@ref). It returns the paths to all example setups.166167get_examples()168169# Editing an existing elixir is the best way to start your first own investigation using Trixi.jl.170171# To edit an existing elixir, you first have to find a suitable one and then copy it to a local172# folder. Let's have a look at how to download the `elixir_euler_ec.jl` elixir used in the previous173# section from the [Trixi.jl GitHub repository](https://github.com/trixi-framework/Trixi.jl).174175# - All examples are located inside176# the [`examples`](https://github.com/trixi-framework/Trixi.jl/tree/main/examples) folder.177# - Navigate to the178# file [`elixir_euler_ec.jl`](https://github.com/trixi-framework/Trixi.jl/blob/main/examples/tree_2d_dgsem/elixir_euler_ec.jl).179# - Right-click the `Raw` button on the right side of the webpage and choose `Save as...`180# (or `Save Link As...`).181# - Choose a folder and save the file.182183# ### Modifying an existing setup184185# As an example, we will change the initial condition for calculations that occur in186# `elixir_euler_ec.jl`. Initial conditions for [`CompressibleEulerEquations2D`](@ref) consist of187# initial values for ``\rho``, ``\rho v_1``, ``\rho v_2`` and ``\rho e_{\text{total}}``. One of the common initial188# conditions for the compressible Euler equations is a simple density wave. Let's implement it.189190# - Open the downloaded file `elixir_euler_ec.jl` with a text editor.191# - Go to the line with the following code:192# ````julia193# initial_condition = initial_condition_weak_blast_wave194# ````195# Here, [`initial_condition_weak_blast_wave`](@ref) is used as the initial condition.196# - Comment out the line using the `#` symbol:197# ````julia198# # initial_condition = initial_condition_weak_blast_wave199# ````200# - Now you can create your own initial conditions. Add the following code after the201# commented line:202203function initial_condition_density_waves(x, t, equations::CompressibleEulerEquations2D)204v1 = 0.1 # velocity along x-axis205v2 = 0.2 # velocity along y-axis206rho = 1.0 + 0.98 * sinpi(sum(x) - t * (v1 + v2)) # density wave profile207p = 20 # pressure208rho_e_total = p / (equations.gamma - 1) + 1 / 2 * rho * (v1^2 + v2^2)209return SVector(rho, rho * v1, rho * v2, rho_e_total)210end211initial_condition = initial_condition_density_waves212nothing; #hide #md213214# - Execute the following code one more time, but instead of `path/to/file` paste the path to the215# `elixir_euler_ec.jl` file that you just edited.216# ````julia217# using Trixi218# trixi_include(path/to/file);219# using Plots220# plot(sol)221# ````222# Then you will obtain a new solution from running the simulation with a different initial223# condition.224225trixi_include(@__MODULE__, joinpath(examples_dir(), "tree_2d_dgsem", "elixir_euler_ec.jl"), #hide #md226initial_condition = initial_condition); #hide #md227pd = PlotData2D(sol) #hide #md228p1 = plot(pd["rho"]) #hide #md229p2 = plot(pd["v1"], clim = (0.05, 0.15)) #hide #md230p3 = plot(pd["v2"], clim = (0.15, 0.25)) #hide #md231p4 = plot(pd["p"], clim = (10, 30)) #hide #md232plot(p1, p2, p3, p4) #hide #md233234# To get exactly the same picture execute the following.235# ````julia236# pd = PlotData2D(sol)237# p1 = plot(pd["rho"])238# p2 = plot(pd["v1"], clim=(0.05, 0.15))239# p3 = plot(pd["v2"], clim=(0.15, 0.25))240# p4 = plot(pd["p"], clim=(10, 30))241# plot(p1, p2, p3, p4)242# ````243244# Feel free to make further changes to the initial condition to observe different solutions.245246# Now you are able to download, modify and execute simulation setups for Trixi.jl. To explore247# further details on setting up a new simulation with Trixi.jl, refer to the second part of248# the introduction titled [Create your first setup](@ref create_first_setup).249250Sys.rm("out"; recursive = true, force = true) #hide #md251252253