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