TEST(MaterialLibNewtonRaphson, Sqrt3) { static const int N = 1; // Problem's size. using LocalJacobianMatrix = Eigen::Matrix<double, N, N, Eigen::RowMajor>; using LocalResidualVector = Eigen::Matrix<double, N, 1>; Eigen::PartialPivLU<LocalJacobianMatrix> linear_solver(N); LocalJacobianMatrix jacobian; const int maximum_iterations = 5; const double tolerance = 1e-15; // Solve f(x) = x^2 - S == 0, where S = 3. // // Initial value double state = 1; auto const update_jacobian = [&state](LocalJacobianMatrix& jacobian) { jacobian(0, 0) = 2 * state; }; auto const update_residual = [&state](LocalResidualVector& residual) { residual[0] = state * state - 3; }; auto const update_solution = [&state]( LocalResidualVector const& increment) { state += increment[0]; }; auto const newton_solver = MaterialLib::Solids::NewtonRaphson< decltype(linear_solver), LocalJacobianMatrix, decltype(update_jacobian), LocalResidualVector, decltype(update_residual), decltype(update_solution)>(linear_solver, update_jacobian, update_residual, update_solution, maximum_iterations, tolerance); auto const success_iterations = newton_solver.solve(jacobian); EXPECT_TRUE(static_cast<bool>(success_iterations)); EXPECT_LE(*success_iterations, maximum_iterations); ASSERT_LE(state - std::sqrt(3), std::numeric_limits<double>::epsilon()); }
void OGProjection<Scalar>::project_internal(SpaceSharedPtr<Scalar> space, WeakForm<Scalar>* wf, Scalar* target_vec) { // Sanity check. if(wf == nullptr) throw Hermes::Exceptions::NullException(1); if(target_vec == nullptr) throw Exceptions::NullException(2); // Initialize DiscreteProblem. DiscreteProblem<Scalar> dp(wf, space); dp.set_linear(); // Initialize linear solver. Hermes::Hermes2D::LinearSolver<Scalar> linear_solver(&dp, true); linear_solver.set_verbose_output(false); // Perform Newton iteration. linear_solver.solve(); memcpy(target_vec, linear_solver.get_sln_vector(), space->get_num_dofs() * sizeof(Scalar)); }
int main(int argc, char* args[]) { // Load the mesh-> HermesSharedPtr mesh; MeshReaderH2D mloader; mloader.load("../square.mesh", mesh); // Perform initial mesh refinement. for (int i=0; i<INIT_REF; i++) mesh->refine_all_elements(); // Create an L2 space-> L2Space<double> space(mesh, P_INIT); // Initialize refinement selector. L2ProjBasedSelector<double> selector(CAND_LIST, CONV_EXP, H2DRS_DEFAULT_ORDER); // Disable weighting of refinement candidates. selector.set_error_weights(1, 1, 1); Solution<double> sln; Solution<double> ref_sln; // Initialize the weak formulation. CustomWeakForm wf("Bdy_bottom_left", mesh); // Initialize the FE problem. DiscreteProblemLinear<double> dp(&wf, space); // Initialize linear solver. Hermes::Hermes2D::LinearSolver<double> linear_solver(&dp); int as = 1; bool done = false; do { // Construct globally refined reference mesh // and setup reference space-> Mesh::ReferenceMeshCreator ref_mesh_creator(mesh); Mesh* ref_mesh = ref_mesh_creator.create_ref_mesh(); Space<double>::ReferenceSpaceCreator ref_space_creator(space, ref_mesh); Space<double>* ref_space = ref_space_creator.create_ref_space(); dp.set_space(ref_space); // Solve the linear system. If successful, obtain the solution. try { linear_solver.solve(); Solution<double>::vector_to_solution(linear_solver.get_sln_vector(), ref_space, &ref_sln); } catch(std::exception& e) { std::cout << e.what(); } // Project the fine mesh solution onto the coarse mesh-> OGProjection<double> ogProjection; ogProjection.project_global(space, &ref_sln, &sln, HERMES_L2_NORM); // Calculate element errors and total error estimate. Adapt<double>* adaptivity = new Adapt<double>(space); double err_est_rel = adaptivity->calc_err_est(&sln, &ref_sln) * 100; // If err_est_rel too large, adapt the mesh-> if(err_est_rel < ERR_STOP) done = true; else { done = adaptivity->adapt(&selector, THRESHOLD, STRATEGY, MESH_REGULARITY); if(Space<double>::get_num_dofs(space) >= NDOF_STOP) { done = true; break; } } // Clean up. delete adaptivity; if(done == false) delete ref_space->get_mesh(); delete ref_space; as++; } while (done == false); if(done) { printf("Success!\n"); return 0; } else { printf("Failure!\n"); return -1; } }
int main(int argc, char* args[]) { // Load the mesh. MeshSharedPtr mesh(new Mesh); MeshReaderH2D mloader; mloader.load("square.mesh", mesh); // Perform initial mesh refinement. for (int i=0; i<INIT_REF; i++) mesh->refine_all_elements(); // Create an L2 space-> SpaceSharedPtr<double> space(new L2Space<double>(mesh, P_INIT)); // Initialize refinement selector. L2ProjBasedSelector<double> selector(CAND_LIST); // Display the mesh. #ifdef SHOW_OUTPUT OrderView oview("Coarse mesh", new WinGeom(0, 0, 440, 350)); oview.show(space); #endif MeshFunctionSharedPtr<double> sln(new Solution<double>); MeshFunctionSharedPtr<double> ref_sln(new Solution<double>); // Initialize the weak formulation. CustomWeakForm wf("Bdy_bottom_left", mesh); #ifdef SHOW_OUTPUT ScalarView view1("Solution", new WinGeom(900, 0, 450, 350)); view1.fix_scale_width(60); #endif // Initialize linear solver. Hermes::Hermes2D::LinearSolver<double> linear_solver(&wf, space); int as = 1; bool done = false; do { // Construct globally refined reference mesh // and setup reference space-> Mesh::ReferenceMeshCreator ref_mesh_creator(mesh); MeshSharedPtr ref_mesh = ref_mesh_creator.create_ref_mesh(); Space<double>::ReferenceSpaceCreator ref_space_creator(space, ref_mesh); SpaceSharedPtr<double> ref_space = ref_space_creator.create_ref_space(); ref_space->save("space-real.xml"); ref_space->free(); ref_space->load("space-real.xml"); #ifdef WITH_BSON ref_space->save_bson("space-real.bson"); ref_space->free(); ref_space->load_bson("space-real.bson"); #endif linear_solver.set_space(ref_space); // Solve the linear system. If successful, obtain the solution. linear_solver.solve(); Solution<double>::vector_to_solution(linear_solver.get_sln_vector(), ref_space, ref_sln); // Project the fine mesh solution onto the coarse mesh. OGProjection<double> ogProjection; ogProjection.project_global(space, ref_sln, sln, HERMES_L2_NORM); #ifdef SHOW_OUTPUT MeshFunctionSharedPtr<double> val_filter(new ValFilter(ref_sln, 0.0, 1.0)); // View the coarse mesh solution. view1.show(val_filter); oview.show(space); #endif // Calculate element errors and total error estimate. errorCalculator.calculate_errors(sln, ref_sln); double err_est_rel = errorCalculator.get_total_error_squared() * 100; adaptivity.set_space(space); #ifdef SHOW_OUTPUT std::cout << "Error: " << err_est_rel << "%." << std::endl; #endif // If err_est_rel too large, adapt the mesh-> if(err_est_rel < ERR_STOP) done = true; else done = adaptivity.adapt(&selector); as++; } while (done == false); // Wait for keyboard or mouse input. #ifdef SHOW_OUTPUT View::wait(); #endif return as; }
int main(int argc, char** argv) { if (argc < 1) { std::cout << "Arguments less than 2.\n"; exit(-1); } if (argc >= 3) { minv = atoi(argv[1]); maxv = atoi(argv[2]); } struct svm_parameter param; param.svm_type = C_SVC; param.kernel_type = LINEAR; param.degree = 3; param.gamma = 0; // 1/num_features param.coef0 = 0; param.nu = 0.5; param.cache_size = 100; // param.C = 1; param.C = DBL_MAX; param.eps = 1e-3; param.p = 0.1; param.shrinking = 1; param.probability = 0; param.nr_weight = 0; param.weight_label = NULL; param.weight = NULL; svm_set_print_string_function(print_null); int rnd = 1; srand(time(NULL)); #ifdef _TEST0_ std::cout << "[1]******************************************************" << std::endl; std::cout << "\t(1) running programs... [" << inputs_init <<"]" << std::endl; #endif for (int i = 0; i < inputs_init; i++) { for (int j = 0; j < vars; j++) { inputs[j] = rand() % (maxv - minv + 1) + minv; } before_loop(); m(inputs); after_loop(); } start_processing: if (positive_set_changed) { qsort(positive_set, positive_idx, sizeof(node), node::compare); positive_set_changed = false; } if (negative_set_changed) { qsort(negative_set, negative_idx, sizeof(node), node::compare); negative_set_changed = false; } // nice_set_print(); #ifdef _TEST0_ std::cout << "\t(2) converting data into svm format..." << std::endl; #endif svm_linker sl; sl.add_node_set(positive_set, positive_idx, 1); sl.add_node_set(negative_set, negative_idx, -1); #ifdef _TEST0_ std::cout << "\t(3) svm training...[" << sl.l << "]" << std::endl; #endif struct svm_model* model = svm_train((const struct svm_problem *)&sl, ¶m); // svm_save_model("model_file", model); struct coef co; svm_model_visualization(model, &co); printf(" %.16g [0]", co.theta[0]); for (int j = 1; j < vars; j++) printf (" + %.16g [%d]", co.theta[j], j); printf (" >= %.16g\n", -co.theta0); print_svm_samples((const struct svm_problem*)&sl); svm_free_and_destroy_model(&model); rnd++; if (rnd <= max_iter) { #ifdef _TEST0_ std::cout << "[" << rnd << "]*********************************************************" << std::endl; std::cout << "\t(1) running programs...[" << inputs_aft << "]" << std::endl; #endif for (int i = 0; i < inputs_aft; i++) { linear_solver(co, inputs); before_loop(); m(inputs); after_loop(); } goto start_processing; } return 0; }
int main(int argc, char* args[]) { // Load the mesh. MeshSharedPtr mesh(new Mesh); MeshReaderH2D mloader; mloader.load("square.mesh", mesh); // Perform initial mesh refinement. for (int i=0; i<INIT_REF; i++) mesh->refine_all_elements(); mesh->refine_by_criterion(criterion, INIT_REF_CRITERION); ScalarView view1("Solution - Discontinuous Galerkin FEM", new WinGeom(900, 0, 450, 350)); ScalarView view2("Solution - Standard continuous FEM", new WinGeom(900, 400, 450, 350)); if(WANT_DG) { // Create an L2 space. SpaceSharedPtr<double> space_l2(new L2Space<double>(mesh, P_INIT)); // Initialize the solution. MeshFunctionSharedPtr<double> sln_l2(new Solution<double>); // Initialize the weak formulation. CustomWeakForm wf_l2(BDY_BOTTOM_LEFT); // Initialize the FE problem. DiscreteProblem<double> dp_l2(&wf_l2, space_l2); dp_l2.set_linear(); // Initialize linear solver. Hermes::Hermes2D::LinearSolver<double> linear_solver(&dp_l2); Hermes::Mixins::Loggable::Static::info("Assembling Discontinuous Galerkin (nelem: %d, ndof: %d).", mesh->get_num_active_elements(), space_l2->get_num_dofs()); // Solve the linear system. If successful, obtain the solution. Hermes::Mixins::Loggable::Static::info("Solving Discontinuous Galerkin."); try { linear_solver.solve(); if(DG_SHOCK_CAPTURING) { FluxLimiter flux_limiter(FluxLimiter::Kuzmin, linear_solver.get_sln_vector(), space_l2, true); flux_limiter.limit_second_orders_according_to_detector(); flux_limiter.limit_according_to_detector(); flux_limiter.get_limited_solution(sln_l2); view1.set_title("Solution - limited Discontinuous Galerkin FEM"); } else Solution<double>::vector_to_solution(linear_solver.get_sln_vector(), space_l2, sln_l2); // View the solution. view1.show(sln_l2); } catch(std::exception& e) { std::cout << e.what(); } } if(WANT_FEM) { // Create an H1 space. SpaceSharedPtr<double> space_h1(new H1Space<double>(mesh, P_INIT)); // Initialize the solution. MeshFunctionSharedPtr<double> sln_h1(new Solution<double>); // Initialize the weak formulation. CustomWeakForm wf_h1(BDY_BOTTOM_LEFT, false); // Initialize the FE problem. DiscreteProblem<double> dp_h1(&wf_h1, space_h1); dp_h1.set_linear(); Hermes::Mixins::Loggable::Static::info("Assembling Continuous FEM (nelem: %d, ndof: %d).", mesh->get_num_active_elements(), space_h1->get_num_dofs()); // Initialize linear solver. Hermes::Hermes2D::LinearSolver<double> linear_solver(&dp_h1); // Solve the linear system. If successful, obtain the solution. Hermes::Mixins::Loggable::Static::info("Solving Continuous FEM."); try { linear_solver.solve(); Solution<double>::vector_to_solution(linear_solver.get_sln_vector(), space_h1, sln_h1); // View the solution. view2.show(sln_h1); } catch(std::exception& e) { std::cout << e.what(); } } // Wait for keyboard or mouse input. View::wait(); return 0; }