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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
%{
#include <iostream>
#include <ostream>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <Array.H>
#include <IntVect.H>
#include <Particles.H>
#include <Box.H>
#include <FArrayBox.H>
#include <BoxArray.H>
#include <MultiFab.H>
#include <Geometry.H>
#include <WarpX.H>
%}
%inline %{
std::ifstream & open_ifstream(const char *filename) {
std::ifstream *infile = new std::ifstream(filename);
return *infile;
}
%}
%inline %{
std::ofstream & open_ofstream(const char *filename) {
std::ofstream *outfile = new std::ofstream(filename);
return *outfile;
}
%}
%inline %{
void close_ofstream(std::ofstream& str) {
str.close();
}
%}
%inline %{
void close_ifstream(std::ifstream & str) {
str.close();
}
%}
%include <std_string.i>
%rename(__str__) display;
typedef double Real;
%include "../../BoxLib/Src/C_BaseLib/Array.H"
%extend Array {
T& __getitem__ (size_t i)
{
BL_ASSERT(i >= 0);
BL_ASSERT(i < self->size());
return self->std::vector<T>::operator[](i);
}
//
// Same as above, except acts on const Array's.
//
const T& __getitem__ (size_t i) const
{
BL_ASSERT(i >= 0);
BL_ASSERT(i < self->size());
return self->std::vector<T>::operator[](i);
}
}
%template(arrayReal) Array<Real>;
%template(arrayInt) Array<int>;
%template(arrayGeometry) Array<Geometry>;
%extend Array<Real> {
PyObject *get_real() {
PyObject *arr = 0;
npy_intp dims[1];
dims[0] = self->std::vector<Real>::size();
arr = PyArray_NewFromDescr(&PyArray_Type,
PyArray_DescrFromType(NPY_DOUBLE), 1, dims, NULL,
self->dataPtr(), NPY_FORTRANORDER|NPY_ARRAY_WRITEABLE, NULL);
Py_INCREF(arr);
return arr;
}
}
%extend Array<int> {
PyObject *get_int() {
PyObject *arr = 0;
npy_intp dims[1];
dims[0] = self->std::vector<int>::size();
arr = PyArray_NewFromDescr(&PyArray_Type,
PyArray_DescrFromType(NPY_INT), 1, dims, NULL,
self->dataPtr(), NPY_FORTRANORDER|NPY_ARRAY_WRITEABLE, NULL);
Py_INCREF(arr);
return arr;
}
}
// Note that IntVect.H cannot be directly included since swig cannot parse the line setting up "const int* getVect".
//%include "../../BoxLib/Src/C_BaseLib/IntVect.H"
class IntVect {
public:
IntVect(int i, int j, int k);
IntVect(const IntVect& rhs);
IntVect& shift(int coord, int s);
// static functions
static const IntVect& TheZeroVector();
static const IntVect& TheUnitVector();
static const IntVect& TheNodeVector();
static const IntVect& TheCellVector();
};
%extend IntVect {
//void writeOn(std::ofstream *os){
// *os << *self;
//}
//void read(std::ifstream* ifs){
// *ifs >> *self;
//}
int __getitem__(int index){
if (index < 0 || index >= BL_SPACEDIM) {
// SWIG_SetErrorMsg(PyExc_IndexError,"Index out of bounds\n");
return 0;
}
return (*self)[index];
}
int __len__() volatile { return BL_SPACEDIM; }
void __setitem__(int index,int val){
if (index < 0 || index >= BL_SPACEDIM) {
//SWIG_SetErrorMsg(PyExc_IndexError,"Index out of bounds\n");
} else {
(*self).setVal(index,val);
}
}
int __cmp__(const IntVect* other){
if( (*self) == (*other) ) {
return 0;
}
if( (*self) <= (*other) ) {
return -1;
}
return 1;
}
PyObject *get() {
PyObject *arr = 0;
npy_intp dims[1];
dims[0] = BL_SPACEDIM;
arr = PyArray_NewFromDescr(&PyArray_Type,
PyArray_DescrFromType(NPY_INT), 1, dims, NULL,
self->getVect(), NPY_FORTRANORDER|NPY_ARRAY_WRITEABLE, NULL);
Py_INCREF(arr);
return arr;
}
//std::string display() {
// std::ostringstream str;
// str << *self;
// return str.str();
//}
}
//%include "../../BoxLib/Src/C_BaseLib/Box.H"
//%include "../../BoxLib/Src/C_BaseLib/FArrayBox.H"
//%include "../../BoxLib/Src/C_BaseLib/BoxArray.H"
//%include "../../BoxLib/Src/C_BaseLib/MultiFab.H"
//#if (BL_SPACEDIM > 2)
%ignore GetDLogA;
//#endif
%include "../../BoxLib/Src/C_BaseLib/Geometry.H"
%template(arrayBoxArray) Array<BoxArray>;
%include "../../BoxLib/Src/C_ParticleLib/Particles.H"
//%template("WarpXParticleBase") Particle<PIdx::nattribs,0>;
%template("WarpXParticleContainerBase") ParticleContainer<PIdx::nattribs,0,std::vector<Particle<PIdx::nattribs,0> > >;
%ignore GetChargeDensity;
%include "../Source/ParticleContainer.H"
%include "../Source/WarpXParticleContainer.H"
%extend WarpXParticleContainer {
PyObject * getLocations() {
Array<Real> result(0);
self->GetParticleLocations(result);
npy_intp dims[2] = {BL_SPACEDIM, self->TotalNumberOfParticles()};
PyObject *arr = PyArray_NewFromDescr(&PyArray_Type,
PyArray_DescrFromType(NPY_DOUBLE), 2, dims, NULL,
result.dataPtr(), NPY_ARRAY_F_CONTIGUOUS|NPY_ARRAY_WRITEABLE, NULL);
Py_INCREF(arr);
return arr;
}
PyObject * getData(int start_comp, int num_comp) {
Array<Real> result(0);
self->GetParticleData(result, start_comp, num_comp);
npy_intp dims[2] = {num_comp, self->TotalNumberOfParticles()};
PyObject *arr = PyArray_NewFromDescr(&PyArray_Type,
PyArray_DescrFromType(NPY_DOUBLE), 2, dims, NULL,
result.dataPtr(), NPY_ARRAY_F_CONTIGUOUS|NPY_ARRAY_WRITEABLE, NULL);
Py_INCREF(arr);
return arr;
}
PyObject * getAllData() {
int num_comp = PIdx::nattribs;
Array<Real> result(0);
self->GetParticleData(result, 0, num_comp);
npy_intp dims[2] = {num_comp, self->TotalNumberOfParticles()};
PyObject *arr = PyArray_NewFromDescr(&PyArray_Type,
PyArray_DescrFromType(NPY_DOUBLE), 2, dims, NULL,
result.dataPtr(), NPY_ARRAY_F_CONTIGUOUS|NPY_ARRAY_WRITEABLE, NULL);
Py_INCREF(arr);
return arr;
}
};
%include "../../BoxLib/Src/C_AmrCoreLib/AmrCore.H"
%include "../Source/WarpX.H"
|