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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
%{
#include <WarpX.H>
using namespace amrex;
%}
%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;
%include "../../amrex/Src/Base/AMReX_Array.H"
%extend amrex::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);
}
}
// This is needed so that swig knows the amrex::Real is just a float or double
%include "../../amrex/Src/Base/AMReX_REAL.H"
%template(arrayReal) amrex::Array<amrex::Real>;
%template(arrayInt) amrex::Array<int>;
%template(arrayGeometry) amrex::Array<amrex::Geometry>;
%extend amrex::Array<amrex::Real> {
PyObject *get_real() {
// Get the data as a writeable numpy array, directly accessing the memory.
PyObject *arr = 0;
npy_intp dims[1];
dims[0] = self->std::vector<amrex::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 amrex::Array<int> {
PyObject *get_int() {
// Get the data as a writeable numpy array, directly accessing the memory.
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;
}
}
// This is needed by swig to define the macro D_DECL when including AMReX_IntVect.H
%include "../../amrex/Src/Base/AMReX_SPACE.H"
// This include can only be done with the modified AMReX_IntVect.H file that hides the ref-qualifiers from swig.
%include "../../amrex/Src/Base/AMReX_IntVect.H"
// Save this code, which is needed if AMReX_IntVect.H cannot be included.
// AMReX_IntVect.H uses ref-qualifiers in the lines setting up getVect that cannot be parsed by swig.
/*
class amrex::IntVect {
public:
IntVect(int i, int j, int k);
IntVect(const amrex::IntVect& rhs);
IntVect& shift(int coord, int s);
// static functions
static const amrex::IntVect& TheZeroVector();
static const amrex::IntVect& TheUnitVector();
static const amrex::IntVect& TheNodeVector();
static const amrex::IntVect& TheCellVector();
};
*/
%extend amrex::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 amrex::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 "../../amrex/Src/Base/AMReX_Box.H"
%include "../../amrex/Src/Base/AMReX_BaseFab.H"
%template(BaseFabReal) amrex::BaseFab<amrex::Real>;
%extend amrex::BaseFab<amrex::Real> {
PyObject * get(size_t n=0) {
PyObject *arr = 0;
npy_intp dims[BL_SPACEDIM];
amrex::IntVect size = self->box().size();
dims[0] = size[0];
dims[1] = size[1];
dims[2] = size[2];
arr = PyArray_NewFromDescr(&PyArray_Type,
PyArray_DescrFromType(NPY_DOUBLE), BL_SPACEDIM, dims, NULL,
self->dataPtr(n), NPY_FORTRANORDER|NPY_ARRAY_WRITEABLE, NULL);
Py_INCREF(arr);
return arr;
}
}
//%include "../../amrex/Src/Base/AMReX_FArrayBox.H"
//%include "../../amrex/Src/Base/AMReX_BoxArray.H"
%include "../../amrex/Src/Base/AMReX_FabArray.H"
%extend amrex::FabArray {
FAB& __getitem__ (size_t i)
{
BL_ASSERT(i >= 0);
BL_ASSERT(i < self->size());
return self->get(i);
}
//
// Same as above, except acts on const Array's.
//
const FAB& __getitem__ (size_t i) const
{
BL_ASSERT(i >= 0);
BL_ASSERT(i < self->size());
return self->get(i);
}
}
%template(FabArrayFArrayBox) amrex::FabArray< amrex::FArrayBox >;
%include "../../amrex/Src/Base/AMReX_MultiFab.H"
%template(arrayMultifab) amrex::Array< std::unique_ptr<amrex::MultiFab> >;
%template(arrayarrayMultifab) amrex::Array<amrex::Array< std::unique_ptr<amrex::MultiFab> > >;
#if (BL_SPACEDIM > 2)
// GetDLogA is only defined with BL_SPACEDIM <= 2
%ignore GetDLogA;
#endif
%include "../../amrex/Src/Base/AMReX_Geometry.H"
%template(arrayBoxArray) amrex::Array<amrex::BoxArray>;
%include "../../amrex/Src/Particle/AMReX_Particles.H"
// Becuase of an apparent problem in swig, the macro around the wrapping of tile_size gives an error during compilation
%ignore tile_size;
// Swig doesn't handle the unique_ptr return value causing an error during compilation
%ignore GetChargeDensity;
// Wrapping WarpXParIter fails since swig doesn't handle the alias SoA properly causing an error during compilation
%ignore WarpXParIter;
%template("WarpXParticleContainerBase") amrex::ParticleContainer<0,0,PIdx::nattribs>;
%include "../Source/ParticleContainer.H"
%include "../Source/WarpXParticleContainer.H"
%extend WarpXParticleContainer {
int getNattribs() {
return PIdx::nattribs;
}
PyObject * getLocations() {
amrex::Array<amrex::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) {
amrex::Array<amrex::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;
amrex::Array<amrex::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 "../../amrex/Src/AmrCore/AMReX_AmrCore.H"
%include "../Source/WarpX.H"
|