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  label starCellId
42 )
43 {
44  starCellId = max(1, starCellId); // Enforce 1-based cellId
45 
46  for (const edge& e : edges)
47  {
48  os << starCellId
49  << ' ' << starcdLine // 2(line) shape
50  << ' ' << e.size()
51  << ' ' << 401 // arbitrary value
52  << ' ' << starcdLineType; // 5(line)
53 
54  os << nl
55  << " " << starCellId << " "
56  << (e[0]+1) << " " << (e[1]+1) << nl;
57 
58  ++starCellId;
59  }
60 }
61 
62 
63 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
64 
66 (
67  Ostream& os,
68  const pointField& pointLst,
69  const label nEdges
70 )
71 {
72  const word caseName = os.name().nameLessExt();
73 
74  os << "! STARCD file written " << clock::dateTime().c_str() << nl
75  << "! " << pointLst.size() << " points, " << nEdges << " lines" << nl
76  << "! case " << caseName << nl
77  << "! ------------------------------" << nl;
78 
79 // forAll(zoneLst, zoneI)
80 // {
81 // os << "ctable " << zoneI + 1 << " line" << nl
82 // << "ctname " << zoneI + 1 << " "
83 // << zoneLst[zoneI].name() << nl;
84 // }
85 
86  os << "! ------------------------------" << nl
87  << "*set icvo mxv - 1" << nl
88  << "vread " << caseName << ".vrt icvo,,,coded" << nl
89  << "cread " << caseName << ".cel icvo,,,add,coded" << nl
90  << "*set icvo" << nl
91  << "! end" << nl;
92 
93  os.flush();
94 }
95 
96 
97 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
98 
99 Foam::fileFormats::STARCDedgeFormat::STARCDedgeFormat
100 (
101  const fileName& filename
102 )
103 {
104  read(filename);
105 }
106 
107 
108 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
109 
111 (
112  const fileName& filename
113 )
114 {
115  clear();
116 
117  fileName baseName = filename.lessExt();
118 
119  // STARCD index of points
120  List<label> pointId;
121 
122  // Read points from .vrt file
123  readPoints
124  (
125  IFstream(starFileName(baseName, STARCDCore::VRT_FILE))(),
126  storedPoints(),
127  pointId
128  );
129 
130  // Build inverse mapping (STARCD pointId -> index)
131  Map<label> mapPointId(2*pointId.size());
132  forAll(pointId, i)
133  {
134  mapPointId.insert(pointId[i], i);
135  }
136  pointId.clear();
137 
138  // Note which points were really used and which can be culled
139  bitSet usedPoints(points().size());
140 
141 
142  // Read .cel file
143  // ~~~~~~~~~~~~~~
144  IFstream is(starFileName(baseName, STARCDCore::CEL_FILE));
145  if (!is.good())
146  {
148  << "Cannot read file " << is.name()
149  << exit(FatalError);
150  }
151 
152  readHeader(is, STARCDCore::HEADER_CEL);
153 
154  DynamicList<edge> dynEdges;
155 
156  label ignoredLabel, shapeId, nLabels, cellTableId, typeId;
157  DynamicList<label> vertexLabels(64);
158 
159  token tok;
160 
161  while (is.read(tok).good() && tok.isLabel())
162  {
163  // const label starCellId = tok.labelToken();
164  is >> shapeId
165  >> nLabels
166  >> cellTableId
167  >> typeId;
168 
169  vertexLabels.clear();
170  vertexLabels.reserve(nLabels);
171 
172  // Read indices - max 8 per line
173  for (label i = 0; i < nLabels; ++i)
174  {
175  label vrtId;
176  if ((i % 8) == 0)
177  {
178  is >> ignoredLabel; // Skip cellId for continuation lines
179  }
180  is >> vrtId;
181 
182  // Convert original vertex id to point label
183  vertexLabels.append(mapPointId[vrtId]);
184  }
185 
186  if (typeId == starcdLineType)
187  {
188  if (vertexLabels.size() >= 2)
189  {
190  dynEdges.append(edge(vertexLabels[0], vertexLabels[1]));
191 
192  usedPoints.set(vertexLabels[0]);
193  usedPoints.set(vertexLabels[1]);
194  }
195  }
196  }
197 
198  mapPointId.clear();
199 
200  // Not all points were used, subset/cull them accordingly
201  if (!usedPoints.all())
202  {
203  label nUsed = 0;
204 
205  pointField& pts = storedPoints();
206  for (const label pointi : usedPoints)
207  {
208  if (nUsed != pointi)
209  {
210  pts[nUsed] = pts[pointi];
211  }
212 
213  // Map prev -> new id
214  mapPointId.set(pointi, nUsed);
215 
216  ++nUsed;
217  }
218  pts.resize(nUsed);
219 
220  // Renumber edge vertices
221  for (edge& e : dynEdges)
222  {
223  e[0] = mapPointId[e[0]];
224  e[1] = mapPointId[e[1]];
225  }
226  }
227 
228  storedEdges().transfer(dynEdges);
229 
230  return true;
231 }
232 
233 
235 (
236  const fileName& filename,
237  const edgeMesh& mesh,
238  IOstreamOption streamOpt,
239  const dictionary&
240 )
241 {
242  // ASCII only, allow output compression
243  streamOpt.format(IOstream::ASCII);
244 
245  const pointField& pointLst = mesh.points();
246  const edgeList& edgeLst = mesh.edges();
247 
248  fileName baseName = filename.lessExt();
249 
250  // The .vrt file
251  {
252  OFstream os(starFileName(baseName, STARCDCore::VRT_FILE), streamOpt);
253  writePoints(os, pointLst);
254  }
255 
256  // The .cel file
257  {
258  OFstream os(starFileName(baseName, STARCDCore::CEL_FILE), streamOpt);
260  writeLines(os, edgeLst);
261  }
262 
263  // Write a simple .inp file. Never compressed
264  writeCase
265  (
266  OFstream(starFileName(baseName, STARCDCore::INP_FILE))(),
267  pointLst,
268  edgeLst.size()
269  );
270 }
271 
272 
273 // ************************************************************************* //
Foam::fileFormats::STARCDCore::INP_FILE
Definition: STARCDCore.H:79
Foam::token::isLabel
bool isLabel() const noexcept
Token is LABEL.
Definition: tokenI.H:497
Foam::edgeList
List< edge > edgeList
A List of edges.
Definition: edgeList.H:63
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
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:111
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:113
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::IOstreamOption::format
streamFormat format() const noexcept
Get the current stream format.
Definition: IOstreamOption.H:286
Foam::fileFormats::STARCDedgeFormat::write
static void write(const fileName &filename, const edgeMesh &mesh, IOstreamOption streamOpt=IOstreamOption(), const dictionary &options=dictionary::null)
Write edge mesh to file.
Definition: STARCDedgeFormat.C:235
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:230
Foam::DynamicList::reserve
void reserve(const label len)
Definition: DynamicListI.H:333
Foam::writeHeader
static void writeHeader(Ostream &os, const word &fieldName)
Definition: rawSurfaceWriterImpl.C:66
bitSet.H
Foam::DynamicList::clear
void clear() noexcept
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicListI.H:391
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::fileFormats::STARCDCore::VRT_FILE
Definition: STARCDCore.H:77
Foam::IOstream::good
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:233
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:511
Foam::IOstreamOption
The IOstreamOption is a simple container for options an IOstream can normally have.
Definition: IOstreamOption.H:63
Foam::max
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
Foam::fileFormats::STARCDCore::CEL_FILE
Definition: STARCDCore.H:76
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
os
OBJstream os(runTime.globalPath()/outputName)
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
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
Foam::edgeMesh::edges
const edgeList & edges() const noexcept
Return edges.
Definition: edgeMeshI.H:105
Foam::IOstreamOption::ASCII
"ascii" (normal default)
Definition: IOstreamOption.H:72
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
clear
patchWriters clear()
Foam::nl
constexpr char nl
Definition: Ostream.H:404
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:116
Foam::ISstream::read
virtual Istream & read(token &t)
Return next token from stream.
Definition: ISstream.C:530
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::edgeMesh
Mesh data needed to do the Finite Area discretisation.
Definition: edgeFaMesh.H:53
Foam::fileFormats::STARCDedgeFormat::writeCase
static void writeCase(Ostream &os, const pointField &pointLst, const label nEdges)
Definition: STARCDedgeFormat.C:66