0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

「偏微分方程式を解くために、sympy でどのように入力したらいいですか。」をAI先生に聞きました。

Last updated at Posted at 2025-05-10

・??? Docker、FEniCS と FiPy

(本日の)ChatGPT先生へ

質問リスト

# 質問リスト
1. 偏微分方程式を解くために、sympy でどのように入力したらいいですか。
2. 結果を出力して下さい。
3. 上記のバージョンを教えて下さい。
4. 上記をFEniCS や FiPyのソースコードで教えて下さい。結果も出力して下さい。
5. Docker 版は、何ですか。pwdも合わせて教えて下さい。
6. いいえ。私は、chromebookです。
7. 有効です。
8. $USER と $(pwd)について、何を指定したらいいかわかりません。$ や ()が必要ですか。
9. FEniCSをやってみます。FiPy のセットアップも教えて下さい。
10. どうして、インストールに違いがありますか。
11. pip は pip3 だったような気がします。pipでいいですか。

・以下の最後の質問を省略されました。

私の質問をMarkdown形式でリスト化して出力して。Markdown形式でリストからアスタリスクは削除して。1行目に、# 質問リスト の文字を出力して。最後の質問も追加して下さい。追番を追加して下さい。Markdown形式で。

(以下回答抜粋)

例:1次元熱方程式...

from sympy import symbols, Function, Eq, diff, pdsolve

x, t = symbols('x t')
u = Function('u')(x, t)

# 偏微分方程式の定義
pde = Eq(diff(u, t), diff(u, x, x))

# 解く(可能な場合)
sol = pdsolve(pde)
print(sol)

...出力例です。

from sympy import symbols, Function, Eq, diff, pdsolve

x, t = symbols('x t')
u = Function('u')(x, t)

# 偏微分方程式の定義
pde = Eq(diff(u, t), diff(u, x, x))

# 解く
sol = pdsolve(pde)
print(sol)

出力される結果(SymPyのバージョンによって多少異なる場合あり):

Eq(u(x, t), exp(C1)*erf(x/(2*sqrt(t))) + C2)

または、以下のような一般解の形式になる場合もあります:

Eq(u(x, t), f1(x + sqrt(-1)*sqrt(t)) + f2(x - sqrt(-1)*sqrt(t)))

上記をFEniCS や FiPyのソースコードで教えて下さい。結果も出力して下さい。
【1】FEniCS を用いた実装(時間依存1D熱伝導)

from fenics import *
import numpy as np
import matplotlib.pyplot as plt

# 時間と空間のパラメータ
T = 0.5          # 最終時刻
num_steps = 50   # 時間ステップ数
dt = T / num_steps  # 時間刻み
nx = 30
mesh = UnitIntervalMesh(nx)
V = FunctionSpace(mesh, 'P', 1)

# 初期条件
u_0 = Expression('exp(-100*pow(x[0]-0.5, 2))', degree=2)
u_n = interpolate(u_0, V)

# 境界条件
bc = DirichletBC(V, Constant(0), 'on_boundary')

# 弱形式の定義
u = TrialFunction(V)
v = TestFunction(V)
f = Constant(0)
a = u*v*dx + dt*dot(grad(u), grad(v))*dx
L = (u_n + dt*f)*v*dx

u = Function(V)
t = 0

# 結果の格納と出力
for n in range(num_steps):
    t += dt
    solve(a == L, u, bc)
    u_n.assign(u)

# プロット
plot(u)
plt.title("FEniCS: Solution at t = {:.2f}".format(t))
plt.xlabel("x")
plt.ylabel("u(x, t)")
plt.show()

【2】FiPy を用いた実装

from fipy import CellVariable, Grid1D, TransientTerm, DiffusionTerm, Viewer
import numpy as np

# メッシュと変数の定義
nx = 50
dx = 1.0 / nx
mesh = Grid1D(nx=nx, dx=dx)
u = CellVariable(name="u", mesh=mesh, value=0.0)

# 初期条件(ガウス関数)
x = mesh.cellCenters[0]
u[:] = np.exp(-100 * (x - 0.5) ** 2)

# 方程式の定義
eq = TransientTerm() == DiffusionTerm()

# 可視化
viewer = Viewer(vars=u, datamin=0., datamax=1.)
viewer.plot()

# 時間ステップ
dt = 0.001
steps = 100

for step in range(steps):
    eq.solve(var=u, dt=dt)
    viewer.plot()

print("FiPy: 計算完了。最後のステップの中心値 =", u[nx//2])

Docker 版は、何ですか。pwdも合わせて教えて下さい。
【3】実際の Docker コマンド例

docker run -ti -v $(pwd):/home/fenics/shared -w /home/fenics/shared quay.io/fenicsproject/stable:current

【Chromebook で FEniCS を使う方法】...
【FiPy のセットアップ手順】(Crostini 環境)...

どうして、インストールに違いがありますか。

【FEniCS と FiPy のインストール方法の違い】

・以下省略 難しくなってきました。

Gemini先生へ

copilot先生へ

いつもの? sympyの実行環境と 参考のおすすめです。

(テンプレート)

いつもと違うおすすめです。

Qiita内

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?