-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLauncher.cpp
More file actions
108 lines (91 loc) · 3.03 KB
/
Launcher.cpp
File metadata and controls
108 lines (91 loc) · 3.03 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
#include "Launcher.h"
#include "Adapter.h"
#include "AdaptedInputParser.h"
#include "Driver.h"
#include <filesystem>
#include <gmsh.h>
#include <iostream>
#include <stdexcept>
namespace tulip {
namespace {
bool hasSuffix(const std::string& value, const std::string& suffix)
{
return value.size() >= suffix.size() &&
value.compare(value.size() - suffix.size(), suffix.size(), suffix) == 0;
}
bool isInputJson(const std::string& filename)
{
return hasSuffix(filename, ".tulip.input.json");
}
bool isAdaptedJson(const std::string& filename)
{
return hasSuffix(filename, ".tulip.adapted.json");
}
std::string ensureTrailingSlash(const std::string& folder)
{
if (folder.empty() || folder.back() == '/') {
return folder;
}
return folder + "/";
}
std::string extractCaseName(const std::string& inputFile)
{
const auto filename = std::filesystem::path(inputFile).filename().string();
if (hasSuffix(filename, ".tulip.input.json")) {
return filename.substr(0, filename.size() - std::string(".tulip.input.json").size());
}
if (hasSuffix(filename, ".tulip.adapted.json")) {
return filename.substr(0, filename.size() - std::string(".tulip.adapted.json").size());
}
return std::filesystem::path(filename).stem().string();
}
} // namespace
Launcher::Launcher(const std::string& inputFile, const std::string& exportFolder)
: inputFile_(inputFile),
exportFolder_(exportFolder)
{
if (exportFolder_.empty()) {
exportFolder_ = "./" + std::filesystem::path(inputFile).parent_path().string() + "/";
}
}
void Launcher::run()
{
std::cout << "Loading input file: " << inputFile_ << std::endl;
const std::string caseNamePrefix = extractCaseName(inputFile_) + ".";
const std::string outputPathPrefix = ensureTrailingSlash(exportFolder_) + caseNamePrefix;
if (isAdaptedJson(inputFile_)) {
auto driver = Driver::loadFromAdaptedFile(inputFile_);
driver.setExportFolder(outputPathPrefix);
std::cout << "Running Tulip analysis..." << std::endl;
driver.run();
}
else if (isInputJson(inputFile_)) {
const bool initializedHere = !gmsh::isInitialized();
if (initializedHere) {
gmsh::initialize();
}
try {
Adapter adapter(inputFile_);
AdaptedInputParser parser(inputFile_, adapter.getAdaptedInputJSON());
Driver driver(parser.readModel(), parser.readDriverOptions());
driver.setExportFolder(outputPathPrefix);
std::cout << "Running Tulip analysis..." << std::endl;
driver.run();
}
catch (...) {
if (initializedHere) {
gmsh::finalize();
}
throw;
}
if (initializedHere) {
gmsh::finalize();
}
}
else {
throw std::runtime_error(
"Unsupported input file extension: expected .tulip.input.json or .tulip.adapted.json");
}
std::cout << "-- tulip finished successfully --" << std::endl;
}
} // namespace tulip