项目

一般

简介

新增功能 #65 » sample.cpp

王 绪伦, 2024-07-12 07:52

 
1
#include <Gmsh.h>  
2
#include <vector>  
3
  
4
int main() {  
5
    // 初始化Gmsh  
6
    gmsh::initialize();  
7
  
8
    // 创建一个新的模型  
9
    gmsh::model::add("complex_shape");  
10
  
11
    // 定义曲面1的三角片  
12
    std::vector<std::vector<int>> triangles1 = {  
13
        {1, 2, 3},  
14
        {3, 4, 1}  
15
    };  
16
  
17
    // 定义曲面2的三角片  
18
    std::vector<std::vector<int>> triangles2 = {  
19
        {5, 6, 7},  
20
        {7, 8, 5}  
21
    };  
22
  
23
    // 添加曲面1的顶点和三角片  
24
    std::vector<double> coordinates1 = {  
25
        0.0, 0.0, 0.0, // 顶点1  
26
        1.0, 0.0, 0.0, // 顶点2  
27
        1.0, 1.0, 0.0, // 顶点3  
28
        0.0, 1.0, 0.0  // 顶点4  
29
    };  
30
    for (size_t i = 0; i < coordinates1.size() / 3; ++i) {  
31
        gmsh::model::geo::addPoint(coordinates1[3*i], coordinates1[3*i+1], coordinates1[3*i+2], 0.1, i+1);  
32
    }  
33
    for (const auto& triangle : triangles1) {  
34
        gmsh::model::geo::addLine(triangle[0], triangle[1], 10 + (&triangle - &triangles1[0]) * 3);  
35
        gmsh::model::geo::addLine(triangle[1], triangle[2], 11 + (&triangle - &triangles1[0]) * 3);  
36
        gmsh::model::geo::addLine(triangle[2], triangle[0], 12 + (&triangle - &triangles1[0]) * 3);  
37
    }  
38
    std::vector<int> lines1 = {10, 11, 12, 13};  
39
    gmsh::model::geo::addCurveLoop(lines1, 20);  
40
  
41
    // 添加曲面2的顶点和三角片  
42
    std::vector<double> coordinates2 = {  
43
        1.0, 1.0, 0.0, // 顶点5(与曲面1的顶点3共享)  
44
        2.0, 1.0, 0.0, // 顶点6  
45
        2.0, 2.0, 0.0, // 顶点7  
46
        1.0, 2.0, 0.0  // 顶点8  
47
    };  
48
    for (size_t i = 0; i < coordinates2.size() / 3; ++i) {  
49
        int pointTag = i + 1 + coordinates1.size() / 3; // 顶点标签从曲面1的最后一个顶点之后开始  
50
        gmsh::model::geo::addPoint(coordinates2[3*i], coordinates2[3*i+1], coordinates2[3*i+2], 0.1, pointTag);  
51
    }  
52
    for (const auto& triangle : triangles2) {  
53
        gmsh::model::geo::addLine(triangle[0], triangle[1], 20 + (&triangle - &triangles2[0]) * 3);  
54
        gmsh::model::geo::addLine(triangle[1], triangle[2], 21 + (&triangle - &triangles2[0]) * 3);  
55
        gmsh::model::geo::addLine(triangle[2], triangle[0], 22 + (&triangle - &triangles2[0]) * 3);  
56
    }  
57
    std::vector<int> lines2 = {20, 21, 22, 23};  
58
    gmsh::model::geo::addCurveLoop(lines2, 30);  
59
  
60
    // 添加实体  
61
    std::vector<int> surfaceTags = {20, 30};  
62
    gmsh::model::geo::addVolume({surfaceTags}, 40);  
63
  
64
    // 设置网格划分参数  
65
    gmsh::model::mesh::setAlgorithm(gmsh::mesh::algorithm::FRONT_DELAUNAY, 0);  
66
    gmsh::model::mesh::setSize(gmsh::mesh::size::LCAR, 0.1);  
67
  
68
    // 执行网格划分  
69
    gmsh::model::mesh::generate(3);  
70
  
71
    // 保存网格到文件  
72
    gmsh::write("complex_shape.msh");  
73
  
74
    // 清理并退出Gmsh  
75
    gmsh::finalize();  
76
  
77
    return 0;  
78
}
    (1-1/1)