PageRenderTime 67ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/Plugin/HomologyComputation.cpp

https://bitbucket.org/pefarrell/gmsh
C++ | 155 lines | 126 code | 23 blank | 6 comment | 25 complexity | 05009e519eb1b935d6e91d41b0319a63 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-3.0, BSD-3-Clause
  1. // Gmsh - Copyright (C) 1997-2018 C. Geuzaine, J.-F. Remacle
  2. //
  3. // See the LICENSE.txt file for license information. Please report all
  4. // bugs and problems to the public mailing list <gmsh@onelab.info>.
  5. //
  6. // Contributed by Matti Pellikka <matti.pellikka@microsoft.com>.
  7. #include <stdlib.h>
  8. #include <string>
  9. #include <iostream>
  10. #include <sstream>
  11. #include "GmshGlobal.h"
  12. #include "GmshConfig.h"
  13. #include "GModel.h"
  14. #include "Homology.h"
  15. #include "HomologyComputation.h"
  16. #if defined(HAVE_KBIPACK)
  17. StringXNumber HomologyComputationOptions_Number[] = {
  18. {GMSH_FULLRC, "ComputeHomology", NULL, 1.},
  19. {GMSH_FULLRC, "ComputeCohomology", NULL, 0.},
  20. {GMSH_FULLRC, "HomologyPhysicalGroupsBegin", NULL, -1.},
  21. {GMSH_FULLRC, "CohomologyPhysicalGroupsBegin", NULL, -1.},
  22. {GMSH_FULLRC, "CreatePostProcessingViews", NULL, 1.},
  23. {GMSH_FULLRC, "ReductionOmit", NULL, 1.},
  24. {GMSH_FULLRC, "ReductionCombine", NULL, 3.},
  25. {GMSH_FULLRC, "PostProcessSimplify", NULL, 1.},
  26. {GMSH_FULLRC, "ReductionHeuristic", NULL, 1.}
  27. };
  28. StringXString HomologyComputationOptions_String[] = {
  29. {GMSH_FULLRC, "DomainPhysicalGroups", NULL, ""},
  30. {GMSH_FULLRC, "SubdomainPhysicalGroups", NULL, ""},
  31. {GMSH_FULLRC, "ReductionImmunePhysicalGroups", NULL, ""},
  32. {GMSH_FULLRC, "DimensionOfChainsToSave", NULL, "0, 1, 2, 3"},
  33. {GMSH_FULLRC, "Filename", NULL, "homology.msh"}
  34. };
  35. extern "C"
  36. {
  37. GMSH_Plugin *GMSH_RegisterHomologyComputationPlugin()
  38. {
  39. return new GMSH_HomologyComputationPlugin();
  40. }
  41. }
  42. std::string GMSH_HomologyComputationPlugin::getHelp() const
  43. {
  44. return "Plugin(HomologyComputation) computes representative chains "
  45. "of basis elements of (relative) homology and cohomology spaces.\n\n"
  46. "Define physical groups in order to specify the computation "
  47. "domain and the relative subdomain. Otherwise the whole mesh "
  48. "is the domain and the relative subdomain is empty. \n\n"
  49. "Plugin(HomologyComputation) creates new views, one for each "
  50. "basis element. The resulting basis chains of desired dimension "
  51. "together with the mesh are saved to the given file.";
  52. }
  53. int GMSH_HomologyComputationPlugin::getNbOptions() const
  54. {
  55. return sizeof(HomologyComputationOptions_Number) / sizeof(StringXNumber);
  56. }
  57. StringXNumber *GMSH_HomologyComputationPlugin::getOption(int iopt)
  58. {
  59. return &HomologyComputationOptions_Number[iopt];
  60. }
  61. int GMSH_HomologyComputationPlugin::getNbOptionsStr() const
  62. {
  63. return sizeof(HomologyComputationOptions_String) / sizeof(StringXString);
  64. }
  65. StringXString *GMSH_HomologyComputationPlugin::getOptionStr(int iopt)
  66. {
  67. return &HomologyComputationOptions_String[iopt];
  68. }
  69. bool GMSH_HomologyComputationPlugin::parseStringOpt
  70. (int stringOpt, std::vector<int>& intList)
  71. {
  72. std::string list = HomologyComputationOptions_String[stringOpt].def;
  73. intList.clear();
  74. int n;
  75. char a;
  76. std::istringstream ss(list);
  77. while(ss >> n){
  78. intList.push_back(n);
  79. if(ss >> a){
  80. if(a != ',') {
  81. Msg::Error("Unexpected character \'%c\' while parsing \'%s\'", a,
  82. HomologyComputationOptions_String[stringOpt].str);
  83. return false;
  84. }
  85. }
  86. }
  87. return true;
  88. }
  89. PView *GMSH_HomologyComputationPlugin::execute(PView *v)
  90. {
  91. std::string fileName = HomologyComputationOptions_String[4].def;
  92. int hom = (int)HomologyComputationOptions_Number[0].def;
  93. int coh = (int)HomologyComputationOptions_Number[1].def;
  94. int hompg = (int)HomologyComputationOptions_Number[2].def;
  95. int cohpg = (int)HomologyComputationOptions_Number[3].def;
  96. bool pviews = (bool)HomologyComputationOptions_Number[4].def;
  97. bool omit = (bool)HomologyComputationOptions_Number[5].def;
  98. int combine = (int)HomologyComputationOptions_Number[6].def;
  99. bool smoothen = (bool)HomologyComputationOptions_Number[7].def;
  100. int heuristic = (int)HomologyComputationOptions_Number[8].def;
  101. std::vector<int> domain;
  102. std::vector<int> subdomain;
  103. std::vector<int> imdomain;
  104. std::vector<int> dimsave;
  105. if(!parseStringOpt(0, domain)) return 0;
  106. if(!parseStringOpt(1, subdomain)) return 0;
  107. if(!parseStringOpt(2, imdomain)) return 0;
  108. if(!parseStringOpt(3, dimsave)) return 0;
  109. GModel* m = GModel::current();
  110. Homology* homology = new Homology(m, domain, subdomain, imdomain,
  111. true, combine, omit, smoothen, heuristic);
  112. homology->setFileName(fileName);
  113. if(hom != 0) homology->findHomologyBasis(dimsave);
  114. if(coh != 0) homology->findCohomologyBasis(dimsave);
  115. for(unsigned int i = 0; i < dimsave.size(); i++) {
  116. int dim = dimsave.at(i);
  117. if(dim > -1 && dim < 4 && hom != 0) {
  118. homology->addChainsToModel(dim, pviews, hompg);
  119. if(hompg != -1) hompg += homology->betti(dim);
  120. }
  121. }
  122. for(unsigned int i = 0; i < dimsave.size(); i++) {
  123. int dim = dimsave.at(i);
  124. if(dim > -1 && dim < 4 && coh != 0) {
  125. homology->addCochainsToModel(dim, pviews, cohpg);
  126. if(cohpg != -1) cohpg += homology->betti(dim);
  127. }
  128. }
  129. delete homology;
  130. return 0;
  131. }
  132. #endif