Skip to content

Commit 8df4831

Browse files
authored
Merge pull request #119 from eric2003/hexin
add eno example
2 parents 374c13e + f9b7bcb commit 8df4831

File tree

223 files changed

+29363
-7411
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

223 files changed

+29363
-7411
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import numpy as np
2+
3+
def initial_condition(x):
4+
"""生成初始条件:在 [0.1, 0.3] 区间内为 1.0,其他位置为 0.0。
5+
6+
参数:
7+
x (np.ndarray): 空间坐标数组
8+
9+
返回:
10+
np.ndarray: 初始条件数组
11+
"""
12+
return np.where((x >= 0.1) & (x <= 0.3), 1.0, 0.0)
13+
14+
def analytical_solution(x, t, a, domain_length):
15+
"""计算理论解:初始波形以速度 a 平移,并应用周期边界条件。
16+
17+
参数:
18+
x (np.ndarray): 空间坐标数组
19+
t (float): 时间
20+
a (float): 波速
21+
domain_length (float): 区域的周期长度 L
22+
23+
返回:
24+
np.ndarray: 时间 t 后的波形
25+
"""
26+
# 计算平移后的坐标(无边界处理)
27+
shifted_x = x - a * t
28+
29+
# 应用周期边界条件,将坐标限制在 [0, domain_length) 范围内
30+
shifted_x_periodic = (shifted_x + domain_length) % domain_length
31+
32+
# 返回平移后的初始条件
33+
return initial_condition(shifted_x_periodic)
34+
35+
36+
import matplotlib.pyplot as plt
37+
38+
# 定义参数
39+
L = 1.0 # 区域长度
40+
a = 1.0 # 波速
41+
t = 0.4 # 时间
42+
x = np.linspace(0, L, 1000) # 空间网格
43+
44+
# 计算初始条件和理论解
45+
u0 = initial_condition(x)
46+
u_analytical = analytical_solution(x, t, a, L)
47+
48+
# 可视化
49+
plt.plot(x, u0, label="Initial condition")
50+
plt.plot(x, u_analytical, label=f"Analytical (t={t})")
51+
plt.legend()
52+
plt.xlabel("x")
53+
plt.ylabel("u")
54+
plt.show()
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import numpy as np
2+
3+
def test_periodic_mapping():
4+
L = 1.0
5+
test_cases = [
6+
(-0.2, 0.8),
7+
(1.3, 0.3),
8+
(0.5, 0.5),
9+
(-1.1, 0.9),
10+
(2.7, 0.7),
11+
]
12+
13+
for x, expected in test_cases:
14+
result = (x + L) % L
15+
assert np.isclose(result, expected), f"Failed for {x}: {result} vs {expected}"
16+
print("所有测试通过!")
17+
18+
test_periodic_mapping()
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import numpy as np
2+
3+
def calculate_eno_crj(r, j, k):
4+
result = 0
5+
6+
# 外层求和:m 从 j+1 到 k
7+
for m in range(j + 1, k + 1):
8+
numerator = 0
9+
10+
# 内层求和:l 从 0 到 k,且 l ≠ m
11+
for l in range(0, k + 1):
12+
if l == m:
13+
continue # 跳过 l = m 的情况
14+
15+
product = 1
16+
17+
# 内层连乘:q 从 0 到 k,且 q ≠ m, l
18+
for q in range(0, k + 1):
19+
if q == m or q == l:
20+
continue # 跳过 q = m 或 q = l 的情况
21+
product *= (r - q + 1)
22+
23+
numerator += product
24+
25+
denominator = 1
26+
27+
# 分母连乘:l 从 0 到 k,且 l ≠ m
28+
for l in range(0, k + 1):
29+
if l == m:
30+
continue # 跳过 l = m 的情况
31+
denominator *= (m - l)
32+
33+
result += numerator / denominator
34+
35+
return result
36+
37+
for k in range(1,8):
38+
print(f"=== k = {k} ===")
39+
# 计算矩阵并存储到列表中
40+
eno_mat = np.zeros((k+1,k))
41+
for r in range(-1,k):
42+
for j in range(k):
43+
eno_mat[r+1, j] = calculate_eno_crj(r, j, k)
44+
45+
print(f'{eno_mat=}')
46+
print(f'{type(eno_mat)=}')

0 commit comments

Comments
 (0)