1 |
|
2 |
/******************************************************* |
3 |
* |
4 |
* Copyright (c) 2003-2009 by University of Queensland |
5 |
* Earth Systems Science Computational Center (ESSCC) |
6 |
* http://www.uq.edu.au/esscc |
7 |
* |
8 |
* Primary Business: Queensland, Australia |
9 |
* Licensed under the Open Software License version 3.0 |
10 |
* http://www.opensource.org/licenses/osl-3.0.php |
11 |
* |
12 |
*******************************************************/ |
13 |
|
14 |
#ifndef __DATAVAR_H__ |
15 |
#define __DATAVAR_H__ |
16 |
|
17 |
#include <escriptexport/escriptexport.h> |
18 |
|
19 |
class DBfile; |
20 |
class NcFile; |
21 |
|
22 |
namespace escript { |
23 |
class Data; |
24 |
} |
25 |
|
26 |
namespace escriptexport { |
27 |
|
28 |
class FinleyMesh; |
29 |
|
30 |
/// \brief A class that provides functionality to read an escript data object |
31 |
/// from NetCDF file or an escript::Data instance and write that data in Silo |
32 |
/// or VTK XML formats. |
33 |
class DataVar |
34 |
{ |
35 |
public: |
36 |
/// \brief Constructor with variable name |
37 |
ESCRIPTEXPORT_DLL_API |
38 |
DataVar(const std::string& name); |
39 |
|
40 |
/// \brief Copy constructor. Performs a deep copy of the data values. |
41 |
ESCRIPTEXPORT_DLL_API |
42 |
DataVar(const DataVar& d); |
43 |
|
44 |
/// \brief Destructor |
45 |
ESCRIPTEXPORT_DLL_API |
46 |
~DataVar(); |
47 |
|
48 |
/// \brief Initialises values and IDs from an escript::Data instance. |
49 |
/// |
50 |
/// \return true if a valid instance of expanded data was supplied, |
51 |
/// false if the initialisation was unsuccessful. |
52 |
ESCRIPTEXPORT_DLL_API |
53 |
bool initFromEscript(escript::Data& escriptData, FinleyMesh_ptr mesh); |
54 |
|
55 |
/// \brief Initialises with integral mesh data like IDs or tags. |
56 |
/// |
57 |
/// The data is retrieved from the mesh using the variable name. |
58 |
ESCRIPTEXPORT_DLL_API |
59 |
bool initFromMesh(FinleyMesh_ptr mesh); |
60 |
|
61 |
/// \brief Reads values and IDs for this variable from escript NetCDF file. |
62 |
/// |
63 |
/// \return true if the file was found and contains valid escript data |
64 |
/// with at least one sample, false otherwise. |
65 |
/// \note Only expanded data with rank <=2 is supported at the moment. |
66 |
ESCRIPTEXPORT_DLL_API |
67 |
bool initFromNetCDF(const std::string& filename, FinleyMesh_ptr mesh); |
68 |
|
69 |
/// \brief Writes the data into given directory in given Silo file. |
70 |
/// |
71 |
/// If Silo was not available at compile time or the mesh was not set |
72 |
/// beforehand using setMesh() or if a Silo function fails this method |
73 |
/// returns false. |
74 |
ESCRIPTEXPORT_DLL_API |
75 |
bool writeToSilo(DBfile* dbfile, const std::string& siloPath); |
76 |
|
77 |
/// \brief Returns the rank of the data. |
78 |
ESCRIPTEXPORT_DLL_API |
79 |
int getRank() const { return rank; } |
80 |
|
81 |
/// \brief Returns true if the variable data is node centered, false if |
82 |
/// zone centered. |
83 |
ESCRIPTEXPORT_DLL_API |
84 |
bool isNodeCentered() const; |
85 |
|
86 |
/// \brief Returns the name of the associated mesh. |
87 |
/// |
88 |
/// The returned name is one of the sub-meshes of the mesh set with |
89 |
/// setMesh() determined on the basis of the function space type and |
90 |
/// whether reduced elements are used or not. |
91 |
ESCRIPTEXPORT_DLL_API |
92 |
std::string getMeshName() const { return meshName; } |
93 |
|
94 |
/// \brief Returns the shape vector of the data. |
95 |
/// |
96 |
/// The shape vector has as many elements as the rank of this variable. |
97 |
ESCRIPTEXPORT_DLL_API |
98 |
const IntVec& getShape() const { return shape; } |
99 |
|
100 |
/// \brief Returns the variable name. |
101 |
ESCRIPTEXPORT_DLL_API |
102 |
std::string getName() const { return varName; } |
103 |
|
104 |
/// \brief Returns the Silo tensor definition for this tensor. |
105 |
/// |
106 |
/// If the data is tensor data then the components of the tensor are stored |
107 |
/// separately in the Silo file. This method then returns a string that |
108 |
/// contains the proper Silo expression to put the tensor together again. |
109 |
/// For non-tensor data this method returns an empty string. |
110 |
ESCRIPTEXPORT_DLL_API |
111 |
std::string getTensorDef() const; |
112 |
|
113 |
/// \brief Returns the number of data values. |
114 |
ESCRIPTEXPORT_DLL_API |
115 |
int getNumberOfSamples() const { return numSamples; } |
116 |
|
117 |
/// \brief Returns the array of data values where array[i] is the i-th |
118 |
/// component of the data. |
119 |
ESCRIPTEXPORT_DLL_API |
120 |
const CoordArray& getData() const { return dataArray; } |
121 |
|
122 |
private: |
123 |
void cleanup(); |
124 |
|
125 |
/// \brief Averages and filters data. |
126 |
/// |
127 |
/// If a sample consists of more than one data point then this method |
128 |
/// averages over the data points and returns the resulting array. |
129 |
/// In any case, the data is filtered according to the stride value. |
130 |
float* averageData(const float* src, size_t stride); |
131 |
|
132 |
/// \brief Prepares a sample ID -> index mapping which is used to reorder |
133 |
/// data. |
134 |
IndexMap buildIndexMap(); |
135 |
|
136 |
/// \brief Reorders the samples according to the corresponding node IDs. |
137 |
/// |
138 |
/// \return true if the function space is supported and the number of |
139 |
/// elements or nodes corresponds to the number of data samples. |
140 |
bool filterSamples(const FinleyMesh_ptr mesh); |
141 |
|
142 |
bool initialized; |
143 |
std::string varName; |
144 |
int numSamples, rank, ptsPerSample, centering, funcSpace; |
145 |
IntVec shape; |
146 |
IntVec sampleID; |
147 |
CoordArray dataArray; |
148 |
std::string meshName, siloMeshName; |
149 |
}; |
150 |
|
151 |
inline IndexMap DataVar::buildIndexMap() |
152 |
{ |
153 |
IndexMap sampleID2idx; |
154 |
int idx = 0; |
155 |
IntVec::const_iterator idIt; |
156 |
for (idIt = sampleID.begin(); idIt != sampleID.end(); idIt++, idx++) |
157 |
sampleID2idx[*idIt] = idx; |
158 |
|
159 |
return sampleID2idx; |
160 |
} |
161 |
|
162 |
} // namespace escriptexport |
163 |
|
164 |
#endif // __DATAVAR_H__ |
165 |
|