/* An example of a program solving -div(k grad u) + cu = f in a 2D domain using linear finite elements. */ #include #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 "cg.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 CG Code ************************/ // I've hardcoded the number 3 here. In general this should be a // class, and the dimension of the matrix and the vector should // be num_dofs = dofs_per_element(); typedef struct { double LM[3][3]; //local matrix int DM[3]; //DoF mapping } LocalMatrix; LocalMatrix *local_matrices; //Calculate the action of our matrix A (for which // we're solving Ax=b) on the given vector 'in' and // saves the result in the given vector 'out' void myMatrix(double* out, double* in, int n) { 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=0) { for (int j=0; j