void calc_f_dev(int natoms,real charge[],rvec x[],rvec f[], t_idef *idef,real *xiH,real *xiS) { enum { wwwO, wwwH1, wwwH2, wwwS, wwwNR }; int lj_index[wwwNR] = { 0, 4, 4, 8 }; real rmsf,dFH,dFS,dFO,dr_14; real q[wwwNR],c6[wwwNR],c12[wwwNR],c12ratio; rvec fff[2],dx; int i,j,aj; for(i=0; (i<wwwNR); i++) { q[i] = charge[i]; c12[i] = idef->iparams[lj_index[i]].lj.c12; c6[i] = idef->iparams[lj_index[i]].lj.c6; } calc_force(natoms,f,fff); rmsf = norm(fff[0]); dFS = 0; dFH = 0; dFO = 0; for(i=0; (i<4); i++) { for(j=4; (j<8); j++) { if (c12[i] != 0) { rvec_sub(x[i],x[j],dx); aj = j % 4; dr_14 = pow(iprod(dx,dx),-7); c12ratio = -12*sqrt(c12[aj]/c12[i])*dr_14*iprod(fff[0],dx); switch (i) { case wwwH1: case wwwH2: dFH += c12ratio; break; case wwwS: dFS += c12ratio; break; case wwwO: dFS += c12ratio; break; } } } } if (debug) { fprintf(debug,"FFF: dFS=%10.3e, dFH=%10.3e, dFO=%10.3e, rmsf=%10.3e\n", dFS,dFH,dFO,rmsf); } if (dFH == 0) *xiH = 1; else *xiH=rmsf/(10*c12[wwwH1]*dFH); if (dFS == 0) *xiS = 1; else *xiS=rmsf/(10*c12[wwwS]*dFS); }
/* Utility Function: cell_neighbor * Calculate forces between all atoms of cell1 and cell2 * and return potential energy */ double calc_pair_interactions(Cell* cell1, Cell* cell2, const SimParams* params) { double potentialEnergy = 0.0; int i,j; for(i=0;i<cell1->n_atoms;i++) for(j=0;j<cell2->n_atoms;j++) { potentialEnergy += calc_force(&(cell1->atoms[i]), &(cell2->atoms[j]), params); } return potentialEnergy; }
/*Utility Function: calc_self_interactions */ double calc_self_interactions(Cell* this_cell, const SimParams* params) { double potentialEnergy = 0.0; int i,j; for(i=0;i<this_cell->n_atoms;i++) for(j=i+1;j<this_cell->n_atoms;j++) { potentialEnergy += calc_force(&(this_cell->atoms[i]), &(this_cell->atoms[j]), params); } return potentialEnergy; }
int main(int argc, char** argv) { const char filename[]="10.txt"; // Determine the number of atoms Natoms = get_natoms(filename); // Initialize the positions ! init(Natoms, filename); // Loop over timesteps std::ofstream simFile,enerFile; simFile.open("LDmj_sim.xyz"); enerFile.open("LDmj_sim.ener"); enerFile << "Time step" << std::setw(15) << "time" << std::setw(15) << "PE" << std::setw(15) << "KE" << std::setw(15) << "TE" << std::setw(15) << "Px" << std::setw(15) << "Py" << std::setw(15) << "Pz" << std::setw(15) << std::endl; for(int k=0; k<=1000; k++) { elapsed_time = dt*double(k); // Calculate pair-energy and forces if(k==0) calc_force(); calc_kenergy(); TE=U+KE; std::cout << std::setw(8) << k << std::setw(15) << elapsed_time << std::setw(15) << U << std::setw(15) << KE << std::setw(15) << TE << std::setw(15) << px << std::setw(15) << py << std::setw(15) << pz << std::setw(15) << std::endl; calc_momentum(); write_xyz(simFile, k); dump_stats(enerFile, k); // parameters are computed for (i+1) vv_scheme(); } simFile.close(); enerFile.close(); return 0; }
void vv_scheme() { double tfact; // TODO : Perform openMP. // reinitialize all the temp variables to zero. for(int i=0; i<Natoms; i++){ r_old[i][0] = r[i][0]; r_old[i][1] = r[i][1]; r_old[i][2] = r[i][2]; v_old[i][0] = v[i][0]; v_old[i][1] = v[i][1]; v_old[i][2] = v[i][2]; f_old[i][0] = f[i][0]; f_old[i][1] = f[i][1]; f_old[i][2] = f[i][2]; } for(int i=0; i<Natoms; i++){ tfact = dt/(2.0*m[i]); // Step 1. v(t+0.5dt) v[i][0] = v_old[i][0] + f_old[i][0]*tfact; v[i][1] = v_old[i][1] + f_old[i][1]*tfact; v[i][2] = v_old[i][2] + f_old[i][2]*tfact; v_old[i][0] = v[i][0]; v_old[i][1] = v[i][1]; v_old[i][2] = v[i][2]; // Step 2. r(t+dt) r[i][0] = r_old[i][0] + v_old[i][0]*dt; r[i][1] = r_old[i][1] + v_old[i][1]*dt; r[i][2] = r_old[i][2] + v_old[i][2]*dt; } calc_pairenergy(); calc_force(); for(int i=0; i<Natoms; i++){ // Step 3. v(t+dt) v[i][0] = v_old[i][0] + f[i][0]*tfact; v[i][1] = v_old[i][1] + f[i][1]*tfact; v[i][2] = v_old[i][2] + f[i][2]*tfact; } }
void run_simulation(simulation_t *simulation) { collide_particles(simulation->src_buf, simulation->count); // iterate through each particle and addup all forces for (unsigned int i = 0; i < simulation->count; i++) { particle_t *a = &simulation->src_buf[i]; particle_t *a2 = &simulation->dst_buf[i]; vec3f forces; forces.x = forces.y = forces.z = 0; for (unsigned int j = 0; j < simulation->count; j++) { if (i != j) { particle_t *b = &simulation->src_buf[j]; forces = vec_add(forces, calc_force(a, b)); } } // a = F / m vec3f acc = vec_scale(forces, 1 / a->mass); integrate(a, a2, acc, simulation->dt); } // swap buffers particle_t *tmp = simulation->src_buf; simulation->src_buf = simulation->dst_buf; simulation->dst_buf = tmp; }