NASedgeFormat.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "NASedgeFormat.H"
27 #include "IFstream.H"
28 #include "IStringStream.H"
29 #include "PackedBoolList.H"
30 
31 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
32 
34 (
35  const fileName& filename
36 )
37 {
38  read(filename);
39 }
40 
41 
42 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
43 
45 (
46  const fileName& filename
47 )
48 {
49  clear();
50 
51  IFstream is(filename);
52  if (!is.good())
53  {
55  << "Cannot read file " << filename
56  << exit(FatalError);
57  }
58 
59  DynamicList<point> dynPoints;
60  DynamicList<edge> dynEdges;
61  DynamicList<label> pointId; // Nastran index of points
62 
63  while (is.good())
64  {
65  string line;
66  is.getLine(line);
67 
68  // Skip empty or comment
69  if (line.empty() || line[0] == '$')
70  {
71  continue;
72  }
73 
74  // Check if character 72 is continuation
75  if (line.size() > 72 && line[72] == '+')
76  {
77  line = line.substr(0, 72);
78 
79  while (true)
80  {
81  string buf;
82  is.getLine(buf);
83 
84  if (buf.size() > 72 && buf[72] == '+')
85  {
86  line += buf.substr(8, 64);
87  }
88  else
89  {
90  line += buf.substr(8, buf.size()-8);
91  break;
92  }
93  }
94  }
95 
96 
97  // Read first word
98  IStringStream lineStream(line);
99  word cmd;
100  lineStream >> cmd;
101 
102  if (cmd == "CBEAM" || cmd == "CROD")
103  {
104  edge e;
105 
106  // label groupId = readLabel(IStringStream(line.substr(16,8))());
107  e[0] = readLabel(IStringStream(line.substr(24,8))());
108  e[1] = readLabel(IStringStream(line.substr(32,8))());
109 
110  // discard groupID
111  dynEdges.append(e);
112  }
113  else if (cmd == "PLOTEL")
114  {
115  edge e;
116 
117  // label groupId = readLabel(IStringStream(line.substr(16,8))());
118  e[0] = readLabel(IStringStream(line.substr(16,8))());
119  e[1] = readLabel(IStringStream(line.substr(24,8))());
120 
121  // discard groupID
122  dynEdges.append(e);
123  }
124  else if (cmd == "GRID")
125  {
126  label index = readLabel(IStringStream(line.substr(8,8))());
127  scalar x = parseNASCoord(line.substr(24, 8));
128  scalar y = parseNASCoord(line.substr(32, 8));
129  scalar z = parseNASCoord(line.substr(40, 8));
130 
131  pointId.append(index);
132  dynPoints.append(point(x, y, z));
133  }
134  else if (cmd == "GRID*")
135  {
136  // Long format is on two lines with '*' continuation symbol
137  // on start of second line.
138  // Typical line (spaces compacted)
139  // GRID* 126 0 -5.55999875E+02 -5.68730474E+02
140  // * 2.14897901E+02
141 
142  label index = readLabel(IStringStream(line.substr(8,16))());
143  scalar x = parseNASCoord(line.substr(40, 16));
144  scalar y = parseNASCoord(line.substr(56, 16));
145 
146  is.getLine(line);
147  if (line[0] != '*')
148  {
150  << "Expected continuation symbol '*' when reading GRID*"
151  << " (double precision coordinate) format" << nl
152  << "Read:" << line << nl
153  << "File:" << is.name() << " line:" << is.lineNumber()
154  << exit(FatalError);
155  }
156  scalar z = parseNASCoord(line.substr(8, 16));
157 
158  pointId.append(index);
159  dynPoints.append(point(x, y, z));
160  }
161  }
162 
163  // transfer to normal lists
164  storedPoints().transfer(dynPoints);
165 
166  pointId.shrink();
167  dynEdges.shrink();
168 
169  // Build inverse mapping (NASTRAN pointId -> index)
170  Map<label> mapPointId(2*pointId.size());
171  forAll(pointId, i)
172  {
173  mapPointId.insert(pointId[i], i);
174  }
175 
176  // note which points were really used and which can be culled
177  PackedBoolList usedPoints(points().size());
178 
179 
180  // Pass1: relabel edges
181  // ~~~~~~~~~~~~~~~~~~~~
182  forAll(dynEdges, i)
183  {
184  edge& e = dynEdges[i];
185  e[0] = mapPointId[e[0]];
186  e[1] = mapPointId[e[1]];
187 
188  usedPoints.set(e[0]);
189  usedPoints.set(e[1]);
190  }
191  pointId.clearStorage();
192  mapPointId.clear();
193 
194  // not all the points were used, cull them accordingly
195  if (unsigned(points().size()) != usedPoints.count())
196  {
197  label nUsed = 0;
198 
199  pointField& pts = storedPoints();
200  forAll(pts, pointi)
201  {
202  if (usedPoints.get(pointi))
203  {
204  if (nUsed != pointi)
205  {
206  pts[nUsed] = pts[pointi];
207  }
208 
209  // map prev -> new id
210  mapPointId[pointi] = nUsed;
211 
212  ++nUsed;
213  }
214  }
215 
216  pts.setSize(nUsed);
217 
218  // renumber edge vertices
219  forAll(dynEdges, edgeI)
220  {
221  edge& e = dynEdges[edgeI];
222 
223  e[0] = mapPointId[e[0]];
224  e[1] = mapPointId[e[1]];
225  }
226  }
227 
228 
229  // transfer to normal lists
230  storedEdges().transfer(dynEdges);
231 
232  return true;
233 }
234 
235 
236 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
tUEqn clear()
A line primitive.
Definition: line.H:56
A class for handling file names.
Definition: fileName.H:79
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
unsigned int get(const label) const
Get value at index I.
Definition: PackedListI.H:954
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
label lineNumber() const
Return current stream line number.
Definition: IOstream.H:435
void read(Istream &, label &, const dictionary &)
In-place read with dictionary lookup.
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:330
scalar y
void set(const PackedList< 1 > &)
Set specified bits.
const pointField & points
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:56
An edge is a list of two point labels. The functionality it provides supports the discretisation on a...
Definition: edge.H:58
A class for handling words, derived from string.
Definition: word.H:59
const fileName & name() const
Return the name of the stream.
Definition: IFstream.H:116
void transfer(const FixedList< T, Size > &)
Copy (not transfer) the argument contents.
Definition: FixedListI.H:191
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
Definition: DynamicListI.H:296
label readLabel(Istream &is)
Definition: label.H:64
DynamicList< T, SizeInc, SizeMult, SizeDiv > & shrink()
Shrink the allocated space to the number of elements used.
Definition: DynamicListI.H:252
static const char nl
Definition: Ostream.H:260
Input from file stream.
Definition: IFstream.H:81
virtual bool read(const fileName &)
Read from a file.
Definition: NASedgeFormat.C:45
NASedgeFormat(const fileName &)
Construct from file name.
Definition: NASedgeFormat.C:34
unsigned int count() const
Count number of bits set, O(log(n))
Definition: PackedList.C:55
void setSize(const label)
Reset size of List.
Definition: List.C:281
A bit-packed bool list.
ISstream & getLine(string &, const bool continuation=true)
Read line into a string.
Definition: ISstream.C:692
vector point
Point is a vector.
Definition: point.H:41
Input from memory buffer stream.
Definition: IStringStream.H:49
void clearStorage()
Clear the list and delete storage.
Definition: DynamicListI.H:243
static scalar parseNASCoord(const string &s)
Definition: readNAS.C:50
const dimensionedScalar e
Elementary charge.
Definition: doubleScalar.H:105