1111mu = 0.5
1212Q = 4.0
1313
14- # Define function
15- if dde .backend .backend_name == "pytorch" :
16- import torch
17-
18- sin = torch .sin
19- cos = torch .cos
20- elif dde .backend .backend_name == "paddle" :
21- import paddle
22-
23- sin = paddle .sin
24- cos = paddle .cos
25- elif dde .backend .backend_name == "jax" :
26- import jax .numpy as jnp
27-
28- sin = jnp .sin
29- cos = jnp .cos
14+ # Define functions
15+ sin = dde .backend .sin
16+ cos = dde .backend .cos
17+ stack = dde .backend .stack
3018
3119geom = dde .geometry .Rectangle ([0 , 0 ], [1 , 1 ])
20+ BC_type = ["hard" , "soft" ][0 ]
3221
3322
3423def boundary_left (x , on_boundary ):
@@ -66,6 +55,7 @@ def func(x):
6655 return np .hstack ((ux , uy , Sxx , Syy , Sxy ))
6756
6857
58+ # Soft Boundary Conditions
6959ux_top_bc = dde .icbc .DirichletBC (geom , lambda x : 0 , boundary_top , component = 0 )
7060ux_bottom_bc = dde .icbc .DirichletBC (geom , lambda x : 0 , boundary_bottom , component = 0 )
7161uy_left_bc = dde .icbc .DirichletBC (geom , lambda x : 0 , boundary_left , component = 1 )
@@ -81,6 +71,17 @@ def func(x):
8171)
8272
8373
74+ # Hard Boundary Conditions
75+ def hard_BC (x , f ):
76+ Ux = f [:, 0 ] * x [:, 1 ] * (1 - x [:, 1 ])
77+ Uy = f [:, 1 ] * x [:, 0 ] * (1 - x [:, 0 ]) * x [:, 1 ]
78+
79+ Sxx = f [:, 2 ] * x [:, 0 ] * (1 - x [:, 0 ])
80+ Syy = f [:, 3 ] * (1 - x [:, 1 ]) + (lmbd + 2 * mu ) * Q * sin (np .pi * x [:, 0 ])
81+ Sxy = f [:, 4 ]
82+ return stack ((Ux , Uy , Sxx , Syy , Sxy ), axis = 1 )
83+
84+
8485def fx (x ):
8586 return (
8687 - lmbd
@@ -147,10 +148,10 @@ def pde(x, f):
147148 return [momentum_x , momentum_y , stress_x , stress_y , stress_xy ]
148149
149150
150- data = dde . data . PDE (
151- geom ,
152- pde ,
153- [
151+ if BC_type == "hard" :
152+ bcs = []
153+ else :
154+ bcs = [
154155 ux_top_bc ,
155156 ux_bottom_bc ,
156157 uy_left_bc ,
@@ -159,7 +160,12 @@ def pde(x, f):
159160 sxx_left_bc ,
160161 sxx_right_bc ,
161162 syy_top_bc ,
162- ],
163+ ]
164+
165+ data = dde .data .PDE (
166+ geom ,
167+ pde ,
168+ bcs ,
163169 num_domain = 500 ,
164170 num_boundary = 500 ,
165171 solution = func ,
@@ -170,6 +176,8 @@ def pde(x, f):
170176activation = "tanh"
171177initializer = "Glorot uniform"
172178net = dde .nn .PFNN (layers , activation , initializer )
179+ if BC_type == "hard" :
180+ net .apply_output_transform (hard_BC )
173181
174182model = dde .Model (data , net )
175183model .compile ("adam" , lr = 0.001 , metrics = ["l2 relative error" ])
0 commit comments