OGS
queryMesh.cpp
Go to the documentation of this file.
1 
10 #include <tclap/CmdLine.h>
11 
12 #include <array>
13 #include <memory>
14 #include <sstream>
15 #include <string>
16 
17 #include "BaseLib/FileTools.h"
18 #include "BaseLib/StringTools.h"
19 #include "InfoLib/GitInfo.h"
22 #include "MeshLib/Mesh.h"
23 #include "MeshLib/Node.h"
24 
25 int main(int argc, char* argv[])
26 {
27  TCLAP::CmdLine cmd(
28  "Query mesh information.\n\n"
29  "OpenGeoSys-6 software, version " +
31  ".\n"
32  "Copyright (c) 2012-2021, OpenGeoSys Community "
33  "(http://www.opengeosys.org)",
35  TCLAP::UnlabeledValueArg<std::string> mesh_arg(
36  "mesh-file", "input mesh file", true, "", "string");
37  cmd.add(mesh_arg);
38  TCLAP::MultiArg<std::size_t> eleId_arg("e", "element-id", "element ID",
39  false, "number");
40  cmd.add(eleId_arg);
41  TCLAP::MultiArg<std::size_t> nodeId_arg("n", "node-id", "node ID", false,
42  "number");
43  cmd.add(nodeId_arg);
44  TCLAP::SwitchArg showNodeWithMaxEle_arg(
45  "", "show-node-with-max-elements",
46  "show a node having the max number of connected elements", false);
47  cmd.add(showNodeWithMaxEle_arg);
48 
49  cmd.parse(argc, argv);
50 
51  const std::string filename(mesh_arg.getValue());
52 
53  // read the mesh file
54  auto const mesh =
55  std::unique_ptr<MeshLib::Mesh>(MeshLib::IO::readMeshFromFile(filename));
56  if (!mesh)
57  {
58  return EXIT_FAILURE;
59  }
60 
61  std::vector<std::size_t> selected_node_ids;
62  if (showNodeWithMaxEle_arg.getValue())
63  {
64  auto itr = std::max_element(
65  mesh->getNodes().begin(), mesh->getNodes().end(),
66  [&mesh](MeshLib::Node* i, MeshLib::Node* j)
67  {
68  return mesh->getElementsConnectedToNode(*i).size() <
69  mesh->getElementsConnectedToNode(*j).size();
70  });
71  if (itr != mesh->getNodes().end())
72  {
73  MeshLib::Node* node = *itr;
74  selected_node_ids.push_back(node->getID());
75  }
76  }
77  selected_node_ids.insert(selected_node_ids.end(),
78  nodeId_arg.getValue().begin(),
79  nodeId_arg.getValue().end());
80 
81  auto const materialIds = materialIDs(*mesh);
82  for (auto ele_id : eleId_arg.getValue())
83  {
84  std::stringstream out;
85  out << std::scientific
86  << std::setprecision(std::numeric_limits<double>::digits10);
87  out << "--------------------------------------------------------"
88  << std::endl;
89  auto* ele = mesh->getElement(ele_id);
90  out << "# Element " << ele->getID() << std::endl;
91  out << "Type : " << CellType2String(ele->getCellType()) << std::endl;
92  if (materialIds)
93  {
94  out << "Mat ID : " << (*materialIds)[ele_id] << std::endl;
95  }
96  out << "Nodes: " << std::endl;
97  for (unsigned i = 0; i < ele->getNumberOfNodes(); i++)
98  {
99  out << ele->getNode(i)->getID() << " " << *ele->getNode(i)
100  << std::endl;
101  }
102  out << "Content: " << ele->getContent() << std::endl;
103  out << "Neighbors: ";
104  for (unsigned i = 0; i < ele->getNumberOfNeighbors(); i++)
105  {
106  if (ele->getNeighbor(i))
107  {
108  out << ele->getNeighbor(i)->getID() << " ";
109  }
110  else
111  {
112  out << "none ";
113  }
114  }
115  out << std::endl;
116  INFO("{:s}", out.str());
117  }
118 
119  auto const& connections = MeshLib::calculateNodesConnectedByElements(*mesh);
120  for (auto node_id : selected_node_ids)
121  {
122  std::stringstream out;
123  out << std::scientific
124  << std::setprecision(std::numeric_limits<double>::digits10);
125  out << "--------------------------------------------------------"
126  << std::endl;
127  MeshLib::Node const* node = mesh->getNode(node_id);
128  out << "# Node " << node->getID() << std::endl;
129  out << "Coordinates: " << *node << std::endl;
130  out << "Connected elements ("
131  << mesh->getElementsConnectedToNode(*node).size() << "): ";
132  for (auto ele : mesh->getElementsConnectedToNode(*node))
133  {
134  out << ele->getID() << " ";
135  }
136  out << std::endl;
137  out << "Connected nodes (" << connections[node->getID()].size()
138  << "): ";
139  for (auto nd : connections[node->getID()])
140  {
141  out << nd->getID() << " ";
142  }
143  out << std::endl;
144  INFO("{:s}", out.str());
145  }
146 }
Definition of the Element class.
Filename manipulation routines.
Git information.
void INFO(char const *fmt, Args const &... args)
Definition: Logging.h:32
Definition of the Mesh class.
Definition of the Node class.
Definition of string helper functions.
std::size_t getID() const
Definition: Point3dWithID.h:62
GITINFOLIB_EXPORT const std::string ogs_version
MeshLib::Mesh * readMeshFromFile(const std::string &file_name)
std::vector< std::vector< Node * > > calculateNodesConnectedByElements(Mesh const &mesh)
Definition: Mesh.cpp:331
PropertyVector< int > const * materialIDs(Mesh const &mesh)
Definition: Mesh.cpp:258
std::string CellType2String(const CellType t)
Given a MeshElemType this returns the appropriate string.
Definition: MeshEnums.cpp:157
int main(int argc, char *argv[])
Definition: queryMesh.cpp:25
Definition of readMeshFromFile function.