-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
83 lines (60 loc) · 2.34 KB
/
main.cpp
File metadata and controls
83 lines (60 loc) · 2.34 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
#include <math.h>
#include <fstream>
#include <iostream>
#include "rapidjson/document.h"
#include "lattice.h"
using namespace std;
static char* getCharFromString(const string& str){
char *charPtr = new char [str.length()+1];
strcpy(charPtr, str.c_str());
return charPtr;
}
static rapidjson::Document getJsonDocumentFromFile(const string& fileName){
ifstream fileStream(fileName);
if (!fileStream.is_open()) throw runtime_error("Could not open config file");
std::string tmp((istreambuf_iterator<char>(fileStream)),
istreambuf_iterator<char>());
const char* fileContent = getCharFromString(tmp);
rapidjson::Document document;
document.Parse<rapidjson::kParseCommentsFlag>(fileContent);
delete[] fileContent;
return document;
}
static std::map<std::string, double> getMapFromJsonDocument(rapidjson::Document& document) {
std::map<std::string, double> parametersMap;
std::list<std::string> parameters {"rho", "beta", "alpha", "theta", "kappa", "mu", "gamma", "sigma" };
for (auto& parameter: parameters) {
const char* parameterName = getCharFromString(parameter);
parametersMap.emplace(parameter, document[parameterName].GetDouble());
delete[] parameterName;
}
return parametersMap;
}
int main(int argc, char* argv[]){
if (argc == 1) {
std::cout << "You must enter the json file containing the parameters" << std::endl;
return 1;
}
bool skipFrames = false;
auto document = getJsonDocumentFromFile(argv[1]);
auto parameters = getMapFromJsonDocument(document);
size_t iterations = document["iterations"].GetInt();
size_t size = document["size"].GetInt();
if (document.HasMember("frameskips"))
skipFrames = true;
Lattice lattice(size, parameters);
for (size_t i=0; i<iterations; i++) {
std::cout << "iterations " + std::to_string(i) + "/" + std::to_string(iterations) << "\r";
lattice.diffuse();
lattice.freeze();
lattice.attach();
lattice.melt();
lattice.addNoise();
if (skipFrames) {
if (i % (document["frameskips"].GetInt()+1) == 0)
lattice.saveTo(document["filepath_prefix"].GetString()+std::string("_")+std::to_string(i));
}
}
lattice.saveTo(document["filepath_prefix"].GetString());
return 0;
}