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 1 commit
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update unitest for N-S equation with sympy-base 'nu'
  • Loading branch information
HydrogenSulfate committed Aug 25, 2023
commit 8783ec9d5e97ae3edc3f500847e474a02f6a2c7a
21 changes: 13 additions & 8 deletions ppsci/utils/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,11 @@ def _cvt_to_key(sympy_node: sp.Basic):
if isinstance(
sympy_node, (sp.Symbol, sp.core.function.UndefinedFunction, sp.Function)
):
return sympy_node.name
if hasattr(sympy_node, "name"):
# custom function
return sympy_node.name
else:
str(sympy_node)
elif isinstance(sympy_node, sp.Derivative):
# convert Derivative(u(x,y),(x,2),(y,2)) to "u__x__x__y__y"
expr_str = sympy_node.args[0].name
Expand All @@ -256,7 +260,7 @@ def _cvt_to_key(sympy_node: sp.Basic):
return str(sympy_node)


class NodeBase(nn.Layer):
class Node(nn.Layer):
"""The base class of the node in expression tree."""

def __init__(self, expr: sp.Basic):
Expand All @@ -265,15 +269,15 @@ def __init__(self, expr: sp.Basic):
self.key = _cvt_to_key(self.expr)

def forward(self, **kwargs):
raise NotImplementedError("NodeBase.forward is not implemented")
raise NotImplementedError("Node.forward is not implemented")

def __str__(self):
return (
self.__class__.__name__ + f"(expr: {self.expr}), type: {type(self.expr)})"
)


class OperatorNode(NodeBase):
class OperatorNode(Node):
"""
A node representing a sp operator in the computational graph.
"""
Expand All @@ -283,7 +287,7 @@ def __init__(self, expr: SYMPY_BUILTIN_FUNC):

def forward(self, data_dict: Dict):
if self.expr.func == sp.Add:
data_dict[self.key] = paddle.add_n(
data_dict[self.key] = sum(
[data_dict[_cvt_to_key(arg)] for arg in self.expr.args]
)
elif self.expr.func == sp.Mul:
Expand Down Expand Up @@ -316,7 +320,7 @@ def forward(self, data_dict: Dict):
return data_dict


class LayerNode(NodeBase):
class LayerNode(Node):
"""
A node representing a neural network in the computational graph
"""
Expand All @@ -330,13 +334,14 @@ def forward(self, data_dict: Dict):
return data_dict

output_dict = self.model(data_dict)
print("call model forward")
for key, value in output_dict.items():
data_dict[key] = value

return data_dict


class ConstantNode(NodeBase):
class ConstantNode(Node):
"""
A node representing a constant in the computational graph.
"""
Expand Down Expand Up @@ -366,7 +371,7 @@ class ComposedFunc(nn.Layer):
Compose multiple functions into one function.
"""

def __init__(self, target: str, funcs: List[NodeBase]):
def __init__(self, target: str, funcs: List[Node]):
super().__init__()
self.funcs = funcs
self.target = target
Expand Down
2 changes: 1 addition & 1 deletion ppsci/utils/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def ensure_logger(log_func):
def wrapped_log_func(fmt, *args):
if _logger is None:
init_logger()
_logger.info(
_logger.warning(
"Before you call functions within the logger, the logger has already "
"been automatically initialized. Since `log_file` is not specified by "
"default, information will not be written to any file except being "
Expand Down
2 changes: 1 addition & 1 deletion test/utils/test_linear_elasticity_sympy.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ def test_linearelasticity(E, nu, lambda_, mu, rho, dim, time):
# test for result
for key in functional_expr_dict:
assert paddle.allclose(
output_dict_functional[key], output_dict_sympy[key], atol=1e-7
output_dict_functional[key], output_dict_sympy[key], atol=2e-7
)


Expand Down
Loading