/[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 3991 by caltinay, Tue Sep 25 23:52:22 2012 UTC revision 4616 by caltinay, Tue Jan 14 22:57:47 2014 UTC
# Line 1  Line 1 
1    
2  /*****************************************************************************  /*****************************************************************************
3  *  *
4  * Copyright (c) 2003-2012 by University of Queensland  * Copyright (c) 2003-2013 by University of Queensland
5  * http://www.uq.edu.au  * http://www.uq.edu.au
6  *  *
7  * Primary Business: Queensland, Australia  * Primary Business: Queensland, Australia
# Line 27  using namespace boost::python; Line 27  using namespace boost::python;
27    
28  namespace ripley {  namespace ripley {
29    
30    template<typename T>
31    std::vector<T> extractPyArray(const object& obj, const std::string& name,
32                                  int expectedLength=0)
33    {
34        std::vector<T> result;
35        if (extract<tuple>(obj).check() || extract<list>(obj).check()) {
36            if (expectedLength==0 || len(obj)==expectedLength) {
37                for (int i=0; i<len(obj); i++) {
38                    result.push_back(extract<T>(obj[i]));
39                }
40            } else {
41                std::stringstream ssError;
42                ssError << "argument '" << name << "' has wrong length";
43                std::string error(ssError.str());
44                throw RipleyException(error.c_str());
45            }
46        } else {
47            std::stringstream ssError;
48            ssError << "argument '" << name << "' must be a tuple or list";
49            std::string error(ssError.str());
50            throw RipleyException(error.c_str());
51        }
52    
53        return result;
54    }
55    
56  escript::Data readBinaryGrid(std::string filename, escript::FunctionSpace fs,  escript::Data readBinaryGrid(std::string filename, escript::FunctionSpace fs,
57          const object& pyFirst, const object& pyNum, const object& pyShape,          const object& pyShape, double fill, int byteOrder, int dataType,
58          double fill=0.)          const object& pyFirst, const object& pyNum, const object& pyMultiplier,
59            const object& pyReverse)
60  {  {
61      int dim=fs.getDim();      int dim=fs.getDim();
62      std::vector<int> first(dim), numValues(dim), shape;      GridParameters params;
63    
64      if (extract<tuple>(pyFirst).check() || extract<list>(pyFirst).check()) {      params.first = extractPyArray<int>(pyFirst, "first", dim);
65          if (len(pyFirst)==dim) {      params.numValues = extractPyArray<int>(pyNum, "numValues", dim);
66              for (int i=0; i<dim; i++) {      params.multiplier = extractPyArray<int>(pyMultiplier, "multiplier", dim);
67                  first[i]=extract<int>(pyFirst[i]);      params.reverse = extractPyArray<int>(pyReverse, "reverse", dim);
68              }      params.byteOrder = byteOrder;
69          } else      params.dataType = dataType;
70              throw RipleyException("Argument 'first' has wrong length");      std::vector<int> shape(extractPyArray<int>(pyShape, "shape"));
     } else  
         throw RipleyException("Argument 'first' must be a tuple or list");  
71    
72      if (extract<tuple>(pyNum).check() || extract<list>(pyNum).check()) {      const RipleyDomain* dom=dynamic_cast<const RipleyDomain*>(fs.getDomain().get());
73          if (len(pyNum)==dim) {      if (!dom)
74              for (int i=0; i<dim; i++) {          throw RipleyException("Function space must be on a ripley domain");
                 numValues[i]=extract<int>(pyNum[i]);  
             }  
         } else  
             throw RipleyException("Argument 'numValues' has wrong length");  
     } else  
         throw RipleyException("Argument 'numValues' must be a tuple or list");  
75    
76      if (extract<tuple>(pyShape).check() || extract<list>(pyShape).check()) {      escript::Data res(fill, shape, fs, true);
77          for (int i=0; i<len(pyShape); i++) {      dom->readBinaryGrid(res, filename, params);
78              shape.push_back(extract<int>(pyShape[i]));      return res;
79          }  }
80      } else  
81          throw RipleyException("Argument 'shape' must be a tuple or list");  escript::Data readNcGrid(std::string filename, std::string varname,
82            escript::FunctionSpace fs, const object& pyShape, double fill,
83            const object& pyFirst, const object& pyNum, const object& pyMultiplier,
84            const object& pyReverse)
85    {
86        int dim=fs.getDim();
87        GridParameters params;
88    
89        params.first = extractPyArray<int>(pyFirst, "first", dim);
90        params.numValues = extractPyArray<int>(pyNum, "numValues", dim);
91        params.multiplier = extractPyArray<int>(pyMultiplier, "multiplier", dim);
92        params.reverse = extractPyArray<int>(pyReverse, "reverse", dim);
93        std::vector<int> shape(extractPyArray<int>(pyShape, "shape"));
94    
95      const RipleyDomain* dom=dynamic_cast<const RipleyDomain*>(fs.getDomain().get());      const RipleyDomain* dom=dynamic_cast<const RipleyDomain*>(fs.getDomain().get());
96      if (!dom)      if (!dom)
97          throw RipleyException("Function space must be on a ripley domain");          throw RipleyException("Function space must be on a ripley domain");
98    
   
99      escript::Data res(fill, shape, fs, true);      escript::Data res(fill, shape, fs, true);
100        dom->readNcGrid(res, filename, varname, params);
     dom->readBinaryGrid(res, filename, first, numValues);  
101      return res;      return res;
102  }  }
103    
# Line 155  escript::Domain_ptr _rectangle(double _n Line 184  escript::Domain_ptr _rectangle(double _n
184  }  }
185  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];}
186    
187  } // end of namespace  } // end of namespace ripley
188    
189    
190  BOOST_PYTHON_MODULE(ripleycpp)  BOOST_PYTHON_MODULE(ripleycpp)
# Line 168  BOOST_PYTHON_MODULE(ripleycpp) Line 197  BOOST_PYTHON_MODULE(ripleycpp)
197    
198      register_exception_translator<ripley::RipleyException>(&(esysUtils::esysExceptionTranslator));      register_exception_translator<ripley::RipleyException>(&(esysUtils::esysExceptionTranslator));
199    
200        scope().attr("__doc__") = "To use this module, please import esys.ripley";
201        scope().attr("BYTEORDER_NATIVE") = (int)ripley::BYTEORDER_NATIVE;
202        scope().attr("BYTEORDER_LITTLE_ENDIAN") = (int)ripley::BYTEORDER_LITTLE_ENDIAN;
203        scope().attr("BYTEORDER_BIG_ENDIAN") = (int)ripley::BYTEORDER_BIG_ENDIAN;
204        scope().attr("DATATYPE_INT32") = (int)ripley::DATATYPE_INT32;
205        scope().attr("DATATYPE_FLOAT32") = (int)ripley::DATATYPE_FLOAT32;
206        scope().attr("DATATYPE_FLOAT64") = (int)ripley::DATATYPE_FLOAT64;
207    
208      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),      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),
209  "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"
210  ":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"
# Line 189  BOOST_PYTHON_MODULE(ripleycpp) Line 226  BOOST_PYTHON_MODULE(ripleycpp)
226  ":param d0: number of subdivisions in direction 0\n:type d0: ``int``\n"  ":param d0: number of subdivisions in direction 0\n:type d0: ``int``\n"
227  ":param d1: number of subdivisions in direction 1\n:type d1: ``int``");  ":param d1: number of subdivisions in direction 1\n:type d1: ``int``");
228      def("_theculprit_", ripley::_who);      def("_theculprit_", ripley::_who);
     def("LoadMesh", ripley::RipleyDomain::loadMesh, (arg("filename")),  
            "Loads a ripley domain from a dump file" ":rtype: `Domain`");  
   
     def("ReadMesh", ripley::RipleyDomain::readMesh, (arg("filename")),  
             "Reads a ripley domain from a file created by write().\n\n"  
             ":rtype: `RipleyDomain`\n:param filename:\n:type filename: ``string``\n");  
229    
230      def("_readBinaryGrid", &ripley::readBinaryGrid, (arg("filename"), arg("functionspace"), arg("first"), arg("numValues"), arg("shape"), arg("fill")=0.));      def("_readBinaryGrid", &ripley::readBinaryGrid, (arg("filename"),
231                    arg("functionspace"), arg("shape"), arg("fill")=0.,
232                    arg("byteOrder"), arg("dataType"), arg("first"),
233                    arg("numValues"), arg("multiplier"), arg("reverse")),
234    "Reads a binary Grid");
235    
236        def("_readNcGrid", &ripley::readNcGrid, (arg("filename"), arg("varname"),
237                    arg("functionspace"), arg("shape"), arg("fill"), arg("first"),
238                    arg("numValues"), arg("multiplier"), arg("reverse")),
239    "Reads a grid from a netCDF file");
240    
241      class_<ripley::RipleyDomain, bases<escript::AbstractContinuousDomain> >      class_<ripley::RipleyDomain, bases<escript::AbstractContinuousDomain>, boost::noncopyable >
242          ("RipleyDomain", "", no_init)          ("RipleyDomain", "", no_init)
         .def("write", &ripley::RipleyDomain::write, args("filename"),  
                 "Writes the current mesh to a file with the given name. It can subsequently be recovered using ReadMesh().")  
243          .def("print_mesh_info", &ripley::RipleyDomain::Print_Mesh_Info, (arg("full")=false),          .def("print_mesh_info", &ripley::RipleyDomain::Print_Mesh_Info, (arg("full")=false),
244                  "Prints out a summary about the mesh.\n"                  "Prints out a summary about the mesh.\n"
245                  ":param full: whether to output additional data\n:type full: ``bool``")                  ":param full: whether to output additional data\n:type full: ``bool``")
246            .def("writeBinaryGrid", &ripley::RipleyDomain::writeBinaryGrid)
247    
248          .def("dump", &ripley::RipleyDomain::dump, args("filename"),          .def("dump", &ripley::RipleyDomain::dump, args("filename"),
249                  "Dumps the mesh to a file with the given name.")                  "Dumps the mesh to a file with the given name.")
250            .def("getGridParameters", &ripley::RipleyDomain::getGridParameters,
251    "Returns the tuple (origin, spacing, elements) where the entries are tuples:\n"
252    "``origin``=the coordinates of the domain's global origin,\n"
253    "``spacing``=the element size (=node spacing) of the domain,\n"
254    "``elements``=the global number of elements in all dimensions\n\n"
255    ":rtype: ``tuple``")
256          .def("getDescription", &ripley::RipleyDomain::getDescription,          .def("getDescription", &ripley::RipleyDomain::getDescription,
257  ":return: a description for this domain\n:rtype: ``string``")  ":return: a description for this domain\n:rtype: ``string``")
258          .def("getDim", &ripley::RipleyDomain::getDim, ":rtype: ``int``")          .def("getDim", &ripley::RipleyDomain::getDim, ":rtype: ``int``")
# Line 310  args("solver", "preconditioner", "packag Line 356  args("solver", "preconditioner", "packag
356          .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`");
357    
358      class_<ripley::Brick, bases<ripley::RipleyDomain> >("RipleyBrick", "", no_init);      class_<ripley::Brick, bases<ripley::RipleyDomain> >("RipleyBrick", "", no_init);
359      class_<ripley::Rectangle, bases<ripley::RipleyDomain> >("RipleyRectangle", "", no_init);      class_<ripley::Rectangle, bases<ripley::RipleyDomain> >("RipleyRectangle", "", no_init)
360        .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"
361    ":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"
362    "guassian blur and s is the sigma parameter."
363        );
364  }  }
365    

Legend:
Removed from v.3991  
changed lines
  Added in v.4616

  ViewVC Help
Powered by ViewVC 1.1.26