GLYLIB
0.3.0b
|
00001 // Function written by B. Lachele Foley, 2007 00002 // updated by same 20100307 00003 //#include <mylib.h> 00004 //#include <molecules.h> 00005 #include "../inc/mylib.h" 00006 #include "../inc/molecules.h" 00007 /****************** get_residue_COM() *********************/ 00008 /* Finds the center of mass of the molecule and places it in the 00009 * appropriate location. Similar functions exist for the other 00010 * structures. 00011 */ 00012 coord_3D get_residue_COM(residue *r,atype *ATYPE, int xs){ 00013 int ta=0; 00014 double tmm=0,*mass; 00015 coord_3D c; 00016 // Set the reference masses to use 00017 // an calculate the mw while you're at it 00018 mass=(double*)calloc(r[0].na,sizeof(double)); 00019 // calculate the molecular weight 00020 for(ta=0;ta<r[0].na;ta++){ 00021 if((ATYPE!=NULL)&&(ATYPE!=0x0)){mass[ta]=ATYPE[r[0].a[ta].t].m;} 00022 else{ if(r[0].a[ta].m==0){ 00023 fprintf(stderr,"Warning: Atom found with zero mass in get_residue_COM\n");} 00024 mass[ta]=r[0].a[ta].m; } 00025 tmm+=mass[ta]; 00026 } 00027 //printf("the molecular weight is %f\n",tmm); 00028 if(tmm==0){mywhine("Found residue with zero mw.\n(Have types/masses been assigned?");} 00029 r[0].m=tmm; 00030 00031 c.i=0; 00032 c.j=0; 00033 c.k=0; 00034 //printf("center of mass is at: %f %f %f \n",m[0].COM.i,m[0].COM.j,m[0].COM.k); 00035 // calculate the center of mass 00036 if(xs==-1){ 00037 for(ta=0;ta<r[0].na;ta++){ // COM = sum(m_i * r_i) / sum(m_i) 00038 c.i+=r[0].a[ta].x.i*mass[ta]; 00039 c.j+=r[0].a[ta].x.j*mass[ta]; 00040 c.k+=r[0].a[ta].x.k*mass[ta]; 00041 } 00042 } 00043 else{ 00044 for(ta=0;ta<r[0].na;ta++){ // COM = sum(m_i * r_i) / sum(m_i) 00045 c.i+=r[0].a[ta].xa[xs].i*mass[ta]; 00046 c.j+=r[0].a[ta].xa[xs].j*mass[ta]; 00047 c.k+=r[0].a[ta].xa[xs].k*mass[ta]; 00048 } 00049 } 00050 free(mass); 00051 //printf("center of mass is at: %f %f %f \n",m[0].COM.i,m[0].COM.j,m[0].COM.k); 00052 c.i/=tmm; 00053 c.j/=tmm; 00054 c.k/=tmm; 00055 //printf("center of mass is at: %20.12e %20.12e %20.12e \n",m[0].COM.i,m[0].COM.j,m[0].COM.k); 00056 return c; 00057 } 00058 00059 /****************** get_molecule_COM() *********************/ 00060 /* Finds the center of mass of the molecule and places it in the 00061 * appropriate location. Similar functions exist for the other 00062 * structures. 00063 */ 00064 coord_3D get_molecule_COM(molecule *m,atype *ATYPE, int xs){ 00065 int ta=0,tr=0; 00066 double tmm=0,**mass; 00067 coord_3D c; 00068 // Set the reference masses to use 00069 // an calculate the mw while you're at it 00070 mass=(double**)calloc(m[0].nr,sizeof(double*)); 00071 // calculate the molecular weight 00072 for(tr=0;tr<m[0].nr;tr++){ 00073 mass[tr]=(double*)calloc(m[0].r[tr].na,sizeof(double)); 00074 for(ta=0;ta<m[0].r[tr].na;ta++){ 00075 if((ATYPE!=NULL)&&(ATYPE!=0x0)){mass[tr][ta]=ATYPE[m[0].r[tr].a[ta].t].m;} 00076 else{ if(m[0].r[tr].a[ta].m==0){ 00077 fprintf(stderr,"Warning: Atom found with zero mass in get_molecule_COM\n");} 00078 mass[tr][ta]=m[0].r[tr].a[ta].m; } 00079 tmm+=mass[tr][ta]; 00080 } 00081 } 00082 //printf("the molecular weight is %f\n",tmm); 00083 if(tmm==0){mywhine("molecular weight of molecule %s is zero.\n(Have types/masses been assigned?");} 00084 m[0].m=tmm; 00085 00086 c.i=0; 00087 c.j=0; 00088 c.k=0; 00089 //printf("center of mass is at: %f %f %f \n",m[0].COM.i,m[0].COM.j,m[0].COM.k); 00090 // calculate the center of mass 00091 if(xs==-1){ 00092 for(tr=0;tr<m[0].nr;tr++){ // COM = sum(m_i * r_i) / sum(m_i) 00093 for(ta=0;ta<m[0].r[tr].na;ta++){ // COM = sum(m_i * r_i) / sum(m_i) 00094 c.i+=m[0].r[tr].a[ta].x.i*mass[tr][ta]; 00095 c.j+=m[0].r[tr].a[ta].x.j*mass[tr][ta]; 00096 c.k+=m[0].r[tr].a[ta].x.k*mass[tr][ta]; 00097 } 00098 } 00099 } 00100 else { 00101 for(tr=0;tr<m[0].nr;tr++){ // COM = sum(m_i * r_i) / sum(m_i) 00102 for(ta=0;ta<m[0].r[tr].na;ta++){ // COM = sum(m_i * r_i) / sum(m_i) 00103 c.i+=m[0].r[tr].a[ta].xa[xs].i*mass[tr][ta]; 00104 c.j+=m[0].r[tr].a[ta].xa[xs].j*mass[tr][ta]; 00105 c.k+=m[0].r[tr].a[ta].xa[xs].k*mass[tr][ta]; 00106 } 00107 } 00108 } 00109 for(tr=0;tr<m[0].nr;tr++){ 00110 free(mass[tr]); 00111 } 00112 free(mass); 00113 //printf("center of mass is at: %f %f %f \n",m[0].COM.i,m[0].COM.j,m[0].COM.k); 00114 c.i/=tmm; 00115 c.j/=tmm; 00116 c.k/=tmm; 00117 //printf("center of mass is at: %20.12e %20.12e %20.12e \n",m[0].COM.i,m[0].COM.j,m[0].COM.k); 00118 return c; 00119 } 00120 00121 /****************** get_assembly_molecule_COM() *********************/ 00122 /* Finds the center of mass based on the molecules and places it in 00123 * the appropriate location. Similar functions exist for the other 00124 * structures. 00125 */ 00126 coord_3D get_assembly_molecule_COM(assembly *a,atype *ATYPE, int xs){ 00127 int ta=0,tr=0,tm=0; 00128 double tmm=0,***mass; 00129 coord_3D c; 00130 // Set the reference masses to use 00131 // and calculate the mw while you're at it 00132 mass=(double***)calloc(a[0].nm,sizeof(double**)); 00133 for(tm=0;tm<a[0].nm;tm++){ 00134 mass[tm]=(double**)calloc(a[0].m[tm][0].nr,sizeof(double*)); 00135 for(tr=0;tr<a[0].m[tm][0].nr;tr++){ 00136 mass[tm][tr]=(double*)calloc(a[0].m[tm][0].r[tr].na,sizeof(double)); 00137 for(ta=0;ta<a[0].m[tm][0].r[tr].na;ta++){ 00138 // calculate the molecular weight 00139 if((ATYPE!=NULL)&&(ATYPE!=0x0)){mass[tm][tr][ta]=ATYPE[a[0].m[tm][0].r[tr].a[ta].t].m;} 00140 else{ if(a[0].m[tm][0].r[tr].a[ta].m==0){ 00141 fprintf(stderr,"Warning: Atom found with zero mass in get_assembly_molecule_COM\n");} 00142 mass[tm][tr][ta]=a[0].m[tm][0].r[tr].a[ta].m; } 00143 tmm+=mass[tm][tr][ta]; 00144 } 00145 } 00146 } 00147 //printf("the molecular weight is %f\n",tmm); 00148 if(tmm==0){mywhine("molecular weight of molecule %s is zero.\n(Have types/masses been assigned?");} 00149 a[0].mass=tmm; 00150 00151 c.i=0; 00152 c.j=0; 00153 c.k=0; 00154 //printf("center of mass is at: %f %f %f \n",m[0].COM.i,m[0].COM.j,m[0].COM.k); 00155 // calculate the center of mass 00156 if(xs==-1){ 00157 for(tm=0;tm<a[0].nm;tm++){ // COM = sum(m_i * r_i) / sum(m_i) 00158 for(tr=0;tr<a[0].m[tm][0].nr;tr++){ // COM = sum(m_i * r_i) / sum(m_i) 00159 for(ta=0;ta<a[0].m[tm][0].r[tr].na;ta++){ // COM = sum(m_i * r_i) / sum(m_i) 00160 c.i+=a[0].m[tm][0].r[tr].a[ta].x.i*mass[tm][tr][ta]; 00161 c.j+=a[0].m[tm][0].r[tr].a[ta].x.j*mass[tm][tr][ta]; 00162 c.k+=a[0].m[tm][0].r[tr].a[ta].x.k*mass[tm][tr][ta]; 00163 } 00164 } 00165 } 00166 } 00167 else { 00168 for(tm=0;tm<a[0].nm;tm++){ // COM = sum(m_i * r_i) / sum(m_i) 00169 for(tr=0;tr<a[0].m[tm][0].nr;tr++){ // COM = sum(m_i * r_i) / sum(m_i) 00170 for(ta=0;ta<a[0].m[tm][0].r[tr].na;ta++){ // COM = sum(m_i * r_i) / sum(m_i) 00171 c.i+=a[0].m[tm][0].r[tr].a[ta].xa[xs].i*mass[tm][tr][ta]; 00172 c.j+=a[0].m[tm][0].r[tr].a[ta].xa[xs].j*mass[tm][tr][ta]; 00173 c.k+=a[0].m[tm][0].r[tr].a[ta].xa[xs].k*mass[tm][tr][ta]; 00174 } 00175 } 00176 } 00177 } 00178 for(tm=0;tm<a[0].nm;tm++){ 00179 for(tr=0;tr<a[0].m[tm][0].nr;tr++){ 00180 free(mass[tm][tr]); 00181 } 00182 free(mass[tm]); 00183 } 00184 free(mass); 00185 //printf("center of mass is at: %f %f %f \n",m[0].COM.i,m[0].COM.j,m[0].COM.k); 00186 c.i/=tmm; 00187 c.j/=tmm; 00188 c.k/=tmm; 00189 //printf("center of mass is at: %20.12e %20.12e %20.12e \n",m[0].COM.i,m[0].COM.j,m[0].COM.k); 00190 return c; 00191 } 00192 00193 /****************** get_ensemble_COM() *********************/ 00194 /* Finds the center of mass of the ensemble and places it in the 00195 * appropriate location. Similar functions exist for the other 00196 * structures. 00197 * NOTE!! Assemblies within ensembles are assumed to be redundant. 00198 * That is, the molecules in the ensemble are also represented 00199 * within the assemblies (copying the molecule pointers keeps 00200 * the data from taking extra space). So, when calculating the 00201 * center of mass for the ensemble, this function ONLY considers 00202 * molecules and not assemblies. 00203 */ 00204 coord_3D get_ensemble_COM(ensemble *e,atype *ATYPE, int xs){ 00205 int ta=0,tr=0,tm=0; 00206 double tmm=0,***mass; 00207 coord_3D c; 00208 // Set the reference masses to use 00209 // and calculate the mw while you're at it 00210 mass=(double***)calloc(e[0].nm,sizeof(double**)); 00211 for(tm=0;tm<e[0].nm;tm++){ 00212 mass[tm]=(double**)calloc(e[0].m[tm].nr,sizeof(double*)); 00213 for(tr=0;tr<e[0].m[tm].nr;tr++){ 00214 mass[tm][tr]=(double*)calloc(e[0].m[tm].r[tr].na,sizeof(double)); 00215 for(ta=0;ta<e[0].m[tm].r[tr].na;ta++){ 00216 // calculate the molecular weight 00217 if((ATYPE!=NULL)&&(ATYPE!=0x0)){mass[tm][tr][ta]=ATYPE[e[0].m[tm].r[tr].a[ta].t].m;} 00218 else{ if(e[0].m[tm].r[tr].a[ta].m==0){ 00219 fprintf(stderr,"Warning: Atom found with zero mass in get_ensemble_COM\n");} 00220 mass[tm][tr][ta]=e[0].m[tm].r[tr].a[ta].m; } 00221 tmm+=mass[tm][tr][ta]; 00222 } 00223 } 00224 } 00225 //printf("the molecular weight is %f\n",tmm); 00226 if(tmm==0){mywhine("molecular weight of molecule %s is zero.\n(Have types/masses been assigned?");} 00227 e[0].mass=tmm; 00228 00229 c.i=0; 00230 c.j=0; 00231 c.k=0; 00232 //printf("center of mass is at: %f %f %f \n",m[0].COM.i,m[0].COM.j,m[0].COM.k); 00233 // calculate the center of mass 00234 if(xs==-1){ 00235 for(tm=0;tm<e[0].nm;tm++){ // COM = sum(m_i * r_i) / sum(m_i) 00236 for(tr=0;tr<e[0].m[tm].nr;tr++){ // COM = sum(m_i * r_i) / sum(m_i) 00237 for(ta=0;ta<e[0].m[tm].r[tr].na;ta++){ // COM = sum(m_i * r_i) / sum(m_i) 00238 c.i+=e[0].m[tm].r[tr].a[ta].x.i*mass[tm][tr][ta]; 00239 c.j+=e[0].m[tm].r[tr].a[ta].x.j*mass[tm][tr][ta]; 00240 c.k+=e[0].m[tm].r[tr].a[ta].x.k*mass[tm][tr][ta]; 00241 } 00242 } 00243 } 00244 } 00245 else { 00246 for(tm=0;tm<e[0].nm;tm++){ // COM = sum(m_i * r_i) / sum(m_i) 00247 for(tr=0;tr<e[0].m[tm].nr;tr++){ // COM = sum(m_i * r_i) / sum(m_i) 00248 for(ta=0;ta<e[0].m[tm].r[tr].na;ta++){ // COM = sum(m_i * r_i) / sum(m_i) 00249 c.i+=e[0].m[tm].r[tr].a[ta].xa[xs].i*mass[tm][tr][ta]; 00250 c.j+=e[0].m[tm].r[tr].a[ta].xa[xs].j*mass[tm][tr][ta]; 00251 c.k+=e[0].m[tm].r[tr].a[ta].xa[xs].k*mass[tm][tr][ta]; 00252 } 00253 } 00254 } 00255 } 00256 for(tm=0;tm<e[0].nm;tm++){ 00257 for(tr=0;tr<e[0].m[tm].nr;tr++){ 00258 free(mass[tm][tr]); 00259 } 00260 free(mass[tm]); 00261 } 00262 free(mass); 00263 //printf("center of mass is at: %f %f %f \n",m[0].COM.i,m[0].COM.j,m[0].COM.k); 00264 c.i/=tmm; 00265 c.j/=tmm; 00266 c.k/=tmm; 00267 //printf("center of mass is at: %20.12e %20.12e %20.12e \n",m[0].COM.i,m[0].COM.j,m[0].COM.k); 00268 return c; 00269 }