-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·121 lines (89 loc) · 3.21 KB
/
Copy pathmain.cpp
File metadata and controls
executable file
·121 lines (89 loc) · 3.21 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
#include <stdio.h>
#include <iostream>
#include <ctime>
#include <mpi.h>
#include "helper.hpp"
#include "simulation.hpp"
#ifdef gpp9
// gcc Version >= 9
#include "filesystem"
namespace filesystem = std::filesystem;
#else
// gcc Version < 9
#include "experimental/filesystem"
namespace filesystem = std::experimental::filesystem;
#endif // DEBUG
inline void print_scenarios(filesystem::path folder) {
std::string file;
for (auto entry : filesystem::directory_iterator(folder)) {
file = std::string(entry.path().filename());
std::size_t dat = file.find(std::string(".dat"));
if (dat != std::string::npos) {
printf("- %s \n", file.substr(0, dat).c_str());
}
}
}
int main(int argn, char** args) {
int rank, input_size, output_size;
MPI_Init(&argn, &args);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
filesystem::path input, output;
if (rank == 0) {
std::string input_path = get_cmd_argument(argn, args, "-in");
std::string output_path = get_cmd_argument(argn, args, "-out");
if(input_path.empty()) {
input_path = "../scenarios";
}
if(output_path.empty()) {
output_path = "../vtk-files";
}
input = filesystem::path(input_path);
if(!filesystem::exists(input)) {
printf("\u001B[31m Input path %s not found!\u001B[0m \n", input.c_str());
MPI_Abort(MPI_COMM_WORLD, -1);
}
if (argn == 1) {
// no problem name given, exit program with error message
printf("Please give the scenario name that should be simulated!\n");
print_scenarios(input);
printf("\n example: ./sim Cavity100 \n");
MPI_Abort(MPI_COMM_WORLD, -1);
}
// take second argument since first is the program name
std::string scenario = std::string(args[1]);
input /= (scenario + ".dat");
// check if .dat file for scenario exists
if(!filesystem::exists(input)) {
// no .dat file found
printf("\u001B[31m No .dat file found for scenario '%s' in input folder '%s'! \u001B[0m \n", scenario.c_str(), input.c_str());
MPI_Abort(MPI_COMM_WORLD, -1);
}
// create output path
output = filesystem::path(output_path);
output /= (scenario + "-" + std::to_string(time(0)));
filesystem::create_directories(output);
// add scenario name so vtk files have the correct name
output /= scenario; // TODO: maybe this should be moved inside run_simulation and use the 'problem' parameter
input_size = input.string().size();
output_size = output.string().size();
}
MPI_Barrier(MPI_COMM_WORLD);
MPI_Bcast(&input_size, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(&output_size, 1, MPI_INT, 0, MPI_COMM_WORLD);
std::string in = std::string(input_size, ' ');
std::string out = std::string(output_size, ' ');
if(rank == 0) {
in = input.string();
out = output.string();
}
MPI_Bcast(const_cast<char*>(in.data()), input_size, MPI_CHAR, 0, MPI_COMM_WORLD);
MPI_Bcast(const_cast<char*>(out.data()), output_size, MPI_CHAR, 0, MPI_COMM_WORLD);
// append rank to output path
sprintf(out.data(), "%s_p%d", out.c_str(), rank);
input = filesystem::path(in.c_str());
output = filesystem::path(out.c_str());
MPI_Barrier(MPI_COMM_WORLD);
run_simulation(input, output);
MPI_Finalize();
return 0;
}