gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
HomologyComputation.cpp
Go to the documentation of this file.
1 // Gmsh - Copyright (C) 1997-2022 C. Geuzaine, J.-F. Remacle
2 //
3 // See the LICENSE.txt file in the Gmsh root directory for license information.
4 // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues.
5 //
6 // Contributed by Matti Pellikka <matti.pellikka@microsoft.com>.
7 
8 #include <stdlib.h>
9 #include <string>
10 #include <iostream>
11 #include <sstream>
12 #include "GmshGlobal.h"
13 #include "GmshConfig.h"
14 #include "GModel.h"
15 #include "Homology.h"
16 #include "HomologyComputation.h"
17 
18 #if defined(HAVE_KBIPACK)
19 
20 StringXNumber HomologyComputationOptions_Number[] = {
21  {GMSH_FULLRC, "ComputeHomology", nullptr, 1.},
22  {GMSH_FULLRC, "ComputeCohomology", nullptr, 0.},
23  {GMSH_FULLRC, "HomologyPhysicalGroupsBegin", nullptr, -1.},
24  {GMSH_FULLRC, "CohomologyPhysicalGroupsBegin", nullptr, -1.},
25  {GMSH_FULLRC, "CreatePostProcessingViews", nullptr, 1.},
26  {GMSH_FULLRC, "ReductionOmit", nullptr, 1.},
27  {GMSH_FULLRC, "ReductionCombine", nullptr, 3.},
28  {GMSH_FULLRC, "PostProcessSimplify", nullptr, 1.},
29  {GMSH_FULLRC, "ReductionHeuristic", nullptr, 1.}};
30 
31 StringXString HomologyComputationOptions_String[] = {
32  {GMSH_FULLRC, "DomainPhysicalGroups", nullptr, ""},
33  {GMSH_FULLRC, "SubdomainPhysicalGroups", nullptr, ""},
34  {GMSH_FULLRC, "ReductionImmunePhysicalGroups", nullptr, ""},
35  {GMSH_FULLRC, "DimensionOfChainsToSave", nullptr, "0, 1, 2, 3"},
36  {GMSH_FULLRC, "Filename", nullptr, "homology.msh"}};
37 
38 extern "C" {
39 GMSH_Plugin *GMSH_RegisterHomologyComputationPlugin()
40 {
41  return new GMSH_HomologyComputationPlugin();
42 }
43 }
44 
45 std::string GMSH_HomologyComputationPlugin::getHelp() const
46 {
47  return "Plugin(HomologyComputation) computes representative chains "
48  "of basis elements of (relative) homology and cohomology spaces.\n\n"
49 
50  "Define physical groups in order to specify the computation "
51  "domain and the relative subdomain. Otherwise the whole mesh "
52  "is the domain and the relative subdomain is empty. \n\n"
53 
54  "Plugin(HomologyComputation) creates new views, one for each "
55  "basis element. The resulting basis chains of desired dimension "
56  "together with the mesh are saved to the given file.";
57 }
58 
59 int GMSH_HomologyComputationPlugin::getNbOptions() const
60 {
61  return sizeof(HomologyComputationOptions_Number) / sizeof(StringXNumber);
62 }
63 
64 StringXNumber *GMSH_HomologyComputationPlugin::getOption(int iopt)
65 {
66  return &HomologyComputationOptions_Number[iopt];
67 }
68 
69 int GMSH_HomologyComputationPlugin::getNbOptionsStr() const
70 {
71  return sizeof(HomologyComputationOptions_String) / sizeof(StringXString);
72 }
73 
74 StringXString *GMSH_HomologyComputationPlugin::getOptionStr(int iopt)
75 {
76  return &HomologyComputationOptions_String[iopt];
77 }
78 
79 bool GMSH_HomologyComputationPlugin::parseStringOpt(int stringOpt,
80  std::vector<int> &intList)
81 {
82  std::string list = HomologyComputationOptions_String[stringOpt].def;
83  intList.clear();
84 
85  int n;
86  char a;
87  std::istringstream ss(list);
88  while(ss >> n) {
89  intList.push_back(n);
90  if(ss >> a) {
91  if(a != ',') {
92  Msg::Error("Unexpected character \'%c\' while parsing \'%s\'", a,
93  HomologyComputationOptions_String[stringOpt].str);
94  return false;
95  }
96  }
97  }
98  return true;
99 }
100 
101 PView *GMSH_HomologyComputationPlugin::execute(PView *v)
102 {
103  std::string fileName = HomologyComputationOptions_String[4].def;
104  int hom = (int)HomologyComputationOptions_Number[0].def;
105  int coh = (int)HomologyComputationOptions_Number[1].def;
106  int hompg = (int)HomologyComputationOptions_Number[2].def;
107  int cohpg = (int)HomologyComputationOptions_Number[3].def;
108  bool pviews = (bool)HomologyComputationOptions_Number[4].def;
109  bool omit = (bool)HomologyComputationOptions_Number[5].def;
110  int combine = (int)HomologyComputationOptions_Number[6].def;
111  bool smoothen = (bool)HomologyComputationOptions_Number[7].def;
112  int heuristic = (int)HomologyComputationOptions_Number[8].def;
113 
114  std::vector<int> domain;
115  std::vector<int> subdomain;
116  std::vector<int> imdomain;
117  std::vector<int> dimsave;
118  if(!parseStringOpt(0, domain)) return nullptr;
119  if(!parseStringOpt(1, subdomain)) return nullptr;
120  if(!parseStringOpt(2, imdomain)) return nullptr;
121  if(!parseStringOpt(3, dimsave)) return nullptr;
122 
123  GModel *m = GModel::current();
124 
125  Homology *homology = new Homology(m, domain, subdomain, imdomain, true,
126  combine, omit, smoothen, heuristic);
127  homology->setFileName(fileName);
128 
129  if(hom != 0) homology->findHomologyBasis(dimsave);
130  if(coh != 0) homology->findCohomologyBasis(dimsave);
131 
132  for(std::size_t i = 0; i < dimsave.size(); i++) {
133  int dim = dimsave.at(i);
134  if(dim > -1 && dim < 4 && hom != 0) {
135  homology->addChainsToModel(dim, pviews, hompg);
136  if(hompg != -1) hompg += homology->betti(dim);
137  }
138  }
139  for(std::size_t i = 0; i < dimsave.size(); i++) {
140  int dim = dimsave.at(i);
141  if(dim > -1 && dim < 4 && coh != 0) {
142  homology->addCochainsToModel(dim, pviews, cohpg);
143  if(cohpg != -1) cohpg += homology->betti(dim);
144  }
145  }
146 
147  delete homology;
148 
149  return nullptr;
150 }
151 
152 #endif
StringXString
Definition: Options.h:910
PView
Definition: PView.h:27
GMSH_Plugin
Definition: Plugin.h:26
Msg::Error
static void Error(const char *fmt,...)
Definition: GmshMessage.cpp:482
StringXNumber
Definition: Options.h:918
HomologyComputation.h
GMSH_FULLRC
#define GMSH_FULLRC
Definition: Options.h:20
Homology.h
GModel
Definition: GModel.h:44
StringXString::def
std::string def
Definition: Options.h:914
GmshGlobal.h
GModel.h
GModel::current
static GModel * current(int index=-1)
Definition: GModel.cpp:136