【网格生成】Gmsh快速入门教程 --3.Gmsh API

在前面两篇文章12中我们分别介绍了图形化界面和内置解析器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的网格是存储在对应的各个实体中的。举个例子,一个立方体划分完三维网格(其实会首先调用一维和二维网格划分),一维的线单元会保存在单元所在的边的对象内,二维单元保存在所在的面对象内,三维单元保存在体对象内。

  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
【优质项目推荐】 1、项目代码均经过严格本地测试,运行OK,确保功能稳定后才上传平台。可放心下载并立即投入使用,若遇到任何使用问题,随时欢迎私信反馈与沟通,博主会第一时间回复。 2、项目适用于计算机相关专业(如计科、信息安全、数据科学、人工智能、通信、物联网、自动化、电子信息等)的在校学生、专业教师,或企业员工,小白入门等都适用。 3、该项目不仅具有很高的学习借鉴价值,对于初学者来说,也是入门进阶的绝佳选择;当然也可以直接用于 毕设、课设、期末大作业或项目初期立项演示等。 3、开放创新:如果您有一定基础,且热爱探索钻研,可以在此代码基础上二次开发,进行修改、扩展,创造出属于自己的独特应用。 欢迎下载使用优质资源!欢迎借鉴使用,并欢迎学习交流,共同探索编程的无穷魅力! 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值