NASedgeFormat.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2012 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 
33 Foam::fileFormats::NASedgeFormat::NASedgeFormat
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  (
56  "fileFormats::NASedgeFormat::read(const fileName&)"
57  )
58  << "Cannot read file " << filename
59  << exit(FatalError);
60  }
61 
62  DynamicList<point> dynPoints;
63  DynamicList<edge> dynEdges;
64  DynamicList<label> pointId; // Nastran index of points
65 
66  while (is.good())
67  {
68  string line;
69  is.getLine(line);
70 
71  // Skip empty or comment
72  if (line.empty() || line[0] == '$')
73  {
74  continue;
75  }
76 
77  // Check if character 72 is continuation
78  if (line.size() > 72 && line[72] == '+')
79  {
80  line = line.substr(0, 72);
81 
82  while (true)
83  {
84  string buf;
85  is.getLine(buf);
86 
87  if (buf.size() > 72 && buf[72] == '+')
88  {
89  line += buf.substr(8, 64);
90  }
91  else
92  {
93  line += buf.substr(8, buf.size()-8);
94  break;
95  }
96  }
97  }
98 
99 
100  // Read first word
101  IStringStream lineStream(line);
102  word cmd;
103  lineStream >> cmd;
104 
105  if (cmd == "CBEAM" || cmd == "CROD")
106  {
107  edge e;
108 
109  // label groupId = readLabel(IStringStream(line.substr(16,8))());
110  e[0] = readLabel(IStringStream(line.substr(24,8))());
111  e[1] = readLabel(IStringStream(line.substr(32,8))());
112 
113  // discard groupID
114  dynEdges.append(e);
115  }
116  else if (cmd == "PLOTEL")
117  {
118  edge e;
119 
120  // label groupId = readLabel(IStringStream(line.substr(16,8))());
121  e[0] = readLabel(IStringStream(line.substr(16,8))());
122  e[1] = readLabel(IStringStream(line.substr(24,8))());
123 
124  // discard groupID
125  dynEdges.append(e);
126  }
127  else if (cmd == "GRID")
128  {
129  label index = readLabel(IStringStream(line.substr(8,8))());
130  scalar x = parseNASCoord(line.substr(24, 8));
131  scalar y = parseNASCoord(line.substr(32, 8));
132  scalar z = parseNASCoord(line.substr(40, 8));
133 
134  pointId.append(index);
135  dynPoints.append(point(x, y, z));
136  }
137  else if (cmd == "GRID*")
138  {
139  // Long format is on two lines with '*' continuation symbol
140  // on start of second line.
141  // Typical line (spaces compacted)
142  // GRID* 126 0 -5.55999875E+02 -5.68730474E+02
143  // * 2.14897901E+02
144 
145  label index = readLabel(IStringStream(line.substr(8,16))());
146  scalar x = parseNASCoord(line.substr(40, 16));
147  scalar y = parseNASCoord(line.substr(56, 16));
148 
149  is.getLine(line);
150  if (line[0] != '*')
151  {
153  (
154  "fileFormats::NASedgeFormat::read(const fileName&)"
155  )
156  << "Expected continuation symbol '*' when reading GRID*"
157  << " (double precision coordinate) format" << nl
158  << "Read:" << line << nl
159  << "File:" << is.name() << " line:" << is.lineNumber()
160  << exit(FatalError);
161  }
162  scalar z = parseNASCoord(line.substr(8, 16));
163 
164  pointId.append(index);
165  dynPoints.append(point(x, y, z));
166  }
167  }
168 
169  // transfer to normal lists
170  storedPoints().transfer(dynPoints);
171 
172  pointId.shrink();
173  dynEdges.shrink();
174 
175  // Build inverse mapping (NASTRAN pointId -> index)
176  Map<label> mapPointId(2*pointId.size());
177  forAll(pointId, i)
178  {
179  mapPointId.insert(pointId[i], i);
180  }
181 
182  // note which points were really used and which can be culled
183  PackedBoolList usedPoints(points().size());
184 
185 
186  // Pass1: relabel edges
187  // ~~~~~~~~~~~~~~~~~~~~
188  forAll(dynEdges, i)
189  {
190  edge& e = dynEdges[i];
191  e[0] = mapPointId[e[0]];
192  e[1] = mapPointId[e[1]];
193 
194  usedPoints.set(e[0]);
195  usedPoints.set(e[1]);
196  }
197  pointId.clearStorage();
198  mapPointId.clear();
199 
200  // not all the points were used, cull them accordingly
201  if (unsigned(points().size()) != usedPoints.count())
202  {
203  label nUsed = 0;
204 
205  pointField& pts = storedPoints();
206  forAll(pts, pointI)
207  {
208  if (usedPoints.get(pointI))
209  {
210  if (nUsed != pointI)
211  {
212  pts[nUsed] = pts[pointI];
213  }
214 
215  // map prev -> new id
216  mapPointId[pointI] = nUsed;
217 
218  ++nUsed;
219  }
220  }
221 
222  pts.setSize(nUsed);
223 
224  // renumber edge vertices
225  forAll(dynEdges, edgeI)
226  {
227  edge& e = dynEdges[edgeI];
228 
229  e[0] = mapPointId[e[0]];
230  e[1] = mapPointId[e[1]];
231  }
232  }
233 
234 
235  // transfer to normal lists
236  storedEdges().transfer(dynEdges);
237 
238  return true;
239 }
240 
241 
242 // ************************************************************************* //
Input from memory buffer stream.
Definition: IStringStream.H:49
const pointField & points
const fileName & name() const
Return the name of the stream.
Definition: IFstream.H:116
vector point
Point is a vector.
Definition: point.H:41
void set(const PackedList< 1 > &)
Set specified bits.
A bit-packed bool list.
A class for handling words, derived from string.
Definition: word.H:59
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
Definition: DynamicListI.H:310
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
unsigned int count() const
Count number of bits set, O(log(n))
Definition: PackedList.C:55
Input from file stream.
Definition: IFstream.H:81
An edge is a list of two point labels. The functionality it provides supports the discretisation on a...
Definition: edge.H:58
label readLabel(Istream &is)
Definition: label.H:64
A line primitive.
Definition: line.H:56
static scalar parseNASCoord(const string &s)
Definition: readNAS.C:50
static const char nl
Definition: Ostream.H:260
void setSize(const label)
Reset size of List.
Definition: List.C:318
const dimensionedScalar e
Elementary charge.
Definition: doubleFloat.H:78
virtual bool read(const fileName &)
Read from a file.
Definition: NASedgeFormat.C:45
#define forAll(list, i)
Definition: UList.H:421
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
DynamicList< T, SizeInc, SizeMult, SizeDiv > & shrink()
Shrink the allocated space to the number of elements used.
Definition: DynamicListI.H:258
label lineNumber() const
Return current stream line number.
Definition: IOstream.H:438
ISstream & getLine(string &)
Raw, low-level getline into a string function.
Definition: ISstreamI.H:77
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:56
error FatalError
scalar y
A class for handling file names.
Definition: fileName.H:69
bool read(const char *, int32_t &)
Definition: int32IO.C:87
void clearStorage()
Clear the list and delete storage.
Definition: DynamicListI.H:249
UEqn clear()
void transfer(const FixedList< T, Size > &)
Copy (not transfer) the argument contents.
Definition: FixedListI.H:185
unsigned int get(const label) const
Get value at index I.
Definition: PackedListI.H:972