Allink  v0.1
Forces.cpp
00001 #include "Forces.h"
00002 #include <stdarg.h>
00003 void Forces::Shout(const char * s, ...){
00004 #ifndef DIN_DEBUG
00005   va_list args;
00006   va_start(args, s);
00007   fprintf(stderr, ">");
00008   vfprintf(stderr, s, args);
00009   fprintf(stderr, "\n");
00010   va_end(args);
00011 #else  
00012   return;
00013 #endif
00014 }
00015 Forces::Forces(int argc,char **argv,int NInEdge,char *ConfFileExt){
00016   Shout("Constructor/no starting configuration");
00017   InitConst();
00018   sprintf(ConfFile,"%s",ConfFileExt);
00019   char ConfF[40];
00020   sprintf(ConfF,ConfFile);
00021   if(ReadConfDinamica(ConfF)){
00022     Dx = 1./(double)(NEdge-1);
00023   }
00024   if(VAR_IF_TYPE(SysShape,SYS_2D)){
00025     nEdge[0] = NEdge;
00026     double Ratio = pEdge(1)*pInvEdge(0);
00027     nEdge[1] = (int)(nEdge[0]*Ratio + 0.0001);
00028     for(int i=0;;i++){
00029       if((int)(nEdge[1]/Ratio) == nEdge[0]) break;
00030       nEdge[0]++;
00031       nEdge[1] = (int)(nEdge[0]*Ratio + 0.0001);
00032       printf("using: nEdge[0] %d nEdge[1] %d\n",nEdge[0],nEdge[1]);
00033       if(i>=10){
00034    printf("Could not find the appropriate border partition %d %d\n",nEdge[0],nEdge[1]);
00035    exit(0);
00036       }
00037     }
00038     SetNLink(4);
00039     ReSetNPart(nEdge[0]*nEdge[1]);
00040     ReSetNChain(nEdge[1]);
00041     SetNPCh(nEdge[0]);
00042   }
00043   else if(VAR_IF_TYPE(SysShape,SYS_3D)){
00044     SetNLink(6);
00045     ReSetNChain(NEdge*NEdge);
00046     ReSetNPart(NEdge*NEdge*NEdge);
00047   }
00048   else if(VAR_IF_TYPE(SysShape,SYS_ROD)){
00049     SetNLink(1);
00050     ReSetNChain(1);
00051     ReSetNPart(NEdge);
00052   }
00053   else if(VAR_IF_TYPE(SysShape,SYS_STALK)){
00054     SetNLink(3);
00055     ReSetNPart(4*NEdge);
00056     ReSetNChain(4);
00057     //    Kf.El[0] *= Gen->NPart;
00058   }
00059   else if(VAR_IF_TYPE(SysShape,SYS_LEAVES)){
00060     SetNLink(3);
00061     ReSetNPart(NEdge);
00062     ReSetNChain(1);
00063     //    Kf.El[0] *= Gen->NPart;
00064   }
00065   else if(VAR_IF_TYPE(SysShape,SYS_PORE)){
00066     SetNLink(3);
00067     ReSetNPart(NEdge);
00068     ReSetNChain(1);
00069   }
00070   else if(VAR_IF_TYPE(SysShape,SYS_1D)){
00071     SetNLink(2);
00072     ReSetNPart(NEdge);
00073     ReSetNChain(1);
00074   }
00075   else if(VAR_IF_TYPE(SysShape,SYS_RIGID)){
00076     ReSetNPart(0);
00077     SetNLink(0);
00078   }
00079   else if(VAR_IF_TYPE(SysShape,SYS_TRIAL)){
00080     ReSetNPart(3*3*3);
00081     SetNLink(0);
00082     ReSetNChain(1);
00083   }
00084   else if(VAR_IF_TYPE(SysShape,SYS_MD)){
00085     SetNLink(0);
00086     ReSetNPart(NEdge);
00087     ReSetNChain(1);
00088   }
00089   else if(VAR_IF_TYPE(SysShape,SYS_MC)){
00090     SetNLink(0);
00091     ReSetNPart(NEdge);
00092     ReSetNChain(1);
00093   }
00094   else if(VAR_IF_TYPE(SysShape,SYS_ELECTRO)){
00095     SetNLink(4);
00096     ReSetNPart(NEdge+NSpline);
00097     ReSetNChain(1);
00098   }
00099   ReSetNPCh(pNPart()/pNChain());
00100   //SetNPCh(NEdge);
00101   SetDeltat(Deltat);
00102   SetStep(0);
00103   SetNType(2);
00104   CreateInitial();
00105   {
00106     MInt = new MatInt(3,3);
00107     MInt->SetCoeff(-24.33,0,0);
00108     MInt->SetCoeff(-7.22,0,1);
00109     MInt->SetCoeff(-24.33,0,2);
00110     MInt->SetCoeff(-0.1,1,1);
00111     MInt->SetCoeff(-7.22,1,2);
00112     MInt->SetCoeff(0.,2,2);
00113     MInt->SetCoeff(3.,0,0,0);
00114     MInt->SetCoeff(3.,0,0,1);
00115     MInt->SetCoeff(3.,0,0,2);
00116     MInt->SetCoeff(3.,0,1,1);
00117     MInt->SetCoeff(3.,0,1,2);
00118     MInt->SetCoeff(3.,0,2,2);
00119     MInt->SetCoeff(0.,1,1,1);
00120     MInt->SetCoeff(3.,1,1,2);
00121     MInt->SetCoeff(3.,1,2,2);
00122     MInt->SetCoeff(0.,2,2,2);
00123   }
00124   AllocMethod();
00125   PrepareSys();
00126   PrepareParallel(argc,argv);
00127   if(VAR_IF_TYPE(SysShape,SYS_LEAVES)) IfNano = 2;
00128   if(VAR_IF_TYPE(SysShape,SYS_PORE)) IfNano = 2;
00129   if(VAR_IF_TYPE(SysShape,SYS_ELECTRO)){
00130     Pm[NEdge].Typ = 2;
00131     Pm[NEdge+1].Typ = 2;
00132   }
00133   //Pc->PrintCells();
00134   //for(int p=0;p<pNPart();p++) CheckDomDec(p);
00135   //Interp();
00136 }
00137 Forces::~Forces(){
00138   Shout("Freeing");
00139   fclose(StatFile1);
00140   fclose(StatFile2);
00141   Shout("Freeing/md: forces domain decomposition");  
00142   if(VAR_IF_TYPE(SysAlloc,ALL_FORCES)){
00143     delete [] Fm;
00144   }
00145   if(VAR_IF_TYPE(SysAlloc,ALL_MD)){
00146     delete Pc;
00147   }
00148   if(VAR_IF_TYPE(SysAlloc,ALL_MC)){
00149     delete Pc;
00150     delete [] OldNrgBead;
00151     delete [] OldNrgCh;
00152     for(int p=0;p<pNPCh();p++){
00153       delete [] OldPos[p];
00154     }
00155     delete [] OldPos;
00156   }
00157   if(VAR_IF_TYPE(SysAlloc,ALL_BIAS)){
00158     for(int t=0;t<NTrialBias;t++){
00159       delete [] BondPosBias[t];
00160     }
00161     delete [] BondPosBias;
00162     delete [] CumProbBias;
00163   }  
00164   if(VAR_IF_TYPE(SysAlloc,ALL_SPLINE)){
00165     free(Pl);
00166   }
00167   if(VAR_IF_TYPE(SysAlloc,ALL_DENS)){
00168     free(LocDens2);
00169     free(LocDens3);
00170     free(Dens2);
00171     free(Dens3);
00172   }
00173   if(VAR_IF_TYPE(SysShape,SYS_ROD)){
00174     delete IntMatrix;
00175   }
00176   if(VAR_IF_TYPE(SysShape,SYS_2D)){
00177     delete IntMatrix;
00178   }
00179   if(VAR_IF_TYPE(SysShape,SYS_LEAVES)){
00180     delete IntMatrix;
00181   }
00182 #ifdef __glut_h__
00183   free(Cylinder);
00184 #endif
00185 }
00186 Forces::Forces(int argc,char **argv,char *ConfF,char *Snapshot){
00187   Shout("constructor with intial configuration");
00188   InitConst();
00189   if(ReadConfDinamica(ConfF)){
00190     Dx = 1./(double)(NEdge-1);
00191   }
00192   Open(Snapshot,BF_PART);
00193   if(VAR_IF_TYPE(SysShape,SYS_LEAVES)) IfNano = 2;
00194   if(VAR_IF_TYPE(SysShape,SYS_PORE)) IfNano = 2;
00195   AllocMethod();
00196   PrepareParallel(argc,argv);
00197   PrepareSys();
00198  //Interp();
00199 }
00200 void Forces::PrepareSys(){
00201   Shout("Preparing system");
00202   OldNrgSys = 0.;
00203   DefNanoForceParam();
00204   /*calculate the energies per chain or per particle for
00205     the calculation of the MC.
00206   */
00207   if(VAR_IF_TYPE(SysShape,SYS_MC)){
00208     if(VAR_IF_TYPE(SysAlloc,ALL_DENS)){
00209       Shout("Preparing system/adding densities\n");
00210       OldNrgSys = DensFuncNrgSys();
00211       ChemPotEx += NrgPBead;
00212       if(VAR_IF_TYPE(CalcMode,CALC_NcVT)){
00213    //CalcTotNrgCh();
00214       }
00215       if(VAR_IF_TYPE(CalcMode,CALC_NVT)){
00216    ;//CalcTotNrgBead();
00217       }
00218       if(VAR_IF_TYPE(CalcMode,CALC_mcVT)){
00219    if(!VAR_IF_TYPE(CalcMode,CALC_CONF_BIAS))
00220      ;//CalcTotNrgCh();
00221       }
00222     }
00223     //defining all the allocated chains
00224     for(int p=0;p<pNAllocP();p++){
00225       int c = (int)(p/(double)pNPCh());
00226       Pm[p].CId = c;
00227       Pm[p].Typ = 0;
00228       if( p%pNPCh() >= Block[0].Asym ) Pm[p].Typ = 1;
00229       if( p%pNPCh() == pNPCh() - 1) continue;
00230       Ln[p].NLink = 1;
00231       Ln[p].Link[0] = p+1;
00232     }
00233   }
00234   if(VAR_IF_TYPE(SysShape,SYS_ELECTRO)){
00235     for(int p=0;p<NEdge;p++){
00236       Pm[p].Typ = 2;
00237       Pm[p].Idx = p;
00238       Pm[p].CId = p;
00239       Ln[p].NLink = 0;
00240       SetBkf(p);
00241     }
00242     for(int p=NEdge;p<NEdge+NSpline;p++){
00243       Pm[p].Typ = 0;
00244       Pm[p].Idx = p;
00245       Pm[p].CId = NEdge;
00246       Ln[p].NLink = 1;
00247       Ln[p].Link[0] = p+1;
00248       if(p==NEdge+NSpline-1) Ln[p].NLink = 0;
00249       SetBkf(p);
00250     }
00251   }
00252   if(VAR_IF_TYPE(SysShape,SYS_MD)){
00253     Shout("Preparing system/calculating forces\n");
00254     OldNrgSys = SumForcesMD();
00255   }
00256   else if(VAR_IF_TYPE(SysShape,SYS_ROD)){
00257     IntMatrix = new Matrice(pNPart(),pNPart());
00258   }
00259   else if(VAR_IF_TYPE(SysShape,SYS_LEAVES)){
00260     IntMatrix = new Matrice(pNPCh(),pNPCh());
00261   }
00262   else if(VAR_IF_TYPE(SysShape,SYS_2D)){
00263     IntMatrix = new Matrice(pNPart(),pNPart());
00264   }
00265   /*for the chemical potential, to avoid the calculation of
00266     large exponential */
00267   NrgPBead = 0.;//2.*OldNrgSys/(double)pNPart();
00268   Shout("Prepared");
00269 }
00270 void Forces::FillMatrix(){
00271   if(!IfFillMatrix) return;
00272   IntMatrix->Clear();
00273   double Inter = pEdge(CLat1)/(double)NEdge;//fabs(pPos(1,0) - pPos(0,0));
00274   SPLINE Weight;
00275   Weight.a0 = Kf.El[2];
00276   Weight.a1 = 0./Inter;
00277   Weight.a2 = Kf.Lap/SQR(Inter);
00278   Weight.a3 = 0./(Inter*SQR(Inter));
00279   Weight.a4 = Kf.SLap/(SQR(Inter)*SQR(Inter));
00280   if(VAR_IF_TYPE(SysShape,SYS_ROD)){
00281     int NDim = 1;
00282     Matrice *CoeffMatrix = new Matrice(Weight,NDim);
00283     CoeffMatrix->Print();
00284     for(int r=0;r<pNPart();r++){
00285       if(Pm[r].Typ != 0){ IntMatrix->Set(r,r,1.);continue;}
00286       if(r >= 2) IntMatrix->Set(r,r-2,CoeffMatrix->Val(2,0));
00287       if(r >= 1) IntMatrix->Set(r,r-1,CoeffMatrix->Val(2,1));
00288       if(r < pNPart()-1) IntMatrix->Set(r,r+1,CoeffMatrix->Val(2,3));
00289       if(r < pNPart()-2) IntMatrix->Set(r,r+2,CoeffMatrix->Val(2,4));
00290       IntMatrix->Set(r,r,CoeffMatrix->Val(2,2));
00291     }
00292     IntMatrix->Invert();
00293     //IntMatrix->Print();
00294     delete CoeffMatrix;
00295   }
00296   else if(VAR_IF_TYPE(SysShape,SYS_LEAVES)){
00297     int NDim = 1;
00298     Matrice *CoeffMatrix = new Matrice(Weight,NDim);
00299     CoeffMatrix->Print();
00300     for(int r=0;r<pNPCh();r++){
00301       if(Pm[r].Typ != 0){ IntMatrix->Set(r,r,1.);continue;}
00302       if(r >= 2) IntMatrix->Set(r,r-2,CoeffMatrix->Val(2,0));
00303       if(r >= 1) IntMatrix->Set(r,r-1,CoeffMatrix->Val(2,1));
00304       if(r < pNPart()-1) IntMatrix->Set(r,r+1,CoeffMatrix->Val(2,3));
00305       if(r < pNPart()-2) IntMatrix->Set(r,r+2,CoeffMatrix->Val(2,4));
00306       IntMatrix->Set(r,r,CoeffMatrix->Val(2,2));
00307     }
00308     //IntMatrix->Invert();
00309     IntMatrix->Print();
00310     delete CoeffMatrix;
00311   }
00312   else if(VAR_IF_TYPE(SysShape,SYS_2D)){
00313     int NDim = 2;
00314     Matrice *CoeffMatrix = new Matrice(Weight,NDim);
00315     CoeffMatrix->Print();
00316     for(int p=0;p<pNPart();p++){
00317       if(Pm[p].Typ != 0){
00318    IntMatrix->Set(p,p,1.);
00319    continue;
00320       }
00321       int pym1 = Ln[p].Link[0];
00322       int pyp1 = Ln[p].Link[1];
00323       int pym2 = Ln[pym1].Link[0];
00324       int pyp2 = Ln[pyp1].Link[1];
00325       int pxm1 = Ln[p].Link[2];
00326       int pxp1 = Ln[p].Link[3];
00327       int pxm2 = Ln[pxm1].Link[2];
00328       int pxp2 = Ln[pxp1].Link[3];
00329       // printf("%d)\n",p);
00330       //printf("%d %d %d %d\n",pym2,pym1,pyp1,pyp2);
00331       // printf("%d %d %d %d\n",pxm2,pxm1,pxp1,pxp2);
00332       if(PeriodicImage[0]){
00333    IntMatrix->Set(p,pxm2,CoeffMatrix->Val(2,0));
00334    IntMatrix->Set(p,pxm1,CoeffMatrix->Val(2,1));
00335    IntMatrix->Set(p,pxp1,CoeffMatrix->Val(2,3));
00336    IntMatrix->Set(p,pxp2,CoeffMatrix->Val(2,4));
00337       }
00338       else{
00339    if(pxm2 == p-2*nEdge[1])
00340      IntMatrix->Set(p,pxm2,CoeffMatrix->Val(2,0));
00341    if(pxm1 == p-nEdge[1])
00342      IntMatrix->Set(p,pxm1,CoeffMatrix->Val(2,1));
00343    if(pxp1 == p+nEdge[1])
00344      IntMatrix->Set(p,pxp1,CoeffMatrix->Val(2,3));
00345    if(pxp2 == p+2*nEdge[1])
00346      IntMatrix->Set(p,pxp2,CoeffMatrix->Val(2,4));
00347       }
00348       IntMatrix->Add(p,p,CoeffMatrix->Val(2,2));
00349       if(PeriodicImage[1]){
00350    IntMatrix->Set(p,pym2,CoeffMatrix->Val(2,0));
00351    IntMatrix->Set(p,pym1,CoeffMatrix->Val(2,1));
00352    IntMatrix->Set(p,pyp1,CoeffMatrix->Val(2,3));
00353    IntMatrix->Set(p,pyp2,CoeffMatrix->Val(2,4));
00354       }
00355       else{
00356    if(pym2 == p-2)
00357      IntMatrix->Set(p,pym2,CoeffMatrix->Val(2,0));
00358    if(pym1 == p-1)
00359      IntMatrix->Set(p,pym1,CoeffMatrix->Val(2,1));
00360    if(pyp1 == p+1)
00361      IntMatrix->Set(p,pyp1,CoeffMatrix->Val(2,3));
00362    if(pyp2 == p+2)
00363      IntMatrix->Set(p,pyp2,CoeffMatrix->Val(2,4));
00364       }
00365     }
00366     IntMatrix->Invert();
00367     //IntMatrix->Print();
00368     delete CoeffMatrix;
00369   }
00370   IfFillMatrix = 0;
00371 }
00372 void Forces::PrepareParallel(int argc,char **argv){
00373 #ifdef OMPI_MPI_H 
00374   Shout("Preparing parallelisation");
00375   MPI_Init(&argc,&argv);
00376   int Rank=0,Size=0;
00377   MPI_Comm_rank(MPI_COMM_WORLD, &Rank);
00378   MPI_Comm_size(MPI_COMM_WORLD, &Size);
00379   int Partition = (int)(argc/(double)Size);
00380   NFile[0] = Partition*Rank;
00381   NFile[1] = Partition*(Rank+1);
00382   if(Rank==Size-1) NFile[1] += argc%Size;
00383   Proc = new SingProc(Size,Rank);
00384 #endif
00385 }
00386 void Forces::AllocMethod(){
00387   Shout("Allocation");
00388   StatFile1 = fopen("StatDyn1.dat","w");
00389   StatFile2 = fopen("StatDyn2.dat","w");
00390   ChooseCalcMode(CalcMode);
00391   ChoosePot(CalcMode);
00392   ChooseThermostat(ThermMode);
00393   double Edge[3] = {pEdge(0),pEdge(1),pEdge(2)};
00394   double Pos[3];
00395   //Pc->PrintCells();
00396   //CheckPairList();
00397   SetDeltat(Deltat);
00398   if(VAR_IF_TYPE(SysShape,SYS_MD)){
00399     Shout("Allocating/md: forces, domain decomposition");
00400     Fm = (FORCES *)calloc(pNAllocP(),sizeof(FORCES));
00401     VAR_ADD_TYPE(SysAlloc,ALL_MD);
00402     VAR_ADD_TYPE(SysAlloc,ALL_FORCES);
00403     Pc = new DomDec(Edge,pNPart(),sqrt(Kf.CutOff2));
00404     for(int p=0;p<pNPart();p++){pPos(p,Pos);Pc->AddPart(p,Pos);}
00405   }
00406   if(VAR_IF_TYPE(SysShape,SYS_ROD)){
00407     Shout("Allocating/rod: forces");
00408     Fm = (FORCES *)calloc(pNAllocP(),sizeof(FORCES));
00409     VAR_ADD_TYPE(SysAlloc,ALL_FORCES);
00410   }
00411   else if(VAR_IF_TYPE(SysShape,SYS_MC)){
00412     Shout("Allocating/mc: domain decomposition old chain positions, old energies for particles and chains, first bead distribution, bias (cumulative probabilities and bead positions)");  
00413     Pc = new DomDec(Edge,pNPart(),sqrt(Kf.CutOff2));
00414     double Pos[3];
00415     for(int p=0;p<pNPart();p++){pPos(p,Pos);Pc->AddPart(p,Pos);}
00416     //Pc->PrintCells();
00417     if(VAR_IF_TYPE(CalcMode,CALC_NVT)){
00418       OldNrgBead = new double[3*pNAllocP()];
00419       if(!OldNrgBead){
00420    printf("Could not allocate OldNrgPm \n");
00421    exit(1);
00422       }
00423     }
00424     OldNrgCh = new double[3*pNAllocC()];
00425     FirstBeadDistr = new double[NBin];
00426     if(!OldNrgCh){
00427       printf("Could not allocate OldNrgCh\n");
00428       exit(1);
00429     }
00430     OldPos = (double **)calloc(pNPCh(),sizeof(double));
00431     if( !OldPos){
00432       printf("Could not allocate OldPos\n");
00433       exit(1);
00434     }
00435     for(int p=0;p<pNPCh();p++){
00436       OldPos[p] = (double *)calloc(3,sizeof(double));
00437     }
00438     CumProbBias = new double[NTrialBias];
00439     BondPosBias = new double*[NTrialBias];
00440     for(int t=0;t<NTrialBias;t++){
00441       BondPosBias[t] = new double[3];
00442     }
00443     VAR_ADD_TYPE(SysAlloc,ALL_MC);
00444     VAR_ADD_TYPE(SysAlloc,ALL_BIAS);
00445     GaussVar = sqrt(pReOverCutOff()/(3.*(pNPCh()-1)))/2.;
00446     GaussVar = sqrt(1./pkSpr());
00447   }
00448   else if(VAR_IF_TYPE(SysShape,SYS_ELECTRO)){
00449     Shout("Allocating/mc: domain decomposition old chain positions, old energies for particles and chains, first bead distribution, bias (cumulative probabilities and bead positions)");  
00450     Pc = new DomDec(Edge,pNPart(),sqrt(Kf.CutOff2));
00451     double Pos[3];
00452     for(int p=0;p<pNPart();p++){pPos(p,Pos);Pc->AddPart(p,Pos);}
00453     //Pc->PrintCells();
00454     if(VAR_IF_TYPE(CalcMode,CALC_NVT)){
00455       OldNrgBead = new double[3*pNAllocP()];
00456       if(!OldNrgBead){
00457    printf("Could not allocate OldNrgPm \n");
00458    exit(1);
00459       }
00460     }
00461     OldNrgCh = new double[3*pNAllocC()];
00462     FirstBeadDistr = new double[NBin];
00463     if(!OldNrgCh){
00464       printf("Could not allocate OldNrgCh\n");
00465       exit(1);
00466     }
00467     OldPos = (double **)calloc(pNPCh(),sizeof(double));
00468     if( !OldPos){
00469       printf("Could not allocate OldPos\n");
00470       exit(1);
00471     }
00472     for(int p=0;p<pNPCh();p++){
00473       OldPos[p] = (double *)calloc(3,sizeof(double));
00474     }
00475     CumProbBias = new double[NTrialBias];
00476     BondPosBias = new double*[NTrialBias];
00477     for(int t=0;t<NTrialBias;t++){
00478       BondPosBias[t] = new double[3];
00479     }
00480     VAR_ADD_TYPE(SysAlloc,ALL_MC);
00481     VAR_ADD_TYPE(SysAlloc,ALL_BIAS);
00482   }
00483   if(VAR_IF_TYPE(SysShape,SYS_1D) || VAR_IF_TYPE(SysShape,SYS_TRIAL)){
00484     Shout("Allocating/splines");  
00485     Pl = (PART *)calloc(NSpline,sizeof(PART));
00486     VAR_ADD_TYPE(SysAlloc,ALL_SPLINE);
00487   }
00488   if(VAR_IF_TYPE(CalcMode,CALC_DENS)){
00489     Shout("Allocating/particle densities, sum of local densities");  
00490     LocDens2 = (double *)calloc(pNPCh()*pNType(),sizeof(double));
00491     LocDens3 = (double *)calloc(pNPCh()*pNType(),sizeof(double));
00492     Dens2 = (double *)calloc(pNAllocP()*pNType(),sizeof(double));
00493     Dens3 = (double *)calloc(pNAllocP()*pNType(),sizeof(double));
00494     if( !Dens2 || !Dens3){
00495       printf("Could not allocate Dens2 Dens3\n");
00496       exit(1);
00497     }
00498     VAR_ADD_TYPE(SysAlloc,ALL_DENS);
00499   }
00500 #ifdef __glut_h__
00501   Cylinder = (GLuint *)calloc(pNNano(),sizeof(GLuint));
00502 #endif  
00503   ChemPotId = log(NChemPotId/pVol());
00504 }
00505 void Forces::InitConst(){
00506   Shout("Init constants");  
00507   NEdge = 80+1;//NInEdge;
00508   Dx = 1./(double)(NEdge-1);
00509   Kf.El[0] = 11.;//2.*NEdge/10.;//Elastic coupling
00510   Kf.El[1] = 11.;//Elastic coupling
00511   Kf.El[2] = 11.;//Elastic coupling
00512   Kf.Lap = 20.*pow(Dx,2.);//bending rigidity
00513   Kf.SLap = 0.;//.015*pow(Dx,2.);//Surface tension
00514   Kf.Ext = 1.;
00515   Kf.LJ = 1.;
00516   Kf.LJMin = .5;
00517   Kf.CutOff2 = 1.;
00518   Kf.Cont = 0.;//.001;
00519   Kf.Elong[0] = 0.1;
00520   Kf.Elong[1] = 0.1;
00521   Kf.Elong[2] = 0.1;
00522   Kf.ForThr = 100.;
00523   IntMax = 100;
00524   IncrDist = 0.01;
00525   IfInterp=FIT_FORTH;
00526   IfFillMatrix=1;
00527   Nano->Rad = .05;
00528   Nano->Height = .3;
00529   ChemPotId = 0.;
00530   ChemPotEx = 0.;
00531   Nano->Pos[0] = 0.;
00532   Nano->Pos[1] = 0.;
00533   Nano->Pos[2] = .5;
00534   BoundCond[0] = 1;BoundCond[1] = 1;
00535   BoundCond[2] = 1;BoundCond[3] = 1;
00536   BoundCond[4] = 1;BoundCond[5] = 1;
00537   PeriodicImage[0] = 1;PeriodicImage[1] = 1;PeriodicImage[2] = 1;
00538   Time = 0.;
00539   NUpdate = 100;
00540   NWrite = 1000;
00541   NBin = 100;
00542   NTrialBias = 10;
00543   //  GaussVar = sqrt(1./pkSpr());
00544   ThermMode = THERM_LANG;
00545   CalcMode = CALC_LJ39;
00546   //  Part2Move = NEdge/2+NEdge;
00547 #ifdef __glut_h__
00548   NSpline = 100;
00549   IfSphere = 0;
00550   IfLine = 1;
00551   IfSpline = 0;
00552   NShow = 1;
00553   IfMovie=0;
00554   Frame = 0;
00555   IfExt = 0;
00556   IfRot = 0;
00557   BeadType = 0;
00558 #endif
00559   IfExit = 0;
00560   IfNano = 0;
00561   DynFlag = 0;
00562   SetNBlock(1);
00563 }
00564 void Forces::Info(){
00565   printf("-----INFO-------\n");
00566   if(VAR_IF_TYPE(SysShape,SYS_MC)){
00567     // Pc->Erase();
00568     // ClearDens();
00569     // for(int p=0;p<pNPart();p++)
00570     //   Pc->AddPart(p,Pm[p].Pos);
00571     // AddDens(0,pNPart());
00572     printf("Energy: Sys %lf Ch  Part \n",OldNrgSys);
00573     // CalcTotNrgCh();
00574     // for(int c=0;c<pNChain();c++)
00575     //   printf("%d %lf %lf %lf\n",c,OldNrgCh[c*3],OldNrgCh[c*3+1],OldNrgCh[c*3+2]);
00576   }
00577   else if(VAR_IF_TYPE(SysShape,SYS_MD)){
00578     for(int p=0;p<pNPart();p++){
00579       if(p == Bead2Move) printf("p      ");
00580       printf("%d) (%lf,%lf,%lf) (%lf %lf %lf) %lf-%lf-%lf\n",p,pPos(p,0),pPos(p,1),pPos(p,2),pVel(p,0),pVel(p,1),pVel(p,2),Fm[p].Dir[2],Fm[p].Dir[2],Fm[p].Dir[2]);
00581     }
00582   }
00583   printf("------------\n");
00584 }
00585 int Forces::ReadConfDinamica(char *InFile){
00586   SysType = 0;
00587   SysShape = 0;
00588   CalcMode = 0;
00589   FILE *FileToRead = fopen(InFile,"r");
00590   if(FileToRead == NULL){
00591     printf("The conf file %s is missing\n",InFile);
00592     return 1;
00593   }
00594   double buff[12];
00595   char SysInit[20];
00596   char *Line = (char *)malloc(256*sizeof(char));
00597   //fgets(Line,256,FileToRead);
00598   int NNano = 0;
00599   for(int k=0;!(fgets(Line,256,FileToRead)==NULL);k++){
00600     if(strstr(Line, "Rigid") == Line) NNano++;
00601   }
00602   SetNNano(NNano);
00603   NNano = 0;
00604   rewind(FileToRead);
00605   for(int k=0;!(fgets(Line,256,FileToRead)==NULL);k++){
00606     //printf("%s",Line);
00607     if(1 == sscanf(Line,"NEdge %lf",buff) )
00608       NEdge = (int)*buff;
00609     else if(1 == sscanf(Line,"SysShape %s",SysInit) ){
00610       if(!strcmp(SysInit,"leaves") )
00611    VAR_ADD_TYPE(SysShape,SYS_LEAVES);
00612       else if(!strcmp(SysInit,"pore") )
00613    VAR_ADD_TYPE(SysShape,SYS_PORE);
00614       else if(!strcmp(SysInit,"1d") )
00615    VAR_ADD_TYPE(SysShape,SYS_1D);
00616       else if(!strcmp(SysInit,"2d") )
00617    VAR_ADD_TYPE(SysShape,SYS_2D);
00618       else if(!strcmp(SysInit,"3d") )
00619    VAR_ADD_TYPE(SysShape,SYS_3D);
00620       else if(!strcmp(SysInit,"rod") )
00621    VAR_ADD_TYPE(SysShape,SYS_ROD);
00622       else if(!strcmp(SysInit,"trial") )
00623    VAR_ADD_TYPE(SysShape,SYS_TRIAL);
00624       else if(!strcmp(SysInit,"rigid") )
00625    VAR_ADD_TYPE(SysShape,SYS_RIGID);
00626       else if(!strcmp(SysInit,"stalk") )
00627    VAR_ADD_TYPE(SysShape,SYS_STALK);
00628       else if(!strcmp(SysInit,"md") )
00629    VAR_ADD_TYPE(SysShape,SYS_MD);
00630       else if(!strcmp(SysInit,"mc") )
00631    VAR_ADD_TYPE(SysShape,SYS_MC); 
00632       else if(!strcmp(SysInit,"electro") ){
00633    VAR_ADD_TYPE(SysShape,SYS_ELECTRO);
00634    //VAR_ADD_TYPE(SysShape,SYS_MC); 
00635       }
00636       else{
00637    printf("system type not recognized\n");
00638    exit(1);
00639       }
00640     }
00641     else if(1 == sscanf(Line,"CalcMode %s",SysInit) ){
00642       if(!strcmp(SysInit,"NVT") )
00643    VAR_ADD_TYPE(CalcMode,CALC_NVT);
00644       else if(!strcmp(SysInit,"NcVT") )
00645    VAR_ADD_TYPE(CalcMode,CALC_NcVT);
00646       else if(!strcmp(SysInit,"mcVT") )
00647    VAR_ADD_TYPE(CalcMode,CALC_mcVT);
00648       else if(!strcmp(SysInit,"mVT") )
00649    VAR_ADD_TYPE(CalcMode,CALC_mVT);
00650       else{
00651    printf("calculation type not recognized\n");
00652    exit(1);
00653       }
00654     }
00655     else if(1 == sscanf(Line,"Thermostat %s",SysInit) ){
00656       if(!strcmp(SysInit,"Langevin") )
00657    ThermMode = THERM_LANG;
00658       else if(!strcmp(SysInit,"Andersen") )
00659    ThermMode = THERM_AND;
00660       else if(!strcmp(SysInit,"Berendsen") )
00661    ThermMode = THERM_BERE;
00662       else if(!strcmp(SysInit,"no") )
00663    ThermMode = THERM_NO;
00664       else{
00665    printf("thermostat not recognized\n");
00666    exit(1);
00667       }
00668     }
00669     else if(1 == sscanf(Line,"PotentialMode %s",SysInit) ){
00670       if(!strcmp(SysInit,"Pair") )
00671    VAR_ADD_TYPE(CalcMode,CALC_PAIR);
00672       else if(!strcmp(SysInit,"DensFunc") )
00673    VAR_ADD_TYPE(CalcMode,CALC_DENS);
00674       else if(!strcmp(SysInit,"DensFuncCh") )
00675    VAR_ADD_TYPE(CalcMode,CALC_DENS);
00676     }
00677     else if(1 == sscanf(Line,"Potential %s",SysInit) ){
00678       if(!strcmp(SysInit,"LJ") )
00679    VAR_ADD_TYPE(CalcMode,CALC_LJ);
00680       else if(!strcmp(SysInit,"LJ39") )
00681    VAR_ADD_TYPE(CalcMode,CALC_LJ39);
00682       else if(!strcmp(SysInit,"Harmonic") )
00683    VAR_ADD_TYPE(CalcMode,CALC_HARM);
00684       else if(!strcmp(SysInit,"Step") )
00685    VAR_ADD_TYPE(CalcMode,CALC_STEP);
00686       else if(!strcmp(SysInit,"Electro") )
00687    VAR_ADD_TYPE(CalcMode,CALC_ELECTRO);
00688       else{
00689    printf("interaction potential not recognized\n");
00690    exit(1);
00691       }
00692     }
00693     else if(1 == sscanf(Line,"IfInterp %lf",buff) )
00694       IfInterp = (int)*buff;
00695     else if(1 == sscanf(Line,"Lap %lf",buff) )
00696       Kf.Lap = *buff;
00697     else if(1 == sscanf(Line,"SLap %lf",buff) )
00698       Kf.SLap = *buff;
00699     else if(1 == sscanf(Line,"Ext %lf",buff) )
00700       Kf.Ext = *buff;
00701     else if(1 == sscanf(Line,"LJ %lf",buff) )
00702       Kf.LJ = *buff;
00703     else if(1 == sscanf(Line,"LJMin %lf",buff) )
00704       Kf.LJMin = *buff;
00705     else if(1 == sscanf(Line,"kSpr %lf",buff) )
00706       SetkSpr(*buff);
00707     else if(1 == sscanf(Line,"SprRest %lf",buff) )
00708       SetSprRest(*buff);
00709     else if(1 == sscanf(Line,"kBen %lf",buff) )
00710       SetkBen(*buff);
00711     else if(1 == sscanf(Line,"SimLimit %lf",buff) )
00712       SimLimit = (int)*buff;
00713     else if(1 == sscanf(Line,"CutOff %lf",buff) )
00714       Kf.CutOff2 = SQR(*buff);
00715     else if(1 == sscanf(Line,"Cont %lf",buff) )
00716       Kf.Cont = *buff;
00717     else if(1 == sscanf(Line,"NWrite %lf",buff) )
00718       NWrite = (int)*buff;
00719     else if(1 == sscanf(Line,"NBin %lf",buff) )
00720       NBin = (int)*buff;
00721     else if(1 == sscanf(Line,"NGrid %lf",buff) )
00722       NGrid = (int)*buff;
00723     else if(1 == sscanf(Line,"NUpdate %lf",buff) )
00724       NUpdate = (int)*buff;
00725     else if(strstr(Line, "Rigid") == Line){
00726       NanoString(Line,NNano++);
00727     }
00728 #ifdef __glut_h__
00729     else if(1 == sscanf(Line,"IfMovie %lf",buff) )
00730       IfMovie = (int)*buff;
00731     else if(1 == sscanf(Line,"IfLine %lf",buff) )
00732       IfLine = (int)*buff;
00733     else if(1 == sscanf(Line,"NSpline %lf",buff) )
00734       NSpline = *buff;
00735     else if(1 == sscanf(Line,"IfSphere %lf",buff) )
00736       IfSphere = (int)*buff;
00737 #endif
00738     else if(3 == sscanf(Line,"Edge %lf %lf %lf",buff,buff+1,buff+2) ){
00739       SetEdge(buff[0],0);
00740       SetEdge(buff[1],1);
00741       SetEdge(buff[2],2);
00742     }
00743     else if(3 == sscanf(Line,"El %lf %lf %lf",buff,buff+1,buff+2) ){
00744       Kf.El[0] = *buff;
00745       Kf.El[1] = *(buff+1);
00746       Kf.El[2] = *(buff+2);
00747     }
00748     else if(3 == sscanf(Line,"Elong %lf %lf %lf",buff,buff+1,buff+2) ){
00749       Kf.Elong[0] = *buff;
00750       Kf.Elong[1] = *(buff+1);
00751       Kf.Elong[2] = *(buff+2);
00752     }
00753     else if(6 == sscanf(Line,"Boundary %lf %lf %lf %lf %lf %lf",buff,buff+1,buff+2,buff+3,buff+4,buff+5)){
00754       BoundCond[0] = (int)buff[0];
00755       BoundCond[1] = (int)buff[1];
00756       BoundCond[2] = (int)buff[2];
00757       BoundCond[3] = (int)buff[3];
00758       BoundCond[4] = (int)buff[4];
00759       BoundCond[5] = (int)buff[5];
00760     }
00761     else if(3 == sscanf(Line,"Periodic  %lf %lf %lf",buff,buff+1,buff+2)){
00762       PeriodicImage[0] = (int)*buff;
00763       PeriodicImage[1] = (int)*(buff+1);
00764       PeriodicImage[2] = (int)*(buff+2);
00765     }
00766     else if(1 == sscanf(Line,"Deltat %lf",buff) )
00767       Deltat = *buff;
00768     else if(1 == sscanf(Line,"Temp %lf",buff) )
00769       SetTemp(buff[0]);
00770     else if(1 == sscanf(Line,"NChemPotId %lf",buff) )
00771       NChemPotId = *buff;
00772     else if(1 == sscanf(Line,"NTrialBias %lf",buff) ){
00773       NTrialBias =(int) *buff;
00774     }
00775     else if(1 == sscanf(Line,"ChemPotEx %lf",buff) )
00776       ChemPotEx = *buff;
00777     else if(1 == sscanf(Line,"IfConfBias %lf",buff) ){
00778       if((int)*buff == 1)
00779       VAR_ADD_TYPE(CalcMode,CALC_CONF_BIAS);
00780     }
00781     else if(1 == sscanf(Line,"IfBilBias %lf",buff) ){
00782       if((int)*buff == 1){
00783       VAR_ADD_TYPE(CalcMode,CALC_BIL_BIAS);
00784    StudySys();
00785       }
00786     }
00787     else if(1 == sscanf(Line,"IfSphBias %lf",buff) ){
00788       if((int)*buff == 1)
00789       VAR_ADD_TYPE(CalcMode,CALC_SPH_BIAS);
00790     }
00791     else if(1 == sscanf(Line,"Viscosity %lf",buff) )
00792       Viscosity = *buff;
00793     else if(1 == sscanf(Line,"TNSlab %lf",buff) )
00794       Tens.NSlab = (int)*buff;
00795     else if(1 == sscanf(Line,"TNComp %lf",buff) )
00796       Tens.NComp = (int)*buff;
00797     else if(1 == sscanf(Line,"TCalcMode %s",SysInit) ){
00798       if(!strcmp(SysInit,"2d") ){
00799    VAR_ADD_TYPE(Tens.CalcMode,CALC_2d);
00800    Tens.NDim = 2;
00801       }
00802       else if(!strcmp(SysInit,"3d") ){
00803    VAR_ADD_TYPE(Tens.CalcMode,CALC_3d);
00804    Tens.NDim = 3;
00805       }
00806       else{
00807    printf("Pressure summation not recognized\n");
00808    exit(1);
00809       }
00810     }
00811   }
00812   //for(int n=0;n<pNNano();n++) for(int d=0;d<3;d++) Nano[n].Pos[d] *= pEdge(d);
00813   ChemPotId = log(NChemPotId/pVol());
00814   printf("Sys] NEdge %d SysShape %d %s Interp %d  \n",NEdge,SysShape,SysInit,IfInterp);
00815   printf("Forces] Lap %lf SLap %lf Ext %lf LJ %lf Cont %lf El %lf %lf %lf Elong %lf %lf %lf\n",Kf.Lap,Kf.SLap,Kf.Ext,Kf.LJ,Kf.Cont,Kf.El[0],Kf.El[1],Kf.El[2],Kf.Elong[0],Kf.Elong[1],Kf.Elong[2]);
00816   printf("External] Center %lf %lf %lf Rad %lf Hei %lf\n",Nano->Pos[0],Nano->Pos[1],Nano->Pos[2],Nano->Rad,Nano->Height);
00817   fclose(FileToRead);
00818   return 0;
00819 }
00820 int Forces::ReSetNPart(int NewNPart){
00821   int OldNPart = pNPart();
00822   //if the number of allocated particle is less then the new particles returns
00823   Block[0].NPart = NewNPart;
00824   if(SetNPart(NewNPart)) return 1;
00825   if(VAR_IF_TYPE(SysAlloc,ALL_MC)){
00826     double *Dens2T = (double *)calloc(pNPart()*pNType(),sizeof(double));
00827     double *Dens3T = (double *)calloc(pNPart()*pNType(),sizeof(double));
00828     for(int p=0;p<OldNPart*pNType();p++){
00829       Dens2T[p] = Dens2[p];
00830       Dens3T[p] = Dens3[p];
00831     }
00832     //memcpy(Dens2,Dens2T,OldNPart*pNType());
00833     //memcpy(Dens3,Dens3T,OldNPart*pNType());
00834     free(Dens2);
00835     free(Dens3);
00836     Dens2 = (double *)calloc(pNAllocP()*pNType(),sizeof(double));
00837     Dens3 = (double *)calloc(pNAllocP()*pNType(),sizeof(double));
00838     // memcpy(Dens2T,Dens2,OldNPart*pNType());
00839     // memcpy(Dens3T,Dens3,OldNPart*pNType());
00840     for(int p=0;p<OldNPart*pNType();p++){
00841       Dens2[p] = Dens2T[p];
00842       Dens3[p] = Dens3T[p];
00843     }
00844     free(Dens2T);
00845     free(Dens3T);
00846     double *TempNrgPart = new double[OldNPart];
00847     for(int p=0;p<OldNPart;p++){
00848       TempNrgPart[p] = OldNrgBead[p];
00849     }
00850     delete[] OldNrgBead;
00851     OldNrgBead = new double[pNAllocP()];
00852     for(int p=0;p<OldNPart;p++){
00853       OldNrgBead[p] = TempNrgPart[p];
00854     }
00855     delete []TempNrgPart;
00856   }
00857   if(VAR_IF_TYPE(SysAlloc,ALL_MD)){
00858     FORCES *TmpFm = new FORCES [OldNPart];
00859     for(int p=0;p<OldNPart;p++){
00860       for(int d=0;d<3;d++){
00861    TmpFm[p].Dir[d] = Fm[p].Dir[d];
00862    TmpFm[p].Ext[d] = Fm[p].Ext[d];
00863       }
00864     }
00865     free(Fm);
00866     Fm = (FORCES *)calloc(pNAllocP(),sizeof(FORCES));
00867     for(int p=0;p<OldNPart;p++){
00868       for(int d=0;d<3;d++){
00869    Fm[p].Dir[d] = TmpFm[p].Dir[d];
00870    Fm[p].Ext[d] = TmpFm[p].Ext[d];
00871       }
00872     }
00873     free(TmpFm);
00874   }
00875   return 0;
00876 }
00877 int Forces::ReSetNChain(int NewNChain){
00878   int OldNChain = pNChain();
00879   Block[0].NChain = NewNChain;
00880   if(SetNChain(NewNChain))return 1;
00881   if(VAR_IF_TYPE(SysAlloc,ALL_MC)){
00882     double *TempNrgCh = new double[OldNChain];
00883     for(int c=0;c<3*OldNChain;c++){
00884       TempNrgCh[c] = OldNrgCh[c];
00885     }
00886     delete[] OldNrgCh;
00887     OldNrgCh = new double[pNAllocC()];
00888     for(int c=0;c<3*OldNChain;c++){
00889       OldNrgCh[c] = TempNrgCh[c];
00890     }
00891     delete []TempNrgCh;
00892   }
00893   if(VAR_IF_TYPE(SysAlloc,ALL_MD)){
00894   }
00895   return 0;
00896 }
00897 void Forces::ReSetNPCh(int NewNPCh){
00898   Block[0].NPCh = NewNPCh;
00899   SetNPCh(NewNPCh);
00900 }
00901 void Forces::ReOpen(char *FName,int Bf){
00902   double Edge[3] = {pEdge(0),pEdge(1),pEdge(2)};
00903   Pc->Erase();
00904   delete Pc;
00905   Open(FName,Bf);
00906   Pc = new DomDec(Edge,pNPart(),sqrt(Kf.CutOff2));
00907   double Pos[3];
00908   for(int p=0;p<pNPart();p++){pPos(p,Pos);Pc->AddPart(p,Pos);}
00909   PrepareSys();
00910 }