lululxvi / deepxde

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

'NoneType' object is not subscriptable error in parabolic 2D problem #698

Closed athtareq closed 2 years ago

athtareq commented 2 years ago

Hello, I'm trying to solve this problem: Screenshot from 2022-05-25 11-18-50 eq2

Using this code (for p=4)

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np
import tensorflow as tf

import deepxde as dde

from matplotlib import pyplot

def func(x,y):
    return x*tf.sin(5.0*np.pi*y)+tf.exp(-((x-0.5)**2+(y-0.5)**2)/0.02) 

def pde (x , y) :
    dy_t = dde.grad.jacobian(y, x, i=0, j=1)
    dy_x = tf . gradients (y , x ) [0]
    dy_x , dy_y = dy_x [: , 0:1] , dy_x [: , 1:2]
    dy_xx = tf . gradients ( dy_x , x ) [0][: , 0:1]
    dy_yy = tf . gradients ( dy_y , x ) [0][: , 1:]
    dy_grad=tf.gradients(dy_x,x)[0]
    return dy_t - 3*((dy_x)**2 * dy_grad) - func(x[: , 0:1],x[: , 1:2])

def func_bdy(x) :
    return np.zeros([len(x),1])

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

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

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

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

geom = dde.geometry.Rectangle([0,0],[1,1])
timedomain = dde.geometry.TimeDomain(0, 1)
geomtime = dde.geometry.GeometryXTime(geom, timedomain)

bc1 = dde.DirichletBC(geomtime, func_bdy, boundary_l)
bc2 = dde.DirichletBC(geomtime, func_bdy, boundary_r)
bc3 = dde.PeriodicBC(geomtime, 0, boundary_t)
bc4 = dde.PeriodicBC(geomtime, 0, boundary_b)
ic = dde.icbc.IC(
     geomtime, func, lambda _, on_initial: on_initial
 )
data = dde.data.TimePDE(
    geomtime, pde, [bc1,bc2,bc3,bc4,ic], num_domain=254, num_boundary=80, num_initial=160,num_test=1500
)
metrics_=None
net = dde.maps.FNN([3] + [50] * 4 + [1], "tanh", "Glorot uniform")
model = dde.Model(data, net)

model.compile("adam", lr=0.001, metrics=metrics_,loss='MSE')
model.train(epochs=20000)
model.compile("L-BFGS-B", metrics=metrics_)
losshistory, train_state = model.train()
dde.saveplot(losshistory, train_state, issave=True, isplot=True)

But I get the error

'NoneType' object is not subscriptable in the first model.compile.

Any ideas or mistakes in the code ? Thanks !

athtareq commented 2 years ago

My issue was passing func instead of func_bdy

WANGDI-1291 commented 2 years ago

Hi~ The initial condition needs to be a matrix. I want to know why you just use a vector 'np.zeros([len(x),1])' here. Thanks!

athtareq commented 2 years ago

Hi~ The initial condition needs to be a matrix. I want to know why you just use a vector 'np.zeros([len(x),1])' here. Thanks!

Hello ! 'np.zeros([len(x),1])' represents a len(x) by 1 matrix filled with zeroes