255{
256
258
259
261 "--enable-petsc, --enable-trilinos, or --enable-eigen");
262
263
264#ifndef LIBMESH_ENABLE_AMR
265 libmesh_example_requires(false, "--enable-amr");
266#else
267
269
270
271 {
272 std::ifstream i("general.in");
273 libmesh_error_msg_if(!i,
'[' <<
init.comm().rank() <<
"] Can't find general.in; exiting early.");
274 }
275
276
277 GetPot infile("general.in");
278
279
280 infile.parse_command_line(argc, argv);
281
283 param.read(infile);
284
285
286 libmesh_example_requires(2 <= LIBMESH_DIM, "2D support");
287
288
289
291
292
293 std::unique_ptr<MeshRefinement> mesh_refinement =
295
296
298
299 libMesh::out <<
"Reading in and building the mesh" << std::endl;
300
301
302 mesh.
read(param.domainfile.c_str());
303
304
306
307
308
310 initial_uniform_refinements.uniformly_refine(param.coarserefinements);
311
313
314
316
317
319
321
322 equation_systems.init ();
323
324
326 equation_systems.print_info();
328
329 {
330
331 unsigned int a_step = 0;
332 for (; a_step != param.max_adaptivesteps; ++a_step)
333 {
334
335
336 if (param.global_tolerance != 0.)
337 libmesh_assert_equal_to (param.nelem_target, 0);
338
339
340 else
341 libmesh_assert_greater (param.nelem_target, 0);
342
344
345
347
348
349 write_output(equation_systems, a_step,
"primal", param);
350
351
353
354
356
357
359
360
361
364
365
367
368
369
371
372
373
375
376
378
379
381
382
383 primal_solution.
swap(dual_solution_0);
384 write_output(equation_systems, a_step,
"adjoint_0", param);
385
386
387 primal_solution.
swap(dual_solution_0);
388
389
391
392
393 primal_solution.
swap(dual_solution_1);
394 write_output(equation_systems, a_step,
"adjoint_1", param);
395
396
397 primal_solution.
swap(dual_solution_1);
398
400 << a_step
401 << ", we have "
403 << " active elements and "
404 << equation_systems.n_active_dofs()
405 << " active dofs."
406 << std::endl;
407
408
416
418 << std::setprecision(17)
419 << std::abs(QoI_0_computed - QoI_0_exact) / std::abs(QoI_0_exact)
420 << std::endl;
421
423 << std::setprecision(17)
424 << std::abs(QoI_1_computed - QoI_1_exact) / std::abs(QoI_1_exact)
425 << std::endl
426 << std::endl;
427
428
430
431
432 std::unique_ptr<AdjointRefinementEstimator> adjoint_refinement_error_estimator =
434
435
436 adjoint_refinement_error_estimator->estimate_error(system, QoI_elementwise_error);
437
438
439
440 libMesh::out <<
"The computed relative error in QoI 0 is "
441 << std::setprecision(17)
442 << std::abs(adjoint_refinement_error_estimator->get_global_QoI_error_estimate(0)) / std::abs(QoI_0_exact)
443 << std::endl;
444
445 libMesh::out <<
"The computed relative error in QoI 1 is "
446 << std::setprecision(17)
447 << std::abs(adjoint_refinement_error_estimator->get_global_QoI_error_estimate(1)) / std::abs(QoI_1_exact)
448 << std::endl
449 << std::endl;
450
451
452 libMesh::out <<
"The effectivity index for the computed error in QoI 0 is "
453 << std::setprecision(17)
454 << std::abs(adjoint_refinement_error_estimator->get_global_QoI_error_estimate(0)) / std::abs(QoI_0_computed - QoI_0_exact)
455 << std::endl;
456
457 libMesh::out <<
"The effectivity index for the computed error in QoI 1 is "
458 << std::setprecision(17)
459 << std::abs(adjoint_refinement_error_estimator->get_global_QoI_error_estimate(1)) / std::abs(QoI_1_computed - QoI_1_exact)
460 << std::endl
461 << std::endl;
462
463
464
465 if (!param.refine_uniformly)
466 for (std::size_t i=0; i<QoI_elementwise_error.size(); i++)
467 if (QoI_elementwise_error[i] != 0.)
468 QoI_elementwise_error[i] = std::abs(QoI_elementwise_error[i]);
469
470
471
472
473
474
475 if (param.refine_uniformly)
476 {
477 mesh_refinement->uniformly_refine(1);
478 }
479
480 else if (param.global_tolerance >= 0. && param.nelem_target == 0.)
481 {
482 mesh_refinement->flag_elements_by_error_tolerance (QoI_elementwise_error);
483
484 mesh_refinement->refine_and_coarsen_elements();
485 }
486
487 else
488 {
490 {
491 libMesh::out <<
"We reached the target number of elements." << std::endl << std::endl;
492 break;
493 }
494
495 mesh_refinement->flag_elements_by_nelem_target (QoI_elementwise_error);
496
497 mesh_refinement->refine_and_coarsen_elements();
498 }
499
500
501 equation_systems.reinit();
502
505 << " active elements and "
506 << equation_systems.n_active_dofs()
507 << " active dofs."
508 << std::endl;
509 }
510
511
512 if (a_step == param.max_adaptivesteps)
513 {
516
517 write_output(equation_systems, a_step,
"primal", param);
518
520
523
526
530
531
533
535
536 primal_solution.
swap(dual_solution_0);
537 write_output(equation_systems, a_step,
"adjoint_0", param);
538
539 primal_solution.
swap(dual_solution_0);
540
542
543 primal_solution.
swap(dual_solution_1);
544 write_output(equation_systems, a_step,
"adjoint_1", param);
545
546 primal_solution.
swap(dual_solution_1);
547
549 << a_step
550 << ", we have "
552 << " active elements and "
553 << equation_systems.n_active_dofs()
554 << " active dofs."
555 << std::endl;
556
560
565
567 << std::setprecision(17)
568 << std::abs(QoI_0_computed - QoI_0_exact) / std::abs(QoI_0_exact)
569 << std::endl;
570
572 << std::setprecision(17)
573 << std::abs(QoI_1_computed - QoI_1_exact) / std::abs(QoI_1_exact)
574 << std::endl
575 << std::endl;
576
577
578
579
581
582
583 std::unique_ptr<AdjointRefinementEstimator> adjoint_refinement_error_estimator =
585
586
587 adjoint_refinement_error_estimator->estimate_error(system, QoI_elementwise_error);
588
589
590
591 libMesh::out <<
"The computed relative error in QoI 0 is "
592 << std::setprecision(17)
593 << std::abs(adjoint_refinement_error_estimator->get_global_QoI_error_estimate(0)) / std::abs(QoI_0_exact)
594 << std::endl;
595
596 libMesh::out <<
"The computed relative error in QoI 1 is "
597 << std::setprecision(17)
598 << std::abs(adjoint_refinement_error_estimator->get_global_QoI_error_estimate(1)) / std::abs(QoI_1_exact)
599 << std::endl
600 << std::endl;
601
602
603 libMesh::out <<
"The effectivity index for the computed error in QoI 0 is "
604 << std::setprecision(17)
605 << std::abs(adjoint_refinement_error_estimator->get_global_QoI_error_estimate(0)) / std::abs(QoI_0_computed - QoI_0_exact)
606 << std::endl;
607
608 libMesh::out <<
"The effectivity index for the computed error in QoI 1 is "
609 << std::setprecision(17)
610 << std::abs(adjoint_refinement_error_estimator->get_global_QoI_error_estimate(1)) / std::abs(QoI_1_computed - QoI_1_exact)
611 << std::endl
612 << std::endl;
613
614
615
616
617
618
619
620
621 libmesh_assert_less(std::abs(adjoint_refinement_error_estimator->get_global_QoI_error_estimate(0)) / std::abs(QoI_0_computed - QoI_0_exact), 2.5);
622 libmesh_assert_greater(std::abs(adjoint_refinement_error_estimator->get_global_QoI_error_estimate(0)) / std::abs(QoI_0_computed - QoI_0_exact), .4);
623 libmesh_assert_less(std::abs(adjoint_refinement_error_estimator->get_global_QoI_error_estimate(1)) / std::abs(QoI_1_computed - QoI_1_exact), 2.5);
624 libmesh_assert_greater(std::abs(adjoint_refinement_error_estimator->get_global_QoI_error_estimate(1)) / std::abs(QoI_1_computed - QoI_1_exact), .4);
625
626
627 libmesh_assert_less(std::abs(QoI_0_computed - QoI_0_exact), 2e-4);
628 libmesh_assert_less(std::abs(QoI_1_computed - QoI_1_exact), 2e-4);
629 }
630 }
631
633 << "] Completing output."
634 << std::endl;
635
636#endif
637
638
639 return 0;
640}
void write_output(EquationSystems &es, unsigned int a_step, std::string solution_type, FEMParameters ¶m)
void set_system_parameters(LaplaceSystem &system, FEMParameters ¶m)
std::unique_ptr< AdjointRefinementEstimator > build_adjoint_refinement_error_estimator(QoISet &qois)
std::unique_ptr< MeshRefinement > build_mesh_refinement(MeshBase &mesh, FEMParameters ¶m)
virtual void postprocess()
Runs a postprocessing loop over all elements, and if postprocess_sides is true over all sides.
Number & get_QoI_value(std::string type, unsigned int QoI_index)
bool assemble_qoi_sides
If assemble_qoi_sides is true (it is false by default), the assembly loop for a quantity of interest ...
bool postprocess_sides
If postprocess_sides is true (it is false by default), the postprocessing loop will loop over all sid...
virtual LinearSolver< Number > * get_linear_solver() const override
virtual std::pair< unsigned int, Real > adjoint_solve(const QoISet &qoi_indices=QoISet()) override
This function sets the _is_adjoint boolean member of TimeSolver to true and then calls the adjoint_so...
This is the EquationSystems class.
The ErrorVector is a specialization of the StatisticsVector for error data computed on a finite eleme...
virtual void solve() override
Invokes the solver associated with the system.
The LibMeshInit class, when constructed, initializes the dependent libraries (e.g.
This base class can be inherited from to provide interfaces to linear solvers from different packages...
virtual void reuse_preconditioner(bool)
Set the same_preconditioner flag, which indicates if we reuse the same preconditioner for subsequent ...
void all_second_order(const bool full_ordered=true)
Calls the range-based version of this function with a range consisting of all elements in the mesh.
virtual void read(const std::string &name, void *mesh_data=nullptr, bool skip_renumber_nodes_and_elements=false, bool skip_find_neighbors=false, bool skip_detect_interior_parents=false)=0
Interfaces for reading/writing a mesh to/from a file.
void print_info(std::ostream &os=libMesh::out, const unsigned int verbosity=0, const bool global=true) const
Prints relevant information about the mesh.
virtual dof_id_type n_active_elem() const =0
Implements (adaptive) mesh refinement algorithms for a MeshBase.
The Mesh class is a thin wrapper, around the ReplicatedMesh class by default.
Provides a uniform interface to vector storage schemes for different linear algebra libraries.
virtual void swap(NumericVector< T > &v)
Swaps the contents of this with v.
processor_id_type processor_id() const
Data structure for specifying which Quantities of Interest should be calculated in an adjoint or a pa...
void add_indices(const std::vector< unsigned int > &indices)
Add this indices to the set to be calculated.
void set_weight(std::size_t, Real)
Set the weight for this index.
void set_adjoint_already_solved(bool setting)
Setter for the adjoint_already_solved boolean.
std::unique_ptr< NumericVector< Number > > solution
Data structure to hold solution values.
NumericVector< Number > & get_adjoint_solution(unsigned int i=0)
void init(triangulateio &t)
Initializes the fields of t to nullptr/0 as necessary.
SolverPackage default_solver_package()