本文整理汇总了C++中H2DReader::load方法的典型用法代码示例。如果您正苦于以下问题:C++ H2DReader::load方法的具体用法?C++ H2DReader::load怎么用?C++ H2DReader::load使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类H2DReader
的用法示例。
在下文中一共展示了H2DReader::load方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main (int argc, char* argv[]) {
// load the mesh file
Mesh Cmesh, phimesh, basemesh;
H2DReader mloader;
mloader.load("small.mesh", &basemesh);
basemesh.refine_towards_boundary(TOP_MARKER, REF_INIT);
basemesh.refine_towards_boundary(BOT_MARKER, REF_INIT - 1);
Cmesh.copy(&basemesh);
phimesh.copy(&basemesh);
// Spaces for concentration and the voltage.
H1Space C(&Cmesh, C_bc_types, NULL, P_INIT);
H1Space phi(MULTIMESH ? &phimesh : &Cmesh, phi_bc_types, essential_bc_values, P_INIT);
// set polynomial degrees.
C.set_uniform_order(P_INIT);
phi.set_uniform_order(P_INIT);
info("ndof: %d", C.get_num_dofs() + phi.get_num_dofs());
// The weak form for 2 equations.
WeakForm wf(2);
Solution C_prev_time, // prveious time step solution, for the time integration
C_prev_newton, // solution convergin during the Newton's iteration
phi_prev_time,
phi_prev_newton;
// Add the bilinear and linear forms
// generally, the equation system is described:
// a11(u1, v1) + a12(u2, v1) + a1n(un, v1) = l1(v1)
// a21(u1, v2) + a22(u2, v2) + a2n(un, v2) = l2(v2)
// an1(u1, vn) + an2(u2, vn) + ann(un, vn) = ln(vn)
wf.add_matrix_form(0, 0, callback(J_euler_DFcDYc), H2D_UNSYM, H2D_ANY, &phi_prev_newton);
wf.add_matrix_form(0, 1, callback(J_euler_DFcDYphi), H2D_UNSYM, H2D_ANY, &C_prev_newton);
wf.add_matrix_form(1, 0, callback(J_euler_DFphiDYc), H2D_UNSYM);
wf.add_matrix_form(1, 1, callback(J_euler_DFphiDYphi), H2D_UNSYM);
wf.add_vector_form(0, callback(Fc_euler), H2D_ANY, Tuple<MeshFunction*>(&C_prev_time, &C_prev_newton, &phi_prev_newton));
wf.add_vector_form(1, callback(Fphi_euler), H2D_ANY, Tuple<MeshFunction*>(&C_prev_newton, &phi_prev_newton));
// Neumann voltage boundary.
wf.add_vector_form_surf(1, callback(linear_form_surf_top), TOP_MARKER);
// Nonlinear solver.
NonlinSystem nls(&wf, Tuple<Space*>(&C, &phi));
info("UmfpackSolver initialized");
// View initial guess for Newton's method
// initial BC
C_prev_time.set_const(&Cmesh, C0);
phi_prev_time.set_const(MULTIMESH ? &phimesh : &Cmesh, 0);
// phi_prev_time.set_exact(MULTIMESH ? &phimesh : &Cmesh, voltage_ic);
// C_prev_time.set_exact(&Cmesh, concentration_ic);
C_prev_newton.copy(&C_prev_time);
phi_prev_newton.copy(&phi_prev_time);
nls.project_global(Tuple<MeshFunction*>(&C_prev_newton, &phi_prev_newton),
Tuple<Solution*>(&C_prev_newton, &phi_prev_newton));
bool success = solveAdaptive(Cmesh, phimesh, basemesh, nls, C, phi, C_prev_time,
C_prev_newton, phi_prev_time, phi_prev_newton);
// bool success = solveNonadaptive(Cmesh, nls, C_prev_time, C_prev_newton,
// phi_prev_time, phi_prev_newton);
if (success) {
printf("SUCCESSFUL\n");
} else {
printf("FAIL\n");
}
#define ERROR_SUCCESS 0
#define ERROR_FAILURE -1
return success ? ERROR_SUCCESS : ERROR_FAILURE;
}
示例2: main
int main(int argc, char **argv)
{
// Instantiate a class with global functions.
Hermes2D hermes2d;
// Time measurement.
TimePeriod cpu_time;
cpu_time.tick();
// Load the mesh.
Mesh mesh;
H2DReader mloader;
mloader.load("square.mesh", &mesh);
// Perform initial mesh refinemets.
for (int i = 0; i < INIT_REF_NUM; i++) mesh.refine_all_elements();
// Set exact solution.
CustomExactSolution exact(&mesh);
// Initialize boundary conditions
DefaultEssentialBCNonConst bc_essential("Bdy", &exact);
EssentialBCs bcs(&bc_essential);
// Initialize the weak formulation.
CustomWeakFormPoisson wf1;
// Create an H1 space with default shapeset.
H1Space space(&mesh, &bcs, P_INIT);
int ndof = Space::get_num_dofs(&space);
info("ndof: %d", ndof);
info("---- Assembling by DiscreteProblem, solving by %s:",
MatrixSolverNames[matrix_solver].c_str());
// Initialize the linear discrete problem.
DiscreteProblem dp1(&wf1, &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);
if (matrix_solver == SOLVER_AZTECOO)
{
((AztecOOSolver*) solver)->set_solver(iterative_method);
((AztecOOSolver*) solver)->set_precond(preconditioner);
// Using default iteration parameters (see solver/aztecoo.h).
}
// Begin time measurement of assembly.
cpu_time.tick(HERMES_SKIP);
// Initial coefficient vector for the Newton's method.
scalar* coeff_vec = new scalar[ndof];
memset(coeff_vec, 0, ndof*sizeof(scalar));
// Perform Newton's iteration.
if (!hermes2d.solve_newton(coeff_vec, &dp1, solver, matrix, rhs)) error("Newton's iteration failed.");
// Translate the resulting coefficient vector into the Solution sln.
Solution sln1;
Solution::vector_to_solution(coeff_vec, &space, &sln1);
// CPU time measurement.
double time = cpu_time.tick().last();
// Calculate errors.
double rel_err_1 = hermes2d.calc_rel_error(&sln1, &exact, HERMES_H1_NORM) * 100;
info("CPU time: %g s.", time);
info("Exact H1 error: %g%%.", rel_err_1);
delete(matrix);
delete(rhs);
delete(solver);
// View the solution and mesh.
ScalarView sview("Solution", new WinGeom(0, 0, 440, 350));
sview.show(&sln1);
//OrderView oview("Polynomial orders", new WinGeom(450, 0, 400, 350));
//oview.show(&space);
// TRILINOS PART:
info("---- Assembling by DiscreteProblem, solving by NOX:");
// Initialize the weak formulation for Trilinos.
CustomWeakFormPoisson wf2(TRILINOS_JFNK);
// Initialize DiscreteProblem.
DiscreteProblem dp2(&wf2, &space);
// Time measurement.
cpu_time.tick(HERMES_SKIP);
// Set initial vector for NOX.
// NOTE: Using zero vector was causing convergence problems.
info("Projecting to obtain initial vector for the Newton's method.");
Solution init_sln(&mesh, 0.0);
OGProjection::project_global(&space, &init_sln, coeff_vec);
//.........这里部分代码省略.........
示例3: 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 refinements.
for(int i=0; i < INIT_REF_NUM; i++) mesh.refine_all_elements();
// Initialize boundary conditions
DefaultEssentialBCConst bc_essential("Bottom", T1);
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.
CustomWeakFormPoissonNewton wf(LAMBDA, ALPHA, T0, "Heat_flux");
// Initialize the FE problem.
DiscreteProblem dp(&wf, &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);
// Initial coefficient vector for the Newton's method.
scalar* coeff_vec = new scalar[ndof];
memset(coeff_vec, 0, ndof*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 sln;
Solution::vector_to_solution(coeff_vec, &space, &sln);
// Visualize the solution.
ScalarView view("Solution", new WinGeom(0, 0, 300, 400));
view.show(&sln, HERMES_EPS_VERYHIGH);
ScalarView gradview("Gradient", new WinGeom(310, 0, 300, 400));
MagFilter grad(Hermes::vector<MeshFunction *>(&sln, &sln),
Hermes::vector<int>(H2D_FN_DX, H2D_FN_DY));
gradview.show(&grad, HERMES_EPS_VERYHIGH);
// Wait for all views to be closed.
View::wait();
// Clean up.
delete solver;
delete matrix;
delete rhs;
delete [] coeff_vec;
return 0;
}
示例4: main
int main(int argc, char* argv[])
{
/* So far the DG assembling is very slow for higher order polynomials,
so only constant functions are used here.
if(argc < 3)
error("Too few arguments in example-euler-gamm-explicit");
Ord2 P_INIT= Ord2(atoi(argv[1]), atoi(argv[2]));
*/
Ord2 P_INIT= Ord2(0, 0);
// Load the mesh.
Mesh mesh;
H2DReader mloader;
mloader.load("GAMM-channel.mesh", &mesh);
// Perform initial mesh refinements.
for (int i = 0; i < INIT_REF_NUM; i++) mesh.refine_all_elements();
mesh.refine_towards_boundary(1, 1);
mesh.refine_element_id(1053);
mesh.refine_element_id(1054);
mesh.refine_element_id(1087);
mesh.refine_element_id(1088);
mesh.refine_element_id(1117);
mesh.refine_element_id(1118);
mesh.refine_element_id(1151);
mesh.refine_element_id(1152);
// Enter boundary markers.
BCTypes bc_types;
bc_types.add_bc_neumann(Hermes::vector<int>(BDY_SOLID_WALL, BDY_INLET_OUTLET));
// Create L2 spaces with default shapesets.
L2Space space_rho(&mesh, &bc_types, P_INIT);
L2Space space_rho_v_x(&mesh, &bc_types, P_INIT);
L2Space space_rho_v_y(&mesh, &bc_types, P_INIT);
L2Space space_e(&mesh, &bc_types, P_INIT);
// Initialize solutions, set initial conditions.
Solution sln_rho, sln_rho_v_x, sln_rho_v_y, sln_e, prev_rho, prev_rho_v_x, prev_rho_v_y, prev_e;
sln_rho.set_exact(&mesh, ic_density);
sln_rho_v_x.set_exact(&mesh, ic_density_vel_x);
sln_rho_v_y.set_exact(&mesh, ic_density_vel_y);
sln_e.set_exact(&mesh, ic_energy);
prev_rho.set_exact(&mesh, ic_density);
prev_rho_v_x.set_exact(&mesh, ic_density_vel_x);
prev_rho_v_y.set_exact(&mesh, ic_density_vel_y);
prev_e.set_exact(&mesh, ic_energy);
// Initialize weak formulation.
WeakForm wf(4);
// Bilinear forms coming from time discretization by explicit Euler's method.
wf.add_matrix_form(0, 0, callback(bilinear_form_0_0_time));
wf.add_matrix_form(1, 1, callback(bilinear_form_1_1_time));
wf.add_matrix_form(2, 2, callback(bilinear_form_2_2_time));
wf.add_matrix_form(3, 3, callback(bilinear_form_3_3_time));
// Volumetric linear forms.
// Linear forms coming from the linearization by taking the Eulerian fluxes' Jacobian matrices
// from the previous time step.
// Unnecessary for FVM.
if(P_INIT.order_h > 0 || P_INIT.order_v > 0) {
// First flux.
wf.add_vector_form(0, callback(linear_form_0_1), HERMES_ANY, Hermes::vector<MeshFunction*>(&prev_rho_v_x));
wf.add_vector_form(1, callback(linear_form_1_0_first_flux), HERMES_ANY,
Hermes::vector<MeshFunction*>(&prev_rho, &prev_rho_v_x, &prev_rho_v_y));
wf.add_vector_form(1, callback(linear_form_1_1_first_flux), HERMES_ANY,
Hermes::vector<MeshFunction*>(&prev_rho, &prev_rho_v_x, &prev_rho_v_y));
wf.add_vector_form(1, callback(linear_form_1_2_first_flux), HERMES_ANY,
Hermes::vector<MeshFunction*>(&prev_rho, &prev_rho_v_x, &prev_rho_v_y));
wf.add_vector_form(1, callback(linear_form_1_3_first_flux), HERMES_ANY,
Hermes::vector<MeshFunction*>(&prev_rho, &prev_rho_v_x, &prev_rho_v_y, &prev_e));
wf.add_vector_form(2, callback(linear_form_2_0_first_flux), HERMES_ANY,
Hermes::vector<MeshFunction*>(&prev_rho, &prev_rho_v_x, &prev_rho_v_y));
wf.add_vector_form(2, callback(linear_form_2_1_first_flux), HERMES_ANY,
Hermes::vector<MeshFunction*>(&prev_rho, &prev_rho_v_x, &prev_rho_v_y));
wf.add_vector_form(2, callback(linear_form_2_2_first_flux), HERMES_ANY,
Hermes::vector<MeshFunction*>(&prev_rho, &prev_rho_v_x, &prev_rho_v_y));
wf.add_vector_form(2, callback(linear_form_2_3_first_flux), HERMES_ANY,
Hermes::vector<MeshFunction*>(&prev_rho, &prev_rho_v_x, &prev_rho_v_y, &prev_e));
wf.add_vector_form(3, callback(linear_form_3_0_first_flux), HERMES_ANY,
Hermes::vector<MeshFunction*>(&prev_rho, &prev_rho_v_x, &prev_rho_v_y, &prev_e));
wf.add_vector_form(3, callback(linear_form_3_1_first_flux), HERMES_ANY,
Hermes::vector<MeshFunction*>(&prev_rho, &prev_rho_v_x, &prev_rho_v_y, &prev_e));
wf.add_vector_form(3, callback(linear_form_3_2_first_flux), HERMES_ANY,
Hermes::vector<MeshFunction*>(&prev_rho, &prev_rho_v_x, &prev_rho_v_y, &prev_e));
wf.add_vector_form(3, callback(linear_form_3_3_first_flux), HERMES_ANY,
Hermes::vector<MeshFunction*>(&prev_rho, &prev_rho_v_x, &prev_rho_v_y, &prev_e));
// Second flux.
wf.add_vector_form(0, callback(linear_form_0_2), HERMES_ANY, Hermes::vector<MeshFunction*>(&prev_rho_v_y));
wf.add_vector_form(1, callback(linear_form_1_0_second_flux), HERMES_ANY,
Hermes::vector<MeshFunction*>(&prev_rho, &prev_rho_v_x, &prev_rho_v_y));
wf.add_vector_form(1, callback(linear_form_1_1_second_flux), HERMES_ANY,
Hermes::vector<MeshFunction*>(&prev_rho, &prev_rho_v_x, &prev_rho_v_y));
wf.add_vector_form(1, callback(linear_form_1_2_second_flux), HERMES_ANY,
Hermes::vector<MeshFunction*>(&prev_rho, &prev_rho_v_x, &prev_rho_v_y));
//.........这里部分代码省略.........
示例5: main
int main(int argc, char* argv[])
{
// Load the mesh
Mesh mesh;
H2DReader mloader;
mloader.load("domain.mesh", &mesh);
mesh.refine_all_elements();
// Initialize the shapeset and the cache
H1Shapeset shapeset;
PrecalcShapeset pss(&shapeset);
// Create finite element space
H1Space space(&mesh, &shapeset);
space.set_bc_types(bc_types);
space.set_bc_values(bc_values);
space.set_uniform_order(P_INIT);
// Enumerate basis functions
space.assign_dofs();
// initialize the weak formulation
WeakForm wf(1);
wf.add_biform(0, 0, bilinear_form, bilinear_form_ord, SYM);
wf.add_liform(0, linear_form, linear_form_ord);
wf.add_liform_surf(0, linear_form_surf, linear_form_surf_ord, 2);
// Visualize solution and mesh
ScalarView sview("Coarse solution", 0, 100, 798, 700);
OrderView oview("Polynomial orders", 800, 100, 798, 700);
// Matrix solver
UmfpackSolver solver;
// DOF and CPU convergence graphs
SimpleGraph graph_dof, graph_cpu;
// Adaptivity loop
int it = 1, ndofs;
bool done = false;
double cpu = 0.0;
Solution sln_coarse, sln_fine;
do
{
info("\n---- Adaptivity step %d ---------------------------------------------\n", it++);
// Time measurement
begin_time();
// Solve the coarse mesh problem
LinSystem ls(&wf, &solver);
ls.set_spaces(1, &space);
ls.set_pss(1, &pss);
ls.assemble();
ls.solve(1, &sln_coarse);
// Time measurement
cpu += end_time();
// View the solution and mesh
sview.show(&sln_coarse);
oview.show(&space);
// Time measurement
begin_time();
// Solve the fine mesh problem
RefSystem rs(&ls);
rs.assemble();
rs.solve(1, &sln_fine);
// Calculate error estimate wrt. fine mesh solution
H1OrthoHP hp(1, &space);
double err_est = hp.calc_error(&sln_coarse, &sln_fine) * 100;
info("Estimate of error: %g%%", err_est);
// add entry to DOF convergence graph
graph_dof.add_values(space.get_num_dofs(), err_est);
graph_dof.save("conv_dof.dat");
// add entry to CPU convergence graph
graph_cpu.add_values(cpu, err_est);
graph_cpu.save("conv_cpu.dat");
// If err_est too large, adapt the mesh
if (err_est < ERR_STOP) done = true;
else {
hp.adapt(THRESHOLD, STRATEGY, ADAPT_TYPE, ISO_ONLY, MESH_REGULARITY);
ndofs = space.assign_dofs();
if (ndofs >= NDOF_STOP) done = true;
}
// Time measurement
cpu += end_time();
}
while (done == false);
verbose("Total running time: %g sec", cpu);
// Show the fine solution - this is the final result
sview.set_title("Final solution");
//.........这里部分代码省略.........
示例6: 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); // quadrilaterals
// mloader.load("square_tri.mesh", &mesh); // triangles
// Perform initial mesh refinements.
for (int i = 0; i<INIT_REF_NUM; i++) mesh.refine_all_elements();
// Define exact solution.
CustomExactSolution exact_sln(&mesh, SLOPE);
// Initialize the weak formulation.
CustomWeakFormPoisson wf(SLOPE);
// Initialize boundary conditions.
DefaultEssentialBCNonConst bc_essential(BDY_DIRICHLET, &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, 450, 350));
sview.show_mesh(false);
sview.fix_scale_width(60);
OrderView oview("Polynomial orders", new WinGeom(460, 0, 410, 350));
// 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.
Space* ref_space = Space::construct_refined_space(&space);
// Set up the solver, matrix, and rhs according to the solver selection.
SparseMatrix* matrix = create_matrix(matrix_solver);
Vector* rhs = create_vector(matrix_solver);
Solver* solver = create_linear_solver(matrix_solver, matrix, rhs);
// Assemble the reference problem.
info("Solving on reference mesh.");
bool is_linear = true;
DiscreteProblem* dp = new DiscreteProblem(&wf, ref_space, is_linear);
dp->assemble(matrix, rhs);
// Time measurement.
cpu_time.tick();
// Solve the linear system of the reference problem. If successful, obtain the solution.
Solution ref_sln;
if(solver->solve()) Solution::vector_to_solution(solver->get_solution(), ref_space, &ref_sln);
else error ("Matrix solver failed.\n");
// Time measurement.
cpu_time.tick();
// Project the fine mesh solution onto the coarse mesh.
Solution sln;
info("Projecting reference solution on coarse mesh.");
OGProjection::project_global(&space, &ref_sln, &sln, matrix_solver);
// 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.
//.........这里部分代码省略.........
示例7: main
int main(int argc, char* argv[])
{
// Load the mesh.
Mesh mesh;
H2DReader mloader;
mloader.load("domain.mesh", &mesh);
// Perform initial mesh refinements.
mesh.refine_towards_vertex(3, CORNER_REF_LEVEL);
// Enter boundary markers.
BCTypes bc_types;
bc_types.add_bc_dirichlet(BDY_INNER);
bc_types.add_bc_neumann(Hermes::Tuple<int>(BDY_BOTTOM, BDY_OUTER, BDY_LEFT));
// Enter Dirichlet boudnary values.
BCValues bc_values;
bc_values.add_zero(BDY_INNER);
// Create an H1 space with default shapeset.
H1Space space(&mesh, &bc_types, &bc_values, P_INIT);
int ndof = Space::get_num_dofs(&space);
info("ndof = %d", ndof);
// Initialize the weak formulation.
WeakForm wf;
wf.add_matrix_form(callback(bilinear_form));
wf.add_vector_form(callback(linear_form));
wf.add_vector_form_surf(callback(linear_form_surf_bottom), BDY_BOTTOM);
wf.add_vector_form_surf(callback(linear_form_surf_outer), BDY_OUTER);
wf.add_vector_form_surf(callback(linear_form_surf_left), BDY_LEFT);
// Initialize the FE problem.
bool is_linear = true;
DiscreteProblem dp(&wf, &space, is_linear);
// Set up the solver, matrix, and rhs according to the solver selection.
SparseMatrix* matrix = create_matrix(matrix_solver);
Vector* rhs = create_vector(matrix_solver);
Solver* solver = create_linear_solver(matrix_solver, matrix, rhs);
// Initialize the solution.
Solution sln;
// Assemble the stiffness matrix and right-hand side vector.
info("Assembling the stiffness matrix and right-hand side vector.");
dp.assemble(matrix, rhs);
// Solve the linear system and if successful, obtain the solution.
info("Solving the matrix problem.");
if(solver->solve())
Solution::vector_to_solution(solver->get_solution(), &space, &sln);
else
error ("Matrix solver failed.\n");
// Visualize the approximation.
ScalarView view("Solution", new WinGeom(0, 0, 440, 350));
view.show(&sln);
// Compute and show gradient magnitude.
// (Note that the gradient at the re-entrant
// corner needs to be truncated for visualization purposes.)
ScalarView gradview("Gradient", new WinGeom(450, 0, 400, 350));
MagFilter grad(Hermes::Tuple<MeshFunction *>(&sln, &sln), Hermes::Tuple<int>(H2D_FN_DX, H2D_FN_DY));
gradview.show(&grad);
// Wait for the views to be closed.
View::wait();
// Clean up.
delete solver;
delete matrix;
delete rhs;
return 0;
}
示例8: main
int main(int argc, char* argv[])
{
// Time measurement.
TimePeriod cpu_time;
cpu_time.tick();
// Load the mesh.
Mesh mesh;
H2DReader mloader;
mloader.load("square.mesh", &mesh);
// Perform initial mesh refinements.
for (int i=0; i<INIT_REF; i++) mesh.refine_all_elements();
// Create an L2 space with default shapeset.
L2Space space(&mesh, bc_types, NULL, Ord2(P_H, P_V));
int ndof = Space::get_num_dofs(&space);
info("ndof = %d", ndof);
// Initialize the weak formulation.
WeakForm wf;
wf.add_matrix_form(callback(bilinear_form));
wf.add_vector_form(callback(linear_form));
wf.add_matrix_form_surf(callback(bilinear_form_boundary), H2D_DG_BOUNDARY_EDGE);
wf.add_vector_form_surf(callback(linear_form_boundary), H2D_DG_BOUNDARY_EDGE);
wf.add_matrix_form_surf(callback(bilinear_form_interface), H2D_DG_INNER_EDGE);
// Initialize the FE problem.
bool is_linear = true;
DiscreteProblem dp(&wf, &space, is_linear);
// Set up the solver, matrix, and rhs according to the solver selection.
SparseMatrix* matrix = create_matrix(matrix_solver);
Vector* rhs = create_vector(matrix_solver);
Solver* solver = create_linear_solver(matrix_solver, matrix, rhs);
// Initialize the preconditioner in the case of SOLVER_AZTECOO.
if (matrix_solver == SOLVER_AZTECOO)
{
((AztecOOSolver*) solver)->set_solver(iterative_method);
((AztecOOSolver*) solver)->set_precond(preconditioner);
// Using default iteration parameters (see solver/aztecoo.h).
}
// Initialize the solution.
Solution sln;
// Assemble the stiffness matrix and right-hand side vector.
info("Assembling the stiffness matrix and right-hand side vector.");
dp.assemble(matrix, rhs);
// Solve the linear system and if successful, obtain the solution.
info("Solving the matrix problem.");
if(solver->solve())
Solution::vector_to_solution(solver->get_solution(), &space, &sln);
else
error ("Matrix solver failed.\n");
// Time measurement.
cpu_time.tick();
// Clean up.
delete solver;
delete matrix;
delete rhs;
info("ndof = %d", ndof);
info("Coordinate ( 0.1, 0.1) value = %lf", sln.get_pt_value(0.1, 0.1));
info("Coordinate ( 0.3, 0.3) value = %lf", sln.get_pt_value(0.3, 0.3));
info("Coordinate ( 0.5, 0.5) value = %lf", sln.get_pt_value(0.5, 0.5));
info("Coordinate ( 0.7, 0.7) value = %lf", sln.get_pt_value(0.7, 0.7));
double coor_xy[4] = {0.1, 0.3, 0.5, 0.7};
double value[4] = {0.999885, 0.844340, 0.000000, 0.000000};
for (int i = 0; i < 4; i++)
{
if ((value[i] - sln.get_pt_value(coor_xy[i], coor_xy[i])) < 1E-6)
{
printf("Success!\n");
}
else
{
printf("Failure!\n");
return ERR_FAILURE;
}
}
return ERR_SUCCESS;
}
示例9: 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 uniform mesh refinement.
mesh.refine_all_elements();
// Initialize boundary conditions.
DefaultEssentialBCConst zero_disp("Bottom", 0.0);
EssentialBCs bcs(&zero_disp);
// Create x- and y- displacement space using the default H1 shapeset.
H1Space u1_space(&mesh, &bcs, P_INIT);
H1Space u2_space(&mesh, &bcs, P_INIT);
info("ndof = %d.", Space::get_num_dofs(Hermes::vector<Space *>(&u1_space, &u2_space)));
// Initialize the weak formulation.
CustomWeakFormLinearElasticity wf(E, nu, rho*g1, "Top", f0, f1);
// Testing n_dof and correctness of solution vector
// for p_init = 1, 2, ..., 10
int success = 1;
Solution xsln, ysln;
for (int p_init = 1; p_init <= 6; p_init++) {
printf("********* p_init = %d *********\n", p_init);
u1_space.set_uniform_order(p_init);
u2_space.set_uniform_order(p_init);
int ndof = Space::get_num_dofs(Hermes::vector<Space *>(&u1_space, &u2_space));
info("ndof = %d", ndof);
// Initialize the FE problem.
DiscreteProblem dp(&wf, Hermes::vector<Space *>(&u1_space, &u2_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);
// Initial coefficient vector for the Newton's method.
scalar* coeff_vec = new scalar[ndof];
memset(coeff_vec, 0, ndof*sizeof(scalar));
// Perform Newton's iteration.
bool verbose = true;
bool jacobian_changed = true;
if (!hermes2d.solve_newton(coeff_vec, &dp, solver, matrix, rhs, jacobian_changed,
NEWTON_TOL, NEWTON_MAX_ITER, verbose)) error("Newton's iteration failed.");
// Translate the resulting coefficient vector into the Solution sln.
Solution u1_sln, u2_sln;
Solution::vector_to_solutions(coeff_vec, Hermes::vector<Space *>(&u1_space, &u2_space),
Hermes::vector<Solution *>(&u1_sln, &u2_sln));
double sum = 0;
for (int i=0; i < ndof; i++) sum += coeff_vec[i];
printf("coefficient sum = %g\n", sum);
// Actual test. The values of 'sum' depend on the
// current shapeset. If you change the shapeset,
// you need to correct these numbers.
if (p_init == 1 && fabs(sum - 1.41886e-05) > 1e-5) success = 0;
if (p_init == 2 && fabs(sum - 1.60006e-05) > 1e-5) success = 0;
if (p_init == 3 && fabs(sum - 1.60810e-05) > 1e-5) success = 0;
if (p_init == 4 && fabs(sum - 1.61106e-05) > 1e-5) success = 0;
if (p_init == 5 && fabs(sum - 1.61065e-05) > 1e-5) success = 0;
if (p_init == 6 && fabs(sum - 1.61112e-05) > 1e-5) success = 0;
delete [] coeff_vec;
delete solver;
delete matrix;
delete rhs;
}
if (success == 1) {
printf("Success!\n");
return ERR_SUCCESS;
}
else {
printf("Failure!\n");
return ERR_FAILURE;
}
}
示例10: main
int main(int argc, char* argv[])
{
// load the mesh file
Mesh mesh;
H2DReader mloader;
mloader.load("sample.mesh", &mesh);
// initialize the shapeset and the cache
H1Shapeset shapeset;
PrecalcShapeset pss(&shapeset);
// create the x displacement space
H1Space xdisp(&mesh, &shapeset);
xdisp.set_bc_types(bc_types);
xdisp.set_bc_values(bc_values);
// create the y displacement space
H1Space ydisp(&mesh, &shapeset);
ydisp.set_bc_types(bc_types);
ydisp.set_bc_values(bc_values);
// initialize the weak formulation
WeakForm wf(2);
wf.add_biform(0, 0, callback(bilinear_form_0_0), SYM); // Note that only one symmetric part is
wf.add_biform(0, 1, callback(bilinear_form_0_1), SYM); // added in the case of symmetric bilinear
wf.add_biform(1, 1, callback(bilinear_form_1_1), SYM); // forms.
wf.add_liform_surf(0, callback(linear_form_surf_0), 3);
wf.add_liform_surf(1, callback(linear_form_surf_1), 3);
// initialize the linear system and solver
UmfpackSolver umfpack;
LinSystem sys(&wf, &umfpack);
sys.set_spaces(2, &xdisp, &ydisp);
sys.set_pss(1, &pss);
// testing n_dof and correctness of solution vector
// for p_init = 1, 2, ..., 10
int success = 1;
for (int p_init = 1; p_init <= 10; p_init++) {
printf("********* p_init = %d *********\n", p_init);
xdisp.set_uniform_order(p_init);
int ndofs = xdisp.assign_dofs(0);
ydisp.set_uniform_order(p_init);
ndofs += ydisp.assign_dofs(ndofs);
// assemble the stiffness matrix and solve the system
Solution xsln, ysln;
sys.assemble();
sys.solve(2, &xsln, &ysln);
scalar *sol_vector;
int n_dof;
sys.get_solution_vector(sol_vector, n_dof);
printf("n_dof = %d\n", n_dof);
double sum = 0;
for (int i=0; i < n_dof; i++) sum += sol_vector[i];
printf("coefficient sum = %g\n", sum);
// Actual test. The values of 'sum' depend on the
// current shapeset. If you change the shapeset,
// you need to correct these numbers.
if (p_init == 1 && fabs(sum - 3.50185e-06) > 1e-3) success = 0;
if (p_init == 2 && fabs(sum - 4.34916e-06) > 1e-3) success = 0;
if (p_init == 3 && fabs(sum - 4.60553e-06) > 1e-3) success = 0;
if (p_init == 4 && fabs(sum - 4.65616e-06) > 1e-3) success = 0;
if (p_init == 5 && fabs(sum - 4.62893e-06) > 1e-3) success = 0;
if (p_init == 6 && fabs(sum - 4.64336e-06) > 1e-3) success = 0;
if (p_init == 7 && fabs(sum - 4.63724e-06) > 1e-3) success = 0;
if (p_init == 8 && fabs(sum - 4.64491e-06) > 1e-3) success = 0;
if (p_init == 9 && fabs(sum - 4.64582e-06) > 1e-3) success = 0;
if (p_init == 10 && fabs(sum - 4.65028e-06) > 1e-3) success = 0;
}
#define ERROR_SUCCESS 0
#define ERROR_FAILURE -1
if (success == 1) {
printf("Success!\n");
return ERROR_SUCCESS;
}
else {
printf("Failure!\n");
return ERROR_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 mesh refinements (optional).
for (int i = 0; i < INIT_REF_NUM; i++) mesh.refine_all_elements();
// Initialize the weak formulation.
CustomWeakFormPoissonDirichlet wf("Aluminum", LAMBDA_AL, "Copper",
LAMBDA_CU, VOLUME_HEAT_SRC);
// Initialize boundary conditions.
CustomDirichletCondition bc_essential(Hermes::vector<std::string>("Bottom", "Inner", "Outer", "Left"),
BDY_A_PARAM, BDY_B_PARAM, BDY_C_PARAM);
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 FE problem.
DiscreteProblem dp(&wf, &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);
// Initial coefficient vector for the Newton's method.
scalar* coeff_vec = new scalar[ndof];
memset(coeff_vec, 0, ndof*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 sln;
Solution::vector_to_solution(coeff_vec, &space, &sln);
// VTK output.
if (VTK_VISUALIZATION) {
// Output solution in VTK format.
Linearizer lin;
bool mode_3D = true;
lin.save_solution_vtk(&sln, "sln.vtk", "Temperature", mode_3D);
info("Solution in VTK format saved to file %s.", "sln.vtk");
// Output mesh and element orders in VTK format.
Orderizer ord;
ord.save_orders_vtk(&space, "ord.vtk");
info("Element orders in VTK format saved to file %s.", "ord.vtk");
}
ndof = Space::get_num_dofs(&space);
printf("ndof = %d\n", ndof);
double sum = 0;
for (int i=0; i < ndof; i++) sum += coeff_vec[i];
printf("coefficient sum = %g\n", sum);
bool success = true;
if (fabs(sum + 4.2471) > 1e-3) success = 0;
if (success == 1) {
printf("Success!\n");
return ERR_SUCCESS;
}
else {
printf("Failure!\n");
return ERR_FAILURE;
}
}
示例12: main
int main (int argc, char* argv[]) {
// Initialize the library's global functions.
Hermes2D hermes2D;
// Load the mesh file.
Mesh C_mesh, phi_mesh, u1_mesh, u2_mesh, basemesh;
H2DReader mloader;
mloader.load("small.mesh", &basemesh);
#ifdef TWO_BASE_MESH
Mesh basemesh_electrochem; // Base mesh to hold C and phi
Mesh basemesh_deformation; // Base mesh for deformation displacements u1 and u2
basemesh_electrochem.copy(&basemesh);
basemesh_deformation.copy(&basemesh);
basemesh_electrochem.refine_towards_boundary(BDY_BOT, REF_INIT - 1);
basemesh_electrochem.refine_all_elements(1);
C_mesh.copy(&basemesh_electrochem);
phi_mesh.copy(&basemesh_electrochem);
for (int i = 0; i < REF_INIT - 1; i++) {
basemesh_deformation.refine_all_elements(1); //horizontal
basemesh_deformation.refine_all_elements(2); //vertical
}
u1_mesh.copy(&basemesh_deformation);
u2_mesh.copy(&basemesh_deformation);
#else
basemesh.refine_towards_boundary(BDY_BOT, REF_INIT);
basemesh.refine_towards_boundary(BDY_SIDE_FIXED, REF_INIT);
basemesh.refine_towards_boundary(BDY_SIDE_FREE, REF_INIT - 1);
basemesh.refine_all_elements(1);
C_mesh.copy(&basemesh);
phi_mesh.copy(&basemesh);
u1_mesh.copy(&basemesh);
u2_mesh.copy(&basemesh);
#endif
// Enter Dirichlet and Neumann boundary markers for Poisson.
DefaultEssentialBCConst bc_phi_voltage(BDY_TOP, VOLTAGE);
DefaultEssentialBCConst bc_phi_zero(BDY_BOT, 0.0);
EssentialBCs bcs_phi(Hermes::vector<EssentialBC*>(&bc_phi_voltage, &bc_phi_zero));
DefaultEssentialBCConst bc_u1(BDY_SIDE_FIXED, 0.0);
EssentialBCs bcs_u1(&bc_u1);
DefaultEssentialBCConst bc_u2(BDY_SIDE_FIXED, 0.0);
EssentialBCs bcs_u2(&bc_u2);
// Spaces for concentration and the voltage.
H1Space C_space(&C_mesh, P_INIT);
H1Space phi_space(MULTIMESH ? &phi_mesh : &C_mesh, &bcs_phi, P_INIT);
H1Space u1_space(MULTIMESH ? &u1_mesh : &C_mesh, &bcs_u1, P_INIT);
H1Space u2_space(MULTIMESH ? &u2_mesh : &C_mesh, &bcs_u2, P_INIT);
int ndof = Space::get_num_dofs(Hermes::vector<Space*>(&C_space, &phi_space, &u1_space, &u2_space));
Solution C_sln, C_ref_sln;
Solution phi_sln, phi_ref_sln;
Solution u1_sln, u1_ref_sln;
Solution u2_sln, u2_ref_sln;
// Assign initial condition to mesh.
InitialSolutionConcentration C_prev_time(&C_mesh, C0);
InitialSolutionVoltage phi_prev_time(MULTIMESH ? &phi_mesh : &C_mesh);
InitialSolutionU1 u1_prev_time(MULTIMESH ? &u1_mesh : &C_mesh);
InitialSolutionU2 u2_prev_time(MULTIMESH ? &u2_mesh : &C_mesh);
// The weak form for 2 equations.
CustomWeakFormNernstPlanckEuler wf(TAU, C0, lin_force_coup, mech_lambda, mech_mu, K, L, D, &C_prev_time);
// Add the bilinear and linear forms.
if (TIME_DISCR == 2)
error("Crank-Nicholson forms are not implemented yet");
// Project the initial condition on the FE space to obtain initial
// coefficient vector for the Newton's method.
info("Projecting initial condition to obtain initial vector for the Newton's method.");
scalar* coeff_vec_coarse = new scalar[ndof];
OGProjection::project_global(Hermes::vector<Space *>(&C_space, &phi_space, &u1_space, &u2_space),
Hermes::vector<MeshFunction *>(&C_prev_time, &phi_prev_time, &u1_prev_time, &u2_prev_time),
coeff_vec_coarse, matrix_solver);
// Initialize the FE problem.
bool is_linear = false;
DiscreteProblem dp_coarse(&wf, Hermes::vector<Space *>(&C_space, &phi_space, &u1_space, &u2_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);
// Visualization windows.
char title[1000];
ScalarView Cview("Concentration [mol/m3]", new WinGeom(0, 0, 400, 300));
//.........这里部分代码省略.........
示例13: 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();
// Enter boundary markers.
BCTypes bc_types;
bc_types.add_bc_dirichlet(BDY_HORIZONTAL);
bc_types.add_bc_neumann(BDY_VERTICAL);
// Enter Dirichlet boundary values.
BCValues bc_values;
bc_values.add_function(BDY_HORIZONTAL, 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(bilinear_form, bilinear_form_ord, HERMES_SYM);
wf.add_vector_form(linear_form, linear_form_ord);
wf.add_vector_form_surf(linear_form_surf, linear_form_surf_ord, BDY_VERTICAL);
// Initialize coarse and reference mesh solution.
Solution sln, ref_sln;
// Initialize refinement selector.
H1ProjBasedSelector selector(CAND_LIST, CONV_EXP, H2DRS_DEFAULT_ORDER);
// DOF and CPU convergence graphs initialization.
SimpleGraph graph_dof, graph_cpu;
// Adaptivity loop:
int as = 1;
bool done = false;
do
{
info("---- Adaptivity step %d:", as);
// Construct globally refined reference mesh and setup reference space.
Space* ref_space = construct_refined_space(&space);
// Assemble the reference problem.
info("Solving on reference mesh.");
bool is_linear = true;
DiscreteProblem* dp = new DiscreteProblem(&wf, ref_space, is_linear);
SparseMatrix* matrix = create_matrix(matrix_solver);
Vector* rhs = create_vector(matrix_solver);
Solver* solver = create_linear_solver(matrix_solver, matrix, rhs);
dp->assemble(matrix, rhs);
// Time measurement.
cpu_time.tick();
// Solve the linear system of the reference problem. If successful, obtain the solution.
if(solver->solve()) Solution::vector_to_solution(solver->get_solution(), ref_space, &ref_sln);
else error ("Matrix solver failed.\n");
// Time measurement.
cpu_time.tick();
// Project the fine mesh solution onto the coarse mesh.
info("Projecting reference solution on coarse mesh.");
OGProjection::project_global(&space, &ref_sln, &sln, matrix_solver);
// Calculate element errors and total error estimate.
info("Calculating error estimate.");
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
{
info("Adapting coarse mesh.");
done = adaptivity->adapt(&selector, THRESHOLD, STRATEGY, MESH_REGULARITY);
}
if (Space::get_num_dofs(&space) >= NDOF_STOP) done = true;
//.........这里部分代码省略.........
示例14: main
int main(int argc, char* argv[])
{
Hermes2D hermes_2D;
// Load the mesh.
Mesh mesh;
H2DReader mloader;
mloader.load("domain.mesh", &mesh);
// Initial mesh refinements.
for (int i=0; i < 4; i++) mesh.refine_all_elements();
mesh.refine_towards_boundary(HERMES_ANY, 4);
// Initialize boundary conditions.
DefaultEssentialBCConst zero_vel_bc_x_brl(Hermes::vector<std::string>("Bottom", "Right", "Left"), 0.0);
DefaultEssentialBCConst vel_bc_x_top(Hermes::vector<std::string>("Top"), XVEL_TOP);
EssentialBCs bcs_vel_x(Hermes::vector<EssentialBoundaryCondition*>(&vel_bc_x_top, &zero_vel_bc_x_brl));
DefaultEssentialBCConst zero_vel_bc_y(Hermes::vector<std::string>("Bottom", "Right", "Top", "Left"), 0.0);
EssentialBCs bcs_vel_y(&zero_vel_bc_y);
EssentialBCs bcs_pressure;
// Spaces for velocity components and pressure.
H1Space xvel_space(&mesh, &bcs_vel_x, P_INIT_VEL);
H1Space yvel_space(&mesh, &bcs_vel_y, P_INIT_VEL);
#ifdef PRESSURE_IN_L2
L2Space p_space(&mesh, &bcs_pressure, P_INIT_PRESSURE);
#else
H1Space p_space(&mesh, &bcs_pressure, P_INIT_PRESSURE);
#endif
Hermes::vector<Space *> spaces = Hermes::vector<Space *>(&xvel_space, &yvel_space, &p_space);
// Calculate and report the number of degrees of freedom.
int ndof = Space::get_num_dofs(Hermes::vector<Space *>(&xvel_space, &yvel_space, &p_space));
info("ndof = %d.", ndof);
// Define projection norms.
ProjNormType vel_proj_norm = HERMES_H1_NORM;
#ifdef PRESSURE_IN_L2
ProjNormType p_proj_norm = HERMES_L2_NORM;
#else
ProjNormType p_proj_norm = HERMES_H1_NORM;
#endif
ProjNormType t_proj_norm = HERMES_H1_NORM;
// Solutions for the Newton's iteration and time stepping.
info("Setting initial conditions.");
Solution xvel_prev_time, yvel_prev_time, p_prev_time;
xvel_prev_time.set_zero(&mesh);
yvel_prev_time.set_zero(&mesh);
p_prev_time.set_zero(&mesh);
Hermes::vector<Solution*> slns = Hermes::vector<Solution*>(&xvel_prev_time, &yvel_prev_time,
&p_prev_time);
// Initialize weak formulation.
WeakForm* wf = new WeakFormDrivenCavity(Re, "Top", time_step,
&xvel_prev_time, &yvel_prev_time);
// Initialize the FE problem.
DiscreteProblem dp(wf, spaces);
// 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 views.
VectorView vview("velocity", new WinGeom(0, 0, 400, 400));
ScalarView pview("pressure", new WinGeom(410, 0, 400, 400));
//vview.set_min_max_range(0, 1.6);
vview.fix_scale_width(80);
pview.fix_scale_width(80);
pview.show_mesh(true);
// Project the initial condition on the FE space to obtain initial
// coefficient vector for the Newton's method.
scalar* coeff_vec = new scalar[Space::get_num_dofs(spaces)];
info("Projecting initial condition to obtain initial vector for the Newton's method.");
OGProjection::project_global(spaces, slns, coeff_vec, matrix_solver,
Hermes::vector<ProjNormType>(vel_proj_norm, vel_proj_norm, p_proj_norm, t_proj_norm));
// Time-stepping loop:
char title[100];
double current_time = 0;
int num_time_steps = T_FINAL / time_step;
for (int ts = 1; ts <= num_time_steps; ts++)
{
info("---- Time step %d, time = %g:", ts, current_time);
// Perform Newton's iteration.
info("Solving nonlinear problem:");
bool verbose = true;
if (!hermes_2D.solve_newton(coeff_vec, &dp, solver, matrix, rhs,
NEWTON_TOL, NEWTON_MAX_ITER, verbose)) error("Newton's iteration failed.");
// Update previous time level solutions.
Solution::vector_to_solutions(coeff_vec, spaces, slns);
// Show the solution at the end of time step.
sprintf(title, "Velocity, time %g", current_time);
vview.set_title(title);
vview.show(&xvel_prev_time, &yvel_prev_time);
//.........这里部分代码省略.........
示例15: main
int main(int argc, char* argv[])
{
// Load the mesh.
Mesh mesh;
H2DReader mloader;
mloader.load("domain.mesh", &mesh);
// Perform initial mesh refinements.
mesh.refine_element(0);
// Create an H1 space.
H1Space space(&mesh, bc_types, essential_bc_values, P_INIT);
// Initialize the weak formulation.
WeakForm wf;
wf.add_matrix_form(callback(bilinear_form));
wf.add_vector_form(callback(linear_form));
// Initialize the linear system.
LinSystem ls(&wf, &space);
// Testing n_dof and correctness of solution vector
// for p_init = 1, 2, ..., 10
int success = 1;
Solution sln;
for (int p_init = 1; p_init <= 10; p_init++) {
printf("********* p_init = %d *********\n", p_init);
space.set_uniform_order(p_init);
// Assemble and solve the matrix problem.
ls.assemble();
ls.solve(&sln);
scalar *sol_vector;
int n_dof;
ls.get_solution_vector(sol_vector, n_dof);
printf("n_dof = %d\n", n_dof);
double sum = 0;
for (int i=0; i < n_dof; i++) sum += sol_vector[i];
printf("coefficient sum = %g\n", sum);
// Actual test. The values of 'sum' depend on the
// current shapeset. If you change the shapeset,
// you need to correct these numbers.
if (p_init == 1 && fabs(sum - 0.1875) > 1e-3) success = 0;
if (p_init == 2 && fabs(sum + 0.927932) > 1e-3) success = 0;
if (p_init == 3 && fabs(sum + 0.65191) > 1e-3) success = 0;
if (p_init == 4 && fabs(sum + 0.939909) > 1e-3) success = 0;
if (p_init == 5 && fabs(sum + 0.63356) > 1e-3) success = 0;
if (p_init == 6 && fabs(sum + 0.905309) > 1e-3) success = 0;
if (p_init == 7 && fabs(sum + 0.61996) > 1e-3) success = 0;
if (p_init == 8 && fabs(sum + 0.909494) > 1e-3) success = 0;
if (p_init == 9 && fabs(sum + 0.610543) > 1e-3) success = 0;
if (p_init == 10 && fabs(sum + 0.902731) > 1e-3) success = 0;
}
#define ERROR_SUCCESS 0
#define ERROR_FAILURE -1
if (success == 1) {
printf("Success!\n");
return ERROR_SUCCESS;
}
else {
printf("Failure!\n");
return ERROR_FAILURE;
}
}