-
Notifications
You must be signed in to change notification settings - Fork 4
/
Snapshot.h
238 lines (218 loc) · 6.06 KB
/
Snapshot.h
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#ifndef INCLUDED_SNAPSHOT_H
#define INCLUDED_SNAPSHOT_H
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <rpc/xdr.h>
#include <argp.h>
#include <vector>
#include <math.h>
#include <string.h>
//#include <tr1/unordered_map>
#include <unordered_map>
#include <string>
#include <sstream>
#include "HemeLBExtractionFileTypes.h"
#include "Site.h"
#include "Mapping.h"
class Column {
public:
Column(unsigned int num_records, double offset)
{
this->num_records = num_records;
this->offset = offset;
stats_calcd = false;
records = new double[num_records];
}
~Column()
{
delete[] records;
}
double get(uint64_t index)
{
if(index >= num_records) {
fprintf(stderr, "Error: index (%lu) exceeds number of records in column (%lu)\n", index, num_records);
exit(1);
}
return records[index];
}
void set_plus_offset(uint64_t index, float value)
{
if(index >= num_records) {
fprintf(stderr, "Error: index (%lu) exceeds number of records in column (%lu)\n", index, num_records);
exit(1);
}
records[index] = value + offset;
stats_calcd = false;
}
void print(FILE *outfile, uint64_t index) {
if(index >= num_records) {
fprintf(stderr, "Error: index (%lu) exceeds number of records in column (%lu)\n", index, num_records);
exit(1);
}
fprintf(outfile, "%e", records[index]);
}
void print_stats(FILE *outfile) {
calc_stats();
fprintf(outfile, "%e %e %e %e", average, stdev, max, min);
}
private:
uint64_t num_records;
double *records;
double offset;
bool stats_calcd;
double average;
double stdev;
double max, min;
void calc_stats()
{
if(!stats_calcd) {
double sum = 0, sum2 = 0;
max = -INFINITY;
min = INFINITY;
for(uint64_t i = 0; i < num_records; i++) {
sum += records[i];
sum2 += records[i] * records[i];
if(max < records[i]) {
max = records[i];
}
if(min > records[i]) {
min = records[i];
}
}
average = sum / num_records;
stdev = sqrt(sum2/num_records - average*average);
stats_calcd = true;
}
}
};
class Snapshot {
public:
Snapshot(HEADER *header, FIELD_HEADER *field_header)
{
this->num_sites = header->num_sites;
this->num_columns = header->num_columns;
this->voxelsz = header->voxelsz;
sites = new Site[num_sites];
columns = new Column*[num_columns];
unsigned int c = 0;
for(unsigned int i = 0; i < header->field_count; i++) {
for(unsigned int bro=0; bro < field_header[i].num_floats; bro++) {
columns[c] = new Column(num_sites, field_header[i].offset);
c++;
}
}
}
void set_timestep(double timestep)
{
this->timestep = timestep;
}
double get_timestep()
{
return timestep;
}
void site_set(unsigned int site_index, uint32_t x, uint32_t y, uint32_t z)
{
sites[site_index].set(x, y, z);
}
void column_set_plus_offset(uint32_t column_index, uint64_t site_index, double value)
{
if(column_index >= num_columns) {
fprintf(stderr, "Error: column index (%u) exceeds number of columns (%u)\n", column_index, num_columns);
exit(1);
}
columns[column_index]->set_plus_offset(site_index, value);
}
double get(uint32_t column_index, uint64_t site_index)
{
return columns[column_index]->get(site_index);
}
bool get_site_index(uint32_t a, uint32_t b, uint32_t c, uint64_t *site_index)
{
for(unsigned int i = 0; i < num_sites; i++) {
if(sites[i].equals(a, b, c)) {
*site_index = i;
return true;
}
}
// If no such site can be found, return false
return false;
}
int get_site_index(Site *s, uint64_t *site_index)
{
return get_site_index(s->x, s->y, s->z, site_index);
}
Site * get_sites()
{
return sites;
}
/* Very inefficient calculation of site_index corresponding to the given site. Only suitable for small data sets. */
SiteIndex * get_site_indices(Site *list, uint64_t list_size)
{
SiteIndex *indices = new SiteIndex[list_size];
for(uint64_t i = 0; i < list_size; i++) {
indices[i].exists = get_site_index(&list[i], &(indices[i].index));
}
return indices;
}
/** Builds hashtable to get indices corresponding to the given site. Necessary for very large data sets. */
SiteIndex * get_site_indices_hashed_lookup(Site *list, uint64_t list_size, bool bool_verbose)
{
// Build the hash table
if(bool_verbose == true) { fprintf(stderr, "# Building hashtable...\n"); }
//std::tr1::unordered_map<std::string, uint64_t> hashtable; //JM for C++11 capability
std::unordered_map<std::string, uint64_t> hashtable;
for(uint64_t i = 0; i < num_sites; i++) {
std::ostringstream oss;
oss << sites[i].x << "," << sites[i].y << "," << sites[i].z;
std::string hashstr = oss.str();
hashtable[hashstr] = i;
}
if(bool_verbose == true) { fprintf(stderr, "# ...done building hashtable\n"); }
if(bool_verbose == true) { fprintf(stderr, "# Calculating mapping...\n"); }
SiteIndex *indices = new SiteIndex[list_size];
for(uint64_t i = 0; i < list_size; i++) {
std::ostringstream oss;
oss << list[i].x << "," << list[i].y << "," << list[i].z;
std::string hashstr = oss.str();
if(hashtable.find(hashstr) != hashtable.end()) {
indices[i].exists = true;
indices[i].index = hashtable[hashstr];
} else {
indices[i].exists = false;
}
}
if(bool_verbose == true) { fprintf(stderr, "# ... done calculating mapping\n"); }
return indices;
}
void print(FILE *outfile)
{
for(uint64_t s = 0; s < num_sites; s++) {
fprintf(outfile, "%f ", timestep);
sites[s].print(outfile, this->voxelsz);
for(uint32_t c = 0; c < num_columns; c++) {
fprintf(outfile, " ");
columns[c]->print(outfile, s);
}
fprintf(outfile, "\n");
}
fprintf(outfile, "\n");
}
void print_stats(FILE *outfile)
{
fprintf(outfile, "%f", timestep);
for(uint32_t c = 0; c < num_columns; c++) {
fprintf(outfile, " ");
columns[c]->print_stats(outfile);
}
fprintf(outfile, "\n");
}
private:
double timestep;
uint64_t num_sites;
uint32_t num_columns;
double voxelsz;
Column **columns;
Site *sites;
};
#endif