// // Program: cond_piano.cxx // // Description: Evaluate the potential and electric fields for a parallel // plate condenser. // // Author: A. Garfagnini // // Date: 16 October 2013 // #include #include #include #include #include #include #include using namespace std; bool debug = false; void usage(const char * pname) { cerr << "Usage: " << pname << " [-d] [-o file_name]" << endl; cerr << "\nEvaluate the Potential for a parallel plate condenser"; cerr << "\nusing the Gauss-Seidel method\n\nOptions:"; cerr << "\n -d : enable debug printout"; cerr << "\nOther options:\n"; cerr << " -o file_name : save output in a user definable file name\n"; } inline int index(int i, int j, int n) { return i + n*j; } void print_efield(ostream & out, double * A, int n, double xmin, double ymin, double delta) { double E_x = 0.0; double E_y = 0.0; double x = xmin; double y = ymin; for (int i=0; i> N; double a = -1.0; double b = +1.0; double dx = (b - a)/(N+1); if (out_file.is_open()) { out_file << "# Grid Division: " << N << endl; out_file << "# Grid Step: " << dx << " from " << a << " to " << b << endl; } cout << "Delta_V accuracy to stop computation: "; double dv_prec; cin >> dv_prec; if (out_file.is_open()) { out_file << "# Delta_V accuracy to stop computation: " << dv_prec << endl; } ostream & myout = (out_file.is_open()) ? out_file : cout; // Create a matrix for the grid int M = N+1; double * V = new double[M*M]; double * Vnew = new double[M*M]; // - Init the system: set the starting conditions for (int i=0; i 0.0) { grid_points ++; dv_global += delta; } } } if (debug) { cout << endl; cout << "Grid points: " << grid_points << endl; cout << "DeltaV_global: " << dv_global / grid_points << endl; } if (time_accuracy) { myout << iter << " " << dv_global << " " << grid_points << " " << dv_global / grid_points << " " << dv_prec << endl; } if (debug) print_matrix(myout, Vnew, M); copy_matrix(Vnew, V, M); } while (dv_global / grid_points > dv_prec); if (electric_field) { myout << "# Final Electric field\n"; print_efield(myout, Vnew, M, a, a, dx); } else if (final_matrix) { myout << "# Final Potential\n"; print_potential(myout, Vnew, M, a, a, dx); } if (out_file.is_open()) { out_file.close(); } return 0; }