Skip to content

Commit bc3c9df

Browse files
committed
vector: Move as_vector to vector.hpp as suggested by Ruprecht.
This also overrides for the const case, as suggested by Klatt. Signed-off-by: Matthew Emmett <[email protected]>
1 parent 455a4c1 commit bc3c9df

File tree

3 files changed

+46
-45
lines changed

3 files changed

+46
-45
lines changed

examples/advection_diffusion/advection_diffusion_sweeper.hpp

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@
1919
#endif
2020

2121
using namespace std;
22+
using pfasst::encap::Encapsulation;
23+
using pfasst::encap::as_vector;
24+
2225

2326
template<typename time = pfasst::time_precision>
2427
class AdvectionDiffusionSweeper
2528
: public pfasst::encap::IMEXSweeper<time>
2629
{
27-
typedef pfasst::encap::Encapsulation<time> Encapsulation;
28-
2930
FFT fft;
3031

3132
vector<complex<double>> ddx, lap;
@@ -35,12 +36,6 @@ class AdvectionDiffusionSweeper
3536
double nu = 0.02;
3637
size_t nf1evals = 0;
3738

38-
inline DVectorT& as_dvector(shared_ptr<Encapsulation> x)
39-
{
40-
shared_ptr<DVectorT> y = dynamic_pointer_cast<DVectorT>(x); assert(y);
41-
return *y.get();
42-
}
43-
4439
public:
4540
AdvectionDiffusionSweeper(size_t nvars)
4641
{
@@ -58,9 +53,9 @@ class AdvectionDiffusionSweeper
5853
cout << "number of f1 evals: " << nf1evals << endl;
5954
}
6055

61-
void exact(shared_ptr<Encapsulation> q, time t)
56+
void exact(shared_ptr<Encapsulation<time>> q, time t)
6257
{
63-
this->exact(as_dvector(q), t);
58+
this->exact(as_vector<double,time>(q), t);
6459
}
6560

6661
void exact(DVectorT& q, time t)
@@ -82,8 +77,8 @@ class AdvectionDiffusionSweeper
8277

8378
void echo_error(time t, bool predict = false)
8479
{
85-
DVectorT& qend = as_dvector(this->get_end_state());
86-
DVectorT qex(qend.size());
80+
auto& qend = as_vector<double,time>(this->get_end_state());
81+
DVectorT qex(qend.size());
8782

8883
exact(qex, t);
8984

@@ -109,10 +104,10 @@ class AdvectionDiffusionSweeper
109104
echo_error(t + dt);
110105
}
111106

112-
void f1eval(shared_ptr<Encapsulation> _f, shared_ptr<Encapsulation> _q, time /*t*/)
107+
void f1eval(shared_ptr<Encapsulation<time>> _f, shared_ptr<Encapsulation<time>> _q, time /*t*/)
113108
{
114-
DVectorT& q = as_dvector(_q);
115-
DVectorT& f = as_dvector(_f);
109+
auto& q = as_vector<double,time>(_q);
110+
auto& f = as_vector<double,time>(_f);
116111

117112
double c = -v / double(q.size());
118113

@@ -125,10 +120,10 @@ class AdvectionDiffusionSweeper
125120
nf1evals++;
126121
}
127122

128-
void f2eval(shared_ptr<Encapsulation> _f, shared_ptr<Encapsulation> _q, time /*t*/)
123+
void f2eval(shared_ptr<Encapsulation<time>> _f, shared_ptr<Encapsulation<time>> _q, time /*t*/)
129124
{
130-
DVectorT& q = as_dvector(_q);
131-
DVectorT& f = as_dvector(_f);
125+
auto& q = as_vector<double,time>(_q);
126+
auto& f = as_vector<double,time>(_f);
132127

133128
double c = nu / double(q.size());
134129

@@ -139,12 +134,12 @@ class AdvectionDiffusionSweeper
139134
fft.backward(f);
140135
}
141136

142-
void f2comp(shared_ptr<Encapsulation> _f, shared_ptr<Encapsulation> _q, time /*t*/, time dt,
143-
shared_ptr<Encapsulation> _rhs)
137+
void f2comp(shared_ptr<Encapsulation<time>> _f, shared_ptr<Encapsulation<time>> _q, time /*t*/, time dt,
138+
shared_ptr<Encapsulation<time>> _rhs)
144139
{
145-
DVectorT& q = as_dvector(_q);
146-
DVectorT& f = as_dvector(_f);
147-
DVectorT& rhs = as_dvector(_rhs);
140+
auto& q = as_vector<double,time>(_q);
141+
auto& f = as_vector<double,time>(_f);
142+
auto& rhs = as_vector<double,time>(_rhs);
148143

149144
auto* z = fft.forward(rhs);
150145
for (size_t i = 0; i < q.size(); i++) {

examples/advection_diffusion/spectral_transfer_1d.hpp

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,12 @@ class SpectralTransfer1D
2121

2222
FFT fft;
2323

24-
inline DVectorT& as_dvector(shared_ptr<Encapsulation> x)
25-
{
26-
shared_ptr<DVectorT> y = dynamic_pointer_cast<DVectorT>(x); assert(y);
27-
return *y.get();
28-
}
29-
30-
inline const DVectorT& as_const_dvector(shared_ptr<const Encapsulation> x)
31-
{
32-
shared_ptr<const DVectorT> y = dynamic_pointer_cast<const DVectorT>(x); assert(y);
33-
return *y.get();
34-
}
35-
3624
public:
3725

3826
void interpolate(shared_ptr<Encapsulation> dst, shared_ptr<const Encapsulation> src)
3927
{
40-
auto& fine = as_dvector(dst);
41-
auto& crse = as_const_dvector(src);
28+
auto& fine = as_vector<double,time>(dst);
29+
auto& crse = as_vector<double,time>(src);
4230

4331
auto* crse_z = fft.forward(crse);
4432
auto* fine_z = fft.get_workspace(fine.size())->z;
@@ -62,8 +50,8 @@ class SpectralTransfer1D
6250

6351
void restrict(shared_ptr<Encapsulation> dst, shared_ptr<const Encapsulation> src)
6452
{
65-
auto& fine = as_const_dvector(src);
66-
auto& crse = as_dvector(dst);
53+
auto& fine = as_vector<double,time>(src);
54+
auto& crse = as_vector<double,time>(dst);
6755

6856
size_t xrat = fine.size() / crse.size();
6957

include/pfasst/encap/vector.hpp

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ namespace pfasst
2727
* precision of the time points; defaults to pfasst::time_precision
2828
*/
2929
template<typename scalar, typename time = time_precision>
30-
class VectorEncapsulation
30+
class VectorEncapsulation
3131
: public vector<scalar>, public Encapsulation<time>
3232
{
3333
public:
3434
//! @{
3535
/**
3636
*/
37-
VectorEncapsulation(int size)
37+
VectorEncapsulation(int size)
3838
: vector<scalar>(size)
3939
{
4040
zero();
@@ -49,7 +49,7 @@ namespace pfasst
4949
{}
5050
/**
5151
* @throws std::bad_cast
52-
* if `other` can not be transformed into pfasst::encap::VectorEncapsulation via
52+
* if `other` can not be transformed into pfasst::encap::VectorEncapsulation via
5353
* `dynamic_cast`
5454
*/
5555
VectorEncapsulation(const Encapsulation<time>& other)
@@ -65,7 +65,7 @@ namespace pfasst
6565
{}
6666
/**
6767
* @throws std::bad_cast
68-
* if `other` can not be transformed into pfasst::encap::VectorEncapsulation via
68+
* if `other` can not be transformed into pfasst::encap::VectorEncapsulation via
6969
* `dynamic_cast`
7070
*/
7171
VectorEncapsulation(Encapsulation<time>&& other)
@@ -109,7 +109,7 @@ namespace pfasst
109109
}
110110

111111
/**
112-
* @note In case any of the elements of `dst` or `src` can not be transformed via
112+
* @note In case any of the elements of `dst` or `src` can not be transformed via
113113
* `dynamic_cast` into pfasst::encap::VectorEncapsulation std::abort is called.
114114
*/
115115
void mat_apply(vector<shared_ptr<Encapsulation<time>>> dst, time a, matrix<time> mat,
@@ -153,7 +153,7 @@ namespace pfasst
153153

154154
/**
155155
* maximum norm of contained elements.
156-
*
156+
*
157157
* This uses std::max with custom comparison function.
158158
*/
159159
scalar norm0() const
@@ -183,6 +183,24 @@ namespace pfasst
183183
}
184184
};
185185

186+
template<typename scalar, typename time = time_precision>
187+
VectorEncapsulation<scalar,time>& as_vector(shared_ptr<Encapsulation<time>> x)
188+
{
189+
typedef VectorEncapsulation<scalar,time> VectorT;
190+
shared_ptr<VectorT> y = dynamic_pointer_cast<VectorT>(x);
191+
assert(y);
192+
return *y.get();
193+
}
194+
195+
template<typename scalar, typename time = time_precision>
196+
const VectorEncapsulation<scalar,time>& as_vector(shared_ptr<const Encapsulation<time>> x)
197+
{
198+
typedef VectorEncapsulation<scalar,time> VectorT;
199+
shared_ptr<const VectorT> y = dynamic_pointer_cast<const VectorT>(x);
200+
assert(y);
201+
return *y.get();
202+
}
203+
186204
}
187205
}
188206

0 commit comments

Comments
 (0)