本文整理汇总了C++中Adapt类的典型用法代码示例。如果您正苦于以下问题:C++ Adapt类的具体用法?C++ Adapt怎么用?C++ Adapt使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Adapt类的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: 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_GLOB_REF_NUM; i++) mesh.refine_all_elements();
mesh.refine_towards_boundary(1, INIT_BDY_REF_NUM);
// 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(jac), HERMES_UNSYM, HERMES_ANY);
wf.add_vector_form(callback(res), HERMES_ANY);
// 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);
// Initialize coarse and reference mesh solution.
Solution sln, ref_sln;
// Time measurement.
TimePeriod cpu_time;
cpu_time.tick();
// DOF and CPU convergence graphs.
SimpleGraph graph_dof_est, graph_cpu_est;
// 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 on the coarse mesh.");
scalar* coeff_vec_coarse = new scalar[Space::get_num_dofs(&space)] ;
Solution* init_sln = new Solution(&mesh, init_cond);
OGProjection::project_global(&space, init_sln, coeff_vec_coarse, matrix_solver);
delete init_sln;
// Newton's loop on the coarse mesh. This is needed to obtain a good
// starting point for the Newton's method on the reference 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;
// 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 discrete problem on the reference mesh.
DiscreteProblem* dp = new DiscreteProblem(&wf, ref_space, is_linear);
// Initialize matrix solver.
SparseMatrix* matrix = create_matrix(matrix_solver);
Vector* rhs = create_vector(matrix_solver);
Solver* solver = create_linear_solver(matrix_solver, matrix, rhs);
// 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();
//.........这里部分代码省略.........
示例2: main
int main(int argc, char* argv[])
{
// Instantiate a class with global functions.
Hermes2D hermes2d;
// Load the mesh.
Mesh mesh;
H2DReader mloader;
mloader.load("domain.mesh", &mesh);
// Perform initial mesh refinement.
for (int i=0; i < INIT_REF_NUM; i++) mesh.refine_all_elements();
//mesh.refine_towards_vertex(3, 5);
// Define exact solution.
CustomExactSolution exact_sln(&mesh);
// Initialize the weak formulation.
DefaultWeakFormLaplace wf;
// Initialize boundary conditions
DefaultEssentialBCNonConst bc_essential("Bdy", &exact_sln);
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);
// Initialize views.
ScalarView sview("Solution", new WinGeom(0, 0, 440, 350));
sview.show_mesh(false);
OrderView oview("Polynomial orders", new WinGeom(450, 0, 410, 350));
// 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);
int ndof_ref = Space::get_num_dofs(ref_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);
// Initialize reference problem.
info("Solving on reference mesh.");
DiscreteProblem dp(&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 ref_sln;
Solution::vector_to_solution(coeff_vec, ref_space, &ref_sln);
// 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);
// 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 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_sln, 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.
//.........这里部分代码省略.........
示例3: main
int main(int argc, char* argv[])
{
// Time measurement.
TimePeriod cpu_time;
cpu_time.tick();
// Load the mesh.
Mesh mesh;
H2DReader mloader;
mloader.load("domain.mesh", &mesh);
// Perform initial mesh refinements.
mesh.refine_all_elements();
// Initialize boundary conditions.
CustomEssentialBCNonConst bc_essential(BDY_HORIZONTAL);
EssentialBCs bcs(&bc_essential);
// Create an H1 space with default shapeset.
H1Space space(&mesh, &bcs, P_INIT);
int ndof = space.get_num_dofs();
info("ndof = %d", ndof);
// Initialize the weak formulation.
CustomWeakFormGeneral wf;
// 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.show_mesh(false);
OrderView oview("Polynomial orders", new WinGeom(450, 0, 400, 350));
// DOF and CPU convergence graphs initialization.
SimpleGraph graph_dof, graph_cpu;
// 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);
// Initialize matrix solver.
SparseMatrix* matrix = create_matrix(matrix_solver);
Vector* rhs = create_vector(matrix_solver);
Solver* solver = create_linear_solver(matrix_solver, matrix, rhs);
// Assemble 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");
// 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);
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_rel too large, adapt the mesh.
if (err_est_rel < ERR_STOP) done = true;
else
//.........这里部分代码省略.........
示例4: main
int main(int argc, char* argv[])
{
// Time measurement.
TimePeriod cpu_time;
cpu_time.tick();
// Load the mesh.
Mesh mesh;
MeshReaderH2D mloader;
mloader.load("square.mesh", &mesh);
// Create an H1 space with default shapeset.
H1Space<double> space(&mesh, P_INIT);
// Initialize the weak formulation.
WeakForm<double> wf_dummy;
// Initialize coarse and reference mesh solution.
Solution<double> sln;
ExactSolutionCustom* ref_sln = NULL;
// Initialize refinement selector.
H1ProjBasedSelector<double> selector(CAND_LIST, CONV_EXP, H2DRS_DEFAULT_ORDER);
// Initialize views.
ScalarView sview("Scalar potential Phi", new WinGeom(0, 0, 610, 300));
sview.fix_scale_width(40);
sview.show_mesh(false);
OrderView oview("Mesh", new WinGeom(620, 0, 600, 300));
// DOF and CPU convergence graphs.
SimpleGraph graph_dof, graph_cpu;
// Adapt<double>ivity loop:
int as = 1; bool done = false;
do
{
info("---- Adapt<double>ivity step %d:", as);
// Construct globally refined reference mesh and setup reference space.
Space<double>* ref_space = Space<double>::construct_refined_space(&space);
// Assign the function f() to the fine mesh.
info("Assigning f() to the fine mesh.");
if(ref_sln != NULL) delete ref_sln;
ref_sln = new ExactSolutionCustom(ref_space->get_mesh());
// Time measurement.
cpu_time.tick();
// Project the fine mesh solution onto the coarse mesh.
info("Projecting reference solution on coarse mesh.");
OGProjection<double>::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 exact error.");
Adapt<double>* adaptivity = new Adapt<double>(&space);
// Note: the error estimate is now equal to the exact error.
double err_exact_rel = adaptivity->calc_err_est(&sln, ref_sln) * 100;
// Report results.
info("ndof_coarse: %d, ndof_fine: %d, err_exact_rel: %g%%",
Space<double>::get_num_dofs(&space), Space<double>::get_num_dofs(ref_space), err_exact_rel);
// Time measurement.
cpu_time.tick();
// Add entry to DOF and CPU convergence graphs.
graph_dof.add_values(Space<double>::get_num_dofs(&space), err_exact_rel);
graph_dof.save("conv_dof.dat");
graph_cpu.add_values(cpu_time.accumulated(), err_exact_rel);
graph_cpu.save("conv_cpu.dat");
// If err_exact_rel too large, adapt the mesh.
if (err_exact_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<double>::get_num_dofs(&space) >= NDOF_STOP) done = true;
// Clean up.
delete adaptivity;
if (done == false)
delete ref_space->get_mesh();
delete ref_space;
}
while (done == false);
verbose("Total running time: %g s", cpu_time.accumulated());
// Show the reference solution - the final result.
//.........这里部分代码省略.........
示例5: main
//.........这里部分代码省略.........
space = new L2Space(&mesh, P_INIT);
selector = new L2ProjBasedSelector(CAND_LIST, CONV_EXP, H2DRS_DEFAULT_ORDER);
norm = HERMES_L2_NORM;
// Disable weighting of refinement candidates.
selector->set_error_weights(1, 1, 1);
wf = new CustomWeakFormDiscontinuousGalerkin(bcs, EPSILON);
}
// Initialize coarse and reference mesh solution.
Solution sln, ref_sln;
// Set exact solution.
CustomExactSolution exact(&mesh, EPSILON);
// DOF and CPU convergence graphs initialization.
SimpleGraph graph_dof, graph_cpu, graph_dof_exact, graph_cpu_exact;
// Time measurement.
TimePeriod cpu_time;
cpu_time.tick();
// Setup data structures for solving the discrete algebraic problem.
SparseMatrix* matrix = create_matrix(matrix_solver);
Vector* rhs = create_vector(matrix_solver);
Solver* solver = create_linear_solver(matrix_solver, matrix, rhs);
// Adaptivity loop:
int as = 1;
bool done = false;
Space* actual_sln_space;
do
{
info("---- Adaptivity step %d:", as);
if (STRATEGY == -1)
actual_sln_space = space;
else
// Construct globally refined reference mesh and setup reference space.
actual_sln_space = Space::construct_refined_space(space, ORDER_INCREASE);
int ndof_fine = Space::get_num_dofs(actual_sln_space);
int ndof_coarse = Space::get_num_dofs(space);
// Solve the linear system. If successful, obtain the solution.
info("Solving on the refined mesh (%d NDOF).", ndof_fine);
DiscreteProblem dp(wf, actual_sln_space);
// Initial coefficient vector for the Newton's method.
scalar* coeff_vec = new scalar[ndof_fine];
memset(coeff_vec, 0, ndof_fine * sizeof(scalar));
// Perform Newton's iteration.
if (!hermes2d.solve_newton(coeff_vec, &dp, solver, matrix, rhs))
error("Newton's iteration failed.");
Solution::vector_to_solution(solver->get_solution(), actual_sln_space, &ref_sln);
// Calculate exact error.
double err_exact_rel = hermes2d.calc_rel_error(&ref_sln, &exact, norm) * 100;
info("ndof_fine: %d, err_exact_rel: %g%%", ndof_fine, err_exact_rel);
if (STRATEGY == -1) done = true; // Do not adapt.
else
{
Adapt* adaptivity = new Adapt(space, norm);
示例6: main
int main(int argc, char* argv[])
{
// Instantiate a class with global functions.
Hermes2D hermes2d;
// Load the mesh.
Mesh u_mesh, v_mesh;
H2DReader mloader;
mloader.load("../square.mesh", &u_mesh);
if (MULTI == false) u_mesh.refine_towards_boundary("Outer", INIT_REF_BDY);
// Create initial mesh (master mesh).
v_mesh.copy(&u_mesh);
// Initial mesh refinements in the v_mesh towards the boundary.
if (MULTI == true) v_mesh.refine_towards_boundary("Outer", INIT_REF_BDY);
// Set exact solutions.
ExactSolutionFitzHughNagumo1 exact_u(&u_mesh);
ExactSolutionFitzHughNagumo2 exact_v(&v_mesh, K);
// Define right-hand sides.
CustomRightHandSide1 rhs_1(K, D_u, SIGMA);
CustomRightHandSide2 rhs_2(K, D_v);
// Initialize the weak formulation.
WeakFormFitzHughNagumo wf(&rhs_1, &rhs_2);
// Initialize boundary conditions
DefaultEssentialBCConst bc_u("Outer", 0.0);
EssentialBCs bcs_u(&bc_u);
DefaultEssentialBCConst bc_v("Outer", 0.0);
EssentialBCs bcs_v(&bc_v);
// Create H1 spaces with default shapeset for both displacement components.
H1Space u_space(&u_mesh, &bcs_u, P_INIT_U);
H1Space v_space(MULTI ? &v_mesh : &u_mesh, &bcs_v, P_INIT_V);
// Initialize coarse and reference mesh solutions.
Solution u_sln, v_sln, u_ref_sln, v_ref_sln;
// Initialize refinement selector.
H1ProjBasedSelector 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;
// 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.
Hermes::vector<Space *>* ref_spaces =
Space::construct_refined_spaces(Hermes::vector<Space *>(&u_space, &v_space));
int ndof_ref = Space::get_num_dofs(Hermes::vector<Space *>(&u_space, &v_space));
// Initialize matrix solver.
SparseMatrix* matrix = create_matrix(matrix_solver);
Vector* rhs = create_vector(matrix_solver);
Solver* solver = create_linear_solver(matrix_solver, matrix, rhs);
// Initialize reference problem.
info("Solving on reference mesh.");
DiscreteProblem* dp = new DiscreteProblem(&wf, *ref_spaces);
dp->assemble(matrix, rhs);
// 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_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);
// 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;
//.........这里部分代码省略.........
示例7: main
//.........这里部分代码省略.........
// Calculate initial coefficient vector for Newton on the fine mesh.
if (as == 1 && ts == 1) {
info("Projecting coarse mesh solution to obtain initial vector on new fine mesh.");
OGProjection::project_global(ref_space, &sln, coeff_vec, matrix_solver);
}
else {
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);
delete ref_sln.get_mesh();
}
// Initialize the FE problem.
bool is_linear = false;
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);
// Perform Newton's iteration.
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 actual solutions.
Solution::vector_to_solutions(coeff_vec, ref_space, &ref_sln);
// Project the fine mesh solution on the coarse mesh.
info("Projecting fine mesh solution on coarse mesh for error calculation.");
OGProjection::project_global(&space, &ref_sln, &sln, matrix_solver);
// Calculate element errors.
info("Calculating error estimate and exact error.");
Adapt* adaptivity = new Adapt(&space);
// Calculate error estimate wrt. fine mesh solution.
double err_est_rel = adaptivity->calc_err_est(&sln, &ref_sln) * 100;
// Calculate error wrt. exact solution.
ExactSolution exact(&mesh, exact_sol);
bool solutions_for_adapt = false;
double err_exact_rel = adaptivity->calc_err_exact(&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("space_err_est_rel: %g%%, space_err_exact_rel: %g%%",
err_est_rel, err_exact_rel);
// Add entries to convergence graphs.
graph_time_err_est.add_values(ts*TAU, err_est_rel);
graph_time_err_est.save("time_error_est.dat");
graph_time_err_exact.add_values(ts*TAU, err_exact_rel);
graph_time_err_exact.save("time_error_exact.dat");
graph_time_dof.add_values(ts*TAU, Space::get_num_dofs(&space));
graph_time_dof.save("time_dof.dat");
graph_time_cpu.add_values(ts*TAU, cpu_time.accumulated());
graph_time_cpu.save("time_cpu.dat");
// If space_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);
if (Space::get_num_dofs(&space) >= NDOF_STOP)
done = true;
else
as++;
}
// Cleanup.
delete [] coeff_vec;
delete solver;
delete matrix;
delete rhs;
delete adaptivity;
delete ref_space;
}
while (!done);
// Copy new time level solution into sln_prev_time.
sln_prev_time.copy(&ref_sln);
}
delete ref_sln.get_mesh();
int ndof_allowed = 35;
printf("ndof actual = %d\n", ndof);
printf("ndof allowed = %d\n", ndof_allowed);
if (ndof <= ndof_allowed) { // ndofs was 33 at the time this test was created
printf("Success!\n");
return ERR_SUCCESS;
}
else {
printf("Failure!\n");
return ERR_FAILURE;
}
}
示例8: main
//.........这里部分代码省略.........
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, -1, P_INIT, P_INIT);
break;
}
ndof_coarse = Space<double>::get_num_dofs(&space);
}
// Spatial adaptivity loop. Note: sln_time_prev must not be changed
// during spatial adaptivity.
bool done = false; int as = 1;
double err_est;
do {
Hermes::Mixins::Loggable::Static::info("Time step %d, adaptivity step %d:", ts, as);
// Construct globally refined reference mesh and setup reference space.
Space<double>* ref_space = Space<double>::construct_refined_space(&space);
int ndof_ref = Space<double>::get_num_dofs(ref_space);
// Perform one Runge-Kutta time step according to the selected Butcher's table.
try
{
runge_kutta.set_space(ref_space);
runge_kutta.set_verbose_output(true);
runge_kutta.setTime(current_time);
runge_kutta.setTimeStep(time_step);
runge_kutta.rk_time_step_newton(&sln_time_prev, &sln_time_new);
}
catch(Exceptions::Exception& e)
{
std::cout << e.what();
}
// Project the fine mesh solution onto the coarse mesh.
Solution<double> sln_coarse;
Hermes::Mixins::Loggable::Static::info("Projecting fine mesh solution on coarse mesh for error estimation.");
OGProjection<double> ogProjection; ogProjection.project_global(&space, &sln_time_new, &sln_coarse);
// Calculate element errors and total error estimate.
Hermes::Mixins::Loggable::Static::info("Calculating error estimate.");
Adapt<double>* adaptivity = new Adapt<double>(&space);
double err_est_rel_total = adaptivity->calc_err_est(&sln_coarse, &sln_time_new) * 100;
// Report results.
Hermes::Mixins::Loggable::Static::info("ndof_coarse: %d, ndof_ref: %d, err_est_rel: %g%%",
Space<double>::get_num_dofs(&space), Space<double>::get_num_dofs(ref_space), err_est_rel_total);
// If err_est too large, adapt the mesh.
if (err_est_rel_total < ERR_STOP) done = true;
else
{
Hermes::Mixins::Loggable::Static::info("Adapting the coarse mesh.");
done = adaptivity->adapt(&selector, THRESHOLD, STRATEGY, MESH_REGULARITY);
if (Space<double>::get_num_dofs(&space) >= NDOF_STOP)
done = true;
else
// Increase the counter of performed adaptivity steps.
as++;
}
// Visualize the solution and mesh.
char title[100];
sprintf(title, "Solution<double>, time %g", current_time);
view.set_title(title);
view.show_mesh(false);
view.show(&sln_time_new);
sprintf(title, "Mesh, time %g", current_time);
ordview.set_title(title);
ordview.show(&space);
// Clean up.
delete adaptivity;
if(!done) {
delete ref_space;
delete sln_time_new.get_mesh();
}
}
while (done == false);
sln_time_prev.copy(&sln_time_new);
// Increase current time and counter of time steps.
current_time += time_step;
ts++;
}
while (current_time < T_FINAL);
// Wait for all views to be closed.
View::wait();
return 0;
}
示例9: main
//.........这里部分代码省略.........
delete solver_coarse;
delete [] coeff_vec_coarse;
}
// Adaptivity loop. Note: sln_prev_time 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 = construct_refined_space(&space);
// Initialize matrix solver.
SparseMatrix* matrix = create_matrix(matrix_solver);
Vector* rhs = create_vector(matrix_solver);
Solver* solver = create_linear_solver(matrix_solver, matrix, rhs);
scalar* coeff_vec = new scalar[Space::get_num_dofs(ref_space)];
// Initialize discrete problem on reference mesh.
DiscreteProblem* dp = new DiscreteProblem(&wf, ref_space, is_linear);
// Calculate initial coefficient vector for Newton on the fine mesh.
if (ts == 1 && as == 1) {
info("Projecting coarse mesh solution to obtain coefficient vector on fine mesh.");
OGProjection::project_global(ref_space, &sln, coeff_vec, matrix_solver);
}
else {
info("Projecting last fine mesh solution to obtain coefficient 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:");
bool verbose = true;
if (!solve_newton(coeff_vec, dp, solver, matrix, rhs,
NEWTON_TOL_FINE, NEWTON_MAX_ITER, verbose)) error("Newton's iteration failed.");
// Store the result in ref_sln.
Solution::vector_to_solution(coeff_vec, ref_space, &ref_sln);
// Project the fine mesh solution onto the coarse mesh.
info("Projecting fine mesh solution on coarse mesh for error estimation.");
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_total = adaptivity->calc_err_est(&sln, &ref_sln) * 100;
// Report results.
info("ndof: %d, ref_ndof: %d, err_est_rel: %g%%",
Space::get_num_dofs(&space), Space::get_num_dofs(ref_space), err_est_rel_total);
// If err_est too large, adapt the mesh.
if (err_est_rel_total < 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;
else
// Increase the counter of performed adaptivity steps.
as++;
}
// Clean up.
delete solver;
delete matrix;
delete rhs;
delete adaptivity;
delete ref_space;
delete dp;
delete [] coeff_vec;
}
while (done == false);
// Visualize the solution and mesh.
char title[100];
sprintf(title, "Solution, time %g", ts*TAU);
view.set_title(title);
view.show_mesh(false);
view.show(&sln);
sprintf(title, "Mesh, time %g", ts*TAU);
ordview.set_title(title);
ordview.show(&space);
// Copy last reference solution into sln_prev_time.
sln_prev_time.copy(&ref_sln);
}
// Wait for all views to be closed.
View::wait();
return 0;
}
示例10: main
int main(int argc, char **args)
{
// Test variable.
int success_test = 1;
if (argc < 2) error("Not enough parameters.");
// Load the mesh.
Mesh mesh;
H3DReader mloader;
if (!mloader.load(args[1], &mesh)) error("Loading mesh file '%s'.", args[1]);
// Initialize the space.
int mx = 2;
Ord3 order(mx, mx, mx);
H1Space space(&mesh, bc_types, NULL, order);
#if defined LIN_DIRICHLET || defined NLN_DIRICHLET
space.set_essential_bc_values(essential_bc_values);
#endif
// Initialize the weak formulation.
WeakForm wf;
wf.add_vector_form(form_0<double, scalar>, form_0<Ord, Ord>);
#if defined LIN_NEUMANN || defined LIN_NEWTON
wf.add_vector_form_surf(form_0_surf<double, scalar>, form_0_surf<Ord, Ord>);
#endif
#if defined LIN_DIRICHLET || defined NLN_DIRICHLET
// preconditioner
wf.add_matrix_form(precond_0_0<double, scalar>, precond_0_0<Ord, Ord>, HERMES_SYM);
#endif
// Initialize the FE problem.
DiscreteProblem fep(&wf, &space);
#if defined LIN_DIRICHLET || defined NLN_DIRICHLET
// use ML preconditioner to speed-up things
MlPrecond pc("sa");
pc.set_param("max levels", 6);
pc.set_param("increasing or decreasing", "decreasing");
pc.set_param("aggregation: type", "MIS");
pc.set_param("coarse: type", "Amesos-KLU");
#endif
NoxSolver solver(&fep);
#if defined LIN_DIRICHLET || defined NLN_DIRICHLET
// solver.set_precond(&pc);
#endif
info("Solving.");
Solution sln(&mesh);
if(solver.solve()) Solution::vector_to_solution(solver.get_solution(), &space, &sln);
else error ("Matrix solver failed.\n");
Solution ex_sln(&mesh);
ex_sln.set_exact(exact_solution);
// Calculate exact error.
info("Calculating exact error.");
Adapt *adaptivity = new Adapt(&space, HERMES_H1_NORM);
bool solutions_for_adapt = false;
double err_exact = adaptivity->calc_err_exact(&sln, &ex_sln, solutions_for_adapt, HERMES_TOTAL_ERROR_ABS);
if (err_exact > EPS)
// Calculated solution is not precise enough.
success_test = 0;
if (success_test) {
info("Success!");
return ERR_SUCCESS;
}
else {
info("Failure!");
return ERR_FAILURE;
}
}
示例11: main
int main(int argc, char* argv[])
{
// Instantiate a class with global functions.
Hermes2D hermes2d;
// 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();
// Set essential boundary conditions.
DefaultEssentialBCConst bc_essential(Hermes::vector<std::string>("right", "top"), 0.0);
EssentialBCs bcs(&bc_essential);
// Create an H1 space with default shapeset.
H1Space space(&mesh, &bcs, P_INIT);
// Associate element markers (corresponding to physical regions)
// with material properties (diffusion coefficient, absorption
// cross-section, external sources).
Hermes::vector<std::string> regions("e1", "e2", "e3", "e4", "e5");
Hermes::vector<double> D_map(D_1, D_2, D_3, D_4, D_5);
Hermes::vector<double> Sigma_a_map(SIGMA_A_1, SIGMA_A_2, SIGMA_A_3, SIGMA_A_4, SIGMA_A_5);
Hermes::vector<double> Sources_map(Q_EXT_1, 0.0, Q_EXT_3, 0.0, 0.0);
// Initialize the weak formulation.
WeakFormsNeutronics::Monoenergetic::Diffusion::DefaultWeakFormFixedSource
wf(regions, D_map, Sigma_a_map, Sources_map);
// 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 = Space::construct_refined_space(&space);
int ndof_ref = Space::get_num_dofs(ref_space);
// Initialize the FE problem.
DiscreteProblem dp(&wf, ref_space);
// Initialize the FE problem.
SparseMatrix* matrix = create_matrix(matrix_solver);
Vector* rhs = create_vector(matrix_solver);
Solver* solver = create_linear_solver(matrix_solver, matrix, rhs);
// 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 on reference emesh.
info("Solving on reference mesh.");
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.
Solution sln;
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;
//.........这里部分代码省略.........
示例12: main
int main(int argc, char* argv[])
{
// Instantiate a class with global functions.
Hermes2D hermes2d;
// Load the mesh.
Mesh mesh;
H2DReader mloader;
mloader.load("motor.mesh", &mesh);
// Initialize the weak formulation.
CustomWeakFormPoisson wf("Motor", EPS_MOTOR, "Air", EPS_AIR);
// Initialize boundary conditions
DefaultEssentialBCConst bc_essential_out("Outer", 0.0);
DefaultEssentialBCConst bc_essential_stator("Stator", VOLTAGE);
EssentialBCs bcs(Hermes::vector<EssentialBoundaryCondition *>(&bc_essential_out, &bc_essential_stator));
// 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);
// Initialize views.
ScalarView sview("Solution", new WinGeom(0, 0, 410, 600));
sview.fix_scale_width(50);
sview.show_mesh(false);
OrderView oview("Polynomial orders", new WinGeom(420, 0, 400, 600));
// 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);
int ndof_ref = Space::get_num_dofs(ref_space);
// Initialize matrix solver.
SparseMatrix* matrix = create_matrix(matrix_solver);
Vector* rhs = create_vector(matrix_solver);
Solver* solver = create_linear_solver(matrix_solver, matrix, rhs);
// Initialize 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();
// VTK output.
if (VTK_VISUALIZATION) {
// Output solution in VTK format.
Linearizer lin;
char* title = new char[100];
sprintf(title, "sln-%d.vtk", as);
lin.save_solution_vtk(&sln, title, "Potential", false);
info("Solution in VTK format saved to file %s.", title);
// Output mesh and element orders in VTK format.
Orderizer ord;
sprintf(title, "ord-%d.vtk", as);
ord.save_orders_vtk(&space, title);
info("Element orders in VTK format saved to file %s.", title);
}
// View the coarse mesh solution and polynomial orders.
if (HERMES_VISUALIZATION) {
sview.show(&sln);
oview.show(&space);
//.........这里部分代码省略.........
示例13: main
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> fine_space(new L2Space<double>(mesh, USE_TAYLOR_SHAPESET ? std::max(P_INIT, 2) : P_INIT, (USE_TAYLOR_SHAPESET ? (Shapeset*)(new L2ShapesetTaylor) : (Shapeset*)(new L2ShapesetLegendre))));
// Initialize refinement selector.
L2ProjBasedSelector<double> selector(CAND_LIST);
selector.set_error_weights(1., 1., 1.);
MeshFunctionSharedPtr<double> sln(new Solution<double>);
MeshFunctionSharedPtr<double> refsln(new Solution<double>);
// Initialize the weak formulation.
WeakFormSharedPtr<double> wf(new CustomWeakForm("Bdy_bottom_left", mesh));
ScalarView view1("Solution", new WinGeom(900, 0, 450, 350));
view1.fix_scale_width(60);
// Initialize linear solver.
Hermes::Hermes2D::LinearSolver<double> linear_solver;
linear_solver.set_weak_formulation(wf);
adaptivity.set_space(fine_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 refspace_creator(fine_space, ref_mesh, 0);
SpaceSharedPtr<double> refspace = refspace_creator.create_ref_space();
try
{
linear_solver.set_space(refspace);
linear_solver.solve();
if (USE_TAYLOR_SHAPESET)
{
PostProcessing::VertexBasedLimiter limiter(refspace, linear_solver.get_sln_vector(), P_INIT);
refsln = limiter.get_solution();
}
else
{
Solution<double>::vector_to_solution(linear_solver.get_sln_vector(), refspace, refsln);
}
view1.show(refsln);
OGProjection<double>::project_global(fine_space, refsln, sln, HERMES_L2_NORM);
}
catch (Exceptions::Exception& e)
{
std::cout << e.info();
}
catch (std::exception& e)
{
std::cout << e.what();
}
// Calculate element errors and total error estimate.
errorCalculator.calculate_errors(sln, refsln);
double err_est_rel = errorCalculator.get_total_error_squared() * 100;
std::cout << "Error: " << err_est_rel << "%." << std::endl;
// 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.
View::wait();
return 0;
}