lululxvi / deepxde

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

Problems that occur when using deepxde to solve convection diffusion equation in 1D #250

Closed juice-drinker closed 3 years ago

juice-drinker commented 3 years ago

Dear @lululxvi I am trying to solve a diffusion equation in 1D according to the examples, but some error occurs.The error message is as follows:

"""~~~~~~""" Traceback (most recent call last): File "C:\Users\Administrator\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\ops\math_ops.py", line 1136, in binary_op_wrapper out = r_op(x) File "C:\Users\Administrator\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\ops\math_ops.py", line 1155, in r_binary_op_wrapper x = ops.convert_to_tensor(x, dtype=y.dtype.base_dtype, name="x") File "C:\Users\Administrator\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\framework\ops.py", line 1475, in convert_to_tensor (dtype.name, value.dtype.name, value)) ValueError: Tensor conversion requested dtype float64 for Tensor with dtype float32: <tf.Tensor 'cond/strided_slice_2:0' shape=(?, 1) dtype=float32>

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "D:/code/DL/examples/try_2.py", line 74, in main() File "D:/code/DL/examples/try_2.py", line 49, in main model.compile("adam", lr=1e-3,metrics = ["l2 relative error"]) File "D:\code\DL\deepxde\utils.py", line 52, in wrapper result = f(*args, kwargs) File "D:\code\DL\deepxde\model.py", line 93, in compile self.losses = self.data.losses(self.net.targets, self.net.outputs, loss, self) File "D:\code\DL\deepxde\data\pde.py", line 159, in losses return tf.cond(tf.equal(model.net.data_id, 0), losses_train, losses_test) File "C:\Users\Administrator\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\util\dispatch.py", line 201, in wrapper return target(*args, *kwargs) File "C:\Users\Administrator\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\util\deprecation.py", line 507, in new_func return func(args, kwargs) File "C:\Users\Administrator\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 1230, in cond orig_res_t, res_t = context_t.BuildCondBranch(true_fn) File "C:\Users\Administrator\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 1066, in BuildCondBranch original_result = fn() File "D:\code\DL\deepxde\data\pde.py", line 144, in losses_train error = bc.error(self.train_x, model.net.inputs, outputs, beg, end) File "D:\code\DL\deepxde\boundary_conditions.py", line 58, in error return outputs[beg:end, self.component : self.component + 1] - values File "C:\Users\Administrator\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\ops\math_ops.py", line 1141, in binary_op_wrapper raise e File "C:\Users\Administrator\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\ops\math_ops.py", line 1125, in binary_op_wrapper return func(x, y, name=name) File "C:\Users\Administrator\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\util\dispatch.py", line 201, in wrapper return target(*args, **kwargs) File "C:\Users\Administrator\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\ops\math_ops.py", line 527, in subtract return gen_math_ops.sub(x, y, name) File "C:\Users\Administrator\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\ops\gen_math_ops.py", line 10465, in sub "Sub", x=x, y=y, name=name) File "C:\Users\Administrator\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 506, in _apply_op_helper inferred_from[input_arg.type_attr])) TypeError: Input 'y' of 'Sub' Op has type float64 that does not match type float32 of argument 'x'. """~~~~""" And here comes my code: """~~~~""" 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 def main(): def pde(x, y): du_y = tf.gradients(y, x)[0] du_x,du_t = du_y[:,0:1],du_y[:,1:2] du_xx = tf.gradients(du_x,x)[0][:,0:1] return (du_t + 2 * du_x + y - du_xx - x[:,0:1] * 2 tf.exp(-x[:,1:2]) - 2 x[:,0:1] tf.exp(-x[:,1:2]) + 2 * tf.exp(-x[:,1:2]))

geom = dde.geometry.Interval(-1, 1)
timedomain = dde.geometry.TimeDomain(0, 0.99)
geomtime = dde.geometry.GeometryXTime(geom, timedomain)
bc = dde.DirichletBC(geomtime, lambda x: 0, lambda _, on_boundary: on_boundary)
bc2 =  dde.DirichletBC(geomtime, lambda x: tf.exp(-x[:,1:2]), lambda _, on_boundary: on_boundary)
ic = dde.IC(
    geomtime, lambda x: x[:, 0:1]**2, lambda _, on_initial: on_initial
)
data = dde.data.TimePDE(
    geomtime, pde, [bc,bc2, ic], num_domain=2540, num_boundary=80, num_initial=160
)
net = dde.maps.FNN([2] + [20] * 3 + [1], "tanh", "Glorot normal")
model = dde.Model(data, net)
model.compile("adam", lr=1e-3,metrics = ["l2 relative error"])
losshistory, train_state = model.train(epochs=15000)
dde.saveplot(losshistory, train_state, issave=True, isplot=True)

"""~~~~~~""" I just made some modifications to the pde in the example, but some errors appeared. I would like to know what went wrong. I am very grateful if you can answer my question.

lululxvi commented 3 years ago
juice-drinker commented 3 years ago

It workes well after modifications according to your advice,thank you so much!