Skip to content

Commit

Permalink
four order grad and plat equ equation (PaddlePaddle#167)
Browse files Browse the repository at this point in the history
* update index computation for order 2

* grad for high order der

* four-order eq

* fix definition
  • Loading branch information
xingfeng01 authored Aug 10, 2022
1 parent 12616ca commit c462cbc
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 12 deletions.
22 changes: 10 additions & 12 deletions paddlescience/loss/loss_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def __compute_formula_item(self, item, input, input_attr, labels,
item, input, input_attr, labels, labels_attr)
elif item.is_Derivative:
# print("*** der:", item)
rst = rst * self.__compute_formula_der(item, normal)
rst = rst * self.__compute_formula_der(item, input, normal)
else:
pass

Expand Down Expand Up @@ -139,7 +139,7 @@ def __compute_formula_function(self, item, input, input_attr, labels,
# f_idx = self.parameter_pde.index(item)
# return input[:, f_idx + input_attr.parameter_pde_start] # TODO

def __compute_formula_der(self, item, normal):
def __compute_formula_der(self, item, input, normal):

jacobian = self.jacobian
hessian = self.hessian
Expand All @@ -161,32 +161,30 @@ def __compute_formula_der(self, item, normal):

# parser jacobin for order 1
if order == 1:

v = item.args[1][0]
if v == sympy.Symbol('n'):
rst = normal * jacobian[:, f_idx, :] # TODO
else:
var_idx = self.indvar.index(v)

rst = jacobian[:, f_idx, var_idx]

# parser hessian for order 2
elif order == 2:

var_idx = list()
for it in item.args[1:]:
for i in range(it[1]):
idx = self.indvar.index(it[0])
var_idx.append(idx)
rst = hessian[f_idx][:, var_idx[0], var_idx[1]]

# if (len(item.args[1:]) == 1):
# var_idx = self.indvar.index(item.args[1][0])
# rst = hessian[f_idx][:, var_idx, var_idx]
# else:
# var_idx1 = self.indvar.index(item.args[1][0])
# var_idx2 = self.indvar.index(item.args[2][0])
# rst = hessian[f_idx][:, var_idx1, var_idx2]
# order >= 3
else:
out = self.outs[:, f_idx]
for it in item.args[1:]:
for i in range(it[1]):
idx = self.indvar.index(it[0])
out = paddle.incubate.autograd.grad(out, input)[:, idx]
rst = out

return rst

Expand Down
1 change: 1 addition & 0 deletions paddlescience/pde/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
from .pde_laplace import Laplace
from .pde_poisson import Poisson
from .pde_navier_stokes import NavierStokes
from .pde_plate_equilibrium import PlateEquilibrium
60 changes: 60 additions & 0 deletions paddlescience/pde/pde_plate_equilibrium.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Copyright (c) 2022 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.

from .pde_base import PDE
import sympy
import numpy as np

__all__ = ['PlateEquilibrium']


# PlateEquilibrium equation
class PlateEquilibrium(PDE):
def __init__(self,
stiff,
mass=1.0,
rhs=None,
time_dependent=False,
weight=1.0):

if time_dependent == True:

t = sympy.Symbol('t')
x = sympy.Symbol('x')
y = sympy.Symbol('y')
w = sympy.Function('w')(t, x, y)
super(PlateEquilibrium, self).__init__([t, x, y], [w], weight)

w4x = w.diff(x).diff(x).diff(x).diff(x)
w4y = w.diff(y).diff(y).diff(y).diff(y)
w2x2y = w.diff(y).diff(y).diff(x).diff(x)
w2t = w.diff(t).diff(t)

eq = stiff * w4x + 2.0 * stiff * w2x2y + stiff * w4y + mass * w.diff(
t).diff(t)
self.add_equation(eq, rhs)

else:

x = sympy.Symbol('x')
y = sympy.Symbol('y')
w = sympy.Function('w')(x, y)
super(PlateEquilibrium, self).__init__([x, y], [w], weight)

w4x = w.diff(x).diff(x).diff(x).diff(x)
w4y = w.diff(y).diff(y).diff(y).diff(y)
w2x2y = w.diff(y).diff(y).diff(x).diff(x)

eq = stiff * w4x + 2.0 * stiff * w2x2y + stiff * w4y
self.add_equation(eq, rhs)

0 comments on commit c462cbc

Please sign in to comment.