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

[Feature] Replace python funcion with sympy expression #507

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
2d95737
add sympy2func
AndPuQing Aug 9, 2023
f8bb795
fix node
AndPuQing Aug 10, 2023
b31a785
fix var name
AndPuQing Aug 10, 2023
d9d30d1
fix implementation
AndPuQing Aug 13, 2023
6877901
Merge branch 'develop' into feature-sympy
HydrogenSulfate Aug 23, 2023
2ccc9f5
add sympy to func module
HydrogenSulfate Aug 24, 2023
bb9d71c
add type hint and clean code
HydrogenSulfate Aug 24, 2023
8783ec9
update unitest for N-S equation with sympy-base 'nu'
HydrogenSulfate Aug 25, 2023
6f376b6
delete redundant sympy2func.py
HydrogenSulfate Aug 25, 2023
cfa853f
remove sympy to function code from expression.py to sym_to_func.py
HydrogenSulfate Aug 25, 2023
62e9855
update type hint in expression
HydrogenSulfate Aug 26, 2023
fee4553
update code
HydrogenSulfate Aug 26, 2023
1b7642e
update code
HydrogenSulfate Aug 26, 2023
1538f0d
update code
HydrogenSulfate Aug 26, 2023
36e48e5
refine sym_to_func.py
HydrogenSulfate Aug 26, 2023
d32fd84
replace sympy PDE for biharmonic and laplace
HydrogenSulfate Aug 26, 2023
4e61fa5
refine sym_to_func.py
HydrogenSulfate Aug 27, 2023
4baf466
fix bug in _cvt_to_key
HydrogenSulfate Aug 27, 2023
ba2a5c2
refine sym_to_func and expression code
HydrogenSulfate Aug 27, 2023
f057eec
add euler_beam static code(WIP, can not running, to be debug)
HydrogenSulfate Aug 27, 2023
ffd6b27
wip code
HydrogenSulfate Aug 28, 2023
0809194
Merge branch 'develop' into cvt_pyfunc_to_sym
HydrogenSulfate Aug 28, 2023
b0d1df1
temporary code(need to be refined)
HydrogenSulfate Aug 28, 2023
2fa8a8b
Merge branch 'develop' into cvt_pyfunc_to_sym
HydrogenSulfate Sep 6, 2023
b2a4509
update solver code
HydrogenSulfate Sep 6, 2023
13d6ff6
replace more pdes with sympy
HydrogenSulfate Sep 6, 2023
ffd3a93
simplify code in solver
HydrogenSulfate Sep 6, 2023
256f31a
update code
HydrogenSulfate Sep 7, 2023
3a870d9
rename 'normal_dot_vel' to 'normal_dot_vec'
HydrogenSulfate Sep 7, 2023
08e92d3
fix bug
HydrogenSulfate Sep 7, 2023
c2373ca
update unitest
HydrogenSulfate Sep 7, 2023
051dcd6
remove redundant unitest
HydrogenSulfate Sep 7, 2023
2eacfea
Merge branch 'develop' into cvt_pyfunc_to_sym
HydrogenSulfate Sep 7, 2023
c7eea1b
remove unnecessary code
HydrogenSulfate Sep 7, 2023
615fefb
remove unnecessary more code
HydrogenSulfate Sep 7, 2023
e096ea2
use DETACH_FUNC_NAME instead of 'detach'
HydrogenSulfate Sep 7, 2023
cb16107
add derivatives for sdf function
HydrogenSulfate Sep 7, 2023
899a2d2
replace .diff.diff with .diff(, 2)
HydrogenSulfate Sep 10, 2023
640c080
support exporting expression to .dot and .png file for visualizing an…
HydrogenSulfate Sep 10, 2023
16aae52
remove compute_sdf_derivatives for next PR
HydrogenSulfate Sep 10, 2023
9b46410
refine docstring of ppsci/data/dataset/array_dataset.py
HydrogenSulfate Sep 10, 2023
f179ec1
remove sdf_derivatives code in geometry for next PR
HydrogenSulfate Sep 10, 2023
2e37bca
remove print code in solver
HydrogenSulfate Sep 10, 2023
6ecb31b
rename sympy_to_function to lambdify and add it in ppsci.*
HydrogenSulfate Sep 10, 2023
0a88a08
rename for test files
HydrogenSulfate Sep 11, 2023
18a1638
rename sym_to_func.py to symbolic.py
HydrogenSulfate Sep 12, 2023
8ccf858
update linear_init_ and conv_init_ to kaiming style
HydrogenSulfate Sep 12, 2023
978cdfc
Merge branch 'develop' into cvt_pyfunc_to_sym
HydrogenSulfate Sep 12, 2023
a319e99
refine probability document
HydrogenSulfate Sep 12, 2023
be67fb4
change list to tuple
HydrogenSulfate Sep 12, 2023
eecc5e9
update docstrings of equations
HydrogenSulfate Sep 13, 2023
cb7f777
Merge branch 'develop' into cvt_pyfunc_to_sym
HydrogenSulfate Sep 13, 2023
59bc990
Merge branch 'develop' into cvt_pyfunc_to_sym
HydrogenSulfate Sep 13, 2023
c0228f0
larger atol to 1e-7 for test_linear_elasticity
HydrogenSulfate Sep 13, 2023
408e378
fix seed to 42 for test_linear_elasticity
HydrogenSulfate Sep 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/zh/api/probability.md
@@ -1,3 +1,5 @@
# Probability(概率编程) 模块

::: ppsci.probability
handler: python
options:
Expand Down
1 change: 1 addition & 0 deletions docs/zh/api/utils.md
Expand Up @@ -19,5 +19,6 @@
- load_checkpoint
- load_pretrain
- save_checkpoint
- lambdify
show_root_heading: false
heading_level: 3
8 changes: 4 additions & 4 deletions examples/aneurysm/aneurysm.py
Expand Up @@ -132,7 +132,7 @@ def inlet_w_ref_func(_in):
)
igc_outlet = ppsci.constraint.IntegralConstraint(
equation["NormalDotVec"].equations,
{"normal_dot_vel": 2.54},
{"normal_dot_vec": 2.54},
geom["outlet_geo"],
{
**train_dataloader_cfg,
Expand All @@ -141,12 +141,12 @@ def inlet_w_ref_func(_in):
"integral_batch_size": 310,
},
ppsci.loss.IntegralLoss("sum"),
weight_dict={"normal_dot_vel": 0.1},
weight_dict={"normal_dot_vec": 0.1},
name="igc_outlet",
)
igc_integral = ppsci.constraint.IntegralConstraint(
equation["NormalDotVec"].equations,
{"normal_dot_vel": -2.54},
{"normal_dot_vec": -2.54},
geom["integral_geo"],
{
**train_dataloader_cfg,
Expand All @@ -155,7 +155,7 @@ def inlet_w_ref_func(_in):
"integral_batch_size": 310,
},
ppsci.loss.IntegralLoss("sum"),
weight_dict={"normal_dot_vel": 0.1},
weight_dict={"normal_dot_vec": 0.1},
name="igc_integral",
)
# wrap constraints together
Expand Down
24 changes: 12 additions & 12 deletions examples/bracket/bracket.py
Expand Up @@ -127,15 +127,15 @@
support_interior_constraint = ppsci.constraint.InteriorConstraint(
equation["LinearElasticity"].equations,
{
"equilibrium_x": 0,
"equilibrium_y": 0,
"equilibrium_z": 0,
"stress_disp_xx": 0,
"stress_disp_yy": 0,
"stress_disp_zz": 0,
"stress_disp_xy": 0,
"stress_disp_xz": 0,
"stress_disp_yz": 0,
"equilibrium_x": 0,
"equilibrium_y": 0,
"equilibrium_z": 0,
},
geom["geo"],
{**train_dataloader_cfg, "batch_size": 2048},
Expand All @@ -149,30 +149,30 @@
& (z < BOUNDS_SUPPORT_Z[1])
),
weight_dict={
"equilibrium_x": "sdf",
"equilibrium_y": "sdf",
"equilibrium_z": "sdf",
"stress_disp_xx": "sdf",
"stress_disp_yy": "sdf",
"stress_disp_zz": "sdf",
"stress_disp_xy": "sdf",
"stress_disp_xz": "sdf",
"stress_disp_yz": "sdf",
"equilibrium_x": "sdf",
"equilibrium_y": "sdf",
"equilibrium_z": "sdf",
},
name="support_interior",
)
bracket_interior_constraint = ppsci.constraint.InteriorConstraint(
equation["LinearElasticity"].equations,
{
"equilibrium_x": 0,
"equilibrium_y": 0,
"equilibrium_z": 0,
"stress_disp_xx": 0,
"stress_disp_yy": 0,
"stress_disp_zz": 0,
"stress_disp_xy": 0,
"stress_disp_xz": 0,
"stress_disp_yz": 0,
"equilibrium_x": 0,
"equilibrium_y": 0,
"equilibrium_z": 0,
},
geom["geo"],
{**train_dataloader_cfg, "batch_size": 1024},
Expand All @@ -186,15 +186,15 @@
& (z < BOUNDS_BRACKET_Z[1])
),
weight_dict={
"equilibrium_x": "sdf",
"equilibrium_y": "sdf",
"equilibrium_z": "sdf",
"stress_disp_xx": "sdf",
"stress_disp_yy": "sdf",
"stress_disp_zz": "sdf",
"stress_disp_xy": "sdf",
"stress_disp_xz": "sdf",
"stress_disp_yz": "sdf",
"equilibrium_x": "sdf",
"equilibrium_y": "sdf",
"equilibrium_z": "sdf",
},
name="bracket_interior",
)
Expand Down
2 changes: 1 addition & 1 deletion examples/laplace/laplace2d.py
Expand Up @@ -29,7 +29,7 @@
EVAL_FREQ = 200

# set output directory
OUTPUT_DIR = "./output/laplace2d" if not args.output_dir else args.output_dir
OUTPUT_DIR = "./output_laplace2d" if not args.output_dir else args.output_dir
logger.init_logger("ppsci", f"{OUTPUT_DIR}/train.log", "info")

# set model
Expand Down
4 changes: 1 addition & 3 deletions examples/pipe/poiseuille_flow.py
Expand Up @@ -133,9 +133,7 @@ def output_trans_p(input, out):

# set euqation
equation = {
"NavierStokes": ppsci.equation.NavierStokes(
nu=lambda out: out["nu"], rho=RHO, dim=2, time=False
)
"NavierStokes": ppsci.equation.NavierStokes(nu="nu", rho=RHO, dim=2, time=False)
}

# set constraint
Expand Down
1 change: 1 addition & 0 deletions mkdocs.yml
Expand Up @@ -87,6 +87,7 @@ nav:
- ppsci.validate: zh/api/validate.md
- ppsci.visualize: zh/api/visualize.md
- ppsci.experimental: zh/api/experimental.md
- ppsci.probability: zh/api/probability.md
- 使用指南: zh/user_guide.md
- 开发与复现指南:
- 开发指南: zh/development.md
Expand Down
2 changes: 2 additions & 0 deletions ppsci/__init__.py
Expand Up @@ -29,6 +29,7 @@

from ppsci.utils.checker import run_check # isort:skip
from ppsci.utils.checker import run_check_mesh # isort:skip
from ppsci.utils import lambdify # isort:skip

__all__ = [
"arch",
Expand All @@ -47,4 +48,5 @@
"experimental",
"run_check",
"run_check_mesh",
"lambdify",
]
14 changes: 4 additions & 10 deletions ppsci/constraint/boundary_constraint.py
Expand Up @@ -23,7 +23,6 @@

import numpy as np
import sympy
from sympy.parsing import sympy_parser as sp_parser
from typing_extensions import Literal

from ppsci import geometry
Expand Down Expand Up @@ -86,14 +85,12 @@ def __init__(
weight_dict: Optional[Dict[str, Union[float, Callable]]] = None,
name: str = "BC",
):
self.output_expr = output_expr
for label_name, expr in self.output_expr.items():
if isinstance(expr, str):
self.output_expr[label_name] = sp_parser.parse_expr(expr)

self.label_dict = label_dict
self.input_keys = geom.dim_keys
self.output_keys = list(label_dict.keys())
self.output_keys = tuple(label_dict.keys())
self.output_expr = {
k: v for k, v in output_expr.items() if k in self.output_keys
}
# "area" will be kept in "output_dict" for computation.
if isinstance(geom, geometry.Mesh):
self.output_keys += ["area"]
Expand Down Expand Up @@ -137,9 +134,6 @@ def __init__(
weight = {key: np.ones_like(next(iter(label.values()))) for key in label}
if weight_dict is not None:
for key, value in weight_dict.items():
if isinstance(value, str):
value = sp_parser.parse_expr(value)

if isinstance(value, (int, float)):
weight[key] = np.full_like(next(iter(label.values())), value)
elif isinstance(value, sympy.Basic):
Expand Down
15 changes: 4 additions & 11 deletions ppsci/constraint/initial_constraint.py
Expand Up @@ -23,7 +23,6 @@

import numpy as np
import sympy
from sympy.parsing import sympy_parser as sp_parser
from typing_extensions import Literal

from ppsci import geometry
Expand Down Expand Up @@ -89,14 +88,12 @@ def __init__(
weight_dict: Optional[Dict[str, Callable]] = None,
name: str = "IC",
):
self.output_expr = output_expr
for label_name, expr in self.output_expr.items():
if isinstance(expr, str):
self.output_expr[label_name] = sp_parser.parse_expr(expr)

self.label_dict = label_dict
self.input_keys = geom.dim_keys
self.output_keys = list(label_dict.keys())
self.output_keys = tuple(label_dict.keys())
self.output_expr = {
k: v for k, v in output_expr.items() if k in self.output_keys
}
# "area" will be kept in "output_dict" for computation.
if isinstance(geom.geometry, geometry.Mesh):
self.output_keys += ["area"]
Expand All @@ -117,8 +114,6 @@ def __init__(
# prepare label
label = {}
for key, value in label_dict.items():
if isinstance(value, str):
value = sp_parser.parse_expr(value)
if isinstance(value, (int, float)):
label[key] = np.full_like(next(iter(input.values())), value)
elif isinstance(value, sympy.Basic):
Expand All @@ -142,8 +137,6 @@ def __init__(
weight = {key: np.ones_like(next(iter(label.values()))) for key in label}
if weight_dict is not None:
for key, value in weight_dict.items():
if isinstance(value, str):
value = sp_parser.parse_expr(value)
if isinstance(value, (int, float)):
weight[key] = np.full_like(next(iter(label.values())), value)
elif isinstance(value, sympy.Basic):
Expand Down
14 changes: 4 additions & 10 deletions ppsci/constraint/integral_constraint.py
Expand Up @@ -24,7 +24,6 @@
import numpy as np
import paddle
import sympy
from sympy.parsing import sympy_parser as sp_parser
from typing_extensions import Literal

from ppsci import geometry
Expand Down Expand Up @@ -86,14 +85,12 @@ def __init__(
weight_dict: Optional[Dict[str, Callable]] = None,
name: str = "IgC",
):
self.output_expr = output_expr
for label_name, expr in self.output_expr.items():
if isinstance(expr, str):
self.output_expr[label_name] = sp_parser.parse_expr(expr)

self.label_dict = label_dict
self.input_keys = geom.dim_keys
self.output_keys = list(label_dict.keys())
self.output_keys = tuple(label_dict.keys())
self.output_expr = {
k: v for k, v in output_expr.items() if k in self.output_keys
}
# "area" will be kept in "output_dict" for computation.
if isinstance(geom, geometry.Mesh):
self.output_keys += ["area"]
Expand Down Expand Up @@ -149,9 +146,6 @@ def __init__(
weight = {key: np.ones_like(next(iter(label.values()))) for key in label}
if weight_dict is not None:
for key, value in weight_dict.items():
if isinstance(value, str):
value = sp_parser.parse_expr(value)

if isinstance(value, (int, float)):
weight[key] = np.full_like(next(iter(label.values())), value)
elif isinstance(value, sympy.Basic):
Expand Down
13 changes: 4 additions & 9 deletions ppsci/constraint/interior_constraint.py
Expand Up @@ -23,7 +23,6 @@

import numpy as np
import sympy
from sympy.parsing import sympy_parser as sp_parser
from typing_extensions import Literal

from ppsci import geometry
Expand Down Expand Up @@ -86,14 +85,12 @@ def __init__(
weight_dict: Optional[Dict[str, Union[Callable, float]]] = None,
name: str = "EQ",
):
self.output_expr = output_expr
for label_name, expr in self.output_expr.items():
if isinstance(expr, str):
self.output_expr[label_name] = sp_parser.parse_expr(expr)

self.label_dict = label_dict
self.input_keys = geom.dim_keys
self.output_keys = list(label_dict.keys())
self.output_keys = tuple(label_dict.keys())
self.output_expr = {
k: v for k, v in output_expr.items() if k in self.output_keys
}
# "area" will be kept in "output_dict" for computation.
if isinstance(geom, geometry.Mesh):
self.output_keys += ["area"]
Expand All @@ -114,8 +111,6 @@ def __init__(
# prepare label
label = {}
for key, value in label_dict.items():
if isinstance(value, str):
value = sp_parser.parse_expr(value)
if isinstance(value, (int, float)):
label[key] = np.full_like(next(iter(input.values())), value)
elif isinstance(value, sympy.Basic):
Expand Down
14 changes: 4 additions & 10 deletions ppsci/constraint/periodic_constraint.py
Expand Up @@ -24,7 +24,6 @@
import numpy as np
import paddle
import sympy
from sympy.parsing import sympy_parser as sp_parser
from typing_extensions import Literal

from ppsci import geometry
Expand Down Expand Up @@ -73,13 +72,11 @@ def __init__(
weight_dict: Optional[Dict[str, Callable]] = None,
name: str = "PeriodicBC",
):
self.output_expr = output_expr
for label_name, expr in self.output_expr.items():
if isinstance(expr, str):
self.output_expr[label_name] = sp_parser.parse_expr(expr)

self.input_keys = geom.dim_keys
self.output_keys = list(output_expr.keys())
self.output_keys = tuple(output_expr.keys())
self.output_expr = {
k: v for k, v in output_expr.items() if k in self.output_keys
}
# "area" will be kept in "output_dict" for computation.
if isinstance(geom, geometry.Mesh):
self.output_keys += ["area"]
Expand Down Expand Up @@ -143,9 +140,6 @@ def __init__(
weight = {key: np.ones_like(next(iter(label.values()))) for key in label}
if weight_dict is not None:
for key, value in weight_dict.items():
if isinstance(value, str):
value = sp_parser.parse_expr(value)

if isinstance(value, (int, float)):
weight[key] = np.full_like(next(iter(label.values())), value)
elif isinstance(value, sympy.Basic):
Expand Down
9 changes: 5 additions & 4 deletions ppsci/constraint/supervised_constraint.py
Expand Up @@ -60,19 +60,20 @@ def __init__(
output_expr: Optional[Dict[str, Callable]] = None,
name: str = "Sup",
):
self.output_expr = output_expr

# build dataset
_dataset = dataset.build_dataset(dataloader_cfg["dataset"])

self.input_keys = _dataset.input_keys
self.output_keys = (
list(output_expr.keys()) if output_expr is not None else _dataset.label_keys
tuple(output_expr.keys())
if output_expr is not None
else _dataset.label_keys
)

self.output_expr = output_expr
if self.output_expr is None:
self.output_expr = {
key: lambda out, k=key: out[k] for key in self.output_keys
key: (lambda out, k=key: out[k]) for key in self.output_keys
}

# construct dataloader with dataset and dataloader_cfg
Expand Down
6 changes: 3 additions & 3 deletions ppsci/data/dataset/array_dataset.py
Expand Up @@ -29,9 +29,9 @@ class NamedArrayDataset(io.Dataset):
Args:
input (Dict[str, np.ndarray]): Input dict.
label (Dict[str, np.ndarray]): Label dict.
weight (Optional[Dict[str, np.ndarray]], optional): Weight dict.
transforms (Optional[vision.Compose], optional): Compose object contains sample wise
transform(s).
weight (Optional[Dict[str, np.ndarray]]): Weight dict. Defaults to None.
transforms (Optional[vision.Compose]): Compose object contains sample wise
transform(s). Defaults to None.

Examples:
>>> import ppsci
Expand Down