本文整理汇总了C++中DiscreteProblem类的典型用法代码示例。如果您正苦于以下问题:C++ DiscreteProblem类的具体用法?C++ DiscreteProblem怎么用?C++ DiscreteProblem使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了DiscreteProblem类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main()
{
// Time measurement.
TimePeriod cpu_time;
cpu_time.tick();
// Create space, set Dirichlet BC, enumerate basis functions.
Space* space = new Space(A, B, NELEM, DIR_BC_LEFT, DIR_BC_RIGHT, P_INIT, NEQ, NEQ);
// Enumerate basis functions, info for user.
int ndof = Space::get_num_dofs(space);
info("ndof: %d", ndof);
// Initialize the weak formulation.
WeakForm wf(2);
wf.add_matrix_form(0, 0, jacobian_0_0);
wf.add_matrix_form(0, 1, jacobian_0_1);
wf.add_matrix_form(1, 0, jacobian_1_0);
wf.add_matrix_form(1, 1, jacobian_1_1);
wf.add_vector_form(0, residual_0);
wf.add_vector_form(1, residual_1);
// Initialize the FE problem.
bool is_linear = false;
DiscreteProblem *dp = new DiscreteProblem(&wf, space, is_linear);
// Newton's loop.
// Fill vector coeff_vec using dof and coeffs arrays in elements.
double *coeff_vec = new double[Space::get_num_dofs(space)];
get_coeff_vector(space, coeff_vec);
// 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);
int it = 1;
bool success = false;
while (1)
{
// Obtain the number of degrees of freedom.
int ndof = Space::get_num_dofs(space);
// Assemble the Jacobian matrix and residual vector.
dp->assemble(coeff_vec, matrix, rhs);
// Calculate the l2-norm of residual vector.
double res_l2_norm = get_l2_norm(rhs);
// Info for user.
info("---- Newton iter %d, ndof %d, res. l2 norm %g", it, Space::get_num_dofs(space), res_l2_norm);
// If l2 norm of the residual vector is within tolerance, then quit.
// NOTE: at least one full iteration forced
// here because sometimes the initial
// residual on fine mesh is too small.
if(res_l2_norm < NEWTON_TOL && it > 1) break;
// Multiply the residual vector with -1 since the matrix
// equation reads J(Y^n) \deltaY^{n+1} = -F(Y^n).
for(int i=0; i<ndof; i++) rhs->set(i, -rhs->get(i));
// Solve the linear system.
if(!(success = solver->solve()))
error ("Matrix solver failed.\n");
// Add \deltaY^{n+1} to Y^n.
for (int i = 0; i < ndof; i++) coeff_vec[i] += solver->get_solution()[i];
// If the maximum number of iteration has been reached, then quit.
if (it >= NEWTON_MAX_ITER) error ("Newton method did not converge.");
// Copy coefficients from vector y to elements.
set_coeff_vector(coeff_vec, space);
it++;
}
info("Total running time: %g s", cpu_time.accumulated());
// Test variable.
info("ndof = %d.", Space::get_num_dofs(space));
// Cleanup.
for(unsigned i = 0; i < DIR_BC_LEFT.size(); i++)
delete DIR_BC_LEFT[i];
DIR_BC_LEFT.clear();
for(unsigned i = 0; i < DIR_BC_RIGHT.size(); i++)
delete DIR_BC_RIGHT[i];
DIR_BC_RIGHT.clear();
delete matrix;
delete rhs;
delete solver;
delete[] coeff_vec;
delete dp;
delete space;
if (success)
{
//.........这里部分代码省略.........
示例2: main
int main(int argc, char* argv[])
{
// Load the mesh.
Mesh mesh;
H2DReader mloader;
mloader.load("square_quad.mesh", &mesh);
// Perform initial 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_dirichlet(BDY_DIRICHLET);
bc_types.add_bc_neumann(BDY_NEUMANN_LEFT);
// Enter Dirichlet boudnary values.
BCValues bc_values;
bc_values.add_function(BDY_DIRICHLET, essential_bc_values);
// Create an H1 space with default shapeset.
H1Space 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_vector_form(linear_form, linear_form_ord);
// Initialize refinement selector.
H1ProjBasedSelector selector(CAND_LIST, CONV_EXP, H2DRS_DEFAULT_ORDER);
// Set exact solution.
ExactSolution exact(&mesh, fndd);
// Initialize views.
ScalarView sview("Solution", new WinGeom(0, 0, 440, 350));
sview.show_mesh(false);
OrderView oview("Polynomial orders", new WinGeom(450, 0, 420, 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 = 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.
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);
// 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, 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;
// Calculate exact error for each solution component.
solutions_for_adapt = false;
double err_exact_rel = adaptivity->calc_err_exact(&sln, &exact, solutions_for_adapt, HERMES_TOTAL_ERROR_REL | HERMES_ELEMENT_ERROR_REL) * 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.
//.........这里部分代码省略.........
示例3: main
int main()
{
// Create space.
// Transform input data to the format used by the "Space" constructor.
SpaceData *md = new SpaceData();
Space* space = new Space(md->N_macroel, md->interfaces, md->poly_orders, md->material_markers, md->subdivisions, N_GRP, N_SLN);
delete md;
// Enumerate basis functions, info for user.
int ndof = Space::get_num_dofs(space);
info("ndof: %d", ndof);
// Plot the space.
space->plot("space.gp");
for (int g = 0; g < N_GRP; g++)
{
space->set_bc_left_dirichlet(g, flux_left_surf[g]);
space->set_bc_right_dirichlet(g, flux_right_surf[g]);
}
// Initialize the weak formulation.
WeakForm wf(2);
wf.add_matrix_form(0, 0, jacobian_mat1_0_0, NULL, mat1);
wf.add_matrix_form(0, 0, jacobian_mat2_0_0, NULL, mat2);
wf.add_matrix_form(0, 0, jacobian_mat3_0_0, NULL, mat3);
wf.add_matrix_form(0, 1, jacobian_mat1_0_1, NULL, mat1);
wf.add_matrix_form(0, 1, jacobian_mat2_0_1, NULL, mat2);
wf.add_matrix_form(0, 1, jacobian_mat3_0_1, NULL, mat3);
wf.add_matrix_form(1, 0, jacobian_mat1_1_0, NULL, mat1);
wf.add_matrix_form(1, 0, jacobian_mat2_1_0, NULL, mat2);
wf.add_matrix_form(1, 0, jacobian_mat3_1_0, NULL, mat3);
wf.add_matrix_form(1, 1, jacobian_mat1_1_1, NULL, mat1);
wf.add_matrix_form(1, 1, jacobian_mat2_1_1, NULL, mat2);
wf.add_matrix_form(1, 1, jacobian_mat3_1_1, NULL, mat3);
wf.add_vector_form(0, residual_mat1_0, NULL, mat1);
wf.add_vector_form(0, residual_mat2_0, NULL, mat2);
wf.add_vector_form(0, residual_mat3_0, NULL, mat3);
wf.add_vector_form(1, residual_mat1_1, NULL, mat1);
wf.add_vector_form(1, residual_mat2_1, NULL, mat2);
wf.add_vector_form(1, residual_mat3_1, NULL, mat3);
// Initialize the FE problem.
bool is_linear = false;
DiscreteProblem *dp = new DiscreteProblem(&wf, space, is_linear);
// Newton's loop.
// Fill vector coeff_vec using dof and coeffs arrays in elements.
double *coeff_vec = new double[Space::get_num_dofs(space)];
get_coeff_vector(space, coeff_vec);
// 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);
int it = 1;
while (1)
{
// Obtain the number of degrees of freedom.
int ndof = Space::get_num_dofs(space);
// Assemble the Jacobian matrix and residual vector.
dp->assemble(coeff_vec, matrix, rhs);
// Calculate the l2-norm of residual vector.
double res_l2_norm = get_l2_norm(rhs);
// Info for user.
info("---- Newton iter %d, ndof %d, res. l2 norm %g", it, Space::get_num_dofs(space), res_l2_norm);
// If l2 norm of the residual vector is within tolerance, then quit.
// NOTE: at least one full iteration forced
// here because sometimes the initial
// residual on fine mesh is too small.
if(res_l2_norm < NEWTON_TOL && it > 1) break;
// Multiply the residual vector with -1 since the matrix
// equation reads J(Y^n) \deltaY^{n+1} = -F(Y^n).
for(int i=0; i<ndof; i++) rhs->set(i, -rhs->get(i));
// Solve the linear system.
if(!solver->solve())
error ("Matrix solver failed.\n");
// Add \deltaY^{n+1} to Y^n.
for (int i = 0; i < ndof; i++) coeff_vec[i] += solver->get_solution()[i];
// If the maximum number of iteration has been reached, then quit.
if (it >= NEWTON_MAX_ITER) error ("Newton method did not converge.");
// Copy coefficients from vector y to elements.
set_coeff_vector(coeff_vec, space);
it++;
//.........这里部分代码省略.........
示例4: main
int main() {
// Time measurement.
TimePeriod cpu_time;
cpu_time.tick();
// Create coarse mesh, set Dirichlet BC, enumerate basis functions.
Space* space = new Space(A, B, NELEM, DIR_BC_LEFT, DIR_BC_RIGHT, P_INIT, NEQ);
// Enumerate basis functions, info for user.
int ndof = Space::get_num_dofs(space);
info("ndof: %d", ndof);
// Initialize the weak formulation.
WeakForm wf;
wf.add_matrix_form(jacobian);
wf.add_vector_form(residual);
// Initialize the FE problem.
bool is_linear = false;
DiscreteProblem *dp_coarse = new DiscreteProblem(&wf, space, is_linear);
// Newton's loop on coarse mesh.
// Fill vector coeff_vec using dof and coeffs arrays in elements.
double *coeff_vec_coarse = new double[Space::get_num_dofs(space)];
get_coeff_vector(space, coeff_vec_coarse);
// Set up the solver, matrix, and rhs 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);
int it = 1;
while (1) {
// Obtain the number of degrees of freedom.
int ndof_coarse = Space::get_num_dofs(space);
// Assemble the Jacobian matrix and residual vector.
dp_coarse->assemble(coeff_vec_coarse, matrix_coarse, rhs_coarse);
// Calculate the l2-norm of residual vector.
double res_l2_norm = get_l2_norm(rhs_coarse);
// Info for user.
info("---- Newton iter %d, ndof %d, res. l2 norm %g", it, Space::get_num_dofs(space), res_l2_norm);
// If l2 norm of the residual vector is within tolerance, then quit.
// NOTE: at least one full iteration forced
// here because sometimes the initial
// residual on fine mesh is too small.
if(res_l2_norm < NEWTON_TOL_COARSE && it > 1) break;
// Multiply the residual vector with -1 since the matrix
// equation reads J(Y^n) \deltaY^{n+1} = -F(Y^n).
for(int i=0; i < ndof_coarse; i++) rhs_coarse->set(i, -rhs_coarse->get(i));
// Solve the linear system.
if(!solver_coarse->solve())
error ("Matrix solver failed.\n");
// Add \deltaY^{n+1} to Y^n.
for (int i = 0; i < ndof_coarse; i++) coeff_vec_coarse[i] += solver_coarse->get_solution()[i];
// If the maximum number of iteration has been reached, then quit.
if (it >= NEWTON_MAX_ITER) error ("Newton method did not converge.");
// Copy coefficients from vector y to elements.
set_coeff_vector(coeff_vec_coarse, space);
it++;
}
// Cleanup.
delete matrix_coarse;
delete rhs_coarse;
delete solver_coarse;
delete [] coeff_vec_coarse;
delete dp_coarse;
// DOF and CPU convergence graphs.
SimpleGraph graph_dof_est, graph_cpu_est;
SimpleGraph 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);
// Initialize the FE problem.
bool is_linear = false;
DiscreteProblem* dp = new DiscreteProblem(&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);
//.........这里部分代码省略.........
示例5: main
int main(int argc, char* argv[])
{
// Load the mesh.
Mesh mesh;
H2DReader mloader;
mloader.load("lshape.mesh", &mesh); // quadrilaterals
// 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), HERMES_SYM);
wf.add_vector_form(callback(linear_form));
// Initialize refinement selector.
H1ProjBasedSelector selector(CAND_LIST, CONV_EXP, H2DRS_DEFAULT_ORDER);
// Set exact solution.
ExactSolution exact(&mesh, fndd);
// 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 = 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.
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, 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;
// Calculate exact error for each solution component.
solutions_for_adapt = false;
double err_exact_rel = adaptivity->calc_err_exact(&sln, &exact, solutions_for_adapt, HERMES_TOTAL_ERROR_REL | HERMES_ELEMENT_ERROR_REL) * 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);
graph_cpu.save("conv_cpu_est.dat");
graph_dof_exact.add_values(Space::get_num_dofs(&space), err_exact_rel);
graph_dof_exact.save("conv_dof_exact.dat");
graph_cpu_exact.add_values(cpu_time.accumulated(), err_exact_rel);
graph_cpu_exact.save("conv_cpu_exact.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);
//.........这里部分代码省略.........
示例6: main
int main() {
// Create space, set Dirichlet BC, enumerate basis functions.
Space* space = new Space(A, B, NELEM, DIR_BC_LEFT, DIR_BC_RIGHT, P_INIT, NEQ);
info("ndof = %d", Space::get_num_dofs(space));
// Initialize the weak formulation.
WeakForm wf(4);
wf.add_matrix_form(0, 0, jacobian_1_1);
wf.add_matrix_form(0, 2, jacobian_1_3);
wf.add_matrix_form(0, 3, jacobian_1_4);
wf.add_matrix_form(1, 1, jacobian_2_2);
wf.add_matrix_form(1, 2, jacobian_2_3);
wf.add_matrix_form(1, 3, jacobian_2_4);
wf.add_matrix_form(2, 0, jacobian_3_1);
wf.add_matrix_form(2, 1, jacobian_3_2);
wf.add_matrix_form(2, 2, jacobian_3_3);
wf.add_matrix_form(3, 0, jacobian_4_1);
wf.add_matrix_form(3, 1, jacobian_4_2);
wf.add_matrix_form(3, 3, jacobian_4_4);
wf.add_vector_form(0, residual_1);
wf.add_vector_form(1, residual_2);
wf.add_vector_form(2, residual_3);
wf.add_vector_form(3, residual_4);
wf.add_matrix_form_surf(0, 0, jacobian_surf_right_U_Re_Re, BOUNDARY_RIGHT);
wf.add_matrix_form_surf(0, 2, jacobian_surf_right_U_Re_Im, BOUNDARY_RIGHT);
wf.add_matrix_form_surf(1, 1, jacobian_surf_right_U_Im_Re, BOUNDARY_RIGHT);
wf.add_matrix_form_surf(1, 3, jacobian_surf_right_U_Im_Im, BOUNDARY_RIGHT);
// Initialize the FE problem.
bool is_linear = false;
DiscreteProblem *dp = new DiscreteProblem(&wf, space, is_linear);
// Set zero initial condition.
double *coeff_vec = new double[Space::get_num_dofs(space)];
set_zero(coeff_vec, Space::get_num_dofs(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);
int it = 1;
while (1) {
// Assemble the Jacobian matrix and residual vector.
dp->assemble(coeff_vec, matrix, rhs);
// Calculate the l2-norm of residual vector.
double res_l2_norm = get_l2_norm(rhs);
// Info for user.
info("---- Newton iter %d, ndof %d, res. l2 norm %g", it, Space::get_num_dofs(space), res_l2_norm);
// If l2 norm of the residual vector is within tolerance, then quit.
// NOTE: at least one full iteration forced
// here because sometimes the initial
// residual on fine mesh is too small.
if(res_l2_norm < NEWTON_TOL && it > 1) break;
// Multiply the residual vector with -1 since the matrix
// equation reads J(Y^n) \deltaY^{n+1} = -F(Y^n).
for(int i = 0; i < Space::get_num_dofs(space); i++) rhs->set(i, -rhs->get(i));
// Solve the linear system.
if(!solver->solve())
error ("Matrix solver failed.\n");
// Add \deltaY^{n+1} to Y^n.
for (int i = 0; i < Space::get_num_dofs(space); i++) coeff_vec[i] += solver->get_solution()[i];
// If the maximum number of iteration has been reached, then quit.
if (it >= NEWTON_MAX_ITER) error ("Newton method did not converge.");
it++;
}
// Plot the solution.
Linearizer l(space);
l.plot_solution("solution.gp");
// cleaning
delete dp;
delete rhs;
delete solver;
delete[] coeff_vec;
delete space;
delete bc_u_re_left;
delete bc_u_im_left;
delete matrix;
info("Done.");
return 0;
}
示例7: 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.
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, 330, 350));
//sview.show_mesh(false);
//sview.fix_scale_width(50);
//OrderView oview("Polynomial orders", new WinGeom(340, 0, 300, 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);
// 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");
// 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);
//.........这里部分代码省略.........
示例8: main
int main(int argc, char* argv[])
{
// Load the mesh.
Mesh u_mesh, v_mesh;
H2DReader mloader;
mloader.load("crack.mesh", &u_mesh);
// Perform initial uniform mesh refinement.
for (int i=0; i < INIT_REF_NUM; i++) u_mesh.refine_all_elements();
// Create initial mesh for the vertical displacement component.
// This also initializes the multimesh hp-FEM.
v_mesh.copy(&u_mesh);
// Create H1 spaces with default shapesets.
H1Space u_space(&u_mesh, bc_types_xy, essential_bc_values, P_INIT);
H1Space v_space(MULTI ? &v_mesh : &u_mesh, bc_types_xy, essential_bc_values, P_INIT);
// Initialize the weak formulation.
WeakForm wf(2);
wf.add_matrix_form(0, 0, callback(bilinear_form_0_0), HERMES_SYM);
wf.add_matrix_form(0, 1, callback(bilinear_form_0_1), HERMES_SYM);
wf.add_matrix_form(1, 1, callback(bilinear_form_1_1), HERMES_SYM);
wf.add_vector_form_surf(1, linear_form_surf_1, linear_form_surf_1_ord, BDY_TOP);
// 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;
// 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.
Tuple<Space *>* ref_spaces = construct_refined_spaces(Tuple<Space *>(&u_space, &v_space));
// Assemble the reference problem.
info("Solving on reference mesh.");
bool is_linear = true;
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);
dp->assemble(matrix, rhs);
// Time measurement.
cpu_time.tick();
// Solve the linear system of the reference problem. If successful, obtain the solutions.
if(solver->solve()) Solution::vector_to_solutions(solver->get_solution(), *ref_spaces,
Tuple<Solution *>(&u_ref_sln, &v_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(Tuple<Space *>(&u_space, &v_space), Tuple<Solution *>(&u_ref_sln, &v_ref_sln),
Tuple<Solution *>(&u_sln, &v_sln), matrix_solver);
// Calculate element errors.
info("Calculating error estimate and exact error.");
Adapt* adaptivity = new Adapt(Tuple<Space *>(&u_space, &v_space), Tuple<ProjNormType>(HERMES_H1_NORM, HERMES_H1_NORM));
adaptivity->set_error_form(0, 0, bilinear_form_0_0<scalar, scalar>, bilinear_form_0_0<Ord, Ord>);
adaptivity->set_error_form(0, 1, bilinear_form_0_1<scalar, scalar>, bilinear_form_0_1<Ord, Ord>);
adaptivity->set_error_form(1, 0, bilinear_form_1_0<scalar, scalar>, bilinear_form_1_0<Ord, Ord>);
adaptivity->set_error_form(1, 1, bilinear_form_1_1<scalar, scalar>, bilinear_form_1_1<Ord, Ord>);
// Calculate error estimate for each solution component and the total error estimate.
Tuple<double> err_est_rel;
bool solutions_for_adapt = true;
double err_est_rel_total = adaptivity->calc_err_est(Tuple<Solution *>(&u_sln, &v_sln), Tuple<Solution *>(&u_ref_sln, &v_ref_sln), solutions_for_adapt,
HERMES_TOTAL_ERROR_REL | HERMES_ELEMENT_ERROR_ABS, &err_est_rel) * 100;
// Time measurement.
cpu_time.tick();
// Report results.
info("ndof_coarse[0]: %d, ndof_fine[0]: %d, err_est_rel[0]: %g%%",
u_space.Space::get_num_dofs(), (*ref_spaces)[0]->Space::get_num_dofs(), err_est_rel[0]*100);
info("ndof_coarse[1]: %d, ndof_fine[1]: %d, err_est_rel[1]: %g%%",
v_space.Space::get_num_dofs(), (*ref_spaces)[1]->Space::get_num_dofs(), err_est_rel[1]*100);
info("ndof_coarse_total: %d, ndof_fine_total: %d, err_est_rel_total: %g%%",
Space::get_num_dofs(Tuple<Space *>(&u_space, &v_space)), Space::get_num_dofs(*ref_spaces), err_est_rel_total);
// Add entry to DOF and CPU convergence graphs.
graph_dof_est.add_values(Space::get_num_dofs(Tuple<Space *>(&u_space, &v_space)), err_est_rel_total);
graph_dof_est.save("conv_dof_est.dat");
//.........这里部分代码省略.........
示例9: 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
//.........这里部分代码省略.........
示例10: main
int main(int argc, char* argv[])
{
// Time measurement.
TimePeriod cpu_time;
cpu_time.tick();
// Load the mesh.
Mesh u1_mesh, u2_mesh;
H2DReader mloader;
mloader.load("../bracket.mesh", &u1_mesh);
// Initial mesh refinements.
u1_mesh.refine_element_id(1);
u1_mesh.refine_element_id(4);
// Create initial mesh for the vertical displacement component.
// This also initializes the multimesh hp-FEM.
u2_mesh.copy(&u1_mesh);
// Initialize boundary conditions.
DefaultEssentialBCConst zero_disp(BDY_RIGHT, 0.0);
EssentialBCs bcs(&zero_disp);
// Create x- and y- displacement space using the default H1 shapeset.
H1Space u1_space(&u1_mesh, &bcs, P_INIT);
H1Space u2_space(&u2_mesh, &bcs, P_INIT);
info("ndof = %d.", Space::get_num_dofs(Hermes::vector<Space *>(&u1_space, &u2_space)));
// Initialize the weak formulation.
CustomWeakForm wf(E, nu, rho*g1, BDY_TOP, f0, f1);
// Initialize the FE problem.
bool is_linear = true;
DiscreteProblem dp(&wf, Hermes::vector<Space *>(&u1_space, &u2_space), is_linear);
// Initialize coarse and reference mesh solutions.
Solution u1_sln, u2_sln, u1_ref_sln, u2_ref_sln;
// Initialize refinement selector.
H1ProjBasedSelector selector(CAND_LIST, CONV_EXP, H2DRS_DEFAULT_ORDER);
// Initialize views.
//ScalarView s_view_0("Solution (x-displacement)", new WinGeom(0, 0, 400, 350));
//s_view_0.show_mesh(false);
//ScalarView s_view_1("Solution (y-displacement)", new WinGeom(760, 0, 400, 350));
//s_view_1.show_mesh(false);
//OrderView o_view_0("Mesh (x-displacement)", new WinGeom(410, 0, 340, 350));
//OrderView o_view_1("Mesh (y-displacement)", new WinGeom(1170, 0, 340, 350));
//ScalarView mises_view("Von Mises stress [Pa]", new WinGeom(0, 405, 400, 350));
// DOF and CPU convergence graphs.
SimpleGraph graph_dof_est, graph_cpu_est;
// 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 *>(&u1_space, &u2_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 the reference problem.
info("Solving on reference mesh.");
bool is_linear = true;
DiscreteProblem* dp = new DiscreteProblem(&wf, *ref_spaces, is_linear);
dp->assemble(matrix, rhs);
// Time measurement.
cpu_time.tick();
// Solve the linear system of the reference problem. If successful, obtain the solutions.
if(solver->solve()) Solution::vector_to_solutions(solver->get_solution(), *ref_spaces,
Hermes::vector<Solution *>(&u1_ref_sln, &u2_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(Hermes::vector<Space *>(&u1_space, &u2_space),
Hermes::vector<Solution *>(&u1_ref_sln, &u2_ref_sln),
Hermes::vector<Solution *>(&u1_sln, &u2_sln), matrix_solver);
// View the coarse mesh solution and polynomial orders.
//s_view_0.show(&u1_sln);
//o_view_0.show(&u1_space);
//s_view_1.show(&u2_sln);
//o_view_1.show(&u2_space);
// For von Mises stress Filter.
//double lambda = (E * nu) / ((1 + nu) * (1 - 2*nu));
//double mu = E / (2*(1 + nu));
//VonMisesFilter stress(Hermes::vector<MeshFunction *>(&u1_sln, &u2_sln), lambda, mu);
//.........这里部分代码省略.........
示例11: 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;
//.........这里部分代码省略.........
示例12: main
int main()
{
// Time measurement.
TimePeriod cpu_time;
cpu_time.tick();
// Create coarse mesh, set Dirichlet BC, enumerate basis functions.
Space* space = new Space(A, B, NELEM, DIR_BC_LEFT, DIR_BC_RIGHT, P_INIT, NEQ);
// Enumerate basis functions, info for user.
int ndof = Space::get_num_dofs(space);
info("ndof: %d", ndof);
// Initialize the weak formulation.
WeakForm wf;
wf.add_matrix_form(jacobian);
wf.add_vector_form(residual);
double elem_errors[MAX_ELEM_NUM]; // This array decides what
// elements will be refined.
ElemPtr2 ref_elem_pairs[MAX_ELEM_NUM]; // To store element pairs from the
// FTR solution. Decides how
// elements will be hp-refined.
for (int i=0; i < MAX_ELEM_NUM; i++)
{
ref_elem_pairs[i][0] = new Element();
ref_elem_pairs[i][1] = new Element();
}
// DOF and CPU convergence graphs.
SimpleGraph graph_dof_exact, graph_cpu_exact;
/// Adaptivity loop:
int as = 1;
bool done = false;
do
{
info("---- Adaptivity step %d:", as);
// Initialize the FE problem.
bool is_linear = false;
DiscreteProblem *dp_coarse = new DiscreteProblem(&wf, space, is_linear);
// Newton's loop on coarse mesh.
// Fill vector coeff_vec using dof and coeffs arrays in elements.
double *coeff_vec_coarse = new double[Space::get_num_dofs(space)];
get_coeff_vector(space, coeff_vec_coarse);
// Set up the solver, matrix, and rhs 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);
int it = 1;
while (1)
{
// Obtain the number of degrees of freedom.
int ndof_coarse = Space::get_num_dofs(space);
// Assemble the Jacobian matrix and residual vector.
dp_coarse->assemble(coeff_vec_coarse, matrix_coarse, rhs_coarse);
// Calculate the l2-norm of residual vector.
double res_l2_norm = get_l2_norm(rhs_coarse);
// Info for user.
info("---- Newton iter %d, ndof %d, res. l2 norm %g", it, Space::get_num_dofs(space), res_l2_norm);
// If l2 norm of the residual vector is within tolerance, then quit.
// NOTE: at least one full iteration forced
// here because sometimes the initial
// residual on fine mesh is too small.
if(res_l2_norm < NEWTON_TOL_COARSE && it > 1) break;
// Multiply the residual vector with -1 since the matrix
// equation reads J(Y^n) \deltaY^{n+1} = -F(Y^n).
for(int i=0; i<ndof_coarse; i++) rhs_coarse->set(i, -rhs_coarse->get(i));
// Solve the linear system.
if(!solver_coarse->solve())
error ("Matrix solver failed.\n");
// Add \deltaY^{n+1} to Y^n.
for (int i = 0; i < ndof_coarse; i++) coeff_vec_coarse[i] += solver_coarse->get_solution()[i];
// If the maximum number of iteration has been reached, then quit.
if (it >= NEWTON_MAX_ITER) error ("Newton method did not converge.");
// Copy coefficients from vector y to elements.
set_coeff_vector(coeff_vec_coarse, space);
it++;
}
// Cleanup.
delete matrix_coarse;
delete rhs_coarse;
delete solver_coarse;
delete [] coeff_vec_coarse;
delete dp_coarse;
//.........这里部分代码省略.........
示例13: main
//.........这里部分代码省略.........
space_rho_v_y.set_uniform_order(P_INIT);
space_e.set_uniform_order(P_INIT);
}
// Adaptivity loop:
int as = 1;
bool done = false;
do
{
info("---- Adaptivity step %d:", as);
// Construct globally refined reference mesh and setup reference space.
// Global polynomial order increase = 0;
int order_increase = 0;
Hermes::Tuple<Space *>* ref_spaces = construct_refined_spaces(Hermes::Tuple<Space *>(&space_rho, &space_rho_v_x,
&space_rho_v_y, &space_e), order_increase);
// Project the previous time level solution onto the new fine mesh.
info("Projecting the previous time level solution onto the new fine mesh.");
OGProjection::project_global(*ref_spaces, Hermes::Tuple<Solution *>(&prev_rho, &prev_rho_v_x, &prev_rho_v_y, &prev_e),
Hermes::Tuple<Solution *>(&prev_rho, &prev_rho_v_x, &prev_rho_v_y, &prev_e), matrix_solver,
Hermes::Tuple<ProjNormType>(HERMES_L2_NORM, HERMES_L2_NORM, HERMES_L2_NORM, HERMES_L2_NORM));
if(as > 1) {
delete rsln_rho.get_mesh();
delete rsln_rho_v_x.get_mesh();
delete rsln_rho_v_y.get_mesh();
delete rsln_e.get_mesh();
}
// Assemble the reference problem.
info("Solving on reference mesh.");
bool is_linear = true;
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);
// The FE problem is in fact a FV problem.
dp->set_fvm();
#ifdef HERMES_USE_VECTOR_VALUED_FORMS
dp->use_vector_valued_forms();
#endif
dp->assemble(matrix, rhs);
// Solve the linear system of the reference problem. If successful, obtain the solutions.
if(solver->solve()) Solution::vector_to_solutions(solver->get_solution(), *ref_spaces,
Hermes::Tuple<Solution *>(&rsln_rho, &rsln_rho_v_x, &rsln_rho_v_y, &rsln_e));
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(Hermes::Tuple<Space *>(&space_rho, &space_rho_v_x,
&space_rho_v_y, &space_e), Hermes::Tuple<Solution *>(&rsln_rho, &rsln_rho_v_x, &rsln_rho_v_y, &rsln_e),
Hermes::Tuple<Solution *>(&sln_rho, &sln_rho_v_x, &sln_rho_v_y, &sln_e), matrix_solver,
Hermes::Tuple<ProjNormType>(HERMES_L2_NORM, HERMES_L2_NORM, HERMES_L2_NORM, HERMES_L2_NORM));
// Calculate element errors and total error estimate.
info("Calculating error estimate.");
Adapt* adaptivity = new Adapt(Hermes::Tuple<Space *>(&space_rho, &space_rho_v_x,
&space_rho_v_y, &space_e), Hermes::Tuple<ProjNormType>(HERMES_L2_NORM, HERMES_L2_NORM, HERMES_L2_NORM, HERMES_L2_NORM));
bool solutions_for_adapt = true;
// Error components.
Hermes::Tuple<double> *error_components = new Hermes::Tuple<double>(4);
double err_est_rel_total = adaptivity->calc_err_est(Hermes::Tuple<Solution *>(&sln_rho, &sln_rho_v_x, &sln_rho_v_y, &sln_e),
Hermes::Tuple<Solution *>(&rsln_rho, &rsln_rho_v_x, &rsln_rho_v_y, &rsln_e), solutions_for_adapt,
示例14: main
int main()
{
// Create space, set Dirichlet BC, enumerate basis functions.
Space* space = new Space(A, B, NELEM, DIR_BC_LEFT, DIR_BC_RIGHT, P_INIT, NEQ);
int ndof = Space::get_num_dofs(space);
info("ndof: %d", ndof);
// Initialize the weak formulation.
WeakForm wf;
wf.add_matrix_form(jacobian);
wf.add_vector_form(residual);
// Initialize the FE problem.
DiscreteProblem *dp = new DiscreteProblem(&wf, space);
// Allocate coefficient vector.
double *coeff_vec = new double[ndof];
memset(coeff_vec, 0, ndof*sizeof(double));
// 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);
// Time stepping loop.
double current_time = 0.0;
while (current_time < T_FINAL)
{
// Newton's loop.
// Fill vector coeff_vec using dof and coeffs arrays in elements.
get_coeff_vector(space, coeff_vec);
int it = 1;
while (true)
{
// Assemble the Jacobian matrix and residual vector.
dp->assemble(coeff_vec, matrix, rhs);
// Calculate the l2-norm of residual vector.
double res_l2_norm = get_l2_norm(rhs);
// Info for user.
info("---- Newton iter %d, ndof %d, res. l2 norm %g", it, Space::get_num_dofs(space), res_l2_norm);
// If l2 norm of the residual vector is within tolerance, then quit.
// NOTE: at least one full iteration forced
// here because sometimes the initial
// residual on fine mesh is too small.
if(res_l2_norm < NEWTON_TOL && it > 1) break;
// Multiply the residual vector with -1 since the matrix
// equation reads J(Y^n) \deltaY^{n+1} = -F(Y^n).
for(int i=0; i<ndof; i++) rhs->set(i, -rhs->get(i));
// Solve the linear system.
if(!solver->solve())
error ("Matrix solver failed.\n");
// Add \deltaY^{n+1} to Y^n.
for (int i = 0; i < ndof; i++) coeff_vec[i] += solver->get_solution()[i];
// If the maximum number of iteration has been reached, then quit.
if (it >= NEWTON_MAX_ITER) error ("Newton method did not converge.");
// Copy coefficients from vector y to elements.
set_coeff_vector(coeff_vec, space);
it++;
}
// Plot the solution.
Linearizer l(space);
char filename[100];
sprintf(filename, "solution_%g.gp", current_time);
l.plot_solution(filename);
info("Solution %s written.", filename);
current_time += TAU;
}
// Plot the resulting space.
space->plot("space.gp");
// Cleaning
delete dp;
delete rhs;
delete solver;
delete[] coeff_vec;
delete space;
delete matrix;
info("Done.");
return 0;
}
示例15: main
int main(int argc, char* argv[])
{
// Instantiate a class with global functions.
Hermes2D hermes2d;
// Load the mesh.
Mesh mesh;
H2DReader mloader;
mloader.load("../square_quad.mesh", &mesh);
// Perform initial mesh refinement.
for (int i=0; i < INIT_REF_NUM; i++) mesh.refine_all_elements();
// Set exact solution.
CustomExactSolution exact(&mesh, ALPHA);
// Define right-hand side.
CustomRightHandSide rhs(ALPHA);
// Initialize the weak formulation.
CustomWeakForm wf(&rhs);
// Initialize boundary conditions
DefaultEssentialBCNonConst bc(BDY_DIRICHLET, &exact);
EssentialBCs bcs(&bc);
// 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);
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.");
DiscreteProblem* dp = new DiscreteProblem(&wf, ref_space);
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);
graph_cpu.save("conv_cpu_est.dat");
graph_dof_exact.add_values(Space::get_num_dofs(&space), err_exact_rel);
graph_dof_exact.save("conv_dof_exact.dat");
graph_cpu_exact.add_values(cpu_time.accumulated(), err_exact_rel);
graph_cpu_exact.save("conv_cpu_exact.dat");
//.........这里部分代码省略.........