Allink  v0.1
VarData.cpp
1 /***********************************************************************
2 VarData: This Program reads and writes a specific file format
3 storing all the information relative to a set of equal structure
4 polymers to the CHAIN, PART and GENERAL structures. It provides
5 two different ways to backfold the coordinates, a fanction that
6 creates an initial system with different option and some function
7 for the data analisys. The first calculate the distribution of the
8 monomer in the box, the second the distribution of the bonds.
9 Copyright (C) 2008 by Giovanni Marelli <sabeiro@virgilio.it>
10 
11 
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16 
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21 
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 ***********************************************************************/
26 #include "../include/VarData.h"
27 #include <stdarg.h>
28 
29 void VarData::VarMessage(const char * s, ...)
30 {
31 #ifdef VAR_DEBUG
32  va_list args;
33  va_start(args, s);
34  fprintf(stderr,"VarData] ");
35  vfprintf(stderr, s, args);
36  fprintf(stderr, "\n");
37  va_end(args);
38 #else
39  return;
40 #endif
41 }
43  VarMessage("Constructing");
44  Mat = new Matematica();
45  MInt = new MatInt(3,3);
46  Gen = (GENERAL *) calloc(1,sizeof(GENERAL));
47  Nano = (NANO *) calloc(1,sizeof(NANO));
48  Soft = (SOFT *) calloc(1,sizeof(SOFT));
49  Gen->NBlock = 1;
50  Block = (BLOCK *)calloc(Gen->NBlock,sizeof(BLOCK));
51  if( !Gen | !Mat ){printf("Il costruttore non alloca\n");return;}
52  sprintf(cWhat2Draw,"part");
53  CNorm = 2;
54  CLat1 = (CNorm+1)%3;
55  CLat2 = (CNorm+2)%3;
56  SetNPCh(32); //# beads
57  for(int d=0;d<3;d++) ShiftPos[d] = 0.;
58  for(int d=0;d<3;d++) ScaleF[d] = 1.;
59  Gen->NLink = 0;//max # of links
60  NAddChain = 0;//added homopolymer chains
61  NAddChol = 0;
62  NSolvent = 0;
63  NStuffing = 0;
64  Gen->Deltat = 0.01;
65  Gen->chiN = 30.;
66  Gen->kappaN = 100.;
67  Gen->rho = 50.;
68  Gen->vBB = -0.1;
69  Gen->NChain = 1;
70  Gen->Temp = 1.;
71  Gen->Edge[0] = 16.;
72  Gen->Edge[1] = 16.;
73  Gen->Edge[2] = 32.;
74  Gen->NBlock = 1;
75  Gen->WFuncStraight2 = .9;
76  Gen->WFuncStraight3 = 1.0;
77  SysType = 0;
78  SysFormat = 0;
79  SysCreate = 0;
80  VAR_ADD_TYPE(SysCreate,VAR_ABSORBING);
82  NChType = CHAIN_EVERY;
83  NPType = BEAD_EVERY;
84  NEdge = 200;
85  IfNormalize = 0;
86  IfPlotMem = 0;
87  Gen->NNano = 1;
88  NSoft = 0;
89  Pm = NULL;Ch = NULL;
90  Point2Shape(SHAPE_NONE);
91 }
93  VarMessage("Destroying");
94  delete Mat;
95  free(Gen);
96  free(Nano);
97  free(Block);
98  free(Soft);
99  free(Ch);
100  for(int p=0;p<Gen->NPart;p++)
101  free(Ln[p].Link);
102  free(Pm);
103  free(Ln);
104 }
105 bool VarData::Open(char *InFile,int Bf){
106  VarMessage("Open");
107  Gen->NType = 0;
108  for(int d=0;d<3;d++)
109  Gen->Edge[d]=0.;
110  VAR_REM_TYPE(SysType,VAR_EDGE);
111  // VAR_REM_TYPE(SysType,VAR_CHAIN_DEF);
112  FILE *FileToRead;
113  if((FileToRead = fopen(InFile,"r"))==0){
114  printf("The file is missing %s \n",InFile);
115  return 1;
116  }
117  ReadHeader(FileToRead);
118  if( !VAR_IF_TYPE(SysType,VAR_OPEN_TRUST) )
119  if(ReadPassThru(FileToRead)) return 1;
120  ReadPart(FileToRead);
121  //if(AllocPart()) return 1;
122  ShiftRef(Bf);
123  fclose(FileToRead);
124  return 0;
125 }
126 bool VarData::OpenRisk(char *InFile,int Bf){
127  FILE *FileToRead;
128  if((FileToRead = fopen(InFile,"r"))==0){
129  printf("The file is missing %s \n",InFile);
130  return 1;
131  }
132  ReadHeader(FileToRead);
133  ReadPart(FileToRead);
134  ShiftRef(Bf);
135  fclose(FileToRead);
136  return 0;
137 }
138 Properties Properties::operator+ (const Properties& Pr) const{
139  Properties Result;
140  Result.RePhob = (this->RePhob + Pr.RePhob);
141  Result.RePhil = (this->RePhil + Pr.RePhil);
142  Result.VolPhob = (this->VolPhob + Pr.VolPhob);
143  Result.VolPhil = (this->VolPhil + Pr.VolPhil);
144  Result.FactPhob = (this->FactPhob + Pr.FactPhob);
145  Result.FactPhil = (this->FactPhil + Pr.FactPhil);
146  Result.GyrPhob = (this->GyrPhob + Pr.GyrPhob);
147  Result.GyrPhil = (this->GyrPhil + Pr.GyrPhil);
148  Result.ChDiff = (this->ChDiff + Pr.ChDiff);
149  return Result;
150 }
151 Properties Properties::operator* (const double& c) const{
152  Properties Result;
153  Result.RePhob = (this->RePhob * c);
154  Result.RePhil = (this->RePhil * c);
155  Result.VolPhob = (this->VolPhob * c);
156  Result.VolPhil = (this->VolPhil * c);
157  Result.FactPhob = (this->FactPhob * c);
158  Result.FactPhil = (this->FactPhil * c);
159  Result.GyrPhob = (this->GyrPhob * c);
160  Result.GyrPhil = (this->GyrPhil * c);
161  Result.ChDiff = (this->ChDiff * c);
162  return Result;
163 }
164 void VarData::SysInfo(char *cSystem){
165  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);
166 }
167 void VarData::SysDef(char *cSystem){
168  sprintf(cSystem,"# SysType) Edge %d Sys (Txvl %d Xvt %d) \n",
169  VAR_IF_TYPE(SysType,VAR_EDGE),SysFormat==VAR_SYS_TXVL,SysFormat==VAR_SYS_XVT);
170 }
172  double v2[6];
173  double v3[10];
174  v2[0] = -2. * (Gen->kappaN+3.)/Gen->rho;//00
175  v2[3] = Gen->vBB;//11
176  v2[1] = Gen->chiN/Gen->rho + .5*(v2[0]+v2[2]);//01
177  v2[2] = v2[1];//02
178  v2[4] = v2[3];//12
179  if( 1 == 1){//Hydrophobic
180  v2[2] = 1.*v2[0];//02
181  v2[4] = v2[1];//12
182  }
183  v2[5] = 0.;//22
184  v3[0] = 1.5 * (Gen->kappaN+2.)/QUAD(Gen->rho);//000
185  v3[1] = v3[0];//001
186  v3[2] = v3[0];//002
187  v3[3] = v3[0];//011
188  v3[4] = v3[0];//012
189  v3[5] = v3[0];//022
190  v3[6] = 0.;//111
191  v3[7] = v3[0];//112
192  v3[8] = v3[0];//122
193  v3[9] = 0.;//222
194  SetCoeff(v2,v3);
195 }
196 void VarData::SetCoeff(double *v2,double *v3){
197  MInt->SetCoeff(v2[0],0,0);
198  MInt->SetCoeff(v2[1],0,1);
199  MInt->SetCoeff(v2[2],0,2);
200  MInt->SetCoeff(v2[3],1,1);
201  MInt->SetCoeff(v2[4],1,2);
202  MInt->SetCoeff(v2[5],2,2);
203  MInt->SetCoeff(v3[0],0,0,0);
204  MInt->SetCoeff(v3[1],0,0,1);
205  MInt->SetCoeff(v3[2],0,0,2);
206  MInt->SetCoeff(v3[3],0,1,1);
207  MInt->SetCoeff(v3[4],0,1,2);
208  MInt->SetCoeff(v3[5],0,2,2);
209  MInt->SetCoeff(v3[6],1,1,1);
210  MInt->SetCoeff(v3[7],1,1,2);
211  MInt->SetCoeff(v3[8],1,2,2);
212  MInt->SetCoeff(v3[9],2,2,2);
213 }
214 MatInt::MatInt(int NTypeExt,int NOrdExt){
215  NOrd = NOrdExt;
216  NType = NTypeExt;
217  if(NOrd > 1){
218  int NMax = IntType(NType-1,NType-1)+1;
219  IntMatr2 = new double[NMax];
220  }
221  if(NOrd > 2){
222  int NMax = IntType(NType-1,NType-1,NType-1)+1;
223  IntMatr3 = new double[NMax];
224  }
225 }
226 void MatInt::FillEntries(double *Matr,int Ord){
227  if(Ord == 2){
228  for(int t=0;t<NType;t++){
229  for(int tt=t;tt<NType;tt++){
230  int ttt = IntType(t,tt);
231  IntMatr2[ttt] = Matr[ttt];
232  }
233  }
234  }
235  if(Ord == 3){
236  for(int t=0;t<NType;t++){
237  for(int tt=t;tt<NType;tt++){
238  for(int ttt=tt;ttt<NType;ttt++){
239  int tf = IntType(t,tt,ttt);
240  IntMatr3[tf] = Matr[tf];
241  }
242  }
243  }
244  }
245 }
246 double MatInt::Coeff(int t1,int t2){
247  return IntMatr2[IntType(t1,t2)];
248 }
249 double MatInt::Coeff(int t1,int t2,int t3){
250  return IntMatr3[IntType(t1,t2,t3)];
251 }
252 void MatInt::SetCoeff(double Co,int t1,int t2){
253  IntMatr2[NType*t1+t2] = Co;
254  IntMatr2[NType*t2+t1] = Co;
255  //IntMatr2[IntType(t1,t2)] = Co;
256 }
257 void MatInt::SetCoeff(double Co,int t1,int t2,int t3){
258  IntMatr3[(t1*NType+t2)*NType+t3] = Co;
259  IntMatr3[(t1*NType+t3)*NType+t2] = Co;
260  IntMatr3[(t2*NType+t1)*NType+t3] = Co;
261  IntMatr3[(t2*NType+t3)*NType+t1] = Co;
262  IntMatr3[(t3*NType+t2)*NType+t1] = Co;
263  IntMatr3[(t3*NType+t1)*NType+t2] = Co;
264  // IntMatr3[IntType(t1,t2,t3)] = Co;
265 }
266 int MatInt::IntType(int t1,int t2){
267  return NType*t1+t2;
268  return NType*MIN(t1,t2)+MAX(t1,t2);
269  int t = 0;
270  for(int ta=0;ta<MIN(t1,t2);ta++){
271  for(int tb=ta;tb<MAX(t1,t2);tb++){
272  t++;
273  }
274  }
275  return t;
276 }
277 int MatInt::IntType(int t1,int t2,int t3){
278  return (t1*NType+t2)*NType+t3;
279  int tTemp = 0;
280  int tSort[3] = {t1,t2,t3};
281  for(int d=1;d<3;d++){
282  if(tSort[d] < tSort[d-1]){
283  tTemp = tSort[d];
284  tSort[d] = tSort[d-1];
285  tSort[d-1] = tTemp;
286  }
287  }
288  return (NType*tSort[0]+tSort[1])*NType+tSort[2];
289 }
291  printf(" |_");
292  for(int tc=0;tc<NType;tc++)
293  printf("_____%d_____",tc);
294  printf("|\n");
295  for(int tr=0;tr<NType;tr++){
296  printf("%d| ",tr);
297  for(int tc=0;tc<NType;tc++){
298  printf("%lf ",Coeff(tr,tc));
299  }
300  printf("|\n");
301  }
302  printf("\n");
303  for(int t=0;t<NType;t++){
304  for(int tt=t;tt<NType;tt++){
305  for(int ttt=tt;ttt<NType;ttt++){
306  int tf = IntType(t,tt,ttt);
307  printf("%d%d%d) %lf\n",t,tt,ttt,Coeff(t,tt,ttt));
308  }
309  }
310  }
311 }
312 void MatInt::Rescale(double SFactor,int Order){
313  if(Order == 2){
314  for(int t1=0;t1<NType;t1++)
315  for(int t2=0;t2<NType;t2++)
316  IntMatr2[(t1*NType+t2)] *= SFactor;
317  }
318  if(Order == 3){
319  for(int t1=0;t1<NType;t1++)
320  for(int t2=0;t2<NType;t2++)
321  for(int t3=0;t3<NType;t3++)
322  IntMatr3[(t1*NType+t2)*NType+t3] *= SFactor;
323  }
324 }
325 //memchr in wich position a caracter is
326 //memcmp how distant are to pointers
327 //memcpy copy n bytes from the second pointer to the first
328 //memmove the same but uses a buffer and the memory can overlap
329 //memset set the value of the n character in a string
330 //strcat append a value to a string
331 //strchr says to a pointer where is a character
332 //strcmp compare two strings
333 //strcoll compare two strings avoiding null-character
334 //strcopy copy two strings
335 //strcspn find the position in wich one char of the second string appear in the first
336 //strncat append n char to the string
337 //strncmp compare n char from the begining
int ReadPart(FILE *FileToRead)
Reads particle type and position.
void SetNPCh(int NewNCh)
Set and reallocate the number of particles per chains.
CHAIN * Ch
Information on all chains.
Definition: VarData.h:1050
Information about the nanoparticle.
Definition: VarData.h:423
double Deltat
Timestep.
Definition: VarData.h:337
double kappaN
Incompressibility.
Definition: VarData.h:325
General information of the system.
Definition: VarData.h:299
void SysDef(char *cSystem)
Print a string with the system definitions.
Definition: VarData.cpp:167
~VarData()
Destructor.
Definition: VarData.cpp:92
bool OpenRisk(char *InFile, int BF)
Opens a file without reallocationg.
Definition: VarData.cpp:126
BLOCK * Block
Information for every block.
Definition: VarData.h:1054
void SysInfo(char *cSystem)
Print a string with the system information.
Definition: VarData.cpp:164
int NAddChol
Additional cholesterol chains into the membrane.
Definition: VarData.h:1068
LINKS * Ln
Array of linking between the particles.
Definition: VarData.h:1048
double ShiftPos[3]
Reference position.
Definition: VarData.h:1058
NANO * Nano
Extra particle.
Definition: VarData.h:1044
double chiN
Incompatibility.
Definition: VarData.h:323
Energy contribution from the density functional hamiltonian.
Definition: VarData.h:388
double Hamaker
Strength of the interaction.
Definition: VarData.h:447
double Edge[4]
xyzr edges of the simulation box
Definition: VarData.h:309
void SetCoeff(double Co, int t1, int t2)
Set the prefactor.
Definition: VarData.cpp:252
char cWhat2Draw[STRSIZE]
What to draw.
Definition: VarData.h:1042
double WFuncStraight2
Weighting function straight length.
Definition: VarData.h:339
int Asym
Diblock limit of the chain.
Definition: VarData.h:273
double Temp
Temperature.
Definition: VarData.h:303
double Rad
Size.
Definition: VarData.h:445
double Height
Height of the cylinder.
Definition: VarData.h:449
Information for every soft object.
Definition: VarData.h:276
int NChType
Type of chain selected.
Definition: VarData.h:1080
int CLat2
lateral coordinate
Definition: VarData.h:1078
Information for every block.
Definition: VarData.h:255
int IfPlotMem
If PlotMem is allocated and filled.
Definition: VarData.h:1094
int NLink
Maximum number of bonds.
Definition: VarData.h:357
Some calculated properties of the system.
Definition: VarData.h:364
bool Open(char *InFile, int BF)
Open the.
Definition: VarData.cpp:105
int NAddChain
Additional homopolymer chains into the membrane.
Definition: VarData.h:1066
int SysCreate
Contains the information for the creation.
Definition: VarData.h:1090
void Print()
Print the entries.
Definition: VarData.cpp:290
int Shape
0 none, 1 spherical, 2 cylindrical 3 wall
Definition: VarData.h:473
void ReadHeader(FILE *FileToRead)
Reads a header.
int NStuffing
Stuffing for the cylinder.
Definition: VarData.h:1072
double vBB
Chemical potential of the water.
Definition: VarData.h:319
int NPType
Type of particle selected.
Definition: VarData.h:1082
int NPartNearSphere
Number of particle to be considered in the radial density profile.
Definition: VarData.h:1064
MatInt * MInt
Matrix of the prefactor of the interactions.
Definition: VarData.h:529
int NChain
Number of chain.
Definition: VarData.h:347
int IntType(int t1, int t2)
Matrix element of the two type interaction.
Definition: VarData.cpp:266
int NEdge
Number of particles per edge.
Definition: VarData.h:1084
VarData()
Set the constants.
Definition: VarData.cpp:42
void Point2Shape(int iShape)
Point to the shape function.
Definition: VarDataEl.cpp:490
int NPCh
Number of particle per chain.
Definition: VarData.h:349
void ShiftRef(int BackFold)
Backfold the system wrt the reference position.
int SysType
Contains the definition of the system.
Definition: VarData.h:1086
double Coeff(int t1, int t2)
Prefactor of the force.
Definition: VarData.cpp:246
double ScaleF[3]
Scale factor.
Definition: VarData.h:1060
void FillEntries(double *Matr, int Ord)
Fill all the entries.
Definition: VarData.cpp:226
int SysFormat
Contains the definition of the file format.
Definition: VarData.h:1088
int NSolvent
Solvent molecules.
Definition: VarData.h:1070
SOFT * Soft
Soft bodies.
Definition: VarData.h:1052
Matematica * Mat
Implementation of all usefull algorythms.
Definition: VarData.h:527
double WFuncStraight3
Weighting function straight length.
Definition: VarData.h:341
MatInt(int NType, int NOrd)
Creates and fill the matrix.
Definition: VarData.cpp:214
int NType
of types of the particle
Definition: VarData.h:355
double RePhob
End2End distance of the.
Definition: VarData.h:373
Implementation of useful algorythms.
Definition: Matematica.h:76
int IfNormalize
If normalize the lateral dimensions to one.
Definition: VarData.h:1092
int CLat1
lateral coordinate
Definition: VarData.h:1076
int ReadPassThru(FILE *FileToRead)
Reads the information to alloc the structure.
int NSoft
Number of soft bodies.
Definition: VarData.h:1062
void Rescale(double SFactor, int Order)
Rescale entries.
Definition: VarData.cpp:312
int NPart
Number of particle.
Definition: VarData.h:345
PART * Pm
Particle information of all particle.
Definition: VarData.h:1046
int NBlock
Number of blocks.
Definition: VarData.h:359
void VarMessage(const char *s,...)
If enabled call the function position.
Definition: VarData.cpp:29
void SetCoeff()
Set the virial coefficients from the known values of density coex...
Definition: VarData.cpp:171
double rho
Density coexistence.
Definition: VarData.h:327
int CNorm
Normal coordinate.
Definition: VarData.h:1074