forked from UBCHREST/ablate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
111 lines (96 loc) · 4.11 KB
/
main.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
#include <fstream>
#include <iostream>
#include <memory>
#include "builder.hpp"
#include "environment/download.hpp"
#include "environment/runEnvironment.hpp"
#include "listing.hpp"
#include "localPath.hpp"
#include "parameters/petscPrefixOptions.hpp"
#include "utilities/mpiUtilities.hpp"
#include "utilities/petscUtilities.hpp"
#include "yamlParser.hpp"
using namespace ablate;
const char* replacementInputPrefix = "-yaml::";
int main(int argc, char** args) {
// initialize petsc and mpi
environment::RunEnvironment::Initialize(&argc, &args);
ablate::utilities::PetscUtilities::Initialize();
// check to see if we should print version
PetscBool printInfo = PETSC_FALSE;
PetscOptionsGetBool(nullptr, nullptr, "-version", &printInfo, nullptr) >> utilities::PetscUtilities::checkError;
if (!printInfo) {
PetscOptionsGetBool(nullptr, nullptr, "--info", &printInfo, nullptr) >> utilities::PetscUtilities::checkError;
}
if (printInfo) {
Builder::PrintInfo(std::cout);
return 0;
}
PetscBool printVersion = PETSC_FALSE;
PetscOptionsGetBool(nullptr, nullptr, "--version", &printVersion, nullptr) >> utilities::PetscUtilities::checkError;
if (printVersion) {
Builder::PrintVersion(std::cout);
return 0;
}
PetscInt delay = -1;
PetscBool delaySpecified;
PetscOptionsGetInt(nullptr, nullptr, "--delay", &delay, &delaySpecified) >> utilities::PetscUtilities::checkError;
if (delaySpecified) {
PetscSleep(delay) >> utilities::PetscUtilities::checkError;
}
// check to see if we should print options
PetscBool printParserOptions = PETSC_FALSE;
PetscOptionsGetBool(nullptr, nullptr, "--help", &printParserOptions, nullptr) >> utilities::PetscUtilities::checkError;
if (printParserOptions) {
std::cout << cppParser::Listing::Get() << std::endl;
return 0;
}
// check to see if we should print options
char filename[PETSC_MAX_PATH_LEN] = "";
PetscBool fileSpecified = PETSC_FALSE;
PetscOptionsGetString(nullptr, nullptr, "--input", filename, PETSC_MAX_PATH_LEN, &fileSpecified) >> utilities::PetscUtilities::checkError;
if (!fileSpecified) {
throw std::invalid_argument("the --input must be specified");
}
// locate or download the file
std::filesystem::path filePath;
if (ablate::environment::Download::IsUrl(filename)) {
ablate::environment::Download downloader(filename);
filePath = downloader.Locate();
} else {
cppParser::LocalPath locator(filename);
filePath = locator.Locate();
}
if (!std::filesystem::exists(filePath)) {
throw std::invalid_argument("unable to locate input file: " + filePath.string());
}
{
// build options from the command line
auto yamlOptions = std::make_shared<ablate::parameters::PetscPrefixOptions>(replacementInputPrefix);
// create the yaml parser
std::shared_ptr<cppParser::YamlParser> parser = std::make_shared<cppParser::YamlParser>(filePath, yamlOptions->GetMap());
// setup the monitor
auto setupEnvironmentParameters = parser->GetByName<ablate::parameters::Parameters>("environment");
environment::RunEnvironment::Setup(*setupEnvironmentParameters, filePath);
// Copy over the input file
int rank;
MPI_Comm_rank(PETSC_COMM_WORLD, &rank) >> utilities::MpiUtilities::checkError;
if (rank == 0) {
std::filesystem::path inputCopy = environment::RunEnvironment::Get().GetOutputDirectory() / filePath.filename();
std::ofstream stream(inputCopy);
parser->Print(stream);
stream.close();
}
// run with the parser
Builder::Run(parser);
// check for unused parameters
auto unusedValues = parser->GetUnusedValues();
if (!unusedValues.empty()) {
std::cout << "WARNING: The following input parameters were not used:" << std::endl;
for (const auto& unusedValue : unusedValues) {
std::cout << unusedValue << std::endl;
}
}
}
ablate::environment::RunEnvironment::Finalize();
}