Allink  v0.1
ElPolyProfDens.cpp
00001 /***********************************************************************
00002 ElPolyProfDens: 
00003 Copyright (C) 2008 by Giovanni Marelli <sabeiro@virgilio.it>
00004 
00005 
00006 This program is free software; you can redistribute it and/or modify
00007 it under the terms of the GNU General Public License as published by
00008 the Free Software Foundation; either version 2 of the License, or
00009 (at your option) any later version.
00010 
00011 This program is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 GNU General Public License for more details.
00015 
00016 You should have received a copy of the GNU General Public License
00017 along with this program; if not, write to the Free Software
00018 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ***********************************************************************/
00020 #include "ElPoly.h"
00024 int ElPoly::DensProf(int NBin,int NSample,int Coord){
00025   int NType = 3;
00026   double *DensityAv = (double *)calloc(NType*NBin,sizeof(double));
00027   double FreeE = 0.;
00028   double ChDensMean = 0.;
00029   double ChDensSqr  = 0.;
00030   SetEdge(.5*MIN(pEdge(CLat1),pEdge(CLat2)),3);
00031   double *VolEl = (double *)calloc(NBin,sizeof(double));
00032   VolumeCircSlab(VolEl,NBin);
00033   if(Coord > 3 || Coord < 0) return 1;
00034   for(int f=NFile[0];f<NFile[1];f++){
00035     Processing(f);
00036     //printf("Opening: %s\n",cFile[f]);
00037     if(Coord < 3)
00038       if(OpenRisk(cFile[f],BF_PART))return 0;
00039       else if(Coord == 3){
00040    if(OpenRisk(cFile[f],BF_NANO))return 0;
00041       }
00042     double RefZed = .5*pCm(CNorm);
00043     if(pNNano() > 0) RefZed = pNanoPos(0,CNorm);
00044     // if(Coord == 2 && VAR_IF_TYPE(SysType,VAR_MEMBRANE) )
00045     //   ShiftSys(SHIFT_CM);
00047     if(Coord < 3){
00048       //DensityProfile(Coord,NBin,NType,DensityAv);
00049       BfDefChain();
00050       int NPartition = NSample;
00051       double *Density = (double *)calloc(NPartition*NPartition*NType*NBin,sizeof(double));
00052       double Pos[4];
00053       char cLine[STRSIZE];
00054       for(int b=0;b<pNBlock();b++){
00055    if(!strncmp(Block[b].Name,"PEP",3) ) continue;
00056    if(!strcmp(Block[b].Name,"WATER") )  continue;
00057    for(int p=Block[b].InitIdx;p<Block[b].EndIdx;p++){
00058      int t = pType(p);
00059      for(int d=0;d<3;d++) Pos[d] = pPos(p,d);
00060      Pos[CNorm] = pPos(p,CNorm) - RefZed + .5*pEdge(CNorm);
00061      int vx = (int)(NPartition*Pos[CLat1]*pInvEdge(CLat2));
00062      if(vx < 0 || vx >= NBin) continue;
00063      int vy = (int)(NPartition*Pos[CLat2]*pInvEdge(CLat2));
00064      if(vy < 0 || vy >= NBin) continue;
00065      int vz = (int)(NBin*Pos[CNorm]*pInvEdge(CNorm));
00066      if(vz < 0 || vz >= NBin) continue;
00067      if(CHAIN_IF_TYPE(Ch[pChain(p)].Type,CHAIN_ADDED)) t = 2;
00068      if(!strncmp(Block[b].Name,"ADDED",5)) t = 2;
00069      if(!strncmp(Block[b].Name,"CHOL",4))  t = 2;
00070      Density[((vz*NType+t)*NPartition+vy)*NPartition+vx] += 1.;
00071    }
00072       }
00073       for(int vx=0;vx<NPartition;vx++){
00074    for(int vy=0;vy<NPartition;vy++){
00075      double Media = 0.;
00076      double Weight = 0.;
00077      for(int v=0;v<NBin;v++){
00078        Weight += Density[((v*NType+0)*NPartition+vy)*NPartition+vx];
00079        Media += v*Density[((v*NType+0)*NPartition+vy)*NPartition+vx];
00080      }
00081      int vMedia = Weight > 0 ? (int)(Media/Weight) : NBin/2;
00082      int vOffset = vMedia - NBin/2;
00083      for(int t=0;t<NType;t++){
00084        for(int v=0;v<NBin;v++){
00085          int vOther = v + vOffset;
00086          if(vOther < 0)
00087       vOther = NBin - vOther;
00088          if(vOther >= NBin)
00089       vOther = vOther - NBin;
00090          DensityAv[v*NType+t] += Density[((vOther*NType+t)*NPartition+vy)*NPartition+vx];
00091        }
00092      }
00093    }
00094       }
00095       free(Density);
00096     }
00097     else{
00098       Vettore NanoAxis(Nano[0].Axis[0],Nano[0].Axis[1],Nano[0].Axis[2]);
00099       Vettore PosRel(3);
00100       Vettore Dist(3);
00101       for(int b=0;b<pNBlock();b++){
00102    if(!strncmp(Block[b].Name,"PEP",3) ) continue;
00103    for(int p=0;p<pNPart();p++){
00104      for(int d=0;d<3;d++){
00105        PosRel.Set(remainder(pNanoPos(0,d)-pPos(p,d),pEdge(d)),d);
00106      }
00107      double RadDist = ABS(Dist.PerpTo3(&PosRel,&NanoAxis));
00108      int v = (int)(RadDist/pEdge(3)*NBin);
00109      if(v < 0 || v >= NBin) continue;
00110      int t = pType(p);
00111      if(CHAIN_IF_TYPE(Ch[pChain(p)].Type,CHAIN_ADDED)) t = 2;
00112      DensityAv[v*NType+t] += 1.;
00113    }
00114       }
00115     }
00116     ChDensMean += pNChain()/(pEdge(CLat1)*pEdge(CLat2));
00117     ChDensSqr  += SQR(pNChain()/(pEdge(CLat1)*pEdge(CLat2)));
00118   }
00119 #ifdef OMPI_MPI_H
00120   MPI_Allreduce(MPI_IN_PLACE,DensityAv,NBin, MPI_DOUBLE, MPI_SUM, Proc->CommGrid);
00121   int Rank=0;
00122   MPI_Comm_rank(Proc->CommGrid, &Rank);
00123   if(Rank==0){
00124 #endif
00125     printf("\n");
00126     double NormaF = (NFile[1] - NFile[0]);
00127     double VolumeSlab = (pEdge(0)*pEdge(1)*pEdge(2))/(double)NBin;
00128     for(int v=0;v<NBin;v++){
00129       for(int t=0;t<NType;t++){
00130    if(Coord < 3) 
00131      DensityAv[v*NType+t] /= VolumeSlab*NormaF;
00132    else 
00133      DensityAv[v*NType+t] /= NormaF*VolEl[v];
00134       }
00135     }
00136     char FileName[256];
00137     sprintf(FileName,"DensityChi%.0fKappa%.0fRho%.0f.dat",pchiN(),pkappaN(),prho());
00138     FILE *FileToWrite = fopen(FileName,"w");
00139     char cSystem[STRSIZE];
00140     SysDef(cSystem);
00141     double ChDensErr = sqrt(ChDensSqr - SQR(ChDensMean)/NormaF )/NormaF;
00142     fprintf(FileToWrite,"# %s\n",cSystem);
00143     fprintf(FileToWrite,"# ChainDens %lf %lf\n",ChDensMean/NormaF,ChDensErr);
00144     for(int v=0;v<NBin;v++){
00145       fprintf(FileToWrite,"%lf %lf %lf %lf\n",(double)v/(double)NBin*pEdge(Coord),DensityAv[v*NType],DensityAv[v*NType+1],DensityAv[v*NType+2]);
00146     }
00147     fclose(FileToWrite);
00148 #ifdef OMPI_MPI_H
00149   }
00150 #endif
00151   free(DensityAv);
00152   return 0;
00153 }
00157 void ElPoly::DensProfNormalSlab(int NBin,int NSample,int Coord){
00158   int NType = 3;
00159   double *DensityAv = (double *)calloc(NType*NBin,sizeof(double));
00160   double FreeE = 0.;
00161   double ChDensMean = 0.;
00162   double ChDensSqr  = 0.;
00163   SetEdge(.5*MIN(pEdge(CLat1),pEdge(CLat2)),3);
00164   double *VolEl = (double *)calloc(NBin,sizeof(double));
00165   VolumeCircSlab(VolEl,NBin);
00166   if(Coord > 3 || Coord < 0) return;
00167   for(int f=NFile[0];f<NFile[1];f++){
00168     Processing(f);
00169     //printf("Opening: %s\n",cFile[f]);
00170     if(Coord < 3)
00171       if(OpenRisk(cFile[f],BF_PART))return;
00172       else if(Coord == 3){
00173    if(OpenRisk(cFile[f],BF_NANO))return;
00174       }
00175     double RefZed = .5*pCm(CNorm);
00176     if(pNNano() > 0) RefZed = pNanoPos(0,CNorm);
00177     // if(Coord == 2 && VAR_IF_TYPE(SysType,VAR_MEMBRANE) )
00178     //   ShiftSys(SHIFT_CM);
00180     if(Coord < 3){
00181       //DensityProfile(Coord,NBin,NType,DensityAv);
00182       BfDefChain();
00183       int NPartition = NSample;
00184       double *Density = (double *)calloc(NPartition*NPartition*NType*NBin,sizeof(double));
00185       double Pos[4];
00186       char cLine[STRSIZE];
00187       for(int b=0;b<pNBlock();b++){
00188    if(!strncmp(Block[b].Name,"PEP",3) ) continue;
00189    if(!strcmp(Block[b].Name,"WATER") )  continue;
00190    for(int p=Block[b].InitIdx;p<Block[b].EndIdx;p++){
00191      int t = pType(p);
00192      for(int d=0;d<3;d++)
00193        Pos[d] = pPos(p,d);
00194      Pos[CNorm] = pPos(p,CNorm) - RefZed + .5*pEdge(CNorm);
00195      int vx = (int)(NPartition*Pos[CLat1]*pInvEdge(CLat2));
00196      if(vx < 0 || vx >= NBin) continue;
00197      int vy = (int)(NPartition*Pos[CLat2]*pInvEdge(CLat2));
00198      if(vy < 0 || vy >= NBin) continue;
00199      int vz = (int)(NBin*Pos[CNorm]*pInvEdge(CNorm));
00200      if(vz < 0 || vz >= NBin) continue;
00201      if(CHAIN_IF_TYPE(Ch[pChain(p)].Type,CHAIN_ADDED)) t = 2;
00202      if(!strncmp(Block[b].Name,"ADDED",5)) t = 2;
00203      if(!strncmp(Block[b].Name,"CHOL",4))  t = 2;
00204      Density[((vz*NType+t)*NPartition+vy)*NPartition+vx] += 1.;
00205    }
00206       }
00207       for(int vx=0;vx<NPartition;vx++){
00208    for(int vy=0;vy<NPartition;vy++){
00209      double Media = 0.;
00210      double Weight = 0.;
00211      for(int v=0;v<NBin;v++){
00212        Weight += Density[((v*NType+0)*NPartition+vy)*NPartition+vx];
00213        Media += v*Density[((v*NType+0)*NPartition+vy)*NPartition+vx];
00214      }
00215      int vMedia = Weight > 0 ? (int)(Media/Weight) : NBin/2;
00216      int vOffset = vMedia - NBin/2;
00217      for(int t=0;t<NType;t++){
00218        for(int v=0;v<NBin;v++){
00219          int vOther = v + vOffset;
00220          if(vOther < 0)
00221       vOther = NBin - vOther;
00222          if(vOther >= NBin)
00223       vOther = vOther - NBin;
00224          DensityAv[v*NType+t] += Density[((vOther*NType+t)*NPartition+vy)*NPartition+vx];
00225        }
00226      }
00227    }
00228       }
00229       free(Density);
00230     }
00231     else{
00232       Vettore NanoAxis(Nano[0].Axis[0],Nano[0].Axis[1],Nano[0].Axis[2]);
00233       Vettore PosRel(3);
00234       Vettore Dist(3);
00235       for(int b=0;b<pNBlock();b++){
00236    if(!strncmp(Block[b].Name,"PEP",3)) continue;
00237    for(int p=0;p<pNPart();p++){
00238      for(int d=0;d<3;d++){
00239        PosRel.Set(remainder(pNanoPos(0,d)-pPos(p,d),pEdge(d)),d);
00240      }
00241      double RadDist = ABS(Dist.PerpTo3(&PosRel,&NanoAxis));
00242      int v = (int)(RadDist/pEdge(3)*NBin);
00243      if(v < 0 || v >= NBin) continue;
00244      int t = pType(p);
00245      if(CHAIN_IF_TYPE(Ch[pChain(p)].Type,CHAIN_ADDED))
00246        t = 2;
00247      DensityAv[v*NType+t] += 1.;
00248    }
00249       }
00250     }
00251     ChDensMean += pNChain()/(pEdge(CLat1)*pEdge(CLat2));
00252     ChDensSqr  += SQR(pNChain()/(pEdge(CLat1)*pEdge(CLat2)));
00253   }
00254 #ifdef OMPI_MPI_H
00255   MPI_Allreduce(MPI_IN_PLACE,DensityAv,NBin, MPI_DOUBLE, MPI_SUM, Proc->CommGrid);
00256   int Rank=0;
00257   MPI_Comm_rank(Proc->CommGrid, &Rank);
00258   if(Rank==0){
00259 #endif
00260     printf("\n");
00261     double NormaF = (NFile[1] - NFile[0]);
00262     double VolumeSlab = (pEdge(0)*pEdge(1)*pEdge(2))/(double)NBin;
00263     for(int v=0;v<NBin;v++){
00264       for(int t=0;t<NType;t++){
00265    if(Coord < 3) 
00266      DensityAv[v*NType+t] /= VolumeSlab*NormaF;
00267    else 
00268      DensityAv[v*NType+t] /= NormaF*VolEl[v];
00269       }
00270     }
00271     char FileName[256];
00272     sprintf(FileName,"DensityChi%.0fKappa%.0fRho%.0f.dat",pchiN(),pkappaN(),prho());
00273     FILE *FileToWrite = fopen(FileName,"w");
00274     char cSystem[STRSIZE];
00275     SysDef(cSystem);
00276     double ChDensErr = sqrt(ChDensSqr - SQR(ChDensMean)/NormaF )/NormaF;
00277     fprintf(FileToWrite,"# %s\n",cSystem);
00278     fprintf(FileToWrite,"# ChainDens %lf %lf\n",ChDensMean/NormaF,ChDensErr);
00279     for(int v=0;v<NBin;v++){
00280       fprintf(FileToWrite,"%lf %lf %lf %lf\n",(double)v/(double)NBin*pEdge(Coord),DensityAv[v*NType],DensityAv[v*NType+1],DensityAv[v*NType+2]);
00281     }
00282     fclose(FileToWrite);
00283 #ifdef OMPI_MPI_H
00284   }
00285 #endif
00286   free(DensityAv);
00287   return;
00288 }
00292 void ElPoly::SlabProf(int NBin,int nNano,int Coord1){
00293   if(Coord1 != CLat1 && Coord1 != CLat2){
00294     printf("%d is not a lateral coordinate\n",Coord1,CLat1,CLat2);
00295   }
00296   //SigErr(pNNano() < 2,"The number of nano should be at least two");
00297   int Coord2 = Coord1 == CLat1 ? CLat2 : CLat1;
00298   double *Dens = (double *) calloc(NBin,sizeof(double));
00299   double *Temp = (double *) calloc(NBin,sizeof(double));
00300   double *Prof = (double *) calloc(6*NBin,sizeof(double));
00301   double **Plot = (double **)calloc(3,sizeof(double));
00302   for(int t=0;t<3;t++){
00303     Plot[t] = (double *)calloc(NBin*NBin,sizeof(double));
00304   }
00305   double *Count = (double *) calloc(6*NBin,sizeof(double));
00306   Vettore Pos(3);
00307   Vettore PosRel(3);
00308   //FILE *Ciccia = fopen("Ciccia.dat","w");
00309   for(int f=NFile[0];f<NFile[1];f++){
00310     Processing(f);
00311     //--------------open,-back-fold----------------------
00312     if(OpenRisk(cFile[f],BF_NO)) return;
00313     if(pNNano() == 1){
00314       SetNNano(2);
00315       Nano[1].Pos[0] = 0.;
00316       Nano[1].Pos[1] = 0.;
00317       Nano[1].Pos[2] = .5*pEdge(CNorm);
00318     }
00319     Vettore Axis(3);
00320     for(int d=0;d<3;d++){
00321       Axis.Set(Nano[1].Pos[d]-Nano[0].Pos[d],d);
00322     }
00323     Axis.Set(0.,CNorm);
00324     Axis.Normalize();
00325     double InvEdge = (pEdge(CLat1)*Axis.Val(0) + pEdge(CLat2)*Axis.Val(1))/(Axis.Val(0) + Axis.Val(1));
00326     InvEdge = 1./InvEdge;
00327     //Nano[nNano].Pos[CLat1] = 0.;
00328     //Nano[nNano].Pos[CLat2] = 0.;
00329     for(int b=0;b<pNBlock();b++){
00330       int TypePlus = 0;
00331       if(!strncmp(Block[b].Name,"PEP",3) ) TypePlus = 1;
00332       if(!strcmp(Block[b].Name,"WATER") )  continue;
00333       for(int p=Block[b].InitIdx;p<Block[b].EndIdx;p++){
00334    int t = 2*pType(p);
00335    for(int d=0;d<3;d++){
00336      double Dist = Pm[p].Pos[d] - Nano[nNano].Pos[d];
00337      Dist -= floor(Dist*pInvEdge(d)+.5)*pEdge(d);
00338      Pos.Set(Dist,d);
00339    }
00340    Pos.Set(0.,CNorm);
00341    double LatDist = PosRel.PerpTo(&Pos,&Axis);
00342    if(LatDist > Nano[nNano].Rad) continue;
00343    double Dist = Pos.ProjOnAxis(&Axis);
00344    Pos.Set(pPos(p,CNorm)-Nano[nNano].Pos[CNorm],CNorm);
00345    int t1 = TypePlus ? 2 : pType(p);
00346    //fprintf(Ciccia,"%lf %lf %lf %d\n",pPos(p,0),pPos(p,1),pPos(p,2),t1);
00347    //fprintf(Ciccia,"%lf %lf %lf %d\n",Pos.Val(0)+Nano[nNano].Pos[0]-5.,Pos.Val(1)+Nano[nNano].Pos[1]+5.,Pos.Val(2)+Nano[nNano].Pos[2],t1);
00348    if(pPos(p,CNorm) > pNanoPos(nNano,CNorm)) t += 1;
00349    int v = (int)(Dist*InvEdge*NBin);
00350    if(v < 0 || v >= NBin) continue;
00351    int z = (int)(pPos(p,CNorm)*pInvEdge(CNorm)*NBin);
00352    if(z < 0 || z >= NBin) continue;
00353    Prof[v*6+t] += pPos(p,CNorm) - pCm(CNorm);
00354    Count[v*6+t] += 1.;
00355    Plot[t1][v*NBin+z] += 1.;
00356    if(!TypePlus) Dens[v] += 1.;
00357       }
00358     }
00359   }
00360   //fclose(Ciccia);
00361   printf("\n");
00362   //normalize
00363   for(int b=0;b<NBin;b++){
00364     for(int t=0;t<6;t++){
00365       Prof[b*6+t] /= Count[b*6+t] > 0. ? Count[b*6+t] : 1.;
00366     }
00367   }
00368   double Norm = 1./(NFile[1]-NFile[0]);
00369   for(int v=0;v<SQR(NBin);v++){
00370     for(int t=0;t<3;t++){
00371       Plot[t][v] *= Norm;
00372     }
00373   }
00374   Matrice Mask1(5);
00375   Mask1.FillGaussian(.5,3.);
00376   Mask1.Print();
00377   int NDim = 1;
00378   int IfMinImConv = 1;
00379   Mask1.ConvoluteMatrix(Dens,NBin,NDim,IfMinImConv);
00380   //write profile
00381   FILE *FSlab = fopen("SlabProfile.dat","w");
00382   FILE *FDens = fopen("SlabDensProfile.dat","w");
00383   double InvNFile = 1./(double)(NFile[1]-NFile[0]);
00384   for(int b=0;b<NBin;b++){
00385     double x = b/(double)NBin*pEdge(Coord1);
00386     //fprintf(FSlab,"%lf %lf %lf %lf %lf %lf %lf\n",x,Prof[b*6  ],Prof[b*6+1],Prof[b*6+2],Prof[b*6+3],Prof[b*6+4],Prof[b*6+5],Prof[b*6+6]);
00387     fprintf(FSlab,"%lf %lf %lf %lf %lf \n",x,Prof[b*6  ],Prof[b*6+1],Prof[b*6+2],Prof[b*6+3],Prof[b*6+4]);
00388     fprintf(FDens,"%lf %lf\n",x,Dens[b]*InvNFile);
00389   }
00390   fclose(FSlab);
00391   fclose(FDens);
00392   //smooth and write dens
00393   Matrice Mask(5,5);
00394   Mask.FillGaussian(.5,3.);
00395   Mask.Print();
00396   NDim = 2;
00397   IfMinImConv = 1;
00398   for(int t=0;t<3;t++){
00399     Mask.ConvoluteMatrix(Plot[t],NBin,NDim,IfMinImConv);
00400     Mask.ConvoluteMatrix(Plot[t],NBin,NDim,IfMinImConv);
00401   }
00402   FILE *FSlabProf = fopen("SlabDens.xzd","w");
00403   double LatDim[3] = {pEdge(Coord1),pEdge(CNorm),30.};
00404   PrintDens(FSlabProf,Plot,LatDim,NBin);
00405   fclose(FSlabProf);
00406   free(Prof);
00407   free(Count);
00408   free(Temp);
00409   free(Dens);
00410 }
00414 void ElPoly::PrintDens(FILE *FileToWrite,double **Plot,double *LatDim,int NBin){
00415   int link[4] = {0,0,0,0};
00416   double InvNBin = 1./(double)NBin;
00417   fprintf(FileToWrite,"#l(%lf %lf %lf) v[%d] d[%s]\n",LatDim[0],LatDim[1],LatDim[2],NBin,ChooseDraw(EL_DENS));
00418   double Dr = InvNBin*LatDim[0];
00419   double Dz = InvNBin*LatDim[1];
00420   for(int t=0,p=0,c=0;t<3;t++){
00421     for(int vv=0;vv<NBin-1;vv++){
00422       for(int v=0;v<NBin-1;v++){
00423    link[0] = (v+0)*NBin + (vv+0);
00424    if(Plot[t][link[0]] < .02) continue;
00425    //fprintf(FileToWrite,"{t[%d %d %d] x(%lf %lf %lf)}\n",p,c,t, v*InvNBin*pEdge(3),(vv)*InvNBin*(Border[1]-Border[0]),(Plot[(v*NBin+vv)*NType+t]));
00426    //continue;
00427    //------------defines-the-squares---------------------
00428    link[1] = (v+0)*NBin + (vv+1);
00429    link[2] = (v+1)*NBin + (vv+0);
00430    link[3] = (v+1)*NBin + (vv+1);
00431    int pRef = p;
00432    for(int lx=0;lx<2;lx++){
00433      for(int ly=0;ly<2;ly++){
00434        int l = 2*lx+ly;
00435        double NanoAdded = Plot[2][(link[l])];//+Plot[3][(link[l])];
00436        double Phob = t == 0 ? Plot[0][(link[l])] : 0.;
00437        double Phil = t == 1 ? Plot[1][(link[l])] : 0.;
00438        double r = (v +lx)*Dr + .5*Dr;
00439        double z = (vv+ly)*Dz + .5*Dz;
00440        double dens = Plot[t][(link[l])];//+Plot[(v*NBin+vv)*NType+1]+Plot[(v*NBin+vv)*NType+2]);
00441        int l1 = pRef + (p+1)%4;
00442        int l2 = pRef + (p+2)%4;
00443        int l3 = pRef + (p+3)%4;
00444        fprintf(FileToWrite,"{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);
00445        p++;
00446      }
00447    }
00448    c++;
00449       }
00450     }
00451   }
00452 }
00456 void ElPoly::CartDens(int NBin,int nNano){
00457   int NType = 5;
00458   double **Plot = (double **)calloc(NType,sizeof(double));
00459   for(int t=0;t<NType;t++){
00460     Plot[t] = (double *)calloc(NBin*NBin,sizeof(double));
00461   }
00462   double NBinInv = 1./(double)NBin;
00463   double InvNFile = 1./(double)(NFile[1]-NFile[0]);
00464   double LatDim[3] = {pEdge(CLat1),pEdge(CNorm),30.};
00465   char FName[20];
00466   double Width = 2.;
00467   for(int f=NFile[0];f<NFile[1];f++){
00468     Processing(f);
00469     //--------------open,-back-fold----------------------
00470     if(OpenRisk(cFile[f],NBackFold)) return;
00471     for(int p=0;p<pNPart();p++){
00472       double Dx = fabs(pCm(CLat1) - pPos(p,CLat1));
00473       double Dy = fabs(pCm(CLat2) - pPos(p,CLat2));
00474       if(Dy > Width) continue;
00475       int vx = (int)(2.*Dx*pInvEdge(CLat1)*NBin);
00476       if(vx<0 || vx>=NBin) continue;
00477       int vz = (int)(pPos(p,CNorm)*pInvEdge(CNorm)*NBin);
00478       if(vz<0 || vz>=NBin) continue;
00479       int t = pType(p);
00480       //if((p%Block[0].Asym)!=0)continue;
00481       // if(VAR_IF_TYPE(Ch[Pm[p].CId].Type,CHAIN_INNER)) t = 0;
00482       // else if(VAR_IF_TYPE(Ch[Pm[p].CId].Type,CHAIN_OUTER)) t = 1;
00483       // else continue;
00484       Plot[t][vx*NBin+vz] += 1.;
00485     }
00486   }
00487   Matrice Mask(5,5);
00488   Mask.FillGaussian(.5,3.);
00489   Mask.Print();
00490   for(int v=0;v<NBin*NBin;v++) for(int t=0;t<NType;t++) Plot[t][v] *= InvNFile;
00491   int NDim = 2;
00492   int IfMinImConv = 1;
00493   for(int t=0;t<NType;t++){
00494     Mask.ConvoluteMatrix(Plot[t],NBin,NDim,IfMinImConv);
00495     Mask.ConvoluteMatrix(Plot[t],NBin,NDim,IfMinImConv);
00496   }
00497   sprintf(FName,"CartDens.dat");
00498   FILE *FileToWrite = fopen(FName,"w");
00499   PrintDens(FileToWrite,Plot,LatDim,NBin);
00500   int NAvx = 4;
00501   int NAvz = 4;
00502   double NAv = 1./(double)(NAvx*NAvz);
00503   double Avx = 0.;
00504   double Avz = 0.;
00505   double Av = 0.;
00506   for(int vx=0;vx<NBin*NBin;vx++) Plot[3][vx] = 1.;
00507   for(int t=0;t<2;t++){
00508     int p = 0;
00509     sprintf(FName,"LineLayer%d.dat",t);
00510     FILE *FLine = fopen(FName,"w");
00511     if(1==1){
00512       fprintf(FLine,"#l(%lf %lf %lf) v[%d] d[part]\n",pEdge(0),pEdge(1),pEdge(2),NBin);
00513       for(int vx=0;vx<NBin;vx++){
00514    for(int vz=0;vz<NBin;vz++){
00515      fprintf(FLine,"{t[%d %d %d] x(%lf %lf %lf)}\n",p++,0,t,(double)(vx*pEdge(CLat1)/(double)NBin),(double)(vz*pEdge(CNorm)/(double)NBin),Plot[t][vx*NBin+vz],t);
00516    }
00517       }
00518     }
00519     for(int vx=0;vx<NBin;vx+=NAvx){
00520       for(int vz=0;vz<NBin;vz+=NAvz){
00521    for(int vvx=0;vvx<NAvx;vvx++){
00522      for(int vvz=0;vvz<NAvz;vvz++){
00523        int vTot = (vx+vvx)*NBin + vz+vvz;
00524        if(vTot >= SQR(NBin)) break;
00525        Plot[3][vTot] *= Plot[t][vTot];
00526        double Weight = Plot[t][vTot];
00527        if(Weight < 1.0) continue;
00528        Avx += (double)(vx*pEdge(CLat1)/(double)NBin)*Weight;
00529        Avz += (double)(vz*pEdge(CNorm)/(double)NBin)*Weight;
00530        Av  += Weight;
00531      }
00532    }
00533    //fprintf(FLine,"%lf %lf %lf %d\n",Avx/Av,Avz/Av,Av/(double)NAverage,2);
00534    fprintf(FLine,"{t[%d %d %d] x(%lf %lf %lf) l[%d]}\n",p++,0,2,Avx/Av,Avz/Av,Av*NAv,p+1);
00535    Avx = 0.;
00536    Avz = 0.;
00537    Av  = 0.;
00538       }
00539     }
00540     fclose(FLine);
00541   }
00542   sprintf(FName,"LineLayer2.dat");
00543   FILE *FLine = fopen(FName,"w");
00544   for(int vx=0;vx<NBin;vx++){
00545     for(int vz=0;vz<NBin;vz++){
00546       double Weight = Plot[3][vx*NBin+vz];
00547       if(Weight < 1.1) continue;
00548       fprintf(FLine,"%lf %lf %lf\n",(double)(vx*pEdge(CLat1)/(double)NBin),(double)(vz*pEdge(CNorm)/(double)NBin),Weight);      
00549     }
00550   }
00551   for(int t=0;t<NType;t++) free(Plot[t]);
00552   free(Plot);
00553 }
00557 void ElPoly::RadDistrF(int NBin,int How,int nNano){
00558   if(nNano > pNNano()){
00559     printf("The specified nanoparticle doesn't exist\n");
00560     return ;
00561   }
00562   int NType = 5;
00563   double *Dens = (double *)calloc(NBin,sizeof(double));
00564   double **Plot = (double **)calloc(NType,sizeof(double));
00565   for(int t=0;t<NType;t++){
00566     Plot[t] = (double *)calloc(NBin*NBin,sizeof(double));
00567   }
00568   //--------The-first-file-defines-the-reference-distance--------
00569   SetEdge(.5*MIN(pEdge(CLat1),pEdge(CLat2)),3);
00570   double RefPos[3] = {pCm(CLat1),pCm(CLat2),pCm(CNorm)};
00571   double LatDim[3] = {pEdge(3),pEdge(CNorm),30.};
00572   if(How >= 6){
00573     LatDim[0] = pEdge(CLat1);
00574     LatDim[1] = pEdge(CLat2);
00575   }
00576   double InvNBin  = 1./(double)NBin;
00577   double InvNFile = 1./(double)(NFile[1]-NFile[0]);
00578   double OldPos[3] = {pNanoPos(nNano,0),pNanoPos(nNano,1),pNanoPos(nNano,2)};
00579   for(int f=NFile[0];f<NFile[1];f++){
00580     Processing(f);
00581     //--------------open,-back-fold----------------------
00582     if(OpenRisk(cFile[f],NBackFold)) return;
00583     BfDefChain();
00584     if(How == 5) PorePos();
00585     RefPos[CLat1] = pNanoPos(nNano,CLat1);
00586     RefPos[CLat2] = pNanoPos(nNano,CLat2);
00587     RefPos[CNorm] = pNanoPos(nNano,CNorm);
00588     if(How == 2){
00589       RefPos[CLat1] = pCm(CLat1);
00590       RefPos[CLat2] = pCm(CLat2);
00591       RefPos[CNorm] = pCm(CNorm);
00592     }
00593     if(How == 0){
00594       for(int d=0;d<3;d++){
00595    RefPos[d] = pCm(d);
00596       }
00597     }
00598     if(How < 6){
00599       //SetEdge(.5*MIN(pEdge(CLat1),pEdge(CLat2)),3);
00600       //----------define-the-distance,-fill-Plot---------------
00601       double TangInv = ABS(Nano[nNano].Axis[CNorm]) > 0. ? sqrt(QUAD(Nano[nNano].Axis[CLat1])+QUAD(Nano[nNano].Axis[CLat2]))/Nano[nNano].Axis[CNorm] : 0.;
00602       if(How == 0) TangInv = 0.;
00603       Vettore Rotated(3);
00604       Vettore Dist(3);
00605       Vettore PosRel(3);
00606       Vettore Zed(3);
00607       Zed.Set(0.,CLat1);Zed.Set(0.,CLat2);Zed.Set(1.,CNorm);
00608       //Vettore NanoAxis(1.,0.,0.);
00609       Vettore NanoAxis(Nano[nNano].Axis[0],Nano[nNano].Axis[1],Nano[nNano].Axis[2]);
00610       if(How == 0){
00611    NanoAxis.Set(0.,CLat1);
00612    NanoAxis.Set(0.,CLat2);
00613    NanoAxis.Set(1.,CNorm);
00614       }
00615       Vettore RotAxis(3);
00616       RotAxis.VetV(&NanoAxis,&Zed);
00617       double Angle = Zed.Angle(&NanoAxis,&Zed);
00618       Matrice M(RotAxis.x,Angle,3);
00619       int vzNano = (int)(pNanoPos(nNano,CNorm)*InvNBin*pInvEdge(CNorm));
00620       for(int b=0;b<pNBlock();b++){
00621    int IfAdded = 0;
00622    if(!strncmp(Block[b].Name,"PEP",3) ) IfAdded = 2;
00623    if(!strcmp(Block[b].Name,"STUFFING") ) continue;
00624    if(!strcmp(Block[b].Name,"WATER") ) continue;
00625    if(!strcmp(Block[b].Name,"CHOL") ) IfAdded = 1;
00626    if(!strcmp(Block[b].Name,"OIL") )  continue;//IfAdded = 1;
00627    if(!strcmp(Block[b].Name,"ADDED") )  continue;//IfAdded = 1;
00628    for(int p=Block[b].InitIdx,link=0;p<Block[b].EndIdx;p++){
00629      // double RadCh = SQR(Ch[Pm[pPos(.CId,0)-Nano[nNano].PosBf[0])+SQR(Ch[Pm[pPos(.CId,1)-Nano[nNano].PosBf[1]);
00630      // if( RadCh < SQR(Nano[nNano].Rad)){continue;}
00631      //finding the position on the axis of the nanoparticle
00632 
00633      int c = pChain(p);
00634      int p1 = c*pNPCh(b);
00635      //if(fabs(Pm[p1].Pos[CNorm] - Nano[0].Pos[CNorm]) < 2.5) continue;
00636      if(fabs(Pm[p1].Pos[CNorm] - Nano[0].Pos[CNorm]) < 3.5) continue;
00637 
00638      // if(!strcmp(Block[b].Name,"TT0"))
00639      //   if(VAR_IF_TYPE(Ch[c].Type,CHAIN_INNER)) continue;
00640      // if(!strcmp(Block[b].Name,"TT1"))
00641      //   if(VAR_IF_TYPE(Ch[c].Type,CHAIN_OUTER)) continue;
00642      // if(VAR_IF_TYPE(Ch[c].Type,CHAIN_OUTER)) continue;
00643    
00644 
00645      for(int d=0;d<3;d++){
00646        double Dist = pPos(p,d) - RefPos[d];
00647        Dist -= floor( Dist*pInvEdge(d) + .5)*pEdge(d);
00648        PosRel.Set(Dist,d);
00649      }
00650      double RadDist = fabs(Dist.PerpTo3(&PosRel,&NanoAxis));
00651      // double RadDist = hypot(PosRel.Val(CLat1),PosRel.Val(CLat2));
00652      //correspondent bin on the radial distance
00653      int vr = (int)(NBin*RadDist*pInvEdge(3));
00654      //Projecting the position on the axis and rotating to the normal
00655      PosRel.ProjOnAxis(&NanoAxis);
00656      M.Mult(PosRel,Rotated);
00657      //double PosNorm = Rotated[CNorm] + .5*pEdge(CNorm) - floor(PosNorm*pInvEdge(CNorm))*pEdge(CNorm);
00658      double PosNorm = PosRel.Val(CNorm) + .5*pEdge(CNorm);
00659      int vz = (int)(NBin*(PosNorm)*pInvEdge(CNorm));
00660      if( vz < 0 || vz >= NBin){
00661        printf("Axis %lf %lf %lf \n",Nano[nNano].Axis[0],Nano[nNano].Axis[1],Nano[nNano].Axis[2]);
00662        printf("Normal %lf > %lf %d > %d\n",PosNorm,pEdge(CNorm),vz,NBin);
00663        continue;
00664      }
00665      if( vr < 0 || vr >= NBin){
00666        continue;
00667      }
00668      //shifting wrt the inclination of the peptide
00669      // v += (int)( (vv-NBin/2)*TangInv);
00670      // if( v < 0) v += NBin;
00671      // if( v >= NBin) v -= NBin;
00672      int t = pType(p);
00673      if(IfAdded==1){
00674        t = 2;
00675        Plot[0][vr*NBin+vz] += 1.;
00676      }
00677      if(IfAdded==2) t = 3;
00678      else Dens[vr] += 1.;
00679      Plot[t][vr*NBin+vz] += 1.;
00680    }
00681       }
00682     }
00683     else if(How >= 6){
00684       for(int b=0;b<pNBlock();b++){
00685    int IfAdded = 0;
00686    if(!strncmp(Block[b].Name,"PEP",3) ) continue;//IfAdded = 1;
00687    //if(!strncmp(Block[b].Name,"ADDED",3) ) IfAdded = 1;
00688    if(!strcmp(Block[b].Name,"STUFFING") ) continue;
00689    if(!strcmp(Block[b].Name,"WATER") ) continue;
00690    if(!strcmp(Block[b].Name,"CHOL") ) IfAdded = 1;
00691    if(!strcmp(Block[b].Name,"OIL") )  IfAdded = 1;
00692    if(!strcmp(Block[b].Name,"ADDED") )  IfAdded = 1;
00693    for(int p=Block[b].InitIdx,link=0;p<Block[b].EndIdx;p++){
00694      double Dist = pPos(p,CLat1) - RefPos[CLat1];
00695      Dist -= floor(Dist*pInvEdge(CLat1)+.5)*pEdge(CLat1) - .5*pEdge(CLat1);
00696      int vx  = (int)(NBin*Dist*pInvEdge(CLat1));
00697      Dist = pPos(p,CLat2) - RefPos[CLat2];
00698      Dist -= floor(Dist*pInvEdge(CLat2)+.5)*pEdge(CLat2) - .5*pEdge(CLat2);
00699      int vy = (int)(NBin*Dist*pInvEdge(CLat2));
00700      int t = pType(p);
00701      if( IfAdded ) Plot[2][vx*NBin+vy] += 1.;
00702      else Plot[t][(vx*NBin+vy)] += 1.;
00703    }
00704       }
00705     }
00706   }
00707 #ifdef OMPI_MPI_H
00708   MPI_Allreduce(MPI_IN_PLACE,Plot, NType*NBin*NBin, MPI_DOUBLE, MPI_SUM, Proc->CommGrid);
00709   int Rank=0;
00710   MPI_Comm_rank(Proc->CommGrid, &Rank);
00711   if(Rank==0){
00712 #endif
00713     printf("\n");
00714     //------------Normalize----------------------
00715     if(How < 6){
00716       double *VolContr = (double *)calloc(NBin,sizeof(double));
00717       VolumeCircSlab(VolContr,NBin);
00718       for(int v=0;v<NBin;v++){
00719    Dens[v] /= VolContr[v]*(NFile[1] - NFile[0]);
00720    for(int vv=0;vv<NBin;vv++){
00721      for(int t=0,n=0;t<NType;t++){
00722        Plot[t][(v*NBin+vv)] /= VolContr[v]*(NFile[1]-NFile[0]);
00723      }
00724    }
00725       }
00726       free(VolContr);
00727     }
00728     if(How >= 6){
00729       for(int v=0;v<NBin;v++){
00730    Dens[v] /= (NFile[1] - NFile[0]);
00731    for(int vv=0;vv<NBin;vv++){
00732      for(int t=0,n=0;t<NType;t++){
00733        Plot[t][(v*NBin+vv)] *= InvNFile;
00734      }
00735    }
00736       }
00737     }
00738     Matrice Mask(5,5);
00739     Mask.FillGaussian(.5,3.);
00740     Mask.Print();
00741     int NDim = 2;
00742     int IfMinImConv = 0;
00743     if(How == 6) IfMinImConv = 1;
00744     for(int t=0;t<NType;t++){
00745       Mask.ConvoluteMatrix(Plot[t],NBin,NDim,IfMinImConv);
00746       Mask.ConvoluteMatrix(Plot[t],NBin,NDim,IfMinImConv);
00747     }
00748     //--------------------writes-the-files---------------------
00749     char FileName[256];
00750     char FileName2[256];
00751     if(How == 0){//Cm Reference
00752       sprintf(FileName,"RadDistrCmR%.1fH%.1fh%.1f",Nano[nNano].Rad,Nano[nNano].Hamaker,Nano[nNano].Height);
00753     }
00754     else if(How==1 || How == 4 || How == 5){//Nano reference
00755       sprintf(FileName,"RadDistrNanoR%.1fH%.1fh%.1f",Nano[nNano].Rad,Nano[nNano].Hamaker,Nano[nNano].Height);
00756     }      
00757     else if(How == 2){//Cm Nano Reference
00758       sprintf(FileName,"RadDistrCmNR%.1fH%.1fh%.1f",Nano[nNano].Rad,Nano[nNano].Hamaker,Nano[nNano].Height);
00759     }
00760     else if(How == 6){//Cm Nano Reference
00761       sprintf(FileName,"CartDistrNanoR%.1fH%.1fh%.1f",Nano[nNano].Rad,Nano[nNano].Hamaker,Nano[nNano].Height);
00762     }
00763     sprintf(FileName2,"%s.rzd",FileName);
00764     FILE *FileToWrite = fopen(FileName2,"w");
00765     PrintDens(FileToWrite,Plot,LatDim,NBin);
00766     fclose(FileToWrite);
00767     sprintf(FileName,"DensProfR%.1fH%.1fh%.1f.dat",Nano[nNano].Rad,Nano[nNano].Hamaker,Nano[nNano].Height);
00768     FILE *FToWrite = fopen(FileName,"w");
00769     for(int v=0;v<NBin;v++){
00770       double r = v*InvNBin*pEdge(3);
00771       fprintf(FToWrite,"%lf %lf\n",r,Dens[v]);
00772     }
00773 #ifdef OMPI_MPI_H
00774   }
00775 #endif
00776   for(int t=0;t<NType;t++)
00777     free(Plot[t]);
00778   free(Plot);
00779 }
00783 void ElPoly::BondDistr(int NSample){
00784   int NType = 3;
00785   double **Plot = (double **)calloc(NType,sizeof(double));
00786   double **Count = (double **)calloc(NType,sizeof(double));
00787   for(int t=0;t<3;t++){
00788     Plot[t] = (double *)calloc(SQR(NSample),sizeof(double));
00789     Count[t] = (double *)calloc(SQR(NSample),sizeof(double));
00790   }
00791   double InvNSample = 1./(double)NSample;
00792   double Cm[4] = {0.,0.,0.,0.};
00793   double Dist[4] = {0.,0.,0.,0.};
00794   double Bond[4] = {0.,0.,0.,0.};
00795   SetEdge(.5*MIN(pEdge(CLat1),pEdge(CLat2)),3);
00796   for(int f=NFile[0];f<NFile[1];f++){
00797     Processing(f);
00798     OpenRisk(cFile[f],BF_CHAIN);
00799     for(int b=0;b<pNBlock();b++){
00800       int IfAdded = 0;
00801       if(!strncmp(Block[b].Name,"PEP",3) ) continue;// IfAdded = 1;
00802       for(int p=Block[b].InitIdx,link=0;p<Block[b].EndIdx-1;p++){
00803    if(pChain(p) != pChain(p+1)) continue;
00804    Dist[3] = 0.;
00805    Bond[3] = 0.;
00806    for(int d=0;d<3;d++){
00807      Cm[d]   = .5*(pPos(p,d) + pPos(p+1,d));
00808      Bond[d] = Pm[p].Pos[d] - Pm[p+1].Pos[d];
00809      Bond[3] += SQR(Bond[d]);
00810      Dist[d] = Cm[d] - Nano[0].Pos[d];
00811      Dist[d] -= floor( Dist[d]*pInvEdge(d) + .5)*pEdge(d);
00812    }
00813    double DistR = sqrt(SQR(Dist[CLat1])+SQR(Dist[CLat2]));
00814    int t = pType(p);
00815    if(t != pType(p+1)) continue;
00816    if(IfAdded) t = 2;
00817    double z = .5*pEdge(CNorm) + Dist[CNorm];
00818    double r = DistR;
00819    int vz = (int)(z*pInvEdge(CNorm)*NSample);
00820    if(vz < 0 || vz >= NSample) continue;
00821    int vr = (int)(r*pInvEdge(3)*NSample);
00822    if(vr < 0 || vr >= NSample) continue;
00823    Plot[t][vr*NSample+vz] += sqrt(Bond[3]);
00824    Count[t][vr*NSample+vz] += 1.;
00825       }
00826     }
00827   }
00828   // for(int t=0;t<NType;t++){
00829   //   for(int v=0;v<SQR(NSample);v++){
00830   //     Plot[t][v] /= Count[t][v] > 0. ? 1./Count[t][v] : 1.;
00831   //   }
00832   // }
00833   double *VolContr = (double *)calloc(NSample,sizeof(double));
00834   VolumeCircSlab(VolContr,NSample);
00835   for(int v=0;v<NSample;v++){
00836     for(int vv=0;vv<NSample;vv++){
00837       for(int t=0,n=0;t<NType;t++){
00838    Plot[t][(v*NSample+vv)] /= VolContr[v]*(NFile[1]-NFile[0]);
00839    //double Norm = Count[t][v*NSample+vv] > 1. ? 1./Count[t][v*NSample+vv] : 1.;
00840    //Plot[t][(v*NSample+vv)] *= Norm;
00841       }
00842     }
00843   }
00844   free(VolContr);
00845   Matrice Mask(5,5);
00846   Mask.FillGaussian(.5,3.);
00847   Mask.Print();
00848   int NDim = 2;
00849   int IfMinImConv = 0;
00850   for(int t=0;t<NType;t++){
00851     Mask.ConvoluteMatrix(Plot[t],NSample,NDim,IfMinImConv);
00852     Mask.ConvoluteMatrix(Plot[t],NSample,NDim,IfMinImConv);
00853   }
00854   char FileName2[60];
00855   sprintf(FileName2,"BondDistr.rzd");
00856   FILE *FileToWrite = fopen(FileName2,"w");
00857   double LatDim[3] = {pEdge(3),pEdge(CNorm),15.};
00858   PrintDens(FileToWrite,Plot,LatDim,NSample);
00859   fclose(FileToWrite);
00860   for(int t=0;t<NType;t++){
00861     free(Plot[t]);
00862     free(Count[t]);
00863   }
00864   free(Plot);
00865   free(Count);
00866 }
00870 void ElPoly::SplayDistr(int NSample){
00871   int NType = 3;
00872   double **Plot = (double **)calloc(NType,sizeof(double));
00873   double **Count = (double **)calloc(NType,sizeof(double));
00874   for(int t=0;t<3;t++){
00875     Plot[t] = (double *)calloc(SQR(NSample),sizeof(double));
00876     Count[t] = (double *)calloc(SQR(NSample),sizeof(double));
00877   }
00878   double InvNSample = 1./(double)NSample;
00879   double Cm[4] = {0.,0.,0.,0.};
00880   double CmP[6] = {0.,0.,0.,0.,0.,0.};
00881   double E2EP[6] = {0.,0.,0.,0.,0.,0.};
00882   double GyrP[2] = {0.,0.};
00883   double DistBA[4] = {0.,0.,0.,0.};
00884   double DistCB[4] = {0.,0.,0.,0.};
00885   int IfMartini = 0;
00886   int HalfLim = (int)(Block[0].Asym*.5);
00887   SetEdge(.5*MIN(pEdge(CLat1),pEdge(CLat2)),3);
00888   double Direct[6] = {0.,0.,0.,0.,0.,0.};
00889   //int pLimit[4] = {4,8,8,13};//Martini
00890   //double TailLength[2] = {1./4.,1./5.};//Martini     
00891   int pLimit[4] = {0,4,4,9};//DFT
00892   double TailLength[2] = {1./4.,1./5.};//DFT
00893   Vettore Ax0(1.,0.,0.);
00894   Vettore Ax2(0.,0.,1.);
00895   for(int f=NFile[0];f<NFile[1];f++){
00896     Processing(f);
00897     OpenRisk(cFile[f],BF_CHAIN);
00898     for(int b=0;b<pNBlock();b++){
00899       double *xc = (double *)calloc(pNPCh(b),sizeof(double));
00900       double *yc = (double *)calloc(pNPCh(b),sizeof(double));
00901       double *zc = (double *)calloc(pNPCh(b),sizeof(double));
00902       if(!strncmp(Block[b].Name,"PEP",3) ) continue;
00903       if(!strncmp(Block[b].Name,"ADDED",3) ) continue;
00904       for(int c=0;c<Block[b].NChain;c++){
00905    for(int i=0;i<2;i++){
00906      GyrP[i] = 0.;
00907      int p1 = Block[b].InitIdx + c*Block[b].NPCh + pLimit[i*2];
00908      int p2 = Block[b].InitIdx + c*Block[b].NPCh + pLimit[i*2+1];
00909      int NBead = 0;
00910      for(int d=0;d<3;d++){
00911        CmP[i*3+d] = 0.;
00912        E2EP[i*3+d] = Pm[p1].Pos[d] - Pm[p2].Pos[d];
00913      }
00914      for(int p=p1;p<p2;p++){
00915        int ppc = p-p1;
00916        xc[ppc] = Pm[p].Pos[0];
00917        yc[ppc] = Pm[p].Pos[1];
00918        zc[ppc] = Pm[p].Pos[2];
00919        NBead += 1;
00920        for(int d=0;d<3;d++){
00921          CmP[i*3+d] += Pm[p].Pos[d];
00922        }
00923      }
00924      for(int d=0;d<3;d++){
00925        CmP[i*3+d] /= (double) NBead;
00926      }
00927      for(int p=p1;p<p2;p++){
00928        for(int d=0;d<3;d++){
00929          GyrP[i] += SQR(Pm[p].Pos[d]-CmP[i*3+d]);
00930        }
00931      }
00932      RETTA rzx = Mat->InterRett(zc,xc,NBead);
00933      RETTA rzy = Mat->InterRett(zc,yc,NBead);
00934      double x1 = Pm[p1].Pos[2]*rzx.m + rzx.q;
00935      double x2 = Pm[p2].Pos[2]*rzx.m + rzx.q;
00936      double y1 = Pm[p1].Pos[2]*rzy.m + rzy.q;
00937      double y2 = Pm[p2].Pos[2]*rzy.m + rzy.q;
00938      Direct[2*i+2] = Pm[p1].Pos[2] - Pm[p2].Pos[2];
00939      Direct[2*i+1] = y1 - y2;
00940      Direct[2*i+0] = x1 - x2;
00941    }
00942    Vettore Dir1(Direct[0],Direct[1],Direct[2]);
00943    Vettore Dir2(Direct[3],Direct[4],Direct[5]);
00944    double Angle = Ax0.Angle(&Dir1,&Dir2);
00945    if(isnan(Angle)) continue;
00946    //int pA = Block[b].InitIdx + c*Block[b].NPCh + 3;//Martini
00947    int pA = Block[b].InitIdx + c*Block[b].NPCh + 3;//DFT
00948    for(int d=0;d<3;d++){
00949      Cm[d] = Nano[0].Pos[d] - Pm[pA].Pos[d];
00950      CmP[0+d] = CmP[0+d] - Nano[0].Pos[d];
00951      CmP[0+d] -= floor( CmP[0+d]*pInvEdge(d) + .5)*pEdge(d);
00952      CmP[3+d] = CmP[3+d] - Nano[0].Pos[d];
00953      CmP[3+d] -= floor( CmP[3+d]*pInvEdge(d) + .5)*pEdge(d);
00954    }
00955    double E2E1 = sqrt(SQR(E2EP[0+0])+SQR(E2EP[0+1])+SQR(E2EP[0+2]));
00956    double E2E2 = sqrt(SQR(E2EP[3+0])+SQR(E2EP[3+1])+SQR(E2EP[3+2]));
00957    double DistR = sqrt(SQR(Cm[CLat1])+SQR(Cm[CLat2]));
00958    double z = .5*pEdge(CNorm) + Cm[CNorm];
00959    double r = DistR;
00960    int vz = (int)(z*pInvEdge(CNorm)*NSample);
00961    if(vz < 0 || vz >= NSample) continue;
00962    int vr = (int)(r*pInvEdge(3)*NSample);
00963    if(vr < 0 || vr >= NSample) continue;
00964    Plot[0][vr*NSample+vz] += Angle;
00965    Count[0][vr*NSample+vz] += 1.;
00966    r = sqrt(SQR(CmP[0+CLat1])+SQR(CmP[0+CLat2]));
00967    z = .5*pEdge(CNorm) + CmP[0+CNorm];
00968    vz = (int)(z*pInvEdge(CNorm)*NSample);
00969    if(vz < 0 || vz >= NSample) continue;
00970    vr = (int)(r*pInvEdge(3)*NSample);
00971    if(vr < 0 || vr >= NSample) continue;
00972    // Plot[1][vr*NSample+vz] += Dir1.Norm()*TailLength[0];
00973    Plot[1][vr*NSample+vz] += E2E1*TailLength[0];
00974    Count[1][vr*NSample+vz] += 1.;
00975    Plot[2][vr*NSample+vz] += sqrt(GyrP[0]*TailLength[0]);
00976    Count[2][vr*NSample+vz] += 1.;
00977    r = sqrt(SQR(CmP[3+CLat1])+SQR(CmP[3+CLat2]));
00978    z = .5*pEdge(CNorm) + CmP[3+CNorm];
00979    vz = (int)(z*pInvEdge(CNorm)*NSample);
00980    if(vz < 0 || vz >= NSample) continue;
00981    vr = (int)(r*pInvEdge(3)*NSample);
00982    if(vr < 0 || vr >= NSample) continue;
00983    //Plot[1][vr*NSample+vz] += Dir2.Norm()*TailLength[1];
00984    Plot[1][vr*NSample+vz] += E2E2*TailLength[1];
00985    Count[1][vr*NSample+vz] += 1.;
00986    Plot[2][vr*NSample+vz] += sqrt(GyrP[1]*TailLength[1]);
00987    Count[2][vr*NSample+vz] += 1.;
00988       }
00989       free(xc);
00990       free(yc);
00991       free(zc);
00992     }
00993   }
00994   double *VolContr = (double *)calloc(NSample,sizeof(double));
00995   VolumeCircSlab(VolContr,NSample);
00996   for(int v=0;v<NSample;v++){
00997     for(int vv=0;vv<NSample;vv++){
00998       for(int t=0,n=0;t<NType;t++){
00999    Plot[t][(v*NSample+vv)] /= VolContr[v]*(NFile[1]-NFile[0]);
01000    //double Norm = Count[t][v*NSample+vv] > 1. ? 1./Count[t][v*NSample+vv] : 1.;
01001    //Plot[t][(v*NSample+vv)] *= Norm;
01002       }
01003     }
01004   }
01005   free(VolContr);
01006   Matrice Mask(5,5);
01007   Mask.FillGaussian(.5,3.);
01008   Mask.Print();
01009   int NDim = 2;
01010   int IfMinImConv = 0;
01011   for(int t=0;t<NType;t++){
01012     Mask.ConvoluteMatrix(Plot[t],NSample,NDim,IfMinImConv);
01013     Mask.ConvoluteMatrix(Plot[t],NSample,NDim,IfMinImConv);
01014   }
01015   char FileName2[60];
01016   sprintf(FileName2,"SplayDistr.rzd");
01017   FILE *FileToWrite = fopen(FileName2,"w");
01018   double LatDim[3] = {pEdge(3),pEdge(CNorm),15.};
01019   PrintDens(FileToWrite,Plot,LatDim,NSample);
01020   fclose(FileToWrite);
01021   for(int t=0;t<NType;t++){
01022     free(Plot[t]);
01023     free(Count[t]);
01024   }
01025   free(Plot);
01026   free(Count);
01027 }
01031 void ElPoly::RadDens2Thick(int NBin){
01032   double *ThickProf = (double *)calloc(3*NBin,sizeof(double));
01033   double *Count = (double *)calloc(3*NBin,sizeof(double));
01034   double **Plot = (double **)calloc(3,sizeof(double));
01035   for(int t=0;t<3;t++){
01036     Plot[t] = (double *)calloc(NBin*NBin,sizeof(double));
01037   }
01038   LoadDensFile(Plot,NBin);
01039   double Cm[2] = {0.,0.};
01040   double CmCount[2] = {0.,0.};
01041   double InvNBin = 1./(double)NBin;
01042   for(int sr=0;sr<NBin;sr++){
01043     for(int sz=0;sz<NBin/2;sz++){
01044       double Pos = (sz-NBin/2)*pEdge(1)*InvNBin;
01045       double Weight = Plot[0][sr*NBin+sz]*Plot[1][sr*NBin+sz];
01046       ThickProf[sr*3  ] += Pos*Weight;
01047       Count[sr*3  ] += Weight;
01048     }
01049     for(int sz=NBin/2;sz<NBin;sz++){
01050       double Pos = (sz-NBin/2)*pEdge(1)*InvNBin;
01051       double Weight = Plot[0][sr*NBin+sz]*Plot[1][sr*NBin+sz];
01052       ThickProf[sr*3+1] += Pos*Weight;
01053       Count[sr*3+1] += Weight;
01054     }
01055     for(int sz=0;sz<NBin;sz++){
01056       ThickProf[sr*3+2] += Plot[2][sr*NBin+sz];
01057       Count[sr*3+2] += 1.;
01058     }
01059   }
01060   //normalize
01061   double Max = 0.;
01062   for(int sr=0;sr<NBin;sr++){
01063     ThickProf[sr*3  ] /= Count[sr*3  ] > 0. ? Count[sr*3  ] : 1.;
01064     ThickProf[sr*3+1] /= Count[sr*3+1] > 0. ? Count[sr*3+1] : 1.;
01065     ThickProf[sr*3+2] /= Count[sr*3+2] > 0. ? Count[sr*3+2] : 1.;
01066     if(Max < ThickProf[sr*3+2]) Max = ThickProf[sr*3+2];
01067   }
01068   for(int sr=0;sr<NBin;sr++){
01069     if(ThickProf[sr*3+2] > .5*Max){
01070       ThickProf[sr*3  ] = 0.;
01071       ThickProf[sr*3+1] = 0.;
01072     }
01073   }
01074   char FName[256]; 
01075   sprintf(FName,"ThickDensProf.dat",cFile[NFile[0]]);
01076   FILE *F2Write = fopen(FName,"w");
01077   for(int sr=0;sr<NBin;sr++){
01078     double x = sr*pEdge(0)/(double)NBin;
01079     fprintf(F2Write,"%lf %lf %lf %lf %lf\n",x,.5*(-ThickProf[sr*3  ]+ThickProf[sr*3+1]),-ThickProf[sr*3  ],ThickProf[sr*3+1],ThickProf[sr*3+2]);
01080   }
01081   for(int t=0;t<3;t++)
01082     free(Plot[t]);
01083   free(Plot);
01084   free(ThickProf);
01085   free(Count);
01086 }
01090 void ElPoly::RadDens2Thick2d(int NBin){
01091   double *ThickProf = (double *)calloc(NBin*NBin,sizeof(double));
01092   double *Count = (double *)calloc(NBin*NBin,sizeof(double));
01093   double Round = 0.001;
01094   for(int p=0;p<pNPart();p++){
01095     if(pType(p) != 1) continue;
01096     int sr = (int)((pPos(p,0)+Round)*pInvEdge(0)*NBin);
01097     int sz = (int)((pPos(p,1)+Round)*pInvEdge(1)*NBin);
01098     ThickProf[sr*NBin+sz] += pPos(p,2);
01099     Count[sr*NBin+sz] += 1.;
01100   }
01101   double Norm = 0.;
01102   for(int sr=0;sr<NBin;sr++){
01103     for(int sz=0;sz<NBin;sz++){
01104       if(Norm < ThickProf[sr*NBin+sz]) Norm = ThickProf[sr*NBin+sz];
01105     }
01106   }
01107   Norm = 1./Norm;
01108   char FName[256]; 
01109   sprintf(FName,"ThickDensProf2d.dat",cFile[NFile[0]]);
01110   FILE *F2Write = fopen(FName,"w");
01111   fprintf(F2Write,"%lf %lf %lf\n",0.,0.,0.);
01112   fprintf(F2Write,"%lf %lf %lf\n",pEdge(0),pEdge(1),1.);
01113   for(int sr=0;sr<NBin;sr++){
01114     double r = sr*pEdge(0)/(double)NBin;
01115     for(int sz=0;sz<NBin;sz++){
01116       double z = sz*pEdge(1)/(double)NBin;
01117       //if(Count[sr*2] <= 1. || Count[sr*2+1] <= 1.)continue;
01118       if(ThickProf[sr*NBin+sz]*Norm < .3) continue;
01119       fprintf(F2Write,"%lf %lf %lf\n",r,z,ThickProf[sr*NBin+sz]*Norm);
01120     }
01121   }
01122   fclose(F2Write);
01123   free(ThickProf);
01124 }
01128 void ElPoly::ThickFromDens(int NBin){
01129   int NType = 3;
01130   double Round = 0.001;
01131   double InvNBin = 1./(double)NBin;
01132   double *Plot  = (double *)calloc(NType*NBin*NBin,sizeof(double));
01133   double *Line = (double *)calloc(2*NBin,sizeof(double));
01134   double *Count = (double *)calloc(2*NBin,sizeof(double));
01135   for(int p=0;p<pNPart();p++){
01136     int vr = (int)((pPos(p,CLat1)+Round)*pInvEdge(0)*NBin);
01137     if (vr<0 || vr>NBin) continue;
01138     int vz = (int)((pPos(p,CLat2)+Round)*pInvEdge(1)*NBin);
01139     if (vz<0 || vz>NBin) continue;
01140     //int t = Pm[p].Typ;
01141     for(int t=0;t<3;t++){
01142       Plot[(vr*NBin+vz)*NType+t] += pVel(p,t);
01143     }
01144   }
01145   for(int vr=0;vr<NBin;vr++){
01146     for(int vz=0;vz<NBin;vz++){
01147       double z = vz*InvNBin*pEdge(1);
01148       double Weight = Plot[(vr*NBin+vz)*NType+1]*Plot[(vr*NBin+vz)*NType+2];
01149       int t = 0;
01150       if(z>pCm(1)) t = 1;
01151       Line[vr*2+t] += z*Weight;
01152       Count[vr*2+t] += Weight;
01153     }
01154   }
01155   FILE *FLine = fopen("ThickProf.dat","w");
01156   for(int vr=0;vr<NBin;vr++){
01157     double r = vr*InvNBin*pEdge(0);
01158     Line[vr*2] /= Count[vr*2] > 0. ? Count[vr*2] : 1.;
01159     Line[vr*2+1] /= Count[vr*2+1] > 0. ? Count[vr*2+1] : 1.;
01160     fprintf(FLine,"%lf %lf %lf\n",r,Line[vr*2],Line[vr*2+1]);
01161   }  
01162   fclose(FLine);
01163   free(Plot);
01164   free(Count);
01165   free(Line);
01166 }
01170 void ElPoly::SlabAngleProfs(int NBin,int NAngle,int Coord){
01171   NAngle = 5;
01172   double *ProfAngle = (double *)calloc(NAngle*NBin,sizeof(double));
01173   double *Count = (double *)calloc(NAngle*NBin,sizeof(double));
01174   double Ref[3] = {.5*pEdge(1),.5*pEdge(1),pEdge(2)};
01175   double BoxRad = sqrt( SQR(.5*pEdge(0)) + SQR(.5*pEdge(1)) ) ;
01176   double InvBoxRad = 1./BoxRad;
01177   int Coord1 = (Coord)%2;
01178   int Coord2 = (Coord+1)%2;
01179   FILE *Ciccia = fopen("SlabAngleSectors.dat","w");
01180   fprintf(Ciccia,"#l(%lf %lf %lf) v[%d] d[part]\n",pEdge(0),pEdge(1),10.,NBin);
01181   for(int p=0,p1=0;p<pNPart();p++){
01182     if(pPos(p,CNorm) < 1.) continue;
01183     double RelDist[2] = {pPos(p,CLat1) - Ref[CLat1],pPos(p,CLat2) - Ref[CLat2]};
01184     double r = hypot(RelDist[0],RelDist[1]);
01185     int rb = (int)(r*InvBoxRad*NBin);
01186     if(rb < 0 || rb >= NBin) continue;
01187     if(Coord == 2) RelDist[CLat2] = - RelDist[CLat2];
01188     if(Coord == 3) {RelDist[CLat1] = - RelDist[CLat1];RelDist[CLat2] = - RelDist[CLat2];}
01189     double phi = fabs(atan2(RelDist[Coord1],RelDist[Coord2]));
01190     int phib = (int)(phi*NAngle/(M_PI*.5));
01191     if(phib < 0 || phib >= NAngle) continue;
01192     ProfAngle[rb*NAngle+phib] += pPos(p,CNorm);
01193     Count[rb*NAngle+phib] += 1.;
01194     fprintf(Ciccia,"{t[%d 0 %d] x(%lf %lf %lf)}\n",p1++,phib,pPos(p,0),pPos(p,1),pPos(p,2));
01195   }
01196   fclose(Ciccia);
01197   FILE *FProf = fopen("SlabRadProfs.dat","w");
01198   for(int rb=0;rb<NBin;rb++){
01199     double r = rb*BoxRad/(double)NBin;
01200     fprintf(FProf,"%lf ",r);
01201     for(int phib=0;phib<NAngle;phib++){
01202       double Norm = Count[rb*NAngle+phib] > 0. ? 1./Count[rb*NAngle+phib] : 1.;
01203       fprintf(FProf,"%lf ",ProfAngle[rb*NAngle+phib]*Norm);
01204     }
01205     fprintf(FProf,"\n");
01206   }
01207   free(ProfAngle);
01208   free(Count);
01209 }
01213 int ElPoly::CoreF(int NSample,int How){
01214   double Cm=0.;
01215   double Volume=0.;
01216   int NVolume = NSample*NSample*NSample;
01217   int NType = 3;//Gen->NType;
01218   double *Plot = (double *) calloc(NVolume*NType,sizeof(double));
01219   double Border[3][2];
01220   Border[0][0]= 0.;
01221   Border[0][1]= pEdge(0);
01222   Border[1][0]= 0.;
01223   Border[1][1]= pEdge(1);
01224   Border[2][0]= 0.;
01225   Border[2][1]= pEdge(2);
01226   for(int f=NFile[0];f<NFile[1];f++){
01227     Processing(f);
01228     if(How == 0){
01229       if(OpenRisk(cFile[f],BF_CHAIN))return 1;
01230     }
01231     else if(How == 1){
01232       if(OpenRisk(cFile[f],BF_NANO))return 1;
01233       BfDefChain();
01234     }
01235     for(int b=0;b<pNBlock();b++){
01236       for(int p=Block[b].InitIdx;p<Block[b].EndIdx;p++){
01237    //if(strcmp(Block[b].Name,"LIPID") ) continue;
01238    int vx = (int)(NSample*(pPos(p,0)-Border[0][0])/(Border[0][1]-Border[0][0]));
01239    if( vx < 0 || vx >= NSample) continue;
01240    int vy = (int)(NSample*(pPos(p,1)-Border[1][0])/(Border[1][1]-Border[1][0]));
01241    if( vy < 0 || vy >= NSample) continue;
01242    int vz = (int)(NSample*(pPos(p,2)-Border[2][0])/(Border[2][1]-Border[2][0]));
01243    if( vz < 0 || vz >= NSample) continue;
01244    int t = pType(p);
01245    if( CHAIN_IF_TYPE(Ch[pChain(p)].Type,CHAIN_ADDED) ) t = 2;
01246    if( t < 0 || t > 3) continue;
01247    Plot[((vx*NSample+vy)*NSample+vz)*NType+t] += 1.;
01248       }
01249       Cm += pCm(2);
01250     }
01251   }
01252   printf("\n");
01253   Cm /= NFile[1] - NFile[0];
01254   FILE *FileToWrite = NULL;
01255   FileToWrite = fopen("Core.xvl","w");
01256   fprintf(FileToWrite,"#l(%lf %lf %lf) v[%d] d[%s]\n",pEdge(0),pEdge(1),pEdge(2),NSample,ChooseDraw(EL_COLOR));
01257   double *Norm = (double *)calloc(NType,sizeof(double));
01258   for(int t=0;t<NType;t++){
01259     for(int v=0;v<NVolume;v++){
01260       if(Norm[t] < Plot[v*NType+t])
01261    Norm[t] = Plot[v*NType+t];
01262     }
01263     Norm[t] = Norm[t] <= 0. ? 1. : Norm[t];
01264   }
01265   //for(int t=0;t<NType;t++){
01266   for(int vx=0;vx<NSample;vx++){
01267     for(int vy=0;vy<NSample;vy++){
01268       for(int vz=NSample-1;vz>0;vz--){
01269    double Dens0 = Plot[((vx*NSample+vy)*NSample+vz)*NType+2]/Norm[2];
01270    double Dens1 = Plot[((vx*NSample+vy)*NSample+vz)*NType+0]/Norm[0];
01271    double Dens2 = Plot[((vx*NSample+vy)*NSample+vz)*NType+1]/Norm[1];
01272    if(Dens0+Dens1+Dens2 <= 0.0)continue;
01273    fprintf(FileToWrite,"{x(%lf %lf %lf) ",
01274       Border[0][0]+vx/(double)NSample*(Border[0][1]-Border[0][0]),
01275       Border[1][0]+vy/(double)NSample*(Border[1][1] - Border[1][0]),
01276       Border[2][0]+vz/(double)NSample*(Border[2][1] - Border[2][0]));
01277    fprintf(FileToWrite,"v(%lf %lf %lf)}\n",Dens0,Dens1,Dens2);
01278    Volume += 1.;
01279       }
01280     }
01281   }
01282   //}
01283   Volume /= NSample*NSample*NSample;
01284   Volume *= (Border[0][1] - Border[0][0])*(Border[1][1] - Border[1][0])*(Border[2][1] - Border[2][0]);
01285   fprintf(FileToWrite,"#Volume %lf\n",Volume);
01286   fclose(FileToWrite);
01287   free(Plot);
01288   free(Norm);
01289   return 0;
01290 }