lululxvi / deepxde

A library for scientific machine learning and physics-informed learning
https://deepxde.readthedocs.io
GNU Lesser General Public License v2.1
2.59k stars 733 forks source link

What about time PDEs with terminal (instead of initial) boundary conditions? #566

Open adolfocorreia opened 2 years ago

adolfocorreia commented 2 years ago

Any tips on using DeepXDE to solve PDEs with terminal time boundary conditions? Consider a PDE problem for a function u(x,t) defined on the time interval [0, T] with the terminal boundary condition u(x,T) = g(x). How should I model it with DeepXDE?

First I tried defining the TimeDomain object with inverted arguments

time_domain = dde.geometry.TimeDomain(T, 0.0)

but I got an error because DeepXDE is internally computing this interval's diameter as a negative value (-T).

I also tried defining an on_terminal function such as

def on_terminal(x, _):
    return np.isclose(x[1], T)

and passing it to the IC object as in

time_domain = dde.geometry.TimeDomain(0.0, T)
space_domain = dde.geometry.Interval(S1, S2)
geom_time = dde.geometry.GeometryXTime(space_domain, time_domain)
terminal_condition = dde.icbc.IC(geom_time, g, on_terminal)

but the TimePDE object still samples from the time boundary with t = 0. Any way to make it sample with t = T?

Maybe there should be a dde.icbc.TC class specific for such terminal condition problems.

lululxvi commented 2 years ago

Use BC for this.

adolfocorreia commented 2 years ago

Thanks for the suggestion. My model is working now using BC!

However, if I may share my impressions on this workaround, it seems not ideal to me to conflate space and time dimensions in the same geometry structure. Besides, DeepXDE is still sampling from both t=0 and t=T and then filtering out t=0 points. As a side effect of this, the actual number of boundary train points being used is lower than the num_boundary argument passed to the PDE class. To compensate for this I need to artificially increase the value of the argument by a factor of at least two.

I'm thinking about implementing my own TerminalCondition class and maybe I could share it with you later.

In any case, I think this issue could be closed if you don't have any more comments.

lululxvi commented 2 years ago

Yes, implementing a new TerminalCondition would be better.

doubao2 commented 2 years ago

Hello, I have the same problem with you. I consider that the initial condition is sampled from t = T, and IC cannot be used. Apply BC according to Dr. Lulu's method, but my initial condition has a Nan value during training. How do you use BC to sample the initial condition at t = T?


geom = dde.geometry.Interval(0, 10) 
timedomain = dde.geometry.TimeDomain(0, 1)
geomtime = dde.geometry.GeometryXTime(geom, timedomain)
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],10)
def boundary_t(x,on_boundary):
return on_boundary and np.isclose(x[1],1)#Actually, this is my initial condition
def func1(x):
return K* np.exp(-r*(T-x[:,1:2]))
def func2(x):
return np.maximum(K-(x[:,0:1]),0)#Actually, this is my initial condition

bc_l = dde.DirichletBC(geomtime,func1, boundary_l) bc_r = dde.DirichletBC(geomtime,lambda x:0, boundary_r) bc_i = dde.DirichletBC(geomtime, func2, boundary_t)


![image](https://user-images.githubusercontent.com/103088959/167990592-bc46a631-6c5e-4e02-b45a-db4aa247c6ee.png)

Unfortunately,  the Nan value appears in the bc_i column.I suspect it is due to the problem of definition, so I want to ask you how to solve this problem. I look forward to your reply. Thank you in advance!
lululxvi commented 2 years ago

The issue could be that there are no points sampled for t=T. You may sample some points manually and then pass them via anchors as dde.data.TimePDE(..., anchors=)

doubao2 commented 2 years ago

When I applied pointseBC according to your method, the initial conditions finally worked. Thank you very much!