double ang_between(double x1[3], double x2[3]) { double mydot = dot3(x1, x2); double mag1 = mag3(x1); double mag2 = mag3(x2); double cos_ang = mydot/mag1/mag2; if (cos_ang > 1) cos_ang = 1; if (cos_ang < -1) cos_ang = -1; return acos(cos_ang); }
int main(int argc, char* argv[]) { // Load the mesh. Mesh mesh; H2DReader mloader; mloader.load("domain.mesh", &mesh); // Initial mesh refinements. for(int i = 0; i < INIT_REF_NUM; i++) mesh.refine_all_elements(); // Create an H1 space. H1Space* phi_space = new H1Space(&mesh, bc_types, essential_bc_values, P_INIT); H1Space* psi_space = new H1Space(&mesh, bc_types, essential_bc_values, P_INIT); int ndof = get_num_dofs(Tuple<Space *>(phi_space, psi_space)); info("ndof = %d.", ndof); // Initialize previous time level solutions. Solution phi_prev_time, psi_prev_time; phi_prev_time.set_exact(&mesh, init_cond_phi); psi_prev_time.set_exact(&mesh, init_cond_psi); // Initialize the weak formulation. WeakForm wf(2); wf.add_matrix_form(0, 0, callback(biform_euler_0_0)); wf.add_matrix_form(0, 1, callback(biform_euler_0_1)); wf.add_matrix_form(1, 0, callback(biform_euler_1_0)); wf.add_matrix_form(1, 1, callback(biform_euler_1_1)); wf.add_vector_form(0, callback(liform_euler_0), H2D_ANY, &phi_prev_time); wf.add_vector_form(1, callback(liform_euler_1), H2D_ANY, &psi_prev_time); // Time stepping loop: int nstep = T_FINAL; for(int ts = 1; ts <= nstep; ts++) { info("Time step %d:", ts); // Newton's method. info("Solving linear system."); Solution phi, psi; bool is_complex = true; if (!solve_linear(Tuple<Space *>(phi_space, psi_space), &wf, matrix_solver, Tuple<Solution *>(&phi, &psi), NULL, is_complex)) error("Linear solve failed."); // Update previous time level solution. phi_prev_time.copy(&phi); psi_prev_time.copy(&psi); } AbsFilter mag2(&psi_prev_time); AbsFilter mag3(&phi_prev_time); #define ERROR_SUCCESS 0 #define ERROR_FAILURE -1 int success = 1; double eps = 1e-5; double val = std::abs(mag2.get_pt_value(0.0, 0.0)); info("Coordinate ( 0, 0) psi value = %lf", std::abs(mag2.get_pt_value(0.0, 0.0))); if (fabs(val - (0.000008)) > eps) { printf("Coordinate ( 0, 0) psi value = %lf\n", val); success = 0; } val = std::abs(mag2.get_pt_value(-0.5, -0.5)); info("Coordinate (-0.5,-0.5) psi value = %lf", std::abs(mag2.get_pt_value(-0.5, -0.5))); if (fabs(val - (0.000004)) > eps) { printf("Coordinate (-0.5,-0.5) psi value = %lf\n", val); success = 0; } val = std::abs(mag2.get_pt_value(0.5, -0.5)); info("Coordinate ( 0.5,-0.5) psi value = %lf", std::abs(mag2.get_pt_value(0.5, -0.5))); if (fabs(val - (0.000004)) > eps) { printf("Coordinate ( 0.5,-0.5) psi value = %lf\n", val); success = 0; } val = std::abs(mag2.get_pt_value(0.5, 0.5)); info("Coordinate ( 0.5, 0.5) psi value = %lf", std::abs(mag2.get_pt_value(0.5, 0.5))); if (fabs(val - (0.000004)) > eps) { printf("Coordinate ( 0.5, 0.5) psi value = %lf\n", val); success = 0; } val = std::abs(mag2.get_pt_value(-0.5, 0.5)); info("Coordinate (-0.5, 0.5) psi value = %lf", std::abs(mag2.get_pt_value(-0.5, 0.5))); if (fabs(val - (0.000004)) > eps) { printf("Coordinate (-0.5, 0.5) psi value = %lf\n", val); success = 0; } val = std::abs(mag3.get_pt_value(0.0, 0.0)); info("Coordinate ( 0, 0) phi value = %lf", std::abs(mag3.get_pt_value(0.0, 0.0))); if (fabs(val - (0.000003)) > eps) { printf("Coordinate ( 0, 0) phi value = %lf\n", val); success = 0; } val = std::abs(mag3.get_pt_value(-0.5, -0.5)); info("Coordinate (-0.5,-0.5) phi value = %lf", std::abs(mag3.get_pt_value(-0.5, -0.5))); if (fabs(val - (0.000001)) > eps) { printf("Coordinate (-0.5,-0.5) phi value = %lf\n", val); success = 0; } val = std::abs(mag3.get_pt_value(0.5, -0.5)); info("Coordinate ( 0.5,-0.5) phi value = %lf", std::abs(mag3.get_pt_value(0.5, -0.5))); if (fabs(val - (0.000001)) > eps) { printf("Coordinate ( 0.5,-0.5) phi value = %lf\n", val); success = 0; } val = std::abs(mag3.get_pt_value(0.5, 0.5)); info("Coordinate ( 0.5, 0.5) phi value = %lf", std::abs(mag3.get_pt_value(0.5, 0.5))); if (fabs(val - (0.000001)) > eps) { printf("Coordinate ( 0.5, 0.5) phi value = %lf\n", val); success = 0; } val = std::abs(mag3.get_pt_value(-0.5, 0.5)); info("Coordinate (-0.5, 0.5) phi value = %lf", std::abs(mag3.get_pt_value(-0.5, 0.5))); if (fabs(val - (0.000001)) > eps) { printf("Coordinate (-0.5, 0.5) phi value = %lf\n", val); success = 0; } if (success == 1) { printf("Success!\n"); return ERROR_SUCCESS; } else { printf("Failure!\n"); return ERROR_FAILURE; } }
void normalize(double y[3], double x[3]) { double mymag = mag3(x); for (unsigned int i = 0; i < 3; i++) y[i] = y[i]/mymag; }
int main(int argc, char* argv[]) { // Load the mesh. Mesh mesh; H2DReader mloader; mloader.load("domain.mesh", &mesh); // Initial mesh refinements. for(int i = 0; i < INIT_REF_NUM; i++) mesh.refine_all_elements(); // Enter boundary markers. BCTypes bc_types; bc_types.add_bc_dirichlet(Hermes::Tuple<int>(BDY_BOTTOM, BDY_RIGHT, BDY_TOP, BDY_LEFT)); // Enter Dirichlet boundary values. BCValues bc_values; bc_values.add_zero(Hermes::Tuple<int>(BDY_BOTTOM, BDY_RIGHT, BDY_TOP, BDY_LEFT)); // Create an H1 space. H1Space* phi_space = new H1Space(&mesh, &bc_types, &bc_values, P_INIT); H1Space* psi_space = new H1Space(&mesh, &bc_types, &bc_values, P_INIT); int ndof = Space::get_num_dofs(Hermes::Tuple<Space *>(phi_space, psi_space)); info("ndof = %d.", ndof); // Initialize previous time level solutions. Solution phi_prev_time, psi_prev_time; phi_prev_time.set_exact(&mesh, init_cond_phi); psi_prev_time.set_exact(&mesh, init_cond_psi); // Initialize the weak formulation. WeakForm wf(2); wf.add_matrix_form(0, 0, callback(biform_euler_0_0)); wf.add_matrix_form(0, 1, callback(biform_euler_0_1)); wf.add_matrix_form(1, 0, callback(biform_euler_1_0)); wf.add_matrix_form(1, 1, callback(biform_euler_1_1)); wf.add_vector_form(0, callback(liform_euler_0), HERMES_ANY, &phi_prev_time); wf.add_vector_form(1, callback(liform_euler_1), HERMES_ANY, &psi_prev_time); // Time stepping loop: int nstep = T_FINAL; for(int ts = 1; ts <= nstep; ts++) { info("Time step %d:", ts); info("Solving linear system."); // Initialize the FE problem. bool is_linear = true; DiscreteProblem dp(&wf, Hermes::Tuple<Space *>(phi_space, psi_space), is_linear); SparseMatrix* matrix = create_matrix(matrix_solver); Vector* rhs = create_vector(matrix_solver); Solver* solver = create_linear_solver(matrix_solver, matrix, rhs); // Assemble the stiffness matrix and right-hand side vector. info("Assembling the stiffness matrix and right-hand side vector."); dp.assemble(matrix, rhs); // Solve the linear system and if successful, obtain the solution. info("Solving the matrix problem."); if(solver->solve()) Solution::vector_to_solutions(solver->get_solution(), Hermes::Tuple<Space *>(phi_space, psi_space), Hermes::Tuple<Solution *>(&phi_prev_time, &psi_prev_time)); else error ("Matrix solver failed.\n"); } AbsFilter mag2(&psi_prev_time); AbsFilter mag3(&phi_prev_time); int success = 1; double eps = 1e-5; double val = std::abs(mag2.get_pt_value(0.0, 0.0)); info("Coordinate ( 0, 0) psi value = %lf", std::abs(mag2.get_pt_value(0.0, 0.0))); if (fabs(val - (0.000008)) > eps) { printf("Coordinate ( 0, 0) psi value = %lf\n", val); success = 0; } val = std::abs(mag2.get_pt_value(-0.5, -0.5)); info("Coordinate (-0.5,-0.5) psi value = %lf", std::abs(mag2.get_pt_value(-0.5, -0.5))); if (fabs(val - (0.000004)) > eps) { printf("Coordinate (-0.5,-0.5) psi value = %lf\n", val); success = 0; } val = std::abs(mag2.get_pt_value(0.5, -0.5)); info("Coordinate ( 0.5,-0.5) psi value = %lf", std::abs(mag2.get_pt_value(0.5, -0.5))); if (fabs(val - (0.000004)) > eps) { printf("Coordinate ( 0.5,-0.5) psi value = %lf\n", val); success = 0; } val = std::abs(mag2.get_pt_value(0.5, 0.5)); info("Coordinate ( 0.5, 0.5) psi value = %lf", std::abs(mag2.get_pt_value(0.5, 0.5))); if (fabs(val - (0.000004)) > eps) { printf("Coordinate ( 0.5, 0.5) psi value = %lf\n", val); success = 0; } val = std::abs(mag2.get_pt_value(-0.5, 0.5)); info("Coordinate (-0.5, 0.5) psi value = %lf", std::abs(mag2.get_pt_value(-0.5, 0.5))); if (fabs(val - (0.000004)) > eps) { printf("Coordinate (-0.5, 0.5) psi value = %lf\n", val); success = 0; } val = std::abs(mag3.get_pt_value(0.0, 0.0)); info("Coordinate ( 0, 0) phi value = %lf", std::abs(mag3.get_pt_value(0.0, 0.0))); if (fabs(val - (0.000003)) > eps) { printf("Coordinate ( 0, 0) phi value = %lf\n", val); success = 0; } val = std::abs(mag3.get_pt_value(-0.5, -0.5)); info("Coordinate (-0.5,-0.5) phi value = %lf", std::abs(mag3.get_pt_value(-0.5, -0.5))); if (fabs(val - (0.000001)) > eps) { printf("Coordinate (-0.5,-0.5) phi value = %lf\n", val); success = 0; } val = std::abs(mag3.get_pt_value(0.5, -0.5)); info("Coordinate ( 0.5,-0.5) phi value = %lf", std::abs(mag3.get_pt_value(0.5, -0.5))); if (fabs(val - (0.000001)) > eps) { printf("Coordinate ( 0.5,-0.5) phi value = %lf\n", val); success = 0; } val = std::abs(mag3.get_pt_value(0.5, 0.5)); info("Coordinate ( 0.5, 0.5) phi value = %lf", std::abs(mag3.get_pt_value(0.5, 0.5))); if (fabs(val - (0.000001)) > eps) { printf("Coordinate ( 0.5, 0.5) phi value = %lf\n", val); success = 0; } val = std::abs(mag3.get_pt_value(-0.5, 0.5)); info("Coordinate (-0.5, 0.5) phi value = %lf", std::abs(mag3.get_pt_value(-0.5, 0.5))); if (fabs(val - (0.000001)) > eps) { printf("Coordinate (-0.5, 0.5) phi value = %lf\n", val); success = 0; } if (success == 1) { printf("Success!\n"); return ERR_SUCCESS; } else { printf("Failure!\n"); return ERR_FAILURE; } }