Loading...
Searching...
No Matches
fileOperation.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) 2017 OpenFOAM Foundation
9 Copyright (C) 2020-2025 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
27Class
28 Foam::fileOperation
29
30Description
31 An encapsulation of filesystem-related operations.
32
33 Several of the file handlers can be configured to use specific ranks for
34 IO operations. These can either be defined from the command-line
35 \c -ioRanks option, or via the \c FOAM_ENV environment variable.
36 In either case, the list of IO ranks shall always include the value
37 \b 0 (master rank). The ranks may be specified as a plain list, or as an
38 OpenFOAM list. The special value \em host can be used to specify selection
39 based on hostname.
40
41 For example,
42 \verbatim
43 FOAM_IORANKS='0 4 8' decomposePar -fileHandler hostCollated
44 FOAM_IORANKS='0,4,8' decomposePar -fileHandler hostCollated
45 FOAM_IORANKS='(0 4 8)' decomposePar -fileHandler hostCollated
46 \endverbatim
47 will generate
48 \verbatim
49 processors12_0-3/ : containing data for processors 0 to 3
50 processors12_4-7/ : containing data for processors 4 to 7
51 processors12_8-11/ : containing data for processors 8 to 11
52 \endverbatim
53
54 The value \em host can be used to specify a single IO rank for each
55 host. For example,
56 \verbatim
57 decomposePar -fileHandler collated -ioRanks host
58 decomposePar -fileHandler hostCollated
59 \endverbatim
60
61Environment
62 - \c FOAM_ENV : list of io-ranks as plain space or comma separated
63 list or as an OpenFOAM formatted list. Eg, '(0 4 8)'
64
65Namespace
66 Foam::fileOperations
67
68Description
69 Namespace for implementations of a fileOperation
70
71\*---------------------------------------------------------------------------*/
72
73#ifndef Foam_fileOperation_H
74#define Foam_fileOperation_H
75
76#include "ISstream.H"
77#include "Ostream.H"
78#include "UPstream.H"
79#include "fileMonitor.H"
80#include "fileNameList.H"
81#include "instantList.H"
82#include "refPtr.H"
83#include "bitSet.H"
84#include "Enum.H"
85#include "Tuple2.H"
86#include "InfoProxy.H"
87
88// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
89
90namespace Foam
91{
92
93// Forward Declarations
94class fileOperation;
95class objectRegistry;
96class regIOobject;
97class IOobject;
98class Time;
99
101
102/*---------------------------------------------------------------------------*\
103 Class fileOperation Declaration
104\*---------------------------------------------------------------------------*/
105
106class fileOperation
107{
108public:
110 // Public Data Types
112 //- Enumeration for the location of an IOobject
113 enum pathType : int
115 NOTFOUND = 0,
116 ABSOLUTE,
117 OBJECT,
119
120 // NOTE: increasing precedence (uncollated, collated, rank-collated)
121
134 };
135 static const Enum<pathType> pathTypeNames_;
137 //- Augment fileName with pathType and local offset
140
141 //- For addressing a range of processors (an int range)
143
144
145private:
146
147 // Private Static Data
148
149 //- Storage of the dummy file handler (demand-driven)
150 static refPtr<fileOperation> dummyHandlerPtr_;
151
152 //- Filtering of processorsDDD directories (in parallel):
153 // - >0 : only accept processorsDDD (default)
154 // - 0 : accept anything (e.g. when detecting nprocs from
155 // directory naming)
156 // - -1 : use nProcs(UPstream::worldComm)
157 static int nProcsFilter_;
158
159
160protected:
161
162 // Protected Static Data
163
164 //- Cache level (eg, for caching time directories). Default: 1
165 static int cacheLevel_;
166
167
168 // Protected Data
169
170 //- Communicator to use
171 mutable label comm_;
173 //- Overall number of processors.
174 // Used to synthesise processor directory naming:
175 // - parallel: UPstream::nProcs(UPstream::commWorld())
176 // - non-parallel: detected from processor dir naming ('processorsNN')
177 label nProcs_;
178
179 //- Distributed roots (parallel run)
180 mutable bool distributed_;
181
182 //- The list of IO ranks (global ranks)
183 // Primarily for additional bookkeeping in non-parallel
184 const labelList ioRanks_;
185
186 //- Detected processors directories
188
189 //- File-change monitor for all registered files
190 mutable std::unique_ptr<fileMonitor> monitorPtr_;
191
192
193 // Protected Member Functions
195 //- Get or create fileMonitor singleton
196 fileMonitor& monitor() const;
197
198 //- Merge two times
199 static void mergeTimes
200 (
201 const UList<instant>& extraTimes,
203 const word& constantName,
205 instantList& times
206 );
207
208 //- Helper: check for file (isFile) or directory (!isFile)
209 static bool isFileOrDir(const bool isFile, const fileName&);
210
211 //- Lookup name of processorsDDD using cache.
212 // \return empty fileName if not found.
214 (
215 const fileName& objectPath,
216 const bool syncPar
217 ) const;
218
219 //- Lookup name of processorsDDD using cache.
220 // \note To be called on all processors
221 // \return empty fileName if not found.
223 (
224 const fileName& objectPath
225 ) const;
226
227 //- Does IOobject exist?
228 //- Is either a directory (empty name()) or a file
229 bool exists(IOobject& io) const;
230
231
232 //- Is proci a master rank in the communicator (in parallel)
233 //- or a master rank in the IO ranks (non-parallel)
234 bool isIOrank(const label proci) const;
235
236 //- Helper: output which ranks are IO
237 void printRanks() const;
238
239
240 //- Construction helper: check for locally allocated communicator
241 static inline label getManagedComm(const label communicator)
242 {
243 return
244 (
245 (
246 communicator < 0
247 || communicator == UPstream::commGlobal()
248 || communicator == UPstream::commSelf()
249 || communicator == UPstream::commWorld()
250 )
251 ? -1
252 : communicator
253 );
254 }
255
256
257private:
258
259 // Private Member Functions
260
261 //- Low-level implementation for subsetting 'clone' operation
262 static autoPtr<fileOperation> New_impl
263 (
264 const fileOperation& origHandler,
266 const labelUList& subProcs,
267 bool verbose
268 );
269
270
271public:
272
273 // Static Data
274
275 //- Return the processors directory name (usually "processors")
276 static word processorsBaseDir;
277
278 //- Name of the default fileHandler
280
281
282 // Public Data Types
283
284 //- Runtime type information
285 TypeName("fileOperation");
286
287
288 //- The currently active file handler. Avoid accessing directly
290
291 //- Reference to a dummy file handler.
293
294
295 // Constructors
296
297 //- Construct from communicator,
298 //- optionally with specified io-ranks and/or distributed roots
299 explicit fileOperation
300 (
301 const label comm,
303 const bool distributedRoots = false
304 );
305
306 //- Construct from communicator with specified io-ranks
307 explicit fileOperation
308 (
309 const Tuple2<label, labelList>& commAndIORanks,
310 const bool distributedRoots = false
311 );
312
313
314 // Declare run-time constructor selection table
315
317 (
318 autoPtr,
320 word,
322 bool verbose
323 ),
324 (verbose)
325 );
328 (
329 autoPtr,
331 comm,
332 (
333 const Tuple2<label, labelList>& commAndIORanks,
334 const bool distributedRoots,
335 bool verbose
336 ),
337 (commAndIORanks, distributedRoots, verbose)
338 );
339
341 // Selectors
342
343 //- Select fileHandler-type.
344 //- Uses defaultFileHandler if the handlerType is empty.
346 (
347 const word& handlerType,
348 bool verbose = false
349 );
350
351 //- Select fileHandler-type.
352 //- Uses defaultFileHandler if the handlerType is empty.
354 (
355 const word& handlerType,
356 const Tuple2<label, labelList>& commAndIORanks,
357 const bool distributedRoots,
358 bool verbose = false
359 );
360
361
362 //- Replicate the given fileHandler properties with
363 //- a subset of (global) ranks.
364 // Always includes rank 0 and constrained by the global numProcs
365 // Returns nullptr if myProcNo is not involved.
367 (
369 const fileOperation& origHandler,
371 const boolUList& useProc,
372 bool verbose = false
373 );
374
375 //- Replicate the given fileHandler properties with
376 //- a subset of (global) ranks.
377 // Always includes rank 0 and constrained by the global numProcs
378 // Returns nullptr if myProcNo is not involved.
380 (
382 const fileOperation& origHandler,
384 const bitSet& useProc,
385 bool verbose = false
386 );
387
388
389 //- Destructor
390 virtual ~fileOperation() = default;
391
392
393 // Factory Methods, Singleton-type Functions
394
395 //- The commonly used uncollatedFileOperation
397
398 //- Return the current file handler.
399 //- Will create the default file handler if necessary.
400 static const fileOperation& fileHandler();
401
402 //- Delete current file handler.
403 // \returns the old handler.
404 // Should have [[nodiscard]], but gcc ignores void casting.
405 static refPtr<fileOperation> fileHandler(std::nullptr_t);
406
407 //- Replace the current file handler.
408 // The following are considered no-ops:
409 // - an empty/invalid newHandler does \b not delete, use a literal
410 // \c nullptr (std::nullptr_t) for that
411 // - if new handler and current handler are identical (same pointer).
412 // .
413 // \returns the old handler (on change), nullptr otherwise
414 // Should have [[nodiscard]], but gcc ignores void casting.
416 (
417 refPtr<fileOperation>& newHandler
418 );
419
420 //- Replace the current file handler.
421 // The following are considered no-ops:
422 // - an empty/invalid newHandler does \b not delete, use a literal
423 // \c nullptr (std::nullptr_t) for that
424 // - if new handler and current handler are identical (same pointer).
425 // .
426 // \returns the old handler (on change), nullptr otherwise
427 // Should have [[nodiscard]], but gcc ignores void casting.
429 (
430 refPtr<fileOperation>&& newHandler
431 );
432
433 //- Replace the current file handler.
434 // The following are considered no-ops:
435 // - an empty/invalid newHandler does \b not delete, use a literal
436 // \c nullptr (std::nullptr_t) for that
437 // - if new handler and current handler are identical (same pointer).
438 // .
439 // \returns the old handler (on change), nullptr otherwise
440 // Should have [[nodiscard]], but gcc ignores void casting.
442 (
443 autoPtr<fileOperation>&& newHandler
444 );
445
446
447 // Static Functions
448
449 //- Return cache level
450 static int cacheLevel() noexcept
451 {
452 return cacheLevel_;
453 }
454
455 //- Set cache level (0 = off). \return the previous value
456 static int cacheLevel(int level) noexcept
457 {
458 int old(cacheLevel_);
459 cacheLevel_ = level;
460 return old;
461 }
462
463 //- Return collated 'processorsDDD' filtering
464 static int nProcsFilter() noexcept
465 {
466 return nProcsFilter_;
467 }
468
469 //- Set collated 'processorsDDD' filtering (0 = off).
470 // \return the previous value
471 static int nProcsFilter(int level) noexcept
472 {
473 int old(nProcsFilter_);
474 nProcsFilter_ = level;
475 return old;
476 }
477
478 //- Sort directory entries according to time value,
479 // with "constant" appearing first (if it exists)
481 (
482 const fileNameList& dirEntries,
483 const word& constantName = "constant"
484 );
485
486 //- True if the file names are identical. False on an empty list
487 static bool uniformFile(const fileNameList& names);
488
489 //- True if the file name is identical on all ranks
490 static bool uniformFile(const label comm, const fileName& name);
491
492
493 // Member Functions
494
495 // Characteristics
496
497 //- Communicator to use
498 label comm() const noexcept
499 {
500 return comm_;
501 }
502
503 //- Set communicator to use [mutable]. Negative values are a no-op.
504 // \return old value
505 label comm(label communicator) const noexcept
506 {
507 label old(comm_);
508 if (communicator >= 0) comm_ = communicator;
509 return old;
510 }
511
512 //- Distributed roots (parallel run)
513 bool distributed() const noexcept
514 {
515 return distributed_;
516 }
517
518 //- Set distributed roots on/off [mutable]
519 // \return old value
520 bool distributed(bool on) const noexcept
521 {
522 bool old(distributed_);
523 distributed_ = on;
524 return old;
525 }
526
527 //- The list of IO ranks (global ranks)
528 // Primarily for additional bookkeeping in non-parallel
529 const labelList& ioRanks() const noexcept { return ioRanks_; }
530
531 //- Return info proxy,
532 //- used to print information to a stream
533 InfoProxy<fileOperation> info() const noexcept { return *this; }
534
535
536 // Member Functions
538 //- True if the fileOperation can be considered valid.
539 //- At the moment, primarily used to detect the dummy fileOperation.
540 virtual bool good() const { return true; }
541
542 //- Transfer ownership of communicator to this fileOperation.
543 //- Use with caution
544 virtual void storeComm() const = 0;
546
547 // OSSpecific equivalents
548
549 //- Make directory
550 virtual bool mkDir(const fileName&, mode_t=0777) const = 0;
551
552 //- Set the file mode
553 virtual bool chMod(const fileName&, const mode_t) const = 0;
554
555 //- Return the file mode
556 virtual mode_t mode
557 (
558 const fileName&,
559 const bool followLink = true
560 ) const = 0;
561
562 //- Return the file type: DIRECTORY, FILE or SYMLINK
563 virtual fileName::Type type
564 (
565 const fileName&,
566 const bool followLink = true
567 ) const = 0;
568
569 //- Does the name exist (as DIRECTORY or FILE) in the file system?
570 // Optionally enable/disable check for gzip file.
571 virtual bool exists
572 (
573 const fileName&,
574 const bool checkGzip = true,
575 const bool followLink = true
576 ) const = 0;
577
578 //- Does the name exist as a DIRECTORY in the file system?
579 virtual bool isDir
580 (
581 const fileName&,
582 const bool followLink = true
583 ) const = 0;
584
585 //- Does the name exist as a FILE in the file system?
586 // Optionally enable/disable check for gzip file.
587 virtual bool isFile
588 (
589 const fileName&,
590 const bool checkGzip = true,
591 const bool followLink = true
592 ) const = 0;
593
594 //- Return size of file
595 virtual off_t fileSize
596 (
597 const fileName&,
598 const bool followLink = true
599 ) const = 0;
600
601 //- Return time of last file modification
602 virtual time_t lastModified
603 (
604 const fileName&,
605 const bool followLink = true
606 ) const = 0;
607
608 //- Return time of last file modification
609 virtual double highResLastModified
610 (
611 const fileName&,
612 const bool followLink = true
613 ) const = 0;
614
615 //- Read a directory and return the entries as a string list
616 virtual fileNameList readDir
617 (
618 const fileName&,
620 const bool filtergz=true,
621 const bool followLink = true
622 ) const = 0;
623
624 //- Copy, recursively if necessary, the source to the destination
625 virtual bool cp
626 (
627 const fileName& src,
628 const fileName& dst,
629 const bool followLink = true
630 ) const = 0;
632 //- Create a softlink. dst should not exist. Returns true if
633 // successful.
634 virtual bool ln(const fileName& src, const fileName& dst) const = 0;
635
636 //- Rename src to dst
637 virtual bool mv
638 (
639 const fileName& src,
640 const fileName& dst,
641 const bool followLink = false
642 ) const = 0;
644 //- Rename to a corresponding backup file
645 // If the backup file already exists, attempt with
646 // "01" .. "99" suffix
647 virtual bool mvBak
648 (
649 const fileName&,
650 const std::string& ext = "bak"
651 ) const = 0;
652
653 //- Remove a file, returning true if successful otherwise false
654 virtual bool rm(const fileName&) const = 0;
655
656 //- Remove a directory and its contents
657 // \param dir the directory to remove
658 // \param silent do not report missing directory
659 // \param emptyOnly only remove empty directories (recursive)
660 virtual bool rmDir
661 (
662 const fileName& dir,
663 const bool silent = false,
664 const bool emptyOnly = false
665 ) const = 0;
666
667 //- Read dir/file (recursively if necessary) on master of the
668 //- communicator, send and write contents to all 'writeOnProc'
669 //- processors with local file name
670 // \param comm the communicator for broadcasting
671 // \param writeOnProc write on the processor
672 // \param src the source file/directory
673 // \param dst the target file/directory.
674 // If empty, treat as being identical to the src.
675 virtual bool broadcastCopy
676 (
677 const label comm,
678 const bool writeOnProc,
679 const fileName& src,
680 const fileName& dst
681 // always recreates links
682 ) const;
683
684
685 // (reg)IOobject functionality
686
687 //- Generate disk file name for object. Opposite of filePath.
688 virtual fileName objectPath
689 (
690 const IOobject& io,
692 const word& typeName
693 ) const;
694
695 //- Search for an object
696 virtual fileName filePath
697 (
699 const bool checkGlobal,
700 const IOobject&,
703 const bool search = true
704 ) const = 0;
705
706 //- Search for a directory
707 virtual fileName dirPath
708 (
710 const bool checkGlobal,
711 const IOobject& io,
712 const bool search = true
713 ) const = 0;
714
715 //- Search directory for objects. Used in IOobjectList.
717 (
718 const objectRegistry& db,
719 const fileName& instance,
720 const fileName& local,
721 word& newInstance
722 ) const;
724 //- Read object header from supplied file
725 virtual bool readHeader
726 (
727 IOobject&,
728 const fileName&,
729 const word& typeName
730 ) const = 0;
731
732 //- Reads header for regIOobject and returns an ISstream
733 // to read the contents.
735 (
737 const fileName&,
738 const word& typeName,
739 const bool readOnProc = true
740 ) const = 0;
741
742 //- Top-level read
743 virtual bool read
744 (
746 const bool masterOnly,
750 const word& typeName
751 ) const = 0;
752
753 //- Writes a regIOobject (so header, contents and divider).
754 // Returns success state. Default action is to write to
755 // the objectPath using writeData. If !writeOnProc the
756 // file does not need to be written (this is used e.g. to
757 // suppress empty local lagrangian data)
758 virtual bool writeObject
759 (
761 IOstreamOption streamOpt = IOstreamOption(),
762 const bool writeOnProc = true
763 ) const;
764
765
766 // Filename (not IOobject) operations
767
768 //- Search for a file or directory.
769 //- Use IOobject version in preference
770 virtual fileName filePath
772 const fileName&,
773 const bool checkGzip = true,
774 const bool followLink = true
775 ) const;
776
777 //- Generate an ISstream that reads a file
778 virtual autoPtr<ISstream> NewIFstream(const fileName&) const = 0;
779
780 //- Generate an OSstream that writes a file
782 (
783 const fileName& pathname,
784 IOstreamOption streamOpt = IOstreamOption(),
785 const bool writeOnProc = true
786 ) const = 0;
787
788 //- Generate an OSstream that writes a file
790 (
792 const fileName& pathname,
793 IOstreamOption streamOpt = IOstreamOption(),
794 const bool writeOnProc = true
795 ) const = 0;
796
797
798 // File modification checking
799
800 //- Add watching of a file. Returns handle
801 virtual label addWatch(const fileName&) const;
802
803 //- Remove watch on a file (using handle)
804 virtual bool removeWatch(const label) const;
805
806 //- Find index (or -1) of file in list of handles
807 virtual label findWatch
808 (
809 const labelList& watchIndices,
810 const fileName&
811 ) const;
812
813 //- Helper: add watches for list of regIOobjects
814 virtual void addWatches(regIOobject&, const fileNameList&) const;
815
816 //- Get name of file being watched (using handle)
817 virtual fileName getFile(const label) const;
818
819 //- Update state of all files
820 virtual void updateStates
821 (
822 const bool masterOnly,
823 const bool syncPar
824 ) const;
825
826 //- Get current state of file (using handle)
827 virtual fileMonitor::fileState getState(const label) const;
828
829 //- Set current state of file (using handle) to unmodified
830 virtual void setUnmodified(const label) const;
831
832
833 // Other
834
835 //- Actual name of processors dir
836 //- (for use in mode PROCOBJECT, PROCINSTANCE)
837 virtual word processorsDir(const IOobject& io) const
838 {
839 return processorsBaseDir;
840 }
841
842 //- Actual name of processors dir
843 //- (for use in mode PROCOBJECT, PROCINSTANCE)
844 virtual word processorsDir(const fileName&) const
845 {
846 return processorsBaseDir;
847 }
848
849 //- Overall number of processors,
850 //- from UPstream::nProcs() or detected from directories/results.
851 label nProcs() const noexcept { return nProcs_; }
852
853 //- Set number of processor directories/results.
854 // Used to cache format of e.g. processorsDDD.
855 // Returns old number of processors.
856 // Only used in decomposePar
857 label nProcs(const label numProcs) noexcept
858 {
859 label old(nProcs_);
860 nProcs_ = numProcs;
861 return old;
863
864 //- Get number of processor directories/results.
865 // Used for e.g. reconstructPar, argList checking
866 virtual label nProcs
867 (
868 const fileName& dir,
869 const fileName& local = "",
871 const label wantedNProcs = 0
872 ) const;
873
874 //- Get sorted list of times
876 (
878 const fileName& directory,
880 const word& constantName = "constant"
881 ) const;
882
883 //- Find time instance where IOobject is located.
884 //- The name of the IOobject can be empty, in which case only the
885 //- IOobject::local() is checked.
886 //- Does not search beyond \c stopInstance (if set) or \c constant.
887 // If the instance cannot be found:
888 // - FatalError when readOpt is (MUST_READ or READ_MODIFIED)
889 // - returns the \c stopInstance (if set and reached)
890 // - return \c constant if constant_fallback is true.
891 // - return an empty word if constant_fallback is false.
892 // .
893 virtual IOobject findInstance
894 (
895 const IOobject& io,
896 const scalar startValue,
898 const word& stopInstance = "",
900 const bool constant_fallback = true
901 ) const;
902
903 //- Callback for time change
904 virtual void setTime(const Time&) const
905 {}
906
907 //- Forcibly wait until all output done. Flush any cached data
908 virtual void flush() const;
910 //- Forcibly parallel sync
911 virtual void sync();
912
913 //- Generate path (like io.path) from root+casename with any
914 // 'processorXXX' replaced by procDir (usually 'processsors')
916 (
917 const IOobject& io,
918 const word& procDir
919 ) const;
921 //- Generate path (like io.path) with provided instance and any
922 // 'processorXXX' replaced by procDir (usually 'processsors')
924 (
925 const IOobject& io,
926 const word& instance,
927 const word& procDir
928 ) const;
929
930 //- Operating on fileName: replace processorXXX with procDir
931 fileName processorsPath(const fileName&, const word& procDir) const;
932
933 //- Split objectPath into part before 'processor' and part after.
934 //
935 // Returns -1 or processor number and optionally number
936 // of processors. Use with care.
937 // - path/"processor"+Foam::name(proci)/local reconstructs input
938 // - path/"processors"+Foam::name(nProcs)/local reconstructs
939 // collated processors equivalence
940 static label splitProcessorPath
941 (
942 const fileName& objectPath,
944 fileName& path,
946 fileName& procDir,
950 procRangeType& group,
952 label& nProcs
953 );
954
955 //- Detect processor number from '/aa/bb/processorDDD/cc'
956 static label detectProcessorPath(const fileName& objPath);
957
958 //- Detect processor number from 'path/processorDDD/abc'
959 //- or 'path/processorsNN/abc', 'path/processorsNN_0-10/abc'
960 static label detectProcessorPath
961 (
962 const fileName& objPath,
964 procRangeType& group,
966 label* numProcs = nullptr
967 );
968
969
970 // Rank selection/sub-selection
971
972 //- Get (contiguous) range/bounds of ranks addressed
973 //- within the given main io-ranks.
974 static labelRange subRanks(const labelUList& mainIOranks);
975
976 //- Get list of global IO master ranks based on the hostname.
977 //- It is assumed that each host range is contiguous.
979
980 //- Get list of global IO ranks from FOAM_IORANKS env variable.
981 //- If set, these correspond to the IO master ranks.
983
984
985 // Housekeeping
986
987 //- Same as nProcs
988 label setNProcs(label numProcs) { return nProcs(numProcs); }
989};
990
991
992//- Read pathType as an integer value
993inline Istream& operator>>(Istream& is, fileOperation::pathType& b)
994{
995 int val(0);
996 is >> val;
997
998 b = static_cast<fileOperation::pathType>(val);
999 return is;
1000}
1001
1002//- Write pathType as an integer value
1004{
1006 return os;
1007}
1008
1009
1010// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
1011
1012//- Return the current file handler
1013//- (will create default file handler if necessary).
1014//- Forwards to fileOperation::handler()
1015inline const fileOperation& fileHandler()
1016{
1018}
1019
1020//- Delete current file handler - forwards to fileOperation::handler()
1021// Should have [[nodiscard]], but gcc ignores void casting.
1022inline refPtr<fileOperation> fileHandler(std::nullptr_t)
1023{
1024 return fileOperation::fileHandler(nullptr);
1025}
1026
1027//- Replace the current file handler - forwards to fileOperation::handler().
1028// \note legacy behaviour, so returns autoPtr instead of refPtr!
1030
1031
1032// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
1033
1034} // End namespace Foam
1035
1036// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
1037
1038#endif
1039
1040// ************************************************************************* //
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition Enum.H:57
A HashTable similar to std::unordered_map.
Definition HashTable.H:124
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition IOobject.H:191
A simple container for options an IOstream can normally have.
streamFormat
Data format (ascii | binary | coherent).
atomicType
Atomic operations (output).
A helper class for outputting values to Ostream.
Definition InfoProxy.H:49
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition List.H:72
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition Time.H:75
A 2-tuple for storing two objects of dissimilar types. The container is similar in purpose to std::pa...
Definition Tuple2.H:51
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition UList.H:89
static const UList< label > & null() noexcept
Definition UList.H:225
static label commWorld() noexcept
Communicator for all ranks (respecting any local worlds).
Definition UPstream.H:1101
static constexpr int commSelf() noexcept
Communicator within the current rank only.
Definition UPstream.H:1088
IntRange< int > rangeType
Int ranges are used for MPI ranks (processes).
Definition UPstream.H:75
static constexpr int commGlobal() noexcept
Communicator for all ranks, irrespective of any local worlds.
Definition UPstream.H:1081
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition autoPtr.H:65
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition bitSet.H:61
Checking for changes to files.
Definition fileMonitor.H:62
fileState
Enumeration defining the file state.
Definition fileMonitor.H:71
A class for handling file names.
Definition fileName.H:75
Type
Enumerations to handle directory entry types.
Definition fileName.H:82
@ FILE
A regular file.
Definition fileName.H:84
An encapsulation of filesystem-related operations.
virtual void addWatches(regIOobject &, const fileNameList &) const
Helper: add watches for list of regIOobjects.
static bool isFileOrDir(const bool isFile, const fileName &)
Helper: check for file (isFile) or directory (!isFile).
static const fileOperation & fileHandler()
Return the current file handler. Will create the default file handler if necessary.
static refPtr< fileOperation > fileHandlerPtr_
The currently active file handler. Avoid accessing directly.
static labelRange subRanks(const labelUList &mainIOranks)
Get (contiguous) range/bounds of ranks addressed within the given main io-ranks.
bool distributed(bool on) const noexcept
Set distributed roots on/off [mutable].
virtual bool chMod(const fileName &, const mode_t) const =0
Set the file mode.
std::unique_ptr< fileMonitor > monitorPtr_
File-change monitor for all registered files.
static int nProcsFilter(int level) noexcept
Set collated 'processorsDDD' filtering (0 = off).
static label getManagedComm(const label communicator)
Construction helper: check for locally allocated communicator.
virtual mode_t mode(const fileName &, const bool followLink=true) const =0
Return the file mode.
virtual bool isFile(const fileName &, const bool checkGzip=true, const bool followLink=true) const =0
Does the name exist as a FILE in the file system?
virtual autoPtr< OSstream > NewOFstream(const fileName &pathname, IOstreamOption streamOpt=IOstreamOption(), const bool writeOnProc=true) const =0
Generate an OSstream that writes a file.
bool distributed() const noexcept
Distributed roots (parallel run).
TypeName("fileOperation")
Runtime type information.
virtual bool mvBak(const fileName &, const std::string &ext="bak") const =0
Rename to a corresponding backup file.
virtual instantList findTimes(const fileName &directory, const word &constantName="constant") const
Get sorted list of times.
virtual label addWatch(const fileName &) const
Add watching of a file. Returns handle.
Tuple2< fileName, Tuple2< pathType, int > > dirIndex
Augment fileName with pathType and local offset.
refPtr< dirIndexList > lookupAndCacheProcessorsPath(const fileName &objectPath, const bool syncPar) const
Lookup name of processorsDDD using cache.
static instantList sortTimes(const fileNameList &dirEntries, const word &constantName="constant")
Sort directory entries according to time value,.
virtual autoPtr< ISstream > NewIFstream(const fileName &) const =0
Generate an ISstream that reads a file.
virtual double highResLastModified(const fileName &, const bool followLink=true) const =0
Return time of last file modification.
virtual autoPtr< OSstream > NewOFstream(IOstreamOption::atomicType atomic, const fileName &pathname, IOstreamOption streamOpt=IOstreamOption(), const bool writeOnProc=true) const =0
Generate an OSstream that writes a file.
virtual bool exists(const fileName &, const bool checkGzip=true, const bool followLink=true) const =0
Does the name exist (as DIRECTORY or FILE) in the file system?
virtual bool rm(const fileName &) const =0
Remove a file, returning true if successful otherwise false.
virtual void flush() const
Forcibly wait until all output done. Flush any cached data.
const labelList ioRanks_
The list of IO ranks (global ranks).
virtual bool removeWatch(const label) const
Remove watch on a file (using handle).
bool exists(IOobject &io) const
Does IOobject exist? Is either a directory (empty name()) or a file.
virtual fileNameList readObjects(const objectRegistry &db, const fileName &instance, const fileName &local, word &newInstance) const
Search directory for objects. Used in IOobjectList.
virtual bool broadcastCopy(const label comm, const bool writeOnProc, const fileName &src, const fileName &dst) const
Read dir/file (recursively if necessary) on master of the communicator, send and write contents to al...
virtual off_t fileSize(const fileName &, const bool followLink=true) const =0
Return size of file.
static word defaultFileHandler
Name of the default fileHandler.
virtual label findWatch(const labelList &watchIndices, const fileName &) const
Find index (or -1) of file in list of handles.
static void mergeTimes(const UList< instant > &extraTimes, const word &constantName, instantList &times)
Merge two times.
label nProcs() const noexcept
Overall number of processors, from UPstream::nProcs() or detected from directories/results.
fileName processorsPath(const IOobject &io, const word &instance, const word &procDir) const
Generate path (like io.path) with provided instance and any.
virtual bool cp(const fileName &src, const fileName &dst, const bool followLink=true) const =0
Copy, recursively if necessary, the source to the destination.
label setNProcs(label numProcs)
Same as nProcs.
virtual fileName::Type type(const fileName &, const bool followLink=true) const =0
Return the file type: DIRECTORY, FILE or SYMLINK.
static refPtr< fileOperation > null()
Reference to a dummy file handler.
virtual bool ln(const fileName &src, const fileName &dst) const =0
Create a softlink. dst should not exist. Returns true if.
fileName processorsCasePath(const IOobject &io, const word &procDir) const
Generate path (like io.path) from root+casename with any.
virtual bool read(regIOobject &, const bool masterOnly, const IOstreamOption::streamFormat format, const word &typeName) const =0
Top-level read.
virtual refPtr< dirIndexList > lookupProcessorsPath(const fileName &objectPath) const
Lookup name of processorsDDD using cache.
label nProcs_
Overall number of processors.
const labelList & ioRanks() const noexcept
The list of IO ranks (global ranks).
virtual bool good() const
True if the fileOperation can be considered valid. At the moment, primarily used to detect the dummy ...
virtual bool readHeader(IOobject &, const fileName &, const word &typeName) const =0
Read object header from supplied file.
List< dirIndex > dirIndexList
static label splitProcessorPath(const fileName &objectPath, fileName &path, fileName &procDir, fileName &local, procRangeType &group, label &nProcs)
Split objectPath into part before 'processor' and part after.
static autoPtr< fileOperation > NewUncollated()
The commonly used uncollatedFileOperation.
virtual word processorsDir(const IOobject &io) const
Actual name of processors dir (for use in mode PROCOBJECT, PROCINSTANCE).
HashTable< dirIndexList > procsDirs_
Detected processors directories.
static label detectProcessorPath(const fileName &objPath)
Detect processor number from '/aa/bb/processorDDD/cc'.
virtual void storeComm() const =0
Transfer ownership of communicator to this fileOperation. Use with caution.
virtual void setTime(const Time &) const
Callback for time change.
pathType
Enumeration for the location of an IOobject.
@ PROCUNCOLLATEDINSTANCE
as PROCUNCOLLATED but with instance
@ WRITEOBJECT
write path exists
@ OBJECT
io.objectPath() exists
@ PROCINSTANCE
as PROCOBJECT but with instance
@ FINDINSTANCE
file found in time directory
@ PROCUNCOLLATED
objectPath exists in 'processorN'
@ ABSOLUTE
instance is absolute directory
@ PROCBASEINSTANCE
as PROCBASEOBJECT but with instance
@ PARENTOBJECT
parent of object path
@ PROCOBJECT
objectPath exists in 'processorsNN_first-last'
@ PROCBASEOBJECT
objectPath exists in 'processorsNN'
virtual bool rmDir(const fileName &dir, const bool silent=false, const bool emptyOnly=false) const =0
Remove a directory and its contents.
virtual bool mv(const fileName &src, const fileName &dst, const bool followLink=false) const =0
Rename src to dst.
bool isIOrank(const label proci) const
Is proci a master rank in the communicator (in parallel) or a master rank in the IO ranks (non-parall...
static int cacheLevel_
Cache level (eg, for caching time directories). Default: 1.
label comm_
Communicator to use.
virtual fileMonitor::fileState getState(const label) const
Get current state of file (using handle).
virtual void setUnmodified(const label) const
Set current state of file (using handle) to unmodified.
static labelList getGlobalIORanks()
Get list of global IO ranks from FOAM_IORANKS env variable. If set, these correspond to the IO master...
label comm() const noexcept
Communicator to use.
InfoProxy< fileOperation > info() const noexcept
Return info proxy, used to print information to a stream.
virtual void updateStates(const bool masterOnly, const bool syncPar) const
Update state of all files.
static int cacheLevel(int level) noexcept
Set cache level (0 = off).
static bool uniformFile(const fileNameList &names)
True if the file names are identical. False on an empty list.
virtual time_t lastModified(const fileName &, const bool followLink=true) const =0
Return time of last file modification.
void printRanks() const
Helper: output which ranks are IO.
static int cacheLevel() noexcept
Return cache level.
virtual IOobject findInstance(const IOobject &io, const scalar startValue, const word &stopInstance="", const bool constant_fallback=true) const
Find time instance where IOobject is located. The name of the IOobject can be empty,...
fileOperation(const label comm, const labelUList &ioRanks=labelUList::null(), const bool distributedRoots=false)
Construct from communicator, optionally with specified io-ranks and/or distributed roots.
static const Enum< pathType > pathTypeNames_
declareRunTimeSelectionTable(autoPtr, fileOperation, comm,(const Tuple2< label, labelList > &commAndIORanks, const bool distributedRoots, bool verbose),(commAndIORanks, distributedRoots, verbose))
virtual void sync()
Forcibly parallel sync.
label nProcs(const label numProcs) noexcept
Set number of processor directories/results.
bool distributed_
Distributed roots (parallel run).
fileMonitor & monitor() const
Get or create fileMonitor singleton.
virtual fileName dirPath(const bool checkGlobal, const IOobject &io, const bool search=true) const =0
Search for a directory.
virtual ~fileOperation()=default
Destructor.
virtual bool isDir(const fileName &, const bool followLink=true) const =0
Does the name exist as a DIRECTORY in the file system?
virtual bool writeObject(const regIOobject &io, IOstreamOption streamOpt=IOstreamOption(), const bool writeOnProc=true) const
Writes a regIOobject (so header, contents and divider).
virtual word processorsDir(const fileName &) const
Actual name of processors dir (for use in mode PROCOBJECT, PROCINSTANCE).
label comm(label communicator) const noexcept
Set communicator to use [mutable]. Negative values are a no-op.
virtual fileName filePath(const bool checkGlobal, const IOobject &, const word &typeName, const bool search=true) const =0
Search for an object.
static word processorsBaseDir
Return the processors directory name (usually "processors").
virtual bool mkDir(const fileName &, mode_t=0777) const =0
Make directory.
UPstream::rangeType procRangeType
For addressing a range of processors (an int range).
declareRunTimeSelectionTable(autoPtr, fileOperation, word,(bool verbose),(verbose))
static labelList getGlobalHostIORanks()
Get list of global IO master ranks based on the hostname. It is assumed that each host range is conti...
static autoPtr< fileOperation > New(const word &handlerType, bool verbose=false)
Select fileHandler-type. Uses defaultFileHandler if the handlerType is empty.
virtual fileName getFile(const label) const
Get name of file being watched (using handle).
static int nProcsFilter() noexcept
Return collated 'processorsDDD' filtering.
virtual fileNameList readDir(const fileName &, const fileName::Type=fileName::FILE, const bool filtergz=true, const bool followLink=true) const =0
Read a directory and return the entries as a string list.
virtual fileName objectPath(const IOobject &io, const word &typeName) const
Generate disk file name for object. Opposite of filePath.
virtual autoPtr< ISstream > readStream(regIOobject &, const fileName &, const word &typeName, const bool readOnProc=true) const =0
Reads header for regIOobject and returns an ISstream.
A range or interval of labels defined by a start and a size.
Definition labelRange.H:66
Registry of regIOobjects.
A class for managing references or pointers (no reference counting).
Definition refPtr.H:54
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition regIOobject.H:71
A class for handling words, derived from Foam::string.
Definition word.H:66
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
bool local
Definition EEqn.H:20
OBJstream os(runTime.globalPath()/outputName)
const auto & io
auto & name
auto & names
Namespace for OpenFOAM.
List< label > labelList
A List of labels.
Definition List.H:62
List< fileName > fileNameList
List of fileName.
refPtr< fileOperation > fileHandler(std::nullptr_t)
Delete current file handler - forwards to fileOperation::handler().
List< instant > instantList
List of instants.
Definition instantList.H:41
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces).
const word GlobalIOList< Tuple2< scalar, vector > >::typeName("scalarVectorTable")
Istream & operator>>(Istream &, directionInfo &)
UList< bool > boolUList
A UList of bools.
Definition UList.H:73
const direction noexcept
Definition scalarImpl.H:265
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition exprTraits.C:127
UList< label > labelUList
A UList of labels.
Definition UList.H:75
fileName search(const word &file, const fileName &directory)
Recursively search the given directory for the file.
Definition fileName.C:642
word format(conversionProperties.get< word >("format"))
#define declareRunTimeSelectionTable(ptrWrapper, baseType, argNames, argList, parList)
Declare a run-time selection (variables and adder classes).
const volScalarField & cp
volScalarField & b
#define TypeName(TypeNameString)
Declare a ClassName() with extra virtual type info.
Definition typeInfo.H:68