Skip to content

Commit ddf9376

Browse files
committed
some cleanup and C++11-fications + copy/move ctors
1 parent c89d4f6 commit ddf9376

File tree

3 files changed

+56
-22
lines changed

3 files changed

+56
-22
lines changed

examples/advection_diffusion/advection_diffusion_sweeper.hpp

Lines changed: 2 additions & 0 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

include/pfasst/encap/imex_sweeper.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <iostream>
66

7+
#include "encapsulation.hpp"
78
#include "encap_sweeper.hpp"
89

910
using namespace std;
@@ -59,12 +60,10 @@ namespace pfasst
5960
Fi[0]->copy(Fi.back());
6061
}
6162

62-
6363
virtual void integrate(time dt, vector<Encapsulation<time>*> dst) const
6464
{
65-
auto* encap = dst[0];
66-
encap->mat_apply(dst, dt, Smat, Fe, true);
67-
encap->mat_apply(dst, dt, Smat, Fi, false);
65+
dst[0]->mat_apply(dst, dt, Smat, Fe, true);
66+
dst[0]->mat_apply(dst, dt, Smat, Fi, false);
6867
}
6968

7069
void setup(bool coarse)

include/pfasst/encap/vector.hpp

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@
99
#include <vector>
1010
#include <cassert>
1111

12-
#include <cstdio>
13-
#include <cstdlib>
14-
#include <cstring>
15-
#include <unistd.h>
16-
17-
#define GPCHKERR(err, msg) if ((err) == -1) { perror(msg); return; }
12+
#include <boost/numeric/ublas/matrix.hpp>
1813

1914
#include "encapsulation.hpp"
2015

@@ -35,16 +30,51 @@ namespace pfasst
3530
{
3631
public:
3732
//! @{
38-
VectorEncapsulation(int size) : vector<scalar>(size)
33+
/**
34+
*/
35+
VectorEncapsulation(int size)
36+
: vector<scalar>(size)
3937
{
4038
zero();
4139
}
40+
41+
/**
42+
* @brief copy constuctor
43+
* @note delegated to sdt::vector<scalar>
44+
*/
45+
VectorEncapsulation(const VectorEncapsulation<scalar, time>& other)
46+
: vector<scalar>(other)
47+
{}
48+
/**
49+
* @throws std::bad_cast
50+
* if `other` can not be transformed into pfasst::encap::VectorEncapsulation via
51+
* `dynamic_cast`
52+
*/
53+
VectorEncapsulation(const Encapsulation<time>& other)
54+
: VectorEncapsulation(dynamic_cast<const VectorEncapsulation<scalar, time>>(other))
55+
{}
56+
57+
/**
58+
* @brief move constructor
59+
* @note delegated to std::vector<scalar>
60+
*/
61+
VectorEncapsulation(VectorEncapsulation<scalar, time>&& other)
62+
: vector<scalar>(other)
63+
{}
64+
/**
65+
* @throws std::bad_cast
66+
* if `other` can not be transformed into pfasst::encap::VectorEncapsulation via
67+
* `dynamic_cast`
68+
*/
69+
VectorEncapsulation(Encapsulation<time>&& other)
70+
: VectorEncapsulation(dynamic_cast<VectorEncapsulation<scalar, time>&&>(other))
71+
{}
4272
//! @}
4373

4474
//! @{
4575
void zero()
4676
{
47-
std::fill(this->begin(), this->end(), 0.0);
77+
this->assign(this->size(), scalar(0.0));
4878
}
4979

5080
void copy(const Encapsulation<time>* x)
@@ -56,7 +86,7 @@ namespace pfasst
5686

5787
void copy(const VectorEncapsulation<scalar, time>* x)
5888
{
59-
std::copy(x->begin(), x->end(), this->begin());
89+
std::copy(x->cbegin(), x->cend(), this->begin());
6090
}
6191
//! @}
6292

@@ -71,10 +101,15 @@ namespace pfasst
71101

72102
void saxpy(time a, const VectorEncapsulation<scalar, time>* x)
73103
{
104+
assert(this->size() == x->size());
74105
for (size_t i = 0; i < this->size(); i++)
75106
{ this->at(i) += a * x->at(i); }
76107
}
77108

109+
/**
110+
* @note In case any of the elements of `dst` or `src` can not be transformed via
111+
* `dynamic_cast` into pfasst::encap::VectorEncapsulation std::abort is called.
112+
*/
78113
void mat_apply(vector<Encapsulation<time>*> dst, time a, matrix<time> mat,
79114
vector<Encapsulation<time>*> src, bool zero = true)
80115
{
@@ -91,7 +126,7 @@ namespace pfasst
91126
assert(src_cast[m] != nullptr);
92127
}
93128

94-
this->mat_apply(dst_cast, a, mat, src_cast, zero);
129+
dst_cast[0]->mat_apply(dst_cast, a, mat, src_cast, zero);
95130
}
96131

97132
void mat_apply(vector<VectorEncapsulation<scalar, time>*> dst, time a, matrix<time> mat,
@@ -100,7 +135,7 @@ namespace pfasst
100135
size_t ndst = dst.size();
101136
size_t nsrc = src.size();
102137

103-
if (zero) { for (size_t n = 0; n < ndst; n++) { dst[n]->zero(); } }
138+
if (zero) { for (auto elem : dst) { elem->zero(); } }
104139

105140
size_t ndofs = dst[0]->size();
106141
for (size_t i = 0; i < ndofs; i++) {
@@ -113,16 +148,14 @@ namespace pfasst
113148
}
114149

115150
/**
116-
* maximum norm of contained data.
151+
* maximum norm of contained elements.
152+
*
153+
* This uses std::max with custom comparison function.
117154
*/
118155
scalar norm0() const
119156
{
120-
scalar max = 0.0;
121-
for (size_t i = 0; i < this->size(); i++) {
122-
scalar v = abs(this->at(i));
123-
if (v > max) { max = v; }
124-
}
125-
return max;
157+
return std::max(this->cbegin(), this->cend(),
158+
[](scalar a, scalar b) {return std::abs(a) < std::abs(b); } );
126159
}
127160
//! @}
128161
};

0 commit comments

Comments
 (0)