Allink  v0.1
ElPolyEl.cpp
00001 /***********************************************************************
00002 ElPoly:This progam provide a graphical visualisation of the data 
00003 opend by VarData using openGL glut. The most important option are 
00004 the possibility of changing the backfold of the polymers with 'c', 
00005 see the subsequent file in the list with '>', see the bond with 'b'. 
00006 Copyright (C) 2008 by Giovanni Marelli <sabeiro@virgilio.it>
00007 
00008 
00009 This program is free software; you can redistribute it and/or modify
00010 it under the terms of the GNU General Public License as published by
00011 the Free Software Foundation; either version 2 of the License, or
00012 (at your option) any later version.
00013 
00014 This program is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 GNU General Public License for more details.
00018 
00019 You should have received a copy of the GNU General Public License
00020 along with this program; if not, write to the Free Software
00021 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 ***********************************************************************/
00023 #include "ElPoly.h"
00024 ;
00025 
00026 ElPoly::ElPoly(int argc,char **argv,int *FilePos){
00027   cFile = (char **)calloc(argc,4);//sizeof(**cFile));
00028   if(cFile == NULL){printf("cFile not allocated\n");return;}
00029   for(int f=0;f<argc;f++){
00030     cFile[f] = (char *)calloc(STRSIZE,sizeof(*cFile));
00031     if(cFile[f] == NULL){printf("cFile not allocated\n");return;}
00032     cFile[f] = argv[FilePos[f]];
00033   }
00034   NFileTot = argc;
00035   NFile[0] = 0; NFile[1] = NFileTot;
00036   NPro =0;//?
00037   quando=0;
00038   if(!pNLink()) IfLine = 1;
00039   else IfLine = 0;
00040   // IfIntorno=0;//?
00041   // IfColour=0;//?
00042   // IfChains=0;//?
00043   // IfChType=0;//?
00044   // Diameter=0.1;//?
00045   // StepDiameter=0.;//?
00046   // ExtraDiam=0.;//?
00047   // Vicinanze=1.;//?
00048   InvScaleUn=1.;
00049   ScaleFact = 1.;
00050   LineSize = 1;
00051   NVisSkip = 1;
00052   NChType = CHAIN_EVERY;
00053   NBackFold = BF_PART;
00054   What2Draw = EL_PART;
00055   for(int d=0;d<3;d++) ShiftPos[d] = 0.;
00056   for(int d=0;d<3;d++) ScaleF[d] = 1.;
00057 #ifdef OMPI_MPI_H 
00058   MPI_Init(&argc,&argv);
00059   int Rank=0,Size=0;
00060   MPI_Comm_rank(MPI_COMM_WORLD, &Rank);
00061   MPI_Comm_size(MPI_COMM_WORLD, &Size);
00062   int Partition = (int)(NFileTot/(double)Size);
00063   NFile[0] = Partition*Rank;
00064   NFile[1] = Partition*(Rank+1);
00065   if(Rank==Size-1) NFile[1] += NFileTot%Size;
00066   Proc = new SingProc(Size,Rank);
00067 #endif
00068   //TrialSys();
00069 }
00070 ElPoly::~ElPoly(){
00071   // for(int f=0;f<NFileTot;f++)
00072   //   free(cFile[f]);
00073   // free(cFile);
00074 #ifdef OMPI_MPI_H 
00075   MPI_Finalize();
00076 #endif
00077 }
00078 int ElPoly::Angle(int Values){
00079   int NRadici = 4;
00080   int NZeri = NRadici;
00081   double *Radici;
00082   Radici = (double *)malloc(NRadici*sizeof(double));
00083   double *Average;
00084   Average = (double *)malloc(NRadici*sizeof(double));
00085   for(int i=0;i<NRadici;i++){
00086     Radici[i] = 0.;
00087     Average[i] = 0.;
00088   }
00089   for(int f=NFile[0];f<NFile[1];f++){
00090     Processing(f);
00091     if(OpenRisk(cFile[f],BF_PART)==0)return 1;
00092     Mat->Ypsilon = pEdge(2)-pCm(2)-1.;
00093     Mat->PreFact = pow((2.*pNPart()/10.)/DUE_PI,.33);
00094     Mat->Func = &Matematica::ContactAngle;
00095     printf("Volume  %lf Cm %lf # to Invert %lf\n",(double)pNPart()/10.,pCm(2),pow(DUE_PI/(2.*pNPart()/10.),.25));
00096     NZeri = Mat->Zeri(0.,DUE_PI/2.,Radici,NRadici);
00097     for(int i=0;i<NZeri;i++){
00098       Radici[i] *= 360./DUE_PI;
00099       Average[i] += Radici[i];
00100     }
00101     if(NZeri == 0){
00102       printf("The function has no real roots\n");
00103     }
00104   }
00105   for(int i=0;i<NZeri;i++){
00106     Average[i] /= NFile[1]-NFile[0];
00107     printf("Angle %lf\n",Average[i]);
00108   }
00109   return 0;
00110 }
00111 int ElPoly::CenterOfMass(int Coord){
00112   double SysCm;
00113   FILE *FileToWrite = NULL;
00114   FileToWrite = fopen("CenterOfMass.dat","w");
00115   for(int f=NFile[0];f<NFile[1];f++){
00116     Processing(f);
00117     if(OpenRisk(cFile[f],1)==0)return 1;
00118     SysCm=0.;
00119     if(Coord == 3){
00120       for(int p=0;p<pNPart();p++){
00121    SysCm += pVel(p,2);
00122       }
00123     }
00124     else{
00125       for(int p=0;p<pNPart();p++){
00126    SysCm += pPos(p,Coord);
00127       }
00128     }
00129       fprintf(FileToWrite,"%lf \n",SysCm/pNPart()); 
00130   }
00131   printf("\n"); 
00132   fclose(FileToWrite);
00133   return 0;
00134 }
00135 int ElPoly::ChangeFile(){
00136   printf("Introduce the first of %d file from the list: ",NFileTot);
00137   scanf("%d",&NFile[0]);
00138   if(NFile[0] < 0 || NFile[0] > NFile[1])
00139     NFile[0] = 0;
00140   quando = NFile[0];
00141   printf("Introduce the second of %d file from the list: ",NFileTot);
00142   scanf("%d",&NFile[1]);
00143   if(NFile[1] < NFile[0] || NFile[1] > NFileTot)
00144     NFile[1] = NFileTot;
00145   return 0;
00146 }
00147 int ElPoly::OpenFile(int f){
00148   if(f >= NFileTot || f < 0)
00149     return 0;
00150   if(Open(cFile[f],NBackFold)) return 1;
00151   NFile[0] = f;
00152   quando = f;
00153   return 0;
00154 }
00155 int ElPoly::OpenFile(char *FileName){
00156   if(Open(FileName,NBackFold))return 1;
00157   return 0;
00158 }
00159 double ElPoly::ContactAngle(double x){
00160   double Ris;
00161   Ris = QUAD( 2*x - QUAD(sin(2*x)) ) / (4./3. * CUB(sin(x))-2.*x*cos(x)+cos(x)*sin(2*x) ) - 5.;
00162   return Ris;
00163 }
00164 // OpenGl----------------------------------
00165 int ElPoly::PropertiesF(){
00166   Properties Pr;
00167   for(int f=NFile[0];f<NFile[1];f++){
00168     Processing(f);
00169     if(OpenRisk(cFile[f],BF_PART))return 0;
00170     Pr = Pr + SysProperties();
00171   }
00172   Pr = Pr * (1./(NFile[1] - NFile[0]));
00173   printf("\n");
00174   char *NomeFile = (char *) malloc(60*sizeof(char));
00175   sprintf(NomeFile,"PropertiesChi%.0fRho%.0fKappa%.0f.txt",pchiN(),prho(),pkappaN());
00176   FILE *FileToWrite = NULL;
00177   FileToWrite = fopen(NomeFile,"w");
00178   fprintf(FileToWrite,"RePhob %lf RePhil %lf\n",Pr.RePhob,Pr.RePhil);
00179   fprintf(FileToWrite,"RadGyrPhob %lf RadGyrPhil %lf\n",Pr.GyrPhob,Pr.GyrPhil);
00180   fprintf(FileToWrite,"StructFactPhob %lf StructFactPhil %lf\n",Pr.FactPhob,Pr.FactPhil);
00181   fprintf(FileToWrite,"VolPhob %lf VolPhil %lf\n",Pr.VolPhob,Pr.VolPhil);
00182   fprintf(FileToWrite,"ChainDiff %lf\n",Pr.ChDiff);
00183   Pr.Print();
00184   fclose(FileToWrite);
00185   return 0;
00186 }
00187 void ElPoly::DivideLayers(int How){
00188   int NLayer = 2;
00189   if(How == VAR_OPPOSED) NLayer = 4;
00190   int *NChStep = (int *)calloc(NLayer,sizeof(int));
00191   int *ChainLevel = (int *)calloc(pNChain(),sizeof(int));
00192   if(How == VAR_OPPOSED){
00193     for(int c=0,b=0;c<pNChain();c++){
00194       if(c*Block[b].NPCh > Block[b].EndIdx) b++;
00195       int Level = 0;
00196       if(CHAIN_IF_TYPE(Ch[c].Type,CHAIN_UP))
00197    Level = 1;
00198       if(!strcmp(Block[b].Name,"LIPID1")) Level += 2;
00199       NChStep[Level]++;
00200       ChainLevel[c] = Level;
00201     }
00202   }
00203   else if(How == VAR_TUBE || How == VAR_VESICLE){
00204     for(int c=0,b=0;c<pNChain();c++){
00205       if(c*Block[b].NPCh > Block[b].EndIdx) b++;
00206       int Level = 0;
00207       if(CHAIN_IF_TYPE(Ch[c].Type,CHAIN_OUTER))
00208    Level = 1;
00209       NChStep[Level]++;
00210       ChainLevel[c] = Level;
00211     }
00212   }
00213   //for(int c=0;c<pNChain();c++)printf("%lf %d %d\n",Ch[c].Pos[CNorm],c,ChainLevel[c]);
00214   for(int f=NFile[0];f<NFile[1];f++){
00215     Processing(f);
00216     if(OpenRisk(cFile[f],BF_NO))return ;
00217     PART *Pn = (PART *)calloc(pNPart(),sizeof(PART));
00218     int pp=0;
00219     for(int p=0;p<pNPart();p++){
00220       for(int d=0;d<3;d++){
00221       Pn[p].Pos[d] = pPos(p,d);
00222       Pn[p].Vel[d] = pVel(p,d);
00223       }
00224       Pn[p].Typ = pType(p);
00225     }
00226     for(int l=0;l<NLayer;l++){
00227       for(int p=0;p<pNPart();p++){
00228       if(ChainLevel[pChain(p)] == l){
00229         for(int d=0;d<3;d++){
00230        SetPos(pp,Pn[p].Pos);
00231        SetVel(pp,Pn[p].Vel);
00232         }
00233      SetType(pp,Pn[p].Typ);
00234         pp++;
00235       }
00236       }
00237     }
00238     DefBlock(NChStep,How);
00239     Write(cFile[f]);
00240     free(Pn);
00241   }
00242   printf("\n");
00243   free(ChainLevel);
00244   free(NChStep);
00245 }
00246 void ElPoly::RemoveChains(){
00247   int nChain = pNChain();
00248   for(int b=0;b<pNBlock();b++){
00249     for(int c=0;c<Block[b].NChain;c++){
00250       if(Ch[c].Pos[CLat1] > .4*pEdge(CLat1) && Ch[c].Pos[CLat1] < .6*pEdge(CLat1) ){
00251    SwapChain(c,nChain-1,b);
00252    nChain--;
00253    SetNPart(pNPart()-pNPCh());
00254       }
00255     }
00256     Block[b].NChain = nChain;
00257   }
00258   SetNChain(nChain);
00259   Block[0].EndIdx = Block[0].NChain*Block[0].NPCh;
00260   for(int b=1;b<pNBlock();b++){
00261     Block[b].InitIdx = Block[0].NChain*Block[b].NPCh + Block[b-1].EndIdx;
00262   }
00263   Write("cleft.dat");
00264 }
00265 // int ElPoly::DensPatch(int NPatch,int Values){
00266 //   int NType = 2;
00267 //   double *DensProf = (double *)calloc(NPatch*Values,sizeof(double));
00268 //   for(int f=NFile[0];f<NFile[1];f++){
00269 //    Processing(f);
00270 //     fprintf(stderr,"Processing: %d/%d %.1f %%\r",f,(NFile[1]-NFile[0]),(f-NFile[0])/(double)(NFile[1]-NFile[0])*100.);
00271 //     if(OpenRisk(cFile[f],BF_PARTICLE))return 0;
00272     
00273 //   }
00274 //   char *FileName = (char *)calloc(60,sizeof(char));
00275 //   sprintf(FileName,"ChainPArea%.0fRho%.0fChiN%.0fKappa.dat",Gen->rho,Gen->chiN,Gen->kappaN);
00276 //   FileToWrite = fopen(FileName,"w");
00277 //   fprintf(FileToWrite,"#%s\n",SysInfo());
00278 // }
00279 char *ElPoly::ChooseDraw(int ExtWhat2Draw){
00280   char *String = (char *)calloc(16,sizeof(char));
00281   if(ExtWhat2Draw == EL_PART)
00282     sprintf(String,"part");
00283   else if(ExtWhat2Draw == EL_PART)
00284     sprintf(String,"part");
00285   else if(ExtWhat2Draw == EL_CHAIN)
00286     sprintf(String,"chain");
00287   else if(ExtWhat2Draw == EL_DENS)
00288     sprintf(String,"dens");
00289   else if(ExtWhat2Draw == EL_QUAD)
00290     sprintf(String,"quad");
00291   else if(ExtWhat2Draw == EL_QUAD1)
00292     sprintf(String,"quad1");
00293   else if(ExtWhat2Draw == EL_POTENTIAL)
00294     sprintf(String,"pot");
00295   else if(ExtWhat2Draw == EL_SURF)
00296     sprintf(String,"surf");
00297   else if(ExtWhat2Draw == EL_CROSS)
00298     sprintf(String,"cross");
00299   else if(ExtWhat2Draw == EL_ISOIPSE)
00300     sprintf(String,"isoipse");
00301   else if(ExtWhat2Draw == EL_SAMPLE)
00302     sprintf(String,"sample");
00303   else if(ExtWhat2Draw == EL_COLOR)
00304     sprintf(String,"color");
00305   else if(ExtWhat2Draw == EL_SKIN)
00306     sprintf(String,"skin");
00307   else if(ExtWhat2Draw == EL_POLYGON)
00308     sprintf(String,"polygon");
00309   else if(ExtWhat2Draw == EL_ISOLEVEL)
00310     sprintf(String,"isolevel");
00311   else if(ExtWhat2Draw == EL_SQUAREMESH)
00312     sprintf(String,"squaremesh");
00313   return String;
00314 }
00315 void ElPoly::ChooseDraw(char *String){
00316   if(!strcmp(String,"part"))
00317     What2Draw = EL_PART;
00318   else if(!strcmp(String,"quad"))
00319     What2Draw = EL_QUAD;
00320   else if(!strcmp(String,"chain"))
00321     What2Draw = EL_CHAIN;
00322   else if(!strcmp(String,"dens"))
00323     What2Draw = EL_DENS;
00324   else if(!strcmp(String,"quad1"))
00325     What2Draw = EL_QUAD1;
00326   else if(!strcmp(String,"pot"))
00327     What2Draw = EL_POTENTIAL;
00328   else if(!strcmp(String,"surf"))
00329     What2Draw = EL_SURF;
00330   else if(!strcmp(String,"cross"))
00331     What2Draw = EL_CROSS;
00332   else if(!strcmp(String,"isoipse"))
00333     What2Draw = EL_ISOIPSE;
00334   else if(!strcmp(String,"sample"))
00335     What2Draw = EL_SAMPLE;
00336   else if(!strcmp(String,"color"))
00337     What2Draw = EL_COLOR;
00338   else if(!strcmp(String,"stalk"))
00339     What2Draw = EL_STALK;
00340   else if(!strcmp(String,"skin"))
00341     What2Draw = EL_SKIN;
00342   else if(!strcmp(String,"polygon"))
00343     What2Draw = EL_POLYGON;
00344   else if(!strcmp(String,"isolevel"))
00345     What2Draw = EL_ISOLEVEL;
00346   else if(!strcmp(String,"squaremesh"))
00347     What2Draw = EL_SQUAREMESH;
00348   else
00349     What2Draw = 0;
00350 }
00351 void ElPoly::SetBoundFile(int InitFile,int EndFile){
00352   if(InitFile < 0)
00353     NFile[0] = 0;
00354   else if(InitFile < EndFile){
00355     NFile[0] = InitFile;
00356     quando = InitFile;
00357   }
00358   else {
00359     printf("Wrong init file %d !< %d\n",InitFile,EndFile);
00360   }
00361   if(EndFile >= NFileTot)
00362     NFile[1] = NFileTot-1;
00363   else if(EndFile > InitFile)
00364     NFile[1] = EndFile;
00365   else {
00366     printf("Wrong end file %d !< %d\n",InitFile,EndFile);
00367   }
00368 }
00369 void ElPoly::Processing(int f){
00370   fprintf(stderr,"Processing: %s %d/%d %.1f %%\r",cFile[f],f,(NFile[1]-NFile[0]),(f-NFile[0])/(double)(NFile[1]-NFile[0])*100.);
00371 }
00372 void ElPoly::Shift2Center(){
00373   double sigma = 
00374     sqrt(QUAD(pReOverCutOff())/(pNPCh()-1)/3.);
00375   for(int f=NFile[0];f<NFile[1];f++){
00376     Processing(f);
00377     Open(cFile[f],BF_NO);
00378     // for(int d=0;d<3;d++){
00379     //   ShiftPos[d] = .5 - pCm(d)*pInvEdge(d);
00380     // }
00381     // ShiftRef(BF_CHAIN);
00382     double Temp = pEdge(CLat1);
00383     SetEdge(pEdge(CNorm),CLat1);
00384     SetEdge(Temp,CNorm);
00385     
00386     // FILE *FWrite = fopen("Response.dat","w");
00387     // fprintf(FWrite,"# L=%lf %lf %lf t=%lf blocks=%d\n",pEdge(0),pEdge(1),pEdge(2),pTime(),pNBlock());
00388     // HeaderInteraction(FWrite);
00389     // HeaderNano(FWrite);
00390     // fprintf(FWrite,"# n=%d N=%d name=%s\n",Block[0].NChain,Block[0].NPCh,Block[0].Name);
00391     // int NChain = 0;
00392     // for(int c=0;c<pNChain();c++){
00393     //   double Dist = 0.;
00394     //   for(int d=0;d<3;d++){
00395     //   Dist += SQR(Nano->Pos[d] - Ch[c].Pos[d]);
00396     //   }
00397     //   if(Dist > SQR(2.5*Nano->Rad)) continue;
00398     //   for(int p=c*pNPCh();p<(c+1)*pNPCh();p++){
00399     //   fprintf(FWrite,"%lf %lf %lf %lf %lf %lf %d\n",
00400     //      pPos(p,0),pPos(p,1),pPos(p,2),
00401     //      pVel(p,0),pVel(p,1),pVel(p,2),pType(p));
00402     //   }
00403     //   NChain++;
00404     // }
00405     // printf("Number of chains %d\n",NChain);
00406     // return;
00407 
00408     for(int p=0;p<pNPart();p++){
00409       double Pos[3] = {pPos(p,CNorm),pPos(p,CLat1),pPos(p,CLat2)};
00410       SetPos(p,Pos);
00411       // for(int d=0;d<3;d++){
00412       //    Pm[p].Vel[d] = Mat->Gaussiano(0.,sigma);
00413       // }
00414     }
00415     Write(cFile[f]);
00416   }
00417 }