-
Notifications
You must be signed in to change notification settings - Fork 178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
为equatiohn/pde/linear_elasticity.py添加单元测试 #416
Conversation
test:equation/pde/linear_elasticity.py
Thanks for your contribution! |
import pytest | ||
from paddle import nn | ||
|
||
from ppsci.equation import LinearElasticity |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不要从模块中直接导入某一个类,改为 from ppsci import equation
|
||
|
||
def jacobian(y: paddle.Tensor, x: paddle.Tensor) -> paddle.Tensor: | ||
return paddle.grad(y, x, create_graph=True, allow_unused=True)[0] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
去掉 allow_unused=True
def jacobian(y: paddle.Tensor, x: paddle.Tensor) -> paddle.Tensor: | ||
return paddle.grad(y, x, create_graph=True, allow_unused=True)[0] | ||
|
||
|
||
def hessian(y: paddle.Tensor, x: paddle.Tensor) -> paddle.Tensor: | ||
return jacobian(jacobian(y, x), x) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
jacobian 和 hessian 的函数移动到所有函数的上方
sigma_xx, sigma_xy, sigma_xz, sigma_yy, sigma_yz, sigma_zz = ( | ||
output[:, 3:4], | ||
output[:, 4:5], | ||
output[:, 5:6], | ||
output[:, 6:7], | ||
output[:, 7:8], | ||
output[:, 8:9], | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
用 paddle.split 而不是手动切片得到各个分量的结果
if __name__ == "__main__": | ||
pytest.main() | ||
|
||
# test_linear_elasticity_navier_y(1e4, 0.3, None, None, 1, 2, True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
删除注释
"E, nu, lambda_, mu, rho, dim, time", | ||
[ | ||
(1e4, 0.3, None, None, 1, 2, False), | ||
(1e4, 0.3, None, None, 1, 2, True), | ||
(1e4, 0.3, None, None, 1, 3, False), | ||
(1e4, 0.3, None, None, 1, 3, True), | ||
], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
目前这个类还未添加对 E
和 nu
的处理逻辑,应该为 E
和 nu
设置为None,而 lambda_
和 mu
设置为实数值
好滴,正在改 |
已经按照review修改,请过目 |
if lambda_ is None or mu is None: | ||
lambda_ = (E * nu) / ((1 + nu) * (1 - 2 * nu)) | ||
mu = E / (2 * (1 + nu)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
目前 LinearElasticity
对给定 E和nu的情况没有进行处理,可以先把这部分的单测case和相关代码删掉
(1e4, 0.3, None, None, 1, 2, False), | ||
(1e4, 0.3, None, None, 1, 2, True), | ||
(1e4, 0.3, None, None, 1, 3, False), | ||
(1e4, 0.3, None, None, 1, 3, True), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
目前 LinearElasticity
对给定 E和nu的情况没有进行处理,可以先把这部分的单测case删掉
# generate output through the model | ||
output = model(input_data) | ||
|
||
# 提取输出中的u, v, w, sigma_xx, sigma_xy, sigma_xz, sigma_yy, sigma_yz, sigma_zz |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
删除这行注释
test_stress_disp_xx = linear_elasticity.equations["stress_disp_xx"](data_dict) | ||
test_stress_disp_yy = linear_elasticity.equations["stress_disp_yy"](data_dict) | ||
test_stress_disp_xy = linear_elasticity.equations["stress_disp_xy"](data_dict) | ||
test_equilibrium_x = linear_elasticity.equations["equilibrium_x"](data_dict) | ||
test_equilibrium_y = linear_elasticity.equations["equilibrium_y"](data_dict) | ||
test_navier_x = linear_elasticity.equations["navier_x"](data_dict) | ||
test_navier_y = linear_elasticity.equations["navier_y"](data_dict) | ||
test_traction_x = linear_elasticity.equations["traction_x"](data_dict) | ||
test_traction_y = linear_elasticity.equations["traction_y"](data_dict) | ||
if dim == 3: | ||
test_stress_zz = linear_elasticity.equations["stress_disp_zz"](data_dict) | ||
test_stress_xz = linear_elasticity.equations["stress_disp_xz"](data_dict) | ||
test_stress_yz = linear_elasticity.equations["stress_disp_yz"](data_dict) | ||
test_equilibrium_z = linear_elasticity.equations["equilibrium_z"](data_dict) | ||
test_navier_z = linear_elasticity.equations["navier_z"](data_dict) | ||
test_traction_z = linear_elasticity.equations["traction_z"](data_dict) | ||
|
||
assert paddle.allclose(expected_stress_disp_xx, test_stress_disp_xx) | ||
assert paddle.allclose(expected_stress_disp_yy, test_stress_disp_yy) | ||
assert paddle.allclose(expected_stress_disp_xy, test_stress_disp_xy) | ||
assert paddle.allclose(expected_equilibrium_x, test_equilibrium_x) | ||
assert paddle.allclose(expected_equilibrium_y, test_equilibrium_y) | ||
assert paddle.allclose(expected_navier_x, test_navier_x) | ||
assert paddle.allclose(expected_navier_y, test_navier_y) | ||
assert paddle.allclose(expected_traction_x, test_traction_x) | ||
assert paddle.allclose(expected_traction_y, test_traction_y) | ||
if dim == 3: | ||
assert paddle.allclose(expected_stress_disp_zz, test_stress_zz) | ||
assert paddle.allclose(expected_stress_disp_xz, test_stress_xz) | ||
assert paddle.allclose(expected_stress_disp_yz, test_stress_yz) | ||
assert paddle.allclose(expected_equilibrium_z, test_equilibrium_z) | ||
assert paddle.allclose(expected_traction_z, test_traction_z) | ||
assert paddle.allclose(expected_navier_z, test_navier_z) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
把test_xxx用for循环放到字典里,减少冗余代码
test_output_names = ["stress_disp_xx", "stress_disp_yy", ...]
test_output = {}
for name in test_output_names:
test_output[name] = linear_elasticity.equations[name](data_dict)
下面的 assert 同理
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好滴,马上改
linear_elasticity = equation.LinearElasticity( | ||
E=None, nu=None, lambda_=lambda_, mu=mu, rho=rho, dim=dim, time=time | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
虽然E和nu暂时只会是None,但这里的E和nu应该用传进来的参数填写吧
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好的
test_output_names = [ | ||
"stress_disp_xx", | ||
"stress_disp_yy", | ||
"stress_disp_xy", | ||
"equilibrium_x", | ||
"equilibrium_y", | ||
"navier_x", | ||
"navier_y", | ||
"traction_x", | ||
"traction_y", | ||
] | ||
test_output = {} | ||
for name in test_output_names: | ||
test_output[name] = linear_elasticity.equations[name](data_dict) | ||
test_output_names = [ | ||
"stress_disp_xx", | ||
"stress_disp_yy", | ||
"stress_disp_xy", | ||
"equilibrium_x", | ||
"equilibrium_y", | ||
"navier_x", | ||
"navier_y", | ||
"traction_x", | ||
"traction_y", | ||
] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里为什么要给 test_output_names 赋值两次呢
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
眼花了
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
再改一处就OK了
if dim == 3: | ||
input_data = paddle.concat([input_data, z], axis=1) | ||
if time: | ||
input_data = paddle.concat([input_data, t], axis=1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PaddleScience里,时间数据t如果有的话,会排在通道维度的最前面,input_data = paddle.concat([t, input_data], axis=1)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okok
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okok
感觉你应该理解错了,我的意思是 时间t和其他通道的数据堆叠在一起时,应该排在第一个通道上……
也就是第 177行代码应该改成 input_data = paddle.concat([t, input_data], axis=1)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
哦哦好的
if dim == 3: | ||
input_data = paddle.concat([input_data, z], axis=1) | ||
if time: | ||
input_data = paddle.concat([input_data, t], axis=1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okok
感觉你应该理解错了,我的意思是 时间t和其他通道的数据堆叠在一起时,应该排在第一个通道上……
也就是第 177行代码应该改成 input_data = paddle.concat([t, input_data], axis=1)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
hi, @ruoyunbai
|
* Create test_linear_elasticity.py test:equation/pde/linear_elasticity.py * Update test_linear_elasticity.py * fix:修正了review提出的问题 * fix:code style * fix:删除注释 * fix:code style * fix:重复代码 * Update test_linear_elasticity.py * time排在dim之前 * fix:t front * style
PR types
Others
PR changes
Others
Describe
为equatiohn/pde/linear_elasticity.py添加单元测试