-
Notifications
You must be signed in to change notification settings - Fork 0
/
samplesim.cc
67 lines (60 loc) · 1.89 KB
/
samplesim.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "capy.hh"
#include "array.hh"
#include <fstream>
#include <iostream>
#include <iomanip>
class MySimulation
{
public:
Capy::Mapping config;
MySimulation(const Capy::Mapping& config_)
: config(config_),
f(config.setdefault("f", Capy::eval("lambda x: x * x")))
{}
void do_time_step(double time_step)
{
double x0 = config.setdefault("x0", 0.0);
double x1 = config.setdefault("x1", 1.0);
x.clear();
y.clear();
for (double t = x0; t < x1 + time_step*1e-10; t += time_step)
{
x.push_back(t);
y.push_back(f(t));
}
config.set("x", Capy::Array(&x[0], x.size()));
config.set("y", Capy::Array(&y[0], y.size()));
}
void write_output(const char *filename)
{
const char *name;
bool verbose = config.setdefault("verbose", false);
if (verbose)
name = config.get("name");
std::ofstream file(filename);
for (unsigned i = 0; i < y.size(); ++i)
if (verbose)
file << name << "(" << std::setw(12) << x[i] << ") = "
<< std::setw(12) << y[i] << "\n";
else
file << std::setw(12) << y[i] << "\n";
}
private:
Capy::Object f;
std::vector<double> x;
std::vector<double> y;
};
PyMODINIT_FUNC
initsamplesim()
{
import_array();
Capy::Extension extension(
"samplesim", "An example of a simulation wrapped with Capy");
Capy::Class<MySimulation> mysim(
extension, "MySimulation", "A stupid simulation examples class");
mysim.add_method<double, &MySimulation::do_time_step>(
"do_time_step", "Run a single time step of the simulation.");
mysim.add_method<const char *, &MySimulation::write_output>(
"write_output", "Write output to the given file name.");
mysim.add_py_member("config", &MySimulation::config);
}