/[escript]/trunk/ripley/src/ripleycpp.cpp
ViewVC logotype

Contents of /trunk/ripley/src/ripleycpp.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4615 - (show annotations)
Mon Jan 13 05:05:33 2014 UTC (5 years, 3 months ago) by caltinay
File size: 18472 byte(s)
Step 1 for #31: wrap parameters in an object.

1
2 /*****************************************************************************
3 *
4 * Copyright (c) 2003-2013 by University of Queensland
5 * http://www.uq.edu.au
6 *
7 * Primary Business: Queensland, Australia
8 * Licensed under the Open Software License version 3.0
9 * http://www.opensource.org/licenses/osl-3.0.php
10 *
11 * Development until 2012 by Earth Systems Science Computational Center (ESSCC)
12 * Development since 2012 by School of Earth Sciences
13 *
14 *****************************************************************************/
15
16 #include <ripley/Brick.h>
17 #include <ripley/Rectangle.h>
18 #include <esysUtils/esysExceptionTranslator.h>
19
20 #include <boost/python.hpp>
21 #include <boost/python/module.hpp>
22 #include <boost/python/def.hpp>
23 #include <boost/python/detail/defaults_gen.hpp>
24 #include <boost/version.hpp>
25
26 using namespace boost::python;
27
28 namespace ripley {
29
30 escript::Data readBinaryGrid(std::string filename, escript::FunctionSpace fs,
31 const object& pyFirst, const object& pyNum, const object& pyMultiplier,
32 const object& pyShape, double fill, int byteOrder, int dataType)
33 {
34 int dim=fs.getDim();
35 GridParameters params;
36 std::vector<int> shape;
37
38 if (extract<tuple>(pyFirst).check() || extract<list>(pyFirst).check()) {
39 if (len(pyFirst)==dim) {
40 for (int i=0; i<dim; i++) {
41 params.first.push_back(extract<int>(pyFirst[i]));
42 }
43 } else
44 throw RipleyException("Argument 'first' has wrong length");
45 } else
46 throw RipleyException("Argument 'first' must be a tuple or list");
47
48 if (extract<tuple>(pyNum).check() || extract<list>(pyNum).check()) {
49 if (len(pyNum)==dim) {
50 for (int i=0; i<dim; i++) {
51 params.numValues.push_back(extract<int>(pyNum[i]));
52 }
53 } else
54 throw RipleyException("Argument 'numValues' has wrong length");
55 } else
56 throw RipleyException("Argument 'numValues' must be a tuple or list");
57
58 if (extract<tuple>(pyShape).check() || extract<list>(pyShape).check()) {
59 for (int i=0; i<len(pyShape); i++) {
60 shape.push_back(extract<int>(pyShape[i]));
61 }
62 } else
63 throw RipleyException("Argument 'shape' must be a tuple or list");
64
65 if (extract<tuple>(pyMultiplier).check() || extract<list>(pyMultiplier).check()) {
66 if (len(pyMultiplier)==dim) {
67 for (int i=0; i<dim; i++) {
68 params.multiplier.push_back(extract<int>(pyMultiplier[i]));
69 }
70 } else
71 throw RipleyException("Argument 'multiplier' has wrong length");
72 } else
73 throw RipleyException("Argument 'multiplier' must be a tuple or list");
74
75 const RipleyDomain* dom=dynamic_cast<const RipleyDomain*>(fs.getDomain().get());
76 if (!dom)
77 throw RipleyException("Function space must be on a ripley domain");
78
79
80 escript::Data res(fill, shape, fs, true);
81
82 dom->readBinaryGrid(res, filename, params);
83 return res;
84 }
85
86 escript::Data readNcGrid(std::string filename, std::string varname,
87 escript::FunctionSpace fs, const object& pyFirst, const object& pyNum,
88 const object& pyMultiplier, const object& pyShape, double fill=0.)
89 {
90 int dim=fs.getDim();
91 GridParameters params;
92 std::vector<int> shape;
93
94 if (extract<tuple>(pyFirst).check() || extract<list>(pyFirst).check()) {
95 if (len(pyFirst)==dim) {
96 for (int i=0; i<dim; i++) {
97 params.first.push_back(extract<int>(pyFirst[i]));
98 }
99 } else
100 throw RipleyException("Argument 'first' has wrong length");
101 } else
102 throw RipleyException("Argument 'first' must be a tuple or list");
103
104 if (extract<tuple>(pyNum).check() || extract<list>(pyNum).check()) {
105 if (len(pyNum)==dim) {
106 for (int i=0; i<dim; i++) {
107 params.numValues.push_back(extract<int>(pyNum[i]));
108 }
109 } else
110 throw RipleyException("Argument 'numValues' has wrong length");
111 } else
112 throw RipleyException("Argument 'numValues' must be a tuple or list");
113
114 if (extract<tuple>(pyMultiplier).check() || extract<list>(pyMultiplier).check()) {
115 if (len(pyMultiplier)==dim) {
116 for (int i=0; i<dim; i++) {
117 params.multiplier.push_back(extract<int>(pyMultiplier[i]));
118 }
119 } else
120 throw RipleyException("Argument 'multiplier' has wrong length");
121 } else
122 throw RipleyException("Argument 'multiplier' must be a tuple or list");
123
124 if (extract<tuple>(pyShape).check() || extract<list>(pyShape).check()) {
125 for (int i=0; i<len(pyShape); i++) {
126 shape.push_back(extract<int>(pyShape[i]));
127 }
128 } else
129 throw RipleyException("Argument 'shape' must be a tuple or list");
130
131 const RipleyDomain* dom=dynamic_cast<const RipleyDomain*>(fs.getDomain().get());
132 if (!dom)
133 throw RipleyException("Function space must be on a ripley domain");
134
135
136 escript::Data res(fill, shape, fs, true);
137
138 dom->readNcGrid(res, filename, varname, params);
139 return res;
140 }
141
142 // These wrappers are required to make the shared pointers work through the
143 // Python wrapper
144
145 // The double for n? is just to keep python happy when people need to deal with
146 // truediv
147 escript::Domain_ptr _brick(double _n0, double _n1, double _n2, const object& l0,
148 const object& l1, const object& l2, int d0, int d1, int d2)
149 {
150 int n0=static_cast<int>(_n0), n1=static_cast<int>(_n1), n2=static_cast<int>(_n2);
151 double x0=0., x1=1., y0=0., y1=1., z0=0., z1=1.;
152 if (extract<tuple>(l0).check()) {
153 tuple x=extract<tuple>(l0);
154 if (len(x)==2) {
155 x0=extract<double>(x[0]);
156 x1=extract<double>(x[1]);
157 } else
158 throw RipleyException("Argument l0 must be a float or 2-tuple");
159 } else if (extract<double>(l0).check()) {
160 x1=extract<double>(l0);
161 } else
162 throw RipleyException("Argument l0 must be a float or 2-tuple");
163
164 if (extract<tuple>(l1).check()) {
165 tuple y=extract<tuple>(l1);
166 if (len(y)==2) {
167 y0=extract<double>(y[0]);
168 y1=extract<double>(y[1]);
169 } else
170 throw RipleyException("Argument l1 must be a float or 2-tuple");
171 } else if (extract<double>(l1).check()) {
172 y1=extract<double>(l1);
173 } else
174 throw RipleyException("Argument l1 must be a float or 2-tuple");
175
176 if (extract<tuple>(l2).check()) {
177 tuple z=extract<tuple>(l2);
178 if (len(z)==2) {
179 z0=extract<double>(z[0]);
180 z1=extract<double>(z[1]);
181 } else
182 throw RipleyException("Argument l2 must be a float or 2-tuple");
183 } else if (extract<double>(l2).check()) {
184 z1=extract<double>(l2);
185 } else
186 throw RipleyException("Argument l2 must be a float or 2-tuple");
187
188 return escript::Domain_ptr(new Brick(n0,n1,n2, x0,y0,z0, x1,y1,z1, d0,d1,d2));
189 }
190
191 const int _q[]={0x61686969,0x746c4144,0x79616e43};
192 escript::Domain_ptr _rectangle(double _n0, double _n1, const object& l0,
193 const object& l1, int d0, int d1)
194 {
195 int n0=static_cast<int>(_n0), n1=static_cast<int>(_n1);
196 double x0=0., x1=1., y0=0., y1=1.;
197 if (extract<tuple>(l0).check()) {
198 tuple x=extract<tuple>(l0);
199 if (len(x)==2) {
200 x0=extract<double>(x[0]);
201 x1=extract<double>(x[1]);
202 } else
203 throw RipleyException("Argument l0 must be a float or 2-tuple");
204 } else if (extract<double>(l0).check()) {
205 x1=extract<double>(l0);
206 } else
207 throw RipleyException("Argument l0 must be a float or 2-tuple");
208
209 if (extract<tuple>(l1).check()) {
210 tuple y=extract<tuple>(l1);
211 if (len(y)==2) {
212 y0=extract<double>(y[0]);
213 y1=extract<double>(y[1]);
214 } else
215 throw RipleyException("Argument l1 must be a float or 2-tuple");
216 } else if (extract<double>(l1).check()) {
217 y1=extract<double>(l1);
218 } else
219 throw RipleyException("Argument l1 must be a float or 2-tuple");
220
221 return escript::Domain_ptr(new Rectangle(n0,n1, x0,y0, x1,y1, d0,d1));
222 }
223 std::string _who(){int a[]={_q[0]^42,_q[1]^42,_q[2]^42,0};return (char*)&a[0];}
224
225 } // end of namespace ripley
226
227
228 BOOST_PYTHON_MODULE(ripleycpp)
229 {
230 // This feature was added in boost v1.34
231 #if ((BOOST_VERSION/100)%1000 > 34) || (BOOST_VERSION/100000 >1)
232 // params are: bool show_user_defined, bool show_py_signatures, bool show_cpp_signatures
233 docstring_options docopt(true, true, false);
234 #endif
235
236 register_exception_translator<ripley::RipleyException>(&(esysUtils::esysExceptionTranslator));
237
238 scope().attr("__doc__") = "To use this module, please import esys.ripley";
239 scope().attr("BYTEORDER_NATIVE") = (int)ripley::BYTEORDER_NATIVE;
240 scope().attr("BYTEORDER_LITTLE_ENDIAN") = (int)ripley::BYTEORDER_LITTLE_ENDIAN;
241 scope().attr("BYTEORDER_BIG_ENDIAN") = (int)ripley::BYTEORDER_BIG_ENDIAN;
242 scope().attr("DATATYPE_INT32") = (int)ripley::DATATYPE_INT32;
243 scope().attr("DATATYPE_FLOAT32") = (int)ripley::DATATYPE_FLOAT32;
244 scope().attr("DATATYPE_FLOAT64") = (int)ripley::DATATYPE_FLOAT64;
245
246 def("Brick", ripley::_brick, (arg("n0"),arg("n1"),arg("n2"),arg("l0")=1.0,arg("l1")=1.0,arg("l2")=1.0,arg("d0")=-1,arg("d1")=-1,arg("d2")=-1),
247 "Creates a hexagonal mesh with n0 x n1 x n2 elements over the brick [0,l0] x [0,l1] x [0,l2].\n\n"
248 ":param n0: number of elements in direction 0\n:type n0: ``int``\n"
249 ":param n1: number of elements in direction 1\n:type n1: ``int``\n"
250 ":param n2: number of elements in direction 2\n:type n2: ``int``\n"
251 ":param l0: length of side 0 or coordinate range of side 0\n:type l0: ``float`` or ``tuple``\n"
252 ":param l1: length of side 1 or coordinate range of side 1\n:type l1: ``float`` or ``tuple``\n"
253 ":param l2: length of side 2 or coordinate range of side 2\n:type l2: ``float`` or ``tuple``\n"
254 ":param d0: number of subdivisions in direction 0\n:type d0: ``int``\n"
255 ":param d1: number of subdivisions in direction 1\n:type d1: ``int``\n"
256 ":param d2: number of subdivisions in direction 2\n:type d2: ``int``");
257
258 def("Rectangle", ripley::_rectangle, (arg("n0"),arg("n1"),arg("l0")=1.0,arg("l1")=1.0,arg("d0")=-1,arg("d1")=-1),
259 "Creates a rectangular mesh with n0 x n1 elements over the rectangle [0,l0] x [0,l1].\n\n"
260 ":param n0: number of elements in direction 0\n:type n0: ``int``\n"
261 ":param n1: number of elements in direction 1\n:type n1: ``int``\n"
262 ":param l0: length of side 0 or coordinate range of side 0\n:type l0: ``float`` or ``tuple``\n"
263 ":param l1: length of side 1 or coordinate range of side 1\n:type l1: ``float`` or ``tuple``\n"
264 ":param d0: number of subdivisions in direction 0\n:type d0: ``int``\n"
265 ":param d1: number of subdivisions in direction 1\n:type d1: ``int``");
266 def("_theculprit_", ripley::_who);
267
268 def("_readBinaryGrid", &ripley::readBinaryGrid, (arg("filename"), arg("functionspace"), arg("first"), arg("numValues"), arg("multiplier"), arg("shape"), arg("fill")=0., arg("byteOrder"), arg("dataType")));
269
270 def("_readNcGrid", &ripley::readNcGrid, (arg("filename"), arg("varname"), arg("functionspace"), arg("first"), arg("numValues"), arg("multiplier"), arg("shape"), arg("fill")=0.));
271
272 class_<ripley::RipleyDomain, bases<escript::AbstractContinuousDomain>, boost::noncopyable >
273 ("RipleyDomain", "", no_init)
274 .def("print_mesh_info", &ripley::RipleyDomain::Print_Mesh_Info, (arg("full")=false),
275 "Prints out a summary about the mesh.\n"
276 ":param full: whether to output additional data\n:type full: ``bool``")
277 .def("writeBinaryGrid", &ripley::RipleyDomain::writeBinaryGrid)
278
279 .def("dump", &ripley::RipleyDomain::dump, args("filename"),
280 "Dumps the mesh to a file with the given name.")
281 .def("getGridParameters", &ripley::RipleyDomain::getGridParameters,
282 "Returns the tuple (origin, spacing, elements) where the entries are tuples:\n"
283 "``origin``=the coordinates of the domain's global origin,\n"
284 "``spacing``=the element size (=node spacing) of the domain,\n"
285 "``elements``=the global number of elements in all dimensions\n\n"
286 ":rtype: ``tuple``")
287 .def("getDescription", &ripley::RipleyDomain::getDescription,
288 ":return: a description for this domain\n:rtype: ``string``")
289 .def("getDim", &ripley::RipleyDomain::getDim, ":rtype: ``int``")
290 .def("getDataShape", &ripley::RipleyDomain::getDataShape, args("functionSpaceCode"),
291 ":return: a pair (dps, ns) where dps=the number of data points per sample, and ns=the number of samples\n:rtype: ``tuple``")
292 .def("getNumDataPointsGlobal", &ripley::RipleyDomain::getNumDataPointsGlobal,
293 ":return: the number of data points summed across all MPI processes\n"
294 ":rtype: ``int``")
295 .def("addPDEToSystem",&ripley::RipleyDomain::addPDEToSystem,
296 args("mat", "rhs", "A", "B", "C", "D", "X", "Y", "d", "y", "d_contact", "y_contact"),
297 "adds a PDE onto the stiffness matrix mat and a rhs\n\n"
298 ":param mat:\n:type mat: `OperatorAdapter`\n:param rhs:\n:type rhs: `Data`\n"
299 ":param A:\n:type A: `Data`\n"
300 ":param B:\n:type B: `Data`\n"
301 ":param C:\n:type C: `Data`\n"
302 ":param D:\n:type D: `Data`\n"
303 ":param X:\n:type X: `Data`\n"
304 ":param Y:\n:type Y: `Data`\n"
305 ":param d:\n:type d: `Data`\n"
306 ":param d_contact:\n:type d_contact: `Data`\n"
307 ":param y_contact:\n:type y_contact: `Data`"
308 )
309 .def("addPDEToRHS",&ripley::RipleyDomain::addPDEToRHS,
310 args("rhs", "X", "Y", "y", "y_contact"),
311 "adds a PDE onto the stiffness matrix mat and a rhs\n\n"
312 ":param rhs:\n:type rhs: `Data`\n"
313 ":param X:\n:type X: `Data`\n"
314 ":param Y:\n:type Y: `Data`\n"
315 ":param y:\n:type y: `Data`\n"
316 ":param y_contact:\n:type y_contact: `Data`"
317 )
318 .def("addPDEToTransportProblem",&ripley::RipleyDomain::addPDEToTransportProblem,
319 args( "tp", "source", "M", "A", "B", "C", "D", "X", "Y", "d", "y", "d_contact", "y_contact"),
320 ":param tp:\n:type tp: `TransportProblemAdapter`\n"
321 ":param source:\n:type source: `Data`\n"
322 ":param M:\n:type M: `Data`\n"
323 ":param A:\n:type A: `Data`\n"
324 ":param B:\n:type B: `Data`\n"
325 ":param C:\n:type C: `Data`\n"
326 ":param D:\n:type D: `Data`\n"
327 ":param X:\n:type X: `Data`\n"
328 ":param Y:\n:type Y: `Data`\n"
329 ":param d:\n:type d: `Data`\n"
330 ":param y:\n:type y: `Data`\n"
331 ":param d_contact:\n:type d_contact: `Data`\n"
332 ":param y_contact:\n:type y_contact: `Data`"
333 )
334 .def("newOperator",&ripley::RipleyDomain::newSystemMatrix,
335 args("row_blocksize", "row_functionspace", "column_blocksize", "column_functionspace", "type"),
336 "creates a SystemMatrixAdapter stiffness matrix and initializes it with zeros\n\n"
337 ":param row_blocksize:\n:type row_blocksize: ``int``\n"
338 ":param row_functionspace:\n:type row_functionspace: `FunctionSpace`\n"
339 ":param column_blocksize:\n:type column_blocksize: ``int``\n"
340 ":param column_functionspace:\n:type column_functionspace: `FunctionSpace`\n"
341 ":param type:\n:type type: ``int``"
342 )
343 .def("newTransportProblem",&ripley::RipleyDomain::newTransportProblem,
344 args("theta", "blocksize", "functionspace", "type"),
345 "creates a TransportProblemAdapter\n\n"
346 ":param theta:\n:type theta: ``float``\n"
347 ":param blocksize:\n:type blocksize: ``int``\n"
348 ":param functionspace:\n:type functionspace: `FunctionSpace`\n"
349 ":param type:\n:type type: ``int``"
350 )
351 .def("getSystemMatrixTypeId",&ripley::RipleyDomain::getSystemMatrixTypeId,
352 args("solver", "preconditioner", "package", "symmetry"),
353 ":return: the identifier of the matrix type to be used for the global stiffness matrix when a particular solver, package, preconditioner, and symmetric matrix is used.\n"
354 ":rtype: ``int``\n"
355 ":param solver:\n:type solver: ``int``\n"
356 ":param preconditioner:\n:type preconditioner: ``int``\n"
357 ":param package:\n:type package: ``int``\n"
358 ":param symmetry:\n:type symmetry: ``int``"
359 )
360 .def("getTransportTypeId",&ripley::RipleyDomain::getTransportTypeId,
361 args("solver", "preconditioner", "package", "symmetry"),
362 ":return: the identifier of the transport problem type to be used when a particular solver, preconditioner, package and symmetric matrix is used.\n"
363 ":rtype: ``int``\n"
364 ":param solver:\n:type solver: ``int``\n"
365 ":param preconditioner:\n:type preconditioner: ``int``\n"
366 ":param package:\n:type package: ``int``\n"
367 ":param symmetry:\n:type symmetry: ``int``"
368 )
369 .def("getX",&ripley::RipleyDomain::getX, ":return: locations in the FEM nodes\n\n"
370 ":rtype: `Data`")
371 .def("getNormal",&ripley::RipleyDomain::getNormal,
372 ":return: boundary normals at the quadrature point on the face elements\n"
373 ":rtype: `Data`")
374 .def("getSize",&ripley::RipleyDomain::getSize,":return: the element size\n"
375 ":rtype: `Data`")
376 .def("setTagMap",&ripley::RipleyDomain::setTagMap,args("name","tag"),
377 "Give a tag number a name.\n\n:param name: Name for the tag\n:type name: ``string``\n"
378 ":param tag: numeric id\n:type tag: ``int``\n:note: Tag names must be unique within a domain")
379 .def("getTag",&ripley::RipleyDomain::getTag,args("name"),":return: tag id for "
380 "``name``\n:rtype: ``string``")
381 .def("isValidTagName",&ripley::RipleyDomain::isValidTagName,args("name"),
382 ":return: True if ``name`` corresponds to a tag, otherwise False\n:rtype: ``bool``")
383 .def("showTagNames",&ripley::RipleyDomain::showTagNames,":return: A space separated list of tag names\n:rtype: ``string``")
384 .def("getMPISize",&ripley::RipleyDomain::getMPISize,":return: the number of processes used for this `Domain`\n:rtype: ``int``")
385 .def("getMPIRank",&ripley::RipleyDomain::getMPIRank,":return: the rank of this process\n:rtype: ``int``")
386 .def("MPIBarrier",&ripley::RipleyDomain::MPIBarrier,"Wait until all processes have reached this point")
387 .def("onMasterProcessor",&ripley::RipleyDomain::onMasterProcessor,":return: True if this code is executing on the master process\n:rtype: `bool`");
388
389 class_<ripley::Brick, bases<ripley::RipleyDomain> >("RipleyBrick", "", no_init);
390 class_<ripley::Rectangle, bases<ripley::RipleyDomain> >("RipleyRectangle", "", no_init)
391 .def("randomFill", &ripley::Rectangle::randomFill,":return: random data\n:rtype: `Data`\n:param seed: pass zero to use system generated seed\n:type seed: `int`\n"
392 ":param details: more info about the type of randomness\nCurrently, the only acceptable value for this tuple is ('gaussian', r, s) where r is the radius of the"
393 "guassian blur and s is the sigma parameter."
394 );
395 }
396

  ViewVC Help
Powered by ViewVC 1.1.26