BUG: UListIO: byteSize overflowing on really big faceLists
[OpenFOAM-2.0.x.git] / src / edgeMesh / edgeFormats / starcd / STARCDedgeFormat.C
blobd70817a0d7a5edc217cc2d42edcf9ac587fce91f
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
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 "STARCDedgeFormat.H"
27 #include "ListOps.H"
28 #include "clock.H"
29 #include "PackedBoolList.H"
30 #include "IStringStream.H"
32 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
34 inline void Foam::fileFormats::STARCDedgeFormat::writeLines
36     Ostream& os,
37     const edgeList& edges
40     writeHeader(os, "CELL");
42     forAll(edges, edgeI)
43     {
44         const edge& e = edges[edgeI];
45         const label cellId = edgeI + 1;
47         os  << cellId                    // includes 1 offset
48             << ' ' << starcdLineShape_   // 2(line) shape
49             << ' ' << e.size()
50             << ' ' << 401                // arbitrary value
51             << ' ' << starcdLineType_;   // 5(line)
53         os  << nl << "  " << cellId << "  "
54             << (e[0]+1) << "  " << (e[1]+1) << nl;
55     }
59 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
61 void Foam::fileFormats::STARCDedgeFormat::writeCase
63     Ostream& os,
64     const pointField& pointLst,
65     const label nEdges
68     word caseName = os.name().lessExt().name();
70     os  << "! STAR-CD file written " << clock::dateTime().c_str() << nl
71         << "! " << pointLst.size() << " points, " << nEdges << " lines" << nl
72         << "! case " << caseName << nl
73         << "! ------------------------------" << nl;
75 //     forAll(zoneLst, zoneI)
76 //     {
77 //         os  << "ctable " << zoneI + 1 << " line" << nl
78 //             << "ctname " << zoneI + 1 << " "
79 //             << zoneLst[zoneI].name() << nl;
80 //     }
82     os  << "! ------------------------------" << nl
83         << "*set icvo mxv - 1" << nl
84         << "vread " << caseName << ".vrt icvo,,,coded" << nl
85         << "cread " << caseName << ".cel icvo,,,add,coded" << nl
86         << "*set icvo" << nl
87         << "! end" << nl;
89     os.flush();
93 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
95 Foam::fileFormats::STARCDedgeFormat::STARCDedgeFormat
97     const fileName& filename
100     read(filename);
104 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
106 bool Foam::fileFormats::STARCDedgeFormat::read
108     const fileName& filename
111     clear();
113     fileName baseName = filename.lessExt();
115     // STAR-CD index of points
116     List<label> pointId;
118     // read points from .vrt file
119     readPoints
120     (
121         IFstream(baseName + ".vrt")(),
122         storedPoints(),
123         pointId
124     );
126     // Build inverse mapping (STAR-CD pointId -> index)
127     Map<label> mapPointId(2*pointId.size());
128     forAll(pointId, i)
129     {
130         mapPointId.insert(pointId[i], i);
131     }
132     pointId.clear();
134     // note which points were really used and which can be culled
135     PackedBoolList usedPoints(points().size());
137     //
138     // read .cel file
139     // ~~~~~~~~~~~~~~
140     IFstream is(baseName + ".cel");
141     if (!is.good())
142     {
143         FatalErrorIn
144         (
145             "fileFormats::STARCDedgeFormat::read(const fileName&)"
146         )
147             << "Cannot read file " << is.name()
148             << exit(FatalError);
149     }
151     readHeader(is, "PROSTAR_CELL");
153     DynamicList<edge>  dynEdges;
155     label lineLabel, shapeId, nLabels, cellTableId, typeId;
156     DynamicList<label> vertexLabels(64);
158     while ((is >> lineLabel).good())
159     {
160         is >> shapeId >> nLabels >> cellTableId >> typeId;
162         vertexLabels.clear();
163         vertexLabels.reserve(nLabels);
165         // read indices - max 8 per line
166         for (label i = 0; i < nLabels; ++i)
167         {
168             label vrtId;
169             if ((i % 8) == 0)
170             {
171                is >> lineLabel;
172             }
173             is >> vrtId;
175             // convert original vertex id to point label
176             vertexLabels.append(mapPointId[vrtId]);
177         }
179         if (typeId == starcdLineType_)
180         {
181             if (vertexLabels.size() >= 2)
182             {
183                 dynEdges.append(edge(vertexLabels[0], vertexLabels[1]));
185                 usedPoints.set(vertexLabels[0]);
186                 usedPoints.set(vertexLabels[1]);
187             }
188         }
189     }
191     mapPointId.clear();
193     // not all the points were used, cull them accordingly
194     if (unsigned(points().size()) != usedPoints.count())
195     {
196         label nUsed = 0;
198         pointField& pts = storedPoints();
199         forAll(pts, pointI)
200         {
201             if (usedPoints.get(pointI))
202             {
203                 if (nUsed != pointI)
204                 {
205                     pts[nUsed] = pts[pointI];
206                 }
208                 // map prev -> new id
209                 mapPointId.set(pointI, nUsed);
211                 ++nUsed;
212             }
213         }
215         pts.setSize(nUsed);
217         // renumber edge vertices
218         forAll(dynEdges, edgeI)
219         {
220             edge& e = dynEdges[edgeI];
222             e[0] = mapPointId[e[0]];
223             e[1] = mapPointId[e[1]];
224         }
225     }
228     storedEdges().transfer(dynEdges);
230     return true;
234 void Foam::fileFormats::STARCDedgeFormat::write
236     const fileName& filename,
237     const edgeMesh& mesh
240     const pointField& pointLst = mesh.points();
241     const edgeList& edgeLst = mesh.edges();
243     fileName baseName = filename.lessExt();
245     writePoints(OFstream(baseName + ".vrt")(), pointLst);
246     writeLines(OFstream(baseName + ".cel")(), edgeLst);
248     // write a simple .inp file
249     writeCase
250     (
251         OFstream(baseName + ".inp")(),
252         pointLst,
253         edgeLst.size()
254     );
258 // ************************************************************************* //