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

Componen-Wise Bondary Conditions on vectorial PDEs #978

Open cdelv opened 1 year ago

cdelv commented 1 year ago

I'm trying to solve a forward Navier Stokes equation. I have the following code

import deepxde as dde
import numpy as np
import matplotlib.pyplot as plt
dde.config.real.set_float64()
rho = 1
mu = 1e-3

def Navier_Stokes_Equation(x, y):
    u = y[:, 0:1]
    v = y[:, 1:2]
    p = y[:, 2:3]
    du_x = dde.grad.jacobian(y, x, i=0, j=0)
    du_y = dde.grad.jacobian(y, x, i=0, j=1)
    du_t = dde.grad.jacobian(y, x, i=0, j=2)
    dv_x = dde.grad.jacobian(y, x, i=1, j=0)
    dv_y = dde.grad.jacobian(y, x, i=1, j=1)
    dv_t = dde.grad.jacobian(y, x, i=1, j=2)
    dp_x = dde.grad.jacobian(y, x, i=2, j=0)
    dp_y = dde.grad.jacobian(y, x, i=2, j=1)
    du_xx = dde.grad.hessian(y, x, component=0, i=0, j=0)
    du_yy = dde.grad.hessian(y, x, component=0, i=1, j=1)
    dv_xx = dde.grad.hessian(y, x, component=1, i=0, j=0)
    dv_yy = dde.grad.hessian(y, x, component=1, i=1, j=1)
    continuity = du_x + dv_y
    x_momentum = rho*(du_t + u*du_x + v*du_y) + dp_x - mu*(du_xx + du_yy)
    y_momentum = rho*(dv_t + u*dv_x + v*dv_y) + dp_y - mu*(dv_xx + dv_yy)
    return [continuity, x_momentum, y_momentum]

Lx, Ly = 5.0, 2.0
x0, y0, R = 2.5, 1, 0.25
tmax = 7
geom = dde.geometry.Rectangle([0, 0], [Lx, Ly])
time_domain = dde.geometry.TimeDomain(0, tmax)

The problem comes when I try to specify Boundary Conditions for specific components, for example

def zero(x):
    return 0

def boundary(x, on_boundary):
    return on_boundary

p = dde.icbc.NeumannBC(geom, zero, boundary, component=2)

data = dde.data.TimePDE(
    geomtime,
    Navier_Stokes_Equation,
    [p],
    num_domain=700,
    num_boundary=200,
    num_initial=100,
)

This fails with the error

operands could not be broadcast together with shapes (1000,3) (2,) Is there an other way of implementing these conditions or is this a bug?

forxltk commented 1 year ago

geom should be geomtime in the BC.

cdelv commented 1 year ago

Thanks, @forxltk. I made your correction and it works fine. I don't know how I missed that.

haytey commented 1 year ago

Thank you for your question. It's very enlightening.

Mouhamedsaw commented 8 months ago

Thanks again for this nice example. As far as I know, dde.saveplot(losshistory, train_state, issave=True, isplot=True) outputs the plot of the train and test losses. Please I just want to know how to plot the solutions (i.e., $u$, $v$, and $p$ in this example) at t=tmax with deepxde.

Thank you for your time and your patience.