Allink  v0.1
VarDataComm.cpp
1 #include <VarData.h>
2 int VarData::SetNPart(int NewNPart){
3  if(Gen->NPart == NewNPart) return 1;
4  int OldNPart = pNPart();
5  Gen->NPart = NewNPart;
6  if( !VAR_IF_TYPE(SysType,VAR_PART_ALLOCATED) ){
7  Gen->NAllocP = Gen->NPart;
8  if(pNPCh() > 0 && pNPCh() < 200 ) Gen->NAllocP += 2000*Gen->NPCh;
9  else Gen->NAllocP += 2000;
10  Gen->NAllocP += 2000;
11  Pm = (PART *)calloc(Gen->NAllocP,sizeof(PART));
12  if(Pm == NULL){
13  printf("Couldn't alloc the particles\n");
14  exit(1);
15  }
16  VAR_ADD_TYPE(SysType,VAR_PART_ALLOCATED);
17  if(pNLink() > 0){
18  AllocLinks(NewNPart);
19  }
20  }
21  if(Gen->NAllocP < NewNPart){
22  Gen->NAllocP = NewNPart;
23  if(pNPCh() > 0 && pNPCh() < 200 ) Gen->NAllocP += 2000*Gen->NPCh;
24  else Gen->NAllocP += 2000;
25  PART *Pn = (PART *)calloc(Gen->NPart,sizeof(PART));
26  Copy(Pn,Pm,OldNPart);
27  Pm = (PART *)realloc(Pm,Gen->NAllocP*sizeof(PART));
28  if(Pm == NULL){
29  printf("Couldn't realloc the particles\n");
30  exit(1);
31  }
32  if(pNLink() > 0){
33  AllocLinks(NewNPart);
34  }
35  Copy(Pm,Pn,OldNPart);
36  free(Pn);
37  }
38  else return 1;
39  return 0;
40 }
41 int VarData::SetNLink(int NewNLink){
42  if(Gen->NLink == NewNLink) return 1;
43  Gen->NLink = NewNLink;
44  return 0;
45 }
46 int VarData::AllocLinks(int NewNPart){
47  if( !VAR_IF_TYPE(SysType,VAR_LINK_ALLOCATED) ){
48  Ln = (LINKS *)calloc(Gen->NAllocP,sizeof(LINKS));
49  if(Ln == NULL){
50  printf("Couldn't alloc the links\n");
51  exit(1);
52  }
53  for(int p=0;p<Gen->NAllocP;p++){
54  Ln[p].Link = (int *)calloc(Gen->NLink,sizeof(int));
55  if(Ln[p].Link == NULL){
56  printf("Couldn't alloc the links\n");
57  exit(1);
58  }
59  }
60  VAR_ADD_TYPE(SysType,VAR_LINK_ALLOCATED);
61  }
62  else if(Gen->NAllocP < NewNPart){
63  Ln = (LINKS *)realloc(Ln,Gen->NAllocP*sizeof(LINKS));
64  if(Ln == NULL){
65  printf("Couldn't realloc the links\n");
66  exit(1);
67  }
68  for(int p=0;p<Gen->NAllocP;p++){
69  Ln[p].Link = (int *)realloc(Ln[p].Link,Gen->NLink*sizeof(int));
70  if(Ln[p].Link == NULL){
71  printf("Couldn't realloc the links\n");
72  exit(1);
73  }
74  }
75  }
76  return 0;
77 }
78 int VarData::SetNChain(int NewNChain){
79  int OldNChain = pNChain();
80  Gen->NChain = NewNChain;
81  if( !VAR_IF_TYPE(SysType,VAR_CH_ALLOCATED) ){
82  Gen->NAllocC = Gen->NChain+200;
83  Ch = (CHAIN *)calloc(Gen->NAllocC,sizeof(CHAIN));
84  VAR_ADD_TYPE(SysType,VAR_CH_ALLOCATED);
85  }
86  if(Gen->NAllocC < NewNChain){
87  Gen->NAllocC = NewNChain + 200;
88  CHAIN *Cn = (CHAIN *)calloc(Gen->NChain,sizeof(CHAIN));
89  Copy(Cn,Ch,OldNChain);
90  Ch = (CHAIN *)realloc(Ch,Gen->NAllocC*sizeof(CHAIN));
91  if(Ch == NULL){
92  printf("Couldn't realloc the chains\n");
93  exit(1);
94  }
95  Copy(Ch,Cn,OldNChain);
96  free(Cn);
97  }
98  else return 1;
99  return 0;
100 }
101 //obsolete?
103  if(Gen->NPart == 0){
104  printf("No particles or no particle tag: {} \n");
105  return ;
106  }
107  if( !VAR_IF_TYPE(SysType,VAR_PART_ALLOCATED) ){
108  Gen->NAllocC = Gen->NChain+100;
109  Gen->NAllocP = Gen->NPart+100*Gen->NPCh;
110  Pm = (PART *)calloc(Gen->NAllocP,sizeof(PART));
111  Ln = (LINKS *)calloc(Gen->NAllocP,sizeof(LINKS));
112  if(Ln == NULL || Pm == NULL){
113  return ;
114  }
115  for(int p=0;p<Gen->NAllocP;p++){
116  Ln[p].Link = (int *)calloc(Gen->NLink,sizeof(int));
117  }
118  Ch = (CHAIN *)calloc(Gen->NAllocC,sizeof(CHAIN));
119  VAR_ADD_TYPE(SysType,VAR_PART_ALLOCATED);
120  VAR_ADD_TYPE(SysType,VAR_CH_ALLOCATED);
121  }
122  //FIXME: doesn't realloc
123  else {
124  //free(Ch);
125  //for(int p=0;p<Gen->NPart;p++) free(Pm[p].Link);
126  //free(Pm);
127  // Pm = (PART *)realloc(Pm,Gen->NPart*sizeof(PART));
128  //Pm = (PART *)calloc(Gen->NPart,sizeof(PART));
129  // for(int p=0;p<Gen->NPart;p++){
130  // Pm[p].Link = (int *)realloc(Pm[p].Link,Gen->NLink*sizeof(int));
131  // //Pm[p].Link = (int *)calloc(Gen->NLink,sizeof(int));
132  // }
133  // Ch = (CHAIN *)realloc(Ch,Gen->NChain*sizeof(CHAIN));
134  //Ch = (CHAIN *)calloc(Gen->NChain,sizeof(CHAIN));
135  }
136  if(!Pm){ printf("Non alloca Pm\n");return ;}
137  if(!Ch){ printf("Non alloca Ch\n");return ;}
138 }
139 void VarData::Copy(PART *P1,PART *P2,int NPartOld){
140  for(int p=0;p<pNPart();p++){
141  if(p >= NPartOld) break;
142  for(int d=0;d<3;d++){
143  P1[p].Pos[d] = P2[p].Pos[d];
144  P1[p].Vel[d] = P2[p].Vel[d];
145  }
146  P1[p].Typ = P2[p].Typ;
147  }
148 }
149 void VarData::Copy(CHAIN *C1,CHAIN *C2,int NChainOld){
150  for(int c=0;c<pNChain();c++){
151  if(c >= NChainOld) break;
152  for(int d=0;d<3;d++){
153  C1[c].Pos[d] = C2[c].Pos[d];
154  C1[c].Vel[d] = C2[c].Vel[d];
155  }
156  C1[c].Type = C2[c].Type;
157  }
158 }
159 void VarData::SetNPCh(int NewNPCh){
160  //blocks?
161  Gen->NPCh = NewNPCh;
162 }
163 int VarData::SetNBlock(int Val){
164  if(Val == Gen->NBlock) return 1;
165  if(!VAR_IF_TYPE(SysType,VAR_NBLOCK_ALL)){
166  Gen->NBlock = Val;
167  Block = (BLOCK *)calloc(Gen->NBlock,sizeof(BLOCK));
168  VAR_ADD_TYPE(SysType,VAR_NBLOCK_ALL);
169  }
170  else{
171  Gen->NBlock = Val;
172  Block = (BLOCK *) realloc(Block,Gen->NBlock*sizeof(BLOCK));
173  }
174  return 0;
175 };
176 int VarData::SetNNano(int Val){
177  if(Val == Gen->NNano) return 1;
178  //if(!VAR_IF_TYPE(SysType,VAR_NNANO_ALL)){
179  // VAR_ADD_TYPE(SysType,VAR_NBLOCK_ALL);
180  Gen->NNano = Val;
181  if(Val == 0) Val = 1;
182  Nano = (NANO *) realloc(Nano,Val*sizeof(NANO));
183  return 0;
184 };
185 void VarData::SetNType(int NewNType){
186  //blocks?
187  Gen->NType = NewNType;
188 }
189 double VarData::pChPos(int c,int d){
190  return Ch[c].Pos[d];
191 };
192 double VarData::pPos(int p,int d){
193  return Pm[p].Pos[d] + Pm[p].Bkf[d];
194 };
195 void VarData::pPos(int p,double *Pos){
196  for(int d=0;d<3;d++)
197  Pos[d] = Pm[p].Pos[d] + Pm[p].Bkf[d];
198 };
199 double VarData::pPosNoBkf(int p,int d){
200  return Pm[p].Pos[d];
201 };
202 double VarData::pVel(int p,int d){
203  return Pm[p].Vel[d];
204 };
205 void VarData::SetPos(int p, double *Pos){
206  for(int d=0;d<3;d++)
207  Pm[p].Pos[d] = Pos[d];
208 };
209 void VarData::SetPos(int p,int d,double Pos){
210  Pm[p].Pos[d] = Pos;
211 };
212 void VarData::SetVel(int p, double *Vel){
213  for(int d=0;d<3;d++)
214  Pm[p].Vel[d] = Vel[d];
215 };
216 void VarData::SetType(int p,int t){
217  Pm[p].Typ = t;
218 };
219 int VarData::pType(int p){
220  return Pm[p].Typ;
221 };
222 int VarData::pChain(int p){
223  return Pm[p].CId;
224 };
225 double VarData::pNanoPos(int n,int d){
226  return Nano[n].Pos[d] + Nano[n].Bkf[d];
227 };
228 void VarData::SetBkf(int p){
229  for(int d=0;d<3;d++)
230  Pm[p].Bkf[d] = -floor(Pm[p].Pos[d]*pInvEdge(d))*pEdge(d);
231 };
233  for(int d=0;d<3;d++)
234  Nano[n].Bkf[d] = -floor(Nano[n].Pos[d]*pInvEdge(d))*pEdge(d);
235 };
236 // void VarData::pPos(int p){
237 // printf("%lf %lf %lf\n",Pm[p].Pos[0],Pm[p].Pos[1],Pm[p].Pos[2]);
238 // };
239 double *VarData::pPos(int p){
240  return Pm[p].Pos;
241 }
242 void VarData::pPos(double *Pos){
243  printf("%lf %lf %lf\n",Pos[0],Pos[1],Pos[2]);
244 };
245 int VarData::pStep(){return Gen->Step;};
246 int VarData::pNPart(){return Gen->NPart;};
247 int VarData::pNChain(){return Gen->NChain;};
248 int VarData::pNChain(int b){return Block[b].NChain;};
249 int VarData::pNPCh(){return Gen->NPCh;};
250 int VarData::pNPCh(int b){return Block[b].NPCh;};
251 int VarData::pNType(){return Gen->NType;};
252 int VarData::pNLink(){return Gen->NLink;};
253 int VarData::pNNano(){return Gen->NNano;};
254 int VarData::pNBlock(){return Gen->NBlock;};
255 int VarData::pNAllocP(){return Gen->NAllocP;};
256 int VarData::pNAllocC(){return Gen->NAllocC;};
int SetNLink(int NewNCh)
Set and reallocate the number of links.
Definition: VarDataComm.cpp:41
void SetNPCh(int NewNCh)
Set and reallocate the number of particles per chains.
CHAIN * Ch
Information on all chains.
Definition: VarData.h:1050
int CId
Chain Identifier.
Definition: VarData.h:224
int pNLink()
Maximum number of bonds.
Information about the nanoparticle.
Definition: VarData.h:423
int Type
Type of the chain (see list CHAIN_)
Definition: VarData.h:246
double pPosNoBkf(int p, int d)
Return the velocity.
double pVel(int p, int d)
Return the velocity.
double pChPos(int p, int d)
Return back folded position.
BLOCK * Block
Information for every block.
Definition: VarData.h:1054
double Vel[4]
xyzr Velocity of the particle
Definition: VarData.h:220
LINKS * Ln
Array of linking between the particles.
Definition: VarData.h:1048
NANO * Nano
Extra particle.
Definition: VarData.h:1044
double Bkf[3]
xyz Backfold distance
Definition: VarData.h:218
int SetNChain(int NewNCh)
Set and reallocate the number of chains.
Definition: VarDataComm.cpp:78
int pChain(int p)
Return the chain.
double Vel[3]
Chain velocity.
Definition: VarData.h:242
int pNAllocC()
Allocated number of chains.
void AllocPart()
Alloc the structures.
void SetPos(int p, double *Pos)
Set the particle position.
int SetNBlock(int Val)
Set NBlock.
void SetNanoBkf(int n)
Set the back folded array for the nano n.
double pInvEdge(int d)
Inverted xyzr edges of the simulation box.
Definition: VarData.h:920
int pType(int p)
Return the type.
Information for every block.
Definition: VarData.h:255
double Pos[3]
xyz Position of the particle
Definition: VarData.h:216
int NLink
Maximum number of bonds.
Definition: VarData.h:357
int NAllocP
Number of allocated particles.
Definition: VarData.h:351
void SetBkf(int p)
Set the back folded array for the particle p.
Information of every chain.
Definition: VarData.h:236
double Pos[3]
Position.
Definition: VarData.h:427
int pNAllocP()
Allocated number of particles.
double pNanoPos(int n, int d)
Return back folded nano position.
int SetNPart(int NewNPart)
Set and reallocate the number of particles.
Definition: VarDataComm.cpp:2
double pEdge(int d)
xyzr edges of the simulation box
Definition: VarData.h:918
int pNPCh()
Number of particle per chain.
int pNBlock()
Number of blocks.
int NChain
Number of chain.
Definition: VarData.h:347
int AllocLinks(int NewNCh)
(re)allocate the links
Definition: VarDataComm.cpp:46
int NPCh
Number of particle per chain.
Definition: VarData.h:349
void SetType(int p, int t)
Set the particle type.
void Copy(PART *P1, PART *P2, int NPartOld)
Copy the part P2 on part P1.
double Bkf[3]
Backfolded position.
Definition: VarData.h:429
int SysType
Contains the definition of the system.
Definition: VarData.h:1086
int pNNano()
Number of nanoparticles.
double Pos[4]
xyzr Postion of the chain
Definition: VarData.h:238
int NPCh
particles per chain
Definition: VarData.h:259
void SetNType(int NewNType)
Set the number of species.
int NChain
chains
Definition: VarData.h:263
int pNType()
of types of the particle
int NType
of types of the particle
Definition: VarData.h:355
double pPos(int p, int d)
Return back folded position.
unsigned long Step
Courrent step.
Definition: VarData.h:343
int pNChain()
Number of chain.
Information of every particle.
Definition: VarData.h:214
int NAllocC
Number of allocated chains.
Definition: VarData.h:353
int SetNNano(int Val)
Set NNano.
int NPart
Number of particle.
Definition: VarData.h:345
PART * Pm
Particle information of all particle.
Definition: VarData.h:1046
int Typ
Type.
Definition: VarData.h:226
int NBlock
Number of blocks.
Definition: VarData.h:359
void SetVel(int p, double *Vel)
Set the particle velocity.
int pStep()
Number of steps.
int pNPart()
Number of particle.