deepxde icon indicating copy to clipboard operation
deepxde copied to clipboard

TypeError: cos(): argument 'input' (position 1) must be Tensor, not SymbolicTensor (Demo: elasticity_plate.py)

Open NischalKMapakshi opened this issue 1 year ago • 0 comments

Dear LuLu, I'm trying to define a body force function that goes into PDE it has trignometric functions like sin and cos, I found out an example in your Demos "elasticity_plate.py".

However when I run the code I get the following error:

issue1: TypeError: cos(): argument 'input' (position 1) must be Tensor, not SymbolicTensor (This is for the f you've defined)

issue2: I have an exact solution for [ux,uy,p], this is how I defined it: def func_exact(x): # x1, x2 = x[:, 0:1], x[:, 1:2] return [(sin(pix[:, 0:1])cos(pix[:, 1:2])), -cos(pix[:, 0:1])sin(pix[:, 1:2]), sin(pi*x[:, 0:1])sin(pix[:, 1:2])]

TypeError: sin(): argument 'input' (position 1) must be Tensor, not numpy.ndarray

For a easier understanding I'm copying that code here:

"""Backend supported: pytorch, paddle

Implementation of the linear elasticity 2D example in paper https://doi.org/10.1016/j.cma.2021.113741. References: https://github.com/sciann/sciann-applications/blob/master/SciANN-Elasticity/Elasticity-Forward.ipynb. """ import deepxde as dde import numpy as np

lmbd = 1.0 mu = 0.5 Q = 4.0

Define function

if dde.backend.backend_name == "pytorch": import torch

sin = torch.sin
cos = torch.cos

elif dde.backend.backend_name == "paddle": import paddle

sin = paddle.sin
cos = paddle.cos

geom = dde.geometry.Rectangle([0, 0], [1, 1])

def boundary_left(x, on_boundary): return on_boundary and dde.utils.isclose(x[0], 0.0)

def boundary_right(x, on_boundary): return on_boundary and dde.utils.isclose(x[0], 1.0)

def boundary_top(x, on_boundary): return on_boundary and dde.utils.isclose(x[1], 1.0)

def boundary_bottom(x, on_boundary): return on_boundary and dde.utils.isclose(x[1], 0.0)

Exact solutions

def func(x): ux = np.cos(2 * np.pi * x[:, 0:1]) * np.sin(np.pi * x[:, 1:2]) uy = np.sin(np.pi * x[:, 0:1]) * Q * x[:, 1:2] ** 4 / 4

E_xx = -2 * np.pi * np.sin(2 * np.pi * x[:, 0:1]) * np.sin(np.pi * x[:, 1:2])
E_yy = np.sin(np.pi * x[:, 0:1]) * Q * x[:, 1:2] ** 3
E_xy = 0.5 * (
    np.pi * np.cos(2 * np.pi * x[:, 0:1]) * np.cos(np.pi * x[:, 1:2])
    + np.pi * np.cos(np.pi * x[:, 0:1]) * Q * x[:, 1:2] ** 4 / 4
)

Sxx = E_xx * (2 * mu + lmbd) + E_yy * lmbd
Syy = E_yy * (2 * mu + lmbd) + E_xx * lmbd
Sxy = 2 * E_xy * mu

return np.hstack((ux, uy, Sxx, Syy, Sxy))

ux_top_bc = dde.icbc.DirichletBC(geom, lambda x: 0, boundary_top, component=0) ux_bottom_bc = dde.icbc.DirichletBC(geom, lambda x: 0, boundary_bottom, component=0) uy_left_bc = dde.icbc.DirichletBC(geom, lambda x: 0, boundary_left, component=1) uy_bottom_bc = dde.icbc.DirichletBC(geom, lambda x: 0, boundary_bottom, component=1) uy_right_bc = dde.icbc.DirichletBC(geom, lambda x: 0, boundary_right, component=1) sxx_left_bc = dde.icbc.DirichletBC(geom, lambda x: 0, boundary_left, component=2) sxx_right_bc = dde.icbc.DirichletBC(geom, lambda x: 0, boundary_right, component=2) syy_top_bc = dde.icbc.DirichletBC( geom, lambda x: (2 * mu + lmbd) * Q * np.sin(np.pi * x[:, 0:1]), boundary_top, component=3, )

def fx(x): return ( -lmbd * ( 4 * np.pi2 * cos(2 * np.pi * x[:, 0:1]) * sin(np.pi * x[:, 1:2]) - Q * x[:, 1:2] ** 3 * np.pi * cos(np.pi * x[:, 0:1]) ) - mu * ( np.pi2 * cos(2 * np.pi * x[:, 0:1]) * sin(np.pi * x[:, 1:2]) - Q * x[:, 1:2] ** 3 * np.pi * cos(np.pi * x[:, 0:1]) ) - 8 * mu * np.pi**2 * cos(2 * np.pi * x[:, 0:1]) * sin(np.pi * x[:, 1:2]) )

def fy(x): return ( lmbd * ( 3 * Q * x[:, 1:2] ** 2 * sin(np.pi * x[:, 0:1]) - 2 * np.pi2 * cos(np.pi * x[:, 1:2]) * sin(2 * np.pi * x[:, 0:1]) ) - mu * ( 2 * np.pi2 * cos(np.pi * x[:, 1:2]) * sin(2 * np.pi * x[:, 0:1]) + (Q * x[:, 1:2] ** 4 * np.pi**2 * sin(np.pi * x[:, 0:1])) / 4 ) + 6 * Q * mu * x[:, 1:2] ** 2 * sin(np.pi * x[:, 0:1]) )

def pde(x, f): E_xx = dde.grad.jacobian(f, x, i=0, j=0) E_yy = dde.grad.jacobian(f, x, i=1, j=1) E_xy = 0.5 * (dde.grad.jacobian(f, x, i=0, j=1) + dde.grad.jacobian(f, x, i=1, j=0))

S_xx = E_xx * (2 * mu + lmbd) + E_yy * lmbd
S_yy = E_yy * (2 * mu + lmbd) + E_xx * lmbd
S_xy = E_xy * 2 * mu

Sxx_x = dde.grad.jacobian(f, x, i=2, j=0)
Syy_y = dde.grad.jacobian(f, x, i=3, j=1)
Sxy_x = dde.grad.jacobian(f, x, i=4, j=0)
Sxy_y = dde.grad.jacobian(f, x, i=4, j=1)

momentum_x = Sxx_x + Sxy_y - fx(x)
momentum_y = Sxy_x + Syy_y - fy(x)

stress_x = S_xx - f[:, 2:3]
stress_y = S_yy - f[:, 3:4]
stress_xy = S_xy - f[:, 4:5]

return [momentum_x, momentum_y, stress_x, stress_y, stress_xy]

data = dde.data.PDE( geom, pde, [ ux_top_bc, ux_bottom_bc, uy_left_bc, uy_bottom_bc, uy_right_bc, sxx_left_bc, sxx_right_bc, syy_top_bc, ], num_domain=500, num_boundary=500, solution=func, num_test=100, )

layers = [2, [40] * 5, [40] * 5, [40] * 5, [40] * 5, 5] activation = "tanh" initializer = "Glorot uniform" net = dde.nn.PFNN(layers, activation, initializer)

model = dde.Model(data, net) model.compile("adam", lr=0.001) losshistory, train_state = model.train(epochs=5000)

dde.saveplot(losshistory, train_state, issave=True, isplot=True)

Any help is appreciated thank you.

NischalKMapakshi avatar Nov 01 '23 13:11 NischalKMapakshi