在 FiPy 中使用扫描功能时的求解器容差和残差 [英] Solver tolerance and residual error when using sweep function in FiPy

查看:124
本文介绍了在 FiPy 中使用扫描功能时的求解器容差和残差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我意识到命令 sweep 没有像我想象的那样工作时,我试图使用 FiPy 来解决一组 PDE.这是我的部分代码示例:

I was trying to use FiPy to solve a set of PDEs when I realized the command sweep was not working the way I thought it would. Here goes a sample with part of my code:

from pylab import *
import sys
from fipy import *

viscosity = 5.55555555556e-06 

Pe =5.

pfi=100.
lfi=0.01

Ly=1.
Nx =200
Ny=100
Lx=Ly*Nx/Ny
dL=Ly/Ny
mesh = PeriodicGrid2DTopBottom(nx=Nx, ny=Ny, dx=dL, dy=dL)

x, y = mesh.cellCenters

xVelocity = CellVariable(mesh=mesh, hasOld=True,  name='X velocity')

xVelocity.constrain(Pe, mesh.facesLeft)
xVelocity.constrain(Pe, mesh.facesRight)

rad=0.1

var1 = DistanceVariable(name='distance to center', mesh=mesh, value=numerix.sqrt((x-Nx*dL/2.)**2+(y-Ny*dL/2.)**2))

pi_fi= CellVariable(mesh=mesh, value=0.,name='Fluid-interface energy map')
pi_fi.setValue(pfi*exp(-1.*(var1-rad)/lfi), where=(var1 > rad) )
pi_fi.setValue(pfi, where=(var1 <= rad))

xVelocityEq = DiffusionTerm(coeff=viscosity) - ImplicitSourceTerm(pi_fi)

xres=10.
while (xres > 1.e-6) :
        xVelocity.updateOld()
        mySolver = LinearGMRESSolver(iterations=1000,tolerance=1.e-6)
        xres = xVelocityEq.sweep(var=xVelocity,solver=mySolver)
        print 'Result = ', xres
#Thats it

简而言之,我声明了一个名为 xVelocityEq 的函数并使用 sweep 解决它.这是我的输出:

In short, I am declaring a function called xVelocityEq and solving it using sweep. Here is my output:

Result =  0.0007856742013190237
Result =  6.414470433257661e-07

如您所见,while 循环在两次迭代后结束.我的第一个问题是:为什么我的第一个残差 (=0.0007856742013190237) 高于求解器的容差?我认为,由于 xVelocityEq 对应于线性系统,求解器容差和残差将意味着相同的事情.

As you can see, the while loop ends after two iterations. My first question is: why is my first residual error (=0.0007856742013190237) higher than the solver's tolerance? I thought that, since xVelocityEq corresponds to a linear system, solver tolerance and residual error would mean the same thing.

如果我增加编号.mySolver 中从 1000 到 10000 的迭代次数,我得到以下输出:

If I increase the no. of iterations in mySolver from 1000 to 10000, I get the following output:

Result =  0.0007856742013190237
Result =  2.4619110931978988e-09

在第一个保持不变的情况下,为什么第二个残差会发生变化?

如果我将 mySolver 中的容差从 1.e-6 增加到 7.e-4,我会得到以下输出:

If I increase the tolerance in mySolver from 1.e-6 to 7.e-4, I get the following output:

Result =  0.0007856742013190237
Result =  6.414470433257661e-07

请注意,这些残差与第一个输出中的相同.现在,如果我尝试将容差进一步增加到 8.e-4,我将得到以下输出:

Note that these residuals are the same as in the first output. Now if I try to further increase the tolerance to 8.e-4, here's what I get as output:

Result =  0.0007856742013190237
Result =  0.0007856742013190237
Result =  0.0007856742013190237
Result =  0.0007856742013190237
Result =  0.0007856742013190237
...

在这一点上,我完全迷失了.为什么所有小于 7.e-4 的求解器容差的残差都具有相同的值?以及为什么对于高于 7.e-4 的求解器容差,这些残差是恒定的并且等于 0.0007856742013190237?

At this point I was completely lost. Why the residuals have the same values for all solver tolerances smaller than 7.e-4? And why these residuals are constant and equal to 0.0007856742013190237 for solver tolerances higher than 7.e-4?

如果我将 mySolver 更改为 LinearLUSolver(迭代数 = 1000,公差 = 1.e-6),这就是我得到的:

If I change the mySolver to LinearLUSolver (iterations=1000, tolerance=1.e-6), here's what I get:

Result =  0.0007856742013190237
Result =  1.6772757200988522e-18

为什么我的第一个残差和以前一样,即使我改变了求解器?

推荐答案

为什么我的第一个残差 (=0.0007856742013190237) 高于求解器的容差?

why is my first residual error (=0.0007856742013190237) higher than the solver's tolerance?

.sweep() 计算的残差是在之前计算的,求解器被调用以计算新的解向量.矩阵L和右侧向量b是基于解向量x的初始值计算的.

The residual calculated by .sweep() is calculated before the solver is invoked to calculated a new solution vector. The matrix L and right-hand-side vector b are calculated based on the initial value of the solution vector x.

残差衡量当前解向量满足非线性 PDE 的程度.求解器容差限制了求解器满足线性方程组的难度.

The residual is a measure of how well the current solution vector satisfies the non-linear PDE. The solver tolerance places a limit on how hard the solver should work to satisfy the linear system of equations discretized from the PDE.

即使 PDE 是线性的(例如,扩散系数不是解变量的函数),初始值也可能无法求解 PDE,因此残差很大.调用求解器后,x 应在求解器容差范围内求解偏微分方程.如果偏微分方程是非线性的,那么线性代数的良好收敛解仍然可能不是偏微分方程的好解;这就是清扫的目的.

Even if the PDE is linear (e.g., the diffusion coefficient is not a function of the solution variable), the initial value presumably doesn't solve the PDE, so the residual is large. After the solver is invoked, then x should solve the PDE, to within the solver tolerance. If the PDE is non-linear, then a well-converged solution to the linear algebra is still probably not a good solution to the PDE; that's what sweeping is for.

我认为,因为 xVelocityEq 对应于一个线性系统,所以求解器容差和残差是同一回事.

I thought that, since xVelocityEq corresponds to a linear system, solver tolerance and residual error would mean the same thing.

跟踪两者没有任何效用.除了求解之前的残差和用于终止求解的求解器容差之外,还可以使用不同的归一化,并且许多求解器文档可能有点粗略.FiPy 使用 |L x - b|_2 作为其残差.求解器可能会根据 b 的大小、L 的对角线或月相进行归一化,所有这些都很难直接将残差与容差进行比较.

There wouldn't be any utility in keeping track of both. In addition to the residual being before the solve and the solver tolerance being used to terminate the solve, there are different normalizations that can be used and a lot of the solver documentation can be kind of sketchy. FiPy uses |L x - b|_2 as its residual. Solvers may normalize by the magnitude of b, the diagonal of L, or the phase of the moon, all of which can make it hard to directly compare the residual with the tolerance.

既然第一个保持不变,为什么第二个残差会发生变化?

Why did the second residual change, given that the first remained the same?

通过允许 1000 次迭代而不是 100 次,求解器能够驱动到更精确的容差,从而导致下一次扫描的残差更小.

By allowing 1000 iterations instead of 100, the solver was able to drive to a more exacting tolerance which, in turn, led to a smaller residual for the next sweep.

为什么所有小于 7.e-4 的求解器容差的残差都具有相同的值?以及为什么对于高于 7.e-4 的求解器容差,这些残差是恒定的并且等于 0.0007856742013190237?

Why the residuals have the same values for all solver tolerances smaller than 7.e-4? And why these residuals are constant and equal to 0.0007856742013190237 for solver tolerances higher than 7.e-4?

可能是因为求解器失败了,所以没有改变解向量的值.一些求解器不报告这一点.在其他情况下,我们应该更好地向您报告这一事实.

Probably because the solver is failing and so not changing the value of the solution vector. Some solvers don't report this. In other cases, we should be doing a better job of reporting that fact to you.

为什么我的第一个残差和以前一样,即使我改变了求解器?

Why in the world is my first residual the same as before, even though I have changed the solver?

残差不是求解器的属性.它是近似 PDE 的离散方程组的一个属性.这些线性代数方程然后是求解器的输入.

The residual is not a property of the solver. It is a property of the discretized system of equations that approximates your PDE. Those linear algebra equations are then the input to the solver.

这篇关于在 FiPy 中使用扫描功能时的求解器容差和残差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆