ExaDG
Loading...
Searching...
No Matches
restart.h
1/* ______________________________________________________________________
2 *
3 * ExaDG - High-Order Discontinuous Galerkin for the Exa-Scale
4 *
5 * Copyright (C) 2021 by the ExaDG authors
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 * ______________________________________________________________________
20 */
21
22#ifndef INCLUDE_EXADG_TIME_INTEGRATION_RESTART_H_
23#define INCLUDE_EXADG_TIME_INTEGRATION_RESTART_H_
24
25// C/C++
26#include <boost/archive/binary_iarchive.hpp>
27#include <boost/archive/binary_oarchive.hpp>
28#include <boost/archive/text_iarchive.hpp>
29#include <boost/archive/text_oarchive.hpp>
30#include <fstream>
31#include <sstream>
32
33// deal.II
34#include <deal.II/base/mpi.h>
35#include <deal.II/lac/la_parallel_block_vector.h>
36#include <deal.II/lac/la_parallel_vector.h>
37
38namespace ExaDG
39{
40inline std::string
41restart_filename(std::string const & name, MPI_Comm const & mpi_comm)
42{
43 std::string const rank =
44 dealii::Utilities::int_to_string(dealii::Utilities::MPI::this_mpi_process(mpi_comm));
45
46 std::string const filename = name + "." + rank + ".restart";
47
48 return filename;
49}
50
51inline void
52rename_restart_files(std::string const & filename)
53{
54 // backup: rename current restart file into restart.old in case something fails while writing
55 std::string const from = filename;
56 std::string const to = filename + ".old";
57
58 std::ifstream ifile(from.c_str());
59 if((bool)ifile) // rename only if file already exists
60 {
61 int const error = rename(from.c_str(), to.c_str());
62
63 AssertThrow(error == 0, dealii::ExcMessage("Can not rename file: " + from + " -> " + to));
64 }
65}
66
67inline void
68write_restart_file(std::ostringstream & oss, std::string const & filename)
69{
70 std::ofstream stream(filename.c_str());
71
72 stream << oss.str() << std::endl;
73}
74
75template<typename VectorType>
76inline void
77print_vector_l2_norm(VectorType const & vector)
78{
79 MPI_Comm const & mpi_comm = vector.get_mpi_communicator();
80 double const l2_norm = vector.l2_norm();
81 if(dealii::Utilities::MPI::this_mpi_process(mpi_comm) == 0)
82 {
83 std::cout << " vector global l2 norm: " << std::scientific << std::setprecision(8)
84 << std::setw(20) << l2_norm << "\n";
85 }
86}
87
95template<typename VectorType, typename BoostArchiveType>
96inline void
97read_write_distributed_vector(VectorType & vector, BoostArchiveType & archive)
98{
99 // Print vector norm here only *before* writing.
100 if(std::is_same<BoostArchiveType, boost::archive::text_oarchive>::value or
101 std::is_same<BoostArchiveType, boost::archive::binary_oarchive>::value)
102 {
103 print_vector_l2_norm(vector);
104 }
105
106 // Depending on VectorType, we have to loop over the blocks to
107 // access the local entries via vector.local_element(i).
108 using Number = typename VectorType::value_type;
109 if constexpr(std::is_same<std::remove_cv_t<VectorType>,
110 dealii::LinearAlgebra::distributed::Vector<Number>>::value)
111 {
112 for(unsigned int i = 0; i < vector.locally_owned_size(); ++i)
113 {
114 archive & vector.local_element(i);
115 }
116 }
117 else if constexpr(std::is_same<std::remove_cv_t<VectorType>,
118 dealii::LinearAlgebra::distributed::BlockVector<Number>>::value)
119 {
120 for(unsigned int i = 0; i < vector.n_blocks(); ++i)
121 {
122 for(unsigned int j = 0; j < vector.block(i).locally_owned_size(); ++j)
123 {
124 archive & vector.block(i).local_element(j);
125 }
126 }
127 }
128 else
129 {
130 AssertThrow(false, dealii::ExcMessage("Reading into this VectorType not supported."));
131 }
132
133 // Print vector norm here only *after* reading.
134 if(std::is_same<BoostArchiveType, boost::archive::text_iarchive>::value or
135 std::is_same<BoostArchiveType, boost::archive::binary_iarchive>::value)
136 {
137 print_vector_l2_norm(vector);
138 }
139}
140
141} // namespace ExaDG
142
143#endif /* INCLUDE_EXADG_TIME_INTEGRATION_RESTART_H_ */
Definition driver.cpp:33
void read_write_distributed_vector(VectorType &vector, BoostArchiveType &archive)
Definition restart.h:97