Skip to content

Improve hard constraint on Boundary Condition #4

Merged
liruipeng merged 10 commits into
fix-enforce-bcfrom
stc/fix-enforce-bc
Jun 3, 2025
Merged

Improve hard constraint on Boundary Condition #4
liruipeng merged 10 commits into
fix-enforce-bcfrom
stc/fix-enforce-bc

Conversation

@stevengogogo
Copy link
Copy Markdown
Collaborator

@stevengogogo stevengogogo commented Jun 2, 2025

Reference:

Page 5, https://arxiv.org/pdf/2102.04626

Topic about bounday condition and multi-dnn is discussed in #7 (comment)

@stevengogogo
Copy link
Copy Markdown
Collaborator Author

Exp: No hard-constraint

I reduce high frequency to 8 for experiment the training process.

python pinn_1d.py   --levels 1 --epochs 100000 --lr 1e-3 --activation tanh --sweeps 1 --plot --hidden_dims 256 256 256 256 256 --loss_type 0 --high_freq 8

image

Iteration  99999/100000, PINN Loss: 1.8789e-01, Err 2-norm:  3.6336e+00, inf-norm: 3.9357e-01

@stevengogogo
Copy link
Copy Markdown
Collaborator Author

stevengogogo commented Jun 2, 2025

Exp: with BC enforcement

y = g0 + (x-0)/(1-0)*(g1 - g0) + (1 - torch.exp(0-x)) * (1 - torch.exp(x-1)) * y # in domain x in [0, 1]
python pinn_1d.py   --levels 1 --epochs 100000 --lr 1e-3 --activation tanh --sweeps 1 --plot --hidden_dims 256 256 256 256 256 --loss_type 0 --high_freq 8 --enforce_bc

image

Iteration  99999/100000, PINN Loss: 4.1775e-02, Err 2-norm:  7.6223e+01, inf-norm: 8.2844e+00

@stevengogogo
Copy link
Copy Markdown
Collaborator Author

stevengogogo commented Jun 2, 2025

python pinn_1d.py   --levels 1 --epochs 100000 --lr 1e-3 --activation tanh --sweeps 1 --plot --hidden_dims 256 256 256 256 256 --loss_type 0 --high_freq 8 --enforce_bc

image

Iteration  99999/100000, PINN Loss: 4.1775e-02, Err 2-norm:  7.6223e+01, inf-norm: 8.2844e+00

@stevengogogo
Copy link
Copy Markdown
Collaborator Author

stevengogogo commented Jun 2, 2025

Exp: enforce boundary condition with linear term

 y = g0 * (1 - x) + g1 * x + x * (1 - x) * y

image

Iteration  99999/100000, PINN Loss: 1.5129e-03, Err 2-norm:  1.7308e+01, inf-norm: 1.5587e+00

@stevengogogo
Copy link
Copy Markdown
Collaborator Author

stevengogogo commented Jun 2, 2025

Question

Why smaller PINN loss does not lead to accurate solution?

#4 (comment)

compare to

#4 (comment)

Does problem 1 actually have 1 solution?

1D PDE problem:

$-u_{xx} + \gamma u = f$

and homogeneous boundary conditions (BC)

The analytical solution is

$u(x) = \sum_k c_k \sin(w_k \pi x)$
and
$f = \sum_k c_k (w_k^2 \pi^2 + \gamma) \sin(w_k \pi x)$

Comment thread pinn/pinn_1d.py Outdated
Comment thread pinn/pinn_1d.py
Comment thread pinn/utils.py
Comment thread pinn/pinn_1d.py
y = g0 * (1 - x) + g1 * x + (1 - torch.exp(x)) * (1 - torch.exp(x-1)) * y
y = g0 * (1 - x) + g1 * x + x * (1 - x) * y
#y = g0 * (1 - x) + g1 * x + (1 - torch.exp(x)) * (1 - torch.exp(x-1)) * y
#y = g0 + (x-0)/(1-0)*(g1 - g0) + (1 - torch.exp(0-x)) * (1 - torch.exp(x-1)) * y # in domain x in [0, 1]
Copy link
Copy Markdown
Owner

@liruipeng liruipeng Jun 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lines 260 and 261 seems to be the same math to me.

Copy link
Copy Markdown
Collaborator Author

@stevengogogo stevengogogo Jun 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The difference is (1-torch.exp(0-x)) keeps positive during the domain [0,1]

This is followed by the idea in page 5 of this paper:
https://arxiv.org/pdf/2102.04626

  • We may need to remove #260

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I see. A minor question: why you write things like exp(0-x) instead of exp(-x), and also (x-0)/(1-0). Are these necessary? Thanks

Copy link
Copy Markdown
Collaborator Author

@stevengogogo stevengogogo Jun 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it is just for understanding the boundary is $[x_0, x_1] = [0,1]$ where $x_0 < x_1$. I can remove it

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it is OK. Actually we should update the code use mesh.ax and mesh.bx, to allow intervals that are not [0,1]

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this as future feature #6 , and will be added after we figure out the bug of training in this PR.

@liruipeng liruipeng requested a review from siuwuncheung June 2, 2025 23:50
@liruipeng
Copy link
Copy Markdown
Owner

liruipeng commented Jun 2, 2025

Question

Why smaller PINN loss does not lead to accurate solution?

#4 (comment)

compare to

#4 (comment)

Does problem 1 actually have 1 solution?

1D PDE problem:

− u x x + γ u = f

and homogeneous boundary conditions (BC)

The analytical solution is

u ( x ) = ∑ k c k sin ⁡ ( w k π x ) and f = ∑ k c k ( w k 2 π 2 + γ ) sin ⁡ ( w k π x )

Good question. I am not sure. @siuwuncheung ideas?

Refer to: #5

@liruipeng liruipeng changed the base branch from main to fix-enforce-bc June 3, 2025 16:57
@stevengogogo
Copy link
Copy Markdown
Collaborator Author

Exp: Single Level PINN without hard contraint on BC

python pinn_1d.py   --levels 1 --epochs 100000 --lr 1e-3 --activation tanh --sweeps 1 --plot --hidden_dims 512 512 512 --loss_type 0 --high_freq 8

image

@liruipeng liruipeng merged commit 148088e into fix-enforce-bc Jun 3, 2025
liruipeng added a commit that referenced this pull request Jun 3, 2025
* Improve hard constraint on Boundary Condition  (#4)

* Add Tony's toy code

* Add missing file

* Create note.tex

* revise encforce bc equation and learning rate

* fix bug: setting frequency

* AD: retain graph

* remove g0 g1

* enforce BC by linear

* fix: use linear scale BC enforcement

* remove retain_graph=True followed by https://docs.pytorch.org/docs/stable/generated/torch.autograd.grad.html

---------

Co-authored-by: Siu Wun Cheung <cheung26@llnl.gov>

* minor change

* code cleanup

---------

Co-authored-by: Steven Chiu <stchiu@email.tamu.edu>
Co-authored-by: Siu Wun Cheung <cheung26@llnl.gov>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants