-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.cpp
More file actions
204 lines (157 loc) · 4.59 KB
/
common.cpp
File metadata and controls
204 lines (157 loc) · 4.59 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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <cmath>
#include <random>
#include <vector>
#include <fstream>
#include <stdio.h>
#include <iostream>
#include <cstring>
#ifdef USE_MPI
#include <mpi.h>
#endif
#include "common.h"
#include "pes/pes.h"
using namespace std;
// I/O routines
void save(std::ofstream& fsave, agent_base_t* agent_bases, int num_agent_bases, region_t region) {
static bool first = true;
if (first) {
fsave << num_agent_bases << " ";
for (int d = 0; d<num_dim; d++) {
fsave << region.lo[d] << " " << region.hi[d] << " ";
}
fsave << std::endl;
first = false;
}
for (int i = 0; i < num_agent_bases; ++i) {
for (int d = 0; d<num_dim; d++) {
fsave << agent_bases[i].pos[d] << " ";
}
fsave << std::endl;
}
fsave << std::endl;
}
void save_molecular(std::ofstream& fsave, std::string* species, agent_base_t* agents, int num_agents, region_t region) {
static bool first = true;
if (first) {
fsave << num_agents << " ";
for (int d = 0; d < num_dim; d++) {
fsave << region.lo[d] << " " << region.hi[d] << " ";
}
fsave << std::endl;
first = false;
}
int num_atoms = num_dim / 3;
for (int i = 0; i < num_agents; ++i) {
fsave << num_atoms << std::endl;
fsave << std::endl;
for (int d = 0; d < num_atoms; d++) {
fsave << species[d] << " " << agents[i].pos[d * 3] << " " << agents[i].pos[d * 3 + 1] << " " << agents[i].pos[d * 3 + 2] << std::endl;
}
}
fsave << std::endl;
}
void save_polychrome(std::ofstream& fsave, agent_base_t** agent_bases, int* num_agent_bases,
int num_swarms, region_t region) {
static bool first = true;
if (first) {
for (int d = 0; d < num_dim; d++) {
fsave << region.lo[d] << " " << region.hi[d] << " ";
}
fsave << std::endl;
first = false;
}
fsave << num_swarms << " ";
for (int j = 0; j < num_swarms; j++) {
fsave << num_agent_bases[j] << " ";
}
fsave << std::endl;
for (int j = 0; j < num_swarms; j++) {
for (int i = 0; i < num_agent_bases[j]; ++i) {
for (int d = 0; d < num_dim; d++) {
fsave << agent_bases[j][i].pos[d] << " ";
}
fsave << std::endl;
}
}
fsave << std::endl;
}
void factor (int* sizes, int num_proc, int num_dimensions) {
int prod = num_proc;
int nx = (int)ceil(pow(num_proc, 1.0 / num_dimensions));
if (nx < 1) {
nx = 1;
}
for (int d = 0; d < num_dimensions; d++) {
int size = nx;
while ( (prod%size != 0) && (size < prod) ) { size++; }
if (size >= prod) { size = prod; }
prod /= size;
sizes[d] = size;
}
}
void get_indices (int* indices, int* sizes, int n, int num_dimensions) {
int denominator = 1;
for (int d = 0; d < num_dimensions; d++) {
int index = (n / denominator) % sizes[d];
denominator *= sizes[d];
indices[d] = index;
}
}
// Particle Initialization
void init_agents(agent_base_t* agent_bases, int num_agent_bases, region_t region) {
std::random_device rd;
std::mt19937 gen(rd());
int lengths[num_dim];
int num_to_factor = num_agent_bases;
factor(lengths, num_to_factor, num_dim);
std::vector<int> shuffle(num_agent_bases);
for (int i = 0; i < shuffle.size(); ++i) {
shuffle[i] = i;
}
for (int i = 0; i < num_agent_bases; ++i) {
// Make sure particles are not spatially sorted
std::uniform_int_distribution<int> rand_int(0, num_agent_bases - i - 1);
int j = rand_int(gen);
int k = shuffle[j];
shuffle[j] = shuffle[num_agent_bases - i - 1];
// Distribute particles evenly to ensure proper spacing
int indices[num_dim];
get_indices (indices, lengths, i, num_dim);
for (int d = 0; d < num_dim; d++) {
double size = region.hi[d] - region.lo[d];
int index_invert = num_dim - d - 1;
agent_bases[i].pos[d] = size * (0.5 + indices[index_invert]) / (1.0 * lengths[index_invert]) + region.lo[d];
}
// Assign random velocities within a bound
std::uniform_real_distribution<float> rand_real(-1.0, 1.0);
for (int d = 0; d < num_dim; d++) {
agent_bases[i].vel[d] = rand_real(gen);
}
}
for (int i = 0; i < num_agent_bases; ++i) {
agent_bases[i].id = i;
}
}
// 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;
}