本文整理汇总了C++中Adapt::adapt方法的典型用法代码示例。如果您正苦于以下问题:C++ Adapt::adapt方法的具体用法?C++ Adapt::adapt怎么用?C++ Adapt::adapt使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Adapt
的用法示例。
在下文中一共展示了Adapt::adapt方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main(int argc, char* argv[])
{
// Time measurement
TimePeriod cpu_time;
cpu_time.tick();
// Load the mesh.
Mesh mesh;
H2DReader mloader;
mloader.load("lshape3q.mesh", &mesh); // quadrilaterals
//mloader.load("lshape3t.mesh", &mesh); // triangles
// Perform initial mesh refinemets.
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_1, BDY_6));
bc_types.add_bc_newton(Hermes::Tuple<int>(BDY_2, BDY_3, BDY_4, BDY_5));
// Enter Dirichlet boundary values.
BCValues bc_values;
bc_values.add_zero(Hermes::Tuple<int>(BDY_1, BDY_6));
// Create an Hcurl space with default shapeset.
HcurlSpace space(&mesh, &bc_types, &bc_values, P_INIT);
// Initialize the weak formulation.
WeakForm wf;
wf.add_matrix_form(callback(bilinear_form), HERMES_SYM);
wf.add_matrix_form_surf(callback(bilinear_form_surf));
wf.add_vector_form_surf(linear_form_surf, linear_form_surf_ord);
// Initialize coarse and reference mesh solutions.
Solution sln, ref_sln;
// Initialize exact solution.
ExactSolution sln_exact(&mesh, exact);
// Initialize refinement selector.
HcurlProjBasedSelector selector(CAND_LIST, CONV_EXP, H2DRS_DEFAULT_ORDER);
// DOF and CPU convergence graphs.
SimpleGraph graph_dof_est, graph_cpu_est,
graph_dof_exact, graph_cpu_exact;
// Adaptivity loop:
int as = 1;
bool done = false;
do
{
info("---- Adaptivity step %d:", as);
// Construct globally refined reference mesh and setup reference space.
Space* ref_space = construct_refined_space(&space);
// Assemble the reference problem.
info("Solving on reference mesh.");
bool is_linear = true;
DiscreteProblem* dp = new DiscreteProblem(&wf, ref_space, is_linear);
SparseMatrix* matrix = create_matrix(matrix_solver);
Vector* rhs = create_vector(matrix_solver);
Solver* solver = create_linear_solver(matrix_solver, matrix, rhs);
dp->assemble(matrix, rhs);
// Time measurement.
cpu_time.tick();
// Solve the linear system of the reference problem. If successful, obtain the solution.
if(solver->solve()) Solution::vector_to_solution(solver->get_solution(), ref_space, &ref_sln);
else error ("Matrix solver failed.\n");
// Time measurement.
cpu_time.tick();
// Project the fine mesh solution onto the coarse mesh.
info("Projecting reference solution on coarse mesh.");
OGProjection::project_global(&space, &ref_sln, &sln, matrix_solver);
// Calculate element errors and total error estimate.
info("Calculating error estimate and exact error.");
Adapt* adaptivity = new Adapt(&space);
double err_est_rel = adaptivity->calc_err_est(&sln, &ref_sln) * 100;
// Calculate exact error,
bool solutions_for_adapt = false;
double err_exact_rel = adaptivity->calc_err_exact(&sln, &sln_exact, solutions_for_adapt) * 100;
// Report results.
info("ndof_coarse: %d, ndof_fine: %d",
Space::get_num_dofs(&space), Space::get_num_dofs(ref_space));
info("err_est_rel: %g%%, err_exact_rel: %g%%", err_est_rel, err_exact_rel);
// Time measurement.
cpu_time.tick();
// Add entry to DOF and CPU convergence graphs.
graph_dof_est.add_values(Space::get_num_dofs(&space), err_est_rel);
graph_dof_est.save("conv_dof_est.dat");
graph_cpu_est.add_values(cpu_time.accumulated(), err_est_rel);
//.........这里部分代码省略.........
示例2: main
int main(int argc, char* argv[])
{
// Load the mesh.
Mesh mesh, basemesh;
H2DReader mloader;
mloader.load("square.mesh", &basemesh);
// Perform initial mesh refinements.
for(int i = 0; i < INIT_REF_NUM; i++) basemesh.refine_all_elements();
mesh.copy(&basemesh);
// Enter boundary markers.
BCTypes bc_types;
bc_types.add_bc_dirichlet(Hermes::vector<int>(BDY_BOTTOM, BDY_RIGHT, BDY_TOP, BDY_LEFT));
// Enter Dirichlet boundary values.
BCValues bc_values;
bc_values.add_zero(Hermes::vector<int>(BDY_BOTTOM, BDY_RIGHT, BDY_TOP, BDY_LEFT));
// Create an H1 space with default shapeset.
H1Space space(&mesh, &bc_types, &bc_values, P_INIT);
int ndof = Space::get_num_dofs(&space);
// Initialize coarse and reference mesh solution.
Solution sln, ref_sln;
// Convert initial condition into a Solution.
Solution sln_prev_time;
sln_prev_time.set_exact(&mesh, init_cond);
// Initialize the weak formulation.
WeakForm wf;
if(TIME_DISCR == 1) {
wf.add_matrix_form(callback(J_euler), HERMES_NONSYM, HERMES_ANY);
wf.add_vector_form(callback(F_euler), HERMES_ANY, &sln_prev_time);
}
else {
wf.add_matrix_form(callback(J_cranic), HERMES_NONSYM, HERMES_ANY);
wf.add_vector_form(callback(F_cranic), HERMES_ANY, &sln_prev_time);
}
// Initialize the FE problem.
bool is_linear = false;
DiscreteProblem dp_coarse(&wf, &space, is_linear);
// Set up the solver, matrix, and rhs for the coarse mesh according to the solver selection.
SparseMatrix* matrix_coarse = create_matrix(matrix_solver);
Vector* rhs_coarse = create_vector(matrix_solver);
Solver* solver_coarse = create_linear_solver(matrix_solver, matrix_coarse, rhs_coarse);
// Create a selector which will select optimal candidate.
H1ProjBasedSelector selector(CAND_LIST, CONV_EXP, H2DRS_DEFAULT_ORDER);
// Project the initial condition on the FE space to obtain initial
// coefficient vector for the Newton's method.
info("Projecting initial condition to obtain initial vector for the Newton's method.");
scalar* coeff_vec_coarse = new scalar[ndof];
OGProjection::project_global(&space, &sln_prev_time, coeff_vec_coarse, matrix_solver);
Solution init_proj;
Solution::vector_to_solution(coeff_vec_coarse, &space, &init_proj);
// Newton's loop on the coarse mesh.
info("Solving on coarse mesh:");
bool verbose = true;
if (!solve_newton(coeff_vec_coarse, &dp_coarse, solver_coarse, matrix_coarse, rhs_coarse,
NEWTON_TOL_COARSE, NEWTON_MAX_ITER, verbose)) error("Newton's iteration failed.");
// Translate the resulting coefficient vector into the Solution sln.
Solution::vector_to_solution(coeff_vec_coarse, &space, &sln);
// Cleanup after the Newton loop on the coarse mesh.
delete matrix_coarse;
delete rhs_coarse;
delete solver_coarse;
delete [] coeff_vec_coarse;
// Time stepping loop.
int num_time_steps = (int)(T_FINAL/TAU + 0.5);
for(int ts = 1; ts <= num_time_steps; ts++)
{
// Periodic global derefinements.
if (ts > 1 && ts % UNREF_FREQ == 0)
{
info("Global mesh derefinement.");
mesh.copy(&basemesh);
space.set_uniform_order(P_INIT);
// Project on globally derefined mesh.
info("Projecting previous fine mesh solution on derefined mesh.");
OGProjection::project_global(&space, &sln_prev_time, &sln);
}
// Adaptivity loop:
bool done = false; int as = 1;
double err_est;
do {
info("Time step %d, adaptivity step %d:", ts, as);
// Construct globally refined reference mesh
//.........这里部分代码省略.........
示例3: main
int main(int argc, char* argv[])
{
// Load the mesh.
Mesh mesh;
H2DReader mloader;
mloader.load("domain.mesh", &mesh);
// Perform initial uniform mesh refinement.
for (int i = 0; i < INIT_REF_NUM; i++) mesh.refine_all_elements();
// Enter boundary markers.
BCTypes bc_types;
bc_types.add_bc_neumann(Hermes::vector<int>(BDY_NEUMANN));
// Create an H1 space with default shapeset.
H1Space space(&mesh, &bc_types, P_INIT);
// Initialize the weak formulation.
WeakForm wf;
wf.add_matrix_form(bilinear_form_1, bilinear_form_ord, HERMES_SYM, 1);
wf.add_matrix_form(bilinear_form_2, bilinear_form_ord, HERMES_SYM, 2);
wf.add_matrix_form(bilinear_form_3, bilinear_form_ord, HERMES_SYM, 3);
wf.add_matrix_form(bilinear_form_4, bilinear_form_ord, HERMES_SYM, 4);
wf.add_matrix_form(bilinear_form_5, bilinear_form_ord, HERMES_SYM, 5);
wf.add_vector_form(linear_form_1, linear_form_ord, 1);
wf.add_vector_form(linear_form_3, linear_form_ord, 3);
// Initialize coarse and reference mesh solution.
Solution sln, ref_sln;
// Initialize refinement selector.
H1ProjBasedSelector selector(CAND_LIST, CONV_EXP, H2DRS_DEFAULT_ORDER);
// Initialize views.
ScalarView sview("Solution", new WinGeom(0, 0, 440, 350));
sview.fix_scale_width(50);
sview.show_mesh(false);
OrderView oview("Polynomial orders", new WinGeom(450, 0, 400, 350));
// DOF and CPU convergence graphs initialization.
SimpleGraph graph_dof, graph_cpu;
// Time measurement.
TimePeriod cpu_time;
cpu_time.tick();
// Adaptivity loop:
int as = 1;
bool done = false;
do
{
info("---- Adaptivity step %d:", as);
// Construct globally refined reference mesh and setup reference space.
Space* ref_space = construct_refined_space(&space);
// Initialize matrix and matrix solver.
SparseMatrix* matrix = create_matrix(matrix_solver);
Vector* rhs = create_vector(matrix_solver);
Solver* solver = create_linear_solver(matrix_solver, matrix, rhs);
// Assemble the reference problem.
info("Solving on reference mesh.");
bool is_linear = true;
DiscreteProblem* dp = new DiscreteProblem(&wf, ref_space, is_linear);
dp->assemble(matrix, rhs);
// Time measurement.
cpu_time.tick();
// Solve the linear system of the reference problem.
// If successful, obtain the solution.
if(solver->solve()) Solution::vector_to_solution(solver->get_solution(), ref_space, &ref_sln);
else error ("Matrix solver failed.\n");
// Project the fine mesh solution onto the coarse mesh.
info("Projecting reference solution on coarse mesh.");
OGProjection::project_global(&space, &ref_sln, &sln, matrix_solver);
// Time measurement.
cpu_time.tick();
// View the coarse mesh solution and polynomial orders.
sview.show(&sln);
oview.show(&space);
// Skip visualization time.
cpu_time.tick(HERMES_SKIP);
// Calculate element errors and total error estimate.
info("Calculating error estimate.");
Adapt* adaptivity = new Adapt(&space);
double err_est_rel = adaptivity->calc_err_est(&sln, &ref_sln) * 100;
// Report results.
info("ndof_coarse: %d, ndof_fine: %d, err_est_rel: %g%%",
Space::get_num_dofs(&space), Space::get_num_dofs(ref_space), err_est_rel);
// Time measurement.
cpu_time.tick();
//.........这里部分代码省略.........
示例4: main
int main(int argc, char* argv[])
{
// Instantiate a class with global functions.
Hermes2D hermes2d;
// Choose a Butcher's table or define your own.
ButcherTable bt(butcher_table_type);
if (bt.is_explicit()) info("Using a %d-stage explicit R-K method.", bt.get_size());
if (bt.is_diagonally_implicit()) info("Using a %d-stage diagonally implicit R-K method.", bt.get_size());
if (bt.is_fully_implicit()) info("Using a %d-stage fully implicit R-K method.", bt.get_size());
// Turn off adaptive time stepping if R-K method is not embedded.
if (bt.is_embedded() == false && ADAPTIVE_TIME_STEP_ON == true) {
warn("R-K method not embedded, turning off adaptive time stepping.");
ADAPTIVE_TIME_STEP_ON = false;
}
// Load the mesh.
Mesh mesh, basemesh;
H2DReader mloader;
mloader.load("../square.mesh", &basemesh);
mesh.copy(&basemesh);
// Initial mesh refinements.
for(int i = 0; i < INIT_GLOB_REF_NUM; i++) mesh.refine_all_elements();
mesh.refine_towards_boundary("Top", INIT_REF_NUM_BDY);
// Initialize boundary conditions.
CustomEssentialBCNonConst bc_essential(Hermes::vector<std::string>("Bottom", "Right", "Top", "Left"));
EssentialBCs bcs(&bc_essential);
// Create an H1 space with default shapeset.
H1Space space(&mesh, &bcs, P_INIT);
int ndof_coarse = Space::get_num_dofs(&space);
info("ndof_coarse = %d.", ndof_coarse);
// Initial condition vector is the zero vector. This is why we
// use the H_OFFSET.
scalar* coeff_vec = new scalar[ndof_coarse];
memset(coeff_vec, 0, ndof_coarse*sizeof(double));
// Convert initial condition into a Solution.
Solution h_time_prev, h_time_new;
Solution::vector_to_solution(coeff_vec, &space, &h_time_prev);
delete [] coeff_vec;
// Initialize the weak formulation.
CustomWeakFormRichardsRK wf;
// Initialize the FE problem.
DiscreteProblem dp(&wf, &space);
// Create a refinement selector.
H1ProjBasedSelector selector(CAND_LIST, CONV_EXP, H2DRS_DEFAULT_ORDER);
// Time stepping loop.
double current_time = 0;
int ts = 1;
do
{
// Periodic global derefinement.
if (ts > 1 && ts % UNREF_FREQ == 0)
{
info("Global mesh derefinement.");
switch (UNREF_METHOD) {
case 1:
mesh.copy(&basemesh);
space.set_uniform_order(P_INIT);
break;
case 2:
mesh.unrefine_all_elements();
space.set_uniform_order(P_INIT);
break;
case 3:
mesh.unrefine_all_elements();
//space.adjust_element_order(-1, P_INIT);
space.adjust_element_order(-1, -1, P_INIT, P_INIT);
break;
default:
error("Wrong global derefinement method.");
}
ndof_coarse = Space::get_num_dofs(&space);
}
// Spatial adaptivity loop. Note: h_time_prev must not be changed
// during spatial adaptivity.
bool done = false;
int as = 1;
double err_est;
do {
info("Time step %d, adaptivity step %d:", ts, as);
// Construct globally refined reference mesh and setup reference space.
Space* ref_space = Space::construct_refined_space(&space);
int ndof_ref = Space::get_num_dofs(ref_space);
// Initialize discrete problem on reference mesh.
DiscreteProblem dp(&wf, ref_space);
//.........这里部分代码省略.........
示例5: main
//.........这里部分代码省略.........
// Time measurement.
cpu_time.tick();
// Initial coefficient vector for the Newton's method.
scalar* coeff_vec = new scalar[ndof_ref];
memset(coeff_vec, 0, ndof_ref * sizeof(scalar));
// Perform Newton's iteration.
bool jacobian_changed = true;
bool verbose = true;
if (!hermes2d.solve_newton(coeff_vec, dp, solver, matrix, rhs, jacobian_changed,
1e-8, 100, verbose)) error("Newton's iteration failed.");
// Translate the resulting coefficient vector into the Solution sln.
Solution::vector_to_solutions(coeff_vec, *ref_spaces, Hermes::vector<Solution *>(&u_ref_sln, &v_ref_sln));
// Project the fine mesh solution onto the coarse mesh.
info("Projecting reference solution on coarse mesh.");
OGProjection::project_global(Hermes::vector<Space *>(&u_space, &v_space),
Hermes::vector<Solution *>(&u_ref_sln, &v_ref_sln),
Hermes::vector<Solution *>(&u_sln, &v_sln), matrix_solver);
cpu_time.tick();
// View the coarse mesh solution and polynomial orders.
s_view_0.show(&u_sln);
o_view_0.show(&u_space);
s_view_1.show(&v_sln);
o_view_1.show(&v_space);
// Calculate element errors.
info("Calculating error estimate and exact error.");
Adapt* adaptivity = new Adapt(Hermes::vector<Space *>(&u_space, &v_space));
// Calculate error estimate for each solution component and the total error estimate.
Hermes::vector<double> err_est_rel;
double err_est_rel_total = adaptivity->calc_err_est(Hermes::vector<Solution *>(&u_sln, &v_sln),
Hermes::vector<Solution *>(&u_ref_sln, &v_ref_sln),
&err_est_rel) * 100;
// Calculate exact error for each solution component and the total exact error.
Hermes::vector<double> err_exact_rel;
bool solutions_for_adapt = false;
double err_exact_rel_total = adaptivity->calc_err_exact(Hermes::vector<Solution *>(&u_sln, &v_sln),
Hermes::vector<Solution *>(&exact_u, &exact_v),
&err_exact_rel, solutions_for_adapt) * 100;
// Time measurement.
cpu_time.tick();
// Report results.
info("ndof_coarse[0]: %d, ndof_fine[0]: %d",
u_space.Space::get_num_dofs(), Space::get_num_dofs((*ref_spaces)[0]));
info("err_est_rel[0]: %g%%, err_exact_rel[0]: %g%%", err_est_rel[0]*100, err_exact_rel[0]*100);
info("ndof_coarse[1]: %d, ndof_fine[1]: %d",
v_space.Space::get_num_dofs(), Space::get_num_dofs((*ref_spaces)[1]));
info("err_est_rel[1]: %g%%, err_exact_rel[1]: %g%%", err_est_rel[1]*100, err_exact_rel[1]*100);
info("ndof_coarse_total: %d, ndof_fine_total: %d",
Space::get_num_dofs(Hermes::vector<Space *>(&u_space, &v_space)), Space::get_num_dofs(*ref_spaces));
info("err_est_rel_total: %g%%, err_est_exact_total: %g%%", err_est_rel_total, err_exact_rel_total);
// Add entry to DOF and CPU convergence graphs.
graph_dof_est.add_values(Space::get_num_dofs(Hermes::vector<Space *>(&u_space, &v_space)), err_est_rel_total);
graph_dof_est.save("conv_dof_est.dat");
graph_cpu_est.add_values(cpu_time.accumulated(), err_est_rel_total);
示例6: main
int main(int argc, char* argv[])
{
// Load the mesh.
Mesh mesh;
H2DReader mloader;
mloader.load("square.mesh", &mesh);
// Perform initial mesh refinements.
for (int i=0; i < INIT_REF_NUM; i++) mesh.refine_all_elements();
mesh.refine_towards_boundary(1, INIT_REF_NUM_BDY);
// Create an H1 space with default shapeset.
H1Space space(&mesh, bc_types, essential_bc_values, P_INIT);
// Initialize the weak formulation.
WeakForm wf;
wf.add_matrix_form(callback(bilinear_form), HERMES_SYM);
wf.add_vector_form(callback(linear_form));
// Initialize coarse and reference mesh solution.
Solution sln, ref_sln;
// Initialize refinement selector.
H1ProjBasedSelector selector(CAND_LIST, CONV_EXP, H2DRS_DEFAULT_ORDER);
// DOF and CPU convergence graphs initialization.
SimpleGraph graph_dof, graph_cpu;
// Time measurement.
TimePeriod cpu_time;
cpu_time.tick();
// Adaptivity loop:
int as = 1;
bool done = false;
do
{
info("---- Adaptivity step %d:", as);
// Construct globally refined reference mesh and setup reference space.
Space* ref_space = construct_refined_space(&space);
// Assemble the reference problem.
info("Solving on reference mesh.");
bool is_linear = true;
DiscreteProblem* dp = new DiscreteProblem(&wf, ref_space, is_linear);
SparseMatrix* matrix = create_matrix(matrix_solver);
Vector* rhs = create_vector(matrix_solver);
Solver* solver = create_linear_solver(matrix_solver, matrix, rhs);
dp->assemble(matrix, rhs);
// Time measurement.
cpu_time.tick();
// Solve the linear system of the reference problem.
// If successful, obtain the solution.
if(solver->solve()) Solution::vector_to_solution(solver->get_solution(), ref_space, &ref_sln);
else error ("Matrix solver failed.\n");
// Project the fine mesh solution onto the coarse mesh.
info("Projecting reference solution on coarse mesh.");
OGProjection::project_global(&space, &ref_sln, &sln, matrix_solver);
// Calculate element errors and total error estimate.
info("Calculating error estimate.");
Adapt* adaptivity = new Adapt(&space, HERMES_H1_NORM);
bool solutions_for_adapt = true;
double err_est_rel = adaptivity->calc_err_est(&sln, &ref_sln, solutions_for_adapt, HERMES_TOTAL_ERROR_REL | HERMES_ELEMENT_ERROR_REL) * 100;
// Report results.
info("ndof_coarse: %d, ndof_fine: %d, err_est_rel: %g%%",
Space::get_num_dofs(&space), Space::get_num_dofs(ref_space), err_est_rel);
// Time measurement.
cpu_time.tick();
// Add entry to DOF and CPU convergence graphs.
graph_dof.add_values(Space::get_num_dofs(&space), err_est_rel);
graph_dof.save("conv_dof_est.dat");
graph_cpu.add_values(cpu_time.accumulated(), err_est_rel);
graph_cpu.save("conv_cpu_est.dat");
// If err_est too large, adapt the mesh.
if (err_est_rel < ERR_STOP) done = true;
else
{
info("Adapting coarse mesh.");
done = adaptivity->adapt(&selector, THRESHOLD, STRATEGY, MESH_REGULARITY);
// Increase the counter of performed adaptivity steps.
if (done == false) as++;
}
if (Space::get_num_dofs(&space) >= NDOF_STOP) done = true;
// Clean up.
delete solver;
delete matrix;
delete rhs;
delete adaptivity;
if(done == false) delete ref_space->get_mesh();
//.........这里部分代码省略.........
示例7: main
//.........这里部分代码省略.........
info("Solving on reference mesh.");
// Assemble the discrete problem.
DiscreteProblem<double> dp(&wf, ref_spaces_const);
NewtonSolver<double> newton(&dp, matrix_solver);
newton.set_verbose_output(false);
Solution<double> u_ref_sln, v_ref_sln;
try
{
newton.solve();
}
catch(Hermes::Exceptions::Exception e)
{
e.printMsg();
error("Newton's iteration failed.");
};
// Translate the resulting coefficient vector into the instance of Solution.
Solution<double>::vector_to_solutions(newton.get_sln_vector(), ref_spaces_const, Hermes::vector<Solution<double>*>(&u_ref_sln, &v_ref_sln));
cpu_time.tick();
verbose("Solution: %g s", cpu_time.last());
// Project the fine mesh solution onto the coarse mesh.
info("Calculating error estimate and exact error.");
OGProjection<double>::project_global(Hermes::vector<const Space<double>*>(&u_space, &v_space),
Hermes::vector<Solution<double>*>(&u_ref_sln, &v_ref_sln),
Hermes::vector<Solution<double>*>(&u_sln, &v_sln), matrix_solver);
// Calculate element errors and total error estimate.
Hermes::vector<double> err_est_rel;
Adapt<double>* adaptivity = new Adapt<double>(Hermes::vector<Space<double>*>(&u_space, &v_space));
double err_est_rel_total = adaptivity->calc_err_est(Hermes::vector<Solution<double>*>(&u_sln, &v_sln),
Hermes::vector<Solution<double>*>(&u_ref_sln, &v_ref_sln), &err_est_rel) * 100.;
// Calculate exact error for each solution component and the total exact error.
Hermes::vector<double> err_exact_rel;
bool solutions_for_adapt = false;
double err_exact_rel_total = adaptivity->calc_err_exact(Hermes::vector<Solution<double>*>(&u_sln, &v_sln),
Hermes::vector<Solution<double>*>(&exact_u, &exact_v),
&err_exact_rel, solutions_for_adapt) * 100.;
cpu_time.tick();
verbose("Error calculation: %g s", cpu_time.last());
// Report results.
info("ndof_coarse[u]: %d, ndof_fine[u]: %d",
u_space.Space<double>::get_num_dofs(), Space<double>::get_num_dofs((*ref_spaces)[0]));
info("err_est_rel[u]: %g%%, err_exact_rel[u]: %g%%", err_est_rel[0]*100, err_exact_rel[0]*100);
info("ndof_coarse[v]: %d, ndof_fine[v]: %d",
v_space.Space<double>::get_num_dofs(), Space<double>::get_num_dofs((*ref_spaces)[1]));
info("err_est_rel[v]: %g%%, err_exact_rel[v]: %g%%", err_est_rel[1]*100, err_exact_rel[1]*100);
info("ndof_coarse_total: %d, ndof_fine_total: %d",
Space<double>::get_num_dofs(Hermes::vector<const Space<double> *>(&u_space, &v_space)), Space<double>::get_num_dofs(ref_spaces_const));
info("err_est_rel_total: %g%%, err_est_exact_total: %g%%", err_est_rel_total, err_exact_rel_total);
// Time measurement.
cpu_time.tick();
double accum_time = cpu_time.accumulated();
// View the coarse mesh solution and polynomial orders.
s_view_u.show(&u_sln);
o_view_u.show(&u_space);
s_view_v.show(&v_sln);
示例8: main
int main(int argc, char* argv[])
{
// Load the mesh.
Mesh mesh;
H2DReader mloader;
mloader.load("../square_quad.mesh", &mesh); // quadrilaterals
// mloader.load("../square_tri.mesh", &mesh); // triangles
// Perform initial mesh refinement.
for (int i=0; i<INIT_REF_NUM; i++) mesh.refine_all_elements();
mesh.refine_towards_boundary(BDY_LAYER, INIT_REF_NUM_BDY);
// Initialize the weak formulation.
WeakFormLinearAdvectionDiffusion wf(STABILIZATION_ON, SHOCK_CAPTURING_ON, B1, B2, EPSILON);
// Initialize boundary conditions
DefaultEssentialBCConst bc_rest(BDY_REST, 1.0);
EssentialBCNonConst bc_layer(BDY_LAYER);
EssentialBCs bcs(Hermes::vector<EssentialBoundaryCondition *>(&bc_rest, &bc_layer));
// Create an H1 space with default shapeset.
H1Space space(&mesh, &bcs, P_INIT);
// Initialize coarse and reference mesh solution.
Solution sln, ref_sln;
// Initialize refinement selector.
H1ProjBasedSelector selector(CAND_LIST, CONV_EXP, H2DRS_DEFAULT_ORDER);
// DOF and CPU convergence graphs initialization.
SimpleGraph graph_dof, graph_cpu;
// Time measurement.
TimePeriod cpu_time;
cpu_time.tick();
// Adaptivity loop:
int as = 1;
bool done = false;
do
{
info("---- Adaptivity step %d:", as);
// Construct globally refined reference mesh and setup reference space.
Space* ref_space = Space::construct_refined_space(&space);
// Assemble the reference problem.
info("Solving on reference mesh.");
bool is_linear = true;
DiscreteProblem* dp = new DiscreteProblem(&wf, ref_space, is_linear);
SparseMatrix* matrix = create_matrix(matrix_solver);
Vector* rhs = create_vector(matrix_solver);
Solver* solver = create_linear_solver(matrix_solver, matrix, rhs);
dp->assemble(matrix, rhs);
// Time measurement.
cpu_time.tick();
// Solve the linear system of the reference problem.
// If successful, obtain the solution.
if(solver->solve()) Solution::vector_to_solution(solver->get_solution(), ref_space, &ref_sln);
else error ("Matrix solver failed.\n");
// Project the fine mesh solution onto the coarse mesh.
info("Projecting reference solution on coarse mesh.");
OGProjection::project_global(&space, &ref_sln, &sln, matrix_solver);
// Calculate element errors and total error estimate.
info("Calculating error estimate.");
Adapt* adaptivity = new Adapt(&space);
double err_est_rel = adaptivity->calc_err_est(&sln, &ref_sln) * 100;
// Report results.
info("ndof_coarse: %d, ndof_fine: %d, err_est_rel: %g%%",
Space::get_num_dofs(&space), Space::get_num_dofs(ref_space), err_est_rel);
// Time measurement.
cpu_time.tick();
// Add entry to DOF and CPU convergence graphs.
graph_dof.add_values(Space::get_num_dofs(&space), err_est_rel);
graph_dof.save("conv_dof_est.dat");
graph_cpu.add_values(cpu_time.accumulated(), err_est_rel);
graph_cpu.save("conv_cpu_est.dat");
// If err_est too large, adapt the mesh.
if (err_est_rel < ERR_STOP) done = true;
else
{
info("Adapting coarse mesh.");
done = adaptivity->adapt(&selector, THRESHOLD, STRATEGY, MESH_REGULARITY);
// Increase the counter of performed adaptivity steps.
if (done == false) as++;
}
if (Space::get_num_dofs(&space) >= NDOF_STOP) done = true;
// Clean up.
delete solver;
//.........这里部分代码省略.........
示例9: main
//.........这里部分代码省略.........
info("Solved!");
// Translate the resulting coefficient vector into the Solution sln.
Solution::vector_to_solutions(coeff_vec_coarse, Hermes::vector<Space *>(&C_space, &phi_space),
Hermes::vector<Solution *>(&C_sln, &phi_sln));
out_fn_vtk(&C_sln,"C_init_sln");
out_fn_vtk(&phi_sln,"phi_init_sln");
//out_fn_vtk(&sln, "sln", ts);
Solution *C_ref_sln, *phi_ref_sln;
PidTimestepController pid(T_FINAL, false, INIT_TAU);
TAU = pid.timestep;
info("Starting time iteration with the step %g", *TAU);
do {
pid.begin_step();
if (pid.get_timestep_number() > 1 && pid.get_timestep_number() % UNREF_FREQ == 0)
{
info("Global mesh derefinement.");
C_mesh.copy(basemesh);
if (MULTIMESH)
{
phi_mesh.copy(basemesh);
}
C_space.set_uniform_order(Ord3(P_INIT_X, P_INIT_Y, P_INIT_Z));
phi_space.set_uniform_order(Ord3(P_INIT_X, P_INIT_Y, P_INIT_Z));
}
bool done = false; int as = 1;
double err_est;
do {
info("Time step %d, adaptivity step %d:", pid.get_timestep_number(), as);
// Construct globally refined reference mesh
// and setup reference space.
int order_increase = 1;
Hermes::vector<Space *>* ref_spaces = construct_refined_spaces(Hermes::vector<Space *>(&C_space, &phi_space),
order_increase);
scalar* coeff_vec = new scalar[Space::get_num_dofs(*ref_spaces)];
DiscreteProblem* dp = new DiscreteProblem(&wf, *ref_spaces, is_linear);
SparseMatrix* matrix = create_matrix(matrix_solver);
Vector* rhs = create_vector(matrix_solver);
Solver* solver = create_linear_solver(matrix_solver, matrix, rhs);
if (as == 1 && pid.get_timestep_number() == 1) {
info("Projecting coarse mesh solution to obtain coefficient vector on new fine mesh.");
OGProjection::project_global(*ref_spaces, Hermes::vector<MeshFunction *>(&C_sln, &phi_sln),
coeff_vec, matrix_solver);
}
else {
info("Projecting previous fine mesh solution to obtain coefficient vector on new fine mesh.");
OGProjection::project_global(*ref_spaces, Hermes::vector<MeshFunction *>(C_ref_sln, phi_ref_sln),
coeff_vec, matrix_solver);
}
if (as > 1) {
// Now deallocate the previous mesh
info("Deallocating the previous mesh");
//delete C_ref_sln->get_mesh();
//delete phi_ref_sln->get_mesh();
//delete C_ref_sln;
//delete phi_ref_sln;
}
/*TODO TEMP */
示例10: main
int main(int argc, char* argv[])
{
// Instantiate a class with global functions.
Hermes2D hermes2d;
// Load the mesh.
Mesh mesh;
H2DReader mloader;
switch (PARAM) {
case 0: mloader.load("../geom0.mesh", &mesh); break;
case 1: mloader.load("../geom1.mesh", &mesh); break;
case 2: mloader.load("../geom2.mesh", &mesh); break;
case 3: mloader.load("../geom3.mesh", &mesh); break;
default: error("Admissible values of PARAM are 0, 1, 2, 3.");
}
// Perform initial mesh refinements.
for (int i=0; i<INIT_REF_NUM; i++) mesh.refine_all_elements();
// Set exact solution.
CustomExactSolution exact(&mesh, PARAM);
// Initialize the weak formulation.
DefaultWeakFormLaplace wf;
// Initialize boundary conditions
DefaultEssentialBCNonConst bc_essential(BDY_DIRICHLET, &exact);
EssentialBCs bcs(&bc_essential);
// Create an H1 space with default shapeset.
H1Space space(&mesh, &bcs, P_INIT);
// Initialize refinement selector.
H1ProjBasedSelector selector(CAND_LIST, CONV_EXP, H2DRS_DEFAULT_ORDER);
// DOF and CPU convergence graphs.
SimpleGraph graph_dof, graph_cpu, graph_dof_exact, graph_cpu_exact;
// Time measurement.
TimePeriod cpu_time;
cpu_time.tick();
// Adaptivity loop:
int as = 1;
bool done = false;
do
{
info("---- Adaptivity step %d:", as);
// Construct globally refined reference mesh and setup reference space.
Space* ref_space = Space::construct_refined_space(&space);
// Set up the solver, matrix, and rhs according to the solver selection.
SparseMatrix* matrix = create_matrix(matrix_solver);
Vector* rhs = create_vector(matrix_solver);
Solver* solver = create_linear_solver(matrix_solver, matrix, rhs);
// Assemble the reference problem.
info("Solving on reference mesh.");
bool is_linear = true;
DiscreteProblem* dp = new DiscreteProblem(&wf, ref_space, is_linear);
dp->assemble(matrix, rhs);
// Time measurement.
cpu_time.tick();
// Solve the linear system of the reference problem. If successful, obtain the solution.
Solution ref_sln;
if(solver->solve()) Solution::vector_to_solution(solver->get_solution(), ref_space, &ref_sln);
else error ("Matrix solver failed.\n");
// Time measurement.
cpu_time.tick();
// Project the fine mesh solution onto the coarse mesh.
Solution sln;
info("Projecting reference solution on coarse mesh.");
OGProjection::project_global(&space, &ref_sln, &sln, matrix_solver);
// Calculate element errors and total error estimate.
info("Calculating error estimate and exact error.");
Adapt* adaptivity = new Adapt(&space);
double err_est_rel = adaptivity->calc_err_est(&sln, &ref_sln) * 100;
// Calculate exact error.
double err_exact_rel = hermes2d.calc_rel_error(&sln, &exact, HERMES_H1_NORM) * 100;
// Report results.
info("ndof_coarse: %d, ndof_fine: %d",
Space::get_num_dofs(&space), Space::get_num_dofs(ref_space));
info("err_est_rel: %g%%, err_exact_rel: %g%%", err_est_rel, err_exact_rel);
// Time measurement.
cpu_time.tick();
// Add entry to DOF and CPU convergence graphs.
graph_dof.add_values(Space::get_num_dofs(&space), err_est_rel);
graph_dof.save("conv_dof_est.dat");
graph_cpu.add_values(cpu_time.accumulated(), err_est_rel);
//.........这里部分代码省略.........
示例11: main
int main(int argc, char* argv[])
{
Hermes2D hermes2d;
// Time measurement.
TimePeriod cpu_time;
cpu_time.tick();
// Load the mesh.
Mesh mesh;
H2DReader mloader;
mloader.load("domain.mesh", &mesh);
//MeshView mv("Initial mesh", new WinGeom(0, 0, 400, 400));
//mv.show(&mesh);
//View::wait(HERMES_WAIT_KEYPRESS);
// Perform initial mesh refinements.
for (int i = 0; i < INIT_REF_NUM; i++) mesh.refine_all_elements();
// Initialize boundary conditions.
DefaultEssentialBCConst bc_essential("Source", P_SOURCE);
EssentialBCs bcs(&bc_essential);
// Create an H1 space with default shapeset.
H1Space space(&mesh, &bcs, P_INIT);
int ndof = Space::get_num_dofs(&space);
info("ndof = %d", ndof);
// Initialize the weak formulation.
CustomWeakFormAcoustics wf(BDY_NEWTON, RHO, SOUND_SPEED, OMEGA);
// Initialize coarse and reference mesh solution.
Solution sln, ref_sln;
// Initialize refinement selector.
H1ProjBasedSelector selector(CAND_LIST, CONV_EXP, H2DRS_DEFAULT_ORDER);
// Initialize views.
ScalarView sview("Solution", new WinGeom(0, 0, 600, 350));
sview.show_mesh(false);
sview.fix_scale_width(50);
OrderView oview("Polynomial orders", new WinGeom(610, 0, 600, 350));
// DOF and CPU convergence graphs initialization.
SimpleGraph graph_dof, graph_cpu;
SparseMatrix* matrix = create_matrix(matrix_solver);
Vector* rhs = create_vector(matrix_solver);
Solver* solver = create_linear_solver(matrix_solver, matrix, rhs);
if (matrix_solver == SOLVER_AZTECOO) {
((AztecOOSolver*) solver)->set_solver(iterative_method);
((AztecOOSolver*) solver)->set_precond(preconditioner);
// Using default iteration parameters (see solver/aztecoo.h).
}
// Adaptivity loop:
int as = 1;
bool done = false;
do
{
info("---- Adaptivity step %d:", as);
// Construct globally refined reference mesh and setup reference space.
Space* ref_space = Space::construct_refined_space(&space);
int ndof_ref = Space::get_num_dofs(ref_space);
// Assemble the reference problem.
info("Solving on reference mesh.");
DiscreteProblem* dp = new DiscreteProblem(&wf, ref_space);
// Time measurement.
cpu_time.tick();
// Initial coefficient vector for the Newton's method.
scalar* coeff_vec = new scalar[ndof_ref];
memset(coeff_vec, 0, ndof_ref * sizeof(scalar));
// Perform Newton's iteration.
if (!hermes2d.solve_newton(coeff_vec, dp, solver, matrix, rhs)) error("Newton's iteration failed.");
// Translate the resulting coefficient vector into the Solution sln.
Solution::vector_to_solution(coeff_vec, ref_space, &ref_sln);
// Project the fine mesh solution onto the coarse mesh.
info("Projecting reference solution on coarse mesh.");
OGProjection::project_global(&space, &ref_sln, &sln, matrix_solver);
// Time measurement.
cpu_time.tick();
// View the coarse mesh solution and polynomial orders.
sview.show(&sln);
oview.show(&space);
// Calculate element errors and total error estimate.
info("Calculating error estimate.");
Adapt* adaptivity = new Adapt(&space);
double err_est_rel = adaptivity->calc_err_est(&sln, &ref_sln) * 100;
//.........这里部分代码省略.........
示例12: main
int main(int argc, char* argv[])
{
// Time measurement.
TimePeriod cpu_time;
cpu_time.tick();
// Load the mesh.
Mesh mesh;
MeshReaderH2D mloader;
mloader.load("domain.mesh", &mesh);
// Perform initial mesh refinements.
for (int i = 0; i < INIT_REF_NUM; i++) mesh.refine_all_elements();
// Initialize boundary conditions.
Hermes::Hermes2D::DefaultEssentialBCConst<std::complex<double> > bc_essential("Dirichlet", std::complex<double>(0.0, 0.0));
EssentialBCs<std::complex<double> > bcs(&bc_essential);
// Create an H1 space with default shapeset.
H1Space<std::complex<double> > space(&mesh, &bcs, P_INIT);
int ndof = space.get_num_dofs();
info("ndof = %d", ndof);
// Initialize the weak formulation.
CustomWeakForm wf("Air", MU_0, "Iron", MU_IRON, GAMMA_IRON,
"Wire", MU_0, std::complex<double>(J_EXT, 0.0), OMEGA);
// Initialize coarse and reference mesh solution.
Solution<std::complex<double> > sln, ref_sln;
// Initialize refinement selector.
H1ProjBasedSelector<std::complex<double> > selector(CAND_LIST, CONV_EXP, H2DRS_DEFAULT_ORDER);
// Initialize views.
Views::ScalarView sview("Solution", new Views::WinGeom(0, 0, 600, 350));
sview.show_mesh(false);
Views::OrderView oview("Polynomial orders", new Views::WinGeom(610, 0, 520, 350));
// DOF and CPU convergence graphs initialization.
SimpleGraph graph_dof, graph_cpu;
Space<std::complex<double> >* ref_space = Space<std::complex<double> >::construct_refined_space(&space);
DiscreteProblem<std::complex<double> > dp(&wf, ref_space);
dp.set_adaptivity_cache();
// Perform Newton's iteration and translate the resulting coefficient vector into a Solution.
Hermes::Hermes2D::NewtonSolver<std::complex<double> > newton(&dp, matrix_solver_type);
// Adaptivity loop:
int as = 1; bool done = false;
do
{
info("---- Adaptivity step %d:", as);
// Construct globally refined reference mesh and setup reference space.
ref_space = Space<std::complex<double> >::construct_refined_space(&space);
dp.set_spaces(ref_space);
int ndof_ref = ref_space->get_num_dofs();
// Initialize reference problem.
info("Solving on reference mesh.");
// Time measurement.
cpu_time.tick();
// Initial coefficient vector for the Newton's method.
std::complex<double>* coeff_vec = new std::complex<double>[ndof_ref];
memset(coeff_vec, 0, ndof_ref * sizeof(std::complex<double>));
// Perform Newton's iteration and translate the resulting coefficient vector into a Solution.
// For iterative solver.
if (matrix_solver_type == SOLVER_AZTECOO)
{
newton.set_iterative_method(iterative_method);
newton.set_preconditioner(preconditioner);
}
try{
newton.solve(coeff_vec);
}
catch(Hermes::Exceptions::Exception e)
{
e.printMsg();
error("Newton's iteration failed.");
}
Hermes::Hermes2D::Solution<std::complex<double> >::vector_to_solution(newton.get_sln_vector(), ref_space, &ref_sln);
// Project the fine mesh solution onto the coarse mesh.
info("Projecting reference solution on coarse mesh.");
OGProjection<std::complex<double> >::project_global(&space, &ref_sln, &sln, matrix_solver_type);
// View the coarse mesh solution and polynomial orders.
RealFilter real_filter(&sln);
sview.show(&real_filter);
oview.show(&space);
// Calculate element errors and total error estimate.
info("Calculating error estimate.");
Adapt<std::complex<double> >* adaptivity = new Adapt<std::complex<double> >(&space);
//.........这里部分代码省略.........
示例13: main
int main(int argc, char **args)
{
// Load the mesh.
Mesh mesh;
H3DReader mesh_loader;
mesh_loader.load("fichera-corner.mesh3d", &mesh);
// Perform initial mesh refinement.
for (int i=0; i < INIT_REF_NUM; i++) mesh.refine_all_elements(H3D_H3D_H3D_REFT_HEX_XYZ);
// Create an H1 space with default shapeset.
H1Space space(&mesh, bc_types, essential_bc_values, Ord3(P_INIT_X, P_INIT_Y, P_INIT_Z));
// Initialize weak formulation.
WeakForm wf;
wf.add_matrix_form(bilinear_form<double, double>, bilinear_form<Ord, Ord>, HERMES_SYM, HERMES_ANY);
wf.add_vector_form(linear_form<double, double>, linear_form<Ord, Ord>, HERMES_ANY);
// Set exact solution.
ExactSolution exact(&mesh, fndd);
// DOF and CPU convergence graphs.
SimpleGraph graph_dof_est, graph_cpu_est, graph_dof_exact, graph_cpu_exact;
// Time measurement.
TimePeriod cpu_time;
cpu_time.tick();
// Initialize the solver in the case of SOLVER_PETSC or SOLVER_MUMPS.
initialize_solution_environment(matrix_solver, argc, args);
// Adaptivity loop.
int as = 1;
bool done = false;
do
{
info("---- Adaptivity step %d:", as);
// Construct globally refined reference mesh and setup reference space.
Space* ref_space = construct_refined_space(&space,1 , H3D_H3D_H3D_REFT_HEX_XYZ);
// Initialize discrete problem.
bool is_linear = true;
DiscreteProblem dp(&wf, ref_space, is_linear);
// Set up the solver, matrix, and rhs according to the solver selection.
SparseMatrix* matrix = create_matrix(matrix_solver);
Vector* rhs = create_vector(matrix_solver);
Solver* solver = create_linear_solver(matrix_solver, matrix, rhs);
// Initialize the preconditioner in the case of SOLVER_AZTECOO.
if (matrix_solver == SOLVER_AZTECOO)
{
((AztecOOSolver*) solver)->set_solver(iterative_method);
((AztecOOSolver*) solver)->set_precond(preconditioner);
// Using default iteration parameters (see solver/aztecoo.h).
}
// Assemble the reference problem.
info("Assembling on reference mesh (ndof: %d).", Space::get_num_dofs(ref_space));
dp.assemble(matrix, rhs);
// Time measurement.
cpu_time.tick();
// Solve the linear system on reference mesh. If successful, obtain the solution.
info("Solving on reference mesh.");
Solution ref_sln(ref_space->get_mesh());
if(solver->solve()) Solution::vector_to_solution(solver->get_solution(), ref_space, &ref_sln);
else error ("Matrix solver failed.\n");
// Time measurement.
cpu_time.tick();
// Project the reference solution on the coarse mesh.
Solution sln(space.get_mesh());
info("Projecting reference solution on coarse mesh.");
OGProjection::project_global(&space, &ref_sln, &sln, matrix_solver);
// Time measurement.
cpu_time.tick();
// Output solution and mesh with polynomial orders.
if (solution_output)
{
out_fn_vtk(&sln, "sln", as);
out_orders_vtk(&space, "order", as);
}
// Skip the visualization time.
cpu_time.tick(HERMES_SKIP);
// Calculate element errors and total error estimate.
info("Calculating error estimate and exact error.");
Adapt *adaptivity = new Adapt(&space, HERMES_H1_NORM);
bool solutions_for_adapt = true;
double err_est_rel = adaptivity->calc_err_est(&sln, &ref_sln, solutions_for_adapt) * 100;
// Calculate exact error.
solutions_for_adapt = false;
//.........这里部分代码省略.........
示例14: main
int main(int argc, char* argv[])
{
// Time measurement.
TimePeriod cpu_time;
cpu_time.tick();
// Load the mesh.
Mesh mesh;
H2DReader mloader;
mloader.load("domain2.mesh", &mesh);
// Perform initial mesh refinements.
for (int i=0; i<INIT_REF_NUM; i++) mesh.refine_all_elements();
// Create an H1 space with default shapeset.
H1Space space(&mesh, bc_types, essential_bc_values, P_INIT);
// Initialize the weak formulation.
WeakForm wf;
wf.add_matrix_form(callback(bilinear_form_iron), HERMES_SYM, 3);
wf.add_matrix_form(callback(bilinear_form_wire), HERMES_SYM, 2);
wf.add_matrix_form(callback(bilinear_form_air), HERMES_SYM, 1);
wf.add_vector_form(callback(linear_form_wire), 2);
// Initialize coarse and reference mesh solution.
Solution sln, ref_sln;
// Initialize refinement selector.
H1ProjBasedSelector selector(CAND_LIST, CONV_EXP, H2DRS_DEFAULT_ORDER);
// Initialize views.
ScalarView sview("Solution", new WinGeom(0, 0, 600, 350));
sview.show_mesh(false);
OrderView oview("Polynomial orders", new WinGeom(610, 0, 520, 350));
// DOF and CPU convergence graphs initialization.
SimpleGraph graph_dof, graph_cpu;
initialize_solution_environment(matrix_solver, argc, argv);
// Adaptivity loop:
int as = 1;
bool done = false;
do
{
info("---- Adaptivity step %d:", as);
// Construct globally refined reference mesh and setup reference space.
Space* ref_space = construct_refined_space(&space);
// Assemble the reference problem.
info("Solving on reference mesh.");
bool is_linear = true;
DiscreteProblem* dp = new DiscreteProblem(&wf, ref_space, is_linear);
SparseMatrix* matrix = create_matrix(matrix_solver);
Vector* rhs = create_vector(matrix_solver);
Solver* solver = create_linear_solver(matrix_solver, matrix, rhs);
if (matrix_solver == SOLVER_AZTECOO) {
((AztecOOSolver*) solver)->set_solver(iterative_method);
((AztecOOSolver*) solver)->set_precond(preconditioner);
// Using default iteration parameters (see solver/aztecoo.h).
}
dp->assemble(matrix, rhs);
// Time measurement.
cpu_time.tick();
// Solve the linear system of the reference problem. If successful, obtain the solution.
if(solver->solve()) Solution::vector_to_solution(solver->get_solution(), ref_space, &ref_sln);
else error ("Matrix solver failed.\n");
// Time measurement.
cpu_time.tick();
// Project the fine mesh solution onto the coarse mesh.
info("Projecting reference solution on coarse mesh.");
OGProjection::project_global(&space, &ref_sln, &sln, matrix_solver);
// View the coarse mesh solution and polynomial orders.
sview.show(&sln);
oview.show(&space);
// Calculate element errors and total error estimate.
info("Calculating error estimate.");
Adapt* adaptivity = new Adapt(&space, HERMES_H1_NORM);
bool solutions_for_adapt = true;
double err_est_rel = adaptivity->calc_err_est(&sln, &ref_sln, solutions_for_adapt, HERMES_TOTAL_ERROR_REL | HERMES_ELEMENT_ERROR_REL) * 100;
// Report results.
info("ndof_coarse: %d, ndof_fine: %d, err_est_rel: %g%%",
Space::get_num_dofs(&space), Space::get_num_dofs(ref_space), err_est_rel);
// Time measurement.
cpu_time.tick();
// Add entry to DOF and CPU convergence graphs.
graph_dof.add_values(Space::get_num_dofs(&space), err_est_rel);
//.........这里部分代码省略.........
示例15: main
//.........这里部分代码省略.........
// Calculate initial coefficient vector on the reference mesh.
scalar* coeff_vec = new scalar[Space::get_num_dofs(ref_space)];
if (as == 1)
{
// In the first step, project the coarse mesh solution.
info("Projecting coarse mesh solution to obtain initial vector on new fine mesh.");
OGProjection::project_global(ref_space, &sln, coeff_vec, matrix_solver);
}
else
{
// In all other steps, project the previous fine mesh solution.
info("Projecting previous fine mesh solution to obtain initial vector on new fine mesh.");
OGProjection::project_global(ref_space, &ref_sln, coeff_vec, matrix_solver);
}
// Now we can deallocate the previous fine mesh.
if(as > 1) delete ref_sln.get_mesh();
// Newton's loop on the fine mesh.
info("Solving on fine mesh:");
if (!solve_newton(coeff_vec, dp, solver, matrix, rhs,
NEWTON_TOL_FINE, NEWTON_MAX_ITER, verbose)) error("Newton's iteration failed.");
// Translate the resulting coefficient vector into the Solution ref_sln.
Solution::vector_to_solution(coeff_vec, ref_space, &ref_sln);
// Project the fine mesh solution on the coarse mesh.
if (as > 1) {
info("Projecting reference solution on new coarse mesh for error calculation.");
OGProjection::project_global(&space, &ref_sln, &sln, matrix_solver);
}
// Calculate element errors and total error estimate.
info("Calculating error estimate.");
Adapt* adaptivity = new Adapt(&space, HERMES_H1_NORM);
bool solutions_for_adapt = true;
double err_est_rel = adaptivity->calc_err_est(&sln, &ref_sln, solutions_for_adapt,
HERMES_TOTAL_ERROR_REL | HERMES_ELEMENT_ERROR_REL) * 100;
// Report results.
info("ndof_coarse: %d, ndof_fine: %d, err_est_rel: %g%%",
Space::get_num_dofs(&space), Space::get_num_dofs(ref_space), err_est_rel);
// Time measurement.
cpu_time.tick();
// Add entry to DOF and CPU convergence graphs.
graph_dof_est.add_values(Space::get_num_dofs(&space), err_est_rel);
graph_dof_est.save("conv_dof_est.dat");
graph_cpu_est.add_values(cpu_time.accumulated(), err_est_rel);
graph_cpu_est.save("conv_cpu_est.dat");
// If err_est_rel too large, adapt the mesh.
if (err_est_rel < ERR_STOP) done = true;
else
{
info("Adapting the coarse mesh.");
done = adaptivity->adapt(&selector, THRESHOLD, STRATEGY, MESH_REGULARITY);
if (Space::get_num_dofs(&space) >= NDOF_STOP)
{
done = true;
break;
}
// Project last fine mesh solution on the new coarse mesh
// to obtain new coars emesh solution.
info("Projecting reference solution on new coarse mesh for error calculation.");
OGProjection::project_global(&space, &ref_sln, &sln, matrix_solver);
}
// Clean up.
delete solver;
delete matrix;
delete rhs;
delete adaptivity;
delete ref_space;
delete dp;
delete [] coeff_vec;
as++;
}
while (done == false);
verbose("Total running time: %g s", cpu_time.accumulated());
int ndof = Space::get_num_dofs(&space);
printf("ndof allowed = %d\n", 400);
printf("ndof actual = %d\n", ndof);
if (ndof < 400) { // ndofs was 389 at the time this test was created.
printf("Success!\n");
return ERR_SUCCESS;
}
else {
printf("Failure!\n");
return ERR_FAILURE;
}
}