Skip to content

Commit c6d6de4

Browse files
committed
Merge pull request #172 from torbjoernk/feature/fix-boris-again
Sweet!
2 parents 4af5156 + fd8e4ef commit c6d6de4

File tree

5 files changed

+76
-28
lines changed

5 files changed

+76
-28
lines changed

examples/boris/bindings/simple_physics_solver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ namespace simple_physics_solver
101101
// cout << " dist = " << dist << endl;
102102
r = sqrt(dist2 + config->sigma2);
103103
// cout << " r = " << r << " (= sqrt(dist^2+" << config->sigma2 << ")" << endl;
104-
// phis[i] += charges[j] / r;
104+
phis[i] += charges[j] / r;
105105

106106
r3 = r * r * r;
107107
for (size_t d = 0; d < DIM; ++d) {

examples/boris/boris_mlsdc.cpp

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ namespace pfasst
2222
{
2323
template<typename scalar>
2424
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)
25+
const size_t nparticles, const size_t niters,
26+
const double abs_res_tol, const double rel_res_tol)
2627
{
2728
MLSDC<> controller;
2829

@@ -33,20 +34,29 @@ namespace pfasst
3334
make_shared<bindings::WrapperSimplePhysicsSolver<double, double>>();
3435
bindings::setup(dynamic_pointer_cast<bindings::WrapperSimplePhysicsSolver<double, double>>(impl_solver));
3536

36-
auto quad1 = quadrature::quadrature_factory<double>(nnodes, quadrature::QuadratureType::GaussLobatto);
37-
auto factory1 = make_shared<ParticleCloudFactory<double>>(nparticles, 3, mass, charge);
38-
string data_file1 = "s" + to_string(nsteps) + "_i" + to_string(niters) + "_dt" + to_string(dt) + "_m" + to_string(nnodes) + "_p" + to_string(nparticles) + "_level1.csv";
39-
auto sweeper1 = make_shared<BorisSweeper<double, double>>(impl_solver, data_file1);
40-
auto transfer1 = make_shared<InjectiveTransfer<double, double>>();
37+
// fine level
38+
auto quad1 = quadrature::quadrature_factory<double>(nnodes,
39+
quadrature::QuadratureType::GaussLobatto);
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>>();
4146
sweeper1->set_quadrature(quad1);
4247
sweeper1->set_factory(factory1);
48+
sweeper1->set_residual_tolerances(abs_res_tol, rel_res_tol);
4349
controller.add_level(sweeper1, transfer1);
4450

45-
auto quad2 = quadrature::quadrature_factory<double>(nnodes, quadrature::QuadratureType::GaussLobatto);
46-
auto factory2 = make_shared<ParticleCloudFactory<double>>(nparticles, 3, mass, charge);
47-
string data_file2 = "s" + to_string(nsteps) + "_i" + to_string(niters) + "_dt" + to_string(dt) + "_m" + to_string(nnodes) + "_p" + to_string(nparticles) + "_level2.csv";
48-
auto sweeper2 = make_shared<BorisSweeper<double, double>>(impl_solver, data_file2);
49-
auto transfer2 = make_shared<InjectiveTransfer<double, double>>();
51+
// coarse level
52+
auto quad2 = quadrature::quadrature_factory<double>(nnodes,
53+
quadrature::QuadratureType::GaussLobatto);
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>>();
5060
sweeper2->set_quadrature(quad2);
5161
sweeper2->set_factory(factory2);
5262
controller.add_level(sweeper2, transfer2);
@@ -60,17 +70,13 @@ namespace pfasst
6070
center->vel()[2] = 100;
6171

6272
auto fine_sweeper = controller.get_finest<BorisSweeper<double, double>>();
63-
shared_ptr<ParticleCloud<double>> q0 = dynamic_pointer_cast<ParticleCloud<double>>(fine_sweeper->get_start_state());
73+
shared_ptr<ParticleCloud<double>> q0 = \
74+
dynamic_pointer_cast<ParticleCloud<double>>(fine_sweeper->get_start_state());
6475
q0->distribute_around_center(center);
65-
CLOG(INFO, "Boris") << "Initial Particle (fine) : " << *(dynamic_pointer_cast<ParticleCloud<double>>(fine_sweeper->get_start_state()));
76+
CLOG(INFO, "Boris") << "Initial Particle (fine) : "
77+
<< *(dynamic_pointer_cast<ParticleCloud<double>>(fine_sweeper->get_start_state()));
6678
fine_sweeper->set_initial_energy();
6779

68-
// auto coarse_sweeper = controller.get_coarsest<BorisSweeper<double, double>>();
69-
// shared_ptr<ParticleCloud<double>> q0_coarse = dynamic_pointer_cast<ParticleCloud<double>>(coarse_sweeper->get_start_state());
70-
// q0_coarse->copy(q0);
71-
// LOG(INFO) << OUT::green << "Initial Particle (coarse): " << *(dynamic_pointer_cast<ParticleCloud<double>>(coarse_sweeper->get_start_state()));
72-
// coarse_sweeper->set_initial_energy();
73-
7480
controller.run();
7581

7682
return fine_sweeper->get_errors();
@@ -93,7 +99,17 @@ int main(int argc, char** argv)
9399
const size_t nnodes = pfasst::config::get_value<size_t>("num_nodes", 5);
94100
const size_t nparticles = pfasst::config::get_value<size_t>("num_particles", 1);
95101
const size_t niters = pfasst::config::get_value<size_t>("num_iter", 2);
96-
97-
pfasst::examples::boris::run_boris_sdc<double>(nsteps, dt, nnodes, nparticles, niters);
102+
const double abs_res_tol = pfasst::config::get_value<double>("abs_res_tol", 0.0);
103+
const double rel_res_tol = pfasst::config::get_value<double>("rel_res_tol", 0.0);
104+
105+
LOG(INFO) << "nsteps=" << nsteps << ", "
106+
<< "dt=" << dt << ", "
107+
<< "nnodes=" << nnodes << ", "
108+
<< "nparticles=" << nparticles << ", "
109+
<< "niter=" << niters << ", "
110+
<< "abs res=" << abs_res_tol << ", "
111+
<< "rel res=" << rel_res_tol;
112+
113+
pfasst::examples::boris::run_boris_sdc<double>(nsteps, dt, nnodes, nparticles, niters, abs_res_tol, rel_res_tol);
98114
}
99115
#endif

examples/boris/boris_sdc.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ int main(int argc, char** argv)
7979
const double abs_res_tol = pfasst::config::get_value<double>("abs_res_tol", 0.0);
8080
const double rel_res_tol = pfasst::config::get_value<double>("rel_res_tol", 0.0);
8181

82+
LOG(INFO) << "nsteps=" << nsteps << ", "
83+
<< "dt=" << dt << ", "
84+
<< "nnodes=" << nnodes << ", "
85+
<< "nparticles=" << nparticles << ", "
86+
<< "niter=" << niters << ", "
87+
<< "abs res=" << abs_res_tol << ", "
88+
<< "rel res=" << rel_res_tol;
89+
8290
pfasst::examples::boris::run_boris_sdc<double>(nsteps, dt, nnodes, nparticles, niters, abs_res_tol, rel_res_tol);
8391
}
8492
#endif

examples/boris/boris_sweeper_impl.hpp

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -546,11 +546,14 @@ namespace pfasst
546546
zero(dst_q[m]);
547547
zero(dst_qq[m]);
548548
for (size_t n = 0; n < nnodes; ++n) {
549+
// for positions
550+
*(dst_qq[m].get()) += rhs[n] * dt * dt * this->qq_mat(m, n)
551+
+ this->start_particles->velocities() * dt * this->q_mat(m, n);
552+
// for velocities
549553
*(dst_q[m].get()) += rhs[n] * dt * this->q_mat(m, n);
550-
*(dst_qq[m].get()) += rhs[n] * dt * dt * this->qq_mat(m, n);
551554
}
552-
BCVLOG(6) << "integral(Q)[" << m << "]: " << *(dst_q[m].get());
553555
BCVLOG(6) << "integral(QQ)[" << m << "]: " << *(dst_qq[m].get());
556+
BCVLOG(6) << "integral(Q)[" << m << "]: " << *(dst_q[m].get());
554557
}
555558
this->log_indent->decrement(6);
556559
}
@@ -564,21 +567,35 @@ namespace pfasst
564567
assert(dst.size() == nnodes);
565568

566569
vector<shared_ptr<encap_type>> dst_cast(nnodes);
570+
vector<shared_ptr<acceleration_type>> q_int(nnodes), qq_int(nnodes);
567571
for (size_t m = 0; m < nnodes; ++m) {
568572
dst_cast[m] = dynamic_pointer_cast<encap_type>(dst[m]);
569573
assert(dst_cast[m]);
574+
575+
qq_int[m] = make_shared<acceleration_type>(this->start_particles->size() * this->start_particles->dim());
576+
q_int[m] = make_shared<acceleration_type>(this->start_particles->size() * this->start_particles->dim());
570577
}
571578

579+
// cf. pySDC: QF(u)
580+
this->integrate(dt, q_int, qq_int);
581+
572582
for (size_t m = 1; m < nnodes; ++m) {
573583
BCVLOG(8) << "for node " << m;
574584
zero(dst_cast[m]->positions());
575585
zero(dst_cast[m]->velocities());
576-
for (size_t j = 0; j < nnodes; ++j) {
577-
dst_cast[m]->positions() += this->particles[j]->velocities() * this->q_mat(m, j) * dt;
578-
dst_cast[m]->velocities() += this->build_rhs(j) * this->q_mat(m, j) * dt;
579-
}
586+
587+
dst_cast[m]->positions() = *(qq_int[m].get());
588+
dst_cast[m]->velocities() = *(q_int[m].get());
589+
590+
// cf. pySDC: L.u[0] - L.u[m+1] (in boris_2nd_order#compute_residual())
580591
dst_cast[m]->positions() += this->start_particles->positions() - this->particles[m]->positions();
581592
dst_cast[m]->velocities() += this->start_particles->velocities() - this->particles[m]->velocities();
593+
594+
// add tau correction (if available)
595+
if (this->tau_q_corrections.size() > 0 && this->tau_qq_corrections.size()) {
596+
dst_cast[m]->positions() += *(this->tau_qq_corrections[m].get());
597+
dst_cast[m]->velocities() += *(this->tau_q_corrections[m].get());
598+
}
582599
}
583600

584601
this->log_indent->decrement(8);
@@ -695,6 +712,12 @@ namespace pfasst
695712
BCVLOG(2) << "+= tau_qq[" << m << "] (<" << this->tau_qq_corrections[m] << ">" << *(this->tau_qq_corrections[m].get()) << ")";
696713
this->s_integrals[m] += *(this->tau_q_corrections[m].get());
697714
this->ss_integrals[m] += *(this->tau_qq_corrections[m].get());
715+
if (m > 0) {
716+
BCVLOG(2) << "-= tau_q[" << m-1 << "] (<" << this->tau_q_corrections[m-1] << ">" << *(this->tau_q_corrections[m-1].get()) << ")";
717+
BCVLOG(2) << "-= tau_qq[" << m-1 << "] (<" << this->tau_qq_corrections[m-1] << ">" << *(this->tau_qq_corrections[m-1].get()) << ")";
718+
this->s_integrals[m] -= *(this->tau_q_corrections[m-1].get());
719+
this->ss_integrals[m] -= *(this->tau_qq_corrections[m-1].get());
720+
}
698721
}
699722
this->log_indent->decrement(2);
700723
}

include/pfasst/config.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ namespace pfasst
238238

239239
options::add_option<double>("Duration", "dt", "time step size");
240240
options::add_option<double>("Duration", "tend", "final time of simulation");
241+
options::add_option<size_t>("Duration", "num_steps", "number time steps");
241242
options::add_option<size_t>("Duration", "num_iter", "number of iterations");
242243

243244
options::add_option<size_t>("Quadrature", "num_nodes", "number of quadrature nodes");

0 commit comments

Comments
 (0)