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