在 GMSH Physical Line 获取 FaceVariable

Get FaceVariable at GMSH Physical Line

我成功地在使用 GMSH 生成的网格上设置了一个非常简单的扩散问题,包括源面(连续体)和汇面(侧壁、顶部)。

from fipy import *

mesh = Gmsh2D('''Point(1) = {0, 0, 0, 1.0};
Point(2) = {12, 0, 0, 0.1};
Point(3) = {12, 16, 0, 0.1};
Point(4) = {15, 16, 0, 0.1};
Point(5) = {15, 100, 0, 1.0};
Point(6) = {0, 100, 0, 1.0};

Line(1) = {1, 2};
Line(2) = {2, 3};
Line(3) = {3, 4};
Line(4) = {4, 5};
Line(5) = {5, 6};
Line(6) = {6, 1};

Line Loop(1) = {1, 2, 3, 4, 5, 6};

Plane Surface(1) = {1};

Physical Line("continuum") = {5};
Physical Line("sidewall") = {2};
Physical Line("top") = {3};

Physical Surface("domain") = {1};
''')

c = CellVariable(name='concentration', mesh=mesh, value=0.)

c.faceGrad.constrain([-0.05 * c.harmonicFaceValue], mesh.physicalFaces["sidewall"])
c.faceGrad.constrain([0.05 * c.harmonicFaceValue], mesh.physicalFaces["top"])
c.constrain(1., mesh.physicalFaces["continuum"])

dim = 1.
D = 1.
dt = 50 * dim**2 / (2 * D)
steps = 100

eq = TransientTerm() == DiffusionTerm(coeff=D)

viewer = Viewer(vars=(c, c.grad), datamin=0., datamax=1.)

for step in range(steps):
    eq.solve(var=c, dt=dt)
    viewer.plot()

TSVViewer(vars=(c, c.grad)).plot(filename="conc.tsv")

raw_input('Press any key...')

计算按预期进行,但我想访问我在 GMSH 中设置的物理面的梯度。我知道我可以使用 c.faceGrad 获得面部的渐变,使用 mesh.physicalFaces['sidewall'] 获得代表物理面部的遮罩。要获得包含在物理面中的面的渐变,我希望使用像 c.faceGrad[mesh.physicalFaces['sidewall']] 这样的索引。但是,这不会产生预期的结果。有没有办法在 physicalFaces 指定的位置访问 FaceVariable?

请记住 c.faceGrad 的形状为 (2, 22009),因此掩码对第二个索引进行操作,因为 mesh.physicalFaces['sidewall'] 的形状为 (22009,)。所以,试试

c.faceGrad[:, mesh.physicalFaces['sidewall']]

并访问正确的形状使用

np.array(c.faceGrad[:, mesh.physicalFaces['sidewall']]).shape

也就是 (2, 160).