/* An example of a program solving -div(k grad u) + cu = f in a 2D domain using linear finite elements. */ #include #include #include "math.h" //Include our own previously written code... // in general we will be using iterative solvers, // but for this particular example (since we're // assembling the full global matrix) we'll use // the Gaussian Elimination direct solver that // we have written for Math609. #include "gaussian.cc" //Include the header file defining the different // data structures that we use. #include "2d_FE.h" // Include a file that deals with getting the data // for the mesh from a few files. #include "readmesh.cc" // Include a file with functions describing the linear // finite elements in 2D. #include "2d_linear.cc" // Include a file with various quadratures. Currently only // a 3 point rule with nodes at the midpoints is implemented. // This rule is exact for polynomials of degree 2 and thus // is good enough when working with linear elements. #include "quadrature_2d.cc" /************************* The Coefficients ************************/ #define PI M_PI double k(Point p) { return 1; } double c(Point p) { return 5; } double f(Point p) { return (5 + 18*PI*PI)*cos(3*PI*p.x)*cos(3*PI*p.y); } // an expected solution (used to check our approximation) double sol(Point p) { return cos(3*PI*p.x)*cos(3*PI*p.y); } /************************* The Main Code ************************/ int main() { std::cout << "Reading the mesh..." << std::endl; if(!read_mesh("data.1")) { std::cout << "Error reading the mesh..." << std::endl; exit(1); } int global_dofs = num_nodes; double** A = new double*[global_dofs]; double* b = new double[global_dofs]; Create_Quadratures(); Quadrature *quad = &MIDPOINTS_RULE; //create the empty matrix and rhs for (int i=0; isize; l++) { // get the values of the point and the weight Point q_point = map_to_FE(quad->points[l], ele); double q_weight = quad->weights[l]; for (int i=0; i