mindflow.pde

class mindflow.pde.Burgers(model, loss_fn='mse')[源代码]

基于PDEWithLoss定义的一维Burgers方程求解问题。

参数:
  • model (mindspore.nn.Cell) - 用于训练的网络模型。

  • loss_fn (str) - 损失函数。默认:”mse”。

支持平台:

Ascend GPU

样例:

>>> from mindflow.pde import Burgers
>>> from mindspore import nn, ops
>>> class Net(nn.Cell):
...     def __init__(self, cin=2, cout=1, hidden=10):
...         super().__init__()
...         self.fc1 = nn.Dense(cin, hidden)
...         self.fc2 = nn.Dense(hidden, hidden)
...         self.fcout = nn.Dense(hidden, cout)
...         self.act = ops.Tanh()
...
...     def construct(self, x):
...         x = self.act(self.fc1(x))
...         x = self.act(self.fc2(x))
...         x = self.fcout(x)
...         return x
>>> model = Net()
>>> problem = Burgers(model)
>>> print(problem.pde())
burgers: u(x, t)Derivative(u(x, t), x) + Derivative(u(x, t), t) - 0.00318309897556901Derivative(u(x, t), (x, 2))
Item numbers of current derivative formula nodes: 3
{'burgers': u(x, t)Derivative(u(x, t), x) + Derivative(u(x, t), t) - 0.00318309897556901Derivative(u(x, t),
(x, 2))}
pde()[源代码]

抽象方法,基于sympy定义的一维Burgers控制方程。

返回:

dict,自定义sympy符号方程。

class mindflow.pde.NavierStokes(model, re=100.0, loss_fn='mse')[源代码]

基于PDEWithLoss定义的二维NavierStokes方程求解问题。

参数:
  • model (mindspore.nn.Cell) - 用于训练的网络模型。

  • re (float) - 无量纲参数,雷诺数是流体惯性力与粘滞力的比值。默认值:100.0。

  • loss_fn (str) - 损失函数。默认值:”mse”。

支持平台:

Ascend GPU

样例:

>>> from mindflow.pde import NavierStokes
>>> from mindspore import nn, ops
>>> class Net(nn.Cell):
...     def __init__(self, cin=3, cout=3, hidden=10):
...         super().__init__()
...         self.fc1 = nn.Dense(cin, hidden)
...         self.fc2 = nn.Dense(hidden, hidden)
...         self.fcout = nn.Dense(hidden, cout)
...         self.act = ops.Tanh()
...
...     def construct(self, x):
...         x = self.act(self.fc1(x))
...         x = self.act(self.fc2(x))
...         x = self.fcout(x)
...         return x
>>> model = Net()
>>> problem = NavierStokes(model)
>>> print(problem.pde())
momentum_x: u(x, y, t)Derivative(u(x, y, t), x) + v(x, y, t)Derivative(u(x, y, t), y) +
Derivative(p(x, y, t), x) + Derivative(u(x, y, t), t) - 0.00999999977648258Derivative(u(x, y, t), (x, 2)) -
0.00999999977648258Derivative(u(x, y, t), (y, 2))
Item numbers of current derivative formula nodes: 6
momentum_y: u(x, y, t)Derivative(v(x, y, t), x) + v(x, y, t)Derivative(v(x, y, t), y) +
Derivative(p(x, y, t), y) + Derivative(v(x, y, t), t) - 0.00999999977648258Derivative(v(x, y, t), (x, 2)) -
0.00999999977648258Derivative(v(x, y, t), (y, 2))
Item numbers of current derivative formula nodes: 6
continuty: Derivative(u(x, y, t), x) + Derivative(v(x, y, t), y)
Item numbers of current derivative formula nodes: 2
{'momentum_x': u(x, y, t)Derivative(u(x, y, t), x) + v(x, y, t)Derivative(u(x, y, t), y) +
Derivative(p(x, y, t), x) + Derivative(u(x, y, t), t) - 0.00999999977648258Derivative(u(x, y, t), (x, 2)) -
0.00999999977648258Derivative(u(x, y, t), (y, 2)),
'momentum_y': u(x, y, t)Derivative(v(x, y, t), x) + v(x, y, t)Derivative(v(x, y, t), y) +
Derivative(p(x, y, t), y) + Derivative(v(x, y, t), t) - 0.00999999977648258Derivative(v(x, y, t), (x, 2)) -
0.00999999977648258Derivative(v(x, y, t), (y, 2)),
'continuty': Derivative(u(x, y, t), x) + Derivative(v(x, y, t), y)}
pde()[源代码]

抽象方法,基于sympy定义的二维NavierStokes控制方程。

返回:

dict,自定义sympy符号方程。

class mindflow.pde.PDEWithLoss(model, in_vars, out_vars)[源代码]

求解偏微分方程问题的基类。 所有用户自定义问题应该从该类继承,用于在每个数据集上设置约束。它用于建立每个子数据集和使用的定义损失函数之间的映射。损失将根据每个子数据集的约束类型自动计算。为了获得目标标签输出,用户必须根据约束类型重载相应的成员函数。例如,对于dataset1,约束类型为“pde”,因此必须重载成员函数“pde”以告诉如何获得pde残差。用于求解残差的数据(例如输入)被传递到parse_node,便可自动计算每个方程的残差。

参数:
  • model (mindspore.nn.Cell) - 用于训练的网络模型。

  • in_vars (List[sympy.core.Symbol]) - model 的输入参数,sympy符号表示的自变量。

  • out_vars (List[sympy.core.Function]) - model 的输出参数,sympy符号表示的因变量。

说明

  • pde 方法必须重写,用于定义sympy符号微分方程。

  • get_loss 方法必须重写,用于计算符号微分方程的损失。

支持平台:

Ascend GPU

样例:

>>> import numpy as np
>>> from mindflow.pde import PDEWithLoss, sympy_to_mindspore
>>> from mindspore import nn, ops, Tensor
>>> from mindspore import dtype as mstype
>>> from sympy import symbols, Function, diff
>>> class Net(nn.Cell):
...     def __init__(self, cin=2, cout=1, hidden=10):
...         super().__init__()
...         self.fc1 = nn.Dense(cin, hidden)
...         self.fc2 = nn.Dense(hidden, hidden)
...         self.fcout = nn.Dense(hidden, cout)
...         self.act = ops.Tanh()
...
...     def construct(self, x):
...         x = self.act(self.fc1(x))
...         x = self.act(self.fc2(x))
...         x = self.fcout(x)
...         return x
>>> model = Net()
>>> class MyProblem(PDEWithLoss):
...     def __init__(self, model, loss_fn=nn.MSELoss()):
...         self.x, self.y = symbols('x t')
...         self.u = Function('u')(self.x, self.y)
...         self.in_vars = [self.x, self.y]
...         self.out_vars = [self.u]
...         super(MyProblem, self).__init__(model, in_vars=self.in_vars, out_vars=self.out_vars)
...         self.loss_fn = loss_fn
...         self.bc_nodes = sympy_to_mindspore(self.bc(), self.in_vars, self.out_vars)
...
...     def pde(self):
...         my_eq = diff(self.u, (self.x, 2)) + diff(self.u, (self.y, 2)) - 4.0
...         equations = {"my_eq": my_eq}
...         return equations
...
...     def bc(self):
...         bc_eq = diff(self.u, (self.x, 1)) + diff(self.u, (self.y, 1)) - 2.0
...         equations = {"bc_eq": bc_eq}
...         return equations
...
...     def get_loss(self, pde_data, bc_data):
...         pde_res = self.parse_node(self.pde_nodes, inputs=pde_data)
...         pde_loss = self.loss_fn(pde_res[0], Tensor(np.array([0.0]), mstype.float32))
...         bc_res = self.parse_node(self.bc_nodes, inputs=bc_data)
...         bc_loss = self.loss_fn(bc_res[0], Tensor(np.array([0.0]), mstype.float32))
...         return pde_loss + bc_loss
>>> problem = MyProblem(model)
>>> print(problem.pde())
>>> print(problem.bc())
my_eq: Derivative(u(x, t), (t, 2)) + Derivative(u(x, t), (x, 2)) - 4.0
Item numbers of current derivative formula nodes: 3
bc_eq: Derivative(u(x, t), t) + Derivative(u(x, t), x) - 2.0
Item numbers of current derivative formula nodes: 3
{'my_eq': Derivative(u(x, t), (t, 2)) + Derivative(u(x, t), (x, 2)) - 4.0}
{'bc_eq': Derivative(u(x, t), t) + Derivative(u(x, t), x) - 2.0}
get_loss()[源代码]

计算所有定义的微分方程的损失。用户必须重写该方法。

parse_node(formula_nodes, inputs=None, norm=None)[源代码]

计算定义的微分方程的预测结果。

参数:
  • formula_nodes (list[FormulaNode]) - 转义后的sympy表达式,可以被MindSpore识别。

  • inputs (Tensor) - 网络模型的输入数据。默认值:None。

  • norm (Tensor) - 输入数据点的法向量。对于曲面上某点P处的法向量是垂直于该点的切平面的向量。默认值:None。

返回:

list[Tensor],偏微分方程的计算结果。

pde()[源代码]

抽象方法,基于sympy定义的控制方程。如果相关约束为控制方程,该方法必须被重写。

class mindflow.pde.Poisson(model, loss_fn='mse')[源代码]

基于PDEWithLoss定义的二维泊松方程求解问题。

参数:
  • model (mindspore.nn.Cell) - 用于训练的网络模型。

  • loss_fn (str) - 损失函数。默认值:”mse”。

支持平台:

Ascend GPU

样例:

>>> from mindflow.pde import Poisson
>>> from mindspore import nn, ops
>>> class Net(nn.Cell):
...     def __init__(self, cin=2, cout=1, hidden=10):
...         super().__init__()
...         self.fc1 = nn.Dense(cin, hidden)
...         self.fc2 = nn.Dense(hidden, hidden)
...         self.fcout = nn.Dense(hidden, cout)
...         self.act = ops.Tanh()
...
...     def construct(self, x):
...         x = self.act(self.fc1(x))
...         x = self.act(self.fc2(x))
...         x = self.fcout(x)
...         return x
>>> model = Net()
>>> problem = Poisson(model)
>>> print(problem.pde())
poisson: Derivative(u(x, y), (x, 2)) + Derivative(u(x, y), (y, 2)) + 1.0
Item numbers of current derivative formula nodes: 3
{'poisson': Derivative(u(x, y), (x, 2)) + Derivative(u(x, y), (y, 2)) + 1.0}
pde()[源代码]

抽象方法,基于sympy定义的二维Poisson控制方程。

返回:

dict,自定义sympy符号方程。

mindflow.pde.sympy_to_mindspore(equations, in_vars, out_vars)[源代码]

将sympy定义的符号方程转换为MindSpore能够支持的语法。

参数:
  • equations (dict) - 自定义偏微分方程组,每个方程的健由用户定义,其值为sympy表达式。

  • in_vars (list[sympy.core.Symbol]) - 求解偏微分方程网络模型的输入参数,sympy符号表示的自变量,和输入数据的维度一致。

  • out_vars (list[sympy.core.Function]) - 求解偏微分方程网络模型的输出参数,sympy符号表示的因变量,和输出数据的维度一致。

返回:

list[FormulaNode],转换后的表达式,能够被MindSpore识别。

支持平台:

Ascend GPU

样例:

>>> from mindflow.pde import sympy_to_mindspore
>>> from sympy import symbols, Function, diff
>>> x, y = symbols('x, y')
>>> u = Function('u')(x, y)
>>> in_vars = [x, y]
>>> out_vars = [u]
>>> eq1 = x + y
>>> eq2 = diff(u, (x, 1)) + diff(u, (y, 1))
>>> equations = {"eq1": eq1, "eq2": eq2}
>>> res = sympy_to_mindspore(equations, in_vars, out_vars)
>>> print(len(res))
eq1: x + y
Item numbers of current derivative formula nodes: 2
eq2: Derivative(u(x, y), x) + Derivative(u(x, y), y)
Item numbers of current derivative formula nodes: 2
2