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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 4738 by sshaw, Wed Mar 12 01:32:36 2014 UTC revision 4934 by jfenwick, Tue May 13 00:28:11 2014 UTC
# Line 18  Line 18 
18  #include <ripley/Rectangle.h>  #include <ripley/Rectangle.h>
19  #include <esysUtils/esysExceptionTranslator.h>  #include <esysUtils/esysExceptionTranslator.h>
20    
21  #include <boost/python.hpp>  #include <boost/python.hpp>
22  #include <boost/python/module.hpp>  #include <boost/python/module.hpp>
23  #include <boost/python/def.hpp>  #include <boost/python/def.hpp>
24  #include <boost/python/detail/defaults_gen.hpp>  #include <boost/python/detail/defaults_gen.hpp>
25  #include <boost/version.hpp>  #include <boost/version.hpp>
26    
27    #include "escript/SubWorld.h"
28    
29  using namespace boost::python;  using namespace boost::python;
30    
31  namespace ripley {  namespace ripley {
# Line 136  escript::Data readNcGrid(std::string fil Line 138  escript::Data readNcGrid(std::string fil
138  // truediv  // truediv
139  escript::Domain_ptr _brick(double _n0, double _n1, double _n2, const object& l0,  escript::Domain_ptr _brick(double _n0, double _n1, double _n2, const object& l0,
140                   const object& l1, const object& l2, int d0, int d1, int d2,                   const object& l1, const object& l2, int d0, int d1, int d2,
141                   const object& objpoints, const object& objtags)                   const object& objpoints, const object& objtags, escript::SubWorld_ptr world)
142  {  {
143      int n0=static_cast<int>(_n0), n1=static_cast<int>(_n1), n2=static_cast<int>(_n2);      int n0=static_cast<int>(_n0), n1=static_cast<int>(_n1), n2=static_cast<int>(_n2);
144      double x0=0., x1=1., y0=0., y1=1., z0=0., z1=1.;      double x0=0., x1=1., y0=0., y1=1., z0=0., z1=1.;
# Line 175  escript::Domain_ptr _brick(double _n0, d Line 177  escript::Domain_ptr _brick(double _n0, d
177          z1=extract<double>(l2);          z1=extract<double>(l2);
178      } else      } else
179          throw RipleyException("Argument l2 must be a float or 2-tuple");          throw RipleyException("Argument l2 must be a float or 2-tuple");
   
180      boost::python::list pypoints=extract<boost::python::list>(objpoints);      boost::python::list pypoints=extract<boost::python::list>(objpoints);
181      boost::python::list pytags=extract<boost::python::list>(objtags);      boost::python::list pytags=extract<boost::python::list>(objtags);
182      int numpts=extract<int>(pypoints.attr("__len__")());      int numpts=extract<int>(pypoints.attr("__len__")());
# Line 184  escript::Domain_ptr _brick(double _n0, d Line 185  escript::Domain_ptr _brick(double _n0, d
185      std::vector<int> tags;      std::vector<int> tags;
186      tags.resize(numtags, -1);      tags.resize(numtags, -1);
187      for (int i=0;i<numpts;++i) {      for (int i=0;i<numpts;++i) {
188          boost::python::object temp=pypoints[i];          tuple temp = extract<tuple>(pypoints[i]);
189          int l=extract<int>(temp.attr("__len__")());          int l=extract<int>(temp.attr("__len__")());
190            if (l != 3)
191                throw RipleyException("Number of coordinates for each dirac point must match dimensions.");
192          for (int k=0;k<l;++k) {          for (int k=0;k<l;++k) {
193              points.push_back(extract<double>(temp[k]));              points.push_back(extract<double>(temp[k]));
194          }          }
# Line 216  escript::Domain_ptr _brick(double _n0, d Line 219  escript::Domain_ptr _brick(double _n0, d
219              throw RipleyException("Error - Unable to extract tag value.");              throw RipleyException("Error - Unable to extract tag value.");
220          }          }
221      }      }
222        if (numtags != numpts)
223            throw RipleyException("Number of tags does not match number of points.");
224      return escript::Domain_ptr(new Brick(n0,n1,n2, x0,y0,z0, x1,y1,z1, d0,d1,d2,      return escript::Domain_ptr(new Brick(n0,n1,n2, x0,y0,z0, x1,y1,z1, d0,d1,d2,
225                                              points, tags, tagstonames));                                              points, tags, tagstonames, world));
226  }  }
227    
228  const int _q[]={0x61686969,0x746c4144,0x79616e43};  //const int _q[]={0x61686969,0x746c4144,0x79616e43};
229    const int _q[]={0x62207363, 0x6574735F, 0x2020214e};
230  escript::Domain_ptr _rectangle(double _n0, double _n1, const object& l0,  escript::Domain_ptr _rectangle(double _n0, double _n1, const object& l0,
231                                 const object& l1, int d0, int d1,                                 const object& l1, int d0, int d1,
232                                 const object& objpoints, const object& objtags)                                 const object& objpoints, const object& objtags,
233                      escript::SubWorld_ptr world
234                      )
235  {  {
236      int n0=static_cast<int>(_n0), n1=static_cast<int>(_n1);      int n0=static_cast<int>(_n0), n1=static_cast<int>(_n1);
237      double x0=0., x1=1., y0=0., y1=1.;      double x0=0., x1=1., y0=0., y1=1.;
# Line 250  escript::Domain_ptr _rectangle(double _n Line 258  escript::Domain_ptr _rectangle(double _n
258          y1=extract<double>(l1);          y1=extract<double>(l1);
259      } else      } else
260          throw RipleyException("Argument l1 must be a float or 2-tuple");          throw RipleyException("Argument l1 must be a float or 2-tuple");
           
261      boost::python::list pypoints=extract<boost::python::list>(objpoints);      boost::python::list pypoints=extract<boost::python::list>(objpoints);
262      boost::python::list pytags=extract<boost::python::list>(objtags);      boost::python::list pytags=extract<boost::python::list>(objtags);
263      int numpts=extract<int>(pypoints.attr("__len__")());      int numpts=extract<int>(pypoints.attr("__len__")());
# Line 259  escript::Domain_ptr _rectangle(double _n Line 266  escript::Domain_ptr _rectangle(double _n
266      std::vector<int> tags;      std::vector<int> tags;
267      tags.resize(numtags, -1);      tags.resize(numtags, -1);
268      for (int i=0;i<numpts;++i) {      for (int i=0;i<numpts;++i) {
269          boost::python::object temp=pypoints[i];          tuple temp = extract<tuple>(pypoints[i]);
270          int l=extract<int>(temp.attr("__len__")());          int l=extract<int>(temp.attr("__len__")());
271            if (l != 2)
272                throw RipleyException("Number of coordinates for each dirac point must match dimensions.");
273          for (int k=0;k<l;++k) {          for (int k=0;k<l;++k) {
274              points.push_back(extract<double>(temp[k]));              points.push_back(extract<double>(temp[k]));
275          }          }
# Line 291  escript::Domain_ptr _rectangle(double _n Line 300  escript::Domain_ptr _rectangle(double _n
300              throw RipleyException("Error - Unable to extract tag value.");              throw RipleyException("Error - Unable to extract tag value.");
301          }          }
302      }      }
303        if (numtags != numpts)
304            throw RipleyException("Number of tags does not match number of points.");
305      return escript::Domain_ptr(new Rectangle(n0,n1, x0,y0, x1,y1, d0,d1,      return escript::Domain_ptr(new Rectangle(n0,n1, x0,y0, x1,y1, d0,d1,
306                                               points, tags, tagstonames));                                               points, tags, tagstonames, world));
307  }  }
308  std::string _who(){int a[]={_q[0]^42,_q[1]^42,_q[2]^42,0};return (char*)&a[0];}  std::string _who(){int a[]={_q[0]^42,_q[1]^42,_q[2]^42,0};return (char*)&a[0];}
309    
# Line 307  BOOST_PYTHON_MODULE(ripleycpp) Line 318  BOOST_PYTHON_MODULE(ripleycpp)
318      docstring_options docopt(true, true, false);      docstring_options docopt(true, true, false);
319  #endif  #endif
320    
321      register_exception_translator<ripley::RipleyException>(&(esysUtils::esysExceptionTranslator));      register_exception_translator<ripley::RipleyException>(&(esysUtils::RuntimeErrorTranslator));
322    
323      scope().attr("__doc__") = "To use this module, please import esys.ripley";      scope().attr("__doc__") = "To use this module, please import esys.ripley";
324      scope().attr("BYTEORDER_NATIVE") = (int)ripley::BYTEORDER_NATIVE;      scope().attr("BYTEORDER_NATIVE") = (int)ripley::BYTEORDER_NATIVE;
# Line 317  BOOST_PYTHON_MODULE(ripleycpp) Line 328  BOOST_PYTHON_MODULE(ripleycpp)
328      scope().attr("DATATYPE_FLOAT32") = (int)ripley::DATATYPE_FLOAT32;      scope().attr("DATATYPE_FLOAT32") = (int)ripley::DATATYPE_FLOAT32;
329      scope().attr("DATATYPE_FLOAT64") = (int)ripley::DATATYPE_FLOAT64;      scope().attr("DATATYPE_FLOAT64") = (int)ripley::DATATYPE_FLOAT64;
330    
331      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,arg("diracPoints")=list(),arg("diracTags")=list()),      def("Brick", ripley::_brick, (arg("n0"),arg("n1"),arg("n2"),arg("l0")=1.0,arg("l1")=1.0,arg("l2")=1.0,
332            arg("d0")=-1,arg("d1")=-1,arg("d2")=-1,arg("diracPoints")=list(),arg("diracTags")=list(), arg("escriptworld")=escript::SubWorld_ptr()),
333  "Creates a hexagonal mesh with n0 x n1 x n2 elements over the brick [0,l0] x [0,l1] x [0,l2].\n\n"  "Creates a hexagonal mesh with n0 x n1 x n2 elements over the brick [0,l0] x [0,l1] x [0,l2].\n\n"
334  ":param n0: number of elements in direction 0\n:type n0: ``int``\n"  ":param n0: number of elements in direction 0\n:type n0: ``int``\n"
335  ":param n1: number of elements in direction 1\n:type n1: ``int``\n"  ":param n1: number of elements in direction 1\n:type n1: ``int``\n"
# Line 329  BOOST_PYTHON_MODULE(ripleycpp) Line 341  BOOST_PYTHON_MODULE(ripleycpp)
341  ":param d1: number of subdivisions in direction 1\n:type d1: ``int``\n"  ":param d1: number of subdivisions in direction 1\n:type d1: ``int``\n"
342  ":param d2: number of subdivisions in direction 2\n:type d2: ``int``");  ":param d2: number of subdivisions in direction 2\n:type d2: ``int``");
343    
344      def("Rectangle", ripley::_rectangle, (arg("n0"),arg("n1"),arg("l0")=1.0,arg("l1")=1.0,arg("d0")=-1,arg("d1")=-1,arg("diracPoints")=list(),arg("diracTags")=list()),      def("Rectangle", ripley::_rectangle, (arg("n0"),arg("n1"),arg("l0")=1.0,arg("l1")=1.0,arg("d0")=-1,arg("d1")=-1,arg("diracPoints")=list(),arg("diracTags")=list(), arg("escriptworld")=escript::SubWorld_ptr()),
345  "Creates a rectangular mesh with n0 x n1 elements over the rectangle [0,l0] x [0,l1].\n\n"  "Creates a rectangular mesh with n0 x n1 elements over the rectangle [0,l0] x [0,l1].\n\n"
346  ":param n0: number of elements in direction 0\n:type n0: ``int``\n"  ":param n0: number of elements in direction 0\n:type n0: ``int``\n"
347  ":param n1: number of elements in direction 1\n:type n1: ``int``\n"  ":param n1: number of elements in direction 1\n:type n1: ``int``\n"
# Line 491  args("solver", "preconditioner", "packag Line 503  args("solver", "preconditioner", "packag
503          .def("getMPIRank",&ripley::RipleyDomain::getMPIRank,":return: the rank of this process\n:rtype: ``int``")          .def("getMPIRank",&ripley::RipleyDomain::getMPIRank,":return: the rank of this process\n:rtype: ``int``")
504          .def("MPIBarrier",&ripley::RipleyDomain::MPIBarrier,"Wait until all processes have reached this point")          .def("MPIBarrier",&ripley::RipleyDomain::MPIBarrier,"Wait until all processes have reached this point")
505          .def("onMasterProcessor",&ripley::RipleyDomain::onMasterProcessor,":return: True if this code is executing on the master process\n:rtype: `bool`");          .def("onMasterProcessor",&ripley::RipleyDomain::onMasterProcessor,":return: True if this code is executing on the master process\n:rtype: `bool`");
506        /* These two class exports are necessary to ensure that the extra methods added by ripley make it to python.
507         * This change became necessary when the Brick and Rectangle constructors turned into factories instead of classes */
508      class_<ripley::Brick, bases<ripley::RipleyDomain> >("RipleyBrick", "", no_init);      class_<ripley::Brick, bases<ripley::RipleyDomain> >("RipleyBrick", "", no_init);
 /*      
     .def("randomFill", &ripley::Brick::randomFill,":return: random data\n:rtype: `Data`\n:param seed: pass zero to use system generated seed\n:type seed: `int`\n"  
 ":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"  
 "guassian blur and s is the sigma parameter."  
     );  
 */      
509      class_<ripley::Rectangle, bases<ripley::RipleyDomain> >("RipleyRectangle", "", no_init);      class_<ripley::Rectangle, bases<ripley::RipleyDomain> >("RipleyRectangle", "", no_init);
 /*      
     .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"  
 ":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"  
 "guassian blur and s is the sigma parameter."  
     );  
 */      
510  }  }
511    

Legend:
Removed from v.4738  
changed lines
  Added in v.4934

  ViewVC Help
Powered by ViewVC 1.1.26