forked from PaddlePaddle/PaddleScience
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request PaddlePaddle#10 from shjNT/shj0
test_examples
- Loading branch information
Showing
8 changed files
with
397 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[pytest] | ||
addopts = -p no:warnings | ||
log_cli = true | ||
#log_cli_level = DEBUG |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#!/bin/bash | ||
|
||
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
cases=`find . -maxdepth 1 -name "test_*.py" | sort ` | ||
ignore="" | ||
bug=0 | ||
|
||
for file in ${cases} | ||
do | ||
echo ${file} | ||
if [[ ${ignore} =~ ${file##*/} ]]; then | ||
echo "skip" | ||
else | ||
python3.7 -m pytest ${file} | ||
if [ $? -ne 0 ]; then | ||
echo ${file} >> result.txt | ||
bug=`expr ${bug} + 1` | ||
fi | ||
fi | ||
done | ||
|
||
echo "total bugs: "${bug} > result.txt | ||
cat result.txt | ||
exit ${bug} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
""" | ||
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
""" | ||
|
||
import pytest | ||
import paddlescience as psci | ||
import numpy as np | ||
import paddle | ||
|
||
|
||
# Analytical solution | ||
def DarcyRecSolution(x, y): | ||
""" | ||
Dirichlet boundary condition | ||
""" | ||
return np.sin(2.0 * np.pi * x) * np.cos(2.0 * np.pi * y) | ||
|
||
|
||
# Generate analytical Solution using Geometry points | ||
def GenSolution(xy, bc_index): | ||
""" | ||
GenSolution | ||
""" | ||
sol = np.zeros((len(xy), 1)).astype(np.float32) | ||
bc_value = np.zeros((len(bc_index), 1)).astype(np.float32) | ||
length1 = len(xy) | ||
for i in range(length1): | ||
sol[i][0] = DarcyRecSolution(xy[i][0], xy[i][1]) | ||
|
||
length2 = len(bc_index) | ||
for i in range(length2): | ||
bc_value[i][0] = sol[bc_index[i]] | ||
|
||
return [sol, bc_value] | ||
|
||
|
||
# right-hand side | ||
def RighthandBatch(xy): | ||
""" | ||
RighthandBatch | ||
""" | ||
return [ | ||
8.0 * 3.1415926 * 3.1415926 * paddle.sin(2.0 * np.pi * xy[:, 0]) * | ||
paddle.cos(2.0 * np.pi * xy[:, 1]) | ||
] | ||
|
||
|
||
# Geometry | ||
geo = psci.geometry.Rectangular( | ||
space_origin=(0.0, 0.0), space_extent=(1.0, 1.0)) | ||
|
||
# PDE Laplace | ||
pdes = psci.pde.Laplace2D() | ||
|
||
# Discretization | ||
pdes, geo = psci.discretize(pdes, geo, space_nsteps=(4, 4)) | ||
|
||
# bc value | ||
golden, bc_value = GenSolution(geo.space_domain, geo.bc_index) | ||
pdes.set_bc_value(bc_value=bc_value) | ||
|
||
# Network | ||
net = psci.network.FCNet( | ||
num_ins=2, | ||
num_outs=1, | ||
num_layers=2, | ||
hidden_size=1, | ||
dtype="float32", | ||
activation="tanh") | ||
|
||
net._parameters["w_0"].set_value( | ||
paddle.to_tensor([[1], [1]]).astype("float32")) | ||
net._parameters["w_1"].set_value(paddle.to_tensor([[1]]).astype("float32")) | ||
|
||
# Loss, TO rename | ||
loss = psci.loss.L2(pdes=pdes, geo=geo, aux_func=RighthandBatch) | ||
|
||
# Algorithm | ||
algo = psci.algorithm.PINNs(net=net, loss=loss) | ||
|
||
# Optimizer | ||
opt = psci.optimizer.Adam(learning_rate=0.001, parameters=net.parameters()) | ||
|
||
# Solver | ||
solver = psci.solver.Solver(algo=algo, opt=opt) | ||
solution = solver.solve(num_epoch=10) | ||
|
||
# Use solution | ||
rslt = solution(geo) | ||
|
||
# Calculate diff and l2 relative error | ||
diff = rslt - golden | ||
root_square_error = np.linalg.norm(diff, ord=2) | ||
mean_square_error = root_square_error * root_square_error / geo.get_domain_size( | ||
) | ||
|
||
|
||
@pytest.mark.ldc2d | ||
def test_darcy2D(): | ||
""" | ||
test darcy2d | ||
""" | ||
golden = np.load("./golden/darcy2D.npz") | ||
expect = golden['expect'] | ||
diff_expect = golden['diff_expect'] | ||
assert np.allclose(rslt, expect), "the rslt was changed" | ||
assert np.isclose(diff_expect, | ||
mean_square_error), "the mean_square_error was changed" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
""" | ||
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
""" | ||
|
||
import pytest | ||
import paddlescience as psci | ||
import numpy as np | ||
import paddle | ||
|
||
|
||
# Analytical solution | ||
def LaplaceRecSolution(x, y, k=1.0): | ||
""" | ||
LaplaceRecSolution | ||
""" | ||
if k == 0.0: | ||
return x * y | ||
else: | ||
return np.cos(k * x) * np.cosh(k * y) | ||
|
||
|
||
# Generate analytical Solution using Geometry points | ||
def GenSolution(xy, bc_index): | ||
""" | ||
GenSolution | ||
""" | ||
sol = np.zeros((len(xy), 1)).astype(np.float32) | ||
bc_value = np.zeros((len(bc_index), 1)).astype(np.float32) | ||
length1 = len(xy) | ||
for i in range(length1): | ||
sol[i] = LaplaceRecSolution(xy[i][0], xy[i][1]) | ||
|
||
length2 = len(bc_index) | ||
for i in range(length2): | ||
bc_value[i][0] = sol[bc_index[i]] | ||
return [sol, bc_value] | ||
|
||
|
||
# Geometry | ||
geo = psci.geometry.Rectangular( | ||
space_origin=(0.0, 0.0), space_extent=(1.0, 1.0)) | ||
|
||
# PDE Laplace | ||
pdes = psci.pde.Laplace2D() | ||
|
||
# Discretization | ||
pdes, geo = psci.discretize(pdes, geo, space_nsteps=(4, 4)) | ||
|
||
# bc value | ||
golden, bc_value = GenSolution(geo.space_domain, geo.bc_index) | ||
pdes.set_bc_value(bc_value=bc_value) | ||
|
||
# Network | ||
net = psci.network.FCNet( | ||
num_ins=2, | ||
num_outs=1, | ||
num_layers=2, | ||
hidden_size=1, | ||
dtype="float32", | ||
activation="tanh") | ||
net._parameters["w_0"].set_value( | ||
paddle.to_tensor([[1], [1]]).astype("float32")) | ||
net._parameters["w_1"].set_value(paddle.to_tensor([[1]]).astype("float32")) | ||
|
||
# Loss, TO rename | ||
loss = psci.loss.L2(pdes=pdes, geo=geo) | ||
|
||
# Algorithm | ||
algo = psci.algorithm.PINNs(net=net, loss=loss) | ||
|
||
# Optimizer | ||
opt = psci.optimizer.Adam(learning_rate=0.001, parameters=net.parameters()) | ||
|
||
# Solver | ||
solver = psci.solver.Solver(algo=algo, opt=opt) | ||
solution = solver.solve(num_epoch=30) | ||
|
||
# Use solution | ||
rslt = solution(geo) | ||
|
||
# Calculate diff and l2 relative error | ||
diff = rslt - golden | ||
root_square_error = np.linalg.norm(diff, ord=2) | ||
mean_square_error = root_square_error * root_square_error / geo.get_domain_size( | ||
) | ||
|
||
|
||
@pytest.mark.laplace2d | ||
def test_Laplace2D(): | ||
""" | ||
test Laplace2D | ||
""" | ||
golden = np.load("./golden/laplace2d.npz") | ||
expect = golden['expect'] | ||
diff_expect = golden['diff_expect'] | ||
assert np.allclose(rslt, expect), "the rslt was changed" | ||
assert np.isclose(diff_expect, | ||
mean_square_error), "the mean_square_error was changed" |
Oops, something went wrong.