-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathh5cgnsfileseparatesolutionutil.cpp
More file actions
200 lines (170 loc) · 4.85 KB
/
Copy pathh5cgnsfileseparatesolutionutil.cpp
File metadata and controls
200 lines (170 loc) · 4.85 KB
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
#include "error_macros.h"
#include "h5cgnsbase.h"
#include "h5cgnsbaseiterativedata.h"
#include "h5cgnsfile.h"
#include "h5cgnsfileseparatesolutionutil.h"
#include "iriclib_errorcodes.h"
#include "internal/iric_logger.h"
#include <Poco/File.h>
#include <Poco/Path.h>
#include <sstream>
#include <stdexcept>
using namespace iRICLib;
#define BACKUP_FILENAME "Case1_input.cgn"
int H5CgnsFileSeparateSolutionUtil::createResultFolderIfNotExists(const std::string& fileName)
{
Poco::File f(resultFolder(fileName));
try {
if (! f.exists()) {
f.createDirectory();
}
return IRIC_NO_ERROR;
} catch (...) {
return IRIC_FOLDER_CREATE_ERROR;
}
}
int H5CgnsFileSeparateSolutionUtil::createBackupFile(const std::string& fileName)
{
Poco::Path path(fileName);
auto backup_fullpath = path.parent().append(Poco::Path(BACKUP_FILENAME)).toString();
Poco::File file(fileName);
try {
file.copyTo(backup_fullpath);
return IRIC_NO_ERROR;
} catch (...) {
return IRIC_FILE_COPY_FAIL;
}
}
int H5CgnsFileSeparateSolutionUtil::checkFileStatus(const std::string& fileName, Status* status)
{
H5CgnsFile* file = nullptr;
try {
file = new H5CgnsFile(fileName, H5CgnsFile::Mode::OpenReadOnly);
} catch (...) {
*status = Status::Broken;
return IRIC_NO_ERROR;
}
int maxSolutionId;
getMaxSeparateResultSolutionId(fileName, &maxSolutionId);
std::vector<double> timeVals;
file->ccBase()->biterData()->readTime(&timeVals);
if (maxSolutionId != static_cast<int>(timeVals.size())) {
*status = Status::Inconsistent;
return IRIC_NO_ERROR;
}
*status = Status::Valid;
return IRIC_NO_ERROR;
}
int H5CgnsFileSeparateSolutionUtil::rebuildBaseIterativeData(const std::string& fileName)
{
H5CgnsFile file(fileName.c_str(), H5CgnsFile::Mode::OpenModify);
for (int i = 0; i < file.baseNum(); ++i) {
auto base = file.baseById(i + 1);
int ier = base->biterData()->clearData();
RETURN_IF_ERR;
}
int maxSolutionId;
getMaxSeparateResultSolutionId(fileName, &maxSolutionId);
int id = 1;
while (id <= maxSolutionId) {
auto fName = fileNameForSolution(fileName, id);
H5CgnsFile resultFile(fName.c_str(), H5CgnsFile::Mode::OpenReadOnly);
for (int i = 0; i < resultFile.baseNum(); ++i) {
auto fromBIter = resultFile.baseById(i + 1)->biterData();
if (fromBIter == nullptr) {continue;}
auto toBIter = file.baseById(i + 1)->biterData();
std::vector<std::string> names;
int ier = fromBIter->getResultNames(&names);
RETURN_IF_ERR;
for (const auto& name : names) {
H5Util::DataArrayValueType type;
ier = fromBIter->readValueType(name, &type);
if (type == H5Util::DataArrayValueType::Int) {
std::vector<int> vals;
fromBIter->readValues(name, &vals);
for (auto v : vals) {
toBIter->writeData(name, v);
}
} else if (type == H5Util::DataArrayValueType::RealDouble) {
std::vector<double> vals;
fromBIter->readValues(name, &vals);
for (auto v : vals) {
toBIter->writeData(name, v);
}
}
}
}
++ id;
}
return IRIC_NO_ERROR;
}
int H5CgnsFileSeparateSolutionUtil::buildFromSeparateResultFiles(const std::string& fileName)
{
// @todo implement this
throw std::runtime_error("Not implemented");
}
int H5CgnsFileSeparateSolutionUtil::clearResultFolder(const std::string& fileName)
{
auto folder = resultFolder(fileName);
Poco::File f(folder);
try {
f.remove(true);
f.createDirectory();
return IRIC_NO_ERROR;;
} catch (...) {
std::ostringstream ss;
ss << "In H5CgnsFileSeparateSolutionUtil::clearResultFolder(), removing and creating folder \"result\" failed";
_iric_logger_error(ss.str());
return IRIC_FOLDER_CLEAR_ERROR ;
}
}
std::string H5CgnsFileSeparateSolutionUtil::fileNameForSolution(const std::string& resultFolder, int solId)
{
std::ostringstream ss;
ss << "Solution" << solId << ".cgn";
auto solFileName = ss.str();
return Poco::Path(resultFolder).append(solFileName).toString();
}
int H5CgnsFileSeparateSolutionUtil::getMaxSeparateResultSolutionId(const std::string& fileName, int* solutionId)
{
int prevMaxExistId = 0;
int maxExistId = 1;
while (true) {
auto fName = fileNameForSolution(fileName, maxExistId);
Poco::File f(fName);
if (f.exists()) {
prevMaxExistId = maxExistId;
maxExistId *= 2;
} else {
maxExistId = prevMaxExistId;
break;
}
}
if (maxExistId == 0) {
*solutionId = 0;
return IRIC_NO_ERROR;
}
int left = maxExistId;
int right = maxExistId * 2;
while (true) {
if (right - left == 1) {
*solutionId = left;
return IRIC_NO_ERROR;
}
int mid = (left + right) / 2;
auto fName = fileNameForSolution(fileName, mid);
Poco::File f(fName);
if (f.exists()) {
left = mid;
} else {
right = mid;
}
}
}
std::string H5CgnsFileSeparateSolutionUtil::resultFolder(const std::string& fileName)
{
Poco::Path path(fileName);
return path.parent().append(Poco::Path("result")).toString();
}
H5CgnsFileSeparateSolutionUtil::H5CgnsFileSeparateSolutionUtil()
{}