计算GMSH地理脚本

时间:2017-06-17 07:25:07

标签: matlab linear-algebra mesh

我遇到了一个问题,需要计算要在Gmsh .geo文件中使用的矩阵的系数。我用Matlab创建了一个小脚本来解决2x2矩阵。代码如下;

clear all;clc;close all 
R = 0.509515; % m
R1 = 0.559467; % m
R2 = 0.579594; % m
L = 0;
L1 = -0.08568;
L2 = 0.13974;
y = [R1-R;R2-R];
x = [L1^2 L1;L2^2 L2];
c = x\y;
x1 = linspace(-0.08568,0.13974,20);

从上面的小代码中,我需要使用矩阵c中的系数,我将其作为c1c2手动转移到以下地理位置文件

R = 0.509515; //% m
R11 = 0.10; //% m
R1 = 0.559467; //% m
R2 = 0.579594; //% m
L = 0;
L1 = -0.08568; 
L2 = 0.13974;
c1 = 4.811;
c2 = -0.1708; 

nPoints = 20; // Number of discretization points (top-right point of the inlet region)
For i In {1 : nPoints}
  x = L1 + (L2-L1)*i/(nPoints + 1);
  pList[i] = newp;
  Point(pList[i]) = {x,
                ( c1*x^2+c2*x+R ),
                0};
EndFor
Spline(1) = {20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};

请注意,在地理位置文件中,我无法使用linspace,因为我不知道GMSH中是否存在linspace语法。我的目标是使地理文件解决矩阵以适合二次多项式。

1 个答案:

答案 0 :(得分:0)

最简单的选择就是在GMSH内部解决2 x 2线性方程组。

通过添加以下计算而不是直接插入c1c2的值,将工作从Matlab移至GMSH。注意,由于它是一个2 x 2系统,我没有考虑数值稳定性;然而,对于这样一个实际应用的小系统来说,无论如何都不应该是重要的。

// Calculating solution of Ax=y
// A = [L1^2 L1; L2^2 L2]; y = [R1-R; R2-R]; x = [c1;c2]
// Ainv = 1/det(A)*[L2 -L1; -L2^2 L1^2]
// x = Ainv*y
det = L1*L1*L2-L1*L2*L2;
If (Abs(det) < 1E-10) //Yeah, I am lazy.
    Printf("Ill-conditioned fit required. Will crash.");
EndIf
factor = 1./det;
c1 = factor*(L2*(R1-R) - L1*(R2-R));
c2 = factor*(-L2*L2*(R1-R) +L1*L1*(R2-R));