void LaxFriedrichsStep() { int i, j; double rho, u, e, p; // compute flux F from U for (j = 0; j < N; j++) { rho = U[j][0]; u = U[j][1]/U[j][0]; e = U[j][2]; p = (gama - 1) * (e - rho * u * u/ 2); F[j][0] = rho * u; F[j][1] = rho * u * u + p; F[j][2] = (e + p) * u; } // Lax-Friedrichs step for (j = 1; j < N - 1; j++) for (i = 0; i < 3; i++){ newU[j][i] = (U[j + 1][i] + U[j - 1][i]) / 2 - tau / h * (F[j + 1][i] - F[j - 1][i]); } boundaryConditions(newU); // update U from newU for (j = 1; j < N - 1; j++) for (i = 0; i < 3; i++){ U[j][i] = newU[j][i]; } }
void solve(solver stepAlgorithm, double tMax, char *filename, int plots) { initialize(); double t = 0.0; int step = 0; int plot = 0; FILE *out; char filename_tmp[1024]; int j; double rho_avg = 0.0, u_avg = 0.0, e_avg = 0.0, P_avg = 0.0; double rho, u, e, P; tau = CFL * h / cMax(); while(plot<=plots) { sprintf(filename_tmp, "%s_step_%d.dat", filename, plot); if(!(out = fopen(filename_tmp, "w"))){ fprintf(stderr, "problem opening file %s\n", filename); exit(1); } // write solution in plot files and print double rho_avg = 0.0, u_avg = 0.0, e_avg = 0.0, P_avg = 0.0; for (j = 0; j < N; j++) { rho = U[j][0]; u = U[j][1] / U[j][0]; e = U[j][2]; P = (U[j][2] - U[j][1] * U[j][1] / U[j][0] / 2) * (gama - 1.0); rho_avg += rho; u_avg += u; e_avg += e; P_avg += P; fprintf(out, "%d\t%f\t%f\t%f\t%f\n", j, rho, u, e, P); } fclose(out); if (rho_avg != 0.0){ rho_avg /= N;} if (u_avg != 0.0){ u_avg /= N;} if (e_avg != 0.0){ e_avg /= N;} if (P_avg != 0.0){ P_avg /= N;} fprintf(stdout,"Step %d Time %f\tRho_avg %f\t u_avg %f\t e_avg %f\t P_avg %f\n", step, t,rho_avg,u_avg,e_avg,P_avg); plot++; while (t < tMax * plot / (double)(plots)) { boundaryConditions(U); tau = CFL * h / cMax(); stepAlgorithm(); t += tau; step++; } } }
int main(int argc, char** argv) { int nIterations = 10000; if (argc > 1) { nIterations = atoi(argv[1]); } Lattice lattice(4, 8, 5.5, 1.0, 1.0, 1.0, 0, 10, 0, 1, 4, -1); vector<complex<double> > boundaryConditions(4, complex<double>(1.0, 0.0)); DWF linop(0.4, 1.8, 4, pyQCD::wilson, boundaryConditions, &lattice); VectorXcd psi = VectorXcd::Zero(4 * 12 * 4 * 4 * 4 * 8); psi(0) = 1.0; std::cout << "Performing " << nIterations << " matrix-vector products." << std::endl; boost::timer::cpu_timer timer; for (int i = 0; i < nIterations; ++i) { VectorXcd eta = linop.apply(psi); } boost::timer::cpu_times const elapsedTimes(timer.elapsed()); boost::timer::nanosecond_type const elapsed(elapsedTimes.system + elapsedTimes.user); boost::timer::nanosecond_type const walltime(elapsedTimes.wall); std::cout << "Total CPU time = " << elapsed / 1.0e9 << " s" << endl; std::cout << "CPU time per iteration = " << elapsed / 1.0e9 / nIterations << " s" << endl; std::cout << "Walltime = " << walltime / 1.0e9 << " s" << endl; std::cout << "Walltime per iteration = " << walltime / 1.0e9 / nIterations << " s" << endl; std::cout << "Performance: " << linop.getNumFlops() << " floating point operations; " << (double) linop.getNumFlops() / elapsed * 1000.0 << " MFlops / thread" << endl; return 0; }
void solve(double tMax) { initialize(); double t = 0.0; double rho, u, e, P; tau = CFL*h/cMax(); while (t < tMax) { boundaryConditions(U); tau = CFL*h/cMax(); upwindGodunovStep(); t += tau; } //File to plot last step FILE *final_step_file; final_step_file = fopen("final_step.dat", "w"); double current_x; int j; for (j = 0; j < N; j++) { rho = U[j][0]; u = U[j][1]/rho; e = U[j][2]; P = (gama-1.0)*(e-rho*u*u/2.0); current_x = j*h; fprintf(final_step_file, "%f\t%.20f\t%.20f\t%.20f\t%.20f\n", current_x, rho, u, e, P); } }