Trisurf Monte Carlo simulator
Samo Penic
2016-02-29 e87e4e4fcbba57d36dfd17b92f413ccf2c1b322a
commit | author | age
8c1bb1 1 #include <stdio.h>
SP 2 #include <string.h>
3 #include <stdlib.h>
4 #include <libxml/xmlmemory.h>
5 #include <libxml/parser.h>
6 #include <general.h>
7 #include <restore.h>
8 #include <snapshot.h>
9 #include <zlib.h>
10 #include "vesicle.h"
487968 11 #include "vertex.h"
a011d2 12 #include "triangle.h"
a3dbf0 13 #include "bond.h"
SP 14 #include "energy.h"
15 #include "initial_distribution.h"
a011d2 16
8c1bb1 17 ts_bool parseDump(char *dumpfname) {
SP 18     xmlDocPtr doc;
8fa1c0 19     xmlNodePtr cur, cur1,cur2;
SP 20     ts_vesicle *vesicle=NULL;
8c1bb1 21
SP 22     doc = xmlParseFile(dumpfname);
23     
24     if (doc == NULL ) {
25         fatal("Dump file could not be found or parsed. It is correct file?",1);
26     }
27     
28     cur = xmlDocGetRootElement(doc);
29     
30     if (cur == NULL) {
31         fatal("Dump file is empty.",1);
32     }
33     
34     if (xmlStrcmp(cur->name, (const xmlChar *) "VTKFile")) {
35         fatal("document of the wrong type, root node != story",1);
36     }
37     
38     cur = cur->xmlChildrenNode;
39     while (cur != NULL) {
40         if ((!xmlStrcmp(cur->name, (const xmlChar *)"trisurf"))){
3c772b 41             vesicle=parseTrisurfTag(doc, cur);
8c1bb1 42         }
8fa1c0 43         // START Point Position data &  Bonds
SP 44         if ((!xmlStrcmp(cur->name, (const xmlChar *)"UnstructuredGrid"))){
45             cur1 = cur->xmlChildrenNode;
46             while(cur1!=NULL){
47                 if ((!xmlStrcmp(cur1->name, (const xmlChar *)"Piece"))){
48                     cur2=cur1->xmlChildrenNode;
49                     while(cur2!=NULL){
50                         if ((!xmlStrcmp(cur2->name, (const xmlChar *)"Points"))){
51                             fprintf(stderr,"Found point data\n");
52                             if(vesicle!=NULL)
53                                 parseXMLVertexPosition(vesicle, doc, cur);
54                         }
55                         if ((!xmlStrcmp(cur2->name, (const xmlChar *)"Cells"))){
56                         fprintf(stderr,"Found cell(Bonds) data\n");
a3dbf0 57                             if(vesicle!=NULL)
SP 58                                 parseXMLBonds(vesicle, doc, cur);
8fa1c0 59                         }
SP 60                         cur2=cur2->next;
61                     }    
62                 }
63                 cur1 = cur1->next;
64             }
65         }
66         // END Point Position data & Bonds
8c1bb1 67     cur = cur->next;
SP 68     }
69     
70     xmlFreeDoc(doc);
a3dbf0 71     init_normal_vectors(vesicle->tlist);
SP 72     mean_curvature_and_energy(vesicle);
73
74 /* TODO: cells, polymeres, filaments, core, tape */
75
8c1bb1 76     fprintf(stderr,"Restoration completed\n");
3c772b 77     vesicle_free(vesicle);
e87e4e 78     exit(0);
8c1bb1 79     return TS_SUCCESS;
SP 80 }
81
8fa1c0 82
SP 83
84 /* this is a parser of additional data in xml */
8c1bb1 85 ts_vesicle *parseTrisurfTag(xmlDocPtr doc, xmlNodePtr cur){
SP 86     fprintf(stderr,"Parsing trisurf tag\n");
487968 87     xmlNodePtr child;
SP 88
89 #ifdef COMPRESS
8c1bb1 90     /* base64decode */
SP 91     size_t cLen;
92     /*size_t tLen;
93     const unsigned char test[]="Test";
94     char *cTest=base64_encode(test, 4,&tLen);
95     unsigned char *cuTest=base64_decode((char *)cTest,tLen,&tLen);
96     cuTest[tLen]=0;
97     fprintf(stderr,"%s\n",cuTest);
98     */
99     xmlChar *b64=xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
100     unsigned char *compressed=base64_decode((char *)b64,strlen((char *)b64)-1,&cLen);
101     /* uncompress */
102     unsigned char *subtree=(unsigned char *)malloc(512000*sizeof(unsigned char)); /* TODO: again, the uncompressed string must not exceed this */
103     z_stream infstream;
104     infstream.zalloc = Z_NULL;
105     infstream.zfree = Z_NULL;
106     infstream.opaque = Z_NULL;
107     infstream.avail_in = (ts_uint)cLen; // size of input
108         infstream.next_in = compressed; // input char array
109         infstream.avail_out = (ts_uint)512000; // size of output
110         infstream.next_out = subtree; // output char array
111      
112         // the actual DE-compression work.
113         inflateInit(&infstream);
114         inflate(&infstream, Z_NO_FLUSH);
115         inflateEnd(&infstream);    
116     fprintf(stderr,"%lu\n",cLen);
117     subtree[infstream.total_out]='\0'; //zero terminate string    
118     fprintf(stderr,"%s\n",subtree);
119     
120     free(subtree);
487968 121 #endif
8c1bb1 122     /*parse xml subtree */
SP 123     xmlChar *nvtx, *npoly, *nfono;
124     nvtx = xmlGetProp(cur, (xmlChar *)"nvtx");
125     npoly=xmlGetProp(cur, (xmlChar *)"npoly");
126     nfono=xmlGetProp(cur, (xmlChar *)"nfono");
127     fprintf(stderr,"nvtx=%u\n",atoi((char *)nvtx));
128     ts_vesicle *vesicle=init_vesicle(atoi((char *)nvtx),10,10,10,0.1);
129     //vesicle->poly_list=init_poly_list(atoi((char *)npoly),atoi((char *)nmono), vesicle->vlist, vesicle);
130     xmlFree(nvtx);
131     xmlFree(npoly);
132     xmlFree(nfono);
487968 133
SP 134     child = cur->xmlChildrenNode;
135     while (child != NULL) {
136         if ((!xmlStrcmp(child->name, (const xmlChar *)"vtxn"))){
137             parseTrisurfVtxn(vesicle->vlist, doc, child);
138         }
a011d2 139         if ((!xmlStrcmp(child->name, (const xmlChar *)"tria"))){
SP 140             parseTrisurfTria(vesicle, doc, child);
141         }
e9ac7b 142         if ((!xmlStrcmp(child->name, (const xmlChar *)"trianeigh"))){
SP 143             parseTrisurfTriaNeigh(vesicle, doc, child);
144         }
a011d2 145          if ((!xmlStrcmp(child->name, (const xmlChar *)"tristar"))){
SP 146             parseTrisurfTristar(vesicle, doc, child);
147         }
148
487968 149     child = child->next;
SP 150     }
151
152
153
8c1bb1 154     return vesicle;
SP 155 }
487968 156
a011d2 157
SP 158
159 /* Low level tags parsers */
160
161 ts_bool parseTrisurfVtxn(ts_vertex_list *vlist, xmlDocPtr doc, xmlNodePtr cur){
487968 162
SP 163     xmlChar *chari;
164     xmlChar *neighs;
165     char *n;
166     char *token;
167     ts_uint neighi;
168     ts_uint i;
169     chari = xmlGetProp(cur, (xmlChar *)"idx");
170     i=atoi((char *)chari);
171     xmlFree(chari);
172     ts_vertex *vtx=vlist->vtx[i];
173     neighs = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
174     //fprintf(stderr,"Found neigh for vtx %u that seems to have index %u with neighs=%s\n",i,vtx->idx,neighs);
175
176     n=(char *)neighs;
177     token=strtok(n," ");
178     while(token!=NULL){
179         neighi=atoi(token);
180         //fprintf(stderr,"%u", neighi);
181         vtx_add_neighbour(vtx,vlist->vtx[neighi]);
182         token=strtok(NULL," ");
183     }    
184     xmlFree(neighs);
185     return TS_SUCCESS;
186 }
187
a011d2 188 ts_bool parseTrisurfTria(ts_vesicle *vesicle, xmlDocPtr doc, xmlNodePtr cur){
SP 189     xmlChar *triangles;
190     char *tria;
191     char *vtx[3];
192     
e87e4e 193     ts_uint i,j;
a011d2 194     triangles = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
SP 195     tria=(char *)triangles;
e87e4e 196     vtx[0]=strtok(tria," ");
SP 197     for(i=1;i<3;i++)    vtx[i]=strtok(NULL," ");
198     j=0;
a011d2 199     while(vtx[2]!=NULL){
SP 200         triangle_add(vesicle->tlist, vesicle->vlist->vtx[atoi(vtx[0])],vesicle->vlist->vtx[atoi(vtx[1])],vesicle->vlist->vtx[atoi(vtx[2])]);
201         for(i=0;i<3;i++)    vtx[i]=strtok(NULL," ");
e87e4e 202         j++;
a011d2 203     }    
e87e4e 204     fprintf(stderr,"Parsing triangles %s j=%d\n",triangles,j);    
a011d2 205
SP 206     xmlFree(triangles);
207     return TS_SUCCESS;
208 }
209
210
e9ac7b 211 ts_bool parseTrisurfTriaNeigh(ts_vesicle *vesicle, xmlDocPtr doc, xmlNodePtr cur){
SP 212     xmlChar *triangles;
213     char *tria;
214     char *ntria[3];
215     ts_uint i,j;
216     triangles = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
217     tria=(char *)triangles;
e87e4e 218     ntria[0]=strtok(tria," ");
SP 219     for(i=1;i<3;i++)    ntria[i]=strtok(NULL," ");
e9ac7b 220     j=0;
SP 221     while(ntria[2]!=NULL){
222         triangle_add_neighbour(vesicle->tlist->tria[j],vesicle->tlist->tria[atoi(ntria[0])]);
223         triangle_add_neighbour(vesicle->tlist->tria[j],vesicle->tlist->tria[atoi(ntria[1])]);
224         triangle_add_neighbour(vesicle->tlist->tria[j],vesicle->tlist->tria[atoi(ntria[2])]);
225         j++;
226         for(i=0;i<3;i++)    ntria[i]=strtok(NULL," ");
227     }    
228     fprintf(stderr,"Parsing triangle neighbors j=%d\n",j);    
229
230     xmlFree(triangles);
231     return TS_SUCCESS;
232 }
233
234
a011d2 235 ts_bool parseTrisurfTristar(ts_vesicle *vesicle, xmlDocPtr doc, xmlNodePtr cur){
SP 236
237     xmlChar *chari;
238     xmlChar *tristar;
239     char *t;
240     char *token;
241     ts_uint neighi;
242     ts_uint i;
243     chari = xmlGetProp(cur, (xmlChar *)"idx");
244     i=atoi((char *)chari);
245     xmlFree(chari);
246     ts_vertex *vtx=vesicle->vlist->vtx[i];
247     tristar = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
248 //    fprintf(stderr,"Found tristar for vtx %u that seems to have index %u with tristar=%s\n",i,vtx->idx,tristar);
249
250     t=(char *)tristar;
251     token=strtok(t," ");
252     while(token!=NULL){
253         neighi=atoi(token);
254         //fprintf(stderr,"%u", neighi);
255         vertex_add_tristar(vtx,vesicle->tlist->tria[neighi]);
256         token=strtok(NULL," ");
257     }    
258     xmlFree(tristar);
259     return TS_SUCCESS;
260 }
261
8fa1c0 262
SP 263 /* this is a parser of vertex positions and bonds from main xml data */
264 ts_bool parseXMLVertexPosition(ts_vesicle *vesicle,xmlDocPtr doc, xmlNodePtr cur){
265     xmlNodePtr child = cur->xmlChildrenNode;
266     xmlChar *points;
267     char *pts;
268     int i, idx;
269     char *token[3];
270     while (child != NULL) {
271         if ((!xmlStrcmp(child->name, (const xmlChar *)"DataArray"))){
272             points = xmlNodeListGetString(doc, child->xmlChildrenNode, 1);
273             pts=(char *)points;
e87e4e 274             token[0]=strtok(pts," ");
SP 275             for(i=1;i<3;i++)    token[i]=strtok(NULL," ");
8fa1c0 276             idx=0;
SP 277             while(token[0]!=NULL){
278                 vesicle->vlist->vtx[idx]->x=atof(token[0]);
279                 vesicle->vlist->vtx[idx]->y=atof(token[1]);
280                 vesicle->vlist->vtx[idx]->z=atof(token[2]);
281                 for(i=0;i<3;i++)    token[i]=strtok(NULL," ");    
282                 idx++;
283             }
284             xmlFree(points);
285         }
286         child=child->next;
287     }
288     return TS_SUCCESS;
289 }
a3dbf0 290 ts_bool parseXMLBonds(ts_vesicle *vesicle,xmlDocPtr doc, xmlNodePtr cur){
SP 291     xmlNodePtr child = cur->xmlChildrenNode;
292     xmlChar *bonds;
293     char *b;
294     int i, idx;
295     char *token[2];
296     while (child != NULL) {
297         if ((!xmlStrcmp(child->name, (const xmlChar *)"DataArray"))){
298             bonds = xmlNodeListGetString(doc, child->xmlChildrenNode, 1);
299             b=(char *)bonds;
e87e4e 300             token[0]=strtok(b," ");
SP 301             token[1]=strtok(NULL," ");
a3dbf0 302             idx=0;
SP 303             while(token[0]!=NULL){
304                 bond_add(vesicle->blist, vesicle->vlist->vtx[atoi(token[0])], vesicle->vlist->vtx[atoi(token[1])]);
305                 for(i=0;i<2;i++)    token[i]=strtok(NULL," ");    
306                 idx++;
307             }
308             xmlFree(bonds);
309         }
310         child=child->next;
311     }
312     return TS_SUCCESS;
313 }
314
8fa1c0 315
SP 316