forked from gjkennedy/neohookean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmesh.h
163 lines (137 loc) · 4.79 KB
/
mesh.h
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <algorithm>
#include <fstream>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
// Data Structures
struct Node {
int index;
double x, y, z;
};
struct Element {
int index;
std::vector<int> nodeIndices;
};
struct NodeSet {
std::string name;
std::vector<int> nodeIndices;
};
// Utility Functions
std::string trim(const std::string &str) {
size_t first = str.find_first_not_of(' ');
if (std::string::npos == first) {
return str;
}
size_t last = str.find_last_not_of(' ');
return str.substr(first, (last - first + 1));
}
// Main Parsing Logic
template <typename T>
void load_mesh(std::string filename, int *num_elements, int *num_nodes,
int **element_nodes, T **xloc) {
std::ifstream meshFile(filename);
if (!meshFile.is_open()) {
std::cerr << "Failed to open file: " << filename << std::endl;
}
std::string line;
std::vector<Node> nodes;
std::map<int, Element> elements;
std::map<std::string, NodeSet> nodeSets;
bool inNodesSection = false, inElementsSection = false,
inNodeSetsSection = false;
std::string currentSetName;
while (getline(meshFile, line)) {
line = trim(line);
if (line.empty() || line[0] == '*') {
inNodesSection = line.find("*Node") != std::string::npos;
inElementsSection = line.find("*Element") != std::string::npos;
inNodeSetsSection = line.find("*Nset") != std::string::npos;
if (inNodeSetsSection) {
size_t namePos = line.find("Nset=");
if (namePos != std::string::npos) {
currentSetName = line.substr(namePos + 5);
nodeSets[currentSetName] = NodeSet{currentSetName};
}
}
continue;
}
if (inNodesSection) {
std::istringstream iss(line);
std::string indexStr;
std::getline(iss, indexStr,
','); // Read up to the first comma to get the node index.
int nodeIndex = std::stoi(indexStr); // Convert index string to int.
Node node;
node.index = nodeIndex;
std::string coordinateStr;
std::getline(iss, coordinateStr,
','); // Read up to the next comma for the x coordinate.
node.x = std::stod(coordinateStr); // Convert to double.
std::getline(iss, coordinateStr,
','); // Read up to the next comma for the y coordinate.
node.y = std::stod(coordinateStr); // Convert to double.
std::getline(iss,
coordinateStr); // Read the rest of the line for the z
// coordinate (assuming no more commas).
node.z = std::stod(coordinateStr); // Convert to double.
nodes.push_back(node);
} else if (inElementsSection) {
std::istringstream iss(line);
Element element;
if (!(iss >> element.index)) { // Read and check the element's index.
std::cerr << "Failed to read element index from line: " << line
<< std::endl;
continue; // Skip to the next line if the element index can't be read.
}
// Read the rest of the line as a single string.
std::string restOfLine;
std::getline(iss, restOfLine);
// Use another stringstream to parse the node indices from restOfLine.
std::istringstream nodeStream(restOfLine);
std::string
nodeIndexStr; // Use a string to temporarily hold each node index.
while (std::getline(nodeStream, nodeIndexStr,
',')) { // Read up to the next comma.
if (!nodeIndexStr.empty()) { // Check if the string is not empty.
std::istringstream indexStream(
nodeIndexStr); // Use another stringstream to convert string to
// int.
int nodeIndex;
if (indexStream >> nodeIndex) { // Convert the string to an int.
element.nodeIndices.push_back(nodeIndex);
}
}
}
elements[element.index] = element;
} else if (inNodeSetsSection && !currentSetName.empty()) {
std::istringstream iss(line);
int nodeIndex;
while (iss >> nodeIndex) {
nodeSets[currentSetName].nodeIndices.push_back(nodeIndex);
}
}
}
meshFile.close();
// Convert elements and nodeSets to flat structures
int num_elems = elements.size();
int *elem_nodes = new int[10 * num_elems];
for (const auto &elem : elements) {
for (int j = 0; j < 10; j++) {
elem_nodes[10 * (elem.second.index - 1) + j] =
elem.second.nodeIndices[j] - 1;
}
}
int num_ns = nodes.size();
T *x = new T[3 * num_ns];
for (const auto &node : nodes) {
x[3 * (node.index - 1)] = node.x;
x[3 * (node.index - 1) + 1] = node.y;
x[3 * (node.index - 1) + 2] = node.z;
}
*num_elements = num_elems;
*num_nodes = num_ns;
*element_nodes = elem_nodes;
*xloc = x;
}