在 FiPy 中求解总体平衡 PDE 时遇到问题

发布于 2025-01-18 15:25:34 字数 1245 浏览 3 评论 0原文

我是使用FIPY以及通常使用有限体积方法的初学者。因此,如果我的问题很幼稚,请原谅我

,我试图解决形式的人口平衡方程 pde 边界条件如下 边界条件

x范围为0.5e-6到1000e-6的值,我想使用均匀的网眼。

这是我当前拥有的代码,并且无法正常工作。在代码中,我认为术语g和b_0是时间不变,可以使问题更容易。有人可以指导我如何定义上述问题中的术语以获取工作解决方案吗?

from fipy import *
import scipy

b0 = 11.78*10**7
g0 = 2.549e-7

#Define Mesh
nx = 1000
dx = 10e-7
mesh = Grid1D(nx = nx, dx = dx)
x = mesh.cellCenters[0]

#Define coefficients
G = FaceVariable(mesh=mesh, value = g0)

#Defining the solution Variable
phi = CellVariable(name = 'Solution Variable', mesh = mesh, 
                    hasOld = True)

#Set initial condition
g = (scipy.stats.norm(loc = x[500], scale = 5e-6).pdf(x))*b0
phi.setValue(g)


#Set Boundary Condition
(phi*G).constrain(b0, where = mesh.facesLeft)
(phi*G).constrain(0, where = mesh.facesRight)


#Define the convective coefficient
conv_coefficient = G

#Define the Equation
eqn = TransientTerm() == -ConvectionTerm(coeff = conv_coefficient)


steps = 1
for step in range(steps):
    dt = 10
    eqn.solve(var = phi, dt = dt)


plt.plot(phi.value)

I am a beginner in using FiPy and also in working with Finite Volume Methods in general. So please forgive me if my questions are naïve

I am trying to solve a population balance equation of the form
PDE
with the boundary conditions as follows
Boundary Conditions

The value of x ranges from 0.5e-6 to 1000e-6 and I want to use a uniform mesh.

This is the code that I currently have and it is not working. In the code I have assumed that the terms G and b_0 are time invariant to make the problem easier. Can someone please guide me about how I should define the terms in the above problem to get a working solution?

from fipy import *
import scipy

b0 = 11.78*10**7
g0 = 2.549e-7

#Define Mesh
nx = 1000
dx = 10e-7
mesh = Grid1D(nx = nx, dx = dx)
x = mesh.cellCenters[0]

#Define coefficients
G = FaceVariable(mesh=mesh, value = g0)

#Defining the solution Variable
phi = CellVariable(name = 'Solution Variable', mesh = mesh, 
                    hasOld = True)

#Set initial condition
g = (scipy.stats.norm(loc = x[500], scale = 5e-6).pdf(x))*b0
phi.setValue(g)


#Set Boundary Condition
(phi*G).constrain(b0, where = mesh.facesLeft)
(phi*G).constrain(0, where = mesh.facesRight)


#Define the convective coefficient
conv_coefficient = G

#Define the Equation
eqn = TransientTerm() == -ConvectionTerm(coeff = conv_coefficient)


steps = 1
for step in range(steps):
    dt = 10
    eqn.solve(var = phi, dt = dt)


plt.plot(phi.value)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寒尘 2025-01-25 15:25:34

您的脚本有几个问题:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [1], in <cell line: 21>()
     17 phi = CellVariable(name = 'Solution Variable', mesh = mesh, 
     18                     hasOld = True)
     20 #Set initial condition
---> 21 g = (scipy.stats.norm(loc = x[500], scale = 5e-6).pdf(x))*b0
     22 phi.setValue(g)
     25 #Set Boundary Condition

AttributeError: module 'scipy' has no attribute 'stats'

您必须在使用它们之前导入特定的Scipy模块,例如导入scipy.stats

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [2], in <cell line: 26>()
     22 phi.setValue(g)
     25 #Set Boundary Condition
---> 26 (phi*G).constrain(b0, where = mesh.facesLeft)
     27 (phi*G).constrain(0, where = mesh.facesRight)
     30 #Define the convective coefficient

TypeError: unsupported operand type(s) for *: 'CellVariable' and 'FaceVariable'

Phi在单元中心定义。 g在单元格之间的面上定义。 Fipy不知道如何在两个不同的位置繁殖变量。

如书面,您的第一个边界条件可以重新排列到dirichlet条件

phi.constrain(b0/G, where=mesh.facesLeft)

​=“ https://i.sstatic.net/uihij.png” rel =“ nofollow noreferrer”> ,或

phi.constrain(0, where=mesh.facesRight)
---------------------------------------------------------------------------
VectorCoeffError                          Traceback (most recent call last)
Input In [3], in <cell line: 34>()
     31 conv_coefficient = G
     33 #Define the Equation
---> 34 eqn = TransientTerm() == -ConvectionTerm(coeff = conv_coefficient)
     37 steps = 1
     38 for step in range(steps):

VectorCoeffError: The coefficient must be a vector value.

coeff对流必须是向量(rank-1),但是cons_coeff = g是等级 - 0。您可以制作g rank-1(可能应该),但是您的边界条件没有意义,因为 “在此处输入图像描述” 表示“ vector * scalar = scalar = scalar”。因为 ”在此处输入图像说明“ 只是时间的函数,您可以通过将定义更改为

G = Variable(value = g0)

,而

conv_coefficient = G * [[1]]

完整的代码

from fipy import *
import scipy.stats

b0 = 11.78*10**7
g0 = 2.549e-7

#Define Mesh
nx = 1000
dx = 10e-7
mesh = Grid1D(nx = nx, dx = dx)
x = mesh.cellCenters[0]

#Define coefficients
G = Variable(value = g0)

#Defining the solution Variable
phi = CellVariable(name = 'Solution Variable', mesh = mesh, 
                    hasOld = True)

#Set initial condition
g = (scipy.stats.norm(loc = x[500], scale = 5e-6).pdf(x))*b0
phi.setValue(g)


#Set Boundary Condition
phi.constrain(b0/G, where = mesh.facesLeft)
phi.constrain(0, where = mesh.facesRight)


#Define the convective coefficient
conv_coefficient = G * [[1]]

#Define the Equation
eqn = TransientTerm() == -ConvectionTerm(coeff = conv_coefficient)


steps = 1
for step in range(steps):
    dt = 10
    eqn.solve(var = phi, dt = dt)


Viewer(vars=phi).plot()

仍然存在疑问,因为您的表达方式不一致关于标量和矢量数量。我强烈建议您尝试解决1D表达的PDE。几乎总是误导。您需要知道什么是标量,什么是向量,这很难在1D中跟踪。

Your script has several problems:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [1], in <cell line: 21>()
     17 phi = CellVariable(name = 'Solution Variable', mesh = mesh, 
     18                     hasOld = True)
     20 #Set initial condition
---> 21 g = (scipy.stats.norm(loc = x[500], scale = 5e-6).pdf(x))*b0
     22 phi.setValue(g)
     25 #Set Boundary Condition

AttributeError: module 'scipy' has no attribute 'stats'

You must import specific scipy modules before using them, e.g., import scipy.stats.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [2], in <cell line: 26>()
     22 phi.setValue(g)
     25 #Set Boundary Condition
---> 26 (phi*G).constrain(b0, where = mesh.facesLeft)
     27 (phi*G).constrain(0, where = mesh.facesRight)
     30 #Define the convective coefficient

TypeError: unsupported operand type(s) for *: 'CellVariable' and 'FaceVariable'

phi is defined at cell centers. G is defined at the faces between cells. FiPy doesn't know how to multiply variables in two different locations.

As written, your first boundary condition can be rearranged to the Dirichlet condition f1(0,t) = bdot(t) / G(t), or

phi.constrain(b0/G, where=mesh.facesLeft)

Your second boundary condition can be rearranged to the Dirichlet condition enter image description here, or

phi.constrain(0, where=mesh.facesRight)
---------------------------------------------------------------------------
VectorCoeffError                          Traceback (most recent call last)
Input In [3], in <cell line: 34>()
     31 conv_coefficient = G
     33 #Define the Equation
---> 34 eqn = TransientTerm() == -ConvectionTerm(coeff = conv_coefficient)
     37 steps = 1
     38 for step in range(steps):

VectorCoeffError: The coefficient must be a vector value.

The coeff argument to ConvectionTerm must be a vector (rank-1), but conv_coeff = G is rank-0. You can make G rank-1 (and probably should), but then your boundary condition doesn't make sense, as enter image description hereexpresses "vector * scalar = scalar". Since enter image description hereand enter image description hereare only functions of time, you can make this work by changing the definitions to

G = Variable(value = g0)

and

conv_coefficient = G * [[1]]

The full code is

from fipy import *
import scipy.stats

b0 = 11.78*10**7
g0 = 2.549e-7

#Define Mesh
nx = 1000
dx = 10e-7
mesh = Grid1D(nx = nx, dx = dx)
x = mesh.cellCenters[0]

#Define coefficients
G = Variable(value = g0)

#Defining the solution Variable
phi = CellVariable(name = 'Solution Variable', mesh = mesh, 
                    hasOld = True)

#Set initial condition
g = (scipy.stats.norm(loc = x[500], scale = 5e-6).pdf(x))*b0
phi.setValue(g)


#Set Boundary Condition
phi.constrain(b0/G, where = mesh.facesLeft)
phi.constrain(0, where = mesh.facesRight)


#Define the convective coefficient
conv_coefficient = G * [[1]]

#Define the Equation
eqn = TransientTerm() == -ConvectionTerm(coeff = conv_coefficient)


steps = 1
for step in range(steps):
    dt = 10
    eqn.solve(var = phi, dt = dt)


Viewer(vars=phi).plot()

I still have doubts as your expressions aren't consistent about scalar and vector quantities. I strongly recommend against trying to solve PDEs expressed in 1D. It's almost always misleading. You need to know what is scalar and what is vector and this is hard to keep track of in 1D.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文