Initial commit for version 2.0.x patch release
[OpenFOAM-2.0.x.git] / src / edgeMesh / edgeFormats / nas / NASedgeFormat.C
blob8c33076e295250ebb65688448dc3374e07862f58
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2009-2010 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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.
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.
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/>.
24 \*---------------------------------------------------------------------------*/
26 #include "NASedgeFormat.H"
27 #include "IFstream.H"
28 #include "IStringStream.H"
29 #include "PackedBoolList.H"
31 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
33 Foam::fileFormats::NASedgeFormat::NASedgeFormat
35     const fileName& filename
38     read(filename);
42 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
44 bool Foam::fileFormats::NASedgeFormat::read
46     const fileName& filename
49     clear();
51     IFstream is(filename);
52     if (!is.good())
53     {
54         FatalErrorIn
55         (
56             "fileFormats::NASedgeFormat::read(const fileName&)"
57         )
58             << "Cannot read file " << filename
59             << exit(FatalError);
60     }
62     DynamicList<point>  dynPoints;
63     DynamicList<edge>   dynEdges;
64     DynamicList<label>  pointId;     // Nastran index of points
66     while (is.good())
67     {
68         string line;
69         is.getLine(line);
71         // Skip empty or comment
72         if (line.empty() || line[0] == '$')
73         {
74             continue;
75         }
77         // Check if character 72 is continuation
78         if (line.size() > 72 && line[72] == '+')
79         {
80             line = line.substr(0, 72);
82             while (true)
83             {
84                 string buf;
85                 is.getLine(buf);
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         }
100         // Read first word
101         IStringStream lineStream(line);
102         word cmd;
103         lineStream >> cmd;
105         if (cmd == "CBEAM" || cmd == "CROD")
106         {
107             edge e;
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))());
113             // discard groupID
114             dynEdges.append(e);
115         }
116         else if (cmd == "GRID")
117         {
118             label index = readLabel(IStringStream(line.substr(8,8))());
119             scalar x = parseNASCoord(line.substr(24, 8));
120             scalar y = parseNASCoord(line.substr(32, 8));
121             scalar z = parseNASCoord(line.substr(40, 8));
123             pointId.append(index);
124             dynPoints.append(point(x, y, z));
125         }
126         else if (cmd == "GRID*")
127         {
128             // Long format is on two lines with '*' continuation symbol
129             // on start of second line.
130             // Typical line (spaces compacted)
131             // GRID*      126   0 -5.55999875E+02 -5.68730474E+02
132             // *         2.14897901E+02
134             label index = readLabel(IStringStream(line.substr(8,16))());
135             scalar x = parseNASCoord(line.substr(40, 16));
136             scalar y = parseNASCoord(line.substr(56, 16));
138             is.getLine(line);
139             if (line[0] != '*')
140             {
141                 FatalErrorIn
142                 (
143                     "fileFormats::NASedgeFormat::read(const fileName&)"
144                 )
145                     << "Expected continuation symbol '*' when reading GRID*"
146                     << " (double precision coordinate) format" << nl
147                     << "Read:" << line << nl
148                     << "File:" << is.name() << " line:" << is.lineNumber()
149                     << exit(FatalError);
150             }
151             scalar z = parseNASCoord(line.substr(8, 16));
153             pointId.append(index);
154             dynPoints.append(point(x, y, z));
155         }
156     }
158     // transfer to normal lists
159     storedPoints().transfer(dynPoints);
161     pointId.shrink();
162     dynEdges.shrink();
164     // Build inverse mapping (NASTRAN pointId -> index)
165     Map<label> mapPointId(2*pointId.size());
166     forAll(pointId, i)
167     {
168         mapPointId.insert(pointId[i], i);
169     }
171     // note which points were really used and which can be culled
172     PackedBoolList usedPoints(points().size());
175     // Pass1: relabel edges
176     // ~~~~~~~~~~~~~~~~~~~~
177     forAll(dynEdges, i)
178     {
179         edge& e = dynEdges[i];
180         e[0] = mapPointId[e[0]];
181         e[1] = mapPointId[e[1]];
183         usedPoints.set(e[0]);
184         usedPoints.set(e[1]);
185     }
186     pointId.clearStorage();
187     mapPointId.clear();
189     // not all the points were used, cull them accordingly
190     if (unsigned(points().size()) != usedPoints.count())
191     {
192         label nUsed = 0;
194         pointField& pts = storedPoints();
195         forAll(pts, pointI)
196         {
197             if (usedPoints.get(pointI))
198             {
199                 if (nUsed != pointI)
200                 {
201                     pts[nUsed] = pts[pointI];
202                 }
204                 // map prev -> new id
205                 mapPointId[pointI] = nUsed;
207                 ++nUsed;
208             }
209         }
211         pts.setSize(nUsed);
213         // renumber edge vertices
214         forAll(dynEdges, edgeI)
215         {
216             edge& e = dynEdges[edgeI];
218             e[0] = mapPointId[e[0]];
219             e[1] = mapPointId[e[1]];
220         }
221     }
224     // transfer to normal lists
225     storedEdges().transfer(dynEdges);
227     return true;
231 // ************************************************************************* //