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