-
Notifications
You must be signed in to change notification settings - Fork 4
/
HemeLBExtractionFile.h
606 lines (513 loc) · 17.4 KB
/
HemeLBExtractionFile.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
#ifndef INCLUDED_HEMELBEXTRACTIONFILE_H
#define INCLUDED_HEMELBEXTRACTIONFILE_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 "Snapshot.h"
#include "Vector3.h"
class HemeLBExtractionFile
{
public:
HemeLBExtractionFile(char *fname, double step_length, double scaling, bool verbose, Vector3 *translate)
{
this->step_length = step_length;
this->scaling = scaling;
this->bool_verbose = verbose;
bool_correctly_initialised = false;
snapshot = NULL;
in = fopen(fname, "rb");
if (in == NULL) {
fprintf(stderr, "Could not open file '%s'. Does it exist?\n", fname);
return;
}
xdrstdio_create(&xdrs, in, XDR_DECODE);
has_velocity = false;
has_pressure = false;
has_shearstress = false;
// Read the file header, populating the 'header' class variable. Determines whether file is "normal" or colloids
int r;
r = read_header();
if(r != 0) {
fprintf(stderr, "Problem reading header for file: '%s'.\n", fname);
return;
}
// If file is a colloids file, return now (no more info to be read in)
if(header->is_colloids_file == true) {
bool_correctly_initialised = true;
return;
}
/// Otherwise, if file is "normal", read the field header
r = read_field_header();
if(r != 0) {
fprintf(stderr, "Problem reading field header for file: '%s'.\n", fname);
return;
}
// If specified, convert lattice translation vector (real units) into grid units (via rounding based on voxel size)
if(translate != NULL) {
this->translate_grid_x = (int32_t)(translate->get_x()/header->voxelsz);
this->translate_grid_y = (int32_t)(translate->get_y()/header->voxelsz);
this->translate_grid_z = (int32_t)(translate->get_z()/header->voxelsz);
if(bool_verbose == true) {
fprintf(stderr, "Lattice translation vector (%e,%e,%e) in real units, converted to (%d,%d,%d) in grid units (voxel size = %e).\n", translate->get_x(), translate->get_y(), translate->get_z(), translate_grid_x, translate_grid_y, translate_grid_z, header->voxelsz);
}
} else {
this->translate_grid_x = 0;
this->translate_grid_y = 0;
this->translate_grid_z = 0;
}
// Allocate a snapshot of the right dimensions
snapshot = new Snapshot(header, field_header);
// Get the time of the next snapshot
bool next = read_time_next();
if(next == false) {
bool_no_more_snapshots = true;
} else {
bool_no_more_snapshots = false;
load_next_snapshot(); // Load in the first snapshot
bool_correctly_initialised = true;
}
}
~HemeLBExtractionFile()
{
xdr_destroy(&xdrs);
fclose(in);
}
bool read_time_next()
{
// Check if we have reached the end of the file
if (feof(in)) {
if(bool_verbose == true) {
fprintf(stderr, "# Reached end of file.\n");
}
return false;
}
// Work around for problem with xdr_u_long() not working
uint32_t dt1, dt2;
uint64_t step;
xdr_u_int(&xdrs, &dt1);
xdr_u_int(&xdrs, &dt2);
step = ((uint64_t)dt1)<<32 | dt2;
time_next = step * step_length;
return true;
}
int load_next_snapshot()
{
if(bool_no_more_snapshots == true) {
if(bool_verbose == true) {
fprintf(stderr, "Warning: No more snapshots left in file.\n");
}
return 1;
}
uint32_t gridposx, gridposy, gridposz;
float value;
snapshot->set_timestep(time_next);
for(unsigned int s = 0; s < header->num_sites; s++) {
// Read in grid position of site
xdr_u_int(&xdrs, &gridposx);
xdr_u_int(&xdrs, &gridposy);
xdr_u_int(&xdrs, &gridposz);
// Apply any requested translation of this xtr file directly to the site positions
gridposx += translate_grid_x;
gridposy += translate_grid_y;
gridposz += translate_grid_z;
snapshot->site_set(s, gridposx, gridposy, gridposz);
for(unsigned int i = 0; i < header->num_columns; i++) {
// Read value and add it to the appropriate data column (scaling it by the specified scaling)
xdr_float(&xdrs, &value);
snapshot->column_set_plus_offset(i, s, value * this->scaling);
}
}
bool next = read_time_next();
if(next == false) {
bool_no_more_snapshots = true;
if(bool_verbose == true) {fprintf(stderr, "No more snapshots. (Note that final snapshot may have been incomplete)\n");}
}
return 0;
}
// Workaround for XDR's broken long reading ability
void xdr_long(XDR *xdrs, uint64_t *ret)
{
uint32_t half1, half2;
xdr_u_int(xdrs, &half1);
xdr_u_int(xdrs, &half2);
*ret = ((uint64_t)half1)<<32 | half2; // Assume big-endianness
}
void read_and_print_colloids(FILE *outfile)
{
uint32_t headerLen, recordLen;
uint64_t dsetLen, timeStep;
uint64_t id, rank;
double X, Y, Z, VX, VY, VZ;
while(!feof(in)) {
// Reinitialise reading vars to catch partially written header
headerLen = 0;
recordLen = 0;
dsetLen = 0;
timeStep = 0;
// Read timestep header
xdr_u_int(&xdrs, &headerLen);
xdr_u_int(&xdrs, &recordLen);
xdr_long(&xdrs, &dsetLen);
xdr_long(&xdrs, &timeStep);
// Check for end of file here (to avoid spurious EOF output)
if(feof(in)) {
break;
}
if(recordLen <= 0) {
break;
}
if(bool_verbose == true) {
fprintf(outfile, "# headerLen: %u recordLen %u dsetLen %ld timeStep: %ld\n", headerLen, recordLen, dsetLen, timeStep);
}
// Calc. num particles from the record length data
uint32_t num_particles = dsetLen/recordLen;
for (uint32_t i = 0; i < num_particles; i++) {
// Read particle data
xdr_long(&xdrs, &id);
xdr_long(&xdrs, &rank);
xdr_double(&xdrs, &X);
xdr_double(&xdrs, &Y);
xdr_double(&xdrs, &Z);
xdr_double(&xdrs, &VX);
xdr_double(&xdrs, &VY);
xdr_double(&xdrs, &VZ);
// Rescale the positions by the scaling factor provided to hemeXtract
X *= this->scaling;
Y *= this->scaling;
Z *= this->scaling;
// fprintf(outfile, "TIME: %ld ID: %ld RANK: %ld A0: %e Ah: %e X: %e Y: %e Z: %e\n", timeStep, id, rank, A0, Ah, X, Y, Z);
fprintf(outfile, "%ld %ld %ld %.13e %.13e %.13e %.13e %.13e %.13e\n", timeStep, id, rank, X, Y, Z, VX, VY, VZ);
}
}
if(bool_verbose == true) {
fprintf(stderr, "# Reached end of file.\n");
}
}
bool correctly_initialised()
{
return bool_correctly_initialised;
}
bool is_colloids_file()
{
return header->is_colloids_file;
}
bool no_more_snapshots() {
return bool_no_more_snapshots;
}
uint64_t get_num_sites()
{
return header->num_sites;
}
double get_voxelsz()
{
return header->voxelsz;
}
bool hasVelocity()
{
return has_velocity;
}
bool hasShearStress()
{
return has_shearstress;
}
bool hasPressure()
{
return has_pressure;
}
double get_scaling()
{
return scaling;
}
double get_scalar_quantity(uint32_t column_index, uint64_t site_index)
{
return snapshot->get(column_index, site_index);
}
double get_interpolated_scalar_quantity(uint32_t column_index, lattice_map *map)
{
double average_existing = 0;
uint32_t num_existing = 0;
for(uint32_t i = 0; i < 8; i++) {
if(map->index[i].exists == true) {
average_existing += get_scalar_quantity(column_index, map->index[i].index);
num_existing++;
}
}
if(num_existing == 0) {
return 0;
}
average_existing /= num_existing;
// Get the scalars at the 8 lattice sites forming the cube
double s[8];
for(uint32_t i = 0; i < 8; i++) {
// If there was no corresponding site_index, the site was not measured, so set it to the average
// value of the existing sites in the map
if(map->index[i].exists == false) {
s[i] = average_existing;
} else {
s[i] = get_scalar_quantity(column_index, map->index[i].index);
}
}
// Trilinear interpolation within the cube
return s[0] * (1 - map->a) * (1 - map->b) * (1 - map->c)
+ s[1] * (map->a * (1 - map->b) * (1 - map->c))
+ s[2] * ((1 - map->a) * map->b * (1 - map->c))
+ s[3] * ((1 - map->a) * (1 - map->b) * map->c)
+ s[4] * (map->a * (1 - map->b) * map->c)
+ s[5] * ((1 - map->a) * map->b * map->c)
+ s[6] * (map->a * map->b * (1 - map->c))
+ s[7] * (map->a * map->b * map->c);
}
void get_vector_quantity(uint32_t column_index, uint64_t site_index, Vector3 *returned_val)
{
double vx, vy, vz;
vx = get_scalar_quantity(column_index, site_index);
vy = get_scalar_quantity(column_index + 1, site_index);
vz = get_scalar_quantity(column_index + 2, site_index);
returned_val->set(vx, vy, vz);
}
void get_interpolated_vector_quantity(uint32_t column_index, lattice_map *map, Vector3 *returned_val)
{
double vx, vy, vz;
vx = get_interpolated_scalar_quantity(column_index, map);
vy = get_interpolated_scalar_quantity(column_index + 1, map);
vz = get_interpolated_scalar_quantity(column_index + 2, map);
returned_val->set(vx, vy, vz);
}
void get_pressure(uint64_t site_index, double *returned_val)
{
*returned_val = get_scalar_quantity(column_pressure, site_index);
}
void get_velocity(uint64_t site_index, Vector3 *returned_val)
{
get_vector_quantity(column_velocity, site_index, returned_val);
}
void get_shearstress(uint64_t site_index, double *returned_val)
{
*returned_val = get_scalar_quantity(column_shearstress, site_index);
}
void get_interpolated_pressure(lattice_map *map, double *returned_val)
{
*returned_val = get_interpolated_scalar_quantity(column_pressure, map);
}
void get_interpolated_velocity(lattice_map *map, Vector3 *returned_val)
{
get_interpolated_vector_quantity(column_velocity, map, returned_val);
}
void get_interpolated_shearstress(lattice_map *map, double *returned_val)
{
*returned_val = get_interpolated_scalar_quantity(column_shearstress, map);
}
Site * get_sites()
{
return snapshot->get_sites();
}
SiteIndex * get_site_indices(Site *list, uint64_t list_size)
{
return snapshot->get_site_indices(list, list_size);
}
SiteIndex * get_site_indices_hashed_lookup(Site *list, uint64_t list_size)
{
return snapshot->get_site_indices_hashed_lookup(list, list_size, bool_verbose);
}
double get_time()
{
return snapshot->get_timestep();
}
double get_time_next()
{
return time_next;
}
void print_header(FILE *outfile)
{
fprintf(outfile, "\n# HEADER:\n# -------\n");
fprintf(outfile, "# version=%u\n", header->version);
fprintf(outfile, "# voxelsz=%f\n", header->voxelsz);
fprintf(outfile, "# originx=%f\n", header->originx);
fprintf(outfile, "# originy=%f\n", header->originy);
fprintf(outfile, "# originz=%f\n", header->originz);
fprintf(outfile, "# num_sites=%lu\n", header->num_sites);
fprintf(outfile, "# field_count=%u\n", header->field_count);
fprintf(outfile, "# field_header_length=%u\n", header->field_header_length);
}
void print_field_header(FILE *outfile)
{
fprintf(outfile, "\n# FIELD HEADERS:\n# -------\n");
for(unsigned int i = 0; i < header->field_count; i++) {
fprintf(outfile, "# fieldname=%s\n# num_floats=%u\n# offset=%lf\n# ---\n", field_header[i].name, field_header[i].num_floats, field_header[i].offset);
}
fprintf(outfile, "# Number of 'columns' per snapshot = %d\n", header->num_columns);
}
void print_column_headings(FILE *outfile)
{
fprintf(outfile, "step grid_x grid_y grid_z");
for(unsigned int i = 0; i < header->field_count; i++) {
if(field_header[i].num_floats == 1) {
fprintf(outfile, " %s", field_header[i].name);
} else {
for(unsigned int j = 0; j < field_header[i].num_floats; j++) {
fprintf(outfile, " %s(%d)", field_header[i].name, j);
}
}
}
fprintf(outfile, "\n");
}
void print_stats_column_headings(FILE *outfile)
{
fprintf(outfile, "# step");
for(unsigned int i = 0; i < header->field_count; i++) {
if(field_header[i].num_floats == 1) {
fprintf(outfile, " | %s [average | stdev | max | min]", field_header[i].name);
} else {
for(unsigned int j = 0; j < field_header[i].num_floats; j++) {
fprintf(outfile, " | %s(%d) [average | stdev | max | min]", field_header[i].name, j);
}
}
}
fprintf(outfile, "\n");
}
void print_all(FILE *outfile)
{
snapshot->print(outfile);
}
void print_stats(FILE *outfile)
{
snapshot->print_stats(outfile);
}
private:
/* True if file has been opened and the headers read without problem */
bool bool_correctly_initialised;
/* True if user wants verbose output. If false, means quiet. */
bool bool_verbose;
/* All info contained in the file header */
HEADER *header;
FIELD_HEADER *field_header;
/** The step length for this file (has to be provided by the user since Heme extraction files don't store this value...) */
double step_length;
/** The velocity scaling factor */
double scaling;
/** The currently loaded snapshot */
Snapshot *snapshot;
/** The open extraction file's stream */
FILE *in;
/** The XDR reader for this file */
XDR xdrs;
/** The time of the next snapshot (next to be loaded) */
double time_next;
/** True when no more snapshots can be loaded */
bool bool_no_more_snapshots;
/** True if the file contains these field types */
bool has_velocity, has_shearstress, has_pressure;
/** The column indices in which to find each field type */
uint32_t column_velocity;
uint32_t column_pressure;
uint32_t column_shearstress;
/** The translation vector (in grid units) to be applied to this lattice (if requested by --translate option) */
int32_t translate_grid_x;
int32_t translate_grid_y;
int32_t translate_grid_z;
/** Magic numbers designating a heme file, a heme extraction file and a colloid file */
static const uint32_t heme_magic = 0x686C6221; // hlb!
static const uint32_t extract_magic = 0x78747204; // xtr
static const uint32_t colloid_magic = 0x636F6C04; // colloid
/** Reads a HemeLB extraction file header. Checks that the magic numbers are correct. */
int read_header()
{
uint32_t magic1 = 0, magic2 = 0;
uint32_t num_sites1, num_sites2;
// Read heme magic number
xdr_u_int(&xdrs, &magic1);
if(magic1 != heme_magic) {
fprintf(stderr, "First uint32_t does not match heme magic number.\n");
return 1;
}
header = new HEADER();
// Read extraction magic number
xdr_u_int(&xdrs, &magic2);
if(magic2 == extract_magic) {
if(bool_verbose == true) {
fprintf(stderr, "Reading a normal extraction file.\n");
}
header->is_colloids_file = false;
xdr_u_int(&xdrs, &(header->version));
xdr_double(&xdrs, &(header->voxelsz));
xdr_double(&xdrs, &(header->originx));
xdr_double(&xdrs, &(header->originy));
xdr_double(&xdrs, &(header->originz));
// There is something wrong with either Heme or XDR which means that xdr_u_long is not working (possibly even reading more/less than 8 bytes...)
xdr_u_int(&xdrs, &num_sites1);
xdr_u_int(&xdrs, &num_sites2);
// Combine the two ints into a long (assuming bigendian-ness)
header->num_sites = ((uint64_t)num_sites1)<<32 | num_sites2;
xdr_u_int(&xdrs, &(header->field_count));
xdr_u_int(&xdrs, &(header->field_header_length));
} else if (magic2 == colloid_magic) {
if(bool_verbose == true) {
fprintf(stderr, "Reading a colloids extraction file.\n");
}
header->is_colloids_file = true;
// Check version
uint32_t version=0;
xdr_u_int(&xdrs, &version);
if(bool_verbose == true) {
printf("Colloids version %u\n", version);
}
} else {
fprintf(stderr, "Unknown format: Second uint32_t does not match extraction file or colloids file magic number.\n");
return 1;
}
// Everything went well
return 0;
}
/** Reads the field headers and remembers which columns correspond to velocity, pressure and shearstress. */
int read_field_header()
{
if(bool_verbose == true) {
printf("Reading field header...\n");
}
int const maxlength = 100; // surely it can't be bigger than this...?
field_header = new FIELD_HEADER[header->field_count];
for(unsigned int i = 0; i < header->field_count; i++) {
field_header[i].name = new char[maxlength];
xdr_string(&xdrs, &(field_header[i].name), maxlength);
xdr_u_int(&xdrs, &(field_header[i].num_floats));
xdr_double(&xdrs, &(field_header[i].offset));
}
if(bool_verbose == true) {
printf("Finished reading field header...\n");
}
// Calc. number of 'columns' needed to represent a snapshot
uint32_t num_columns = 0;
for(unsigned int i = 0; i < header->field_count; i++) {
// If file contains velocity, pressure or shearstress, remember the corresponding column index for that field type.
if(strcmp(field_header[i].name, "velocity") == 0) {
has_velocity = true;
column_velocity = num_columns;
} else if(strcmp(field_header[i].name, "pressure") == 0) {
has_pressure = true;
column_pressure = num_columns;
} else if(strcmp(field_header[i].name, "shearstress") == 0) {
has_shearstress = true;
column_shearstress = num_columns;
} else if(strcmp(field_header[i].name, "d_shearstress") == 0) { // For "legacy" reasons only. Some old MCA runs have this field.
has_shearstress = true;
column_shearstress = num_columns;
}
// This field has 'num_floats' columns to it
num_columns += field_header[i].num_floats;
}
header->num_columns = num_columns;
// Everything went well
return 0;
}
};
#endif