surfaceWriter.H
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-2012 OpenFOAM Foundation
9 Copyright (C) 2015-2022 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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
27Namespace
28 Foam::surfaceWriters
29
30Description
31 Namespace for surface writers
32
33Class
34 Foam::surfaceWriter
35
36Description
37 Base class for surface writers.
38
39 The surfaceWriter interface is rather large since we need a writer that
40 can either be initially defined without a surface association
41 and have that added at a later stage, or be defined with a surface
42 association.
43
44 \verbatim
45 formatOptions
46 {
47 someFormat // Eg, ensight, vtk, etc
48 {
49 verbose true;
50 fieldLevel
51 {
52 "p.*" 1e5; // Absolute -> gauge [Pa]
53 T 273.15; // [K] -> [C]
54 U #eval{ 10/sqrt(3) }; // Uniform magU=10
55 }
56 fieldScale
57 {
58 "p.*" 0.01; // [Pa] -> [mbar]
59 }
60
61 scale 1000; // [m] -> [mm]
62 transform
63 {
64 origin (0 0 0);
65 rotation axisAngle;
66 axis (1 0 0);
67 angle 45;
68 }
69
70 }
71 }
72 \endverbatim
73
74 Format options:
75 \table
76 Property | Description | Reqd | Default
77 verbose | Additional output verbosity | no | no
78 scale | Output geometry scaling | no | 1
79 transform | Output coordinate transform | no |
80 fieldLevel | Subtract field level before scaling | no | empty dict
81 fieldScale | Output field scaling | no | empty dict
82 \endtable
83
84Note
85 For surface formats that require geometry in a separate file,
86 it is the responsibility of the implementation (not the caller)
87 to ensure that this occurs.
88
89SourceFiles
90 surfaceWriter.C
91 surfaceWriterI.H
92
93\*---------------------------------------------------------------------------*/
94
95#ifndef Foam_surfaceWriter_H
96#define Foam_surfaceWriter_H
97
98#include "typeInfo.H"
99#include "autoPtr.H"
100#include "tmp.H"
101#include "cartesianCS.H"
102#include "Field.H"
103#include "fileName.H"
104#include "instant.H"
105#include "mergedSurf.H"
106#include "meshedSurfRef.H"
107#include "InfoProxy.H"
109
110// Deprecated methods
111// #define Foam_surfaceWriter_directAccess
112
113// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
114
115namespace Foam
116{
117
118// Forward Declarations
119class Time;
120class surfaceWriter;
121
122Ostream& operator<<(Ostream& os, const InfoProxy<surfaceWriter>& ip);
123
124
125/*---------------------------------------------------------------------------*\
126 Class surfaceWriter Declaration
127\*---------------------------------------------------------------------------*/
128
129class surfaceWriter
130{
131protected:
132
133 // Private Data
134
135 //- Reference to surface or surface components
136 meshedSurfRef surf_;
137
138 //- Surface after merging (parallel)
139 mutable mergedSurf mergedSurf_;
140
141 //- The surface after point coordinate transforms and scaling
142 mutable meshedSurfRef adjustedSurf_;
143
144 //- Dimension for merging
145 scalar mergeDim_;
146
147 //- Output geometry scaling after rotate/translate
148 scalar geometryScale_;
149
150 //- Local coordinate system transformation
151 coordSystem::cartesian geometryTransform_;
152
153
154protected:
155
156 // Protected Data
157
158 //- The topology/surface is up-to-date?
159 mutable bool upToDate_;
160
161 //- Track if geometry has been written since the last open
162 mutable bool wroteGeom_;
163
164 //- Writing in parallel (via master)
166
167 //- Insert additional time sub-directory in the output path
169
170 //- Is point vs cell data
172
173 //- Additional output verbosity
175
176 //- The number of fields
177 label nFields_;
178
179 //- The current time value/name
181
182 //- The full output directory and file (surface) name
184
185 //- Field level to remove (on output)
187
188 //- Field scaling (on output)
190
192 // Protected Member Functions
193
194 //- Verify that the outputPath_ has been set or FatalError
195 void checkOpen() const;
196
197 //- Merge surfaces if they are not already upToDate (parallel)
198 //- or simply mark the surface as being up-to-date
199 virtual bool merge() const;
201 //- Merge surfaces (if not upToDate) and return merged (parallel)
202 //- or regular surface (non-parallel)
203 const meshedSurf& surface() const;
204
205 //- Merge surfaces (if not upToDate) and return merged (parallel)
206 //- or regular surface (non-parallel)
207 //- and apply any coordinate system changes and/or output scaling.
208 const meshedSurfRef& adjustSurface() const;
210 //- Gather (merge) fields with renumbering and shrinking for point data
211 template<class Type>
213
214 //- Apply refLevel and fieldScaling
215 template<class Type>
217 (
218 const word& fieldName,
219 const tmp<Field<Type>>& tfield
220 ) const;
221
222#undef declareSurfaceFieldMethod
223#define declareSurfaceFieldMethods(Type) \
224 \
225 tmp<Field<Type>> mergeField(const Field<Type>& fld) const; \
226 \
227 tmp<Field<Type>> adjustField \
228 ( \
229 const word& fieldName, \
230 const tmp<Field<Type>>& tfield \
231 ) const;
232
239
240 #undef declareSurfaceFieldMethods
242 //- Dummy templated write operation
243 template<class Type>
246 const word& fieldName,
247 const Field<Type>& localValues
248 )
249 {
250 if (!wroteGeom_)
251 {
252 return this->write();
253 }
254 return fileName::null;
255 }
256
257
258public:
259
260 // Public Data
261
262 //- The default merge dimension (1e-8)
263 static scalar defaultMergeDim;
264
265
266 //- Runtime type information
267 TypeName("surfaceWriter");
268
269
270 // Declare run-time constructor selection table
272 (
275 word,
276 (),
277 ()
278 );
279
281 (
282 autoPtr,
284 wordDict,
285 (
286 const dictionary& writeOpts
287 ),
288 (writeOpts)
289 );
290
291
292 // Selectors
293
294 //- True if New is likely to succeed for this writeType
295 static bool supportedType(const word& writeType);
297 //- Return a reference to the selected surfaceWriter
298 static autoPtr<surfaceWriter> New(const word& writeType);
299
300 //- Return a reference to the selected surfaceWriter
301 // Select with extra write option
303 (
304 const word& writeType,
305 const dictionary& writeOptions
306 );
307
308
309 // Constructors
310
311 //- Default construct
313
314 //- Default construct with specified options
315 explicit surfaceWriter(const dictionary& options);
316
317
318 //- Destructor. Calls close()
319 virtual ~surfaceWriter();
320
321
322 // Member Functions
323
324 // Capability
325
326 //- The writer is enabled. If the writer is not enabled, it may be
327 //- possible for the caller to skip various preparatory operations.
328 // This method is primarily useful for the null writer
329 virtual bool enabled() const
330 {
331 return true;
332 }
333
334 //- True if the surface format requires geometry in a separate file.
335 virtual bool separateGeometry() const
336 {
337 return false;
338 }
339
340 //- True if the writer format uses faceIds as part of its output.
341 // Element ids are used by various CAE formats
342 // (abaqus, nastran, starcd, ...)
343 virtual bool usesFaceIds() const
344 {
345 return false;
346 }
347
348
349 // Bookkeeping
350
351 //- Does the writer need an update (eg, lagging behind surface changes)
352 virtual bool needsUpdate() const;
353
354 //- Geometry or fields written since the last open?
355 virtual bool wroteData() const;
356
357 //- Mark that surface changed and the writer will need an update,
358 //- and set nFields = 0.
359 // May also free up unneeded data.
360 // Return false if it was previously already expired.
361 virtual bool expire();
362
363 //- Close any open output, remove association with a surface and
364 //- expire the writer. The parallel flag remains untouched.
365 virtual void clear();
366
367
368 // Surface association
369
370 //- Change association with a surface, expire the writer
371 //- with defined parallel/serial treatment
372 virtual void setSurface
373 (
374 const meshedSurf& surf,
375 bool parallel
376 );
377
378 //- Change association with a surface, expire the writer
379 //- with defined parallel/serial treatment
380 virtual void setSurface
381 (
382 const pointField& points,
383 const faceList& faces,
384 bool parallel
385 );
386
387 //- Change association with a surface, expire the writer
388 //- with the current parallel/serial treatment
389 virtual void setSurface
390 (
391 const meshedSurf& surf
392 );
393
394 //- Change association with a surface, expire the writer
395 //- with the current parallel/serial treatment
396 virtual void setSurface
397 (
398 const pointField& points,
399 const faceList& faces
400 );
401
402
403 // Queries, Access
404
405 //- Test if outputPath has been set
406 inline bool is_open() const noexcept;
407
408 //- Writer is associated with a surface
409 bool hasSurface() const;
410
411 //- The surface to write is empty if the global number of faces is zero
412 bool empty() const;
413
414 //- The global number of faces for the associated surface
415 label size() const;
416
417 //- The number of expected output fields.
418 // Currently only used by the legacy VTK format.
419 inline label nFields() const noexcept;
420
421 //- Set the number of expected output fields
422 // Currently only used by the legacy VTK format.
423 // \return old value
424 inline label nFields(const label n) noexcept;
425
426 //- Are the field data to be treated as point data?
427 inline bool isPointData() const noexcept;
428
429 //- Set handling of field data to face/point data
430 // \return old value
431 inline bool isPointData(const bool on) noexcept;
432
433 //- Should a time directory be spliced into the output path?
434 inline bool useTimeDir() const noexcept;
435
436 //- Enable/disable use of spliced output path
437 // \return old value
438 inline bool useTimeDir(const bool on) noexcept;
439
440 //- Get output verbosity
441 inline bool verbose() const noexcept;
442
443 //- Enable/disable verbose output
444 // \return old value
445 inline bool verbose(const bool on) noexcept;
446
447 //- The current value of the point merge dimension (metre)
448 inline scalar mergeDim() const noexcept;
449
450 //- Change the point merge dimension (metre)
451 // \return old value
452 inline scalar mergeDim(const scalar dist) noexcept;
453
454 //- The current value of the geometry scaling
455 inline scalar scale() const noexcept;
456
457 //- Change the geometry scaling
458 // \return old value
459 inline scalar scale(const scalar factor) noexcept;
460
461 //- The current (cartesian) coordinate system transformation
462 inline const coordSystem::cartesian& transform() const noexcept;
463
464
465 // Time
466
467 //- True if there is a known time
468 inline bool hasTime() const;
469
470 //- The current time value/name
471 inline const word& timeName() const;
472
473 //- The current time value/name
474 inline scalar timeValue() const;
475
476
477 //- Set the current time
478 void setTime(const instant& inst);
479
480 //- Set current time from timeValue, auto generating the name
481 void setTime(scalar timeValue);
482
483 //- Set current time from timeValue and timeName
484 void setTime(scalar timeValue, const word& timeName);
485
486 //- Clear the current time
487 void unsetTime();
488
489
490 //- Begin a time-step
491 virtual void beginTime(const Time& t);
492
493 //- Begin a time-step
494 virtual void beginTime(const instant& inst);
495
496 //- End a time-step
497 virtual void endTime();
498
499
500 // Output
501
502 //- Open for output on specified path, using existing surface
503 virtual void open(const fileName& outputPath);
504
505 //- Open from components
506 virtual void open
507 (
508 const pointField& points,
509 const faceList& faces,
510 const fileName& outputPath,
511 bool parallel
512 );
513
514 //- Open from components
515 virtual void open
516 (
517 const meshedSurf& surf,
518 const fileName& outputPath,
519 bool parallel
520 );
521
522 //- Open from components, with the current parallel/serial treatment
523 virtual void open
524 (
525 const pointField& points,
526 const faceList& faces,
527 const fileName& outputPath
528 );
529
530 //- Open from components, with the current parallel/serial treatment
531 virtual void open
532 (
533 const meshedSurf& surf,
534 const fileName& outputPath
535 );
536
537 //- Finish output, performing any necessary cleanup
538 virtual void close();
539
540
541 // Write
542
543 //- Write separate surface geometry to file.
544 virtual fileName write() = 0;
545
546
547#undef declareSurfaceWriterWriteMethod
548#define declareSurfaceWriterWriteMethod(Type) \
549 \
550 virtual fileName write \
551 ( \
552 const word& fieldName, \
553 const Field<Type>& values \
554 ) = 0
555
562
563
564#undef declareSurfaceWriterWriteMethod
565#define declareSurfaceWriterWriteMethod(Type) \
566 \
567 \
568 virtual fileName write \
569 ( \
570 const word& fieldName, \
571 const Field<Type>& values \
572 ) // override
574
575 // Other IO
576
577 //- Return info proxy.
578 virtual InfoProxy<surfaceWriter> info() const
579 {
580 return *this;
581 }
582
583 //- Output info proxy
584 friend Ostream& operator<<
588 );
591 // Housekeeping
592
593 #ifdef Foam_surfaceWriter_directAccess
595 //- Access number of expected output fields.
596 label& nFields() { return nFields_; }
597
598 //- Access handling of face/point data
599 bool& isPointData() { return isPointData_; }
600
601 //- Access handling of spliced output path
602 bool& useTimeDir() { return useTimeDir_; }
603
604 //- Access output verbosity
605 bool& verbose() { return verbose_; }
606
607 //- Access value of the point merge dimension
608 scalar& mergeDim() { return mergeDim_; }
609
610 #endif
611};
612
614// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
615
616} // End namespace Foam
617
618// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
619
620#include "surfaceWriterI.H"
621
622// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
623
624#endif
625
626// ************************************************************************* //
label n
Info<< nl<< "Wrote faMesh in vtk format: "<< writer.output().name()<< nl;}{ vtk::lineWriter writer(aMesh.points(), aMesh.edges(), fileName(aMesh.mesh().time().globalPath()/"finiteArea-edges"));writer.writeGeometry();writer.beginCellData(4);writer.writeProcIDs();{ Field< scalar > fld(faMeshTools::flattenEdgeField(aMesh.magLe(), true))
Generic templated field type.
Definition: Field.H:82
A helper class for outputting values to Ostream.
Definition: InfoProxy.H:52
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:80
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
A Cartesian coordinate system.
Definition: cartesianCS.H:72
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
A class for handling file names.
Definition: fileName.H:76
static const fileName null
An empty fileName.
Definition: fileName.H:102
An instant of time. Contains the time value and name. Uses Foam::Time when formatting the name.
Definition: instant.H:56
Simple class to manage surface merging information.
Definition: mergedSurf.H:55
Implements a meshed surface by referencing another meshed surface or faces/points components.
Definition: meshedSurfRef.H:56
Abstract definition of a meshed surface defined by faces and points.
Definition: meshedSurf.H:50
Base class for surface writers.
surfaceWriter()
Default construct.
coordSystem::cartesian geometryTransform_
Local coordinate system transformation.
virtual void open(const fileName &outputPath)
Open for output on specified path, using existing surface.
meshedSurfRef surf_
Reference to surface or surface components.
virtual void endTime()
End a time-step.
virtual bool enabled() const
scalar geometryScale_
Output geometry scaling after rotate/translate.
virtual fileName write()=0
Write separate surface geometry to file.
bool useTimeDir_
Insert additional time sub-directory in the output path.
static autoPtr< surfaceWriter > New(const word &writeType)
Return a reference to the selected surfaceWriter.
Definition: surfaceWriter.C:64
bool wroteGeom_
Track if geometry has been written since the last open.
bool isPointData_
Is point vs cell data.
bool useTimeDir() const noexcept
Should a time directory be spliced into the output path?
meshedSurfRef adjustedSurf_
The surface after point coordinate transforms and scaling.
const meshedSurf & surface() const
bool isPointData() const noexcept
Are the field data to be treated as point data?
const word & timeName() const
The current time value/name.
fileName writeTemplate(const word &fieldName, const Field< Type > &localValues)
Dummy templated write operation.
void checkOpen() const
Verify that the outputPath_ has been set or FatalError.
void unsetTime()
Clear the current time.
scalar scale() const noexcept
The current value of the geometry scaling.
virtual void beginTime(const Time &t)
Begin a time-step.
label size() const
The global number of faces for the associated surface.
label nFields_
The number of fields.
static scalar defaultMergeDim
The default merge dimension (1e-8)
virtual ~surfaceWriter()
Destructor. Calls close()
virtual void setSurface(const meshedSurf &surf, bool parallel)
virtual void close()
Finish output, performing any necessary cleanup.
bool parallel_
Writing in parallel (via master)
bool empty() const
The surface to write is empty if the global number of faces is zero.
virtual InfoProxy< surfaceWriter > info() const
Return info proxy.
bool upToDate_
The topology/surface is up-to-date?
dictionary fieldLevel_
Field level to remove (on output)
static bool supportedType(const word &writeType)
True if New is likely to succeed for this writeType.
Definition: surfaceWriter.C:52
bool is_open() const noexcept
Test if outputPath has been set.
void setTime(const instant &inst)
Set the current time.
declareRunTimeSelectionTable(autoPtr, surfaceWriter, wordDict,(const dictionary &writeOpts),(writeOpts))
virtual bool merge() const
mergedSurf mergedSurf_
Surface after merging (parallel)
bool verbose_
Additional output verbosity.
tmp< Field< Type > > adjustFieldTemplate(const word &fieldName, const tmp< Field< Type > > &tfield) const
Apply refLevel and fieldScaling.
dictionary fieldScale_
Field scaling (on output)
virtual bool expire()
virtual bool usesFaceIds() const
True if the writer format uses faceIds as part of its output.
virtual bool needsUpdate() const
Does the writer need an update (eg, lagging behind surface changes)
tmp< Field< Type > > mergeFieldTemplate(const Field< Type > &fld) const
Gather (merge) fields with renumbering and shrinking for point data.
instant currTime_
The current time value/name.
virtual bool separateGeometry() const
True if the surface format requires geometry in a separate file.
bool verbose() const noexcept
Get output verbosity.
bool hasSurface() const
Writer is associated with a surface.
const meshedSurfRef & adjustSurface() const
declareRunTimeSelectionTable(autoPtr, surfaceWriter, word,(),())
virtual void clear()
scalar timeValue() const
The current time value/name.
virtual bool wroteData() const
Geometry or fields written since the last open?
scalar mergeDim() const noexcept
The current value of the point merge dimension (metre)
bool hasTime() const
True if there is a known time.
fileName outputPath_
The full output directory and file (surface) name.
const coordSystem::cartesian & transform() const noexcept
The current (cartesian) coordinate system transformation.
label nFields() const noexcept
The number of expected output fields.
scalar mergeDim_
Dimension for merging.
TypeName("surfaceWriter")
Runtime type information.
A class for managing temporary objects.
Definition: tmp.H:65
A class for handling words, derived from Foam::string.
Definition: word.H:68
OBJstream os(runTime.globalPath()/outputName)
const pointField & points
Namespace for OpenFOAM.
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:44
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
const direction noexcept
Definition: Scalar.H:223
List< face > faceList
A List of faces.
Definition: faceListFwd.H:47
Macros to ease declaration of run-time selection tables.
#define declareRunTimeSelectionTable(ptrWrapper, baseType, argNames, argList, parList)
Declare a run-time selection (variables and adder classes)
#define declareSurfaceFieldMethods(Type)
#define declareSurfaceWriterWriteMethod(Type)
#define TypeName(TypeNameString)
Declare a ClassName() with extra virtual type info.
Definition: typeInfo.H:73