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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3971 - (show annotations)
Wed Sep 19 02:55:35 2012 UTC (6 years, 11 months ago) by caltinay
File size: 14813 byte(s)
Implemented binary grid reader in ripley which is now used in ER Mapper source.
Interface/details likely to change but need to get this running now.

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

  ViewVC Help
Powered by ViewVC 1.1.26