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