Allink  v0.1
VarDataWrite.cpp
00001 /***********************************************************************
00002 VarData: This  Program reads and writes a specific file format 
00003 storing all the information relative to a set of equal structure
00004 polymers to the CHAIN, PART and GENERAL structures. It provides 
00005 two different ways to backfold the coordinates, a fanction that 
00006 creates an initial system with different option and some function
00007 for the data analisys. The first calculate the distribution of the
00008 monomer in the box, the second the distribution of the bonds.
00009 Copyright (C) 2008 by Giovanni Marelli <sabeiro@virgilio.it>
00010 
00011 
00012 This program is free software; you can redistribute it and/or modify
00013 it under the terms of the GNU General Public License as published by
00014 the Free Software Foundation; either version 2 of the License, or
00015 (at your option) any later version.
00016 
00017 This program is distributed in the hope that it will be useful,
00018 but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 GNU General Public License for more details.
00021 
00022 You should have received a copy of the GNU General Public License
00023 along with this program; if not, write to the Free Software
00024 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 ***********************************************************************/
00026 #include "../include/VarData.h"
00027 ;
00028 #ifdef USE_BOOST
00029 void VarData::OpenComprWrite(char *FName,filtering_ostream ZipFile){
00030    ofstream OpenZipFile(FName, ios_base::binary);
00031    // create filter:
00032    ZipFile.push(bzip2_compressor());
00033    ZipFile.push(OpenZipFile);
00034 }
00035 void VarData::OpenComprRead(char *FName,filtering_istream ZipFile){
00036    ifstream OpenZipFile(FName, ios_base::binary);
00037    // create filter:
00038    ZipFile.push(bzip2_compressor());
00039    ZipFile.push(OpenZipFile);
00040 }
00041 void VarData::CloseCompr(filtering_istream ZipFile){
00042   ZipFile.reset();
00043 }
00044 void VarData::CloseCompr(filtering_ostream ZipFile){
00045   ZipFile.reset();
00046 }
00047 #endif
00048 bool VarData::Write(char *OutFile){
00049   if( SysFormat == VAR_SYS_TXVL ){
00050     if(WriteTxvl(OutFile))
00051       return 1;
00052   }
00053   else if( SysFormat == VAR_SYS_XVT ){
00054     if(WriteXvt(OutFile))
00055       return 1;
00056   }
00057   else 
00058     WriteXyz(OutFile);
00059   return 0;
00060 }
00061 bool VarData::WriteTxvl(char *OutFile){
00062   VarMessage("Write");
00063   FILE *FileToWrite = fopen(OutFile,"w");
00064   SigErr(FileToWrite==NULL,"The output file %s could not be opened\n",OutFile);
00065   fprintf(FileToWrite,"# ");
00066   if( VAR_IF_TYPE(SysType,VAR_EDGE) ){
00067     fprintf(FileToWrite,"l( %.2g %.2g %.2g) ",
00068        Gen->Edge[0],Gen->Edge[1],Gen->Edge[2]);
00069   }
00070   fprintf(FileToWrite,"c[%d] ",Gen->NChain);
00071   fprintf(FileToWrite,"s[%d] ",Gen->Step);
00072   fprintf(FileToWrite,"n[%d] ",Gen->NPart);
00073   fprintf(FileToWrite,"L[%d] ",Gen->NLink);
00074   fprintf(FileToWrite,"v[%d] ",NEdge);
00075   fprintf(FileToWrite,"d[%s] ",cWhat2Draw);
00076   fprintf(FileToWrite,"D(%lf) ",Gen->Deltat);
00077   fprintf(FileToWrite,"i(%.2f %.2f %.2f) ",Gen->rho,Gen->chiN,Gen->kappaN);
00078   fprintf(FileToWrite,"\n");
00079   HeaderNano(FileToWrite);
00080   for(int p=0;p<Gen->NPart;p++){
00081     fprintf(FileToWrite,"{ t[%d %d %d] ",
00082        Pm[p].Idx,Pm[p].CId,Pm[p].Typ);
00083     fprintf(FileToWrite,"x(%lf %lf %lf) ",
00084        Pm[p].Pos[0],Pm[p].Pos[1],Pm[p].Pos[2]);
00085     fprintf(FileToWrite,"v(%lf %lf %lf) ",
00086        Pm[p].Vel[0],Pm[p].Vel[1],Pm[p].Vel[2]);
00087     //   printf("Pm[%d].NLink %d\n",p,Pm[p].NLink);
00088     if(pNLink() > 0){
00089       for(int i=0;i<Ln[p].NLink;i++)
00090    fprintf(FileToWrite,"l[%d] ",Ln[p].Link[i]);
00091     }
00092     fprintf(FileToWrite,"}\n");//End of particle info
00093   }
00094   // for(int n=0;n<Gen->NNano;n++){
00095   //   fprintf(FileToWrite,"{t[%d %d %d] ",Gen->NPart-1+n,-1,2);
00096   //   fprintf(FileToWrite,"x(%lf %lf %lf) ",Nano[n].Pos[0],Nano[n].Pos[1],Nano[n].Pos[2]);
00097   //   fprintf(FileToWrite,"v(%lf %lf %lf)}\n",0.,0.,0.);
00098   // }
00099   fclose(FileToWrite);
00100   return 0;
00101 }
00102 void VarData::HeaderInteraction(FILE *FileToWrite){
00103   double Norm2 = CUBE(pReOverCutOff()) / SQR(pNPCh());
00104   double Norm3 = CUBE(SQR(pReOverCutOff()) / pNPCh());
00105   MInt->Rescale(1./Norm2,2);
00106   MInt->Rescale(1./Norm3,3);
00107   fprintf(FileToWrite,"# v=%.2f %.2f %.2f %.2f %.2f %.2f ",MInt->Coeff(0,0),MInt->Coeff(0,1),MInt->Coeff(0,2),MInt->Coeff(1,1),MInt->Coeff(1,2),MInt->Coeff(2,2));
00108   fprintf(FileToWrite,"w=%.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f\n",MInt->Coeff(0,0,0),MInt->Coeff(0,0,1),MInt->Coeff(0,0,2),MInt->Coeff(0,1,1),MInt->Coeff(0,1,2),MInt->Coeff(0,2,2),MInt->Coeff(1,1,1),MInt->Coeff(1,1,2),MInt->Coeff(1,2,2),MInt->Coeff(2,2,2));
00109   fprintf(FileToWrite,"# a2=%.1f a3=%.1f Re=%.1f N=%d ks=%.3f kb=%.3f l0=0\n",Gen->WFuncStraight2,Gen->WFuncStraight3,Gen->ReOverCutOff,Gen->NPCh,Gen->kappaSpring,Gen->kappaBend);
00110   MInt->Rescale(Norm2,2);
00111   MInt->Rescale(Norm3,3);
00112 }
00113 void VarData::ShapeId(int iShape,char *Shape){
00114   sprintf(Shape,"no");
00115   if(VAR_IF_TYPE(iShape,SHAPE_SPH)) sprintf(Shape,"sph");
00116   else if(VAR_IF_TYPE(iShape,SHAPE_TIP)) sprintf(Shape,"tip");
00117   else if(VAR_IF_TYPE(iShape,SHAPE_CYL)) sprintf(Shape,"cyl");
00118   else if(VAR_IF_TYPE(iShape,SHAPE_PILL)) sprintf(Shape,"pill");
00119   else if(VAR_IF_TYPE(iShape,SHAPE_TILT)) sprintf(Shape,"tilt");
00120   else if(VAR_IF_TYPE(iShape,SHAPE_WALL)) sprintf(Shape,"wall"); 
00121   else if(VAR_IF_TYPE(iShape,SHAPE_PORE)) sprintf(Shape,"pore"); 
00122   else if(VAR_IF_TYPE(iShape,SHAPE_EXT)) sprintf(Shape,"ext"); 
00123   else if(VAR_IF_TYPE(iShape,SHAPE_JANUS)) sprintf(Shape,"janus");
00124   else if(VAR_IF_TYPE(iShape,SHAPE_STALK)) sprintf(Shape,"stalk");
00125   else if(VAR_IF_TYPE(iShape,SHAPE_TORUS)) sprintf(Shape,"torus");
00126   else if(VAR_IF_TYPE(iShape,SHAPE_HARM)) sprintf(Shape,"harm");
00127   else if(VAR_IF_TYPE(iShape,SHAPE_UMBR)) sprintf(Shape,"umbr");
00128   else if(VAR_IF_TYPE(iShape,SHAPE_BOUND)) sprintf(Shape,"bound");
00129 }
00130 void VarData::StringNano(char *String,int n){
00131   char Shape[60];
00132   ShapeId(Nano[n].Shape,Shape);
00133   if(VAR_IF_TYPE(Nano[n].Shape,SHAPE_CLUSTER)){ 
00134     sprintf(Shape,"Architecture%d.dat",n);
00135     sprintf(String,"# Pep x(%.2f %.2f %.2f) a(%.2f %.2f %.2f) g(%.2f %.2f %.2f) i[%d] d[%d %d] fn{%s}",Nano[n].Pos[0],Nano[n].Pos[1],Nano[n].Pos[2],Nano[n].Axis[0],Nano[n].Axis[1],Nano[n].Axis[2],Nano[n].Rad,Nano[n].Height,Nano[n].Hamaker,n,Nano[n].NCircle,Nano[n].NHeight,Shape);
00136   }
00137   else if(VAR_IF_TYPE(Nano[n].Shape,SHAPE_CLINKS)){ 
00138     sprintf(Shape,"CrossLinks.dat");
00139     sprintf(String,"# Ext i[%d] fn{%s}",0,Shape);
00140   }
00141   else 
00142     sprintf(String,"# Rigid x(%.2f %.2f %.2f) a(%.2f %.2f %.2f) c(%.2f %.2f %.2f %.4f) s{%s}",Nano[n].Pos[0],Nano[n].Pos[1],Nano[n].Pos[2],Nano[n].Axis[0],Nano[n].Axis[1],Nano[n].Axis[2],Nano[n].Rad,Nano[n].Hamaker,Nano[n].Height,Nano[n].OffSet,Shape);
00143 }
00144 int VarData::HeaderNano(FILE *FileToWrite){
00145   if(Gen->NNano == 0) return 1;
00146   for(int n=0;n<Gen->NNano;n++){
00147     if(Nano->Shape == SHAPE_NONE)continue;
00148     char String[120];
00149     StringNano(String,n);
00150     fprintf(FileToWrite,"%s\n",String);
00151   }
00152   return 0;
00153 }
00154 int VarData::HeaderSoft(char *Line){
00155   if(NSoft == 0) return 1;
00156   for(int n=0;n<NSoft;n++){
00157     char Shape[60];
00158     sprintf(Shape,"no");
00159     if(Soft[n].Topology == VAR_PLANAR) sprintf(Shape,"planar");
00160     if(Soft[n].Topology == VAR_VESICLE) sprintf(Shape,"vesicle");
00161     if(Soft[n].Topology == VAR_COATING) sprintf(Shape,"coating");
00162     if(Soft[n].Topology == VAR_DISTRIBUTED) sprintf(Shape,"distributed");
00163     sprintf(Line,"# Soft x(%.2f %.2f %.2f) c(%.2f %.2f) s{%s} n{%s}\n",Soft[n].Pos[0],Soft[n].Pos[1],Soft[n].Pos[2],Soft[n].Size[0],Soft[n].Size[1],Shape,Soft[n].Name);
00164   }
00165   return 0;
00166 }
00167 bool VarData::WriteXvt(char *OutFile){
00168   VarMessage("Write");
00169   FILE *FileToWrite = NULL;
00170   char Line2Put[512];
00171   FileToWrite = fopen(OutFile,"w");
00172   SigErr(FileToWrite==NULL,"The output file %s could not be opened\n",OutFile);
00173   fprintf(FileToWrite,"# L=%lf %lf %lf t=%lf blocks=%d\n",Gen->Edge[0],Gen->Edge[1],Gen->Edge[2],pTime(),Gen->NBlock);
00174   HeaderInteraction(FileToWrite);
00175   HeaderNano(FileToWrite);
00176   for(int b=0,cOff=0,pCurr=0;b<Gen->NBlock;b++,cOff+=Block[b].NChain){
00177     if(Block[b].NChain == 0) continue;
00178     fprintf(FileToWrite,"# n=%d N=%d name=%s\n",Block[b].NChain,Block[b].NPCh,Block[b].Name);
00179     for(int c=cOff;c<Block[b].NChain+cOff;c++){
00180       for(int p=0;p<Block[b].NPCh;p++,pCurr++){
00181    // if(p > 0){
00182    //   for(int d=0;d<3;d++){
00183    //     if(Pm[pCurr].Pos[d] - Pm[pCurr-1].Pos[d] > .5*pEdge(d))
00184    //       Pm[pCurr].Pos[d] -= pEdge(d);
00185    //     else if(Pm[pCurr].Pos[d] - Pm[pCurr-1].Pos[d] < -.5*pEdge(d))
00186    //       Pm[pCurr].Pos[d] += pEdge(d);
00187    //   }
00188    // }
00189    fprintf(FileToWrite,"%lf %lf %lf ",
00190       pPos(pCurr,0),pPos(pCurr,1),pPos(pCurr,2));
00191    fprintf(FileToWrite,"%lf %lf %lf ",
00192       Pm[pCurr].Vel[0],Pm[pCurr].Vel[1],Pm[pCurr].Vel[2]);
00193    fprintf(FileToWrite," %d ",
00194       Pm[pCurr].Typ);
00195    fprintf(FileToWrite,"\n");
00196       }
00197     }
00198   }
00199   fclose(FileToWrite);
00200   return 0;
00201 }
00202 bool VarData::WriteXyz(char *OutFile){
00203   VarMessage("Write");
00204   FILE *FileToWrite = NULL;
00205   if( (FileToWrite = fopen(OutFile,"w")) == 0){
00206     printf("The output file %s could not be opened\n",OutFile);
00207     return 0;
00208   }
00209   for(int p=0;p<Gen->NPart;p++){
00210     fprintf(FileToWrite,"%lf %lf %lf\n",
00211        Pm[p].Pos[0],Pm[p].Pos[1],Pm[p].Pos[2]);
00212   }
00213   fclose(FileToWrite);
00214   return 0;
00215 }
00216 void VarData::WriteLinkedSurf(FILE *FWrite,double *Plot,int Values,int NType,double *Bound,int* PId){
00217   int p = PId[0];
00218   int c = PId[1];
00219   int t = PId[2];
00220   double InvValues = 1./(double)Values;
00221   int link[4] = {0,0,0,0};
00222   for(int vv=0;vv<Values-1;vv++){
00223     for(int v=0;v<Values-1;v++){
00224       link[0] = (v+0)*Values + (vv+0);
00225       if(Plot[(link[0])*NType+t] < .1) continue;
00226       //------------defines-the-squares---------------------
00227       link[1] = (v+0)*Values + (vv+1);
00228       link[2] = (v+1)*Values + (vv+0);
00229       link[3] = (v+1)*Values + (vv+1);
00230       int pRef = p;
00231       for(int lx=0;lx<2;lx++){
00232    for(int ly=0;ly<2;ly++){
00233      int l = 2*lx+ly;
00234      double NanoAdded = Plot[(link[l])*NType+2]/Bound[2]+Plot[(link[l])*NType+3]/Bound[3];
00235      double Phob = t == 0 ? Plot[(link[l])*NType+0]/Bound[0] : 0.;
00236      double Phil = t == 1 ? Plot[(link[l])*NType+1]/Bound[1] : 0.;
00237      double r = (v +lx)*InvValues*pEdge(3);
00238      double z = (vv+ly)*InvValues*(pEdge(CNorm));
00239      double dens = (Plot[(link[l])*NType+t]);//+Plot[(v*Values+vv)*NType+1]+Plot[(v*Values+vv)*NType+2]);
00240      int l1 = pRef + (p+1)%4;
00241      int l2 = pRef + (p+2)%4;
00242      int l3 = pRef + (p+3)%4;
00243      fprintf(FWrite,"{t[%d %d %d] x(%lf %lf %lf) v(%lf %lf %lf) l[%d] l[%d] l[%d] }\n",p,c,t,r,z,dens,NanoAdded,Phob,Phil,l1,l2,l3);
00244      p++;
00245    }
00246       }
00247       c++;
00248     }
00249   }
00250   PId[0] = p;
00251   PId[1] = c;
00252   PId[2] = t;
00253   return ;
00254   double Threshold = .1;
00255   int *IdSerial = (int *)calloc(Values*Values*2,sizeof(int));
00256   for(int vv=0,p=0;vv<Values-1;vv++){
00257     for(int v=0;v<Values-1;v++){
00258       int l = (vv+0)*Values + (v+0);
00259       IdSerial[l] = p;
00260       printf("%d %d %d %d\n",l,p,v,vv);
00261       if(Plot[l*NType+t] < Threshold) continue;
00262       p++;
00263     }
00264   }
00265   for(int vv=0,p=0,c=0;vv<Values-1;vv++){
00266     for(int v=0;v<Values-1;v++){
00267       link[0] = (v+0)*Values + (vv+0);
00268       if(Plot[(link[0])*NType+t] < Threshold) continue;
00269       //------------defines-the-squares---------------------
00270       link[1] = (vv+0)*Values + (v+1);
00271       link[2] = (vv+1)*Values + (v+0);
00272       link[3] = (vv+1)*Values + (v+1);
00273       printf("%d %d %d %d\n",link[0],link[1],link[2],link[3]);
00274       for(int l=0;l<4;l++){
00275    link[l] = IdSerial[link[l]];
00276       }
00277       printf("%d %d %d %d\n",link[0],link[1],link[2],link[3]);
00278       double NanoAdded = Plot[(link[0])*NType+2]/Bound[2]+Plot[(link[0])*NType+3]/Bound[3];
00279       double Phob = t == 0 ? Plot[(link[0])*NType+0]/Bound[0] : 0.;
00280       double Phil = t == 1 ? Plot[(link[0])*NType+1]/Bound[1] : 0.;
00281       double r = (v)*InvValues*pEdge(3);
00282       double z = (vv)*InvValues*(pEdge(CNorm));
00283       double dens = (Plot[(link[0])*NType+t]);//+Plot[(v*Values+vv)*NType+1]+Plot[(v*Values+vv)*NType+2]);
00284       fprintf(FWrite,"{t[%d %d %d] x(%lf %lf %lf) v(%lf %lf %lf) l[%d] l[%d] l[%d] }\n",link[0],c,t,r,z,dens,NanoAdded,Phob,Phil,link[1],link[2],link[3]);
00285       p++;
00286       c++;
00287    }
00288   }
00289   free(IdSerial);
00290 }
00291 void VarData::WriteSurf(FILE *F2Write,double **Plot,int NSample,int OffSet){
00292   double InvNSample = 1./(double)NSample;
00293   for(int sx=0;sx<NSample;sx++){
00294     for(int sy=0;sy<NSample;sy++){
00295       double x = (sx+.5)*pEdge(CLat1)*InvNSample;
00296       double y = (sy+.5)*pEdge(CLat2)*InvNSample;
00297       int l01 = (sx+0)*NSample+(sy+1)+OffSet;
00298       if(sy == NSample - 1)
00299    l01 = (sx+0)*NSample+(0)+OffSet;
00300       int l10 = (sx+1)*NSample+(sy+0)+OffSet;
00301       if(sx == NSample - 1)
00302    l10 = (0)*NSample+(sy+0)+OffSet;
00303       int lm0 = (sx-1)*NSample+(sy+0)+OffSet;
00304       if(sx == 0)
00305    lm0 = (NSample - 1)*NSample+(sy+0)+OffSet;
00306       int l0m = (sx+0)*NSample+(sy-1)+OffSet;
00307       if(sy == 0)
00308    l0m = (sx)*NSample+(NSample-1)+OffSet;
00309       fprintf(F2Write,"{x(%lf %lf %lf) l[%d] l[%d] l[%d] l[%d]}\n",x,y,Plot[sx][sy],l01,l10,lm0,l0m);
00310     }
00311   }
00312 }