-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
146 lines (134 loc) · 4.84 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
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
#include <cstdio>
#include <string>
#include <fstream>
#include <nlohmann/json.hpp>
#include <iostream>
#include <filesystem>
#include "assembly.h"
#include "grid.h"
#include "error_code.h"
#include "expression.h"
#include "cmd_line_parser.h"
#include "tbb/task_scheduler_init.h"
#include "tbb/global_control.h"
#include "gpu_simulation_device.h"
enum SolverMethod {
FEM,
SemiLagrangianFEM
};
template<typename VeloctyShape, typename PressureShape>
void rungeEstimates(
NSFem::NavierStokesAssembly<VeloctyShape, PressureShape>& solver,
std::string baseFolder,
SolverMethod method,
bool solve
) {
baseFolder += method == FEM ? "FEM" : "SemiLagrangian";
constexpr double dt[3] = {0.01, 0.001, 0.0001};
constexpr double totalTime = 1;
constexpr int totalTimeSteps = totalTime / dt[0];
constexpr int numNestedGrids = 3;
std::string outPaths[numNestedGrids];
for(int i = 0; i < numNestedGrids; ++i) {
outPaths[i] = baseFolder + "_" + std::to_string(i);
if(solve) {
std::filesystem::create_directory(outPaths[i]);
solver.setTimeStep(dt[i]);
solver.setOutputDir(outPaths[i]);
if(method == SolverMethod::FEM) {
solver.solve(totalTime);
} else if(method == SolverMethod::SemiLagrangianFEM) {
solver.semiLagrangianSolve(totalTime);
}
}
}
std::vector<double> commonTimeVelocity[3][totalTimeSteps];
int factor = 1;
for(int i = 0; i < numNestedGrids; ++i) {
for(int j = 0, k = 0; k < totalTimeSteps; j+=factor, k++) {
const std::string cachePath(outPaths[i] + std::string("/out_") + std::to_string(j) + std::string(".json"));
std::fstream cacheStream(cachePath);
assert(cacheStream.is_open());
nlohmann::basic_json cacheJSON;
cacheStream >> cacheJSON;
const int numNodes = cacheJSON["u"].size();
commonTimeVelocity[i][k].resize(numNodes * 2);
std::copy_n(cacheJSON["u"].begin(), numNodes, commonTimeVelocity[i][k].begin());
std::copy_n(cacheJSON["v"].begin(), numNodes, commonTimeVelocity[i][k].begin() + numNodes);
}
factor *= 10;
}
std::vector<double> convergenceOrder(totalTimeSteps);
for(int i = 0; i < totalTimeSteps; ++i) {
double normVV2 = 0, normV2V3 = 0;
for(int j = 0; j < commonTimeVelocity[0][i].size(); ++j) {
const double diff1 = commonTimeVelocity[0][i][j] - commonTimeVelocity[1][i][j];
const double diff2 = commonTimeVelocity[1][i][j] - commonTimeVelocity[2][i][j];
normVV2 += diff1 * diff1;
normV2V3 += diff2 * diff2;
}
normVV2 = std::sqrt(normVV2);
normV2V3 = std::sqrt(normV2V3);
if(normVV2 == 0 || normVV2 == 0) {
continue;
}
convergenceOrder[i] = std::log10(std::abs(normVV2 / normV2V3));
std::cout<<"Convergence: "<<convergenceOrder[i]<<" error: "<<std::pow(10, convergenceOrder[i]) * normVV2 / (std::pow(10, convergenceOrder[i])-1)<<"\n";
}
}
int main(int nargs, char** cargs) {
CMD::CommandLineArgs argParse;
argParse.addParam(
"sceneFile",
"Path to the file describing the simulation",
CMD::CommandLineArgs::Type::String,
true
);
argParse.addParam(
"numThreads",
"The number of threads which the simulator should use. All threads by default.",
CMD::CommandLineArgs::Type::Int,
false
);
argParse.addParam(
"outPath",
"Path to a folder where the resulting caches and images will be saved",
CMD::CommandLineArgs::Type::String,
false
);
if(nargs == 2 && strcmp(cargs[1], "-help") == 0) {
argParse.print(stdout);
return 0;
}
EC::ErrorCode error = argParse.parse(nargs, cargs);
if(error.hasError()) {
fprintf(stderr, "[Error] %s\n", error.getMessage());
fprintf(stderr, "Supported options\n");
argParse.print(stderr);
return 1;
}
int numThreads = [&]() -> int {
const int* threads = argParse.getIntVal("numThreads");
if(threads != nullptr) {
return *threads;
} else {
return tbb::task_scheduler_init::default_num_threads();
}
}();
tbb::global_control tbbMaxThreadsControl(tbb::global_control::max_allowed_parallelism, numThreads);
NSFem::NavierStokesAssembly<NSFem::P2, NSFem::P1> assembler;
error = assembler.init(
argParse.getStringVal("sceneFile"),
argParse.getStringVal("outPath")
);
if(error.hasError()) {
fprintf(stderr, "[Error] %s\n", error.getMessage());
return 1;
}
error = assembler.semiLagrangianSolve();
if(error.hasError()) {
printf("[Error] %s\n", error.getMessage());
return 1;
}
return 0;
}