Skip to content
Snippets Groups Projects
Commit 23ba624a authored by Jan Habscheid's avatar Jan Habscheid
Browse files

Adding examplery code for my problem of problem 2.3

parent c54e23a5
No related branches found
No related tags found
1 merge request!1Project2 working
Project2/src/Figures/MyProblem_0.png

18.6 KiB

Project2/src/Figures/MyProblem_1.png

16.9 KiB

Project2/src/Figures/MyProblem_2.png

20.1 KiB

Project2/src/Figures/MyProblem_3.png

20.8 KiB

Project2/src/Figures/MyProblem_4.png

25.8 KiB

Project2/src/Figures/MyProblem_numerical.png

22.2 KiB

......@@ -46,7 +46,7 @@ x = np.linspace(xmin + dx/2, xmax - dx/2, nx)
# -------------------------------------------------
# Time parameters
# -------------------------------------------------
T = 0.45 # Final time (choose T small enough that waves have not interacted too much)
T = 0.2 # Final time (choose T small enough that waves have not interacted too much)
CFL = 0.5 # CFL number; for f'(u) we know |f'| is at most 2
dt = CFL * dx / 2.0
nt = int(T / dt) + 1
......@@ -138,12 +138,13 @@ u_exact = analytic_solution(x, T)
# Plot all three solutions
# -------------------------------------------------
plt.figure(figsize=(10, 6))
plt.plot(x, u_exact, 'k--', lw=2, label='Analytic')
plt.plot(x, u_lf, 'b-', lw=2, label='Lax-Friedrichs')
plt.plot(x, u_god, 'r-', lw=2, label='Godunov')
# plt.plot(x, u_exact, 'k--', lw=2, label='Analytic')
plt.plot(x, u_lf, 'b-', lw=2)#, label='Lax-Friedrichs')
# plt.plot(x, u_god, 'r-', lw=2, label='Godunov')
plt.xlabel('x')
plt.ylabel('u')
plt.title(f'Solution at time T = {T:.3f}')
plt.legend()
plt.grid(True)
plt.savefig('Figures/MyProblem_numerical.png')
plt.show()
%% Cell type:code id: tags:
``` python
import numpy as np
import matplotlib.pyplot as plt
```
%% Cell type:code id: tags:
``` python
x = np.linspace(-2, 3, 101)
```
%% Cell type:code id: tags:
``` python
def u0_1(x, t):
u0_1 = np.zeros_like(x)
u0_1 = np.where(
x <= 1/2 * t,
0,
np.where(
x < 1-t,
3/4,
np.where(
x <= 1 + 2*t,
(2 - x/t)/4,
0
)
)
)
return u0_1
def u0_2(x, t):
u0_2 = np.zeros_like(x)
u0_2 = np.where(
x <= -t,
0,
np.where(
x < 2*t,
(2 - x/t) / 4,
np.where(
x <= 1 + t/2,
3/4,
0
)
)
)
return u0_2
```
%% Cell type:code id: tags:
``` python
t = np.linspace(0, .2, 5)
for i in range(5):
plt.figure()
plt.title(f'Time: {t[i]}')
plt.plot(x, u0_1(x, t[i]), label=f"Solution 1")
plt.plot(x, u0_2(x, t[i]), label=f"Solution 2")
plt.legend()
plt.grid()
plt.savefig(f'Figures/MyProblem_{i}.png')
plt.show()
```
%% Output
/var/folders/v_/5q1gkdc53z34pdsfnpkx2t340000gn/T/ipykernel_69457/2027164159.py:11: RuntimeWarning: divide by zero encountered in divide
(2 - x/t)/4,
/var/folders/v_/5q1gkdc53z34pdsfnpkx2t340000gn/T/ipykernel_69457/2027164159.py:11: RuntimeWarning: invalid value encountered in divide
(2 - x/t)/4,
/var/folders/v_/5q1gkdc53z34pdsfnpkx2t340000gn/T/ipykernel_69457/2027164159.py:25: RuntimeWarning: divide by zero encountered in divide
(2 - x/t) / 4,
/var/folders/v_/5q1gkdc53z34pdsfnpkx2t340000gn/T/ipykernel_69457/2027164159.py:25: RuntimeWarning: invalid value encountered in divide
(2 - x/t) / 4,
%% Cell type:code id: tags:
``` python
```
Source diff could not be displayed: it is too large. Options to address this: view the blob.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment