-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.c
128 lines (99 loc) · 3.26 KB
/
io.c
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
#include "io.h"
void read_input_grid(int L, int ngrids, int *ising_grids){
// converts [0,1] to [-1,1]
const int blookup[2] = {-1, 1};
// Set filename
char filename[14];
sprintf(filename, "gridinput.bin");
uint32_t one = 1U;
// open file
FILE *ptr = fopen(filename, "rb");
if (ptr==NULL){
fprintf(stderr, "Error opening %s for input!\n", filename);
exit(EXIT_FAILURE);
}
// read header specifying size of grid
int Lcheck;
fread(&Lcheck, sizeof(int), 1, ptr);
if (Lcheck!=L) {
fprintf(stderr, "Error - size of grid in input file does not match L!\n");
exit(EXIT_FAILURE);
}
// Allocate space to read a single grid as bits
int nbytes = L*L/8;
if ( (L*L)%8 !=0 ) { nbytes++; }
char *bitgrid = (char *)malloc(nbytes);
if (bitgrid==NULL){
fprintf(stderr,"Error allocating input buffer!");
exit(EXIT_FAILURE);
}
// Read the grid
fread(bitgrid, sizeof(char), nbytes, ptr);
// Loop over grid points
int ibit=0, ibyte=0;
int isite=0, igrid;
//printf("nbytes = %d\n",nbytes);
for (ibyte=0;ibyte<nbytes;ibyte++){
for (ibit=0;ibit<8;ibit++){
//printf(" %2d ",blookup[(bitgrid[ibyte] >> ibit) & one]);
// Read into every copy of the grid
for (igrid=0;igrid<ngrids;igrid++){
ising_grids[L*L*igrid+isite] = blookup[(bitgrid[ibyte] >> ibit) & one];
}
isite++;
//if (isite%L==0) {printf("\n");}
}
if (isite>L*L) break;
}
free(bitgrid); // free input buffer
fclose(ptr); // close input file
fprintf(stderr, "Read initial configuration of all grids from gridinput.bin\n");
}
void write_ising_grids(int L, int ngrids, int *ising_grids, int isweep){
// Set filename
char filename[15];
sprintf(filename, "gridstates.bin");
//printf("%s\n",filename);
// open file
FILE *ptr = fopen(filename,"ab");
if (ptr==NULL){
FILE *ptr = fopen(filename,"wb"); // open for write if not available for append
if (ptr==NULL){
fprintf(stderr,"Error opening %s for write!\n",filename);
exit(EXIT_FAILURE);
}
}
// file header - size of grid, number of grids and current sweep
fwrite(&L,sizeof(int),1,ptr);
fwrite(&ngrids,sizeof(int),1,ptr);
fwrite(&isweep,sizeof(int),1,ptr);
// pack everything into as few bits as possible
int nbytes = L*L*ngrids/8;
if ( (L*L*ngrids)%8 !=0 ) { nbytes++; }
char *bitgrids = (char *)malloc(nbytes);
if (bitgrids==NULL){
fprintf(stderr,"Error allocating output buffer!");
exit(EXIT_FAILURE);
}
// Set zero
memset(bitgrids, 0U, nbytes);
uint8_t one = 1;
int ibit=0, ibyte=0, iint;
for (iint=0;iint<L*L*ngrids;iint++){ //loop over grid x squares per grid
if ( ising_grids[iint] == 1 ) {
bitgrids[ibyte] |= one << ibit;
//printf("Set bit %d of byte %d\n", ibit, ibyte);
}
ibit++;
if (ibit==8) {
ibit=0;
ibyte++;
}
}
// write to file
fwrite(bitgrids,sizeof(char),nbytes,ptr);
// Release memory
free(bitgrids);
// close file
fclose(ptr);
}