我用Freefem++来解泊松方程
梯度^2u(x,y,z) = -f(x,y,z)
当我有一个f的解析表达式时,它工作得很好,但是现在我有了一个数值定义的f(即定义在一个网格上的一组数据),并且我想知道我是否还可以使用Freefem++。
例如,典型的代码(在本例中是2D问题),如下所示
mesh Sh= square(10,10); // mesh generation of a square
fespace Vh(Sh,P1); // space of P1 Finite Elements
Vh u,v; // u and v belongs to Vh
func f=cos(x)*y; // analytical function
problem Poisson(u,v)= // Definition of the problem
int2d(Sh)(dx(u)*dx(v)+dy(u)*dy(v)) // bilinear form
-int2d(Sh)(f*v) // linear form
+on(1,2,3,4,u=0); // Dirichlet Conditions
Poisson; // Solve Poisson Equation
plot(u); // Plot the result我想知道我是否能用数字来定义f,而不是解析的。
发布于 2014-03-25 16:59:24
网格与空间定义
我们用Nx=10网格和Ny=10定义了一个正方形单元,它在x轴上提供了11个节点,对于y轴提供了相同的节点。
int Nx=10,Ny=10;
int Lx=1,Ly=1;
mesh Sh= square(Nx,Ny,[Lx*x,Ly*y]); //this is the same as square(10,10)
fespace Vh(Sh,P1); // a space of P1 Finite Elements to use for u definition条件与问题陈述
我们不打算使用more,但是我们将处理矩阵(一种使用FreeFem解决的更复杂的方法)。
首先,我们为我们的问题定义CL (Dirichlet问题)。
varf CL(u,psi)=on(1,2,3,4,u=0); //you can eliminate border according to your problem state
Vh u=0;u[]=CL(0,Vh);
matrix GD=CL(Vh,Vh);然后我们定义问题。我建议不要编写dx(u)*dx(v)+dy(u)*dy(v),而是建议使用宏,因此我们将div定义为以下内容,但要注意由//而不是;完成的宏。
macro div(u) (dx(u[0])+dy(u[1])) //Poisson双线性形式变成:
varf Poisson(u,v)= int2d(Sh)(div(u)*div(v));在我们提取出矩阵之后
matrix K=Poisson(Vh,Vh);
matrix KD=K+GD; //we add CL defined above我们继续求解,UMFPACK是FreeFem中的一个求解者,对此不太重视。
set(KD,solver=UMFPACK);这里是你需要的。您希望在某些特定节点上定义函数f的值。我要给你们一个秘密,泊松线性形式。
real[int] b=Poisson(0,Vh);在您想要执行的任何节点上定义函数f的值。
b[100]+=20; //for example at node 100 we want that f equals to 20
b[50]+=50; //and at node 50 , f equals to 50 我们解决我们的系统。
u[]=KD^-1*b; 我们终于得到了情节。
plot(u,wait=1);我希望这会对你有所帮助,感谢我的实习主管奥利维尔,他总是在FreeFem上给我特别的技巧。我测试过了,效果很好。祝好运。
发布于 2015-03-28 17:04:04
afaf的方法适用于函数f是独立的情况。对于int2d(Sh)(f*u*v)这样的术语,还需要另一种解决方案。我提议(实际上,我在Hecht的手册中的某个地方)一种涵盖这两种情况的方法。然而,它只适用于P1有限元,其自由度与网格节点重合。
fespace Vh(Th,P1);
Vh f;
real[int] pot(Vh.ndof);
for(int i=0;i<Vh.ndof;i++){
pot[i]=something; //assign values or read them from a file
}
f[]=pot;https://stackoverflow.com/questions/22578855
复制相似问题