Skip to content
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

Merged
merged 12 commits into from
Jul 10, 2023

Conversation

ruoyunbai
Copy link
Contributor

PR types

Others

PR changes

Others

Describe

为equatiohn/pde/linear_elasticity.py添加单元测试

@paddle-bot
Copy link

paddle-bot bot commented Jul 2, 2023

Thanks for your contribution!

@ruoyunbai ruoyunbai changed the title Develop 为equatiohn/pde/linear_elasticity.py添加单元测试 Jul 2, 2023
@HydrogenSulfate
Copy link
Collaborator

image
这个单测的认领人好像不是你这个账号

import pytest
from paddle import nn

from ppsci.equation import LinearElasticity
Copy link
Collaborator

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]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

去掉 allow_unused=True

Comment on lines 108 to 113
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jacobian 和 hessian 的函数移动到所有函数的上方

Comment on lines 196 to 203
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],
)
Copy link
Collaborator

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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

删除注释

Comment on lines 150 to 156
"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),
],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

目前这个类还未添加对 Enu 的处理逻辑,应该为 Enu 设置为None,而 lambda_mu 设置为实数值

@ruoyunbai
Copy link
Contributor Author

image 这个单测的认领人好像不是你这个账号

老师让我看看这个,我没认领/(ㄒoㄒ)/~~

@HydrogenSulfate
Copy link
Collaborator

image 这个单测的认领人好像不是你这个账号

老师让我看看这个,我没认领/(ㄒoㄒ)/~~

那没事儿,可以按照review改一下,到时候合你的PR

@ruoyunbai
Copy link
Contributor Author

好滴,正在改

@ruoyunbai
Copy link
Contributor Author

已经按照review修改,请过目

Comment on lines 179 to 181
if lambda_ is None or mu is None:
lambda_ = (E * nu) / ((1 + nu) * (1 - 2 * nu))
mu = E / (2 * (1 + nu))
Copy link
Collaborator

@HydrogenSulfate HydrogenSulfate Jul 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

目前 LinearElasticity 对给定 E和nu的情况没有进行处理,可以先把这部分的单测case和相关代码删掉

Comment on lines 152 to 155
(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),
Copy link
Collaborator

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

删除这行注释

Comment on lines 278 to 310
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)
Copy link
Collaborator

@HydrogenSulfate HydrogenSulfate Jul 5, 2023

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 同理

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好滴,马上改

Comment on lines 243 to 245
linear_elasticity = equation.LinearElasticity(
E=None, nu=None, lambda_=lambda_, mu=mu, rho=rho, dim=dim, time=time
)
Copy link
Collaborator

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应该用传进来的参数填写吧

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的

Comment on lines 266 to 290
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",
]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里为什么要给 test_output_names 赋值两次呢

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

眼花了

test/equation/test_linear_elasticity.py Show resolved Hide resolved
Copy link
Collaborator

@HydrogenSulfate HydrogenSulfate left a 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)
Copy link
Collaborator

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)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okok

Copy link
Collaborator

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)

Copy link
Contributor Author

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)
Copy link
Collaborator

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)

Copy link
Collaborator

@HydrogenSulfate HydrogenSulfate left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@HydrogenSulfate HydrogenSulfate merged commit 83be9d9 into PaddlePaddle:develop Jul 10, 2023
@luotao1
Copy link
Collaborator

luotao1 commented Jul 10, 2023

hi, @ruoyunbai

  • 非常感谢你对飞桨框架的贡献,我们正在运营一个PFCC组织,会通过定期分享技术知识与发布开发者主导任务的形式持续为飞桨框架做贡献,详情可见 https://github.com/luotao1 主页说明。
  • 如果你对PFCC有兴趣,请发送邮件至 ext_paddle_oss@baidu.com,我们会邀请你加入~

huohuohuohuohuo123 pushed a commit to huohuohuohuohuo123/PaddleScience that referenced this pull request Aug 12, 2024
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants