在前面兩篇文章1、2中我們分別介紹了圖形化界面和內置解析器geo腳本的使用方式。今天來介紹下Gmsh的第三種使用方式:使用Gmsh API將其集成到其他軟件中。
意義
將網格生成器與求解器等軟件對接形成整體框架。
獲取Gmsh API幾種方式
- 通過官網下載SDK http://www.gmsh.info/bin/Windows/gmsh-git-Windows64-sdk.zip
- pip install --upgrade gmsh (Python)
- 在編譯時加上 cmake -DENABLE_BUILD_DYNAMIC=1 …選項
使用
使用Gmsh API時只需引入導入其對應的庫即可,下面以c++版本爲例。代碼是官方教程的x1.cpp基礎上稍作修改,在一些需關注的部分添加了中文註釋。在編譯時需加上-lgmsh選項以包括動態鏈接庫。
// -----------------------------------------------------------------------------
//
// Gmsh C++ extended tutorial 1
//
// Geometry and mesh data
//
// -----------------------------------------------------------------------------
// The C++ API allows to do much more than what can be done in .geo files. These
// additional features are introduced gradually in the extended tutorials,
// starting with `x1.cpp'.
// In this first extended tutorial, we start by using the API to access basic
// geometrical and mesh data.
#include <iostream>
#include <gmsh.h>
int main(int argc, char **argv)
{
// 判斷輸入參數數量是否符合要求。
if(argc < 2) {
std::cout << "Usage: " << argv[0] << " file" << std::endl;
return 0;
}
std::cout<<"modify by djm"<<std::endl;
// 在使用Gmsh API之前必須調用的初試函數
gmsh::initialize();
// 命令行輸出相關信息
gmsh::option::setNumber("General.Terminal", 1);
// You can run this tutorial on any file that Gmsh can read, e.g. a mesh file
// in the MSH format: `t1.exe file.msh'
// 使用open方法打開參數1的文件
gmsh::open(argv[1]);
// Print the model name and dimension:
std::string name;
gmsh::model::getCurrent(name);
// 生成三維網格
gmsh::model::mesh::generate(3);
// Geometrical data is made of elementary model `entities', called `points'
// (entities of dimension 0), `curves' (entities of dimension 1), `surfaces'
// (entities of dimension 2) and `volumes' (entities of dimension 3). As we
// have seen in the other C++ tutorials, elementary model entities are
// identified by their dimension and by a `tag': a strictly positive
// identification number. Model entities can be either CAD entities (from the
// built-in `geo' kernel or from the OpenCASCADE `occ' kernel) or `discrete'
// entities (defined by a mesh). `Physical groups' are collections of model
// entities and are also identified by their dimension and by a tag.
// Get all the elementary entities in the model, as a vector of (dimension,
// tag) pairs:
std::vector<std::pair<int, int> > entities;
// 獲取實例。在Gmsh中點、邊、面、體都稱之爲實例。
gmsh::model::getEntities(entities);
for(std::size_t i = 0; i < entities.size(); i++) {
// Mesh data is made of `elements' (points, lines, triangles, ...), defined
// by an ordered list of their `nodes'. Elements and nodes are identified by
// `tags' as well (strictly positive identification numbers), and are stored
// ("classified") in the model entity they discretize. Tags for elements and
// nodes are globally unique (and not only per dimension, like entities).
// A model entity of dimension 0 (a geometrical point) will contain a mesh
// element of type point, as well as a mesh node. A model curve will contain
// line elements as well as its interior nodes, while its boundary nodes
// will be stored in the bounding model points. A model surface will contain
// triangular and/or quadrangular elements and all the nodes not classified
// on its boundary or on its embedded entities. A model volume will contain
// tetrahedra, hexahedra, etc. and all the nodes not classified on its
// boundary or on its embedded entities.
// Dimension and tag of the entity:
int dim = entities[i].first, tag = entities[i].second;
// Get the mesh nodes for the entity (dim, tag):
std::vector<std::size_t> nodeTags;
std::vector<double> nodeCoords, nodeParams;
gmsh::model::mesh::getNodes(nodeTags, nodeCoords, nodeParams, dim, tag);
// Get the mesh elements for the entity (dim, tag):
std::vector<int> elemTypes;
std::vector<std::vector<std::size_t> > elemTags, elemNodeTags;
gmsh::model::mesh::getElements(elemTypes, elemTags, elemNodeTags, dim, tag);
// Let's print a summary of the information available on the entity and its
// mesh.
std::cout<<"******************************************************" <<std::endl;
// * Type of the entity:
std::string type;
gmsh::model::getType(dim, tag, type);
std::string name;
gmsh::model::getEntityName(dim, tag, name);
if(name.size()) name += " ";
std::cout << "Entity " << name << "(" << dim << "," << tag << ") of type "
<< type << "\n";
// * Number of mesh nodes and elements:
int numElem = 0;
for(std::size_t j = 0; j < elemTags.size(); j++)
numElem += elemTags[j].size();
std::cout << " - Mesh has " << nodeTags.size() << " nodes and " << numElem
<< " elements\n";
std::cout<<"-----------------------------------------------------" <<std::endl;
// * Entities on its boundary:
std::vector<std::pair<int, int> > boundary;
gmsh::model::getBoundary({{dim, tag}}, boundary);
if(boundary.size()) {
std::cout << " - Boundary entities: ";
for(std::size_t j = 0; j < boundary.size(); j++)
std::cout << "(" << boundary[j].first << "," << boundary[j].second
<< ") ";
std::cout << "\n";
}
std::cout<<"-----------------------------------------------------" <<std::endl;
// * Does the entity belong to physical groups?
std::vector<int> physicalTags;
gmsh::model::getPhysicalGroupsForEntity(dim, tag, physicalTags);
if(physicalTags.size()) {
std::cout << " - Physical group: ";
for(std::size_t j = 0; j < physicalTags.size(); j++) {
std::string n;
gmsh::model::getPhysicalName(dim, physicalTags[j], n);
if(n.size()) n += " ";
std::cout << n << "(" << dim << ", " << physicalTags[j] << ") ";
}
std::cout << "\n";
}
}
// 將內存中的網格寫入文件
gmsh::write("yz.msh");
// We can use this to clear all the model data:
gmsh::clear();
// 使用完畢後,調用終止函數
gmsh::finalize();
return 0;
}
總結
Gmsh API的使用方式還是比較簡單的,通過查看官方的示例文件基本可以瞭解常用的函數。
全局的函數,如open、merge、write
等一般直接在一級命名空間下,與幾何相關的函數往往在model
命名空間下,與網格劃分相關的函數通常在mesh
命名空間下。
如果後續涉及的是對網格數據結構的操作的話,需要明白Gmsh的網格是存儲在對應的各個實體中的。舉個例子,一個立方體劃分完三維網格(其實會首先調用一維和二維網格劃分),一維的線單元會保存在單元所在的邊的對象內,二維單元保存在所在的面對象內,三維單元保存在體對象內。