Allink  v0.1
ElPolyProfPre.cpp
00001 #include "ElPoly.h"
00002 
00003 void ElPoly::SumTens(){
00004   int NType = 3;
00005   double NumDiff = 0.001;
00006   double **Plot = (double **)calloc(NType,sizeof(double));
00007   int v[3];
00008   double FNorma = 1./(double)(NFile[1]-NFile[0]);
00009   char FileName[256];
00010   for(int t=0;t<3;t++) Plot[t] = (double *)calloc(CUB(NEdge),sizeof(double));
00011   double LatLim[3] = {pEdge(0),pEdge(1),pEdge(2)};
00012   double InvLatLim[3] = {1./pEdge(0),1./pEdge(1),1./pEdge(2)};
00013   for(int f=NFile[0];f<NFile[1];f++){
00014     Processing(f);
00015     if(Open(cFile[f],BF_NO))return;
00016     //ShiftSys(SHIFT_CM);
00017     for(int p=0;p<pNPart();p++){
00018       for(int d=0;d<3;d++){
00019       v[d] = (int)((pPos(p,d)+NumDiff)*InvLatLim[d]*NEdge);
00020    if(v[d] < 0 || v[d] >=NEdge) continue;
00021       }
00022       int vTot = v[0] + NEdge*(v[1] + NEdge*v[2]);
00023       for(int t=0;t<NType;t++)
00024       Plot[t][vTot] += pVel(p,t);
00025     }
00026   }
00027   sprintf(FileName,"Av%s",cFile[NFile[0]]);
00028   FILE *FileToWrite = fopen(FileName,"w");
00029   if(FileToWrite == NULL){printf("Can't open %s\n",FileName);return;}
00030   fprintf(FileToWrite,"# l(%.1f %.1f %.1f) r(%.2f %.2f %.2f) v[%d] d[color]\n",LatLim[0],LatLim[1],LatLim[2],Nano->Pos[0],Nano->Pos[1],Nano->Pos[2],NEdge);
00031   double NEdgeInv = 1./(double)NEdge;
00032   Matrice Mask(5,5);
00033   Mask.FillGaussian(.5,3.);
00034   Mask.Print();
00035   int NDim = 2;
00036   int IfMinImConv = 0;
00037   for(int t=0;t<NType;t++){
00038     Mask.ConvoluteMatrix(Plot[t],NEdge,NDim,IfMinImConv);
00039     Mask.ConvoluteMatrix(Plot[t],NEdge,NDim,IfMinImConv);
00040   }
00041   for(int vx=0;vx<NEdge;vx++)
00042     for(int vy=0;vy<NEdge;vy++)
00043       for(int vz=0;vz<NEdge;vz++){
00044    int vTot = vx + NEdge*(vy + NEdge*vz);
00045    double x = vx*LatLim[0]*NEdgeInv;
00046    double y = vy*LatLim[1]*NEdgeInv;
00047    double z = vz*LatLim[2]*NEdgeInv;
00048    if(fabs(Plot[0][vTot] + Plot[1][vTot] + Plot[2][vTot]) > 0.){
00049      fprintf(FileToWrite,"{x(%.3f %.3f %.3f) v(%lf %.2f %.2f)}\n",
00050         x,y,z,
00051         Plot[0][vTot]*FNorma,Plot[1][vTot]*FNorma,Plot[2][vTot]*FNorma);
00052    }
00053       }
00054   for(int t=0;t<NType;t++)
00055     free(Plot[t]);
00056   free(Plot);
00057   fclose(FileToWrite);
00058 }
00059 int ElPoly::PressRadial(){
00060   int NZed = NEdge;
00061   int NRad = NEdge;
00062   int NType = 3;
00063   double *Plot = (double *)calloc(NZed*NRad*NType,sizeof(double));
00064   SetEdge(.5*MIN(pEdge(CLat1),pEdge(CLat2)),3);
00065   //Vettore NanoAxis(0,0,1);
00066   //Vettore PosRel(3);
00067   //Vettore Dist(3);
00068   for(int p=0;p<pNPart();p++){
00069     double Rad = 0.;
00070     for(int d=0;d<2;d++){
00071       Rad += SQR(remainder(Nano->Pos[d] - pPos(p,d) - 0.001,pEdge(d)));
00072       //PosRel.Set(remainder(Nano->PosBf[d] - pPos(p,d),pEdge(d)),d);
00073     }
00074     //double RadDist = ABS(Dist.PerpTo3(&PosRel,&NanoAxis));
00075     double RadDist = sqrt(Rad);
00076     int vr = (int)(RadDist/pEdge(3)*NRad);
00077     int vz = (int)((pPos(p,CNorm)+.01)/pEdge(CNorm)*NZed);
00078     if(vr < 0 || vr >= NRad) continue;
00079     if(vz < 0 || vz >= NZed) continue;
00080     for(int t=0;t<3;t++){
00081       Plot[(vr*NZed+vz)*NType+t] += pVel(p,t);
00082     }
00083   }
00084   //------------Normalize----------------------
00085   double *VolContr = (double *)calloc(NRad,sizeof(double));
00086   VolumeCircSlab(VolContr,NRad);
00087   double Bound[3];
00088   double InvNZed = 1./(double)NZed;
00089   for(int t=0,n=0;t<NType;t++){
00090     Bound[t] = 0.;
00091     for(int vz=0;vz<NZed;vz++){
00092       for(int vr=0;vr<NRad;vr++){
00093    Plot[(vr*NZed+vz)*NType+t] /= VolContr[vr]*3.;
00094    if(Bound[t] < Plot[(vr*NZed+vz)*NType+t])
00095      Bound[t] = Plot[(vr*NZed+vz)*NType+t];
00096       }
00097     }
00098     if(Bound[t] < 0.) Bound[t] = 1.;
00099   }
00100   free(VolContr);  
00101   //-------------------Write---------------------------
00102   FILE *PRadial = fopen("ContourPress.xvl","w");
00103   fprintf(PRadial,"#l(%lf %lf %lf) v[%d] d[%s]\n",pEdge(3),pEdge(CNorm),1.,NZed,ChooseDraw(EL_QUAD1));
00104   FILE *PNormal = fopen("PressNormal.dat","w");
00105   for(int vr=0;vr<NRad;vr++){
00106     double PRad[3] = {0.,0.,0.};
00107     double r = vr*pEdge(3)/(double)NRad;
00108     for(int vz=0;vz<NZed;vz++){
00109       //if(Plot[(v*NBin+vv)*NType+0] + Plot[(v*NBin+vv)*NType+1] + Plot[(v*NBin+vv)*NType+2] + Plot[(v*NBin+vv)*NType+3] < .1) continue;
00110       double z = vz*pEdge(CNorm)/(double)(NZed);
00111       double Press = Plot[(vr*NZed+vz)*NType+0];
00112       double Phob  = Plot[(vr*NZed+vz)*NType+1];
00113       double Phil  = Plot[(vr*NZed+vz)*NType+2];
00114       if(ABS( Press + Phob + Phil) > 0.)
00115       fprintf(PRadial,"{x(%lf %lf 0.) v(%lf %lf %lf)}\n",r,z,Press,Phob,Phil);
00116       PRad[0] += Press;PRad[1] += Phob;PRad[2] += Phil;
00117     }
00118     fprintf(PNormal,"%lf %lf %lf %lf\n",r,PRad[0]*InvNZed,PRad[1]*InvNZed,PRad[2]*InvNZed);
00119   }
00120   fclose(PRadial);
00121   fclose(PNormal);
00122   free(Plot);
00123   free(VolContr);
00124 }
00125 int ElPoly::Tens2dCartRad(){
00126   int NType = 3;
00127   // Press, phob, phil
00128   double *TensRad = (double *)calloc(NEdge*NEdge*NType,sizeof(double));
00129   double *TensNorm = (double *)calloc(NEdge*NEdge*NType,sizeof(double));
00130   double *TensAng = (double *)calloc(NEdge*NEdge*NType,sizeof(double));
00131   //x y z 
00132   double *TensRadTemp = (double *)calloc(NEdge*NEdge*3,sizeof(double));
00133   if(NFile[1]-NFile[0] != 3) return 1;
00134   for(int f=NFile[0];f<NFile[1];f++){
00135     if(Open(cFile[f],BF_PART))return 1;
00136     for(int p=0;p<pNPart();p++){
00137       int vr = (int)((pPos(p,0)+.0001)*NEdge*pInvEdge(0));
00138       int vz = (int)((pPos(p,1)+.0001)*NEdge*pInvEdge(1));
00139       int vTot = (vr*NEdge+vz)*NType;
00140       //pressure
00141       TensRadTemp[vTot+f] += pVel(p,0);
00142       //phob density
00143       TensRad[vTot+1] += pVel(p,1)/3.;
00144       //phil density
00145       TensRad[vTot+2] += pVel(p,2)/3.;
00146     }
00147   }
00148   for(int vr=0;vr<NEdge;vr++){
00149     for(int vz=0;vz<NEdge;vz++){
00150       int vTot = (vr*NEdge+vz)*NType;
00151       double Rad = sqrt(SQR(TensRadTemp[vTot+0])+SQR(TensRadTemp[vTot+1]));
00152       double Trace = TensRadTemp[vTot+0] + TensRadTemp[vTot+1] + TensRadTemp[vTot+2];
00153       double Ang = .5*(Trace - Rad);
00154       //double Ang = atan2(TensRadTemp[vTot+1],TensRadTemp[vTot+0]);
00155       TensRad[vTot+0] = Rad - .5*(Ang+TensRadTemp[vTot+2]);
00156       //TensNorm[vTot] = - TensRadTemp[vTot+2] + .5*(Ang+Rad);
00157       TensNorm[vTot] = TensRadTemp[vTot+2] - .5*(TensRadTemp[vTot+0]+TensRadTemp[vTot+1]);
00158       TensAng[vTot+0] = Ang - .5*(Rad+TensRadTemp[vTot+2]);
00159     }
00160   }
00161   FILE *RadSurfTens = fopen("SurfTensRad.xvl","w");
00162   FILE *NormSurfTens = fopen("SurfTensNorm.xvl","w");
00163   FILE *AngSurfTens = fopen("SurfTensAng.xvl","w");
00164   fprintf(RadSurfTens,"# l(%.2f %.2f %.2f) r(%.2f %.2f %.2f) v[%d] d[color]\n",pEdge(0),pEdge(1),pEdge(2),Nano->Pos[0],Nano->Pos[1],Nano->Pos[2],NEdge);
00165   fprintf(NormSurfTens,"# l(%.2f %.2f %.2f) r(%.2f %.2f %.2f) v[%d] d[color]\n",pEdge(0),pEdge(1),pEdge(2),Nano->Pos[0],Nano->Pos[1],Nano->Pos[2],NEdge);
00166   fprintf(AngSurfTens,"# l(%.2f %.2f %.2f) r(%.2f %.2f %.2f) v[%d] d[color]\n",pEdge(0),pEdge(1),pEdge(2),Nano->Pos[0],Nano->Pos[1],Nano->Pos[2],NEdge);
00167   for(int vr=0;vr<NEdge;vr++){
00168     for(int vz=0;vz<NEdge;vz++){
00169       int vTot = (vr*NEdge+vz)*NType;
00170       double r = vr*pEdge(0)/NEdge;
00171       double z = vz*pEdge(1)/NEdge;
00172       if(fabs(TensRad[vTot+0]+TensRad[vTot+1]+TensRad[vTot+2])>0.){
00173    fprintf(RadSurfTens,"{x(%.4f %.4f %.4f) v(%lf %lf %lf)}\n",
00174          r,z,0.,TensRad[vTot+0],TensRad[vTot+1],TensRad[vTot+2]);
00175    fprintf(NormSurfTens,"{x(%.4f %.4f %.4f) v(%lf %lf %lf)}\n",
00176          r,z,0.,TensNorm[vTot],TensRad[vTot+1],TensRad[vTot+2]);
00177    fprintf(AngSurfTens,"{x(%.4f %.4f %.4f) v(%lf %lf %lf)}\n",
00178          r,z,0.,TensAng[vTot],TensRad[vTot+1],TensRad[vTot+2]);
00179       }
00180     }
00181   }
00182   free(TensRad);
00183   free(TensNorm);
00184   free(TensAng);
00185   free(TensRadTemp);
00186   fclose(RadSurfTens);
00187   fclose(NormSurfTens);
00188   fclose(AngSurfTens);
00189 }
00190 int ElPoly::SurfTens(int NBin){
00191   int NType = 3;
00192   double **TensRad = (double **)calloc(NBin,sizeof(double));
00193   double **NormRad = (double **)calloc(NBin,sizeof(double));
00194   double **TensAng = (double **)calloc(NBin,sizeof(double));
00195   double **NormAng = (double **)calloc(NBin,sizeof(double));
00196   double **TensNorm= (double **)calloc(NEdge,sizeof(double));
00197   double **NormNorm= (double **)calloc(NEdge,sizeof(double));
00198   double **TensCart = (double **)calloc(NBin,sizeof(double));
00199   double **NormCart = (double **)calloc(NBin,sizeof(double));
00200   for(int v=0;v<NBin;v++){
00201     TensRad[v] = (double *)calloc(NType,sizeof(double));
00202     NormRad[v] = (double *)calloc(NType,sizeof(double));
00203     TensNorm[v] = (double *)calloc(NType,sizeof(double));
00204     NormNorm[v] = (double *)calloc(NType,sizeof(double));
00205     TensAng[v] = (double *)calloc(NType,sizeof(double));
00206     NormAng[v] = (double *)calloc(NType,sizeof(double));
00207     TensCart[v] = (double *)calloc(NType,sizeof(double));
00208     NormCart[v] = (double *)calloc(NType,sizeof(double));
00209   }
00210   //double *Plot = (double *)calloc(NBin*NBin,sizeof(double));
00211   SetEdge(MIN((.5*pEdge(CLat1)),(.5*pEdge(CLat2))),3);
00212   double *VolContr = (double *)calloc(NBin,sizeof(double));
00213   VolumeCircSlab(VolContr,NBin);
00214   double Prex = 0.;
00215   double Prey = 0.;
00216   int IfTens = 0;
00217   if(NFile[1]-NFile[0] == 3) IfTens = 1;
00218   for(int f=NFile[0];f<NFile[1];f++){
00219     Processing(f);
00220     if(Open(cFile[f],BF_PART))return 0;
00221     //BackFold(BF_PART);
00222     //Vettore NanoAxis(Nano->Axis[0],Nano->Axis[1],Nano->Axis[2]);
00223     Vettore NanoAxis(0,0,1);
00224     Vettore PosRel(3);
00225     Vettore Dist(3);
00226     for(int p=0;p<pNPart();p++){
00227       for(int d=0;d<3;d++){
00228    PosRel.Set(remainder(pNanoPos(0,d) - pPos(p,d),pEdge(d)),d);
00229       }
00230       double RadDist = ABS(Dist.PerpTo3(&PosRel,&NanoAxis));
00231       int vr = (int)(RadDist/pEdge(3)*NBin);
00232       int vz = (int)((pPos(p,CNorm)+.01)/pEdge(CNorm)*NEdge);
00233       if(vr < 0 || vr >= NBin){continue;}
00234       if(vz < 0 || vz >= NEdge){printf("%d out of %d\n",vz,NBin);continue;}
00235       for(int t=1;t<3;t++){
00236    TensRad[vr][t] += pVel(p,t);
00237    NormRad[vr][t] += 1.;
00238    TensNorm[vz][t] += pVel(p,t);
00239    NormNorm[vz][t] += 1.;
00240       }
00241       if(IfTens == 0){
00242    TensRad[vr][0] += pVel(p,0);
00243    NormRad[vr][0] += 1.;
00244    TensNorm[vz][0] += pVel(p,0);
00245    NormNorm[vz][0] += 1.;
00246       }
00247       else if(IfTens){
00248    TensCart[vr][f] += pVel(p,0);
00249    NormCart[vr][f] += 1.;
00250    double vPre = (pVel(p,1)+pVel(p,2)-pVel(p,0));
00251          if(f==CLat1 || f == CLat2){
00252            TensNorm[vz][0] -= .5*vPre;
00253      if(NType>3){
00254        TensNorm[vz][3] -= .5*vPre*pPos(p,CNorm);
00255        NormNorm[vz][3] += 1.;
00256      }
00257      if(NType>4){
00258        TensNorm[vz][4] -= .5*vPre*pPos(p,CNorm)*pPos(p,CNorm);
00259        NormNorm[vz][4] += 1.;
00260      }
00261      if(f==CLat1)
00262        Prex += vPre;
00263      else 
00264        Prey += vPre;
00265          }
00266       if(f==CNorm){
00267            TensNorm[vz][0] += vPre;
00268      if(NType>3){
00269        TensNorm[vz][3] += vPre*pPos(p,CNorm);
00270        NormNorm[vz][3] += 1.;
00271      }
00272      if(NType>4){
00273        TensNorm[vz][4] += vPre*pPos(p,CNorm)*pPos(p,CNorm);
00274        NormNorm[vz][4] += 1.;
00275      }
00276          }
00277          NormNorm[vz][0] += 1.;
00278       }
00279     } 
00280   }
00281   printf("\n");
00282   double VolElm = 1.;//pEdge(CNorm)/(double)NBin;
00283   FILE *File2Write = fopen("TensRadial.dat","w");
00284   for(int v=0;v<NBin;v++){
00285     for(int d=0;d<3;d++){
00286       TensCart[v][d] /= NormCart[v][d] > 0. ? VolContr[v]*NormCart[v][d] : 1.;
00287     }
00288     TensRad[v][0] = sqrt( SQR(TensCart[v][CLat1])+SQR(TensCart[v][CLat2]) );
00289     TensRad[v][0] -= 5.*.5*(TensCart[v][0]+TensCart[v][1]+TensCart[v][2]-sqrt( SQR(TensCart[v][CLat1])+SQR(TensCart[v][CLat2])) );
00290     //TensRad[v][0] += atan2(TensCart[v][CLat2],TensCart[v][CLat1]);
00291     TensRad[v][0] -= .5*TensCart[v][CNorm];
00292   }
00293   for(int v=0;v<NBin;v++){
00294     fprintf(File2Write,"%lf ",v/(double)NBin*pEdge(3));
00295     for(int t=0;t<NType;t++){
00296       TensRad[v][t] /= NormRad[v][t] > 0. ? NormRad[v][t]: 1.;
00297       fprintf(File2Write,"%lf ",TensRad[v][t]);
00298     }
00299     fprintf(File2Write,"\n");
00300   }
00301   fclose(File2Write);
00302   File2Write = fopen("TensNormal.dat","w");
00303   fprintf(File2Write,"#Press DensPhob DensPhil SponCurv SaddleSplay \n");
00304   for(int v=0;v<NEdge;v++){
00305     fprintf(File2Write,"%lf ",v/(double)NEdge*pEdge(CNorm));
00306     for(int t=0;t<NType;t++){
00307       TensNorm[v][t] /= NormNorm[v][t] > 0. ? VolElm*NormNorm[v][t] : 1.;
00308       fprintf(File2Write,"%lf ",TensNorm[v][t]);
00309     }
00310     fprintf(File2Write,"\n");
00311   }
00312   fclose(File2Write);
00313   File2Write = fopen("TensAngle.dat","w");
00314   fprintf(File2Write,"#Press DensPhob DensPhil SponCurv SaddleSplay \n");
00315   for(int v=0;v<NEdge;v++){
00316     fprintf(File2Write,"%lf ",v/(double)NEdge*pEdge(CNorm));
00317     for(int t=0;t<NType;t++){
00318       TensAng[v][t] /= NormNorm[v][t] > 0. ? VolElm*NormNorm[v][t] : 1.;
00319       fprintf(File2Write,"%lf ",TensNorm[v][t]);
00320     }
00321     fprintf(File2Write,"\n");
00322   }
00323   fclose(File2Write);
00324   for(int v=0;v<NBin;v++){
00325     free(TensRad[v]);
00326     free(NormRad[v]);
00327   }
00328   for(int v=0;v<NEdge;v++){
00329     free(TensNorm[v]);
00330     free(NormNorm[v]);
00331   }
00332   free(TensRad);
00333   free(NormRad);
00334   free(TensNorm);
00335   free(NormNorm);
00336   free(VolContr);
00337   //free(Plot);
00338 }
00339 int ElPoly::PressTrace(){
00340   int NType = 3;
00341   double *Sum = (double *)calloc(NType*CUBE(NEdge),sizeof(double));
00342   if(NFile[1]-NFile[0] != 3){
00343     printf("Just three files\n");
00344     return 1;
00345   }
00346   for(int f=NFile[0];f<NFile[1];f++){
00347     Processing(f);
00348     if(Open(cFile[f],BF_NO))return 1;
00349     for(int p=0;p<pNPart();p++){
00350       int vx = (int)((pPos(p,CLat1)+.1)/pEdge(CLat1)*NEdge);
00351       int vy = (int)((pPos(p,CLat2)+.1)/pEdge(CLat2)*NEdge);
00352       int vz = (int)((pPos(p,CNorm)+.1)/pEdge(CNorm)*NEdge);
00353       if(vx < 0 || vx >= NEdge) continue;
00354       if(vy < 0 || vy >= NEdge) continue;
00355       if(vz < 0 || vz >= NEdge) continue;
00356       int vTot = (vx*NEdge+vy)*NEdge+vz;
00357       Sum[vTot*NType+0] += pVel(p,0)/3.;
00358       Sum[vTot*NType+1] += pVel(p,1)/3.;
00359       Sum[vTot*NType+2] += pVel(p,2)/3.;
00360     }
00361   }
00362   printf("\n");
00363   FILE *FileToWrite = fopen("PressTrace.xvl","w");
00364   fprintf(FileToWrite,"# l(%.1f %.1f %.1f) r(%.2f %.2f %.2f) v[%d] d[color]\n",pEdge(0),pEdge(1),pEdge(2),Nano->Pos[0],Nano->Pos[1],Nano->Pos[2],NEdge);
00365   for(int vx=0;vx<NEdge;vx++){
00366     double x = vx*pEdge(CLat1)/(double)NEdge;
00367     for(int vy=0;vy<NEdge;vy++){
00368       double y = vy*pEdge(CLat2)/(double)NEdge;
00369       for(int vz=0;vz<NEdge;vz++){
00370    double z = vz*pEdge(CNorm)/(double)NEdge;
00371    int v = (vx*NEdge+vy)*NEdge+vz;
00372    if(Sum[v*NType+0] + Sum[v*NType+1] + Sum[v*NType+2] < 0.1) continue;
00373    fprintf(FileToWrite,"{x(%.2f %.2f %.2f)",x,y,z);
00374    fprintf(FileToWrite," v( %lf %.2f %.2f)}\n",Sum[v*NType+0],Sum[v*NType+1],Sum[v*NType+2]);
00375       }
00376     }
00377   }
00378   fclose(FileToWrite);
00379   free(Sum);
00380   //free(Plot);
00381 }
00382 void ElPoly::RestPress(int NBin){
00383   int NType = 3;
00384   double Round = 0.00001;
00385   double InvNBin = 1./(double)NBin;
00386   double **Plot1  = (double **)calloc(NType,sizeof(double));
00387   for(int t=0;t<NType;t++){
00388     Plot1[t] = (double *)calloc(NBin*NBin,sizeof(double));
00389   }
00390   double *Count1 = (double *)calloc(NType*NBin*NBin,sizeof(double));
00391   double *Count2 = (double *)calloc(NType*NBin*NBin,sizeof(double));
00392   double LatDim[3] = {pEdge(CLat1),pEdge(CLat2),pEdge(CNorm)};
00393   double InvLatDim[2] = {1./LatDim[0],1./LatDim[1]};
00394   double FirstCm[3] = {pCm(CLat1),pCm(CLat2),pCm(CNorm)};
00395   for(int p=0;p<pNPart();p++){
00396     int vr = (int)((pPos(p,CLat1)+Round)*InvLatDim[0]*NBin);
00397     if (vr < 0 || vr >= NBin) continue;
00398     int vz = (int)((pPos(p,CLat2)+Round)*InvLatDim[1]*NBin);
00399     if (vz < 0 || vz >= NBin) continue;
00400     int t = Pm[p].Typ;
00401     for(int t=0;t<3;t++){
00402       Plot1[t][(vr*NBin+vz)] += pVel(p,t);//pPos(p,2);
00403       Count1[(vr*NBin+vz)*NType+t] += 1.;
00404     }
00405   }
00406   FILE *FTecPlot = fopen("TecPlotPressDiff.dat","w");
00407   fprintf(FTecPlot,"VARIABLES = \"R\", \"Z\", \"diff\", \"d1\",\"d2\"\n");
00408   fprintf(FTecPlot,"ZONE J=%d, K=%d, F=POINT\n",NBin,NBin);
00409   for(int vx=0;vx<NBin;vx++){
00410     for(int vy=0;vy<NBin;vy++){
00411       double r = vx*pEdge(0)/(double)NBin;
00412       double z = vy*pEdge(1)/(double)NBin - pEdge(1)*.5;
00413       //fprintf(FTecPlot,"%lf %lf %lf %lf %lf\n",r,z,diff0,diff1,diff2);
00414       double Diff = Plot1[0][vx*NBin+vy] - Plot1[1][vx*NBin+vy] - Plot1[2][vx*NBin+vy];
00415       fprintf(FTecPlot,"%lf %lf %lf %lf %lf\n",r,z,Diff,Plot1[1][vx*NBin+vy],Plot1[2][vx*NBin+vy]);
00416     }
00417   }
00418   fclose(FTecPlot);
00419   //difference
00420   FILE *Difference = fopen("PressDifference.xvl","w");
00421   fprintf(Difference,"#l(%lf %lf %lf) v[%d] d[%s]\n",pEdge(CLat1),pEdge(CLat2),pEdge(CNorm),NBin,ChooseDraw(EL_COLOR));
00422   int link[4] = {0,0,0,0};
00423   for(int t=0,p=0,c=0;t<NType;t++){
00424     for(int vx=0;vx<NBin-1;vx++){
00425       for(int vy=0;vy<NBin-1;vy++){
00426    double Diff = Plot1[0][vx*NBin+vy] - Plot1[1][vx*NBin+vy] - Plot1[2][vx*NBin+vy];
00427    fprintf(Difference,"{t[%d %d %d] x(%lf %lf %lf) v(%lf %lf %lf)}\n",p,c,t,vx*InvNBin*pEdge(0),vy*InvNBin*pEdge(1),0.,Diff,Plot1[1][vx*NBin+vy],Plot1[2][vx*NBin+vy]);
00428    p++;
00429       }
00430     }
00431   }
00432   fclose(Difference);
00433   for(int t=0;t<NType;t++){
00434     free(Plot1[t]);
00435   }
00436   free(Plot1);
00437   free(Count1);
00438 }
00439 int ElPoly::Diff2Files(int NBin,int How){
00440   if(NFile[1] - NFile[0] != 2){
00441     printf("The number of files must be two\n");
00442     return 1;
00443   }
00444   int NType = 3;
00445   double Round = 0.00001;
00446   NBin = MIN(NBin,NEdge);
00447   double InvNBin = 1./(double)NBin;
00448   double **Plot1  = (double **)calloc(NType,sizeof(double));
00449   double **Plot2  = (double **)calloc(NType,sizeof(double));
00450   double **PlotDiff  = (double **)calloc(NType,sizeof(double));
00451   for(int t=0;t<NType;t++){
00452     Plot1[t] = (double *)calloc(NBin*NBin,sizeof(double));
00453     Plot2[t] = (double *)calloc(NBin*NBin,sizeof(double));
00454     PlotDiff[t] = (double *)calloc(NBin*NBin,sizeof(double));
00455   }
00456   double *Count1 = (double *)calloc(NType*NBin*NBin,sizeof(double));
00457   double *Count2 = (double *)calloc(NType*NBin*NBin,sizeof(double));
00458   double LatDim[3] = {pEdge(CLat1),pEdge(CLat2),pEdge(CNorm)};
00459   double InvLatDim[2] = {1./LatDim[0],1./LatDim[1]};
00460   double FirstCm[3] = {pCm(CLat1),pCm(CLat2),pCm(CNorm)};
00461   for(int p=0;p<pNPart();p++){
00462     int vr = (int)((pPos(p,CLat1)+Round)*InvLatDim[0]*NBin);
00463     if(vr < 0 || vr >= NBin) continue;
00464     int vz = (int)((pPos(p,CLat2)+Round)*InvLatDim[1]*NBin);
00465     if(vz < 0 || vz >= NBin) continue;
00466     if(How==0){//Press
00467       for(int t=0;t<3;t++){
00468    Plot1[t][vr*NBin+vz] += pVel(p,t);
00469    Count1[(vr*NBin+vz)*NType+t] += 1.;
00470       }
00471     }
00472     else{//Dens
00473       int t = Pm[p].Typ;
00474       Plot1[t][vr*NBin+vz] += pPos(p,2);
00475       Count1[(vr*NBin+vz)*NType+t] += 1.;
00476     }
00477   }
00478   if(Open(cFile[NFile[1]-1],BF_NO))return 1;
00479   for(int p=0;p<pNPart();p++){
00480     int vr = (int)((pPos(p,CLat1)+Round+1.)*InvLatDim[0]*NBin);
00481     if (vr < 0 || vr >= NBin) continue;
00482     int vz = (int)((pPos(p,CLat2)+Round)*InvLatDim[1]*NBin);
00483     if (vz < 0 || vz >= NBin) continue;
00484     if(How==0){//Press
00485       for(int t=0;t<3;t++){
00486    Plot2[t][vr*NBin+vz] += pVel(p,t);
00487    Count2[(vr*NBin+vz)*NType+t] += 1.;
00488       }
00489     }
00490     else{//Dens
00491       int t = Pm[p].Typ;
00492       Plot2[t][vr*NBin+vz] += pPos(p,2);
00493       Count2[(vr*NBin+vz)*NType+t] += 1.;
00494     }
00495   }
00496   Matrice Mask(5,5);
00497   Mask.FillGaussian(.5,3.);
00498   Mask.Print();
00499   for(int t=0;t<NType;t++){
00500     //ConvoluteMatrix(Plot1[t],NBin,&Mask,2);
00501     // ConvoluteMatrix(Plot1[t],NBin,&Mask,2);
00502     //ConvoluteMatrix(Plot2[t],NBin,&Mask,2);
00503     // ConvoluteMatrix(Plot2[t],NBin,&Mask,2);
00504   }
00505   for(int vr=0;vr<NBin;vr++){
00506     for(int vz=0;vz<NBin;vz++){
00507       for(int t=0;t<NType;t++){
00508    Plot1[t][(vr*NBin+vz)] /= Count1[(vr*NBin+vz)*NType+t] > 0. ? Count1[(vr*NBin+vz)*NType+t] : 1.;
00509    Plot2[t][(vr*NBin+vz)] /= Count2[(vr*NBin+vz)*NType+t] > 0. ? Count2[(vr*NBin+vz)*NType+t] : 1.;
00510    PlotDiff[t][(vr*NBin+vz)] = Plot1[t][(vr*NBin+vz)] - Plot2[t][(vr*NBin+vz)];
00511       }
00512       int t = 0;
00513     }
00514   }
00515   int NDim = 2;
00516   int IfMinImConv = 0;
00517   for(int t=0;t<NType;t++){
00518     Mask.ConvoluteMatrix(PlotDiff[t],NBin,NDim,IfMinImConv);
00519     // Mask.ConvoluteMatrix(PlotDiff[t],NBin,2);
00520   }
00521   //tecplot
00522   FILE *FTecPlot = fopen("TecPlotDiff.dat","w");
00523   fprintf(FTecPlot,"VARIABLES = \"R\", \"Z\", \"diff\", \"d1\",\"d2\"\n");
00524   fprintf(FTecPlot,"ZONE J=%d, K=%d, F=POINT\n",NBin,NBin);
00525   //difference
00526   FILE *Difference;
00527   if(How) Difference = fopen("DensDifference.rzd","w");
00528   else Difference = fopen("PressDifference.xvl","w"); 
00529   if(How==1){//Dens
00530     PrintDens(Difference,PlotDiff,LatDim,NBin);
00531     for(int vx=0;vx<NBin;vx++){
00532       for(int vy=0;vy<NBin;vy++){
00533    double r = vx*LatDim[0]/(double)NBin;
00534    double z = vy*LatDim[1]/(double)NBin - LatDim[1]*.5;
00535    fprintf(FTecPlot,"%lf %lf %lf %lf %lf\n",r,z,PlotDiff[0][vx*NBin+vy],Plot1[0][vx*NBin+vy],Plot2[0][vx*NBin+vy]);
00536       }
00537     }
00538   }
00539   else{//Pre
00540     fprintf(Difference,"# l(%.1f %.1f %.1f) v[%d] d[color]\n",LatDim[0],LatDim[1],LatDim[2],NBin);
00541     for(int vr=0;vr<NBin;vr++){
00542       for(int vz=0;vz<NBin;vz++){
00543    double NanoAdded = Plot1[2][(vr*NBin+vz)];
00544    double Phob = PlotDiff[0][(vr*NBin+vz)];
00545    double Phil = PlotDiff[1][(vr*NBin+vz)];
00546    double r = (vr)*InvNBin*LatDim[0];
00547    double z = (vz)*InvNBin*LatDim[1];
00548    double dens = (PlotDiff[2][(vr*NBin+vz)]);
00549    fprintf(Difference,"{x(%lf %lf %lf) v(%lf %lf %lf)}\n",r,z,dens,NanoAdded,Phob,Phil);
00550    double z1 = vz*LatDim[1]/(double)NBin - LatDim[1]*.5;
00551    fprintf(FTecPlot,"%lf %lf %lf %lf %lf\n",r,z1,PlotDiff[0][vr*NBin+vz],Plot1[1][vr*NBin+vz],Plot2[1][vr*NBin+vz]);
00552       }
00553     }
00554   }
00555   fclose(FTecPlot);
00556   fclose(Difference);
00557   for(int t=0;t<NType;t++){
00558     free(Plot1[t]);
00559     free(Plot2[t]);
00560   }
00561   free(Plot1);
00562   free(Plot2);
00563   free(Count1);
00564   free(Count2);
00565   return 0;
00566 }