/*! \brief * Function that does the analysis for a single frame. * * It is called once for each frame. */ static int analyze_frame(t_topology *top, t_trxframe *fr, t_pbc *pbc, int nr, gmx_ana_selection_t *sel[], void *data) { t_analysisdata *d = (t_analysisdata *)data; t_electrostatics dat; /* Print the current frame number to the xvg file */ if (d->fp) { fprintf(d->fp, "%10i ", d->framen); } /* Index the pqr file to the frame number */ std::string pqrname(d->pqr); pqrname = pqrname.substr(0,pqrname.size()-4); std::stringstream pqr; pqr << pqrname << d->framen << ".pqr"; FILE *pqrout; if ( d->dopqr ) { pqrout = ffopen(pqr.str().c_str(), "w"); if ( pqrout == NULL ) { gmx_fatal(FARGS, "\nFailed to open output file, '%s'\n",pqrout); } } /* Get the bond vector and bond midpoint */ rvec bondvector, midpoint; rvec_sub(fr->x[d->a2],fr->x[d->a1],bondvector); double bondlength = pow( iprod(bondvector,bondvector), 0.5); for (int i=0; i<3; i++){ midpoint[i] = fr->x[d->a1][i] + 0.5 * bondvector[i]; } /* * Defined axes based on the nitrile bond vector * The z-axis is the bond axis * The y-axis is from solving y0 = -(z1*y1+z2*y2)/z0, where (y1,y2)=(z2,-z0), then normalizing * The x-axis is the cross product of the y-axis with the z-axis */ double zvec[3] = { bondvector[0] / bondlength, bondvector[1] / bondlength, bondvector[2] / bondlength }; double yvec[3] = { 1, 1, 1 }; if (zvec[0] != 0 ) { yvec[0] = -(zvec[1]*yvec[1]+zvec[2]*yvec[2]) / zvec[0] ; } else if (zvec[1] != 0 ) { yvec[1] = -(zvec[0]*yvec[0]+zvec[2]*yvec[2]) / zvec[1] ; } else if (zvec[2] != 0 ) { yvec[2] = -(zvec[0]*yvec[0]+zvec[1]*yvec[1]) / zvec[2] ; } else { std::cerr << "\nERROR! Bond length is 0! This cannoy be correct!\n"; std::cerr << "BondVEC: " << bondvector[0] << " " << bondvector[1] << " " << bondvector[2] << std::endl; std::cerr << "ZVEC: " << zvec[0] << " " << zvec[1] << " " << zvec[2] << std::endl; } /* Normalize */ double ylen = pow(dot(yvec,yvec),0.5); for (int i=0; i<3;i++) { yvec[i] = yvec[i]/ylen; } double xvec[3]; cross(yvec,zvec,xvec); double xlen = pow(dot(xvec,xvec),0.5); for (int i=0; i<3;i++) { xvec[i] = xvec[i]/xlen; } /* Find the points that are Cho group-sites for calculating the potential */ std::vector<std::vector<double> > each_point; int nsites = 0; if (d->site_ndx.size() > 0) { /* * There are 8 sites from -a1, 8 sites from -a2, 1 site along the bond vector * and 1 site for each atom in site_ndx */ nsites = d->site_ndx.size() + 17; int n = 0; each_point = std::vector<std::vector<double> > (nsites, std::vector<double> (3,0)); //std::vector<std::vector<double> > each_point(nsites,std::vector<double> (3,0)); /* Assign coordinates for the atoms in site_ndx */ for (int i=0; i<(int)d->site_ndx.size(); i++) { for (int j=0; j<3; j++) { each_point[n][j] = fr->x[d->site_ndx[i]][j]; } n++; } /* Assign coordinate for the site along the bond vector */ for (int i=0; i<3; i++) { each_point[n][i] = fr->x[d->a2][i] + bondvector[i] / bondlength * d->ring_dist; } n++; /* 8 points around -a1 */ ring_points( fr->x[d->a1], &xvec[0], &yvec[0], &zvec[0], d->ring_dist, each_point,n); /* 8 points around -a2 */ ring_points( fr->x[d->a2], &xvec[0], &yvec[0], &zvec[0], d->ring_dist, each_point,n); std::vector<double> minv(3,10); std::vector<double> maxv(3,-10); } /* Find the dummy atom points */ /* midpoint xyz vectors */ double pzcomp[3], nzcomp[3], pycomp[3], nycomp[3], pxcomp[3], nxcomp[3]; /* -a1 xyz vectors */ double pzcomp1[3], nzcomp1[3], pycomp1[3], nycomp1[3], pxcomp1[3], nxcomp1[3]; /* -a2 xyz vectors */ double pzcomp2[3], nzcomp2[3], pycomp2[3], nycomp2[3], pxcomp2[3], nxcomp2[3]; for (int i=0; i<3; i++) { /* midpoint xyz vectors */ pzcomp[i] = midpoint[i] + zvec[i] * d->delta; nzcomp[i] = midpoint[i] - zvec[i] * d->delta; pycomp[i] = midpoint[i] + yvec[i] * d->delta; nycomp[i] = midpoint[i] - yvec[i] * d->delta; pxcomp[i] = midpoint[i] + xvec[i] * d->delta; nxcomp[i] = midpoint[i] - xvec[i] * d->delta; /* -a1 xyz vectors */ pzcomp1[i] = fr->x[d->a1][i] + zvec[i] * d->delta; nzcomp1[i] = fr->x[d->a1][i] - zvec[i] * d->delta; pycomp1[i] = fr->x[d->a1][i] + yvec[i] * d->delta; nycomp1[i] = fr->x[d->a1][i] - yvec[i] * d->delta; pxcomp1[i] = fr->x[d->a1][i] + xvec[i] * d->delta; nxcomp1[i] = fr->x[d->a1][i] - xvec[i] * d->delta; /* -a2 xyz vectors */ pzcomp2[i] = fr->x[d->a2][i] + zvec[i] * d->delta; nzcomp2[i] = fr->x[d->a2][i] - zvec[i] * d->delta; pycomp2[i] = fr->x[d->a2][i] + yvec[i] * d->delta; nycomp2[i] = fr->x[d->a2][i] - yvec[i] * d->delta; pxcomp2[i] = fr->x[d->a2][i] + xvec[i] * d->delta; nxcomp2[i] = fr->x[d->a2][i] - xvec[i] * d->delta; } /* Write all the dummy atoms to the pqr file */ if ( d->dopqr ) { write_dummy_atom( pqrout, nzcomp, "MIDz" ); write_dummy_atom( pqrout, pzcomp, "MIDz" ); write_dummy_atom( pqrout, nycomp, "MIDy" ); write_dummy_atom( pqrout, pycomp, "MIDy" ); write_dummy_atom( pqrout, nxcomp, "MIDx" ); write_dummy_atom( pqrout, pxcomp, "MIDx" ); write_dummy_atom( pqrout, nzcomp1, (std::string)*top->atoms.atomname[d->a1]+"z" ); write_dummy_atom( pqrout, pzcomp1, (std::string)*top->atoms.atomname[d->a1]+"z" ); write_dummy_atom( pqrout, nycomp1, (std::string)*top->atoms.atomname[d->a1]+"y" ); write_dummy_atom( pqrout, pycomp1, (std::string)*top->atoms.atomname[d->a1]+"y" ); write_dummy_atom( pqrout, nxcomp1, (std::string)*top->atoms.atomname[d->a1]+"x" ); write_dummy_atom( pqrout, pxcomp1, (std::string)*top->atoms.atomname[d->a1]+"x" ); write_dummy_atom( pqrout, nzcomp2, (std::string)*top->atoms.atomname[d->a2]+"z" ); write_dummy_atom( pqrout, pzcomp2, (std::string)*top->atoms.atomname[d->a2]+"z" ); write_dummy_atom( pqrout, nycomp2, (std::string)*top->atoms.atomname[d->a2]+"y" ); write_dummy_atom( pqrout, pycomp2, (std::string)*top->atoms.atomname[d->a2]+"y" ); write_dummy_atom( pqrout, nxcomp2, (std::string)*top->atoms.atomname[d->a2]+"x" ); write_dummy_atom( pqrout, pxcomp2, (std::string)*top->atoms.atomname[d->a2]+"x" ); if (d->site_ndx.size() > 0) { for (int i=0; i<nsites; i++) { std::stringstream key; key << "p" << i; write_dummy_atom( pqrout, &each_point[i][0], key.str()); } } } /* Initialize all the electrostatics, since they are the result of summing parts */ int nexclude = d->exclude_ndx.size(); dat.field_proj_total = 0.0f; dat.field_proj_each_exclude = std::vector<double> (nexclude,0.0f); dat.field_each_exclude = std::vector<std::vector<double> > (nexclude, std::vector<double> (3,0)); dat.field_proj_exclude = 0.0f; for (int i=0; i<3; i++){ dat.field_exclude[i] = 0.0f; dat.field_mid[i] = 0.0f; dat.field_a1[i] = 0.0f; dat.field_a2[i] = 0.0f; } dat.protein_site = std::vector<double> (nsites, 0.0f); dat.water_site = std::vector<double> (nsites, 0.0f); int g = 0; int nresidues = top->atoms.atom[sel[g]->g->index[sel[g]->p.nr]].resind; std::vector<float> field_per_residue (nresidues,0.0f); int nwater=0; /* Loop through everything */ for (int g = 0; g < nr; ++g) { // for each group if (g > 1) { fprintf(stderr,"\nWarning: More than 1 group was specified. Cowardly ignoring all additional groups\n"); break; } for (int i = 0; i < sel[g]->p.nr; ++i) { // for each atom in the group int atom_ndx = sel[g]->g->index[i]; // how we get to the atom index /* Get .dat parameters and write the atom to the .pqr file */ int resid = top->atoms.atom[atom_ndx].resind; double charge = top->atoms.atom[atom_ndx].q; char *atomname = *top->atoms.atomname[atom_ndx]; char *resname = *top->atoms.resinfo[resid].name; double radius = -1; int namber = d->amber.size(); for (int j=0; j<namber; j++) { if (d->bVerbose) { fprintf(stderr, "\nTrying to match: %s %s to %s (%d/%d)", resname, *top->atoms.atomtype[atom_ndx], d->amber[j].ambername, j, namber); } if ( strncmp(*top->atoms.atomtype[atom_ndx], d->amber[j].ambername, strlen(*top->atoms.atomtype[atom_ndx])+1) == 0 ) { radius = d->amber[j].radius; if (d->bVerbose) { fprintf(stderr,"\nMatched %s on %s to %s! (%d/%d)",resname, *top->atoms.atomtype[atom_ndx], d->amber[j].ambername, j, namber); } break; } } if (radius < 0) { fprintf(stderr,"\nError: Frame %d, could not match %s %s %s %i\n",d->framen, atomname, resname, *top->atoms.atomtype[atom_ndx],i+1); std::exit(1); } if ( d->dopqr ) { fprintf(pqrout,"ATOM %6i %4s %4s %5i %11.3f %7.3f %7.3f %7.4f %7.3f\n",atom_ndx+1,atomname,resname,resid+1,fr->x[atom_ndx][XX]*10,fr->x[atom_ndx][YY]*10,fr->x[atom_ndx][ZZ]*10,charge,radius); } /* Do the field calculations now */ bool keepAtom = true; std::vector<double> anatom (3,0.0f); calculate_field(midpoint, atom_ndx, *top, *fr, &anatom[0], 1); for (int j=0; j<nexclude; j++) { if (atom_ndx == d->exclude_ndx[j]) { keepAtom = false; break; } } if (keepAtom) { field_per_residue[resid] += d->rpdie * project_field(bondvector,&anatom[0]); } for (int j=0; j<3; j++) { dat.field_mid[j] += anatom[j]; } /* Get the field due to each excluded atom */ keepAtom = true; for (int j=0; j<nexclude ; j++) { if (atom_ndx == d->exclude_ndx[j]) { calculate_field(midpoint, d->exclude_ndx[j], *top, *fr, &dat.field_each_exclude[j][0], 1); keepAtom = false; break; } } if (keepAtom) { calculate_field(midpoint, atom_ndx, *top, *fr, dat.field_exclude, 1); calculate_field(fr->x[d->a1], atom_ndx, *top, *fr, dat.field_a1, 1); calculate_field(fr->x[d->a2], atom_ndx, *top, *fr, dat.field_a2, 1); } //d->field_per_residue[resid] += d->rpdie * project_field( /* Do the potential calculations now */ /* We want to exclude atoms at the sites */ /* We are also going to exclude atoms in the exclude group */ if ((int)d->site_ndx.size() > 0) { if (keepAtom) { for (int j=0; j<(int)d->site_ndx.size(); j++) { if (atom_ndx == d->site_ndx[j] ) { keepAtom = false; break; } } } if (keepAtom) { if (strncmp("SOL", resname, sizeof(resname)+1) == 0 || strncmp("HOH", resname, sizeof(resname)+1) == 0 ) { for (int j=0; j<nsites; j++) { double r = calculate_r( &each_point[j][0], atom_ndx, *top, *fr); dat.water_site[j] += calculate_potential( r, atom_ndx, *top, *fr); } nwater++; } else { for (int j=0; j<nsites; j++) { double r = calculate_r( &each_point[j][0], atom_ndx, *top, *fr); dat.protein_site[j] += calculate_potential( r, atom_ndx, *top, *fr); } } } } } } /* Write all output to xvg file */ if (d->fp) { fprintf(d->fp, "%.6e ", d->rpdie * project_field(bondvector, dat.field_mid)); for (int i=0; i<nexclude; i++) { fprintf(d->fp, "%.6e ", d->rpdie * project_field(bondvector, &dat.field_each_exclude[i][0])); } fprintf(d->fp, "%.6e ", d->rpdie * project_field(bondvector, dat.field_exclude)); fprintf(d->fp, "%.6e ", d->rpdie * project_field(xvec,dat.field_exclude)); fprintf(d->fp, "%.6e ", d->rpdie * project_field(yvec,dat.field_exclude)); fprintf(d->fp, "%.6e ", d->rpdie * project_field(zvec,dat.field_exclude)); fprintf(d->fp, "%.6e ", d->rpdie * project_field(xvec,dat.field_a1)); fprintf(d->fp, "%.6e ", d->rpdie * project_field(yvec,dat.field_a1)); fprintf(d->fp, "%.6e ", d->rpdie * project_field(zvec,dat.field_a1)); fprintf(d->fp, "%.6e ", d->rpdie * project_field(xvec,dat.field_a2)); fprintf(d->fp, "%.6e ", d->rpdie * project_field(yvec,dat.field_a2)); fprintf(d->fp, "%.6e ", d->rpdie * project_field(zvec,dat.field_a2)); if ((int)d->site_ndx.size() > 0) { for (int i=0; i<nsites; i++) { fprintf(d->fp, "%.6e %.6e ", d->rpdie * dat.protein_site[i], d->rpdie * dat.water_site[i]); } } fprintf(d->fp, "\n"); } /* close the pqr file */ if ( d->dopqr ) { fclose(pqrout); } /* Print the current frame number to the xvg file */ if (d->fpr) { fprintf(d->fpr, "%10i ", d->framen); for (int i=0;i<nresidues;i++) { fprintf(d->fpr,"%12.6e ",field_per_residue[i]); } fprintf(d->fpr,"\n"); } /* increment the frame number */ d->framen++; /* We need to return 0 to tell that everything went OK */ return 0; }
int main() { int k = 1, err = 10, iter = 0; float ratio = 0; int A[4][5] = { { 1, 1, 1, 1, 1}, { 1, 0, 0, 0, 1}, { 1, 1, 1, 1, 1}, { 1, 0, 0, 0, 1} }; int C[4][5] = { { 1, 1, 1, 1, 1}, { 1, 0, 0, 0, 0}, { 1, 0, 0, 0, 0}, { 1, 1, 1, 1, 1} }; int L[4][5]; int last_used_matrix[4][5] = { { -1, -1, -1, -1, -1}, { -1, -1, -1, -1, -1}, { -1, -1, -1, -1, -1}, { -1, -1, -1, -1, -1} }; t_neuron d_neuron = { .potential = 0, .threshold = 0.5, .output = 0 }; t_synapse synapses[20]; // Création du réseau for (int i = 0; i < 20; i++) { synapses[i].source = create_sensor(); synapses[i].target = &d_neuron; synapses[i].weight = 0; } // Phase d'apprentissage do { feed_neural_network(&synapses, A); learning_phase(&synapses, EPSILON, 1, 0); feed_neural_network(&synapses, C); learning_phase(&synapses, EPSILON, 0, 1); feed_neural_network(&synapses, A); calculate_potential(&synapses, &d_neuron); if (d_neuron.output != 1) err++; feed_neural_network(&synapses, C); calculate_potential(&synapses, &d_neuron); if (d_neuron.output != 0) err++; iter++; ratio = (float) iter / (float) err; //printf("iter: %d && err: %d\n", iter, err); //printf("ratio:%f\n", ratio); } while ( ratio < 0.8); // Phase de test pour A while (k < 20) { int i = 0; int success = 0; while (i < 100000) { while (true) { // Generer copie d'une lettre lcpy(&L, &A); // Generer du bruit sur cette copie generate_noise(&L, k); //print_letter(L); //printf("\n"); if (!matrices_are_equal(L, last_used_matrix)) { lcpy(&last_used_matrix, &L); break; } } feed_neural_network(&synapses, L); calculate_potential(&synapses, &d_neuron); if (d_neuron.output == 1) success++; i++; } success /= 1000; printf("%d%% de succes sur %d essais avec %d%% de bruit.\n", success, i, k*5); k++; } // Phase de test pour C k = 1; while (k < 20) { int i = 0; int success = 0; while (i < 100000) { while (true) { // Generer copie d'une lettre lcpy(&L, &C); // Generer du bruit sur cette copie generate_noise(&L, k); if (!matrices_are_equal(L, last_used_matrix)) { lcpy(&last_used_matrix, &L); break; } } feed_neural_network(&synapses, L); calculate_potential(&synapses, &d_neuron); if (d_neuron.output == 0) success++; i++; } success /= 1000; printf("%d%% de succes sur %d essais avec %d%% de bruit.\n", success, i, k*5); k++; } }