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

Error occurred when implementing geometry difference #561

Closed kachi1992 closed 2 years ago

kachi1992 commented 2 years ago

Dear Prof. Lu

I am trying to construct a complex geometry by using the difference method. It is simply a large rectangle with removing a smaller rectangle. The union implementation works well but the difference results in an error as below. Could you tell me how to fix the error? Thanks!

RuntimeError                              Traceback (most recent call last)
C:\Users\LIC32C~1.JIA\AppData\Local\Temp/ipykernel_23124/1412010660.py in <module>
     56 
     57 model.compile("adam", lr=0.001)
---> 58 model.train(epochs=20000)
     59 model.compile("L-BFGS")
     60 losshistory, train_state = model.train()

~\Anaconda3\envs\pytorch-gpu\lib\site-packages\deepxde\utils\internal.py in wrapper(*args, **kwargs)
     20     def wrapper(*args, **kwargs):
     21         ts = timeit.default_timer()
---> 22         result = f(*args, **kwargs)
     23         te = timeit.default_timer()
     24         print("%r took %f s\n" % (f.__name__, te - ts))

~\Anaconda3\envs\pytorch-gpu\lib\site-packages\deepxde\model.py in train(self, epochs, batch_size, display_every, disregard_previous_best, callbacks, model_restore_path, model_save_path)
    416         self.train_state.set_data_train(*self.data.train_next_batch(self.batch_size))
    417         self.train_state.set_data_test(*self.data.test())
--> 418         self._test()
    419         self.callbacks.on_train_begin()
    420         if optimizers.is_external_optimizer(self.opt_name):

~\Anaconda3\envs\pytorch-gpu\lib\site-packages\deepxde\model.py in _test(self)
    548             self.train_state.y_pred_train,
    549             self.train_state.loss_train,
--> 550         ) = self._outputs_losses(
    551             True,
    552             self.train_state.X_train,

~\Anaconda3\envs\pytorch-gpu\lib\site-packages\deepxde\model.py in _outputs_losses(self, training, inputs, targets, auxiliary_vars)
    345             # TODO: auxiliary_vars
    346             self.net.requires_grad_(requires_grad=False)
--> 347             outs = self.outputs_losses(training, inputs, targets)
    348             self.net.requires_grad_()
    349         elif backend_name == "jax":

~\Anaconda3\envs\pytorch-gpu\lib\site-packages\deepxde\model.py in outputs_losses(training, inputs, targets)
    227             self.net.inputs = torch.as_tensor(inputs)
    228             self.net.inputs.requires_grad_()
--> 229             outputs_ = self.net(self.net.inputs)
    230             # Data losses
    231             if targets is not None:

~\Anaconda3\envs\pytorch-gpu\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
   1100         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1101                 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1102             return forward_call(*input, **kwargs)
   1103         # Do not call functions when jit is used
   1104         full_backward_hooks, non_full_backward_hooks = [], []

~\Anaconda3\envs\pytorch-gpu\lib\site-packages\deepxde\nn\pytorch\fnn.py in forward(self, inputs)
     31             x = self._input_transform(x)
     32         for linear in self.linears[:-1]:
---> 33             x = self.activation(linear(x))
     34         x = self.linears[-1](x)
     35         if self._output_transform is not None:

~\Anaconda3\envs\pytorch-gpu\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
   1100         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1101                 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1102             return forward_call(*input, **kwargs)
   1103         # Do not call functions when jit is used
   1104         full_backward_hooks, non_full_backward_hooks = [], []

~\Anaconda3\envs\pytorch-gpu\lib\site-packages\torch\nn\modules\linear.py in forward(self, input)
    101 
    102     def forward(self, input: Tensor) -> Tensor:
--> 103         return F.linear(input, self.weight, self.bias)
    104 
    105     def extra_repr(self) -> str:

~\Anaconda3\envs\pytorch-gpu\lib\site-packages\torch\nn\functional.py in linear(input, weight, bias)
   1846     if has_torch_function_variadic(input, weight, bias):
   1847         return handle_torch_function(linear, (input, weight, bias), input, weight, bias=bias)
-> 1848     return torch._C._nn.linear(input, weight, bias)
   1849 
   1850 

RuntimeError: expected scalar type Float but found Double

The script I am using is as follows:


import deepxde as dde
import numpy as np

def pde(x, y):
    dy_t = dde.grad.jacobian(y, x, i=0, j=2)
    dy_xx = dde.grad.hessian(y, x, i=0, j=0)
    dy_yy = dde.grad.hessian(y, x, i=1, j=1)
    return dy_t - 1.0*(dy_xx+dy_yy)

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

def boundary_d(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[0], 1) and (x[1]>0.75 or x[1]<0.25) 

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

def boundary_r2(x, on_boundary):
    if(np.isclose(x[0], 0.5) and 0.25<=x[1]<=0.75):
        return on_boundary and True
    if((np.isclose(x[1], 0.25) or np.isclose(x[1], 0.75)) and 0.5<=x[0]<=1.0):
        return on_boundary and True
    return on_boundary and False

def func(x):
    x[:,0] = 0
    return

def func_n(x):
    return np.zeros((len(x),1))

geom1 = dde.geometry.Rectangle([0, 0], [1.0,1.0])
geom2 = dde.geometry.Rectangle([0.5, 0.25], [1.0,0.75])
geom = geom1 - geom2
timedomain = dde.geometry.TimeDomain(0, 1)
geomtime = dde.geometry.GeometryXTime(geom, timedomain)

bc_l = dde.icbc.NeumannBC(geomtime, func_n, boundary_l)
bc_r = dde.icbc.DirichletBC(geomtime, lambda x: 2, boundary_r)
bc_d = dde.icbc.DirichletBC(geomtime, lambda x: 1, boundary_d)
bc_u = dde.icbc.DirichletBC(geomtime, lambda x: 3, boundary_u)
bc_r2 = dde.icbc.DirichletBC(geomtime, lambda x: 2, boundary_r2)
ic = dde.icbc.IC(geomtime, lambda x: 0, lambda _, on_initial: on_initial

data = dde.data.TimePDE(geomtime, pde, [bc_l,bc_r,bc_d,bc_u,bc_r2,ic], num_domain=20000, num_boundary=4000, num_initial=1000, num_test=20000)
net = dde.nn.FNN([3] + [20] * 5 + [1], "tanh", "Glorot uniform")
model = dde.Model(data, net)

model.compile("adam", lr=0.001)
model.train(epochs=20000)
model.compile("L-BFGS")
losshistory, train_state = model.train()
dde.saveplot(losshistory, train_state, issave=True, isplot=True)```
lululxvi commented 2 years ago

What is your backend?

kachi1992 commented 2 years ago

Pytorch

lululxvi commented 2 years ago

Bug fixed. Please download the updated code.

kachi1992 commented 2 years ago

Thank you for you help. Now it runs well without errors.