STARCDedgeFormat.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 "STARCDedgeFormat.H"
27 #include "ListOps.H"
28 #include "clock.H"
29 #include "PackedBoolList.H"
30 #include "IStringStream.H"
31 
32 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
33 
34 inline void Foam::fileFormats::STARCDedgeFormat::writeLines
35 (
36  Ostream& os,
37  const edgeList& edges
38 )
39 {
40  writeHeader(os, "CELL");
41 
42  forAll(edges, edgeI)
43  {
44  const edge& e = edges[edgeI];
45  const label cellId = edgeI + 1;
46 
47  os << cellId // includes 1 offset
48  << ' ' << starcdLineShape_ // 2(line) shape
49  << ' ' << e.size()
50  << ' ' << 401 // arbitrary value
51  << ' ' << starcdLineType_; // 5(line)
52 
53  os << nl << " " << cellId << " "
54  << (e[0]+1) << " " << (e[1]+1) << nl;
55  }
56 }
57 
58 
59 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
60 
62 (
63  Ostream& os,
64  const pointField& pointLst,
65  const label nEdges
66 )
67 {
68  word caseName = os.name().lessExt().name();
69 
70  os << "! STAR-CD file written " << clock::dateTime().c_str() << nl
71  << "! " << pointLst.size() << " points, " << nEdges << " lines" << nl
72  << "! case " << caseName << nl
73  << "! ------------------------------" << nl;
74 
75 // forAll(zoneLst, zoneI)
76 // {
77 // os << "ctable " << zoneI + 1 << " line" << nl
78 // << "ctname " << zoneI + 1 << " "
79 // << zoneLst[zoneI].name() << nl;
80 // }
81 
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;
88 
89  os.flush();
90 }
91 
92 
93 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
94 
96 (
97  const fileName& filename
98 )
99 {
100  read(filename);
101 }
102 
103 
104 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
105 
107 (
108  const fileName& filename
109 )
110 {
111  clear();
112 
113  fileName baseName = filename.lessExt();
114 
115  // STAR-CD index of points
116  List<label> pointId;
117 
118  // read points from .vrt file
119  readPoints
120  (
121  IFstream(baseName + ".vrt")(),
122  storedPoints(),
123  pointId
124  );
125 
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();
133 
134  // note which points were really used and which can be culled
135  PackedBoolList usedPoints(points().size());
136 
137  //
138  // read .cel file
139  // ~~~~~~~~~~~~~~
140  IFstream is(baseName + ".cel");
141  if (!is.good())
142  {
144  << "Cannot read file " << is.name()
145  << exit(FatalError);
146  }
147 
148  readHeader(is, "PROSTAR_CELL");
149 
150  DynamicList<edge> dynEdges;
151 
152  label lineLabel, shapeId, nLabels, cellTableId, typeId;
153  DynamicList<label> vertexLabels(64);
154 
155  while ((is >> lineLabel).good())
156  {
157  is >> shapeId >> nLabels >> cellTableId >> typeId;
158 
159  vertexLabels.clear();
160  vertexLabels.reserve(nLabels);
161 
162  // read indices - max 8 per line
163  for (label i = 0; i < nLabels; ++i)
164  {
165  label vrtId;
166  if ((i % 8) == 0)
167  {
168  is >> lineLabel;
169  }
170  is >> vrtId;
171 
172  // convert original vertex id to point label
173  vertexLabels.append(mapPointId[vrtId]);
174  }
175 
176  if (typeId == starcdLineType_)
177  {
178  if (vertexLabels.size() >= 2)
179  {
180  dynEdges.append(edge(vertexLabels[0], vertexLabels[1]));
181 
182  usedPoints.set(vertexLabels[0]);
183  usedPoints.set(vertexLabels[1]);
184  }
185  }
186  }
187 
188  mapPointId.clear();
189 
190  // not all the points were used, cull them accordingly
191  if (unsigned(points().size()) != usedPoints.count())
192  {
193  label nUsed = 0;
194 
195  pointField& pts = storedPoints();
196  forAll(pts, pointi)
197  {
198  if (usedPoints.get(pointi))
199  {
200  if (nUsed != pointi)
201  {
202  pts[nUsed] = pts[pointi];
203  }
204 
205  // map prev -> new id
206  mapPointId.set(pointi, nUsed);
207 
208  ++nUsed;
209  }
210  }
211 
212  pts.setSize(nUsed);
213 
214  // renumber edge vertices
215  forAll(dynEdges, edgeI)
216  {
217  edge& e = dynEdges[edgeI];
218 
219  e[0] = mapPointId[e[0]];
220  e[1] = mapPointId[e[1]];
221  }
222  }
223 
224 
225  storedEdges().transfer(dynEdges);
226 
227  return true;
228 }
229 
230 
232 (
233  const fileName& filename,
234  const edgeMesh& mesh
235 )
236 {
237  const pointField& pointLst = mesh.points();
238  const edgeList& edgeLst = mesh.edges();
239 
240  fileName baseName = filename.lessExt();
241 
242  writePoints(OFstream(baseName + ".vrt")(), pointLst);
243  writeLines(OFstream(baseName + ".cel")(), edgeLst);
244 
245  // write a simple .inp file
246  writeCase
247  (
248  OFstream(baseName + ".inp")(),
249  pointLst,
250  edgeLst.size()
251  );
252 }
253 
254 
255 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
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
A class for handling file names.
Definition: fileName.H:79
virtual void flush()=0
Flush stream.
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
edgeList & storedEdges()
Non-const access to the edges.
Definition: edgeMeshI.H:34
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:323
Output to file stream.
Definition: OFstream.H:82
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
static void writeHeader(Ostream &, const word &fileType)
Write header for fileType (CELL|VERTEX|BOUNDARY)
Definition: STARCDCore.C:75
pointField & storedPoints()
Non-const access to global points.
Definition: edgeMeshI.H:28
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:330
void reserve(const label)
Reserve allocation space for at least this size.
Definition: DynamicListI.H:152
Various functions to operate on Lists.
static bool readHeader(IFstream &, const word &fileSignature)
Read header.
Definition: STARCDCore.C:41
static void writePoints(Ostream &, const pointField &)
Write header and points to (.vrt) file.
Definition: STARCDCore.C:138
void set(const PackedList< 1 > &)
Set specified bits.
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:56
List< edge > edgeList
Definition: edgeList.H:38
An edge is a list of two point labels. The functionality it provides supports the discretisation on a...
Definition: edge.H:58
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:125
virtual bool read(const fileName &)
Read from file.
A class for handling words, derived from string.
Definition: word.H:59
static string dateTime()
Return the current wall-clock date/time as a string.
Definition: clock.C:57
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.H:294
const fileName & name() const
Return the name of the stream.
Definition: IFstream.H:116
const edgeList & edges() const
Return edges.
Definition: edgeMeshI.H:68
word name() const
Return file name (part beyond last /)
Definition: fileName.C:195
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
Definition: DynamicListI.H:296
STARCDedgeFormat(const fileName &)
Construct from file name.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
static const char nl
Definition: Ostream.H:260
const pointField & points() const
Return points.
Definition: edgeMeshI.H:62
Points connected by edges.
Definition: edgeMesh.H:69
Input from file stream.
Definition: IFstream.H:81
unsigned int count() const
Count number of bits set, O(log(n))
Definition: PackedList.C:55
virtual void clear()
Clear all storage.
Definition: edgeMesh.C:166
void setSize(const label)
Reset size of List.
Definition: List.C:281
A bit-packed bool list.
static void writeCase(Ostream &, const pointField &, const label nEdges)
label cellId
static bool readPoints(IFstream &, pointField &, labelList &ids)
Read points from a (.vrt) file.
Definition: STARCDCore.C:96
fileName lessExt() const
Return file name without extension (part before last .)
Definition: fileName.C:284
const doubleScalar e
Elementary charge.
Definition: doubleScalar.H:105
static void write(const fileName &, const edgeMesh &)
Write edge mesh.
void clear()
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicListI.H:236
void transfer(List< T > &)
Transfer the contents of the argument List into this list.
Definition: List.C:342