714 * mass_minus_tau_Jacobian.copy_from(mass_matrix);
715 * mass_minus_tau_Jacobian.add(-tau, system_matrix);
717 * inverse_mass_minus_tau_Jacobian.
initialize(mass_minus_tau_Jacobian);
723 * inverse_mass_minus_tau_Jacobian.vmult(result, tmp);
733 * <a name=
"codeDiffusionoutput_resultscode"></a>
734 * <h4><code>Diffusion::output_results</code></h4>
738 * The following function then outputs the solution in
vtu files indexed by
739 * the number of the time step and the name of the time stepping method. Of
740 * course, the (exact) result should really be the same
for all time
741 * stepping method, but the output here at least allows us to compare them.
744 *
void Diffusion::output_results(
const double time,
745 *
const unsigned int time_step,
748 * std::string method_name;
754 * method_name =
"forward_euler";
759 * method_name =
"rk3";
764 * method_name =
"rk4";
769 * method_name =
"backward_euler";
774 * method_name =
"implicit_midpoint";
779 * method_name =
"sdirk";
784 * method_name =
"heun_euler";
789 * method_name =
"bogacki_shampine";
794 * method_name =
"dopri";
799 * method_name =
"fehlberg";
804 * method_name =
"cash_karp";
816 * data_out.add_data_vector(solution,
"solution");
818 * data_out.build_patches();
822 *
const std::string filename =
"solution_" + method_name +
"-" +
825 * std::ofstream output(filename);
826 * data_out.write_vtu(output);
828 *
static std::vector<std::pair<double, std::string>> times_and_names;
830 *
static std::string method_name_prev =
"";
831 *
static std::string pvd_filename;
832 *
if (method_name_prev != method_name)
834 * times_and_names.clear();
835 * method_name_prev = method_name;
836 * pvd_filename =
"solution_" + method_name +
".pvd";
838 * times_and_names.emplace_back(time, filename);
839 * std::ofstream pvd_output(pvd_filename);
847 * <a name=
"codeDiffusionexplicit_methodcode"></a>
848 * <h4><code>Diffusion::explicit_method</code></h4>
852 * This function is the driver
for all the
explicit methods. At the
853 * top it initializes the time stepping and the solution (by setting
854 * it to zero and then ensuring that boundary value and hanging node
855 * constraints are respected; of course, with the mesh we use here,
856 * hanging node constraints are not in fact an issue). It then calls
857 * <code>evolve_one_time_step</code> which performs one time step.
858 * Time is stored and incremented through a
DiscreteTime object.
862 * For
explicit methods, <code>evolve_one_time_step</code> needs to
863 * evaluate @f$M^{-1}(f(t,y))@f$, i.e, it needs
864 * <code>evaluate_diffusion</code>. Because
865 * <code>evaluate_diffusion</code> is a member function, it needs to
866 * be bound to <code>this</code>. After each evolution step, we
867 * again apply the correct boundary values and hanging node
872 * Finally, the solution is output
873 * every 10 time steps.
877 *
const unsigned int n_time_steps,
878 *
const double initial_time,
879 *
const double final_time)
881 *
const double time_step =
882 * (final_time - initial_time) /
static_cast<double>(n_time_steps);
885 * constraint_matrix.distribute(solution);
889 * output_results(initial_time, 0, method);
890 *
DiscreteTime time(initial_time, final_time, time_step);
891 *
while (time.is_at_end() ==
false)
893 * explicit_runge_kutta.evolve_one_time_step(
895 *
return this->evaluate_diffusion(time, y);
897 * time.get_current_time(),
898 * time.get_next_step_size(),
900 * time.advance_time();
902 * constraint_matrix.distribute(solution);
904 *
if (time.get_step_number() % 10 == 0)
905 * output_results(time.get_current_time(),
906 * time.get_step_number(),
916 * <a name=
"codeDiffusionimplicit_methodcode"></a>
917 * <h4><code>Diffusion::implicit_method</code></h4>
918 * This function is equivalent to <code>explicit_method</code> but
for
919 * implicit methods. When
using implicit methods, we need to evaluate
920 * @f$M^{-1}(f(t,y))@f$ and @f$\left(I-\tau M^{-1} \frac{\partial f(t,y)}{\partial
921 * y}\right)^{-1}@f$
for which we use the two member
functions previously
926 *
const unsigned int n_time_steps,
927 *
const double initial_time,
928 *
const double final_time)
930 *
const double time_step =
931 * (final_time - initial_time) /
static_cast<double>(n_time_steps);
934 * constraint_matrix.distribute(solution);
938 * output_results(initial_time, 0, method);
939 *
DiscreteTime time(initial_time, final_time, time_step);
940 *
while (time.is_at_end() ==
false)
942 * implicit_runge_kutta.evolve_one_time_step(
944 *
return this->evaluate_diffusion(time, y);
946 * [
this](
const double time,
const double tau,
const Vector<double> &y) {
947 *
return this->id_minus_tau_J_inverse(time, tau, y);
949 * time.get_current_time(),
950 * time.get_next_step_size(),
952 * time.advance_time();
954 * constraint_matrix.distribute(solution);
956 *
if (time.get_step_number() % 10 == 0)
957 * output_results(time.get_current_time(),
958 * time.get_step_number(),
968 * <a name=
"codeDiffusionembedded_explicit_methodcode"></a>
969 * <h4><code>Diffusion::embedded_explicit_method</code></h4>
970 * This function is the driver
for the embedded
explicit methods. It
requires
972 * - coarsen_param: factor multiplying the current time step when the error
973 * is below the threshold.
974 * - refine_param: factor multiplying the current time step when the error
975 * is above the threshold.
976 * - min_delta: smallest time step acceptable.
977 * - max_delta: largest time step acceptable.
978 * - refine_tol: threshold above which the time step is refined.
979 * - coarsen_tol: threshold below which the time step is
coarsen.
983 * Embedded methods use a guessed time step. If the error
using this time step
984 * is too large, the time step will be reduced. If the error is below the
985 * threshold, a larger time step will be tried
for the next time step.
986 * <code>delta_t_guess</code> is the guessed time step produced by the
987 * embedded method. In summary, time step size is potentially modified in
989 * - Reducing or increasing time step size within
991 * - Using the calculated <code>delta_t_guess</code>.
992 * - Automatically adjusting the step size of the last time step to ensure
993 * simulation ends precisely at <code>final_time</code>. This adjustment
997 *
unsigned int Diffusion::embedded_explicit_method(
999 *
const unsigned int n_time_steps,
1000 *
const double initial_time,
1001 *
const double final_time)
1003 *
const double time_step =
1004 * (final_time - initial_time) /
static_cast<double>(n_time_steps);
1005 *
const double coarsen_param = 1.2;
1006 *
const double refine_param = 0.8;
1007 *
const double min_delta = 1
e-8;
1008 *
const double max_delta = 10 * time_step;
1009 *
const double refine_tol = 1
e-1;
1010 *
const double coarsen_tol = 1
e-5;
1013 * constraint_matrix.distribute(solution);
1016 * embedded_explicit_runge_kutta(method,
1023 * output_results(initial_time, 0, method);
1024 *
DiscreteTime time(initial_time, final_time, time_step);
1025 *
while (time.is_at_end() ==
false)
1028 * embedded_explicit_runge_kutta.evolve_one_time_step(
1030 *
return this->evaluate_diffusion(time, y);
1032 * time.get_current_time(),
1033 * time.get_next_step_size(),
1035 * time.set_next_step_size(new_time - time.get_current_time());
1036 * time.advance_time();
1038 * constraint_matrix.distribute(solution);
1040 *
if (time.get_step_number() % 10 == 0)
1041 * output_results(time.get_current_time(),
1042 * time.get_step_number(),
1045 * time.set_desired_next_step_size(
1046 * embedded_explicit_runge_kutta.get_status().delta_t_guess);
1049 *
return time.get_step_number();
1057 * <a name=
"codeDiffusionruncode"></a>
1058 * <h4><code>Diffusion::run</code></h4>
1062 * The following is the main function of the program. At the top, we create
1063 * the grid (a @f$[0,5]\times [0,5]@f$ square) and
refine it four times to get a
1064 * mesh that has 16 by 16 cells,
for a total of 256. We then
set the boundary
1065 * indicator to 1
for those parts of the boundary where @f$x=0@f$ and @f$x=5@f$.
1068 *
void Diffusion::run()
1073 *
for (
const auto &cell :
triangulation.active_cell_iterators())
1074 * for (const auto &face : cell->face_iterators())
1075 * if (face->at_boundary())
1077 * if ((face->
center()[0] == 0.) || (face->
center()[0] == 5.))
1078 * face->set_boundary_id(1);
1080 * face->set_boundary_id(0);
1085 * Next, we
set up the linear systems and fill them with content so that
1086 * they can be used throughout the time stepping process:
1091 * assemble_system();
1095 * Finally, we solve the diffusion problem
using several of the
1096 * Runge-Kutta methods implemented in
namespace TimeStepping, each time
1097 * outputting the error at the
end time. (As explained in the
1098 * introduction, since the exact solution is zero at the
final time, the
1099 * error equals the numerical solution and can be computed by just taking
1100 * the @f$l_2@f$
norm of the solution vector.)
1103 *
unsigned int n_steps = 0;
1104 *
const unsigned int n_time_steps = 200;
1105 *
const double initial_time = 0.;
1106 *
const double final_time = 10.;
1108 * std::cout <<
"Explicit methods:" << std::endl;
1113 * std::cout <<
" Forward Euler: error=" << solution.l2_norm()
1120 * std::cout <<
" Third order Runge-Kutta: error=" << solution.l2_norm()
1127 * std::cout <<
" Fourth order Runge-Kutta: error=" << solution.l2_norm()
1129 * std::cout << std::endl;
1132 * std::cout <<
"Implicit methods:" << std::endl;
1137 * std::cout <<
" Backward Euler: error=" << solution.l2_norm()
1144 * std::cout <<
" Implicit Midpoint: error=" << solution.l2_norm()
1151 * std::cout <<
" Crank-Nicolson: error=" << solution.l2_norm()
1158 * std::cout <<
" SDIRK: error=" << solution.l2_norm()
1160 * std::cout << std::endl;
1163 * std::cout <<
"Embedded explicit methods:" << std::endl;
1168 * std::cout <<
" Heun-Euler: error=" << solution.l2_norm()
1170 * std::cout <<
" steps performed=" << n_steps << std::endl;
1176 * std::cout <<
" Bogacki-Shampine: error=" << solution.l2_norm()
1178 * std::cout <<
" steps performed=" << n_steps << std::endl;
1184 * std::cout <<
" Dopri: error=" << solution.l2_norm()
1186 * std::cout <<
" steps performed=" << n_steps << std::endl;
1192 * std::cout <<
" Fehlberg: error=" << solution.l2_norm()
1194 * std::cout <<
" steps performed=" << n_steps << std::endl;
1200 * std::cout <<
" Cash-Karp: error=" << solution.l2_norm()
1202 * std::cout <<
" steps performed=" << n_steps << std::endl;
1211 * <a name=
"Thecodemaincodefunction"></a>
1212 * <h3>The <code>main()</code> function</h3>
1216 * The following <code>main</code> function is similar to previous examples
1217 * and need not be commented on.
1224 * Step52::Diffusion diffusion;
1227 *
catch (std::exception &exc)
1229 * std::cerr << std::endl
1231 * <<
"----------------------------------------------------"
1233 * std::cerr <<
"Exception on processing: " << std::endl
1234 * << exc.what() << std::endl
1235 * <<
"Aborting!" << std::endl
1236 * <<
"----------------------------------------------------"
1242 * std::cerr << std::endl
1244 * <<
"----------------------------------------------------"
1246 * std::cerr <<
"Unknown exception!" << std::endl
1247 * <<
"Aborting!" << std::endl
1248 * <<
"----------------------------------------------------"
1256<a name=
"Results"></a><h1>Results</h1>
1259The
point of
this program is less to show particular results, but instead to
1260show how it is done. This we have already demonstrated simply by discussing
1261the code above. Consequently, the output the program yields is relatively
1262sparse and consists only of the console output and the solutions given in VTU
1263format
for visualization.
1265The console output contains both errors and,
for some of the methods, the
1266number of steps they performed:
1269 Forward Euler: error=1.00883
1270 Third order Runge-Kutta: error=0.000227982
1271 Fourth order Runge-Kutta: error=1.90541e-06
1274 Backward Euler: error=1.03428
1275 Implicit Midpoint: error=0.00862702
1276 Crank-Nicolson: error=0.00862675
1277 SDIRK: error=0.0042349
1279Embedded
explicit methods:
1280 Heun-Euler: error=0.0073012
1282 Bogacki-Shampine: error=0.000408407
1284 Dopri: error=0.000836695
1286 Fehlberg: error=0.00248922
1288 Cash-Karp: error=0.0787735
1292As expected the higher order methods give (much) more accurate solutions. We
1293also see that the (rather inaccurate) Heun-Euler method increased the number of
1294time steps in order to satisfy the tolerance. On the other hand, the other
1295embedded methods used a lot less time steps than what was prescribed.
1298<a name=
"PlainProg"></a>
1299<h1> The plain program</h1>
1300@include
"step-52.cc"
void attach_dof_handler(const DoFHandler< dim, spacedim > &)
void initialize(const SparsityPattern &sparsity_pattern)
double evolve_one_time_step(const std::function< VectorType(const double, const VectorType &)> &f, const std::function< VectorType(const double, const double, const VectorType &)> &id_minus_tau_J_inverse, double t, double delta_t, VectorType &y) override
__global__ void set(Number *val, const Number s, const size_type N)
void write_pvd_record(std::ostream &out, const std::vector< std::pair< double, std::string > > ×_and_names)
void hyper_cube(Triangulation< dim, spacedim > &tria, const double left=0., const double right=1., const bool colorize=false)
void refine(Triangulation< dim, spacedim > &tria, const Vector< Number > &criteria, const double threshold, const unsigned int max_to_mark=numbers::invalid_unsigned_int)
void coarsen(Triangulation< dim, spacedim > &tria, const Vector< Number > &criteria, const double threshold)
double norm(const FEValuesBase< dim > &fe, const ArrayView< const std::vector< Tensor< 1, dim > > > &Du)
void mass_matrix(FullMatrix< double > &M, const FEValuesBase< dim > &fe, const double factor=1.)
Point< spacedim > point(const gp_Pnt &p, const double tolerance=1e-10)
SymmetricTensor< 2, dim, Number > e(const Tensor< 2, dim, Number > &F)
@ RK_CLASSIC_FOURTH_ORDER
VectorType::value_type * end(VectorType &V)
std::string int_to_string(const unsigned int value, const unsigned int digits=numbers::invalid_unsigned_int)
int(&) functions(const void *v1, const void *v2)
const ::parallel::distributed::Triangulation< dim, spacedim > * triangulation