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

Very complicated boundary #264

Closed
Ryszard2 opened this issue Apr 11, 2021 · 18 comments
Closed

Very complicated boundary #264

Ryszard2 opened this issue Apr 11, 2021 · 18 comments

Comments

@Ryszard2
Copy link

Hello @lululxvi

is it somehow possible to define a boundary like this one?

D

BC

D BC

I do have all the boolean arrays that identify the situation in all the area (domain/no_domain, boundary/no_boundary), indeed I can anchor the training points in the domain:

TP

That makes me confident on the domain (and Initial Condition) part, but I can't work out the definition of the boundary.
I thought of the scipy.interpolate.interp2d and the tf.convert_to_tensor function to convert those boolean arrays into tensor or functions, but still I can't take advantage of all these ingredients.

Thank you,
Riccardo

@smao-astro
Copy link
Contributor

Hi Riccardo,

If your BC is pure Dirichlet, then maybe try deepxde.boundary_conditions.PointSetBC.

@Ryszard2
Copy link
Author

Ryszard2 commented Apr 12, 2021

Thank you @smao-astro

Is it possible to do the same - to provide the array of the appropriate set of points, along with the corresponding values - for the Initial Conditions? I clarify that I'm already able to anchor all the training points that I want in the domain/timedomain.

@smao-astro
Copy link
Contributor

I guess the answer is Yes, though I haven't try that -- PointSetBC is mainly for cases that the constraint NN(x) = f(x) can not be written in analytical expression.

@xy1015874723
Copy link

Excuse me, i have a problem about how to anchor training points , and could you please share your code about anchoring training
points?

@Ryszard2
Copy link
Author

Ryszard2 commented Apr 12, 2021

@xy1015874723

I found out how to do it here: #64
Essentially it's about building a np.array populated with the coordinates you choose in the space-time domain.

My case is kind of complicated and my code is less than newbie level but here is what it's about:

  • I have a boolean matrix L
  • it's size are nrows, ncols
  • every entry of the matrix matches a point in the xy space
  • the step of one row (or one column) in the matrix correspond to a 15 meters step in the xy space

then:

  • I make sure that the training points are only in places where the entry of the matrix is 1
  • I choose the step that I want in x, y, t, since the problem is in the space-time
  • I call TP the np.array of the training points
time  = 10

TP  = np.array([-1, -1, -1])
TP1 = np.array([-1, -1, -1])

i = 0
j = 0
k = 0

while k < time+1:
  while i < nrows:
    while j < ncols:
      if L[i,j] == 1:
        TP1 = np.array([15*j, 15*i, k])
        TP  = np.vstack((TP, TP1))
      j = j + 20   # 20 is the pace along x
    i = i + 20     # 20 is the pace along y
    j = 0
  k = k + 1        #  1 is the pace along time
  i = 0

TP = np.delete(TP,0,0)

Then, when it's time to define the model, I'm going to write something like this:

data = dde.data.TimePDE(
    geomtime, pde, BC,
    num_domain   = 0,
    num_boundary = 100,
    num_initial  = 0)
    anchors      = TP)

@xy1015874723
Copy link

@Ryszard2 thanks a lot

@Ryszard2
Copy link
Author

@smao-astro

I used deepxde.boundary_conditions.PointSetBC.

If I understand it correctly it works like this:

BC_u = dde.boundary_conditions.PointSetBC(X, Y, component=0)

  • X is an array where I anchor the boundary points in the space-time
  • Y is a number, a scalar, that is the same value of Dirichlet on every single point defined in X

I'd have two questions:

  1. Is it possible to use this very function for the Initial Conditions as well, interpreted as Dirichlet Boundary Condition on the border t = 0 of the space-time domain?
  2. Is it possible to enter a Y that is not a scalar but an array, since I'd have to impose an IC that changes from point to point in that complicated domain?

Thank you,
Riccardo

@smao-astro
Copy link
Contributor

By default, Y here is a (N, 1) array which store the true values of outputs[:, component:, component+1] corresponding to X, in other word, the X and Y is one-to-one match on axis 0.

If you have multiple outputs (y1, y2, etc), then for every y specify a PointSetBC object.

dde.boundary_conditions.PointSetBC(X, Y0, component=0)
dde.boundary_conditions.PointSetBC(X, Y1, component=1)

@Ryszard2
Copy link
Author

Thank you @smao-astro
now I can anchor Dirichlet data pointwise, wherever I need it, whatever the value in each point.

10

But...

Even though I applied rigorously all the BC conditions, the outline that I mean to be the boundary of the domain isn't perceived as boundary by the NN, since the solutions are kind of crazy.

11

u0

Here is mi doubt:

  1. Do I have to interpret as boundary every single point in the rectangle (actually a cuboid in the spacetime) that is outside my byzantine domain?
  2. Do I have to apply BCs for all the portion of rectangle that is not in my imaginary domain?

I never placed a single training point (or IC point, or BC point) in all that major portion of rectangle that I meant to be outside the domain. My good intention of define a partial domain seems hardly to be understood by the NN.

Thank you,
Riccardo

@Ryszard2
Copy link
Author

Ryszard2 commented Apr 14, 2021

I may be wrong, but brooding over this problem I got to think that the focal point is not the boundary, it's the whole rectangular domain instead. I'm getting pretty sure that my attention on the boundary is the wrong way to the solution of this problem.

This is a 2D shallow water equations problem. It takes place in a rectangular domain whose depth changes from point to point, as shown in the figures

6

7

  • The colored area is the valley, the water can occupy a place only here.
  • The white area is occupied by mountains whose elevation is out of reach for the water.

That means that the white portion is an area where the IC for the water height is zero. Inside the valley, instead, there is a proper IC distribution for the water height.

I do not have the function of this depth, I have the matrix whose elements provide the value of the depth in every single point of the whole rectangular domain. This is the real problem.
I have two question about it:

  1. For the IC I think that I can resolve by treating it as a Dirichlet BC for the boundary t=0, and with the PointSetBC I will be able to anchor every element of the matrix that I'm willing to take. Am I right?
  2. For the PDE I don't know what to do, at all. There's one element in the PDE that is a f(x,y) but I have a 2D grid instead. I've already tried to handle it by:
    • first with tf.convert_to_tensor for the matrix
    • then with tfp.math.batch_interp_regular_nd_grid for the new tensor, inside the PDE.

It didn't work out, I always got the Resource Exhausted error

What can be done?

Thank you,
Riccardo

@lululxvi
Copy link
Owner

@Ryszard2 I guess your question is that you only need to enforce the PDE inside colored area. To do this, you ask DeepXDE not to sample any point in the recrangle by setting num_domain, num_boundary, num_initial to be 0. Then you sample the points in the colored area by yourself:

data = dde.data.TimePDE(
    geomtime, pde, BC,
    num_domain   = 0,
    num_boundary = 0,
    num_initial  = 0)
    anchors      = TP  # the points your sampled
)

You may test this idea by using a simple example, e.g., a disk inside a square for a poisson equation. Also, see #161

@Ryszard2
Copy link
Author

Ryszard2 commented Apr 21, 2021

Thank you @lululxvi

  1. I anchored the training points in the colored domain
  2. I anchored the training points also in the boundary of the colored domain
  3. I defined the BC on that intricated boundary with the dde.boundary_conditions.PointSetBC function

The problems are:

  1. The spatial domain is enormous, like 15,000 x 6,000. I scaled it heavily up to a 11 x 5 domain because I was having repelling solutions (I tested the mammoth domain issue in other working codes, to verify the impossibility of the training in gigantic domains)
  2. The IC for the water height ranges from 0 to 60, the range of the height throughout the process remains more or less like 0 to 40. I scaled the height variable to a O(1) peak
  3. The time domain is colossal, as it goes up to over 1,000 seconds but I'd opt for not to scale it.

The NN learns well the IC, yet it fails to learn what happens from second one on.

The questions I can formulate from my point of view are:

  1. is there a strategy for distance or refine the training points in a problem like this? I can't keep them near at all in the time direction since the road to 1,000 seconds would get to a billion training points
  2. is there a strategy for the training in a problem like this? NN architecture, optimizer or whatever?

Thank you
Riccardo

@lululxvi
Copy link
Owner

lululxvi commented Apr 26, 2021

Solving large scale PDEs for a very long time is always difficult. You may consider training in parallel in multiple GPUs, see #39. Also, you may consider train using mini-batch, i.e., in each iteration using partial data points.

By the way, if you are solving an inverse problem, then there is no need to consider the whole domain.

@Ryszard2
Copy link
Author

Thank you @lululxvi

So far the solution is wrong.

I scaled the space by the thousands, the time by the thousands, and the water height by like 50: the problem is now like unitary in every aspect.
Supposing that my code is right, is the mini-batch approach more likely to learn the problem rather than the usual full descent step?

Thank you
Riccardo

@lululxvi
Copy link
Owner

Usually, there is no significant difference between mini-batch and full batch, assuming that your training data points are sufficient enough. How about starting from a simple example, e.g., a polygon with a simple PDE like Possion eq to test the code etc?

@Ryszard2
Copy link
Author

Ryszard2 commented Jun 13, 2021

Thank you @lululxvi
I've anchored a small square domain inside a bigger square: it worked great, even with the bothersome shallow water system!

I can't say the same for this complicated domain, so, I've abandoned the approach of anchoring by myself the domain and the boundary points.
So, I've covered the giant text file of the data problem and I built by myself the vertices of the polygon. Like 1600 points put in meticulous counterclockwise order. The polygon is very complicated but it's closed and it's made up of strictly distinct points. Great, so far.

Now I can build the polygon and let DeepXDE set the domain/boundary points but here's the new problem: DeepXDE takes like half an hour to process the line:

data = dde.data.PDE(geom, pde, bc, num_domain = 1000, num_boundary = 500)

It takes forever, and we talking about a minimum amount of points!
Why is that?

Seems like a very complicated polygon is not manageable.

Thank you
Riccardo

@lululxvi
Copy link
Owner

lululxvi commented Jun 16, 2021

Great to know that it works for a simpler geometry!

  • If the polygon contains thousands of points, then sampling could be slow. I guess sampling 1000 points in the domain is very slow, but sampling 500 boundary points is OK. Could you have a check and confirm if I am correct?
  • I have modified the code to make the sampling 400x faster. You need to install v0.11.2. Let me know if it works.

@Ryszard2
Copy link
Author

I have modified the code to make the sampling 400x faster.

Gosh, it sounds good!
And it works: sampling tens of thousands of points, inside an intricated polygon and on its boundary, is now a matter of seconds! 🥇

I guess sampling 1000 points in the domain is very slow, but sampling 500 boundary points is OK.

I'm not sure who was the guilty between the domain points or the boundary ones. I honestly can't manage a check about it since I can run only the up-to-date version of DeepXDE.

I thank you so much, @lululxvi
Riccardo

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