Allink  v0.1
VarData.cpp
00001 /***********************************************************************
00002 VarData: This  Program reads and writes a specific file format 
00003 storing all the information relative to a set of equal structure
00004 polymers to the CHAIN, PART and GENERAL structures. It provides 
00005 two different ways to backfold the coordinates, a fanction that 
00006 creates an initial system with different option and some function
00007 for the data analisys. The first calculate the distribution of the
00008 monomer in the box, the second the distribution of the bonds.
00009 Copyright (C) 2008 by Giovanni Marelli <sabeiro@virgilio.it>
00010 
00011 
00012 This program is free software; you can redistribute it and/or modify
00013 it under the terms of the GNU General Public License as published by
00014 the Free Software Foundation; either version 2 of the License, or
00015 (at your option) any later version.
00016 
00017 This program is distributed in the hope that it will be useful,
00018 but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 GNU General Public License for more details.
00021 
00022 You should have received a copy of the GNU General Public License
00023 along with this program; if not, write to the Free Software
00024 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 ***********************************************************************/
00026 #include "../include/VarData.h"
00027 #include <stdarg.h>
00028 
00029 void VarData::VarMessage(const char * s, ...)
00030 {
00031 #ifdef VAR_DEBUG
00032   va_list args;
00033   va_start(args, s);
00034   fprintf(stderr,"VarData] ");
00035   vfprintf(stderr, s, args);
00036   fprintf(stderr, "\n");
00037   va_end(args);
00038 #else
00039   return;
00040 #endif
00041 }
00042 VarData::VarData(){
00043   VarMessage("Constructing");
00044   Mat = new Matematica();
00045   MInt = new MatInt(3,3);
00046   Gen = (GENERAL *) calloc(1,sizeof(GENERAL));
00047   Nano = (NANO *) calloc(1,sizeof(NANO));
00048   Soft = (SOFT *) calloc(1,sizeof(SOFT));
00049   Gen->NBlock = 1;
00050   Block = (BLOCK *)calloc(Gen->NBlock,sizeof(BLOCK));
00051   if( !Gen | !Mat ){printf("Il costruttore non alloca\n");return;}
00052   sprintf(cWhat2Draw,"part");
00053   CNorm = 2;
00054   CLat1 = (CNorm+1)%3;
00055   CLat2 = (CNorm+2)%3;
00056   SetNPCh(32); //# beads
00057   for(int d=0;d<3;d++) ShiftPos[d] = 0.;
00058   for(int d=0;d<3;d++) ScaleF[d] = 1.;
00059   Gen->NLink = 0;//max # of links
00060   NAddChain = 0;//added homopolymer chains
00061   NAddChol = 0;
00062   NSolvent = 0;
00063   NStuffing = 0;
00064   Gen->Deltat = 0.01;
00065   Gen->chiN = 30.;
00066   Gen->kappaN = 100.;
00067   Gen->rho = 50.;
00068   Gen->vBB = -0.1;
00069   Gen->NChain = 1;
00070   Gen->Temp = 1.;
00071   Gen->Edge[0] = 16.;
00072   Gen->Edge[1] = 16.;
00073   Gen->Edge[2] = 32.;
00074   Gen->NBlock = 1;
00075   Gen->WFuncStraight2 = .9;
00076   Gen->WFuncStraight3 = 1.0;
00077   SysType = 0;
00078   SysFormat = 0;
00079   SysCreate = 0;
00080   VAR_ADD_TYPE(SysCreate,VAR_ABSORBING);
00081   NPartNearSphere=0;
00082   NChType = CHAIN_EVERY;
00083   NPType = BEAD_EVERY;
00084   NEdge = 200;
00085   IfNormalize = 0;
00086   IfPlotMem = 0;
00087   Gen->NNano = 1;
00088   NSoft = 0;
00089   Pm = NULL;Ch = NULL;
00090   Point2Shape(SHAPE_NONE);
00091 }
00092 VarData::~VarData(){
00093   VarMessage("Destroying");
00094   delete Mat;
00095   free(Gen);
00096   free(Nano);
00097   free(Block);
00098   free(Soft);
00099   free(Ch);
00100   for(int p=0;p<Gen->NPart;p++)
00101     free(Ln[p].Link);
00102   free(Pm);
00103   free(Ln);
00104 }
00105 bool VarData::Open(char *InFile,int Bf){
00106   VarMessage("Open");
00107   Gen->NType = 0;
00108   for(int d=0;d<3;d++)
00109     Gen->Edge[d]=0.;
00110   VAR_REM_TYPE(SysType,VAR_EDGE);
00111   // VAR_REM_TYPE(SysType,VAR_CHAIN_DEF);
00112   FILE *FileToRead;
00113   if((FileToRead = fopen(InFile,"r"))==0){
00114     printf("The file is missing %s \n",InFile);
00115     return 1;
00116   }
00117   ReadHeader(FileToRead);
00118   if( !VAR_IF_TYPE(SysType,VAR_OPEN_TRUST) ) 
00119     if(ReadPassThru(FileToRead)) return 1;
00120   ReadPart(FileToRead);
00121   //if(AllocPart()) return 1;
00122   ShiftRef(Bf);
00123   fclose(FileToRead);
00124   return 0;
00125 }
00126 bool VarData::OpenRisk(char *InFile,int Bf){
00127   FILE *FileToRead;  
00128   if((FileToRead = fopen(InFile,"r"))==0){
00129     printf("The file is missing %s \n",InFile);
00130     return 1;
00131   }
00132   ReadHeader(FileToRead);
00133   ReadPart(FileToRead);
00134   ShiftRef(Bf);
00135   fclose(FileToRead);
00136   return 0;
00137 }
00138 Properties Properties::operator+ (const Properties& Pr) const{
00139   Properties Result;
00140   Result.RePhob = (this->RePhob + Pr.RePhob);
00141   Result.RePhil = (this->RePhil + Pr.RePhil);
00142   Result.VolPhob = (this->VolPhob + Pr.VolPhob);
00143   Result.VolPhil = (this->VolPhil + Pr.VolPhil);
00144   Result.FactPhob = (this->FactPhob + Pr.FactPhob);
00145   Result.FactPhil = (this->FactPhil + Pr.FactPhil);
00146   Result.GyrPhob = (this->GyrPhob + Pr.GyrPhob);
00147   Result.GyrPhil = (this->GyrPhil + Pr.GyrPhil);
00148   Result.ChDiff = (this->ChDiff + Pr.ChDiff);
00149   return Result;
00150 }
00151 Properties Properties::operator* (const double& c) const{
00152   Properties Result;
00153   Result.RePhob = (this->RePhob * c);
00154   Result.RePhil = (this->RePhil * c);
00155   Result.VolPhob = (this->VolPhob * c);
00156   Result.VolPhil = (this->VolPhil * c);
00157   Result.FactPhob = (this->FactPhob * c);
00158   Result.FactPhil = (this->FactPhil * c);
00159   Result.GyrPhob = (this->GyrPhob * c);
00160   Result.GyrPhil = (this->GyrPhil * c);
00161   Result.ChDiff = (this->ChDiff * c);
00162   return Result;
00163 }
00164 void VarData::SysInfo(char *cSystem){
00165   sprintf(cSystem,"# NPart %d NType %d NChain %d NPCh %d Asymmetry %d Edge: [%.3g %.3g %.3g] rho %.0f chiN %.0f kappaN %.0f Nano %d (# %d) (%lf %lf %lf) NBlock %d",Gen->NPart,Gen->NType,Gen->NChain,Gen->NPCh,Block[0].Asym,Gen->Edge[0],Gen->Edge[1],Gen->Edge[2],Gen->rho,Gen->chiN,Gen->kappaN,Nano->Shape,Gen->NNano,Nano->Rad,Nano->Hamaker,Nano->Height,Gen->NBlock);
00166 }
00167 void VarData::SysDef(char *cSystem){
00168   sprintf(cSystem,"# SysType) Edge %d Sys (Txvl %d Xvt %d) \n",
00169      VAR_IF_TYPE(SysType,VAR_EDGE),SysFormat==VAR_SYS_TXVL,SysFormat==VAR_SYS_XVT);
00170 }
00171 void VarData::SetCoeff(){
00172   double v2[6];
00173   double v3[10];
00174   v2[0] = -2. * (Gen->kappaN+3.)/Gen->rho;//00
00175   v2[3] = Gen->vBB;//11
00176   v2[1] = Gen->chiN/Gen->rho + .5*(v2[0]+v2[2]);//01
00177   v2[2] = v2[1];//02
00178   v2[4] = v2[3];//12
00179   if( 1 == 1){//Hydrophobic
00180     v2[2] = 1.*v2[0];//02
00181     v2[4] = v2[1];//12
00182   }
00183   v2[5] = 0.;//22
00184   v3[0] = 1.5 * (Gen->kappaN+2.)/QUAD(Gen->rho);//000
00185   v3[1] = v3[0];//001
00186   v3[2] = v3[0];//002
00187   v3[3] = v3[0];//011
00188   v3[4] = v3[0];//012
00189   v3[5] = v3[0];//022
00190   v3[6] = 0.;//111
00191   v3[7] = v3[0];//112
00192   v3[8] = v3[0];//122
00193   v3[9] = 0.;//222
00194   SetCoeff(v2,v3);
00195 }
00196 void VarData::SetCoeff(double *v2,double *v3){
00197   MInt->SetCoeff(v2[0],0,0);
00198   MInt->SetCoeff(v2[1],0,1);
00199   MInt->SetCoeff(v2[2],0,2);
00200   MInt->SetCoeff(v2[3],1,1);
00201   MInt->SetCoeff(v2[4],1,2);
00202   MInt->SetCoeff(v2[5],2,2);
00203   MInt->SetCoeff(v3[0],0,0,0);
00204   MInt->SetCoeff(v3[1],0,0,1);
00205   MInt->SetCoeff(v3[2],0,0,2);
00206   MInt->SetCoeff(v3[3],0,1,1);
00207   MInt->SetCoeff(v3[4],0,1,2);
00208   MInt->SetCoeff(v3[5],0,2,2);
00209   MInt->SetCoeff(v3[6],1,1,1);
00210   MInt->SetCoeff(v3[7],1,1,2);
00211   MInt->SetCoeff(v3[8],1,2,2);
00212   MInt->SetCoeff(v3[9],2,2,2);
00213 }
00214 MatInt::MatInt(int NTypeExt,int NOrdExt){
00215   NOrd = NOrdExt;
00216   NType = NTypeExt;
00217   if(NOrd > 1){
00218     int NMax = IntType(NType-1,NType-1)+1;
00219     IntMatr2 = new double[NMax];
00220   }
00221   if(NOrd > 2){
00222     int NMax = IntType(NType-1,NType-1,NType-1)+1;
00223     IntMatr3 = new double[NMax];
00224   }
00225 }
00226 void MatInt::FillEntries(double *Matr,int Ord){
00227   if(Ord == 2){
00228     for(int t=0;t<NType;t++){
00229       for(int tt=t;tt<NType;tt++){
00230    int ttt = IntType(t,tt);
00231    IntMatr2[ttt] = Matr[ttt];
00232       }
00233     }
00234   }
00235   if(Ord == 3){
00236     for(int t=0;t<NType;t++){
00237       for(int tt=t;tt<NType;tt++){
00238    for(int ttt=tt;ttt<NType;ttt++){
00239      int tf = IntType(t,tt,ttt);
00240      IntMatr3[tf] = Matr[tf];
00241    }
00242       }
00243     }
00244   }
00245 }
00246 double MatInt::Coeff(int t1,int t2){
00247   return IntMatr2[IntType(t1,t2)];
00248 }
00249 double MatInt::Coeff(int t1,int t2,int t3){
00250   return IntMatr3[IntType(t1,t2,t3)];
00251 }
00252 void MatInt::SetCoeff(double Co,int t1,int t2){
00253   IntMatr2[NType*t1+t2] = Co;
00254   IntMatr2[NType*t2+t1] = Co;
00255   //IntMatr2[IntType(t1,t2)] = Co;
00256 }
00257 void MatInt::SetCoeff(double Co,int t1,int t2,int t3){
00258   IntMatr3[(t1*NType+t2)*NType+t3] = Co;
00259   IntMatr3[(t1*NType+t3)*NType+t2] = Co;
00260   IntMatr3[(t2*NType+t1)*NType+t3] = Co;
00261   IntMatr3[(t2*NType+t3)*NType+t1] = Co;
00262   IntMatr3[(t3*NType+t2)*NType+t1] = Co;
00263   IntMatr3[(t3*NType+t1)*NType+t2] = Co;
00264   //  IntMatr3[IntType(t1,t2,t3)] = Co;
00265 }
00266 int MatInt::IntType(int t1,int t2){
00267   return NType*t1+t2;
00268   return NType*MIN(t1,t2)+MAX(t1,t2);
00269   int t = 0;
00270   for(int ta=0;ta<MIN(t1,t2);ta++){
00271     for(int tb=ta;tb<MAX(t1,t2);tb++){
00272       t++;
00273     }
00274   }
00275   return t;
00276 }
00277 int MatInt::IntType(int t1,int t2,int t3){
00278   return (t1*NType+t2)*NType+t3;
00279   int tTemp = 0;
00280   int tSort[3] = {t1,t2,t3};
00281   for(int d=1;d<3;d++){
00282     if(tSort[d] < tSort[d-1]){
00283       tTemp = tSort[d];
00284       tSort[d] = tSort[d-1];
00285       tSort[d-1] = tTemp;
00286     }
00287   }
00288   return (NType*tSort[0]+tSort[1])*NType+tSort[2];
00289 }
00290 void MatInt::Print(){
00291   printf(" |_");
00292   for(int tc=0;tc<NType;tc++)
00293     printf("_____%d_____",tc);
00294   printf("|\n");
00295   for(int tr=0;tr<NType;tr++){
00296     printf("%d| ",tr);
00297     for(int tc=0;tc<NType;tc++){
00298       printf("%lf ",Coeff(tr,tc));
00299     }
00300     printf("|\n");
00301   }
00302   printf("\n");
00303   for(int t=0;t<NType;t++){
00304     for(int tt=t;tt<NType;tt++){
00305       for(int ttt=tt;ttt<NType;ttt++){
00306    int tf = IntType(t,tt,ttt);
00307    printf("%d%d%d) %lf\n",t,tt,ttt,Coeff(t,tt,ttt));
00308       }
00309     }
00310   }
00311 }
00312 void MatInt::Rescale(double SFactor,int Order){
00313   if(Order == 2){
00314     for(int t1=0;t1<NType;t1++)
00315       for(int t2=0;t2<NType;t2++)
00316    IntMatr2[(t1*NType+t2)] *= SFactor;
00317   }
00318   if(Order == 3){
00319     for(int t1=0;t1<NType;t1++)
00320       for(int t2=0;t2<NType;t2++)
00321    for(int t3=0;t3<NType;t3++)
00322      IntMatr3[(t1*NType+t2)*NType+t3] *= SFactor;
00323   }
00324 }
00325 //memchr in wich position a caracter is
00326 //memcmp how distant are to pointers
00327 //memcpy copy n bytes from the second pointer to the first 
00328 //memmove the same but uses a buffer and the memory can overlap
00329 //memset set the value of the n character in a string
00330 //strcat append a value to a string
00331 //strchr says to a pointer where is a character
00332 //strcmp compare two strings
00333 //strcoll compare two strings avoiding null-character
00334 //strcopy copy two strings
00335 //strcspn find the position in wich one char of the second string appear in the first
00336 //strncat append n char to the string
00337 //strncmp compare n char from the begining