Allink  v0.1
VarDataComm.cpp
00001 #include <VarData.h>
00002 int VarData::SetNPart(int NewNPart){
00003   if(Gen->NPart == NewNPart) return 1;
00004   int OldNPart = pNPart();
00005   Gen->NPart = NewNPart;
00006   if( !VAR_IF_TYPE(SysType,VAR_PART_ALLOCATED) ){
00007     Gen->NAllocP = Gen->NPart;
00008     if(pNPCh() > 0 && pNPCh() < 200 ) Gen->NAllocP += 2000*Gen->NPCh;
00009     else Gen->NAllocP += 2000;
00010     Gen->NAllocP += 2000;
00011     Pm = (PART *)calloc(Gen->NAllocP,sizeof(PART));
00012     if(Pm == NULL){
00013       printf("Couldn't alloc the particles\n");
00014       exit(1);
00015     }
00016     VAR_ADD_TYPE(SysType,VAR_PART_ALLOCATED);
00017     if(pNLink() > 0){
00018       AllocLinks(NewNPart);
00019     }
00020   }
00021   if(Gen->NAllocP < NewNPart){
00022     Gen->NAllocP = NewNPart;
00023     if(pNPCh() > 0 && pNPCh() < 200 ) Gen->NAllocP += 2000*Gen->NPCh;
00024     else Gen->NAllocP += 2000;
00025     PART *Pn = (PART *)calloc(Gen->NPart,sizeof(PART));
00026     Copy(Pn,Pm,OldNPart);
00027     Pm = (PART *)realloc(Pm,Gen->NAllocP*sizeof(PART));
00028     if(Pm == NULL){
00029       printf("Couldn't realloc the particles\n");
00030       exit(1);
00031     }
00032     if(pNLink() > 0){
00033       AllocLinks(NewNPart);
00034     }
00035     Copy(Pm,Pn,OldNPart);
00036     free(Pn);
00037   }
00038   else return 1;
00039   return 0;
00040 }
00041 int VarData::SetNLink(int NewNLink){
00042   if(Gen->NLink == NewNLink) return 1;
00043   Gen->NLink = NewNLink;
00044   return 0;
00045 }
00046 int VarData::AllocLinks(int NewNPart){
00047   if( !VAR_IF_TYPE(SysType,VAR_LINK_ALLOCATED) ){
00048     Ln = (LINKS *)calloc(Gen->NAllocP,sizeof(LINKS));
00049     if(Ln == NULL){
00050       printf("Couldn't alloc the links\n");
00051       exit(1);
00052     }
00053     for(int p=0;p<Gen->NAllocP;p++){
00054       Ln[p].Link = (int *)calloc(Gen->NLink,sizeof(int));
00055       if(Ln[p].Link == NULL){
00056    printf("Couldn't alloc the links\n");
00057    exit(1);
00058       }
00059     }
00060     VAR_ADD_TYPE(SysType,VAR_LINK_ALLOCATED);
00061   }
00062   else if(Gen->NAllocP < NewNPart){
00063     Ln = (LINKS *)realloc(Ln,Gen->NAllocP*sizeof(LINKS));
00064     if(Ln == NULL){
00065       printf("Couldn't realloc the links\n");
00066       exit(1);
00067     }
00068     for(int p=0;p<Gen->NAllocP;p++){
00069       Ln[p].Link = (int *)realloc(Ln[p].Link,Gen->NLink*sizeof(int));
00070       if(Ln[p].Link == NULL){
00071    printf("Couldn't realloc the links\n");
00072    exit(1);
00073       }
00074     }
00075   }
00076   return 0;
00077 }
00078 int VarData::SetNChain(int NewNChain){
00079   int OldNChain = pNChain();
00080   Gen->NChain = NewNChain;
00081   if( !VAR_IF_TYPE(SysType,VAR_CH_ALLOCATED) ){
00082     Gen->NAllocC = Gen->NChain+200;
00083     Ch = (CHAIN *)calloc(Gen->NAllocC,sizeof(CHAIN));
00084     VAR_ADD_TYPE(SysType,VAR_CH_ALLOCATED);
00085   }
00086   if(Gen->NAllocC < NewNChain){
00087     Gen->NAllocC = NewNChain + 200;
00088     CHAIN *Cn = (CHAIN *)calloc(Gen->NChain,sizeof(CHAIN));
00089     Copy(Cn,Ch,OldNChain);
00090     Ch = (CHAIN *)realloc(Ch,Gen->NAllocC*sizeof(CHAIN));
00091     if(Ch == NULL){
00092       printf("Couldn't realloc the chains\n");
00093       exit(1);
00094     }
00095     Copy(Ch,Cn,OldNChain);
00096     free(Cn);
00097   }
00098   else return 1;
00099   return 0;
00100 }
00101 //obsolete?
00102 void VarData::AllocPart(){
00103   if(Gen->NPart == 0){
00104     printf("No particles or no particle tag: {} \n");
00105     return ;
00106   }
00107   if( !VAR_IF_TYPE(SysType,VAR_PART_ALLOCATED) ){
00108     Gen->NAllocC = Gen->NChain+100;
00109     Gen->NAllocP = Gen->NPart+100*Gen->NPCh;
00110     Pm = (PART *)calloc(Gen->NAllocP,sizeof(PART));
00111     Ln = (LINKS *)calloc(Gen->NAllocP,sizeof(LINKS));
00112     if(Ln == NULL || Pm == NULL){
00113       return ;
00114     }
00115     for(int p=0;p<Gen->NAllocP;p++){
00116       Ln[p].Link = (int *)calloc(Gen->NLink,sizeof(int));
00117     }
00118     Ch = (CHAIN *)calloc(Gen->NAllocC,sizeof(CHAIN));
00119     VAR_ADD_TYPE(SysType,VAR_PART_ALLOCATED);
00120     VAR_ADD_TYPE(SysType,VAR_CH_ALLOCATED);
00121   }
00122   //FIXME: doesn't realloc
00123   else {
00124     //free(Ch);
00125     //for(int p=0;p<Gen->NPart;p++)  free(Pm[p].Link);
00126     //free(Pm);
00127     // Pm = (PART *)realloc(Pm,Gen->NPart*sizeof(PART));
00128     //Pm = (PART *)calloc(Gen->NPart,sizeof(PART));
00129     // for(int p=0;p<Gen->NPart;p++){
00130     //   Pm[p].Link = (int *)realloc(Pm[p].Link,Gen->NLink*sizeof(int));
00131     //   //Pm[p].Link = (int *)calloc(Gen->NLink,sizeof(int));
00132     // }
00133     // Ch = (CHAIN *)realloc(Ch,Gen->NChain*sizeof(CHAIN));
00134     //Ch = (CHAIN *)calloc(Gen->NChain,sizeof(CHAIN));
00135   }
00136   if(!Pm){ printf("Non alloca Pm\n");return ;}
00137   if(!Ch){ printf("Non alloca Ch\n");return ;}
00138 }
00139 void VarData::Copy(PART *P1,PART *P2,int NPartOld){
00140   for(int p=0;p<pNPart();p++){
00141     if(p >= NPartOld) break;
00142     for(int d=0;d<3;d++){
00143       P1[p].Pos[d] = P2[p].Pos[d];
00144       P1[p].Vel[d] = P2[p].Vel[d];
00145     }
00146     P1[p].Typ = P2[p].Typ;
00147   }
00148 }
00149 void VarData::Copy(CHAIN *C1,CHAIN *C2,int NChainOld){
00150   for(int c=0;c<pNChain();c++){
00151     if(c >= NChainOld) break;
00152     for(int d=0;d<3;d++){
00153       C1[c].Pos[d] = C2[c].Pos[d];
00154       C1[c].Vel[d] = C2[c].Vel[d];
00155     }
00156     C1[c].Type = C2[c].Type;
00157   }
00158 }
00159 void VarData::SetNPCh(int NewNPCh){
00160   //blocks?
00161   Gen->NPCh = NewNPCh;
00162 }
00163 int VarData::SetNBlock(int Val){
00164   if(Val == Gen->NBlock) return 1;
00165   if(!VAR_IF_TYPE(SysType,VAR_NBLOCK_ALL)){
00166     Gen->NBlock = Val;
00167     Block = (BLOCK *)calloc(Gen->NBlock,sizeof(BLOCK));
00168     VAR_ADD_TYPE(SysType,VAR_NBLOCK_ALL);
00169   }
00170   else{
00171     Gen->NBlock = Val;
00172     Block = (BLOCK *) realloc(Block,Gen->NBlock*sizeof(BLOCK));
00173   }
00174   return 0;
00175 };
00176 int VarData::SetNNano(int Val){
00177   if(Val == Gen->NNano) return 1;
00178   //if(!VAR_IF_TYPE(SysType,VAR_NNANO_ALL)){
00179   //  VAR_ADD_TYPE(SysType,VAR_NBLOCK_ALL);
00180   Gen->NNano = Val;
00181   if(Val == 0) Val = 1;
00182   Nano = (NANO *) realloc(Nano,Val*sizeof(NANO));
00183   return 0;
00184 };
00185 void VarData::SetNType(int NewNType){
00186   //blocks?
00187   Gen->NType = NewNType;
00188 }
00189 double VarData::pChPos(int c,int d){
00190   return Ch[c].Pos[d];
00191 };
00192 double VarData::pPos(int p,int d){
00193   return Pm[p].Pos[d] + Pm[p].Bkf[d];
00194 };
00195 void VarData::pPos(int p,double *Pos){
00196   for(int d=0;d<3;d++) 
00197     Pos[d] = Pm[p].Pos[d] + Pm[p].Bkf[d];
00198 };
00199 double VarData::pPosNoBkf(int p,int d){
00200   return Pm[p].Pos[d];
00201 };
00202 double VarData::pVel(int p,int d){
00203   return Pm[p].Vel[d];
00204 };
00205 void VarData::SetPos(int p, double *Pos){
00206   for(int d=0;d<3;d++)
00207     Pm[p].Pos[d] = Pos[d];
00208 };
00209 void VarData::SetPos(int p,int d,double Pos){
00210   Pm[p].Pos[d] = Pos;
00211 };
00212 void VarData::SetVel(int p, double *Vel){
00213   for(int d=0;d<3;d++)
00214     Pm[p].Vel[d] = Vel[d];
00215 };
00216 void VarData::SetType(int p,int t){
00217   Pm[p].Typ = t;
00218 };
00219 int VarData::pType(int p){
00220   return Pm[p].Typ;
00221 };
00222 int VarData::pChain(int p){
00223   return Pm[p].CId;
00224 };
00225 double VarData::pNanoPos(int n,int d){
00226   return Nano[n].Pos[d] + Nano[n].Bkf[d];
00227 };
00228 void VarData::SetBkf(int p){
00229   for(int d=0;d<3;d++) 
00230     Pm[p].Bkf[d] = -floor(Pm[p].Pos[d]*pInvEdge(d))*pEdge(d);
00231 };
00232 void VarData::SetNanoBkf(int n){
00233   for(int d=0;d<3;d++) 
00234     Nano[n].Bkf[d] = -floor(Nano[n].Pos[d]*pInvEdge(d))*pEdge(d);
00235 };
00236 // void VarData::pPos(int p){
00237 //   printf("%lf %lf %lf\n",Pm[p].Pos[0],Pm[p].Pos[1],Pm[p].Pos[2]);
00238 // };
00239 double *VarData::pPos(int p){
00240   return Pm[p].Pos;
00241 }
00242 void VarData::pPos(double *Pos){
00243   printf("%lf %lf %lf\n",Pos[0],Pos[1],Pos[2]);
00244 };
00245 int VarData::pStep(){return Gen->Step;};
00246 int VarData::pNPart(){return Gen->NPart;};
00247 int VarData::pNChain(){return Gen->NChain;};
00248 int VarData::pNChain(int b){return Block[b].NChain;};
00249 int VarData::pNPCh(){return Gen->NPCh;};
00250 int VarData::pNPCh(int b){return Block[b].NPCh;};
00251 int VarData::pNType(){return Gen->NType;};
00252 int VarData::pNLink(){return Gen->NLink;};
00253 int VarData::pNNano(){return Gen->NNano;};
00254 int VarData::pNBlock(){return Gen->NBlock;};
00255 int VarData::pNAllocP(){return Gen->NAllocP;};
00256 int VarData::pNAllocC(){return Gen->NAllocC;};