PFASST++
boris_mlsdc.cpp
Go to the documentation of this file.
1 #include <memory>
2 
3 #include <pfasst.hpp>
4 #include <pfasst/config.hpp>
5 #include <pfasst/logging.hpp>
8 
9 #include "particle.hpp"
10 #include "particle_cloud.hpp"
13 #include "boris_sweeper.hpp"
14 #include "injective_transfer.hpp"
15 
16 
17 namespace pfasst
18 {
19  namespace examples
20  {
21  namespace boris
22  {
23  template<typename scalar>
24  error_map<scalar> run_boris_sdc(const size_t nsteps, const scalar dt, const size_t nnodes,
25  const size_t nparticles, const size_t niters,
26  const double abs_res_tol, const double rel_res_tol)
27  {
28  MLSDC<> controller;
29 
30  const double mass = 1.0;
31  const double charge = 1.0;
32 
33  shared_ptr<bindings::WrapperInterface<double, double>> impl_solver = \
34  make_shared<bindings::WrapperSimplePhysicsSolver<double, double>>();
36 
37  // fine level
38  auto quad1 = quadrature::quadrature_factory<double>(nnodes,
40  auto factory1 = make_shared<ParticleCloudFactory<double>>(nparticles, 3, mass, charge);
41  string data_file1 = "s" + to_string(nsteps) + "_i" + to_string(niters)
42  + "_dt" + to_string(dt) + "_m" + to_string(nnodes)
43  + "_p" + to_string(nparticles) + "_level1.csv";
44  auto sweeper1 = make_shared<BorisSweeper<double, double>>(impl_solver, data_file1);
45  auto transfer1 = make_shared<InjectiveTransfer<double, double>>();
46  sweeper1->set_quadrature(quad1);
47  sweeper1->set_factory(factory1);
48  sweeper1->set_residual_tolerances(abs_res_tol, rel_res_tol);
49  controller.add_level(sweeper1, transfer1);
50 
51  // coarse level
52  auto quad2 = quadrature::quadrature_factory<double>(nnodes,
54  auto factory2 = make_shared<ParticleCloudFactory<double>>(nparticles, 3, mass, charge);
55  string data_file2 = "s" + to_string(nsteps) + "_i" + to_string(niters)
56  + "_dt" + to_string(dt) + "_m" + to_string(nnodes)
57  + "_p" + to_string(nparticles) + "_level2.csv";
58  auto sweeper2 = make_shared<BorisSweeper<double, double>>(impl_solver, data_file2);
59  auto transfer2 = make_shared<InjectiveTransfer<double, double>>();
60  sweeper2->set_quadrature(quad2);
61  sweeper2->set_factory(factory2);
62  controller.add_level(sweeper2, transfer2);
63 
64  controller.set_duration(0.0, nsteps*dt, dt, niters);
65  controller.setup();
66 
67  shared_ptr<Particle<double>> center = make_shared<Particle<double>>();
68  center->pos()[0] = 10;
69  center->vel()[0] = 100;
70  center->vel()[2] = 100;
71 
72  auto fine_sweeper = controller.get_finest<BorisSweeper<double, double>>();
73  shared_ptr<ParticleCloud<double>> q0 = \
74  dynamic_pointer_cast<ParticleCloud<double>>(fine_sweeper->get_start_state());
75  q0->distribute_around_center(center);
76  ML_CLOG(INFO, "Boris", "Initial Particle (fine) : "
77  << *(dynamic_pointer_cast<ParticleCloud<double>>(fine_sweeper->get_start_state())));
78  fine_sweeper->set_initial_energy();
79 
80  controller.run();
81 
82  return fine_sweeper->get_errors();
83  }
84  } // ::pfasst::examples::boris
85  } // ::pfasst::examples
86 } // ::pfasst
87 
88 #ifndef PFASST_UNIT_TESTING
89 int main(int argc, char** argv)
90 {
91  pfasst::init(argc, argv,
92  pfasst::examples::boris::init_opts<>,
93  pfasst::examples::boris::init_logs<>);
94 
95  const size_t nsteps = pfasst::config::get_value<size_t>("num_steps", 1);
96  const double dt = pfasst::config::get_value<double>("delta_step", 0.015625);
97  const size_t nnodes = pfasst::config::get_value<size_t>("num_nodes", 5);
98  const size_t nparticles = pfasst::config::get_value<size_t>("num_particles", 1);
99  const size_t niters = pfasst::config::get_value<size_t>("num_iter", 2);
100  const double abs_res_tol = pfasst::config::get_value<double>("abs_res_tol", 0.0);
101  const double rel_res_tol = pfasst::config::get_value<double>("rel_res_tol", 0.0);
102 
103  ML_CLOG(INFO, "Boris", "nsteps=" << nsteps << ", "
104  << "dt=" << dt << ", "
105  << "nnodes=" << nnodes << ", "
106  << "nparticles=" << nparticles << ", "
107  << "niter=" << niters << ", "
108  << "abs res=" << abs_res_tol << ", "
109  << "rel res=" << rel_res_tol);
110 
111  pfasst::examples::boris::run_boris_sdc<double>(nsteps, dt, nnodes, nparticles, niters, abs_res_tol, rel_res_tol);
112 }
113 #endif
virtual void run()
Solve ODE using MLSDC.
Definition: mlsdc_impl.hpp:62
error_map< scalar > run_boris_sdc(const size_t nsteps, const scalar dt, const size_t nnodes, const size_t nparticles, const size_t niters, const double abs_res_tol, const double rel_res_tol)
Definition: boris_mlsdc.cpp:24
map< error_index, ErrorTuple< scalar >> error_map
void setup(shared_ptr< WrapperInterface< scalar, time >> wrapper)
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.
#define ML_CLOG(level, logger_id, x)
same as CLOG(level, logger, x) from easylogging++
Definition: logging.hpp:117
int main(int argc, char **argv)
Definition: boris_mlsdc.cpp:89
static void init(int argc, char **argv, std::function< void()> opts=nullptr, std::function< void()> logs=nullptr)
Definition: pfasst.hpp:13
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< R > get_finest()
Get coarsest level.
Definition: interface.hpp:163
float dt
Definition: plot.py:10