第29回:境界点は何点必要か — interior lossとboundary loss
ChatGPTで作る有限要素プログラム入門 Season 3 第5回(通算第29回)
はじめに――境界条件はPDE residualとは別の情報である
これまでの1次元実験では
\[ u_\theta(x)=x(1-x)N_\theta(x) \]としてDirichlet boundary conditionをhardに満たしていました. そのため「境界点をどこに置くか」という問題を意図的に消していました.
今回はsoft enforcementに戻り,
\[ \boxed{ \text{interior PDE information} \quad+\quad \text{boundary trace information} } \tag{1} \]を別々にsamplingします.
\[ \boxed{ N_{\partial} \text{ 個のboundary pointsで} \quad u_\theta=g \text{ on }\partial\Omega \quad \text{をどこまで保証できるか?} } \tag{2} \]
1.2次元Poisson problemへ進む
境界samplingを本格的に見るため,
\[ \Omega=(0,1)^2 \]で
\[ \boxed{ -\Delta u=f \quad\text{in }\Omega, \qquad u=g \quad\text{on }\partial\Omega } \tag{3} \]を考えます.
manufactured solutionとして
\[ \boxed{ u^\ast(x,y) = \sin(\pi x)\sin(\pi y), \qquad g=0, } \tag{4} \]従って
\[ \boxed{ f(x,y) = 2\pi^2 \sin(\pi x)\sin(\pi y) } \tag{5} \]を使います.
2.interior lossとboundary lossを分ける
interior collocation pointsを \[ X_\Omega=\{x_i\}_{i=1}^{N_\Omega} \] boundary pointsを \[ X_{\partial}=\{z_j\}_{j=1}^{N_{\partial}} \] とします.
strong residualは
\[ r_\theta(x) = -\Delta u_\theta(x)-f(x) \tag{6} \]です.
sampled lossesを
\[ \boxed{ \mathcal L_{\Omega,N} = \frac1{N_\Omega} \sum_{i=1}^{N_\Omega} |r_\theta(x_i)|^2 } \tag{7} \]と
\[ \boxed{ \mathcal L_{\partial,N} = \frac1{N_{\partial}} \sum_{j=1}^{N_{\partial}} |u_\theta(z_j)-g(z_j)|^2 } \tag{8} \]に分けます.
3.total lossにはbalance parameterが必要である
最も単純には
\[ \boxed{ \mathcal J_N(\theta) = \mathcal L_{\Omega,N} + \lambda_{\partial} \mathcal L_{\partial,N} } \tag{9} \]とします.
ここで \[ \lambda_{\partial}>0 \] はboundary loss weightです.
このparameterは単なるoptimizer trickではなく, 異なるnorm・異なるdimensionの情報をどうbalanceするか というmethod designです.
4.raw sumを使うと点数を変えただけでmethodが変わる
もしmeanではなく
\[ \widetilde{\mathcal J}_N = \sum_{i=1}^{N_\Omega}|r_\theta(x_i)|^2 + \lambda_{\partial} \sum_{j=1}^{N_{\partial}} |u_\theta(z_j)-g(z_j)|^2 \tag{10} \]とすると, \(N_{\partial}\) を2倍にしただけでboundary termのrelative weightもほぼ2倍になります.
つまり
\[ \boxed{ \text{point count change} \Longrightarrow \text{effective penalty change} } \tag{11} \]です.
sampling strategyとloss weightingを分離したいなら, mean lossまたは適切なquadrature weightsを使う方が透明です.
5.continuous targetもdomainとboundaryで別のmeasureを持つ
continuous analogueをnormalised averageで書けば
\[ \boxed{ \mathcal J(u) = \frac1{|\Omega|} \int_\Omega |-\Delta u-f|^2\,dx + \lambda_{\partial} \frac1{|\partial\Omega|} \int_{\partial\Omega} |u-g|^2\,ds. } \tag{12} \]interiorは\(d\)-dimensional measure, boundaryは\((d-1)\)-dimensional measureです.
従って
\[ \boxed{ N_\Omega \quad\text{と}\quad N_{\partial} \text{ を単純に同数にする理由はない.} } \tag{13} \]6.同じgeometric resolutionなら点数のscalingが違う
quasi-uniform sampling scaleを \(h\) とすると, \(d\) 次元domain内では概ね
\[ \boxed{ N_\Omega \asymp h^{-d}, } \tag{14} \]一方boundary上では
\[ \boxed{ N_{\partial} \asymp h^{-(d-1)}. } \tag{15} \]従って
\[ \boxed{ N_{\partial} \asymp N_\Omega^{(d-1)/d}. } \tag{16} \]2次元ならprototype relationは
\[ \boxed{ N_{\partial} \asymp N_\Omega^{1/2}. } \tag{17} \]これは「必要十分なboundary point数」の定理ではなく, 同じ幾何学的resolutionを揃えるときのscaling ruleです.
7.2次元squareならedgewise spacingを見る
各edge \(F\subset\partial\Omega\) に \(N_F\) pointsを置くなら
\[ \boxed{ h_{\partial,F} = \sup_{z\in F} \min_{z_j\in X_{\partial}\cap F} |z-z_j| } \tag{18} \]をboundary fill scaleとして使えます.
globalには
\[ \boxed{ h_{\partial X} = \max_{F} h_{\partial,F}. } \tag{19} \]点数だけでなく, どのedgeに大きなboundary gapが残っているか を記録します.
8.boundary separationも必要である
boundary pointsが一部へ集中しているかを見るため
\[ \boxed{ q_{\partial X} = \frac12 \min_{i\ne j} d_{\partial\Omega}(z_i,z_j) } \tag{20} \]を使います.
polygonではedge arclengthとcorner connectivityを考慮したboundary distanceを使えます.
prototype ratioは
\[ \boxed{ \rho_{\partial X} = \frac{h_{\partial X}}{q_{\partial X}}. } \tag{21} \]9.有限個のboundary pointsだけではDirichlet traceを保証できない
これは単なる注意ではなく,明示的に示せます.
top edge \[ y=1 \] に
\[ \boxed{ x_j = \frac{j}{m}, \qquad j=1,\ldots,m-1 } \tag{22} \]というboundary sampleを置きます.
次のharmonic functionを考えます.
\[ \boxed{ h_m(x,y) = \sin(m\pi x) \frac{\sinh(m\pi y)}{\sinh(m\pi)}. } \tag{23} \]10.このfunctionはinterior PDE residualを全く変えない
直接計算すると
\[ \partial_{xx}h_m = -(m\pi)^2h_m, \qquad \partial_{yy}h_m = (m\pi)^2h_m. \]従って
\[ \boxed{ \Delta h_m=0 \quad\text{in }\Omega. } \tag{24} \]つまり
\[ \widetilde u = u^\ast+c\,h_m \tag{25} \]としても
\[ \boxed{ -\Delta\widetilde u-f=0 \quad\text{everywhere in }\Omega. } \tag{26} \]11.sampled boundary lossもzeroにできる
\(h_m\) は
\[ h_m(0,y)=h_m(1,y)=h_m(x,0)=0. \]top edgeでは
\[ h_m(x,1)=\sin(m\pi x). \]従って式 (22) のsamplesでは
\[ \boxed{ h_m(x_j,1) = \sin(j\pi) = 0. } \tag{27} \]他の3 edgesでもzeroなので, これらのfinite boundary samplesに対して
\[ \boxed{ \mathcal L_{\partial,N}(\widetilde u)=0. } \tag{28} \]12.それでもboundary conditionは点の間で破れている
top edgeのsample間では
\[ \widetilde u(x,1) = c\sin(m\pi x), \]なので \(c\ne0\) ならzero boundary conditionを満たしません.
しかも式 (26) と (28) から
\[ \boxed{ \mathcal L_{\Omega,N}=0, \qquad \mathcal L_{\partial,N}=0 } \tag{29} \]が同時に起こり得ます.
interior PDEをeverywhere満たし,boundary samplesも全部満たしていても, finite boundary observationsだけでは正しいDirichlet traceを決められない.
13.「境界点を何点置けば十分か」に普遍的な答えはない
上のcounterexampleでは \(m\) をboundary point数に合わせて大きくできます.
従ってunrestricted smooth trial classに対して
\[ \boxed{ N_{\partial} = \text{fixed finite number} } \tag{30} \]だけからcontinuous boundary traceを保証することはできません.
保証には追加情報が必要です. 例えば
- hard boundary enforcement,
- trial classのfrequency / smoothness control,
- boundary sampling inequality,
- independent boundary quadrature,
- verified trace norm,
などです.
14.hard constraintならboundary sampling problemを消せる
zero Dirichletならsquareでは例えば
\[ \boxed{ u_\theta(x,y) = x(1-x)y(1-y)N_\theta(x,y) } \tag{31} \]とすれば
\[ u_\theta=0 \quad\text{on }\partial\Omega \]がexactly成立します.
この場合
\[ \boxed{ N_{\partial}=0 } \tag{32} \]でもDirichlet condition自体は満たされます.
つまり「必要なboundary point数」は boundary conditionのenforcement methodにも依存します.
15.nonzero Dirichlet dataもextensionでhardにできる
boundary extension \(G\) とvanishing factor \(\rho\) を使い
\[ \boxed{ u_\theta = G + \rho N_\theta, \qquad G|_{\partial\Omega}=g, \quad \rho|_{\partial\Omega}=0 } \tag{33} \]とすればhard enforcementできます.
ただし複雑geometry,mixed boundary conditions,corner,distance-function regularityでは \(G\) や \(\rho\) の構成自体が新しい問題になります.
16.hard constraintにも代償がある
hard constraintはboundary lossを消しますが,
\[ u_\theta=G+\rho N_\theta \]というparameterisationによって
- gradient scale,
- approximation class,
- conditioning,
- corner behaviour,
が変わります.
従って
\[ \boxed{ \text{hard BC} \not\Rightarrow \text{all boundary-related numerical issues disappear}. } \tag{34} \]17.soft boundary lossはNitsche法そのものではない
PINNの
\[ \lambda_{\partial} \|u_\theta-g\|_{L^2(\partial\Omega)}^2 \]はpenalty型です.
しかしsymmetric Nitsche methodには normal fluxとのconsistency termsも入ります.
従って
\[ \boxed{ \text{boundary MSE penalty} \neq \text{Nitsche method}. } \tag{35} \]「弱くDirichlet条件を課す」という表面的類似だけで同一視してはいけません.
18.\(\lambda_{\partial}\) を大きくすればよいわけでもない
\(\lambda_{\partial}\) が小さすぎればboundary errorを許し, 大きすぎればoptimizerがboundary termを優先しすぎて interior residualの改善が遅くなる可能性があります.
schematically
\[ \boxed{ \lambda_{\partial} \downarrow : \text{boundary under-enforcement}, \qquad \lambda_{\partial} \uparrow : \text{loss imbalance / conditioning}. } \tag{36} \]これは点数とは別のparameterです.
19.boundary point数とboundary weightを混同しない
mean lossを使えば, \(N_{\partial}\) を増やしても boundary termのaverage scaleは自動では増えません.
従って
\[ \boxed{ N_{\partial} : \text{boundary resolution}, \qquad \lambda_{\partial} : \text{loss balance} } \tag{37} \]と役割を分けられます.
raw sumsを使うとこの二つが混ざります.
20.cornersとboundary-condition junctionsは別に扱う
polygonではedge interiorだけでなくcornerも重要です.
特にDirichlet / Neumannが切り替わるpointやre-entrant cornerでは, trace regularityとsolution regularityの両方が変わり得ます.
従って
\[ \boxed{ \text{uniform edge sampling} + \text{special points} } \tag{38} \]という設計が必要になる場合があります.
これは次回のcorner singularityとgraded samplingへつながります.
21.square boundary samplerを実装する
def square_boundary_points(n_per_edge, device):
t = torch.linspace(
0.0, 1.0,
n_per_edge + 2,
device=device
)[1:-1]
z = torch.zeros_like(t)
o = torch.ones_like(t)
bottom = torch.stack([t, z], dim=1)
top = torch.stack([t, o], dim=1)
left = torch.stack([z, t], dim=1)
right = torch.stack([o, t], dim=1)
return torch.cat(
[bottom, top, left, right],
dim=0
)
corner pointsを別途含める場合はduplicate countingを避けます.
22.interior samplerもboundary samplerと分ける
def square_interior_points(N, device, generator=None):
x = torch.rand(
(N, 2),
device=device,
generator=generator
)
return x
interior seedとboundary seedを別々に管理できるようにしておくと, sampling effectを切り分けやすくなります.
23.loss implementation
def total_loss(
model,
x_int,
x_bnd,
lambda_bnd
):
r = pde_residual_2d(
model,
x_int
)
u_bnd = model(x_bnd)
loss_int = torch.mean(
r ** 2
)
loss_bnd = torch.mean(
u_bnd ** 2
)
loss = (
loss_int
+ lambda_bnd * loss_bnd
)
return loss, loss_int, loss_bnd
totalだけでなく \[ \mathcal L_{\Omega,N}, \qquad \mathcal L_{\partial,N} \] を別々にlogします.
24.2次元Laplacianをautomatic differentiationで作る
def pde_residual_2d(model, x):
x = (
x.clone()
.detach()
.requires_grad_(True)
)
u = model(x)
grad_u = torch.autograd.grad(
u,
x,
grad_outputs=torch.ones_like(u),
create_graph=True
)[0]
ux = grad_u[:, 0:1]
uy = grad_u[:, 1:2]
uxx = torch.autograd.grad(
ux,
x,
grad_outputs=torch.ones_like(ux),
create_graph=True
)[0][:, 0:1]
uyy = torch.autograd.grad(
uy,
x,
grad_outputs=torch.ones_like(uy),
create_graph=True
)[0][:, 1:2]
xx = x[:, 0:1]
yy = x[:, 1:2]
f = (
2.0 * math.pi ** 2
* torch.sin(math.pi * xx)
* torch.sin(math.pi * yy)
)
return -(uxx + uyy) - f
25.boundary studyの比較protocol
境界点数だけを比較するなら
\[ \boxed{ \begin{aligned} &\text{same network},\\ &\text{same initial parameters},\\ &\text{same }X_\Omega,\\ &\text{same optimiser},\\ &\text{same }\lambda_{\partial},\\ &\text{same training steps} \end{aligned} } \tag{39} \]を固定し, \(N_{\partial}\) だけを変えます.
\(\lambda_{\partial}\) と \(N_{\partial}\) を同時に変えると, boundary resolutionとloss balanceのどちらが効いたか分からなくなります.
26.training boundary lossだけで評価してはいけない
training pointsとは独立なdense boundary quadrature \[ Z_{\rm ref} \] を用意して
\[ \boxed{ E_{\partial,\rm ref} = \left( \frac1{|\partial\Omega|} \int_{\partial\Omega} |u_\theta-g|^2\,ds \right)^{1/2} } \tag{40} \]を評価します.
さらに
\[ \boxed{ E_{\partial,\infty} = \|u_\theta-g\|_{L^\infty(\partial\Omega)} } \tag{41} \]も見ると,sample間のoscillationを発見しやすくなります.
27.interior errorも同じreference measureで測る
boundary enforcementを強くしても, interior solutionが正しいとは限りません.
従って
\[ \boxed{ R_{\rm ref} = \|-\Delta u_\theta-f\|_{L^2(\Omega)}, } \tag{42} \]\[ \boxed{ E_{L^2} = \|u_\theta-u^\ast\|_{L^2(\Omega)}, \qquad E_{H^1} = |u_\theta-u^\ast|_{H^1(\Omega)} } \tag{43} \]
を同時に記録します.
28.boundary point allocationはedgeごとにも最適化できる
squareで4 edgesすべてへ同数を置くのが常に最良とは限りません.
非一様boundary dataやboundary layerがあるなら
\[ \boxed{ N_{\partial} = \sum_F N_F } \tag{44} \]としてedgewise allocation \[ N_F \] を変える方が自然です.
これは2次元boundary上のanisotropic / graded samplingへつながります.
29.境界点数を決める最低限の考え方
普遍的な「\(N_{\partial}=100\) なら十分」という答えはありません. 代わりに
\[ \boxed{ \begin{aligned} 1.&\ \text{boundary enforcement method},\\ 2.&\ \text{boundary geometry / dimension},\\ 3.&\ \text{trace variation scale},\\ 4.&\ \text{boundary fill distance},\\ 5.&\ \text{loss weight},\\ 6.&\ \text{independent boundary audit} \end{aligned} } \tag{45} \]を順に決めます.
30.AIがboundary lossで間違えやすい点
| mistake | 何が壊れるか |
|---|---|
| \(N_\Omega=N_{\partial}\) を当然とする | dimension scalingを無視する |
| raw sumで点数比較 | point countとloss weightが混ざる |
| sampled boundary MSEがzeroならBCを満たしたと主張 | sample間のtrace oscillationを見落とす |
| boundary MSEをNitsche法と呼ぶ | consistency flux termsを落とす |
| \(\lambda_{\partial}\) と \(N_{\partial}\) を同時に変更 | 原因を切り分けられない |
| hard constraintなら問題が全て消えるとする | parameterisation / geometry issuesを無視する |
| boundary training pointsでboundary errorも測る | finite observation biasを検出できない |
31.boundary-sampling audit prompt
このPINN boundary-condition implementationを数学的に監査してください.
(1) interior lossとboundary lossを別々に定義しているか,
(2) raw sumかmean / quadrature lossかを明示しているか,
(3) \(N_\Omega\) と \(N_{\partial}\) のdimension scalingを考慮しているか,
(4) boundary pointsのedgewise coordinatesを保存しているか,
(5) boundary fill distanceとclusteringを記録しているか,
(6) \(\lambda_{\partial}\) と \(N_{\partial}\) の役割を分離しているか,
(7) finite boundary samplesだけでcontinuous Dirichlet traceを保証していないか,
(8) trainingとは独立なdense boundary quadratureでtrace errorを評価しているか,
(9) hard constraintの場合,boundary extensionとvanishing factorのregularityを確認しているか,
(10) boundary MSE penaltyをNitsche法と同一視していないか,
(11) corner / boundary-condition junctionsを特別に扱う必要がないか確認しているか,
(12) interior residualとboundary errorを同時に報告しているか
を順に確認してください.
32.第29回の結論
PINNsでDirichlet conditionをsoftに課すとき, boundary pointsは単なる補助dataではありません.
\[ \boxed{ X_{\partial} \longrightarrow \text{boundary empirical measure} \longrightarrow \mathcal L_{\partial,N} \longrightarrow \text{learned trace}. } \tag{46} \]同じgeometric resolutionならprototype scalingは
\[ \boxed{ N_{\partial} \asymp N_\Omega^{(d-1)/d}, } \tag{47} \]ですが,これは十分性を保証するmagic formulaではありません.
さらに明示的harmonic modeにより
\[ \boxed{ \mathcal L_{\Omega,N}=0, \qquad \mathcal L_{\partial,N}=0, \qquad u_\theta\ne u^\ast } \tag{48} \]が可能であることを見ました.
境界点数を決める前に,境界を何のmeasureで,どのresolutionで, どのloss weightで観測するかを決める.
次回はsolution regularityそのものがsampling designを変える例として, 第30回:角特異性をPINNsは捉えられるか — regularityとgraded sampling へ進みます.
参考文献・関連ページ
- M. Raissi, P. Perdikaris and G. E. Karniadakis, “Physics-informed neural networks: A deep learning framework for solving forward and inverse problems involving nonlinear partial differential equations”, Journal of Computational Physics 378 (2019), 686–707.
- J. Nitsche, “Über ein Variationsprinzip zur Lösung von Dirichlet-Problemen bei Verwendung von Teilräumen, die keinen Randbedingungen unterworfen sind”, Abhandlungen aus dem Mathematischen Seminar der Universität Hamburg 36 (1971), 9–15.
- 第18回:Nitsche法とは何か — Dirichlet境界条件を弱く課す
- 第27回:PINNsを作る — strong residualを点で最小化する
- 第28回:PINNsでは点配置で答えが変わるか — uniform・random・adaptive sampling
- 関連する先行ノート:1次元Poisson問題でPINNsを動かす
