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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4340 - (hide annotations)
Fri Mar 22 04:38:36 2013 UTC (6 years, 1 month ago) by caltinay
File size: 18492 byte(s)
added ripley method that returns grid parameters as python tuple.

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

  ViewVC Help
Powered by ViewVC 1.1.26