Debian Codegraph @

packages/seacas/applications/nas2exo/N2ENasReader.C

raw
1 //============================================================================
2 // Name : testnas2exo.cpp
3 // Author : Ramon J. Moral (STRA LLC), John Niederhaus (Coordinator, SNL)
4 // Version :
5 // Copyright : (c) Sandia National Labs 2020, 2021, 2022, 2023
6 // Description : Testing nas2exo Library, C++ 14
7 //============================================================================
8
9 #include "N2ENasReader.h"
10 #include <iostream>
11 #include <sstream>
12
13 namespace NasModules {
14
15 N2ENasReader::N2ENasReader(std::string ifname) : inFileName(std::move(ifname))
16 {
17 // TODO Auto-generated constructor stub
18 if (!doesFileExist(this->inFileName)) {
19 std::string msg = "This file does not exist: " + this->inFileName;
20 throw std::invalid_argument(msg);
21 }
22
23 // C++ 14 version
24 // this->inStream = make_unique<std::ifstream>(this->inFileName);
25 // C++ 11 version
26 this->inStream = std::make_unique<std::ifstream>(this->inFileName);
27
28 // Let's set the buffer
29 this->inStream->rdbuf()->pubsetbuf(this->_readBuffer, sizeof(this->_readBuffer));
30
31 this->lineCount = this->lineCounter();
32 }
33
34 bool N2ENasReader::doesFileExist(const std::string &fname)
35 {
36 std::ifstream f(fname);
37 return f.good();
38 }
39
40 std::string N2ENasReader::getModelTitle() { return this->modelTitle; }
41
42 void N2ENasReader::setModelTitle(const std::string &title)
43 {
44 // This gymnastics with std::string is b/c
45 // a Nastran title is limited to 72 chars.
46 this->modelTitle = title.substr(0, 71);
47 }
48
49 unsigned N2ENasReader::lineCounter()
50 {
51
52 this->inStream->clear();
53 this->inStream->seekg(0);
54
55 char buff[512];
56 unsigned count = 0;
57
58 for (;;) {
59
60 this->inStream->getline(buff, 511);
61 if (this->inStream->eof()) {
62 break;
63 }
64 ++count;
65 }
66
67 return count;
68 }
69
70 bool N2ENasReader::processFile()
71 {
72
73 bool result = false;
74
75 // Make sure the file pointer is parked at 0
76 this->inStream->clear();
77 this->inStream->seekg(0);
78
79 char buff[512];
80 unsigned utmp1;
81 unsigned utmp2;
82
83 // Blowout the data so fresh data can be added
84 this->sections.clear();
85 this->elementList.clear();
86 this->gridList.clear();
87
88 for (;;) {
89
90 std::vector<std::string> tokens;
91
92 this->inStream->getline(buff, 511);
93
94 if (this->inStream->eof()) {
95 result = true;
96 break;
97 }
98
99 // Ignore comments
100 if (buff[0] == '$')
101 continue;
102
103 tokens = this->csvLineToTokens(buff);
104
105 unsigned card_id = 0;
106 for (int i = 1; i <= 4; i++) {
107
108 if (tokens[0] == N2EFileCues[i]) {
109 card_id = i;
110 break;
111 }
112 }
113
114 if (card_id == 0)
115 continue;
116
117 sectionType sType;
118 gridType gType;
119 elementType eType;
120 N2EPoint3D pt;
121 N2EGridPtList gLst;
122
123 switch (card_id) {
124 case 1:
125 // Grid
126 std::istringstream(tokens[1]) >> utmp1;
127 std::istringstream(tokens[3]) >> pt.x[0];
128 std::istringstream(tokens[4]) >> pt.x[1];
129 std::istringstream(tokens[5]) >> pt.x[2];
130 gType = std::make_tuple(utmp1, pt);
131 this->gridList.emplace_back(gType);
132 break;
133 case 2:
134 // CTETRA
135 std::istringstream(tokens[1]) >> utmp1;
136 std::istringstream(tokens[2]) >> utmp2;
137 std::istringstream(tokens[3]) >> gLst.v[0];
138 std::istringstream(tokens[4]) >> gLst.v[1];
139 std::istringstream(tokens[5]) >> gLst.v[2];
140 std::istringstream(tokens[6]) >> gLst.v[3];
141 gLst.v[7] = gLst.v[6] = gLst.v[5] = gLst.v[4] = 0;
142 eType = std::make_tuple(utmp1, utmp2, 4, gLst);
143 this->elementList.emplace_back(eType);
144 break;
145
146 case 3:
147 // CHEXA 8 Node only!!!!!
148 std::istringstream(tokens[1]) >> utmp1;
149 std::istringstream(tokens[2]) >> utmp2;
150 std::istringstream(tokens[3]) >> gLst.v[0];
151 std::istringstream(tokens[4]) >> gLst.v[1];
152 std::istringstream(tokens[5]) >> gLst.v[2];
153 std::istringstream(tokens[6]) >> gLst.v[3];
154 std::istringstream(tokens[7]) >> gLst.v[4];
155 std::istringstream(tokens[8]) >> gLst.v[5];
156 // Hexa 8 needs two more nodes from next line
157 this->inStream->getline(buff, 511);
158 tokens = csvLineToTokens(buff);
159 std::istringstream(tokens[0]) >> gLst.v[6];
160 std::istringstream(tokens[1]) >> gLst.v[7];
161 eType = std::make_tuple(utmp1, utmp2, 8, gLst);
162 this->elementList.emplace_back(eType);
163 break;
164 case 4:
165 // Section
166 std::istringstream(tokens[1]) >> utmp1;
167 std::istringstream(tokens[2]) >> utmp2;
168 sType = std::make_tuple(utmp1, utmp2);
169 this->sections.emplace_back(sType);
170 }
171 }
172
173 return result;
174 }
175
176 std::vector<std::string> N2ENasReader::csvLineToTokens(char buff[])
177 {
178 std::vector<std::string> toks;
179 std::stringstream sStream(buff);
180 std::string tmp;
181
182 while (getline(sStream, tmp, ',')) {
183 toks.push_back(tmp);
184 }
185
186 return toks;
187 }
188
189 } // namespace NasModules