Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to define Hard Constraint for a specific BC #252

Closed
katayooneshkofti opened this issue Mar 29, 2021 · 11 comments
Closed

How to define Hard Constraint for a specific BC #252

katayooneshkofti opened this issue Mar 29, 2021 · 11 comments

Comments

@katayooneshkofti
Copy link

Hello @lululxvi,
Thank you for all the responses you have given up to now. Previously I have sent you my code and the training loss was large, so you suggested using "hard BCs" but I have a problem with defining BCs as hard constraints. I attached my initial and boundary conditions. For the first output variable (displacement) I wrote the following code but for the second output variable (temperature) I have no idea how to define hard BCs and Dirichlet IC so as to satisfy them.

    u, T = y[:,0:1], y[:,1:2]  #Defining Outputs displacement and temperature
    r, t = x[:,0:1], x[:,1:2]   #Defining Inputs Radial distance and time
    r_in=1.0 

1
r_out=1.5

net.apply_output_transform( lambda x, y : tf.concat( [ (1-x[:,0:1])*(1.5-x[:,0:1])*((1-np.exp(-x[:,1:2])**2)*y[ :, 0:1], **(for this one I have no idea)** ], axis =1 ) )

1- is the first argument right?
2-How can I define the second argument to satisfy the Dirichlet boundary and initial conditions?

@katayooneshkofti
Copy link
Author

based on the paper with the title of "Systematic Construction of Neural Forms for Solving Partial Differential Equations Inside Rectangular Domains, Subject to Initial, Boundary and Interface Conditions", I tried to define hard BCs for T(r_in, t)=T_star and T(r_out, t)=0, T(x,0)=0, but I think this does not satisfy the boundary conditions:

for second output y[:,1:2]:
(1-x)(1.5-x)tT+2T_star(1.5-x)

I have no idea how can I define these hard BCs?

@lululxvi
Copy link
Owner

lululxvi commented Apr 6, 2021

The paper you mentioned is a good reference. If you still cannot define hard BCs based on that paper, you can only use hard BCs for some BCs, and for the other BCs, use the standard loss functions.

@katayooneshkofti
Copy link
Author

@lululxvi
Thank you for your response. The problem is that the train loss related to the T (r_in, t)=T* is large and I've read all the possible solutions of FAQ for "large train loss" but I can't find any solution for that.

@lululxvi
Copy link
Owner

Then impose T* as hard constraint.

@katayooneshkofti
Copy link
Author

katayooneshkofti commented Apr 14, 2021

@lululxvi would you please help me with the code how can I impose T* as hard bc?

@lululxvi
Copy link
Owner

Something like

def f(x, y):
    r, t = x[:, 0:1], x[:, 1:2]
    T_star = ... # the function on t
    return (r - r_in) * y + T_star

net.apply_output_transform(f)

@katayooneshkofti
Copy link
Author

Dear @lululxvi
I applied your suggestion for "net.apply_output_transform(f)," but the training loss related to that does not appear to be zero. Is it because the given boundary condition is a function of time, preventing it from having a zero value? I have tried many alternatives, but nothing seems to be correct. This is the only boundary condition that has a significant training loss. Do you have any suggestions?

@lululxvi
Copy link
Owner

It should be exactly zero, see Section 2.3 in https://arxiv.org/abs/2102.04626. If not, then there must be something wrong with the code.

@katayooneshkofti
Copy link
Author

@lululxvi Thank you, I will read it carefully.

@FZUcipher
Copy link

It should be exactly zero, see Section 2.3 in https://arxiv.org/abs/2102.04626. If not, then there must be something wrong with the code.

Dear @lululxvi ,
Suppose I have such boundary conditions and initial conditions:
BC: U(0,t)=U(1,t)=0; Ux(0,t)=Ux(1,t)=0
IC: U(x,0)=0
When I make hard constraints on the left and right boundaries and the initial boundary, as follow(I omitted a lot of places):

......
def boundary_l(x, on_boundary):
return on_boundary and np.isclose(x[0], 0)

def boundary_r(x, on_boundary):
return on_boundary and np.isclose(x[0], 1)

def boundary_initial(x, _):
return np.isclose(x[1], 0)
......
bc_1 = dde.icbc.DirichletBC(geomtime, lambda x: 0, boundary_l)
bc_2 = dde.icbc.NeumannBC(geomtime, lambda x: 0, boundary_l)
bc_3 = dde.icbc.DirichletBC(geomtime, lambda x: 0, boundary_r)
bc_4 = dde.icbc.NeumannBC(geomtime, lambda x: 0, boundary_r)
ic_1 = dde.icbc.IC(geomtime, lambda x: 0, lambda _, on_initial: on_initial)

data = dde.data.TimePDE(
geomtime,
pde,
[bc_1, bc_2, bc_3, bc_4, ic_1],
......
net.apply_output_transform(lambda x, y: x[:, 0:1] * (1 - x[:, 0:1]) * x[:, 1:2] * y)

After treatment, the losses of bc_1, bc_3 and ic_1 are all 0, They are also fixed at 0.
So my question is, do I still need to fill in the boundary conditions in the class “dde.data.TimePDE”?
Simply change to

data = dde.data.TimePDE(
geomtime,
pde,
[bc_2, bc_4],

Is that right? Is there any difference from the previous grammar?

@forxltk
Copy link

forxltk commented Jun 21, 2022

@FZUcipher Yes, your code looks good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants