FEDEM Solver  R8.0
Source code of the dynamics solver
FFrResultContainer.H
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2023 SAP SE
2 //
3 // SPDX-License-Identifier: Apache-2.0
4 //
5 // This file is part of FEDEM - https://openfedem.org
7 
13 #ifndef FFR_RESULT_CONTAINER_H
14 #define FFR_RESULT_CONTAINER_H
15 
16 #include <string>
17 #include <vector>
18 #include <map>
19 #include <set>
20 
21 #include "FFrLib/FFrVariable.H"
22 #include "FFrLib/FFrItemGroup.H"
23 #include "FFaLib/FFaOS/FFaIO.H"
24 
25 class FFrExtractor;
27 
28 
30 typedef std::map<double,int> FFrTimeMap;
32 typedef std::vector<FFrEntryBase*> FFrEntrySet;
33 
36 {
38  std::map<int,FFrVariable*> variables;
40  std::map<int,FFrItemGroup*> itemGroups;
48  std::set<std::string>* dict;
49 
52  std::set<std::string>* tdic) : topLevelEntries(tl)
53  {
54  extractorVariables = vars;
55  extractorIGs = itgs;
56  dict = tdic;
57  }
58 };
59 
60 
71 {
72 public:
74  FFrResultContainer(FFrExtractor* extractor, const std::string& fileName);
77 
82 
84  enum Status {
95  };
96 
98  Status close();
99 
102 
104  Status getContainerStatus() const { return myStatus; }
106  bool isHeaderComplete() const { return myStatus >= FFR_HEADER_COMPLETE; }
107 
109  const std::string& getFileName() const { return myFileName; }
111  unsigned int getDate() const { return myDate; }
112 
114  int getStepsInFile() const { return myPhysicalTimeMap.size(); }
116  int getStepSize() const { return timeStepSize; }
118  FT_int getHeaderSize() const { return myHeaderSize; }
119 
121  void printInfo() const;
122 
124  const FFrEntrySet& topLevel() const { return myTopLevelEntries; }
126  const FFrTimeMap& getPhysicalTime() const { return myPhysicalTimeMap; }
127 
129  int positionAtKey(double key, bool getNextHigher = false);
131  bool isPositioned() const { return myPositionedTimeStep > 0; }
133  void resetPositioning();
134 
135 
137  int readPositionedTimestepData(const double* values, int nvals,
138  int bitPos, int cellBits, int repeats)
139  {
140  return this->actualRead((void*)values,nvals,bitPos,cellBits,repeats);
141  }
142 
144  int readPositionedTimestepData(const float* values, int nvals,
145  int bitPos, int cellBits, int repeats)
146  {
147  return this->actualRead((void*)values,nvals,bitPos,cellBits,repeats);
148  }
149 
151  int readPositionedTimestepData(const int* values, int nvals,
152  int bitPos, int cellBits, int repeats)
153  {
154  return this->actualRead((void*)values,nvals,bitPos,cellBits,repeats);
155  }
156 
157 
164  };
165 
169  double getCurrentKey() const { return this->getKey(0); }
171  double getFirstKey() const { return this->getKey(1); }
173  double getLastKey() const { return this->getKey(2); }
174 
176  double getDistanceFromPosKey(bool usePositionedKey = false) const;
178  bool getDistanceToNextKey(double& dist);
179 
181  void collectGarbage(FFrEntryBase* entry);
182 
184  void enablePreRead(bool f) { if (!(iAmPreReading = f)) this->clearPreRead(); }
186  bool isPreReadEnabled() const { return iAmPreReading; }
188  void clearPreRead();
189 
190 protected:
192  void printSizeParameters(FT_int fileSize) const;
193 
195  bool readFileHeader();
197  FFrStatus readVariables(FILE* fp, FFrCreatorData& cd,
198  bool dataBlocks = false);
199 
205  bool readText();
207  bool reopenForDataAccess();
209  void fillPreRead();
211  double getKey(int flag) const;
213  int actualRead(void* var, int nvals, int bitPos, int cellBits, int repeats);
214 
215 private:
217 
218  // data info
219  bool swapBytes;
222  FILE* myFile;
225 
226  // header info
227  std::string myFileName;
228  std::string myModule;
229  unsigned int myDate;
230 
231  // extractor info
235 
236  // information for pre-positioning of the container
239 
241  FFrTimeMap::iterator myCurrentIndexIt;
242 
243  double myWantedKey;
245 
248 
249  char* myPreRead;
251 };
252 
253 #endif
Functions and data type for direct access of large binary files.
#define FT_FILE
File pointer.
Definition: FFaIO.H:136
#define FT_int
64-bit integer type.
Definition: FFaIO.H:134
FFrStatus
Definition: FFrEnums.H:12
std::set< FFrItemGroup *, FFrItemGroup::Less > ItemGroupSet
Definition: FFrItemGroup.H:83
std::vector< FFrEntryBase * > FFrEntrySet
Results entry container.
Definition: FFrResultContainer.H:32
std::map< double, int > FFrTimeMap
Physical time to time step mapping.
Definition: FFrResultContainer.H:26
std::set< FFrVariable *, FFrVariable::Less > VariableSet
Definition: FFrVariable.H:79
Definition: FFrEntryBase.H:28
Front-end for the result extraction module.
Definition: FFrExtractor.H:44
Class holding the data of a Fedem results file.
Definition: FFrResultContainer.H:71
bool swapBytes
Endian mismatch?
Definition: FFrResultContainer.H:219
int getStepSize() const
Returns the size (in bytes) of the results data of each time step.
Definition: FFrResultContainer.H:116
int readPositionedTimestepData(const double *values, int nvals, int bitPos, int cellBits, int repeats)
Reads a double variable from the specified position.
Definition: FFrResultContainer.H:137
int positionAtKey(double key, bool getNextHigher=false)
Positions the file to the specified key (physical time).
Definition: FFrResultContainer.C:953
bool iAmPreReading
Read all results for current time step into core.
Definition: FFrResultContainer.H:247
FFrResultContainer(FFrExtractor *extractor, const std::string &fileName)
The constructor initializes the data members.
Definition: FFrResultContainer.C:31
FFrExtractor * myExtractor
Result extractor that own this container.
Definition: FFrResultContainer.H:232
bool isPositioned() const
Returns whether the file has been positioned.
Definition: FFrResultContainer.H:131
int getStepsInFile() const
Returns the number of time steps in this results container.
Definition: FFrResultContainer.H:114
int myLastReadEndPos
Position after the last data read.
Definition: FFrResultContainer.H:238
bool buildAndResolveHierarchy()
Builds and resolves the results hierarchy of the file.
Definition: FFrResultContainer.C:534
Status getContainerStatus() const
Returns the current status of this results container.
Definition: FFrResultContainer.H:104
FFrResultContainer & operator=(const FFrResultContainer &)=delete
Disable default assignment operator.
bool readText()
Reads the file squentially from current file position.
Definition: FFrResultContainer.C:689
FFrVariableReference * myPhysTimeRef
Pointer to physical time variable.
Definition: FFrResultContainer.H:216
bool isPreReadEnabled() const
Checks if the time step cache is enabled.
Definition: FFrResultContainer.H:186
Status updateContainerStatus()
Updates the container to a new status (used from the system ticks).
Definition: FFrResultContainer.C:110
int readPositionedTimestepData(const int *values, int nvals, int bitPos, int cellBits, int repeats)
Reads an integer variable from the specified position.
Definition: FFrResultContainer.H:151
double myWantedKey
The physical time to read results for.
Definition: FFrResultContainer.H:243
void collectGarbage(FFrEntryBase *entry)
Collects temporary entries to be deleted when parsing finished.
Definition: FFrResultContainer.C:588
std::string myFileName
File name associated with this result container.
Definition: FFrResultContainer.H:227
FFrEntrySet myGarbage
Garbage collection container.
Definition: FFrResultContainer.H:234
bool readFileHeader()
Reads the header of the file.
Definition: FFrResultContainer.C:234
void printInfo() const
Prints out some size parameters for this results container.
Definition: FFrResultContainer.C:655
unsigned int getDate() const
Returns the time stamp of this results container.
Definition: FFrResultContainer.H:111
void clearPreRead()
Clears the time step cache.
Definition: FFrResultContainer.C:88
double getDistanceFromPosKey(bool usePositionedKey=false) const
Returns the difference between current position and the wanted key.
Definition: FFrResultContainer.C:880
bool reopenForDataAccess()
Reopens the results file for direct access of binary data.
Definition: FFrResultContainer.C:606
FFrEntrySet myTopLevelEntries
List of all top-level entries.
Definition: FFrResultContainer.H:233
void printSizeParameters(FT_int fileSize) const
Prints out sime size parameters for the results file.
Definition: FFrResultContainer.C:667
PositionStatus myWantedKeyStatus
Is the key inside, exact, front or rear.
Definition: FFrResultContainer.H:244
Status myStatus
Current status of the file reading.
Definition: FFrResultContainer.H:224
void fillPreRead()
Reads all data associated with current time step into a cache.
Definition: FFrResultContainer.C:1034
PositionStatus
Enums defining the file position statuses that may occur.
Definition: FFrResultContainer.H:159
@ FFR_INSIDE
Definition: FFrResultContainer.H:162
@ FFR_AFTER_END
Definition: FFrResultContainer.H:163
@ FFR_BEFORE_START
Definition: FFrResultContainer.H:161
@ FFR_NOT_SET
Definition: FFrResultContainer.H:160
double getCurrentKey() const
Returns current physical time of this container.
Definition: FFrResultContainer.H:169
Status
Enums defining the various container statuses that may occur.
Definition: FFrResultContainer.H:84
@ FFR_HEADER_INCOMPLETE
Definition: FFrResultContainer.H:90
@ FFR_DATA_PRESENT
Definition: FFrResultContainer.H:93
@ FFR_TEXT_PRESENT
Definition: FFrResultContainer.H:88
@ FFR_TEXT_FILE
Definition: FFrResultContainer.H:87
@ FFR_DATA_CLOSED
Definition: FFrResultContainer.H:92
@ FFR_NO_FILE_FOUND
Definition: FFrResultContainer.H:86
@ FFR_NEW_DATA
Definition: FFrResultContainer.H:94
@ FFR_CONTAINER_INVALID
Definition: FFrResultContainer.H:85
@ FFR_NEW_TEXT
Definition: FFrResultContainer.H:89
@ FFR_HEADER_COMPLETE
Definition: FFrResultContainer.H:91
FFrResultContainer(const FFrResultContainer &)=delete
Disable default copy constructor.
FT_int getHeaderSize() const
Returns the size (in bytes) of the results file header.
Definition: FFrResultContainer.H:118
const std::string & getFileName() const
Returns the file name of this results container.
Definition: FFrResultContainer.H:109
FFrStatus readVariables(FILE *fp, FFrCreatorData &cd, bool dataBlocks=false)
Reads variable definitions and references.
Definition: FFrResultContainer.C:451
void resetPositioning()
Resets the file position.
Definition: FFrResultContainer.C:1013
~FFrResultContainer()
The destructor closes the file and releases heap-allocated data.
Definition: FFrResultContainer.C:59
std::string myModule
Program module that wrote the results file.
Definition: FFrResultContainer.H:228
int myPreReadTimeStep
Current time step in pre-read cache.
Definition: FFrResultContainer.H:250
double getKey(int flag) const
Returns current, first or last physical time of this container.
Definition: FFrResultContainer.C:863
double getFirstKey() const
Returns first physical time of this container.
Definition: FFrResultContainer.H:171
char * myPreRead
Pre-read cache.
Definition: FFrResultContainer.H:249
bool readTimeStepInformation()
Reads time step information from the file.
Definition: FFrResultContainer.C:714
int timeStepSize
Time step size in bytes (max 2GB)
Definition: FFrResultContainer.H:220
PositionStatus getPositioningStatus() const
Returns current file position status flag.
Definition: FFrResultContainer.H:167
double getLastKey() const
Returns last physical time of this container.
Definition: FFrResultContainer.H:173
FILE * myFile
File descriptor used in header parsing.
Definition: FFrResultContainer.H:222
FT_FILE myDataFile
File handle used for data access.
Definition: FFrResultContainer.H:223
unsigned int myDate
Time stamp of the results file.
Definition: FFrResultContainer.H:229
const FFrEntrySet & topLevel() const
Returns a const reference to the top-level entries container.
Definition: FFrResultContainer.H:124
bool iAmLazyPositioned
Only move file pointer first time reading data.
Definition: FFrResultContainer.H:246
const FFrTimeMap & getPhysicalTime() const
Returns a const reference to the time step mapping.
Definition: FFrResultContainer.H:126
FT_int myHeaderSize
Binary start position in bytes.
Definition: FFrResultContainer.H:221
bool getDistanceToNextKey(double &dist)
Calculates the distance to the next key for this results container.
Definition: FFrResultContainer.C:913
FFrTimeMap myPhysicalTimeMap
Physical time to results mapping.
Definition: FFrResultContainer.H:240
int actualRead(void *var, int nvals, int bitPos, int cellBits, int repeats)
Reads a certain number of bytes from the binary results file.
Definition: FFrResultContainer.C:1091
void enablePreRead(bool f)
Enables time step pre-read cache.
Definition: FFrResultContainer.H:184
FFrTimeMap::iterator myCurrentIndexIt
Pointer to what we should read.
Definition: FFrResultContainer.H:241
Status close()
Closes the results file of this container, if still open.
Definition: FFrResultContainer.C:69
int myPositionedTimeStep
Current file position.
Definition: FFrResultContainer.H:237
int readPositionedTimestepData(const float *values, int nvals, int bitPos, int cellBits, int repeats)
Reads a float variable from the specified position.
Definition: FFrResultContainer.H:144
bool isHeaderComplete() const
Returns true, if the file header has been successfully parsed.
Definition: FFrResultContainer.H:106
Definition: FFrVariableReference.H:18
real(dp) function dist(a, b, g)
Calculates the distance between the angles a,b,g and euler.
Definition: rotationModule.f90:274
Incapsulation of temporary data used during results file parsing.
Definition: FFrResultContainer.H:36
VariableSet * extractorVariables
Pointer to the result variable container of the results extractor.
Definition: FFrResultContainer.H:44
ItemGroupSet * extractorIGs
Pointer to the item group container of the results extractor.
Definition: FFrResultContainer.H:46
FFrEntrySet & topLevelEntries
Reference to the top-level entries of the results container.
Definition: FFrResultContainer.H:42
std::map< int, FFrVariable * > variables
Temporary variable container.
Definition: FFrResultContainer.H:38
FFrCreatorData(FFrEntrySet &tl, VariableSet *vars, ItemGroupSet *itgs, std::set< std::string > *tdic)
The constructor initializes the data members.
Definition: FFrResultContainer.H:51
std::map< int, FFrItemGroup * > itemGroups
Temporary item group container.
Definition: FFrResultContainer.H:40
std::set< std::string > * dict
Pointer to the text dictionary of the results extractor.
Definition: FFrResultContainer.H:48