PFASST++
serial_mlsdc.cpp
Go to the documentation of this file.
1 
8 #include <memory>
9 using namespace std;
10 
11 #include <fftw3.h>
12 
13 #include <pfasst.hpp>
14 #include <pfasst/logging.hpp>
15 #include <pfasst/config.hpp>
17 #include <pfasst/encap/vector.hpp>
18 using namespace pfasst::encap;
19 
21 #include "spectral_transfer_1d.hpp"
22 
23 
24 namespace pfasst
25 {
26  namespace examples
27  {
28  namespace advection_diffusion
29  {
37  tuple<error_map, residual_map> run_serial_mlsdc(size_t nlevs,
38  size_t nsteps_in=4,
39  double step_size_in=0.01,
40  size_t num_iter_in=8,
41  size_t nnodes_in=5,
42  size_t ndofs_in=128)
43  {
44  MLSDC<> mlsdc;
45 
46  const size_t nsteps = config::get_value<size_t>("num_steps", nsteps_in);
47  const double dt = config::get_value<double>("step_size", step_size_in);
48  const size_t niters = config::get_value<size_t>("num_iter", num_iter_in);
49  const int xrat = 2;
50  const int trat = 2;
51 
52  size_t nnodes = config::get_value<size_t>("num_nodes", nnodes_in);
53  size_t ndofs = config::get_value<size_t>("spatial_dofs", ndofs_in);
54 
55  const double abs_res_tol = pfasst::config::get_value<double>("abs_res_tol", 0.0);
56  const double rel_res_tol = pfasst::config::get_value<double>("rel_res_tol", 0.0);
57 
58  /*
59  * build space/time discretisation levels and add them to mlsdc
60  * controller. this loop adds the finest level first, and
61  * subsequently refines in time (accoring to 'trat') and space
62  * (according to 'xrat').
63  */
64  for (size_t l = 0; l < nlevs; l++) {
65  auto quad = quadrature::quadrature_factory(nnodes, quadrature::QuadratureType::GaussLobatto);
66  auto factory = make_shared<VectorFactory<double>>(ndofs);
67  auto sweeper = make_shared<AdvectionDiffusionSweeper<>>(ndofs);
68  auto transfer = make_shared<SpectralTransfer1D<>>();
69 
70  ML_LOG(INFO, "expected quadrature error: " << quad->expected_error() << " (" << nnodes << ")");
71 
72  sweeper->set_quadrature(quad);
73  sweeper->set_factory(factory);
74  sweeper->set_residual_tolerances(abs_res_tol, rel_res_tol);
75 
76  mlsdc.add_level(sweeper, transfer);
77 
78  ndofs = ndofs / xrat;
79  nnodes = (nnodes - 1) / trat + 1;
80  }
81 
82  /*
83  * set up the mlsdc controller (which in turn calls 'setup' on the
84  * sweepers that were added above). this stage typically
85  * preallocates various buffers that the sweepers need.
86  */
87  mlsdc.setup();
88 
89  /*
90  * set initial conditions on each level
91  */
92  auto sweeper = mlsdc.get_finest<AdvectionDiffusionSweeper<>>();
93  auto q0 = sweeper->get_start_state();
94  sweeper->exact(q0, 0.0);
95  // sweeper->set_residual_tolerances(1e-5, 0.0);
96 
97  /*
98  * run mlsdc!
99  */
100  mlsdc.set_duration(0.0, nsteps*dt, dt, niters);
101  mlsdc.set_options();
102  mlsdc.run();
103 
104  tuple<error_map, residual_map> rinfo;
105  get<0>(rinfo) = mlsdc.get_finest<AdvectionDiffusionSweeper<>>()->get_errors();
106  for (auto l = mlsdc.coarsest(); l <= mlsdc.finest(); ++l) {
107  get<1>(rinfo).insert(pair<size_t, error_map>(l.level, l.current<AdvectionDiffusionSweeper<>>()->get_residuals()));
108  }
109  return rinfo;
110  }
111  } // ::pfasst::examples::advection_diffusion
112  } // ::pfasst::examples
113 } // ::pfasst
114 
115 #ifndef PFASST_UNIT_TESTING
116 int main(int argc, char** argv)
117 {
118  pfasst::init(argc, argv,
122  fftw_cleanup();
123 }
124 #endif
virtual void run()
Solve ODE using MLSDC.
Definition: mlsdc_impl.hpp:62
virtual void set_options(bool all_sweepers=true)
Set options from command line etc.
int main(int argc, char **argv)
STL namespace.
virtual void add_level(shared_ptr< ISweeper< time >> sweeper, shared_ptr< ITransfer< time >> transfer=shared_ptr< ITransfer< time >>(nullptr), bool coarse=true)
Adding a level to the controller.
static void init(int argc, char **argv, std::function< void()> opts=nullptr, std::function< void()> logs=nullptr)
Definition: pfasst.hpp:13
virtual LevelIter finest()
Convenience accessor to the finest level.
#define ML_LOG(level, x)
same as LOG(level, x) from easylogging++
Definition: logging.hpp:110
virtual void set_duration(time t0, time tend, time dt, size_t niters)
Set basic time scope of the Controller.
Multilevel SDC controller.
Definition: mlsdc.hpp:24
virtual void setup() override
Basic setup routine for this controller.
Definition: mlsdc_impl.hpp:45
shared_ptr< IQuadrature< precision > > quadrature_factory(const size_t nnodes, const QuadratureType qtype)
Instantiates quadrature handler for given number of nodes and type descriptor.
Definition: quadrature.hpp:53
shared_ptr< R > get_finest()
Get coarsest level.
Definition: interface.hpp:163
Encapsulations (short encaps) are the central data type for all PFASST++ algorithms.
virtual shared_ptr< Encapsulation< time > > get_start_state() const
advection-diffusion sweeper with semi-implicit time-integration.
virtual LevelIter coarsest()
Convenience accessor to the coarsest level.
float dt
Definition: plot.py:10
tuple< error_map, residual_map > run_serial_mlsdc(size_t nlevs, size_t nsteps_in=4, double step_size_in=0.01, size_t num_iter_in=8, size_t nnodes_in=5, size_t ndofs_in=128)
Advection/diffusion example using an encapsulated IMEX sweeper.