714 * mass_minus_tau_Jacobian.add(-tau, system_matrix);
716 * inverse_mass_minus_tau_Jacobian.
initialize(mass_minus_tau_Jacobian);
722 * inverse_mass_minus_tau_Jacobian.
vmult(result, tmp);
732 * <a name=
"codeDiffusionoutput_resultscode"></a>
733 * <h4><code>Diffusion::output_results</code></h4>
737 * The following function then outputs the solution in
vtu files indexed by
738 * the number of the time step and the name of the time stepping method. Of
739 * course, the (exact) result should really be the same
for all time
740 * stepping method, but the output here at least allows us to compare them.
743 *
void Diffusion::output_results(
const double time,
744 *
const unsigned int time_step,
747 * std::string method_name;
753 * method_name =
"forward_euler";
758 * method_name =
"rk3";
763 * method_name =
"rk4";
768 * method_name =
"backward_euler";
773 * method_name =
"implicit_midpoint";
778 * method_name =
"sdirk";
783 * method_name =
"heun_euler";
788 * method_name =
"bocacki_shampine";
793 * method_name =
"dopri";
798 * method_name =
"fehlberg";
803 * method_name =
"cash_karp";
821 *
const std::string filename =
"solution_" + method_name +
"-" +
824 * std::ofstream output(filename);
827 *
static std::vector<std::pair<double, std::string>> times_and_names;
829 *
static std::string method_name_prev =
"";
830 *
static std::string pvd_filename;
831 *
if (method_name_prev != method_name)
833 * times_and_names.clear();
834 * method_name_prev = method_name;
835 * pvd_filename =
"solution_" + method_name +
".pvd";
837 * times_and_names.emplace_back(time, filename);
838 * std::ofstream pvd_output(pvd_filename);
846 * <a name=
"codeDiffusionexplicit_methodcode"></a>
847 * <h4><code>Diffusion::explicit_method</code></h4>
851 * This function is the driver
for all the
explicit methods. At the
852 * top it initializes the time stepping and the solution (by setting
853 * it to
zero and then ensuring that boundary value and hanging node
854 * constraints are respected; of course, with the mesh we use here,
855 * hanging node constraints are not in fact an issue). It then calls
856 * <code>evolve_one_time_step</code> which performs
one time step.
857 * Time is stored and incremented through a
DiscreteTime object.
861 * For
explicit methods, <code>evolve_one_time_step</code> needs to
862 * evaluate @f$M^{-1}(f(t,y))@f$, i.e, it needs
863 * <code>evaluate_diffusion</code>. Because
864 * <code>evaluate_diffusion</code> is a member function, it needs to
865 * be bound to <code>this</code>. After each evolution step, we
866 * again apply the correct boundary
values and hanging node
871 * Finally, the solution is output
872 * every 10 time steps.
876 *
const unsigned int n_time_steps,
877 *
const double initial_time,
878 *
const double final_time)
880 *
const double time_step =
881 * (final_time - initial_time) /
static_cast<double>(n_time_steps);
884 * constraint_matrix.distribute(solution);
888 * output_results(initial_time, 0, method);
889 *
DiscreteTime time(initial_time, final_time, time_step);
890 *
while (time.is_at_end() ==
false)
892 * explicit_runge_kutta.evolve_one_time_step(
894 *
return this->evaluate_diffusion(time, y);
896 * time.get_current_time(),
897 * time.get_next_step_size(),
899 * time.advance_time();
901 * constraint_matrix.distribute(solution);
903 *
if (time.get_step_number() % 10 == 0)
904 * output_results(time.get_current_time(),
905 * time.get_step_number(),
915 * <a name=
"codeDiffusionimplicit_methodcode"></a>
916 * <h4><code>Diffusion::implicit_method</code></h4>
917 * This function is equivalent to <code>explicit_method</code> but
for
918 * implicit methods. When
using implicit methods, we need to evaluate
919 * @f$M^{-1}(f(t,y))@f$ and @f$\left(I-\tau M^{-1} \frac{\partial f(t,y)}{\partial
920 * y}\right)^{-1}@f$
for which we use the two member
functions previously
925 *
const unsigned int n_time_steps,
926 *
const double initial_time,
927 *
const double final_time)
929 *
const double time_step =
930 * (final_time - initial_time) /
static_cast<double>(n_time_steps);
933 * constraint_matrix.distribute(solution);
937 * output_results(initial_time, 0, method);
938 *
DiscreteTime time(initial_time, final_time, time_step);
939 *
while (time.is_at_end() ==
false)
941 * implicit_runge_kutta.evolve_one_time_step(
943 *
return this->evaluate_diffusion(time, y);
945 * [
this](
const double time,
const double tau,
const Vector<double> &y) {
946 *
return this->id_minus_tau_J_inverse(time, tau, y);
948 * time.get_current_time(),
949 * time.get_next_step_size(),
951 * time.advance_time();
953 * constraint_matrix.distribute(solution);
955 *
if (time.get_step_number() % 10 == 0)
956 * output_results(time.get_current_time(),
957 * time.get_step_number(),
967 * <a name=
"codeDiffusionembedded_explicit_methodcode"></a>
968 * <h4><code>Diffusion::embedded_explicit_method</code></h4>
969 * This function is the driver
for the embedded
explicit methods. It
requires
971 * - coarsen_param: factor multiplying the current time step when the error
972 * is below the threshold.
973 * - refine_param: factor multiplying the current time step when the error
974 * is above the threshold.
975 * - min_delta: smallest time step acceptable.
976 * - max_delta: largest time step acceptable.
977 * - refine_tol: threshold above which the time step is refined.
978 * - coarsen_tol: threshold below which the time step is
coarsen.
982 * Embedded methods use a guessed time step. If the error
using this time step
983 * is too large, the time step will be reduced. If the error is below the
984 * threshold, a larger time step will be tried
for the next time step.
985 * <code>delta_t_guess</code> is the guessed time step produced by the
986 * embedded method. In summary, time step size is potentially modified in
988 * - Reducing or increasing time step size within
990 * - Using the calculated <code>delta_t_guess</code>.
991 * - Automatically adjusting the step size of the last time step to ensure
992 * simulation ends precisely at <code>final_time</code>. This adjustment
996 *
unsigned int Diffusion::embedded_explicit_method(
998 *
const unsigned int n_time_steps,
999 *
const double initial_time,
1000 *
const double final_time)
1002 *
const double time_step =
1003 * (final_time - initial_time) /
static_cast<double>(n_time_steps);
1004 *
const double coarsen_param = 1.2;
1005 *
const double refine_param = 0.8;
1006 *
const double min_delta = 1
e-8;
1007 *
const double max_delta = 10 * time_step;
1008 *
const double refine_tol = 1
e-1;
1009 *
const double coarsen_tol = 1
e-5;
1012 * constraint_matrix.distribute(solution);
1015 * embedded_explicit_runge_kutta(method,
1022 * output_results(initial_time, 0, method);
1023 *
DiscreteTime time(initial_time, final_time, time_step);
1024 *
while (time.is_at_end() ==
false)
1027 * embedded_explicit_runge_kutta.evolve_one_time_step(
1029 *
return this->evaluate_diffusion(time, y);
1031 * time.get_current_time(),
1032 * time.get_next_step_size(),
1034 * time.set_next_step_size(
new_time - time.get_current_time());
1035 * time.advance_time();
1037 * constraint_matrix.distribute(solution);
1039 *
if (time.get_step_number() % 10 == 0)
1040 * output_results(time.get_current_time(),
1041 * time.get_step_number(),
1044 * time.set_desired_next_step_size(
1045 * embedded_explicit_runge_kutta.get_status().delta_t_guess);
1048 *
return time.get_step_number();
1056 * <a name=
"codeDiffusionruncode"></a>
1061 * The following is the main function of the program. At the top, we create
1062 * the grid (a [0,5]x[0,5] square) and
refine it four times to get a mesh
1063 * that has 16 by 16 cells,
for a total of 256. We then
set the boundary
1064 * indicator to 1
for those parts of the boundary where @f$x=0@f$ and @f$x=5@f$.
1072 *
for (
const auto &cell :
triangulation.active_cell_iterators())
1073 *
for (
const auto &face : cell->face_iterators())
1074 *
if (face->at_boundary())
1076 *
if ((face->center()[0] == 0.) || (face->center()[0] == 5.))
1077 * face->set_boundary_id(1);
1079 * face->set_boundary_id(0);
1084 * Next, we
set up the linear systems and fill them with content so that
1085 * they can be used throughout the time stepping process:
1090 * assemble_system();
1094 * Finally, we solve the diffusion problem
using several of the
1095 * Runge-Kutta methods implemented in
namespace TimeStepping, each time
1096 * outputting the error at the
end time. (As explained in the
1097 * introduction, since the exact solution is
zero at the
final time, the
1098 * error equals the numerical solution and can be computed by just taking
1099 * the @f$l_2@f$
norm of the solution vector.)
1102 *
unsigned int n_steps = 0;
1103 *
const unsigned int n_time_steps = 200;
1104 *
const double initial_time = 0.;
1105 *
const double final_time = 10.;
1107 * std::cout <<
"Explicit methods:" << std::endl;
1112 * std::cout <<
" Forward Euler: error=" << solution.l2_norm()
1119 * std::cout <<
" Third order Runge-Kutta: error=" << solution.l2_norm()
1126 * std::cout <<
" Fourth order Runge-Kutta: error=" << solution.l2_norm()
1128 * std::cout << std::endl;
1131 * std::cout <<
"Implicit methods:" << std::endl;
1136 * std::cout <<
" Backward Euler: error=" << solution.l2_norm()
1143 * std::cout <<
" Implicit Midpoint: error=" << solution.l2_norm()
1150 * std::cout <<
" Crank-Nicolson: error=" << solution.l2_norm()
1157 * std::cout <<
" SDIRK: error=" << solution.l2_norm()
1159 * std::cout << std::endl;
1162 * std::cout <<
"Embedded explicit methods:" << std::endl;
1167 * std::cout <<
" Heun-Euler: error=" << solution.l2_norm()
1169 * std::cout <<
" steps performed=" << n_steps << std::endl;
1175 * std::cout <<
" Bogacki-Shampine: error=" << solution.l2_norm()
1177 * std::cout <<
" steps performed=" << n_steps << std::endl;
1183 * std::cout <<
" Dopri: error=" << solution.l2_norm()
1185 * std::cout <<
" steps performed=" << n_steps << std::endl;
1191 * std::cout <<
" Fehlberg: error=" << solution.l2_norm()
1193 * std::cout <<
" steps performed=" << n_steps << std::endl;
1199 * std::cout <<
" Cash-Karp: error=" << solution.l2_norm()
1201 * std::cout <<
" steps performed=" << n_steps << std::endl;
1210 * <a name=
"Thecodemaincodefunction"></a>
1211 * <h3>The <code>main()</code> function</h3>
1215 * The following <code>main</code> function is similar to previous examples
1216 * and need not be commented on.
1223 * Step52::Diffusion diffusion;
1226 *
catch (std::exception &exc)
1228 * std::cerr << std::endl
1230 * <<
"----------------------------------------------------"
1232 * std::cerr <<
"Exception on processing: " << std::endl
1233 * << exc.what() << std::endl
1234 * <<
"Aborting!" << std::endl
1235 * <<
"----------------------------------------------------"
1241 * std::cerr << std::endl
1243 * <<
"----------------------------------------------------"
1245 * std::cerr <<
"Unknown exception!" << std::endl
1246 * <<
"Aborting!" << std::endl
1247 * <<
"----------------------------------------------------"
1255<a name=
"Results"></a><h1>Results</h1>
1258The
point of
this program is less to show particular results, but instead to
1259show how it is done. This we have already demonstrated simply by discussing
1260the code above. Consequently, the output the program yields is relatively
1261sparse and consists only of the console output and the solutions given in VTU
1262format
for visualization.
1264The console output contains both errors and,
for some of the methods, the
1265number of steps they performed:
1268 Forward Euler: error=1.00883
1269 Third order Runge-Kutta: error=0.000227982
1270 Fourth order Runge-Kutta: error=1.90541e-06
1273 Backward Euler: error=1.03428
1274 Implicit Midpoint: error=0.00862702
1275 Crank-Nicolson: error=0.00862675
1276 SDIRK: error=0.0042349
1278Embedded
explicit methods:
1279 Heun-Euler: error=0.0073012
1281 Bogacki-Shampine: error=0.000408407
1283 Dopri: error=0.000836695
1285 Fehlberg: error=0.00248922
1287 Cash-Karp: error=0.0787735
1291As expected the higher order methods give (much) more accurate solutions. We
1292also see that the (rather inaccurate) Heun-Euler method increased the number of
1293time steps in order to satisfy the tolerance. On the other hand, the other
1294embedded methods used a lot less time steps than what was prescribed.
1297<a name=
"PlainProg"></a>
1298<h1> The plain program</h1>
1299@include
"step-52.cc"
void attach_dof_handler(const DoFHandlerType &)
void add_data_vector(const VectorType &data, const std::vector< std::string > &names, const DataVectorType type=type_automatic, const std::vector< DataComponentInterpretation::DataComponentInterpretation > &data_component_interpretation=std::vector< DataComponentInterpretation::DataComponentInterpretation >())
virtual void build_patches(const unsigned int n_subdivisions=0)
void initialize(const SparsityPattern &sparsity_pattern)
void vmult(Vector< double > &dst, const Vector< double > &src) const
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_vtu(std::ostream &out) const
void set_flags(const FlagType &flags)
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)
static const types::blas_int zero
static const types::blas_int one
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)
void run(const Iterator &begin, const typename identity< Iterator >::type &end, Worker worker, Copier copier, const ScratchData &sample_scratch_data, const CopyData &sample_copy_data, const unsigned int queue_length, const unsigned int chunk_size)
int(&) functions(const void *v1, const void *v2)
const ::parallel::distributed::Triangulation< dim, spacedim > * triangulation