Trisurf Monte Carlo simulator
Samo Penic
2016-02-16 e297565ae34d6cc62f41810a261adbeef576af7b
commit | author | age
854cb6 1 #include<stdio.h>
SP 2 #include<general.h>
3 #include<snapshot.h>
22721d 4 #include<stdlib.h>
SP 5 #include<stdarg.h>
6 #include <zlib.h>
f0bcea 7 #include<inttypes.h>
11fef8 8 #include<config.h>
2e8c7f 9 #include <time.h>
f0bcea 10
22721d 11 ts_uint ts_sprintf(ts_string *str, char *fmt, ...){
SP 12     va_list ap;
13     va_start(ap,fmt);
14     ts_uint n=vsprintf(&(str->string[str->beg]),fmt,ap);
15     va_end(ap);
16     str->beg+=n;
17     return n;
18 }
19
20
854cb6 21 ts_bool xml_trisurf_data(FILE *fh, ts_vesicle *vesicle){
22721d 22
SP 23     ts_string *data=(ts_string *)malloc(sizeof(ts_sprintf));
24     data->string=(char *)malloc(512000*sizeof(char)); /*TODO: warning, can break if the string is to long */
25     data->beg=0;
26     
854cb6 27     xml_trisurf_header(fh, vesicle);
22721d 28     xml_trisurf_tria(data,vesicle->tlist);
SP 29     xml_trisurf_tria_neigh(data,vesicle->tlist);
30     xml_trisurf_vtx_neigh(data,vesicle->vlist);    
31     xml_trisurf_vtx_tristar(data,vesicle->vlist);
32 #ifdef COMPRESSION
f0bcea 33     char *compressed;
588bbb 34     ts_uint nbytes=ts_compress_string64(data->string, data->beg-1, &compressed); //suppress null character at the end with by substracting 1
f0bcea 35     fwrite(compressed, sizeof(unsigned char), nbytes, fh);
SP 36     free (compressed);
22721d 37 #else
SP 38     fprintf(fh,"%s", data->string);
39 #endif
40     free(data->string);
41     free(data);
854cb6 42     xml_trisurf_footer(fh);
SP 43     return TS_SUCCESS;
44 }
45
46 ts_bool xml_trisurf_header(FILE *fh, ts_vesicle *vesicle){
2e8c7f 47 /* format current time */
SP 48     time_t current_time;
49         char *c_time_string;
50     current_time = time(NULL);
51     c_time_string = ctime(&current_time);
e29756 52     
SP 53     fprintf(fh, "<trisurfversion>Trisurf (commit %s), compiled on %s %s</trisurfversion>\n",TS_VERSION, __DATE__,  __TIME__);
2e8c7f 54     fprintf(fh, "<dumpdate>%s</dumpdate>\n", c_time_string);
SP 55     //free (c_time_string);
56
f0bcea 57     fprintf(fh, "<tape>\n");
SP 58     
59     fprintf(fh, "</tape>\n");
854cb6 60     fprintf(fh, "<trisurf nvtx=\"%u\" npoly=\"%u\" nfono=\"%u\">\n", vesicle->vlist->n, vesicle->poly_list->n, vesicle->poly_list->poly[0]->vlist->n);
SP 61     return TS_SUCCESS;
62 }
63
64 ts_bool xml_trisurf_footer(FILE *fh){
65     fprintf(fh, "</trisurf>\n");
66     return TS_SUCCESS;
67 }
68
22721d 69 ts_bool xml_trisurf_tria(ts_string *data, ts_triangle_list *tlist){
854cb6 70     ts_uint i;
588bbb 71     ts_sprintf(data,"<tria>");
854cb6 72     for(i=0; i<tlist->n;i++){
588bbb 73         ts_sprintf(data,"%u %u %u",tlist->tria[i]->vertex[0]->idx, tlist->tria[i]->vertex[1]->idx, tlist->tria[i]->vertex[2]->idx);
854cb6 74     }
588bbb 75     ts_sprintf(data,"</tria>");
854cb6 76     return TS_SUCCESS;
SP 77 }
78
22721d 79 ts_bool xml_trisurf_tria_neigh(ts_string *data, ts_triangle_list *tlist){
854cb6 80     ts_uint i;
22721d 81     ts_sprintf(data,"<trianeigh>\n");
854cb6 82     for(i=0; i<tlist->n;i++){
588bbb 83         ts_sprintf(data,"%u %u %u",tlist->tria[i]->neigh[0]->idx, tlist->tria[i]->neigh[1]->idx, tlist->tria[i]->neigh[2]->idx);
854cb6 84     }
22721d 85     ts_sprintf(data,"</trianeigh>\n");
854cb6 86     return TS_SUCCESS;
SP 87 }
88
22721d 89 ts_bool xml_trisurf_vtx_neigh(ts_string *data, ts_vertex_list *vlist){
854cb6 90     ts_uint i,j;
SP 91     for(i=0;i<vlist->n;i++){
22721d 92         ts_sprintf(data,"<vtxn idx=\"%u\">",vlist->vtx[i]->idx);
854cb6 93         for(j=0;j<vlist->vtx[i]->neigh_no;j++){
22721d 94             ts_sprintf(data,"%u ",vlist->vtx[i]->neigh[j]->idx);
854cb6 95         }
588bbb 96         ts_sprintf(data, "</vtxn>");
854cb6 97     }
SP 98     return TS_SUCCESS;
99 }
100
22721d 101 ts_bool xml_trisurf_vtx_tristar(ts_string *data, ts_vertex_list *vlist){
854cb6 102     ts_uint i,j;
SP 103     for(i=0;i<vlist->n;i++){
22721d 104         ts_sprintf(data,"<tristar idx=\"%u\">",vlist->vtx[i]->idx);
854cb6 105         for(j=0;j<vlist->vtx[i]->tristar_no;j++){
22721d 106             ts_sprintf(data,"%u ",vlist->vtx[i]->tristar[j]->idx);
854cb6 107         }
588bbb 108         ts_sprintf(data, "</tristar>");
854cb6 109     }
SP 110     return TS_SUCCESS;
111 }
112
f0bcea 113
SP 114
115
116 /* UTILITIES */
117
118 /* zlib compression base64 encoded */
119 /* compressed must not be pre-malloced */
120 ts_uint ts_compress_string64(char *data, ts_uint data_len, char **compressed){
121     z_stream defstream;
122     defstream.zalloc = Z_NULL;
123     defstream.zfree = Z_NULL;
124     defstream.opaque = Z_NULL;
125     defstream.avail_in = data_len+1;
126     defstream.next_in = (unsigned char *)data;    
127     char *compr=(char *)malloc(data_len*sizeof(char *));
128     defstream.avail_out = data_len+1;
129     defstream.next_out = (unsigned char *)compr;
130     deflateInit(&defstream, Z_BEST_COMPRESSION);
131         deflate(&defstream, Z_FINISH);
132         deflateEnd(&defstream);
133     /*base64 encode*/
134     size_t nbase;
135     *compressed=base64_encode((unsigned char *)compr,(size_t)defstream.total_out,&nbase);
136     //fwrite(base64, sizeof(unsigned char), nbase, fh);
137     free(compr);
138     return nbase;
139 }
140
141
142 /* base64 encoding, taken from http://stackoverflow.com/questions/342409/how-do-i-base64-encode-decode-in-c */
143 static char encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
144                                 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
145                                 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
146                                 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
147                                 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
148                                 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
149                                 'w', 'x', 'y', 'z', '0', '1', '2', '3',
150                                 '4', '5', '6', '7', '8', '9', '+', '/'};
151 static char *decoding_table = NULL;
152 static int mod_table[] = {0, 2, 1};
153
154
155 char *base64_encode(const unsigned char *data,
156                     size_t input_length,
157                     size_t *output_length) {
158
159     *output_length = 4 * ((input_length + 2) / 3);
160     int i,j;
161     char *encoded_data = malloc(*output_length);
162     if (encoded_data == NULL) return NULL;
163     
164     for (i = 0, j = 0; i < input_length;) {
165
166             uint32_t octet_a = i < input_length ? (unsigned char)data[i++] : 0;
167             uint32_t octet_b = i < input_length ? (unsigned char)data[i++] : 0;
168             uint32_t octet_c = i < input_length ? (unsigned char)data[i++] : 0;
169
170             uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
171
172             encoded_data[j++] = encoding_table[(triple >> 3 * 6) & 0x3F];
173             encoded_data[j++] = encoding_table[(triple >> 2 * 6) & 0x3F];
174             encoded_data[j++] = encoding_table[(triple >> 1 * 6) & 0x3F];
175             encoded_data[j++] = encoding_table[(triple >> 0 * 6) & 0x3F];
176     }
177
178     for (i = 0; i < mod_table[input_length % 3]; i++)
179             encoded_data[*output_length - 1 - i] = '=';
180
181     return encoded_data;
182 }
183
184
185 unsigned char *base64_decode(const char *data,
186                              size_t input_length,
187                              size_t *output_length) {
188
189     int i,j;
190     if (decoding_table == NULL) build_decoding_table();
191
192     if (input_length % 4 != 0) return NULL;
193
194     *output_length = input_length / 4 * 3;
195     if (data[input_length - 1] == '=') (*output_length)--;
196     if (data[input_length - 2] == '=') (*output_length)--;
197
198     unsigned char *decoded_data = malloc(*output_length);
199     if (decoded_data == NULL) return NULL;
200
201     for (i = 0, j = 0; i < input_length;) {
202
203             uint32_t sextet_a = data[i] == '=' ? 0 & i++ : decoding_table[(int)data[i++]];
204             uint32_t sextet_b = data[i] == '=' ? 0 & i++ : decoding_table[(int)data[i++]];
205                uint32_t sextet_c = data[i] == '=' ? 0 & i++ : decoding_table[(int)data[i++]];
206             uint32_t sextet_d = data[i] == '=' ? 0 & i++ : decoding_table[(int)data[i++]];
207
208             uint32_t triple = (sextet_a << 3 * 6)
209             + (sextet_b << 2 * 6)
210             + (sextet_c << 1 * 6)
211             + (sextet_d << 0 * 6);
212
213             if (j < *output_length) decoded_data[j++] = (triple >> 2 * 8) & 0xFF;
214             if (j < *output_length) decoded_data[j++] = (triple >> 1 * 8) & 0xFF;
215             if (j < *output_length) decoded_data[j++] = (triple >> 0 * 8) & 0xFF;
216     }
217
218     return decoded_data;
219 }
220
221
222 void build_decoding_table() {
223
224     decoding_table = malloc(256);
225     int i;
226     for (i = 0; i < 64; i++)
227         decoding_table[(unsigned char) encoding_table[i]] = i;
228 }
229
230
231 void base64_cleanup() {
232     free(decoding_table);
233 }
234
235
236
237
238
239
240