-
Notifications
You must be signed in to change notification settings - Fork 2
/
h5cgnsbase.cpp
348 lines (280 loc) · 8.98 KB
/
h5cgnsbase.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include "error_macros.h"
#include "h5cgnsbase.h"
#include "h5cgnsbaseiterativedata.h"
#include "h5cgnsconditiongroup.h"
#include "h5cgnsfile.h"
#include "h5groupcloser.h"
#include "h5util.h"
#include "iriclib_errorcodes.h"
#include "internal/iric_logger.h"
#include <hdf5.h>
#include <set>
#include <sstream>
#include "private/h5cgnsbase_impl.h"
using namespace iRICLib;
#define LABEL "CGNSBase_t"
#define DEFAULT_ZONE_NAME "iRICZone"
#define ERRORCODE_NAME "ErrorCode"
std::string H5CgnsBase::label()
{
return LABEL;
}
H5CgnsBase::H5CgnsBase(int dimension, const std::string &name, hid_t groupId, H5CgnsFile* file) :
impl {new Impl {this}}
{
impl->m_dimension = dimension;
impl->m_name = name;
impl->m_groupId = groupId;
impl->m_file = file;
_IRIC_LOGGER_TRACE_CALL_START("H5CgnsBase::Impl::loadNames");
impl->loadNames();
_IRIC_LOGGER_TRACE_CALL_END("H5CgnsBase::Impl::loadNames");
if (dimension == 2) {
_IRIC_LOGGER_TRACE_CALL_START("H5CgnsBase::Impl::openOrCreateCcGroup");
impl->openOrCreateCcGroup();
_IRIC_LOGGER_TRACE_CALL_END("H5CgnsBase::Impl::openOrCreateCcGroup");
_IRIC_LOGGER_TRACE_CALL_START("H5CgnsBase::Impl::openOrCreateGeoDataTop");
impl->openOrCreateGeoDataTop();
_IRIC_LOGGER_TRACE_CALL_END("H5CgnsBase::Impl::openOrCreateGeoDataTop");
_IRIC_LOGGER_TRACE_CALL_START("H5CgnsBase::Impl::openOrCreateSolverInformation");
impl->openOrCreateSolverInformation();
_IRIC_LOGGER_TRACE_CALL_END("H5CgnsBase::Impl::openOrCreateSolverInformation");
}
_IRIC_LOGGER_TRACE_CALL_START("H5CgnsBase::Impl::openOrCreateGccTop");
impl->openOrCreateGccTop();
_IRIC_LOGGER_TRACE_CALL_END("H5CgnsBase::Impl::openOrCreateGccTop");
_IRIC_LOGGER_TRACE_CALL_START("H5CgnsBase::Impl::clearNames");
impl->clearNames();
_IRIC_LOGGER_TRACE_CALL_END("H5CgnsBase::Impl::clearNames");
_IRIC_LOGGER_TRACE_CALL_START("H5CgnsBase::Impl::loadZones");
impl->loadZones();
_IRIC_LOGGER_TRACE_CALL_END("H5CgnsBase::Impl::loadZones");
}
H5CgnsBase::H5CgnsBase(int dimension, const std::string &name, H5CgnsFile* file) :
impl {new Impl {this}}
{
impl->m_dimension = dimension;
impl->m_name = name;
impl->m_file = file;
impl->m_groupId = -1;
}
H5CgnsBase::~H5CgnsBase()
{
delete impl;
}
int H5CgnsBase::dimension() const
{
return impl->m_dimension;
}
std::string H5CgnsBase::name() const
{
return impl->m_name;
}
H5CgnsConditionGroup* H5CgnsBase::ccGroup() const
{
return impl->m_ccGroup;
}
H5CgnsGridComplexConditionTop* H5CgnsBase::gccTop() const
{
return impl->m_gccTop;
}
H5CgnsBaseIterativeData* H5CgnsBase::biterData()
{
if (impl->m_biterData != nullptr) {return impl->m_biterData;}
std::unordered_set<std::string> names;
H5Util::getGroupNames(impl->m_groupId, &names);
if (impl->m_file->mode() == H5CgnsFile::Mode::Create) {
impl->m_biterData = impl->createBaseIterativeData();
} else if (impl->m_file->mode() == H5CgnsFile::Mode::OpenReadOnly) {
if (names.find(H5CgnsBaseIterativeData::groupName()) == names.end()) {return nullptr;}
hid_t gid;
_IRIC_LOGGER_TRACE_CALL_START("H5Util::openGroup");
int ier = H5Util::openGroup(impl->m_groupId, H5CgnsBaseIterativeData::groupName(), H5CgnsBaseIterativeData::label(), &gid);
_IRIC_LOGGER_TRACE_CALL_END_WITHVAL("H5Util::openGroup", ier);
if (ier == IRIC_NO_ERROR) {
impl->m_biterData = new H5CgnsBaseIterativeData(gid, this);
}
} else if (impl->m_file->mode() == H5CgnsFile::Mode::OpenModify) {
if (names.find(H5CgnsBaseIterativeData::groupName()) == names.end()) {
impl->m_biterData = impl->createBaseIterativeData();
} else {
hid_t gid;
_IRIC_LOGGER_TRACE_CALL_START("H5Util::openGroup");
int ier = H5Util::openGroup(impl->m_groupId, H5CgnsBaseIterativeData::groupName(), H5CgnsBaseIterativeData::label(), &gid);
_IRIC_LOGGER_TRACE_CALL_END_WITHVAL("H5Util::openGroup", ier);
if (ier != IRIC_NO_ERROR) {return nullptr;}
impl->m_biterData = new H5CgnsBaseIterativeData(gid, this);
}
}
return impl->m_biterData;
}
H5CgnsGeographicDataTop* H5CgnsBase::geoDataTop() const
{
return impl->m_geoDataTop;
}
H5CgnsSolverInformation* H5CgnsBase::solverInformation() const
{
return impl->m_solverInformation;
}
int H5CgnsBase::writeErrorCode(int errorCode)
{
hid_t gId;
_IRIC_LOGGER_TRACE_CALL_START("H5Util::openOrCreateUserDefinedDataGroup");
int ier = H5Util::createUserDefinedDataGroup(impl->m_groupId, ERRORCODE_NAME, &gId);
_IRIC_LOGGER_TRACE_CALL_END("H5Util::openOrCreateUserDefinedDataGroup");
RETURN_IF_ERR;
H5GroupCloser closer(gId);
std::vector<int> data;
data.push_back(errorCode);
_IRIC_LOGGER_TRACE_CALL_START("H5Util::updateOrCreateDataArray");
ier = H5Util::updateOrCreateDataArray(gId, "Value", data);
_IRIC_LOGGER_TRACE_CALL_END_WITHVAL("H5Util::updateOrCreateDataArray", ier);
RETURN_IF_ERR;
return IRIC_NO_ERROR;
}
int H5CgnsBase::readErrorCode(int* errorCode)
{
hid_t gId;
_IRIC_LOGGER_TRACE_CALL_START("H5Util::openOrCreateUserDefinedDataGroup");
int ier = H5Util::openGroup(impl->m_groupId, ERRORCODE_NAME, H5Util::userDefinedDataLabel(), &gId, true);
_IRIC_LOGGER_TRACE_CALL_END("H5Util::openOrCreateUserDefinedDataGroup");
if (ier != IRIC_NO_ERROR) {
// no error code is output.
*errorCode = 0;
return IRIC_NO_ERROR;
}
H5GroupCloser closer(gId);
std::vector<int> data;
_IRIC_LOGGER_TRACE_CALL_START("H5Util::updateOrCreateDataArray");
ier = H5Util::readDataArrayValue(gId, "Value", &data);
_IRIC_LOGGER_TRACE_CALL_END_WITHVAL("H5Util::updateOrCreateDataArray", ier);
RETURN_IF_ERR;
if (data.size() < 1) {
std::ostringstream ss;
ss << "In H5CgnsBase::readErrorCode(), data length == 0";
_iric_logger_error(ss.str());
return IRIC_NO_DATA;
}
*errorCode = data.at(0);
return IRIC_NO_ERROR;
}
int H5CgnsBase::zoneNum() const
{
return static_cast<int> (impl->m_zones.size());
}
H5CgnsZone* H5CgnsBase::zoneById(int zid)
{
if (zid < 1 || zid > static_cast<int> (impl->m_zones.size())) {return nullptr;}
return impl->m_zones.at(zid - 1);
}
std::vector<std::string> H5CgnsBase::zoneNames() const
{
std::vector<std::string> ret;
for (const auto& pair : impl->m_zoneMap) {
ret.push_back(pair.first);
}
return ret;
}
H5CgnsZone* H5CgnsBase::defaultZone()
{
return zone(DEFAULT_ZONE_NAME);
}
H5CgnsZone* H5CgnsBase::zone(const std::string& name)
{
auto it = impl->m_zoneMap.find(name);
if (it == impl->m_zoneMap.end()) {return nullptr;}
return it->second;
}
bool H5CgnsBase::zoneExists(const std::string& name)
{
auto it = impl->m_zoneMap.find(name);
return it != impl->m_zoneMap.end();
}
H5CgnsZone* H5CgnsBase::createDefaultZone(H5CgnsZone::Type type, const std::vector<int> &size)
{
return createZone(nextDefaultName(), type, size);
}
H5CgnsZone* H5CgnsBase::createZone(const std::string& name, H5CgnsZone::Type type, const std::vector<int>& size)
{
return impl->createZone(name, type, size);
}
void H5CgnsBase::addZone(H5CgnsZone* zone)
{
impl->addZone(zone);
}
std::string H5CgnsBase::nextDefaultName()
{
if (impl->m_zones.size() == 0) {
return DEFAULT_ZONE_NAME;
} else {
std::ostringstream ss;
ss << DEFAULT_ZONE_NAME << impl->m_zones.size() + 1;
return ss.str();
}
}
int H5CgnsBase::setSolutionId(int solutionId)
{
for (auto zone : impl->m_zones) {
_IRIC_LOGGER_TRACE_CALL_START("H5CgnsZone::setSolutionId");
int ier = zone->setSolutionId(solutionId);
_IRIC_LOGGER_TRACE_CALL_END_WITHVAL("H5CgnsZone::setSolutionId", ier);
RETURN_IF_ERR;
}
return IRIC_NO_ERROR;
}
int H5CgnsBase::copyGridsTo(H5CgnsBase* target)
{
for (auto zone : impl->m_zones) {
auto zone2 = target->createZone(zone->name(), zone->type(), zone->size());
int ier = zone->copyGridTo(zone2);
RETURN_IF_ERR;
}
return IRIC_NO_ERROR;
}
int H5CgnsBase::copyExceptSolution(H5CgnsBase* target)
{
// copy UserDefinedData
std::vector<std::string> names;
int ier;
ier = H5Util::getGroupNamesWithLabel(impl->m_groupId, H5Util::userDefinedDataLabel(), &names);
RETURN_IF_ERR;
for (auto name : names) {
hid_t srcGroup;
ier = H5Util::openGroup(impl->m_groupId, name, H5Util::userDefinedDataLabel(), &srcGroup);
RETURN_IF_ERR;
H5GroupCloser srcCloser(srcGroup);
hid_t tgtGroup;
ier = H5Util::openGroup(target->impl->m_groupId, name, H5Util::userDefinedDataLabel(), &tgtGroup);
RETURN_IF_ERR;
H5GroupCloser tgtCloser(tgtGroup);
ier = H5Util::copyGroupRecursively(srcGroup, tgtGroup);
RETURN_IF_ERR;
}
// copy Zones
for (int i = 0; i < zoneNum(); ++i) {
auto srcZone = zoneById(i + 1);
auto tgtZone = target->createZone(srcZone->name(), srcZone->type(), srcZone->size());
ier = srcZone->copyExceptSolution(tgtZone);
}
return IRIC_NO_ERROR;
}
int H5CgnsBase::flush()
{
if (impl->m_biterData != nullptr) {
_IRIC_LOGGER_TRACE_CALL_START("H5CgnsBaseIterativeData::flush");
int ier = impl->m_biterData->flush();
_IRIC_LOGGER_TRACE_CALL_END_WITHVAL("H5CgnsBaseIterativeData::flush", ier);
RETURN_IF_ERR;
}
for (auto zone : impl->m_zones) {
_IRIC_LOGGER_TRACE_CALL_START("H5CgnsZone::flush");
int ier = zone->flush();
_IRIC_LOGGER_TRACE_CALL_END_WITHVAL("H5CgnsZone::flush", ier);
RETURN_IF_ERR;
}
return IRIC_NO_ERROR;
}
H5CgnsFile* H5CgnsBase::file() const
{
return impl->m_file;
}