-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
154 lines (123 loc) · 4.24 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
147
148
149
150
151
152
153
154
#include "common.h"
#include <chrono>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iostream>
#include <random>
#include <vector>
#include <omp.h>
// =================
// Helper Functions
// =================
// I/O routines
void save(std::ofstream& fsave, particle_t* parts, int num_parts, double size) {
static bool first = true;
if (first) {
fsave << num_parts << " " << size << "\n";
first = false;
}
for (int i = 0; i < num_parts; ++i) {
fsave << parts[i].x << " " << parts[i].y << "\n";
}
fsave << std::endl;
}
// Particle Initialization
void init_particles(particle_t* parts, int num_parts, double size, int part_seed) {
std::random_device rd;
std::mt19937 gen(part_seed ? part_seed : rd());
int sx = (int)ceil(sqrt((double)num_parts));
int sy = (num_parts + sx - 1) / sx;
std::vector<int> shuffle(num_parts);
for (int i = 0; i < shuffle.size(); ++i) {
shuffle[i] = i;
}
for (int i = 0; i < num_parts; ++i) {
// Make sure particles are not spatially sorted
std::uniform_int_distribution<int> rand_int(0, num_parts - i - 1);
int j = rand_int(gen);
int k = shuffle[j];
shuffle[j] = shuffle[num_parts - i - 1];
// Distribute particles evenly to ensure proper spacing
parts[i].x = size * (1. + (k % sx)) / (1 + sx);
parts[i].y = size * (1. + (k / sx)) / (1 + sy);
// Assign random velocities within a bound
std::uniform_real_distribution<float> rand_real(-1.0, 1.0);
parts[i].vx = rand_real(gen);
parts[i].vy = rand_real(gen);
}
}
// Command Line Option Processing
int find_arg_idx(int argc, char** argv, const char* option) {
for (int i = 1; i < argc; ++i) {
if (strcmp(argv[i], option) == 0) {
return i;
}
}
return -1;
}
int find_int_arg(int argc, char** argv, const char* option, int default_value) {
int iplace = find_arg_idx(argc, argv, option);
if (iplace >= 0 && iplace < argc - 1) {
return std::stoi(argv[iplace + 1]);
}
return default_value;
}
char* find_string_option(int argc, char** argv, const char* option, char* default_value) {
int iplace = find_arg_idx(argc, argv, option);
if (iplace >= 0 && iplace < argc - 1) {
return argv[iplace + 1];
}
return default_value;
}
// ==============
// Main Function
// ==============
int main(int argc, char** argv) {
// Parse Args
if (find_arg_idx(argc, argv, "-h") >= 0) {
std::cout << "Options:" << std::endl;
std::cout << "-h: see this help" << std::endl;
std::cout << "-n <int>: set number of particles" << std::endl;
std::cout << "-o <filename>: set the output file name" << std::endl;
std::cout << "-s <int>: set particle initialization seed" << std::endl;
return 0;
}
// Open Output File
char* savename = find_string_option(argc, argv, "-o", nullptr);
std::ofstream fsave(savename);
// Initialize Particles
int num_parts = find_int_arg(argc, argv, "-n", 1000);
int part_seed = find_int_arg(argc, argv, "-s", 0);
double size = sqrt(density * num_parts);
particle_t* parts = new particle_t[num_parts];
init_particles(parts, num_parts, size, part_seed);
// Algorithm
auto start_time = std::chrono::steady_clock::now();
init_simulation(parts, num_parts, size);
#ifdef _OPENMP
#pragma omp parallel num_threads(16) default(shared)
//#pragma omp parallel num_threads(6)
// printf("max threads is %d\n", omp_get_max_threads());
// printf("current threads is %d\n", omp_get_num_threads());
#endif
{
for (int step = 0; step < nsteps; ++step) {
simulate_one_step(parts, num_parts, size);
// Save state if necessary
#ifdef _OPENMP
#pragma omp master
#endif
if (fsave.good() && (step % savefreq) == 0) {
save(fsave, parts, num_parts, size);
}
}
}
auto end_time = std::chrono::steady_clock::now();
std::chrono::duration<double> diff = end_time - start_time;
double seconds = diff.count();
// Finalize
std::cout << "Simulation Time = " << seconds << " seconds for " << num_parts << " particles.\n";
fsave.close();
delete[] parts;
}