File: MeshVolume.cpp

package info (click to toggle)
gmsh 4.8.4%2Bds2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 87,812 kB
  • sloc: cpp: 378,014; ansic: 99,669; yacc: 7,216; python: 6,680; java: 3,486; lisp: 659; lex: 621; perl: 571; makefile: 470; sh: 440; xml: 415; javascript: 113; pascal: 35; modula3: 32
file content (83 lines) | stat: -rw-r--r-- 2,590 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Gmsh - Copyright (C) 1997-2021 C. Geuzaine, J.-F. Remacle
//
// See the LICENSE.txt file for license information. Please report all
// issues on https://gitlab.onelab.info/gmsh/gmsh/issues.

#include "MeshVolume.h"
#include "GModel.h"
#include "PViewOptions.h"

StringXNumber MeshVolumeOptions_Number[] = {
  {GMSH_FULLRC, "PhysicalGroup", nullptr, -1},
  {GMSH_FULLRC, "Dimension", nullptr, 3}};

extern "C" {
GMSH_Plugin *GMSH_RegisterMeshVolumePlugin()
{
  return new GMSH_MeshVolumePlugin();
}
}

std::string GMSH_MeshVolumePlugin::getHelp() const
{
  return "Plugin(MeshVolume) computes the volume of the mesh.\n\n"
         "Only the elements in the physical group `PhysicalGroup' "
         "of dimension `Dimension' are taken into account, unless "
         "'PhysicalGroup' is negative, in which case all the elements of "
         "the given `Dimension' are considered. If `Dimension` is "
         "negative, all the elments are considered.\n\n"
         "Plugin(MeshVolume) creates one new list-based view.";
}

int GMSH_MeshVolumePlugin::getNbOptions() const
{
  return sizeof(MeshVolumeOptions_Number) / sizeof(StringXNumber);
}

StringXNumber *GMSH_MeshVolumePlugin::getOption(int iopt)
{
  return &MeshVolumeOptions_Number[iopt];
}

PView *GMSH_MeshVolumePlugin::execute(PView *v)
{
  int physical = (int)MeshVolumeOptions_Number[0].def;
  int dim = (int)MeshVolumeOptions_Number[1].def;

  GModel *model = GModel::current();
  if(!model->getNumMeshVertices()) {
    Msg::Error("Plugin(MeshVolume) needs a mesh");
    return v;
  }

  std::vector<GEntity *> entities;
  if(physical == -1) { model->getEntities(entities, dim); }
  else {
    std::map<int, std::vector<GEntity *> > groups;
    model->getPhysicalGroups(dim, groups);
    entities = groups[physical];
  }

  if(entities.empty()) Msg::Warning("The specified domain is empty");

  double vol = 0;
  for(std::size_t i = 0; i < entities.size(); i++)
    for(std::size_t j = 0; j < entities[i]->getNumMeshElements(); j++)
      vol += entities[i]->getMeshElement(j)->getVolume();

  Msg::Info("Mesh volume (physical %d | dimension %d): %g", physical, dim, vol);

  PView *v2 = new PView();
  PViewDataList *data2 = getDataList(v2);
  SBoundingBox3d bbox = model->bounds();
  data2->SP.push_back(bbox.center().x());
  data2->SP.push_back(bbox.center().y());
  data2->SP.push_back(bbox.center().z());
  data2->SP.push_back(vol);
  data2->NbSP = 1;
  v2->getOptions()->intervalsType = PViewOptions::Numeric;
  data2->setName("MeshVolume");
  data2->setFileName("MeshVolume.pos");
  data2->finalize();
  return v2;
}