Skip to content

Commit dd36de0

Browse files
committed
auto: Add auto_build, auto_initial, and ex3.cpp.
Signed-off-by: Matthew Emmett <[email protected]>
1 parent ef2ad09 commit dd36de0

File tree

5 files changed

+136
-6
lines changed

5 files changed

+136
-6
lines changed

examples/advection_diffusion/ex2.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010
#include "advection_diffusion_sweeper.hpp"
1111
#include "spectral_transfer_1d.hpp"
1212

13+
using namespace pfasst;
14+
using namespace pfasst::encap;
15+
1316
int main(int argc, char **argv)
1417
{
15-
pfasst::MLSDC<double> mlsdc;
18+
MLSDC<double> mlsdc;
1619

1720
const int nlevs = 2;
1821
const int nsteps = 4;
@@ -31,8 +34,8 @@ int main(int argc, char **argv)
3134
* (according to 'xrat').
3235
*/
3336
for (int l=0; l<nlevs; l++) {
34-
auto nodes = pfasst::compute_nodes<double>(nnodes, "gauss-lobatto");
35-
auto* factory = new pfasst::encap::VectorFactory<double,double>(ndofs);
37+
auto nodes = compute_nodes<double>(nnodes, "gauss-lobatto");
38+
auto* factory = new VectorFactory<double,double>(ndofs);
3639
auto* sweeper = new AdvectionDiffusionSweeper<double,double>(ndofs);
3740
auto* transfer = new SpectralTransfer1D<double,double>();
3841

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Advection/diffusion example using an encapsulated IMEX sweeper.
3+
*
4+
* This example uses a (serial) multi-level SDC sweeper. It is
5+
* functionally exactly the same as ex2.cpp, but uses the 'auto
6+
* builder' to shorten the build and setup stages of the MLSDC
7+
* controller.
8+
*/
9+
10+
#include <tuple>
11+
12+
#include <pfasst.hpp>
13+
#include <pfasst/encap/automagic.hpp>
14+
#include <pfasst/encap/vector.hpp>
15+
16+
#include "advection_diffusion_sweeper.hpp"
17+
#include "spectral_transfer_1d.hpp"
18+
19+
using namespace std;
20+
using namespace pfasst;
21+
using namespace pfasst::encap;
22+
23+
int main(int argc, char **argv)
24+
{
25+
MLSDC<double> mlsdc;
26+
27+
const int nsteps = 4;
28+
const double dt = 0.01;
29+
const int niters = 4;
30+
31+
vector<pair<int,string>> nodes = {
32+
{ 3, "gauss-lobatto" },
33+
{ 5, "gauss-lobatto" }
34+
};
35+
36+
vector<int> ndofs = { 64, 128 };
37+
38+
/*
39+
* the 'build' function is called once for each level, and returns a
40+
* tuple containing a sweeper, encapsulation factory, and transfer
41+
* routines. in this case our builder is a lambda function that
42+
* captures the 'ndofs' variable from above.
43+
*/
44+
auto build_level = [ndofs] (unsigned int level) {
45+
auto* factory = new VectorFactory<double,double>(ndofs[level]);
46+
auto* sweeper = new AdvectionDiffusionSweeper<double,double>(ndofs[level]);
47+
auto* transfer = new SpectralTransfer1D<double,double>();
48+
49+
return auto_build_tuple<double,double>(sweeper,transfer,factory);
50+
};
51+
52+
/*
53+
* the 'initial' function is called once for each level to set the
54+
* intial conditions.
55+
*/
56+
auto initial = [] (EncapsulatedSweeperMixin<double,double> *sweeper, Encapsulation<double,double> *q0) {
57+
auto* ad = dynamic_cast<AdvectionDiffusionSweeper<double,double>*>(sweeper);
58+
ad->exact(q0, 0.0);
59+
};
60+
61+
auto_build<double,double>(mlsdc, nodes, build_level);
62+
auto_setup<double,double>(mlsdc, initial);
63+
mlsdc.set_duration(dt, nsteps, niters);
64+
mlsdc.run();
65+
66+
return 0;
67+
}

examples/advection_diffusion/fft.hpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,16 @@ class FFT {
3131

3232
~FFT()
3333
{
34-
// XXX
34+
for (auto& x: workspaces) {
35+
auto* wk = std::get<1>(x);
36+
fftw_destroy_plan(wk->ffft);
37+
fftw_destroy_plan(wk->ifft);
38+
fftw_free(wk->wk);
39+
delete wk;
40+
// workspaces.erase(std::get<0>(x));
41+
}
42+
workspaces.clear();
43+
// workspaces.clear();
3544
}
3645

3746
workspace* get_workspace(int ndofs)

include/pfasst/encap/automagic.hpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
#ifndef _PFASST_ENCAP_AUTOMAGIC_HPP_
3+
#define _PFASST_ENCAP_AUTOMAGIC_HPP_
4+
5+
#include <tuple>
6+
7+
#include "../quadrature.hpp"
8+
#include "encapsulation.hpp"
9+
10+
using namespace std;
11+
12+
namespace pfasst {
13+
namespace encap {
14+
15+
template<typename scalar, typename time>
16+
using auto_build_tuple = tuple<pfasst::encap::EncapsulatedSweeperMixin<scalar,time>*,
17+
pfasst::ITransfer*,
18+
pfasst::encap::EncapsulationFactory<scalar,time>*>;
19+
20+
template<typename scalar, typename time, typename controllerT, typename buildT>
21+
void auto_build(controllerT& c, vector<pair<int,string>> nodes, buildT build) {
22+
for (unsigned int l=0; l<nodes.size(); l++) {
23+
auto nds = pfasst::compute_nodes<time>(get<0>(nodes[l]), get<1>(nodes[l]));
24+
auto_build_tuple<scalar,time> tpl = build(l);
25+
auto* sweeper = get<0>(tpl);
26+
auto* transfer = get<1>(tpl);
27+
auto* factory = get<2>(tpl);
28+
sweeper->set_nodes(nds);
29+
sweeper->set_factory(factory);
30+
c.add_level(sweeper, transfer, false);
31+
}
32+
}
33+
34+
template<typename scalar, typename time, typename controllerT, typename initialT>
35+
void auto_setup(controllerT& c, initialT initial) {
36+
c.setup();
37+
for (int l=0; l<c.nlevels(); l++) {
38+
// auto* sweeper = c.get_level<pfasst::encap::EncapsulatedSweeperMixin<scalar,time>>(l);
39+
auto* isweeper = c.get_level(l);
40+
auto* sweeper = dynamic_cast<pfasst::encap::EncapsulatedSweeperMixin<scalar,time>*>(isweeper);
41+
auto* q0 = sweeper->get_state(0);
42+
initial(sweeper, q0);
43+
// sweeper->exact(q0, 0.0);
44+
}
45+
}
46+
47+
}
48+
}
49+
50+
#endif

include/pfasst/encap/imex.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ namespace pfasst {
2222

2323
~IMEX()
2424
{
25-
for (int m=0; m<Q.size(); m++) delete Q[m];
25+
for (int m=0; m<Q.size(); m++) delete Q[m];
26+
for (int m=0; m<S.size(); m++) delete S[m];
27+
for (int m=0; m<T.size(); m++) delete T[m];
2628
for (int m=0; m<pQ.size(); m++) delete pQ[m];
27-
for (int m=0; m<S.size(); m++) delete S[m];
2829
for (int m=0; m<Fe.size(); m++) delete Fe[m];
2930
for (int m=0; m<Fi.size(); m++) delete Fi[m];
3031
}

0 commit comments

Comments
 (0)