STARCDedgeFormat.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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2017 OpenFOAM Foundation
9  Copyright (C) 2016 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "STARCDedgeFormat.H"
30 #include "ListOps.H"
31 #include "clock.H"
32 #include "bitSet.H"
33 #include "StringStream.H"
34 
35 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
36 
37 inline void Foam::fileFormats::STARCDedgeFormat::writeLines
38 (
39  Ostream& os,
40  const edgeList& edges
41 )
42 {
44 
45  label starCellId = 1; // 1-based cellId
46 
47  for (const edge& e : edges)
48  {
49  os << starCellId
50  << ' ' << starcdLine // 2(line) shape
51  << ' ' << e.size()
52  << ' ' << 401 // arbitrary value
53  << ' ' << starcdLineType; // 5(line)
54 
55  os << nl
56  << " " << starCellId << " "
57  << (e[0]+1) << " " << (e[1]+1) << nl;
58 
59  ++starCellId;
60  }
61 }
62 
63 
64 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
65 
67 (
68  Ostream& os,
69  const pointField& pointLst,
70  const label nEdges
71 )
72 {
73  const word caseName = os.name().nameLessExt();
74 
75  os << "! STARCD file written " << clock::dateTime().c_str() << nl
76  << "! " << pointLst.size() << " points, " << nEdges << " lines" << nl
77  << "! case " << caseName << nl
78  << "! ------------------------------" << nl;
79 
80 // forAll(zoneLst, zoneI)
81 // {
82 // os << "ctable " << zoneI + 1 << " line" << nl
83 // << "ctname " << zoneI + 1 << " "
84 // << zoneLst[zoneI].name() << nl;
85 // }
86 
87  os << "! ------------------------------" << nl
88  << "*set icvo mxv - 1" << nl
89  << "vread " << caseName << ".vrt icvo,,,coded" << nl
90  << "cread " << caseName << ".cel icvo,,,add,coded" << nl
91  << "*set icvo" << nl
92  << "! end" << nl;
93 
94  os.flush();
95 }
96 
97 
98 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
99 
100 Foam::fileFormats::STARCDedgeFormat::STARCDedgeFormat
101 (
102  const fileName& filename
103 )
104 {
105  read(filename);
106 }
107 
108 
109 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
110 
112 (
113  const fileName& filename
114 )
115 {
116  clear();
117 
118  fileName baseName = filename.lessExt();
119 
120  // STARCD index of points
121  List<label> pointId;
122 
123  // Read points from .vrt file
124  readPoints
125  (
126  IFstream(starFileName(baseName, STARCDCore::VRT_FILE))(),
127  storedPoints(),
128  pointId
129  );
130 
131  // Build inverse mapping (STARCD pointId -> index)
132  Map<label> mapPointId(2*pointId.size());
133  forAll(pointId, i)
134  {
135  mapPointId.insert(pointId[i], i);
136  }
137  pointId.clear();
138 
139  // Note which points were really used and which can be culled
140  bitSet usedPoints(points().size());
141 
142 
143  // Read .cel file
144  // ~~~~~~~~~~~~~~
145  IFstream is(starFileName(baseName, STARCDCore::CEL_FILE));
146  if (!is.good())
147  {
149  << "Cannot read file " << is.name()
150  << exit(FatalError);
151  }
152 
153  readHeader(is, STARCDCore::HEADER_CEL);
154 
155  DynamicList<edge> dynEdges;
156 
157  label ignoredLabel, shapeId, nLabels, cellTableId, typeId;
158  DynamicList<label> vertexLabels(64);
159 
160  token tok;
161 
162  while (is.read(tok).good() && tok.isLabel())
163  {
164  // const label starCellId = tok.labelToken();
165  is >> shapeId
166  >> nLabels
167  >> cellTableId
168  >> typeId;
169 
170  vertexLabels.clear();
171  vertexLabels.reserve(nLabels);
172 
173  // Read indices - max 8 per line
174  for (label i = 0; i < nLabels; ++i)
175  {
176  label vrtId;
177  if ((i % 8) == 0)
178  {
179  is >> ignoredLabel; // Skip cellId for continuation lines
180  }
181  is >> vrtId;
182 
183  // Convert original vertex id to point label
184  vertexLabels.append(mapPointId[vrtId]);
185  }
186 
187  if (typeId == starcdLineType)
188  {
189  if (vertexLabels.size() >= 2)
190  {
191  dynEdges.append(edge(vertexLabels[0], vertexLabels[1]));
192 
193  usedPoints.set(vertexLabels[0]);
194  usedPoints.set(vertexLabels[1]);
195  }
196  }
197  }
198 
199  mapPointId.clear();
200 
201  // Not all points were used, subset/cull them accordingly
202  if (!usedPoints.all())
203  {
204  label nUsed = 0;
205 
206  pointField& pts = storedPoints();
207  for (const label pointi : usedPoints)
208  {
209  if (nUsed != pointi)
210  {
211  pts[nUsed] = pts[pointi];
212  }
213 
214  // Map prev -> new id
215  mapPointId.set(pointi, nUsed);
216 
217  ++nUsed;
218  }
219  pts.resize(nUsed);
220 
221  // Renumber edge vertices
222  for (edge& e : dynEdges)
223  {
224  e[0] = mapPointId[e[0]];
225  e[1] = mapPointId[e[1]];
226  }
227  }
228 
229  storedEdges().transfer(dynEdges);
230 
231  return true;
232 }
233 
234 
236 (
237  const fileName& filename,
238  const edgeMesh& mesh
239 )
240 {
241  const pointField& pointLst = mesh.points();
242  const edgeList& edgeLst = mesh.edges();
243 
244  fileName baseName = filename.lessExt();
245 
246  writePoints
247  (
248  OFstream(starFileName(baseName, STARCDCore::VRT_FILE))(),
249  pointLst
250  );
251  writeLines
252  (
253  OFstream(starFileName(baseName, STARCDCore::CEL_FILE))(),
254  edgeLst
255  );
256 
257  // Write a simple .inp file
258  writeCase
259  (
260  OFstream(starFileName(baseName, STARCDCore::INP_FILE))(),
261  pointLst,
262  edgeLst.size()
263  );
264 }
265 
266 
267 // ************************************************************************* //
Foam::fileFormats::STARCDCore::INP_FILE
Definition: STARCDCore.H:79
Foam::token::isLabel
bool isLabel() const noexcept
Token is LABEL.
Definition: tokenI.H:481
Foam::edgeList
List< edge > edgeList
A List of edges.
Definition: edgeList.H:63
Foam::fileName::nameLessExt
static std::string nameLessExt(const std::string &str)
Return basename, without extension.
Definition: fileName.C:402
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::bitSet::all
bool all() const
True if all bits in this bitset are set or if the set is empty.
Definition: bitSetI.H:460
Foam::bitSet
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition: bitSet.H:63
Foam::fileFormats::STARCDedgeFormat::read
virtual bool read(const fileName &name)
Read from file.
Definition: STARCDedgeFormat.C:112
Foam::IFstream
Input from file stream, using an ISstream.
Definition: IFstream.H:53
Foam::DynamicList
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:55
Foam::IFstream::name
virtual const fileName & name() const
Read/write access to the name of the stream.
Definition: ISstream.H:124
Foam::read
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition: int32.H:108
Foam::edge
An edge is a list of two point labels. The functionality it provides supports the discretisation on a...
Definition: edge.H:63
Foam::bitSet::set
void set(const bitSet &bitset)
Set specified bits from another bitset.
Definition: bitSetI.H:574
Foam::Map< label >
StringStream.H
Input/output from string buffers.
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
Foam::fileName::lessExt
fileName lessExt() const
Return file name without extension (part before last .)
Definition: fileNameI.H:240
bitSet.H
Foam::DynamicList::reserve
void reserve(const label nElem)
Reserve allocation space for at least this size.
Definition: DynamicListI.H:254
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::fileFormats::STARCDCore::VRT_FILE
Definition: STARCDCore.H:77
STARCDedgeFormat.H
Foam::Field< vector >
Foam::clock::dateTime
static std::string dateTime()
Definition: clock.C:60
clock.H
Foam::DynamicList::append
DynamicList< T, SizeMin > & append(const T &val)
Append an element to the end of this list.
Definition: DynamicListI.H:474
Foam::DynamicList::clear
void clear()
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicListI.H:348
Foam::IOstream::name
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.C:39
Foam::edgeMesh::edges
const edgeList & edges() const
Return edges.
Definition: edgeMeshI.H:105
Foam::fileFormats::STARCDCore::CEL_FILE
Definition: STARCDCore.H:76
Foam::FatalError
error FatalError
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam::Ostream::flush
virtual void flush()=0
Flush stream.
Foam::fileFormats::STARCDCore::writeHeader
static void writeHeader(Ostream &os, const enum fileHeader header)
Write header for fileType (CELL|VERTEX|BOUNDARY)
Definition: STARCDCore.C:156
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::OFstream
Output to file stream, using an OSstream.
Definition: OFstream.H:53
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:381
clear
patchWriters clear()
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::fileFormats::STARCDedgeFormat::write
static void write(const fileName &name, const edgeMesh &mesh)
Write edge mesh.
Definition: STARCDedgeFormat.C:236
Foam::List< label >
Foam::fileFormats::STARCDCore::HEADER_CEL
Definition: STARCDCore.H:68
points
const pointField & points
Definition: gmvOutputHeader.H:1
Foam::constant::electromagnetic::e
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
Foam::List::clear
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:115
Foam::ISstream::read
virtual Istream & read(token &t)
Return next token from stream.
Definition: ISstream.C:158
Foam::fileFormats::STARCDCore::starcdLineType
Definition: STARCDCore.H:89
ListOps.H
Various functions to operate on Lists.
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::fileFormats::STARCDCore::starcdLine
Definition: STARCDCore.H:97
Foam::IOstream::good
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:224
Foam::edgeMesh
Mesh data needed to do the Finite Area discretisation.
Definition: edgeFaMesh.H:52
Foam::fileFormats::STARCDedgeFormat::writeCase
static void writeCase(Ostream &os, const pointField &pointLst, const label nEdges)
Definition: STARCDedgeFormat.C:67