本文整理汇总了C++中Adapt::set_space方法的典型用法代码示例。如果您正苦于以下问题:C++ Adapt::set_space方法的具体用法?C++ Adapt::set_space怎么用?C++ Adapt::set_space使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Adapt
的用法示例。
在下文中一共展示了Adapt::set_space方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main(int argc, char* argv[])
{
// Load the mesh.
MeshSharedPtr mesh(new Mesh);
MeshReaderH2D mloader;
mloader.load("domain.mesh", mesh);
// Define exact solution.
MeshFunctionSharedPtr<double> exact_sln(new CustomExactSolution(mesh));
// Initialize the weak formulation.
CustomWeakForm wf("Right");
// Initialize boundary conditions.
DefaultEssentialBCConst<double> bc_essential("Left", 0.0);
EssentialBCs<double> bcs(&bc_essential);
// Create an H1 space with default shapeset.
SpaceSharedPtr<double> space(new H1Space<double>(mesh, &bcs, P_INIT));
// Set the space to adaptivity.
adaptivity.set_space(space);
// Initialize approximate solution.
MeshFunctionSharedPtr<double> sln(new Solution<double>());
// Initialize refinement selector.
H1ProjBasedSelector<double> selector(CAND_LIST);
// Initialize views.
Views::ScalarView sview("Solution", new Views::WinGeom(0, 0, 440, 350));
sview.show_mesh(false);
sview.fix_scale_width(50);
Views::OrderView oview("Polynomial orders", new Views::WinGeom(450, 0, 420, 350));
// DOF and CPU convergence graphs.
SimpleGraph graph_dof_est, graph_cpu_est, graph_dof_exact, graph_cpu_exact;
// Time measurement.
Hermes::Mixins::TimeMeasurable cpu_time;
cpu_time.tick();
// Adaptivity loop:
int as = 1; bool done = false;
do
{
cpu_time.tick();
// Construct globally refined reference mesh and setup reference space.
Mesh::ReferenceMeshCreator refMeshCreator(mesh);
MeshSharedPtr ref_mesh = refMeshCreator.create_ref_mesh();
Space<double>::ReferenceSpaceCreator refSpaceCreator(space, ref_mesh);
SpaceSharedPtr<double> ref_space = refSpaceCreator.create_ref_space();
int ndof_ref = ref_space->get_num_dofs();
Hermes::Mixins::Loggable::Static::info("---- Adaptivity step %d (%d DOF):", as, ndof_ref);
cpu_time.tick();
Hermes::Mixins::Loggable::Static::info("Solving on reference mesh.");
// Assemble the discrete problem.
DiscreteProblem<double> dp(&wf, ref_space);
NewtonSolver<double> newton(&dp);
//newton.set_verbose_output(false);
MeshFunctionSharedPtr<double> ref_sln(new Solution<double>());
try
{
newton.solve();
}
catch(Hermes::Exceptions::Exception e)
{
e.print_msg();
throw Hermes::Exceptions::Exception("Newton's iteration failed.");
};
// Translate the resulting coefficient vector into the instance of Solution.
Solution<double>::vector_to_solution(newton.get_sln_vector(), ref_space, ref_sln);
cpu_time.tick();
Hermes::Mixins::Loggable::Static::info("Solution: %g s", cpu_time.last());
// Project the fine mesh solution onto the coarse mesh.
Hermes::Mixins::Loggable::Static::info("Calculating error estimate and exact error.");
OGProjection<double> ogProjection; ogProjection.project_global(space, ref_sln, sln);
// Calculate element errors and total error estimate.
errorCalculator.calculate_errors(sln, exact_sln, false);
double err_exact_rel = errorCalculator.get_total_error_squared() * 100;
errorCalculator.calculate_errors(sln, ref_sln, true);
double err_est_rel = errorCalculator.get_total_error_squared() * 100;
cpu_time.tick();
Hermes::Mixins::Loggable::Static::info("Error calculation: %g s", cpu_time.last());
// Report results.
Hermes::Mixins::Loggable::Static::info("ndof_coarse: %d, ndof_fine: %d", space->get_num_dofs(), ref_space->get_num_dofs());
Hermes::Mixins::Loggable::Static::info("err_est_rel: %g%%, err_exact_rel: %g%%", err_est_rel, err_exact_rel);
//.........这里部分代码省略.........
示例2: main
int main(int argc, char* argv[])
{
// Choose a Butcher's table or define your own.
ButcherTable bt(butcher_table_type);
if (bt.is_explicit()) Hermes::Mixins::Loggable::Static::info("Using a %d-stage explicit R-K method.", bt.get_size());
if (bt.is_diagonally_implicit()) Hermes::Mixins::Loggable::Static::info("Using a %d-stage diagonally implicit R-K method.", bt.get_size());
if (bt.is_fully_implicit()) Hermes::Mixins::Loggable::Static::info("Using a %d-stage fully implicit R-K method.", bt.get_size());
// Load the mesh.
MeshSharedPtr mesh(new Mesh), basemesh(new Mesh);
MeshReaderH2D 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<double> bcs(&bc_essential);
// Create an H1 space with default shapeset.
SpaceSharedPtr<double> space(new H1Space<double>(mesh, &bcs, P_INIT));
int ndof_coarse = Space<double>::get_num_dofs(space);
adaptivity.set_space(space);
Hermes::Mixins::Loggable::Static::info("ndof_coarse = %d.", ndof_coarse);
// Zero initial solution. This is why we use H_OFFSET.
MeshFunctionSharedPtr<double> h_time_prev(new ZeroSolution<double>(mesh)), h_time_new(new ZeroSolution<double>(mesh));
// Initialize the constitutive relations.
ConstitutiveRelations* constitutive_relations;
if(constitutive_relations_type == CONSTITUTIVE_GENUCHTEN)
constitutive_relations = new ConstitutiveRelationsGenuchten(ALPHA, M, N, THETA_S, THETA_R, K_S, STORATIVITY);
else
constitutive_relations = new ConstitutiveRelationsGardner(ALPHA, THETA_S, THETA_R, K_S);
// Initialize the weak formulation.
CustomWeakFormRichardsRK wf(constitutive_relations);
// Initialize the FE problem.
DiscreteProblem<double> dp(&wf, space);
// Create a refinement selector.
H1ProjBasedSelector<double> selector(CAND_LIST);
// Visualize initial condition.
char title[100];
ScalarView view("Initial condition", new WinGeom(0, 0, 440, 350));
OrderView ordview("Initial mesh", new WinGeom(445, 0, 440, 350));
view.show(h_time_prev);
ordview.show(space);
// DOF and CPU convergence graphs initialization.
SimpleGraph graph_dof, graph_cpu;
// Time measurement.
Hermes::Mixins::TimeMeasurable cpu_time;
cpu_time.tick();
// Time stepping loop.
double current_time = 0; int ts = 1;
do
{
// Periodic global derefinement.
if (ts > 1 && ts % UNREF_FREQ == 0)
{
Hermes::Mixins::Loggable::Static::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: space->unrefine_all_mesh_elements();
space->adjust_element_order(-1, -1, P_INIT, P_INIT);
break;
default: throw Hermes::Exceptions::Exception("Wrong global derefinement method.");
}
space->assign_dofs();
ndof_coarse = Space<double>::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 {
Hermes::Mixins::Loggable::Static::info("Time step %d, adaptivity step %d:", ts, as);
// Construct globally refined reference mesh and setup reference space.
Mesh::ReferenceMeshCreator refMeshCreator(mesh);
MeshSharedPtr ref_mesh = refMeshCreator.create_ref_mesh();
Space<double>::ReferenceSpaceCreator refSpaceCreator(space, ref_mesh);
SpaceSharedPtr<double> ref_space = refSpaceCreator.create_ref_space();
int ndof_ref = Space<double>::get_num_dofs(ref_space);
//.........这里部分代码省略.........
示例3: main
int main(int argc, char* argv[])
{
// Choose a Butcher's table or define your own.
ButcherTable bt(butcher_table_type);
if (bt.is_explicit()) Hermes::Mixins::Loggable::Static::info("Using a %d-stage explicit R-K method.", bt.get_size());
if (bt.is_diagonally_implicit()) Hermes::Mixins::Loggable::Static::info("Using a %d-stage diagonally implicit R-K method.", bt.get_size());
if (bt.is_fully_implicit()) Hermes::Mixins::Loggable::Static::info("Using a %d-stage fully implicit R-K method.", bt.get_size());
// Load the mesh.
MeshSharedPtr mesh(new Mesh), basemesh(new Mesh);
MeshReaderH2D mloader;
mloader.load("square.mesh", basemesh);
// Perform initial mesh refinements.
for (int i = 0; i < INIT_REF_NUM; i++)
basemesh->refine_all_elements(0, true);
mesh->copy(basemesh);
// Initialize boundary conditions.
EssentialBCNonConst bc_essential("Bdy");
EssentialBCs<double> bcs(&bc_essential);
// Create an H1 space with default shapeset.
SpaceSharedPtr<double> space(new H1Space<double>(mesh, &bcs, P_INIT));
adaptivity.set_space(space);
int ndof_coarse = space->get_num_dofs();
// Previous time level solution (initialized by initial condition).
MeshFunctionSharedPtr<double> sln_time_prev(new CustomInitialCondition(mesh));
// Initialize the weak formulation
CustomNonlinearity lambda(alpha);
Hermes2DFunction<double> f(heat_src);
WeakFormSharedPtr<double> wf(new CustomWeakFormPoisson(&lambda, &f));
// Next time level solution.
MeshFunctionSharedPtr<double> sln_time_new(new Solution<double>(mesh));
// Create a refinement selector.
H1ProjBasedSelector<double> selector(CAND_LIST);
// Visualize initial condition.
ScalarView view("Initial condition", new WinGeom(0, 0, 440, 350));
OrderView ordview("Initial mesh", new WinGeom(445, 0, 410, 350));
if (HERMES_VISUALIZATION)
{
view.show(sln_time_prev);
ordview.show(space);
}
// Initialize Runge-Kutta time stepping.
RungeKutta<double> runge_kutta(wf, space, &bt);
// Time stepping loop.
double current_time = 0; int ts = 1;
do
{
// Periodic global derefinement.
if (ts > 1 && ts % UNREF_FREQ == 0)
{
Hermes::Mixins::Loggable::Static::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, -1, P_INIT, P_INIT);
break;
}
space->assign_dofs();
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;
do {
Hermes::Mixins::Loggable::Static::info("Time step %d, adaptivity step %d:", ts, as);
// 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();
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.set_time(current_time);
runge_kutta.set_time_step(time_step);
runge_kutta.set_tolerance(NEWTON_TOL);
runge_kutta.rk_time_step_newton(sln_time_prev, sln_time_new);
//.........这里部分代码省略.........
示例4: main
//.........这里部分代码省略.........
}
if (ADAPTIVE_TIME_STEP_ON)
{
if (rel_err_time > TIME_ERR_TOL_UPPER)
{
Hermes::Mixins::Loggable::Static::info("rel_err_time %g%% is above upper limit %g%%", rel_err_time, TIME_ERR_TOL_UPPER);
Hermes::Mixins::Loggable::Static::info("Decreasing tau from %g to %g s and restarting time step.",
time_step, time_step * TIME_STEP_DEC_RATIO);
time_step *= TIME_STEP_DEC_RATIO;
continue;
}
else if (rel_err_time < TIME_ERR_TOL_LOWER)
{
Hermes::Mixins::Loggable::Static::info("rel_err_time = %g%% is below lower limit %g%%", rel_err_time, TIME_ERR_TOL_LOWER);
Hermes::Mixins::Loggable::Static::info("Increasing tau from %g to %g s.", time_step, time_step * TIME_STEP_INC_RATIO);
time_step *= TIME_STEP_INC_RATIO;
}
else
{
Hermes::Mixins::Loggable::Static::info("rel_err_time = %g%% is in acceptable interval (%g%%, %g%%)",
rel_err_time, TIME_ERR_TOL_LOWER, TIME_ERR_TOL_UPPER);
}
// Add entry to time step history graph.
time_step_graph.add_values(current_time, time_step);
time_step_graph.save("time_step_history.dat");
}
/* Estimate spatial errors and perform mesh refinement */
Hermes::Mixins::Loggable::Static::info("Spatial adaptivity step %d.", as);
// Project the fine mesh solution onto the coarse mesh.
MeshFunctionSharedPtr<double> sln(new Solution<double>());
Hermes::Mixins::Loggable::Static::info("Projecting fine mesh solution on coarse mesh for error estimation.");
ogProjection.project_global(space, ref_sln, sln);
// Show spatial error.
sprintf(title, "Spatial error est, spatial adaptivity step %d", as);
MeshFunctionSharedPtr<double> space_error_fn(new DiffFilter<double>(Hermes::vector<MeshFunctionSharedPtr<double> >(ref_sln, sln)));
space_error_view.set_title(title);
//space_error_view.show_mesh(false);
MeshFunctionSharedPtr<double> abs_sef(new AbsFilter(space_error_fn));
space_error_view.show(abs_sef);
// Calculate element errors and spatial error estimate.
Hermes::Mixins::Loggable::Static::info("Calculating spatial error estimate.");
adaptivity.set_space(space);
double err_rel_space = errorCalculator.get_total_error_squared() * 100;
// Report results.
Hermes::Mixins::Loggable::Static::info("ndof: %d, ref_ndof: %d, err_rel_space: %g%%",
Space<double>::get_num_dofs(space), Space<double>::get_num_dofs(ref_space), err_rel_space);
// If err_est too large, adapt the mesh.
if (err_rel_space < SPACE_ERR_TOL) done = true;
else
{
Hermes::Mixins::Loggable::Static::info("Adapting the coarse mesh.");
done = adaptivity.adapt(&selector);
if (Space<double>::get_num_dofs(space) >= NDOF_STOP)
done = true;
else
// Increase the counter of performed adaptivity steps.
as++;
}
// Clean up.
if(!done)
}
while (done == false);
// Visualize the solution and mesh->
char title[100];
sprintf(title, "Solution, time %g s", current_time);
sln_view.set_title(title);
//sln_view.show_mesh(false);
sln_view.show(ref_sln);
sprintf(title, "Mesh, time %g s", current_time);
ordview.set_title(title);
ordview.show(space);
// Copy last reference solution into sln_prev_time
sln_prev_time->copy(ref_sln);
// 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;
}
示例5: main
int main(int argc, char* argv[])
{
// Load the mesh.
MeshSharedPtr mesh(new 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<complex> bc_essential("Dirichlet", complex(0.0, 0.0));
EssentialBCs<complex> bcs(&bc_essential);
// Create an H1 space with default shapeset.
SpaceSharedPtr<complex> space(new H1Space<complex>(mesh, &bcs, P_INIT));
// Initialize the weak formulation.
CustomWeakForm wf("Air", MU_0, "Iron", MU_IRON, GAMMA_IRON,
"Wire", MU_0, complex(J_EXT, 0.0), OMEGA);
// Initialize coarse and reference mesh solution.
MeshFunctionSharedPtr<complex> sln(new Hermes::Hermes2D::Solution<complex>());
MeshFunctionSharedPtr<complex> ref_sln(new Hermes::Hermes2D::Solution<complex>());
// Initialize refinement selector.
H1ProjBasedSelector<complex> selector(CAND_LIST);
// DOF and CPU convergence graphs initialization.
SimpleGraph graph_dof, graph_cpu;
DiscreteProblem<complex> dp(&wf, space);
// Perform Newton's iteration and translate the resulting coefficient vector into a Solution.
Hermes::Hermes2D::NewtonSolver<complex> newton(&dp);
// Adaptivity loop:
int as = 1;
bool done = false;
adaptivity.set_space(space);
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<complex>::ReferenceSpaceCreator ref_space_creator(space, ref_mesh);
SpaceSharedPtr<complex> ref_space = ref_space_creator.create_ref_space();
newton.set_space(ref_space);
int ndof_ref = ref_space->get_num_dofs();
// Initialize reference problem.
// Initial coefficient vector for the Newton's method.
complex* coeff_vec = new complex[ndof_ref];
memset(coeff_vec, 0, ndof_ref * sizeof(complex));
// Perform Newton's iteration and translate the resulting coefficient vector into a Solution.
try
{
newton.solve(coeff_vec);
}
catch(Hermes::Exceptions::Exception& e)
{
e.print_msg();
}
Hermes::Hermes2D::Solution<complex>::vector_to_solution(newton.get_sln_vector(), ref_space, ref_sln);
// Project the fine mesh solution onto the coarse mesh.
OGProjection<complex> ogProjection;
ogProjection.project_global(space, ref_sln, sln);
// Calculate element errors and total error estimate.
errorCalculator.calculate_errors(sln, ref_sln);
// If err_est too large, adapt the mesh->
if(errorCalculator.get_total_error_squared() * 100. < ERR_STOP)
done = true;
else
{
adaptivity.adapt(&selector);
}
// Clean up.
delete [] coeff_vec;
// Increase counter.
as++;
}
while (done == false);
complex sum = 0;
for (int i = 0; i < space->get_num_dofs(); i++)
sum += newton.get_sln_vector()[i];
printf("coefficient sum = %f\n", sum);
complex expected_sum;
//.........这里部分代码省略.........
示例6: main
//.........这里部分代码省略.........
space->assign_dofs();
ndof_coarse = space->get_num_dofs();
}
// 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.
// FIXME: This should be increase in the x-direction only.
int order_increase = 1;
// FIXME: This should be '2' but that leads to a segfault.
int refinement_type = 0;
Mesh::ReferenceMeshCreator refMeshCreator(mesh);
MeshSharedPtr ref_mesh = refMeshCreator.create_ref_mesh();
Space<double>::ReferenceSpaceCreator refSpaceCreator(space, ref_mesh);
SpaceSharedPtr<double> ref_space = refSpaceCreator.create_ref_space();
int ndof_ref = ref_space->get_num_dofs();
// Initialize Runge-Kutta time stepping.
RungeKutta<double> runge_kutta(wf, ref_space, &bt);
// Perform one Runge-Kutta time step according to the selected Butcher's table.
Hermes::Mixins::Loggable::Static::info("Runge-Kutta time step (t = %g s, tau = %g s, stages: %d).",
current_time, time_step, bt.get_size());
bool freeze_jacobian = true;
bool block_diagonal_jacobian = false;
bool verbose = true;
try
{
runge_kutta.set_time(current_time);
runge_kutta.set_verbose_output(true);
runge_kutta.set_time_step(time_step);
runge_kutta.set_newton_max_allowed_iterations(NEWTON_MAX_ITER);
runge_kutta.set_newton_tolerance(NEWTON_TOL);
runge_kutta.rk_time_step_newton(sln_time_prev, sln_time_new);
}
catch (Exceptions::Exception& e)
{
e.print_msg();
throw Hermes::Exceptions::Exception("Runge-Kutta time step failed");
}
// Project the fine mesh solution onto the coarse mesh.
MeshFunctionSharedPtr<double> sln_coarse(new Solution<double>);
Hermes::Mixins::Loggable::Static::info("Projecting fine mesh solution on coarse mesh for error estimation.");
OGProjection<double>::project_global(space, sln_time_new, sln_coarse);
// Calculate element errors and total error estimate.
Hermes::Mixins::Loggable::Static::info("Calculating error estimate.");
adaptivity.set_space(space);
errorCalculator.calculate_errors(sln_coarse, sln_time_new);
double err_est_rel_total = errorCalculator.get_total_error_squared() * 100;
// Report results.
Hermes::Mixins::Loggable::Static::info("ndof_coarse: %d, ndof_ref: %d, err_est_rel: %g%%",
space->get_num_dofs(), ref_space->get_num_dofs(), 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);
// Increase the counter of performed adaptivity steps.
as++;
}
} while (done == false);
// Visualize the solution and mesh.
char title[100];
sprintf(title, "Solution, time %g", current_time);
sview.set_title(title);
sview.show_mesh(false);
sview.show(sln_time_new);
sprintf(title, "Mesh, time %g", current_time);
oview.set_title(title);
oview.show(space);
// Copy last reference solution into sln_time_prev->
sln_time_prev->copy(sln_time_new);
dof_history_graph.add_values(current_time, space->get_num_dofs());
dof_history_graph.save("dof_history.dat");
// 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.
Views::View::wait();
return 0;
}
示例7: main
int main(int argc, char* argv[])
{
// Load the mesh.
MeshSharedPtr mesh(new Mesh);
MeshReaderH2D mloader;
mloader.load("domain.mesh", mesh);
// Initialize the weak formulation.
CustomWeakFormPoisson wf("Motor", EPS_MOTOR, "Air", EPS_AIR);
// Initialize boundary conditions
DefaultEssentialBCConst<double> bc_essential_out("Outer", 0.0);
DefaultEssentialBCConst<double> bc_essential_stator("Stator", VOLTAGE);
EssentialBCs<double> bcs(Hermes::vector<EssentialBoundaryCondition<double> *>(&bc_essential_out, &bc_essential_stator));
// Create an H1 space with default shapeset.
SpaceSharedPtr<double> space(new H1Space<double>(mesh, &bcs, P_INIT));
// Set the space to adaptivity.
adaptivity.set_space(space);
// Initialize coarse and fine mesh solution.
MeshFunctionSharedPtr<double> sln(new Solution<double>), ref_sln(new Solution<double>);
// Initialize refinement selector.
H1ProjBasedSelector<double> selector(CAND_LIST, H2DRS_DEFAULT_ORDER);
// Initialize views.
Views::ScalarView sview("Solution", new Views::WinGeom(0, 0, 410, 600));
sview.fix_scale_width(50);
sview.show_mesh(false);
Views::OrderView oview("Polynomial orders", new Views::WinGeom(420, 0, 400, 600));
// DOF and CPU convergence graphs initialization.
SimpleGraph graph_dof, graph_cpu;
// Time measurement.
Hermes::Mixins::TimeMeasurable cpu_time;
DiscreteProblem<double> dp(&wf, space);
NewtonSolver<double> newton(&dp);
newton.set_verbose_output(true);
// Adaptivity loop:
int as = 1;
bool done = false;
do
{
Hermes::Mixins::Loggable::Static::info("---- Adaptivity step %d:", as);
// Time measurement.
cpu_time.tick();
// Construct globally refined mesh and setup fine mesh 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();
int ndof_ref = ref_space->get_num_dofs();
// Initialize fine mesh problem.
Hermes::Mixins::Loggable::Static::info("Solving on fine mesh.");
newton.set_space(ref_space);
// Perform Newton's iteration.
try
{
newton.solve();
}
catch(std::exception& e)
{
std::cout << e.what();
}
// Translate the resulting coefficient vector into the instance of Solution.
Solution<double>::vector_to_solution(newton.get_sln_vector(), ref_space, ref_sln);
// Project the fine mesh solution onto the coarse mesh.
Hermes::Mixins::Loggable::Static::info("Projecting fine mesh solution on coarse mesh.");
OGProjection<double>::project_global(space, ref_sln, sln);
// Time measurement.
cpu_time.tick();
// VTK output.
if (VTK_VISUALIZATION)
{
// Output solution in VTK format.
Views::Linearizer lin;
char* title = new char[100];
sprintf(title, "sln-%d.vtk", as);
lin.save_solution_vtk(ref_sln, title, "Potential", false);
Hermes::Mixins::Loggable::Static::info("Solution in VTK format saved to file %s.", title);
// Output mesh and element orders in VTK format.
Views::Orderizer ord;
sprintf(title, "ord-%d.vtk", as);
ord.save_orders_vtk(space, title);
Hermes::Mixins::Loggable::Static::info("Element orders in VTK format saved to file %s.", title);
//.........这里部分代码省略.........
示例8: 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;
}