先看下面這段官方自帶腳本
/*********************************************************************** Gmsh tutorial 1** Variables, elementary entities (points, curves, surfaces), physical* entities (points, curves, surfaces)**********************************************************************/// The simplest construction in Gmsh's scripting language is the
// `affectation'. The following command defines a new variable `lc':lc = 1e-2;// This variable can then be used in the definition of Gmsh's simplest
// `elementary entity', a `Point'. A Point is defined by a list of four numbers:
// three coordinates (X, Y and Z), and a characteristic length (lc) that sets
// the target element size at the point:Point(1) = {0, 0, 0, lc};// The distribution of the mesh element sizes is then obtained by interpolation
// of these characteristic lengths throughout the geometry. Another method to
// specify characteristic lengths is to use general mesh size Fields (see
// `t10.geo'). A particular case is the use of a background mesh (see `t7.geo').// We can then define some additional points as well as our first curve. Curves
// are Gmsh's second type of elementery entities, and, amongst curves, straight
// lines are the simplest. A straight line is defined by a list of point
// numbers. In the commands below, for example, the line 1 starts at point 1 and
// ends at point 2:Point(2) = {.1, 0, 0, lc} ;
Point(3) = {.1, .3, 0, lc} ;
Point(4) = {0, .3, 0, lc} ;Line(1) = {1,2} ;
Line(2) = {3,2} ;
Line(3) = {3,4} ;
Line(4) = {4,1} ;// The third elementary entity is the surface. In order to define a simple
// rectangular surface from the four curves defined above, a curve loop has first
// to be defined. A curve loop is a list of connected curves, a sign being
// associated with each curve (depending on the orientation of the curve):Curve Loop(1) = {4,1,-2,3} ;// We can then define the surface as a list of curve loops (only one here, since
// there are no holes--see `t4.geo'):Plane Surface(1) = {1} ;// At this level, Gmsh knows everything to display the rectangular surface 6 and
// to mesh it. An optional step is needed if we want to group elementary
// geometrical entities into more meaningful groups, e.g. to define some
// mathematical ("domain", "boundary"), functional ("left wing", "fuselage") or
// material ("steel", "carbon") properties.
//
// Such groups are called "Physical Groups" in Gmsh. By default, if physical
// groups are defined, Gmsh will export in output files only those elements that
// belong to at least one physical group. (To force Gmsh to save all elements,
// whether they belong to physical groups or not, set "Mesh.SaveAll=1;", or
// specify "-save_all" on the command line.)
//
// Here we define a physical curve that groups the left, bottom and right lines
// in a single group (with prescribed tag 5); and a physical surface with name
// "My surface" (with an automatic tag) containg the geometrical surface 1:Physical Curve(5) = {1, 2, 4} ;
Physical Surface("My surface") = {1} ;// Note that starting with Gmsh 3.0, models can be built using different
// geometry kernels than the default "built-in" kernel. By specifying
//
// SetFactory("OpenCASCADE");
//
// any subsequent command in the .geo file would be handled by the OpenCASCADE
// geometry kernel instead of the built-in kernel. A rectangular surface could
// then simply be created with
//
// Rectangle(2) = {.2, 0, 0, 0.1, 0.3};
//
// See tutorial/t16.geo for a complete example, and demos/boolean for more.
//+
Field[1] = Box;
//+
Delete Field [1];
以下是該Gmsh腳本代碼的逐段解釋:
1. 定義特征長度
lc = 1e-2;
- 作用:設置網格的特征長度為0.01,該值將影響后續生成的網格密度。
取0.01時,如下:
取0.1時,如下:
- 說明:
lc
是局部網格尺寸的基準值,越小生成的網格越密。
2. 創建點(Points)
Point(1) = {0, 0, 0, lc};
Point(2) = {.1, 0, 0, lc};
Point(3) = {.1, .3, 0, lc};
Point(4) = {0, .3, 0, lc};
- 作用:在二維平面上定義四個點。
- 參數:
Point(標簽) = {X坐標, Y坐標, Z坐標, 特征長度}
。 - 結果:四個點構成矩形的四個頂點(左下、右下、右上、左上)。
3. 創建線段(Lines)
Line(1) = {1, 2}; // 從點1到點2的線段
Line(2) = {3, 2}; // 從點3到點2的線段
Line(3) = {3, 4}; // 從點3到點4的線段
Line(4) = {4, 1}; // 從點4到點1的線段
- 作用:通過連接點生成四條線段。
- 注意:線段方向影響后續曲線環的定義,負號表示反向(例如
-2
表示線段2的反方向)。
4. 定義曲線環(Curve Loop)
Curve Loop(1) = {4, 1, -2, 3};
- 作用:將線段組合成閉合的環形,用于生成平面。
- 順序:按閉合路徑依次連接線段:
- 線段4(點4→點1)
- 線段1(點1→點2)
- 線段-2(點2→點3,反向線段2)
- 線段3(點3→點4)
5. 創建平面表面(Surface)
Plane Surface(1) = {1};
- 作用:通過曲線環1生成平面表面。
- 說明:此處定義了一個矩形區域,后續將在此區域內生成網格。
6. 定義物理實體(Physical Entities)
Physical Curve(5) = {1, 2, 4};
Physical Surface("My surface") = {1};
- 作用:將幾何實體分組,用于后續仿真或導出。
- 物理曲線5:包含線段1、2、4,代表模型的邊界(例如左、下、右邊)。
- 物理表面"My surface":包含表面1,代表整個矩形區域。
- 意義:物理實體用于在導出網格時標記不同區域(如邊界條件、材料屬性)。
7. 其他代碼片段
Field[1] = Box;
Delete Field [1];
- 作用:嘗試定義一個
Box
類型的場(用于控制網格尺寸),但隨后被刪除。 - 說明:這段代碼可能是測試或誤操作,實際未生效。
關鍵概念總結
- 特征長度 lc:控制網格密度,值越小網格越密。
- 曲線環方向:線段的正負號決定方向,確保閉合路徑正確。
- 物理實體:
- 定義仿真中需要關注的區域(如邊界、體積)。
- 默認僅導出屬于物理實體的網格單元。
生成的幾何結構
- 一個矩形區域,左下角在原點 (0,0),右上角在 (0.1, 0.3)。
- 物理曲線標記了左、下、右邊界,物理表面標記了整個矩形區域。
通過此腳本,Gmsh將生成一個帶有結構化網格的矩形,并僅導出標記的物理實體部分。