Skip to content

Commit 37dfed1

Browse files
committed
Merge pull request #30 from torbjoernk/feature/shared-ptrs
some C++11-fications and technical tweaks
2 parents c89d4f6 + c2b426f commit 37dfed1

17 files changed

+396
-269
lines changed

examples/advection_diffusion/advection_diffusion_sweeper.hpp

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
#include <complex>
99
#include <vector>
1010
#include <cassert>
11+
#include <ostream>
1112

1213
#include <pfasst/encap/imex_sweeper.hpp>
14+
1315
#include "fft.hpp"
1416

1517
#define PI 3.1415926535897932385
@@ -18,7 +20,8 @@
1820
using namespace std;
1921

2022
template<typename time = pfasst::time_precision>
21-
class AdvectionDiffusionSweeper : public pfasst::encap::IMEXSweeper<time>
23+
class AdvectionDiffusionSweeper
24+
: public pfasst::encap::IMEXSweeper<time>
2225
{
2326
typedef pfasst::encap::Encapsulation<time> Encapsulation;
2427
typedef pfasst::encap::VectorEncapsulation<double> DVectorT;
@@ -49,44 +52,46 @@ class AdvectionDiffusionSweeper : public pfasst::encap::IMEXSweeper<time>
4952
cout << "number of f1 evals: " << nf1evals << endl;
5053
}
5154

52-
void exact(Encapsulation* q, time t)
55+
void exact(shared_ptr<Encapsulation> q, time t)
5356
{
54-
DVectorT* q_cast = dynamic_cast<DVectorT*>(q);
55-
assert(q_cast != nullptr);
56-
this->exact(*q_cast, t);
57+
shared_ptr<DVectorT> q_cast = dynamic_pointer_cast<DVectorT>(q);
58+
assert(q_cast);
59+
this->exact(q_cast, t);
5760
}
5861

59-
void exact(DVectorT& q, time t)
62+
void exact(shared_ptr<DVectorT> q, time t)
6063
{
61-
size_t n = q.size();
64+
size_t n = q->size();
6265
double a = 1.0 / sqrt(4 * PI * nu * (t + t0));
6366

6467
for (size_t i = 0; i < n; i++) {
65-
q[i] = 0.0;
68+
q->data()[i] = 0.0;
6669
}
6770

6871
for (int ii = -2; ii < 3; ii++) {
6972
for (size_t i = 0; i < n; i++) {
7073
double x = double(i) / n - 0.5 + ii - t * v;
71-
q[i] += a * exp(-x * x / (4 * nu * (t + t0)));
74+
q->data()[i] += a * exp(-x * x / (4 * nu * (t + t0)));
7275
}
7376
}
7477
}
7578

7679
void echo_error(time t, bool predict = false)
7780
{
78-
DVectorT* qend = dynamic_cast<DVectorT*>(this->get_state(this->get_nodes().size() - 1));
79-
assert(qend != nullptr);
80-
DVectorT qex = DVectorT(qend->size());
81+
shared_ptr<DVectorT> qend = dynamic_pointer_cast<DVectorT>(this->get_state(this->get_nodes().size() - 1));
82+
assert(qend);
83+
shared_ptr<DVectorT> qex = make_shared<DVectorT>(qend->size());
8184

8285
exact(qex, t);
8386

8487
double max = 0.0;
8588
for (size_t i = 0; i < qend->size(); i++) {
86-
double d = abs(qend->at(i) - qex[i]);
89+
double d = abs(qend->data()[i] - qex->data()[i]);
8790
if (d > max) { max = d; }
8891
}
89-
cout << "err: " << scientific << max << " (" << qend->size() << ", " << predict << ")" << endl;
92+
cout << "err: " << scientific << max
93+
<< " (" << qend->size() << ", " << predict << ")"
94+
<< endl;
9095
}
9196

9297
void predict(time t, time dt, bool initial)
@@ -101,75 +106,76 @@ class AdvectionDiffusionSweeper : public pfasst::encap::IMEXSweeper<time>
101106
echo_error(t + dt);
102107
}
103108

104-
void f1eval(Encapsulation* f, Encapsulation* q, time t)
109+
void f1eval(shared_ptr<Encapsulation> f, shared_ptr<Encapsulation> q, time t)
105110
{
106-
DVectorT* f_cast = dynamic_cast<DVectorT*>(f);
107-
assert(f_cast != nullptr);
108-
DVectorT* q_cast = dynamic_cast<DVectorT*>(q);
109-
assert(q_cast != nullptr);
111+
shared_ptr<DVectorT> f_cast = dynamic_pointer_cast<DVectorT>(f);
112+
assert(f_cast);
113+
shared_ptr<DVectorT> q_cast = dynamic_pointer_cast<DVectorT>(q);
114+
assert(q_cast);
110115

111116
this->f1eval(f_cast, q_cast, t);
112117
}
113118

114-
void f1eval(DVectorT* f, DVectorT* q, time t)
119+
void f1eval(shared_ptr<DVectorT> f, shared_ptr<DVectorT> q, time t)
115120
{
116121
double c = -v / double(q->size());
117122

118-
auto* z = fft.forward(*q);
123+
auto* z = fft.forward(q);
119124
for (size_t i = 0; i < q->size(); i++) {
120125
z[i] *= c * ddx[i];
121126
}
122-
fft.backward(*f);
127+
fft.backward(f);
123128

124129
nf1evals++;
125130
}
126131

127-
void f2eval(Encapsulation* f, Encapsulation* q, time t)
132+
void f2eval(shared_ptr<Encapsulation> f, shared_ptr<Encapsulation> q, time t)
128133
{
129-
DVectorT* f_cast = dynamic_cast<DVectorT*>(f);
130-
assert(f_cast != nullptr);
131-
DVectorT* q_cast = dynamic_cast<DVectorT*>(q);
132-
assert(q_cast != nullptr);
134+
shared_ptr<DVectorT> f_cast = dynamic_pointer_cast<DVectorT>(f);
135+
assert(f_cast);
136+
shared_ptr<DVectorT> q_cast = dynamic_pointer_cast<DVectorT>(q);
137+
assert(q_cast);
133138

134139
this->f2eval(f_cast, q_cast, t);
135140
}
136141

137-
void f2eval(DVectorT* f, DVectorT* q, time t)
142+
void f2eval(shared_ptr<DVectorT> f, shared_ptr<DVectorT> q, time t)
138143
{
139144
double c = nu / double(q->size());
140145

141-
auto* z = fft.forward(*q);
146+
auto* z = fft.forward(q);
142147
for (size_t i = 0; i < q->size(); i++) {
143148
z[i] *= c * lap[i];
144149
}
145-
fft.backward(*f);
150+
fft.backward(f);
146151
}
147152

148-
void f2comp(Encapsulation* f, Encapsulation* q, time t, time dt, Encapsulation* rhs)
153+
void f2comp(shared_ptr<Encapsulation> f, shared_ptr<Encapsulation> q, time t, time dt,
154+
shared_ptr<Encapsulation> rhs)
149155
{
150-
DVectorT* f_cast = dynamic_cast<DVectorT*>(f);
151-
assert(f_cast != nullptr);
152-
DVectorT* q_cast = dynamic_cast<DVectorT*>(q);
153-
assert(q_cast != nullptr);
154-
DVectorT* rhs_cast = dynamic_cast<DVectorT*>(rhs);
155-
assert(rhs_cast != nullptr);
156+
shared_ptr<DVectorT> f_cast = dynamic_pointer_cast<DVectorT>(f);
157+
assert(f_cast);
158+
shared_ptr<DVectorT> q_cast = dynamic_pointer_cast<DVectorT>(q);
159+
assert(q_cast);
160+
shared_ptr<DVectorT> rhs_cast = dynamic_pointer_cast<DVectorT>(rhs);
161+
assert(rhs_cast);
156162

157163
this->f2comp(f_cast, q_cast, t, dt, rhs_cast);
158164
}
159165

160-
void f2comp(DVectorT* f, DVectorT* q, time t, time dt, DVectorT* rhs)
166+
void f2comp(shared_ptr<DVectorT> f, shared_ptr<DVectorT> q, time t, time dt,
167+
shared_ptr<DVectorT> rhs)
161168
{
162-
auto* z = fft.forward(*rhs);
169+
auto* z = fft.forward(rhs);
163170
for (size_t i = 0; i < q->size(); i++) {
164171
z[i] /= (1.0 - nu * double(dt) * lap[i]) * double(q->size());
165172
}
166-
fft.backward(*q);
173+
fft.backward(q);
167174

168175
for (size_t i = 0; i < q->size(); i++) {
169-
f->at(i) = (q->at(i) - rhs->at(i)) / double(dt);
176+
f->data()[i] = (q->data()[i] - rhs->data()[i]) / double(dt);
170177
}
171178
}
172-
173179
};
174180

175181
#endif

examples/advection_diffusion/fft.hpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define _FFT_HPP_
99

1010
#include <map>
11+
#include <memory>
1112
#include <cstddef>
1213

1314
#include <pfasst/encap/vector.hpp>
@@ -25,51 +26,51 @@ class FFT
2526
complex<double>* z;
2627
};
2728

28-
map<size_t, workspace*> workspaces;
29+
map<size_t, shared_ptr<workspace>> workspaces;
2930

3031
public:
3132
~FFT()
3233
{
3334
for (auto& x : workspaces) {
34-
auto* wk = std::get<1>(x);
35+
shared_ptr<workspace> wk = std::get<1>(x);
3536
fftw_free(wk->wk);
3637
fftw_destroy_plan(wk->ffft);
3738
fftw_destroy_plan(wk->ifft);
38-
delete wk;
39+
// delete wk;
3940
}
4041
workspaces.clear();
4142
}
4243

43-
workspace* get_workspace(size_t ndofs)
44+
shared_ptr<workspace> get_workspace(size_t ndofs)
4445
{
4546
if (workspaces.find(ndofs) == workspaces.end()) {
46-
workspace* wk = new workspace;
47+
shared_ptr<workspace> wk = make_shared<workspace>();
4748
wk->wk = fftw_alloc_complex(ndofs);
4849
wk->ffft = fftw_plan_dft_1d(ndofs, wk->wk, wk->wk, FFTW_FORWARD, FFTW_ESTIMATE);
4950
wk->ifft = fftw_plan_dft_1d(ndofs, wk->wk, wk->wk, FFTW_BACKWARD, FFTW_ESTIMATE);
5051
wk->z = reinterpret_cast<complex<double>*>(wk->wk);
51-
workspaces.insert(pair<size_t, workspace*>(ndofs, wk));
52+
workspaces.insert(pair<size_t, shared_ptr<workspace>>(ndofs, wk));
5253
}
5354

5455
return workspaces[ndofs];
5556
}
5657

57-
complex<double>* forward(const DVectorT& x)
58+
complex<double>* forward(shared_ptr<const DVectorT> x)
5859
{
59-
workspace* wk = get_workspace(x.size());
60-
for (size_t i = 0; i < x.size(); i++) {
61-
wk->z[i] = x[i];
60+
shared_ptr<workspace> wk = get_workspace(x->size());
61+
for (size_t i = 0; i < x->size(); i++) {
62+
wk->z[i] = x->data()[i];
6263
}
6364
fftw_execute_dft(wk->ffft, wk->wk, wk->wk);
6465
return wk->z;
6566
}
6667

67-
void backward(DVectorT& x)
68+
void backward(shared_ptr<DVectorT> x)
6869
{
69-
workspace* wk = get_workspace(x.size());
70+
shared_ptr<workspace> wk = get_workspace(x->size());
7071
fftw_execute_dft(wk->ifft, wk->wk, wk->wk);
71-
for (size_t i = 0; i < x.size(); i++) {
72-
x[i] = real(wk->z[i]);
72+
for (size_t i = 0; i < x->size(); i++) {
73+
x->data()[i] = real(wk->z[i]);
7374
}
7475
}
7576

examples/advection_diffusion/serial_mlsdc.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* This example uses a (serial) multi-level SDC sweeper.
55
*/
66

7+
#include <memory>
8+
79
#include <fftw3.h>
810

911
#include <pfasst.hpp>
@@ -37,10 +39,10 @@ int main(int argc, char** argv)
3739
* (according to 'xrat').
3840
*/
3941
for (size_t l = 0; l < nlevs; l++) {
40-
auto nodes = compute_nodes<double>(nnodes, "gauss-lobatto");
41-
auto* factory = new VectorFactory<double>(ndofs);
42-
auto* sweeper = new AdvectionDiffusionSweeper<>(ndofs);
43-
auto* transfer = new SpectralTransfer1D<>();
42+
auto nodes = compute_nodes<double>(nnodes, "gauss-lobatto");
43+
auto factory = make_shared<VectorFactory<double>>(ndofs);
44+
auto sweeper = make_shared<AdvectionDiffusionSweeper<>>(ndofs);
45+
auto transfer = make_shared<SpectralTransfer1D<>>();
4446

4547
sweeper->set_nodes(nodes);
4648
sweeper->set_factory(factory);
@@ -61,8 +63,8 @@ int main(int argc, char** argv)
6163
/*
6264
* set initial conditions on each level
6365
*/
64-
auto* sweeper = mlsdc.get_level<AdvectionDiffusionSweeper<>>(mlsdc.nlevels() - 1);
65-
auto* q0 = sweeper->get_state(0);
66+
auto sweeper = mlsdc.get_finest<AdvectionDiffusionSweeper<>>();
67+
auto q0 = sweeper->get_state(0);
6668
sweeper->exact(q0, 0.0);
6769

6870
/*

examples/advection_diffusion/serial_mlsdc_autobuild.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
* controller.
88
*/
99

10+
#include <memory>
11+
#include <cassert>
12+
1013
#include <fftw3.h>
1114

1215
#include <pfasst.hpp>
@@ -43,9 +46,9 @@ int main(int argc, char** argv)
4346
* captures the 'ndofs' variable from above.
4447
*/
4548
auto build_level = [ndofs](size_t level) {
46-
auto* factory = new VectorFactory<double>(ndofs[level]);
47-
auto* sweeper = new AdvectionDiffusionSweeper<>(ndofs[level]);
48-
auto* transfer = new SpectralTransfer1D<>();
49+
auto factory = make_shared<VectorFactory<double>>(ndofs[level]);
50+
auto sweeper = make_shared<AdvectionDiffusionSweeper<>>(ndofs[level]);
51+
auto transfer = make_shared<SpectralTransfer1D<>>();
4952

5053
return AutoBuildTuple<>(sweeper, transfer, factory);
5154
};
@@ -54,9 +57,9 @@ int main(int argc, char** argv)
5457
* the 'initial' function is called once for each level to set the
5558
* intial conditions.
5659
*/
57-
auto initial = [](EncapSweeper<>* sweeper,
58-
Encapsulation<>* q0) {
59-
auto* ad = dynamic_cast<AdvectionDiffusionSweeper<>*>(sweeper);
60+
auto initial = [](shared_ptr<EncapSweeper<>> sweeper, shared_ptr<Encapsulation<>> q0) {
61+
auto ad = dynamic_pointer_cast<AdvectionDiffusionSweeper<>>(sweeper);
62+
assert(ad);
6063
ad->exact(q0, 0.0);
6164
};
6265

0 commit comments

Comments
 (0)