// // Program: capacitor.cxx // // Description: Evaluate the potential and electric fields for a parallel // plate condenser inside a grounded box. // // Author: A. Garfagnini // // Date: 17 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 x_limits[] = {-2.0, 2.0}; double y_limits[] = {-2.0, 2.0}; double dx = (x_limits[1] - x_limits[0])/(N+1); // Create a matrix for the grid int M = N+1; double * V = new double[M*M]; double * Vnew = new double[M*M]; // Init matrix with fixed potential plates for (int i=0; i> 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; // Copy the Potential to the buffer and print it on the screen copy_matrix(V, Vnew, M); if (debug) print_matrix(myout, V, M); int iter = 0; double dv_global = 0.0; int grid_points = 0; do { iter++; dv_global = 0.0; grid_points = 0; // - Evaluate the new field for (int i=1; i 0.0) { grid_points ++; dv_global += delta; } } } // Re-Set the fixed potential plates at x = -1 (V = +1) int ix = (-1.0 - x_limits[0])/dx; int jy_low = (-1.0 - y_limits[0])/dx; int jy_hig = (+1.0 - y_limits[0])/dx; for (int j=jy_low; j dv_prec); if (electric_field) { myout << "# Final Electric field\n"; print_efield(myout, Vnew, M, x_limits[0], y_limits[0], dx); } else if (final_matrix) { myout << "# Final Potential\n"; print_potential(myout, Vnew, M, x_limits[0], y_limits[0], dx); } if (out_file.is_open()) { out_file.close(); } return 0; }