42 #include "libmesh/libmesh.h"
43 #include "libmesh/replicated_mesh.h"
44 #include "libmesh/mesh_refinement.h"
45 #include "libmesh/gmv_io.h"
46 #include "libmesh/equation_systems.h"
47 #include "libmesh/fe.h"
48 #include "libmesh/quadrature_gauss.h"
49 #include "libmesh/dof_map.h"
50 #include "libmesh/sparse_matrix.h"
51 #include "libmesh/numeric_vector.h"
52 #include "libmesh/dense_matrix.h"
53 #include "libmesh/dense_vector.h"
54 #include "libmesh/getpot.h"
55 #include "libmesh/enum_solver_package.h"
56 #include "libmesh/enum_xdr_mode.h"
57 #include "libmesh/enum_norm_type.h"
61 #include "libmesh/transient_system.h"
62 #include "libmesh/linear_implicit_system.h"
63 #include "libmesh/vector_value.h"
67 #include "libmesh/error_vector.h"
68 #include "libmesh/kelly_error_estimator.h"
71 #include "libmesh/elem.h"
83 #ifdef LIBMESH_ENABLE_AMR
85 const std::string & system_name);
94 const std::string & system_name);
119 int main (
int argc,
char ** argv)
126 "--enable-petsc, --enable-trilinos, or --enable-eigen");
128 #ifndef LIBMESH_ENABLE_AMR
129 libmesh_example_requires(
false,
"--enable-amr");
142 <<
"\t " << argv[0] <<
" -init_timestep 0 -n_timesteps 25 [-n_refinements 5]\n"
144 <<
"\t " << argv[0] <<
" -read_solution -init_timestep 26 -n_timesteps 25\n"
149 for (
int i=1; i<argc; i++)
155 GetPot command_line (argc, argv);
164 const bool read_solution = command_line.search(
"-read_solution");
171 unsigned int init_timestep = 0;
175 if (command_line.search(
"-init_timestep"))
176 init_timestep = command_line.next(0);
181 libmesh_error_msg(
"ERROR: Initial timestep not specified!");
186 unsigned int n_timesteps = 0;
189 if (command_line.search(
"-n_timesteps"))
190 n_timesteps = command_line.next(0);
192 libmesh_error_msg(
"ERROR: Number of timesteps not specified");
196 libmesh_example_requires(2 <= LIBMESH_DIM,
"2D support");
214 unsigned int n_refinements = 5;
215 if (command_line.search(
"-n_refinements"))
216 n_refinements = command_line.next(0);
233 system.add_variable (
"u",
FIRST);
238 system.attach_init_function (
init_cd);
241 equation_systems.init ();
253 equation_systems.read(
"saved_solution.xda",
READ);
271 libMesh::out <<
"Initial H1 norm = " << H1norm << std::endl << std::endl;
275 equation_systems.print_info();
277 equation_systems.parameters.set<
unsigned int>(
"linear solver maximum iterations") = 250;
278 equation_systems.parameters.set<
Real>(
"linear solver tolerance") =
TOLERANCE;
296 equation_systems.parameters.set<
Real>(
"diffusivity") = 0.01;
310 const Real dt = 0.025;
311 system.time = init_timestep*dt;
315 for (
unsigned int t_step=init_timestep; t_step<(init_timestep+n_timesteps); t_step++)
321 equation_systems.parameters.set<
Real> (
"time") = system.time;
322 equation_systems.parameters.set<
Real> (
"dt") = dt;
329 std::ostringstream
out;
337 << std::setprecision(3)
351 *system.old_local_solution = *system.current_local_solution;
354 const unsigned int max_r_steps = 2;
357 for (
unsigned int r_step=0; r_step<max_r_steps; r_step++)
368 if (r_step+1 != max_r_steps)
417 equation_systems.reinit ();
422 unsigned int output_freq = 10;
423 if (command_line.search(
"-output_freq"))
424 output_freq = command_line.next(0);
427 if ((t_step+1)%output_freq == 0)
429 std::ostringstream file_name;
431 file_name <<
"out.gmv."
447 libMesh::out <<
"Final H1 norm = " << H1norm << std::endl << std::endl;
450 equation_systems.write(
"saved_solution.xda",
WRITE);
454 #endif // #ifndef LIBMESH_ENABLE_AMR
464 const std::string & libmesh_dbg_var(system_name))
468 libmesh_assert_equal_to (system_name,
"Convection-Diffusion");
486 #ifdef LIBMESH_ENABLE_AMR
488 const std::string & libmesh_dbg_var(system_name))
492 libmesh_assert_equal_to (system_name,
"Convection-Diffusion");
506 FEType fe_type = system.variable_type(0);
517 QGauss qrule (
dim, fe_type.default_quadrature_order());
518 QGauss qface (
dim-1, fe_type.default_quadrature_order());
521 fe->attach_quadrature_rule (&qrule);
522 fe_face->attach_quadrature_rule (&qface);
527 const std::vector<Real> & JxW = fe->get_JxW();
528 const std::vector<Real> & JxW_face = fe_face->get_JxW();
531 const std::vector<std::vector<Real>> & phi = fe->get_phi();
532 const std::vector<std::vector<Real>> & psi = fe_face->get_phi();
536 const std::vector<std::vector<RealGradient>> & dphi = fe->get_dphi();
539 const std::vector<Point> & qface_points = fe_face->get_xyz();
545 const DofMap & dof_map = system.get_dof_map();
557 std::vector<dof_id_type> dof_indices;
564 const Real diffusivity =
588 const unsigned int n_dofs =
589 cast_int<unsigned int>(dof_indices.size());
590 libmesh_assert_equal_to (n_dofs, phi.size());
598 Ke.
resize (n_dofs, n_dofs);
608 for (
unsigned int qp=0; qp<qrule.n_points(); qp++)
615 for (
unsigned int l=0; l != n_dofs; l++)
617 u_old += phi[l][qp]*system.
old_solution (dof_indices[l]);
626 for (
unsigned int i=0; i != n_dofs; i++)
636 (grad_u_old*velocity)*phi[i][qp] +
639 diffusivity*(grad_u_old*dphi[i][qp]))
642 for (
unsigned int j=0; j != n_dofs; j++)
647 phi[i][qp]*phi[j][qp] +
650 (velocity*dphi[j][qp])*phi[i][qp] +
652 diffusivity*(dphi[i][qp]*dphi[j][qp]))
669 const Real penalty = 1.e10;
674 for (
auto s : elem->side_index_range())
675 if (elem->neighbor_ptr(s) ==
nullptr)
677 fe_face->reinit(elem, s);
679 libmesh_assert_equal_to (n_dofs, psi.size());
681 for (
unsigned int qp=0; qp<qface.n_points(); qp++)
688 for (
unsigned int i=0; i != n_dofs; i++)
689 Fe(i) += penalty*JxW_face[qp]*
value*psi[i][qp];
692 for (
unsigned int i=0; i != n_dofs; i++)
693 for (
unsigned int j=0; j != n_dofs; j++)
694 Ke(i,j) += penalty*JxW_face[qp]*psi[i][qp]*psi[j][qp];
714 system.matrix->add_matrix (Ke, dof_indices);
715 system.rhs->add_vector (Fe, dof_indices);
720 #endif // #ifdef LIBMESH_ENABLE_AMR