/[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

branches/ripleygmg_from_3668/ripley/src/ripleycpp.cpp revision 3791 by caltinay, Wed Feb 1 05:10:22 2012 UTC trunk/ripley/src/ripleycpp.cpp revision 4357 by caltinay, Thu Apr 11 06:07:14 2013 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  * Earth Systems Science Computational Center (ESSCC)  * http://www.uq.edu.au
 * http://www.uq.edu.au/esscc  
6  *  *
7  * Primary Business: Queensland, Australia  * Primary Business: Queensland, Australia
8  * Licensed under the Open Software License version 3.0  * Licensed under the Open Software License version 3.0
9  * http://www.opensource.org/licenses/osl-3.0.php  * 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>  #include <ripley/Brick.h>
17  #include <ripley/Rectangle.h>  #include <ripley/Rectangle.h>
# Line 25  using namespace boost::python; Line 27  using namespace boost::python;
27    
28  namespace ripley {  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=0.)
33    {
34        int dim=fs.getDim();
35        std::vector<int> first(dim), numValues(dim), multiplier(dim), shape;
36    
37        if (extract<tuple>(pyFirst).check() || extract<list>(pyFirst).check()) {
38            if (len(pyFirst)==dim) {
39                for (int i=0; i<dim; i++) {
40                    first[i]=extract<int>(pyFirst[i]);
41                }
42            } else
43                throw RipleyException("Argument 'first' has wrong length");
44        } else
45            throw RipleyException("Argument 'first' must be a tuple or list");
46    
47        if (extract<tuple>(pyNum).check() || extract<list>(pyNum).check()) {
48            if (len(pyNum)==dim) {
49                for (int i=0; i<dim; i++) {
50                    numValues[i]=extract<int>(pyNum[i]);
51                }
52            } else
53                throw RipleyException("Argument 'numValues' has wrong length");
54        } else
55            throw RipleyException("Argument 'numValues' must be a tuple or list");
56    
57        if (extract<tuple>(pyShape).check() || extract<list>(pyShape).check()) {
58            for (int i=0; i<len(pyShape); i++) {
59                shape.push_back(extract<int>(pyShape[i]));
60            }
61        } else
62            throw RipleyException("Argument 'shape' must be a tuple or list");
63    
64        if (extract<tuple>(pyMultiplier).check() || extract<list>(pyMultiplier).check()) {
65            if (len(pyMultiplier)==dim) {
66                for (int i=0; i<dim; i++) {
67                    multiplier[i]=extract<int>(pyMultiplier[i]);
68                }
69            } else
70                throw RipleyException("Argument 'multiplier' has wrong length");
71        } else
72            throw RipleyException("Argument 'multiplier' must be a tuple or list");
73    
74        const RipleyDomain* dom=dynamic_cast<const RipleyDomain*>(fs.getDomain().get());
75        if (!dom)
76            throw RipleyException("Function space must be on a ripley domain");
77    
78    
79        escript::Data res(fill, shape, fs, true);
80    
81        dom->readBinaryGrid(res, filename, first, numValues, multiplier);
82        return res;
83    }
84    
85    escript::Data readNcGrid(std::string filename, std::string varname,
86            escript::FunctionSpace fs, const object& pyFirst, const object& pyNum,
87            const object& pyMultiplier, const object& pyShape, double fill=0.)
88    {
89        int dim=fs.getDim();
90        std::vector<int> first(dim), numValues(dim), multiplier(dim), shape;
91    
92        if (extract<tuple>(pyFirst).check() || extract<list>(pyFirst).check()) {
93            if (len(pyFirst)==dim) {
94                for (int i=0; i<dim; i++) {
95                    first[i]=extract<int>(pyFirst[i]);
96                }
97            } else
98                throw RipleyException("Argument 'first' has wrong length");
99        } else
100            throw RipleyException("Argument 'first' must be a tuple or list");
101    
102        if (extract<tuple>(pyNum).check() || extract<list>(pyNum).check()) {
103            if (len(pyNum)==dim) {
104                for (int i=0; i<dim; i++) {
105                    numValues[i]=extract<int>(pyNum[i]);
106                }
107            } else
108                throw RipleyException("Argument 'numValues' has wrong length");
109        } else
110            throw RipleyException("Argument 'numValues' must be a tuple or list");
111    
112        if (extract<tuple>(pyMultiplier).check() || extract<list>(pyMultiplier).check()) {
113            if (len(pyMultiplier)==dim) {
114                for (int i=0; i<dim; i++) {
115                    multiplier[i]=extract<int>(pyMultiplier[i]);
116                }
117            } else
118                throw RipleyException("Argument 'multiplier' has wrong length");
119        } else
120            throw RipleyException("Argument 'multiplier' must be a tuple or list");
121    
122        if (extract<tuple>(pyShape).check() || extract<list>(pyShape).check()) {
123            for (int i=0; i<len(pyShape); i++) {
124                shape.push_back(extract<int>(pyShape[i]));
125            }
126        } else
127            throw RipleyException("Argument 'shape' must be a tuple or list");
128    
129        const RipleyDomain* dom=dynamic_cast<const RipleyDomain*>(fs.getDomain().get());
130        if (!dom)
131            throw RipleyException("Function space must be on a ripley domain");
132    
133    
134        escript::Data res(fill, shape, fs, true);
135    
136        dom->readNcGrid(res, filename, varname, first, numValues, multiplier);
137        return res;
138    }
139    
140  // These wrappers are required to make the shared pointers work through the  // These wrappers are required to make the shared pointers work through the
141  // Python wrapper  // Python wrapper
142    
143  escript::Domain_ptr _brick(int n0, int n1, int n2, const object& l0,  // The double for n? is just to keep python happy when people need to deal with
144    // truediv
145    escript::Domain_ptr _brick(double _n0, double _n1, double _n2, const object& l0,
146                   const object& l1, const object& l2, int d0, int d1, int d2)                   const object& l1, const object& l2, int d0, int d1, int d2)
147  {  {
148        int n0=static_cast<int>(_n0), n1=static_cast<int>(_n1), n2=static_cast<int>(_n2);
149      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.;
150      if (extract<tuple>(l0).check()) {      if (extract<tuple>(l0).check()) {
151          tuple x=extract<tuple>(l0);          tuple x=extract<tuple>(l0);
# Line 72  escript::Domain_ptr _brick(int n0, int n Line 187  escript::Domain_ptr _brick(int n0, int n
187  }  }
188    
189  const int _q[]={0x61686969,0x746c4144,0x79616e43};  const int _q[]={0x61686969,0x746c4144,0x79616e43};
190  escript::Domain_ptr _rectangle(int n0, int n1, const object& l0,  escript::Domain_ptr _rectangle(double _n0, double _n1, const object& l0,
191                                 const object& l1, int d0, int d1)                                 const object& l1, int d0, int d1)
192  {  {
193        int n0=static_cast<int>(_n0), n1=static_cast<int>(_n1);
194      double x0=0., x1=1., y0=0., y1=1.;      double x0=0., x1=1., y0=0., y1=1.;
195      if (extract<tuple>(l0).check()) {      if (extract<tuple>(l0).check()) {
196          tuple x=extract<tuple>(l0);          tuple x=extract<tuple>(l0);
# Line 104  escript::Domain_ptr _rectangle(int n0, i Line 220  escript::Domain_ptr _rectangle(int n0, i
220  }  }
221  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];}
222    
223  }  } // end of namespace ripley
224    
 /**  
     \page ripley Ripley  
     ripleycpp is the python module name that contains the interfaces  
     to the C++ wrapper to ripley.  
 */  
225    
226  BOOST_PYTHON_MODULE(ripleycpp)  BOOST_PYTHON_MODULE(ripleycpp)
227  {  {
# Line 122  BOOST_PYTHON_MODULE(ripleycpp) Line 233  BOOST_PYTHON_MODULE(ripleycpp)
233    
234      register_exception_translator<ripley::RipleyException>(&(esysUtils::esysExceptionTranslator));      register_exception_translator<ripley::RipleyException>(&(esysUtils::esysExceptionTranslator));
235    
236      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),      scope().attr("__doc__") = "To use this module, please import esys.ripley";
237        scope().attr("BYTEORDER_NATIVE") = (int)ripley::BYTEORDER_NATIVE;
238        scope().attr("BYTEORDER_LITTLE_ENDIAN") = (int)ripley::BYTEORDER_LITTLE_ENDIAN;
239        scope().attr("BYTEORDER_BIG_ENDIAN") = (int)ripley::BYTEORDER_BIG_ENDIAN;
240        scope().attr("DATATYPE_INT32") = (int)ripley::DATATYPE_INT32;
241        scope().attr("DATATYPE_FLOAT32") = (int)ripley::DATATYPE_FLOAT32;
242        scope().attr("DATATYPE_FLOAT64") = (int)ripley::DATATYPE_FLOAT64;
243    
244        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),
245  "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"
246  ":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"
247  ":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 134  BOOST_PYTHON_MODULE(ripleycpp) Line 253  BOOST_PYTHON_MODULE(ripleycpp)
253  ":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"
254  ":param d2: number of subdivisions in direction 2\n:type d2: ``int``");  ":param d2: number of subdivisions in direction 2\n:type d2: ``int``");
255    
256      def("Rectangle", ripley::_rectangle, (arg("n0"),arg("n1"),arg("l0")=1.0,arg("l1")=1.0,arg("d0")=1,arg("d1")=1),      def("Rectangle", ripley::_rectangle, (arg("n0"),arg("n1"),arg("l0")=1.0,arg("l1")=1.0,arg("d0")=-1,arg("d1")=-1),
257  "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"
258  ":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"
259  ":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 142  BOOST_PYTHON_MODULE(ripleycpp) Line 261  BOOST_PYTHON_MODULE(ripleycpp)
261  ":param l1: length of side 1 or coordinate range of side 1\n:type l1: ``float`` or ``tuple``\n"  ":param l1: length of side 1 or coordinate range of side 1\n:type l1: ``float`` or ``tuple``\n"
262  ":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"
263  ":param d1: number of subdivisions in direction 1\n:type d1: ``int``");  ":param d1: number of subdivisions in direction 1\n:type d1: ``int``");
264      def("_theculprit_", ripley::_who),      def("_theculprit_", ripley::_who);
265      def("LoadMesh", ripley::RipleyDomain::loadMesh, (arg("filename")),  
266             "Loads a ripley domain from a dump file" ":rtype: `Domain`");      def("_readBinaryGrid", &ripley::readBinaryGrid, (arg("filename"), arg("functionspace"), arg("first"), arg("numValues"), arg("multiplier"), arg("shape"), arg("fill")=0.));
   
     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");  
267    
268      class_<ripley::RipleyDomain, bases<escript::AbstractContinuousDomain> >      def("_readNcGrid", &ripley::readNcGrid, (arg("filename"), arg("varname"), arg("functionspace"), arg("first"), arg("numValues"), arg("multiplier"), arg("shape"), arg("fill")=0.));
269    
270        class_<ripley::RipleyDomain, bases<escript::AbstractContinuousDomain>, boost::noncopyable >
271          ("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().")  
272          .def("print_mesh_info", &ripley::RipleyDomain::Print_Mesh_Info, (arg("full")=false),          .def("print_mesh_info", &ripley::RipleyDomain::Print_Mesh_Info, (arg("full")=false),
273                  "Prints out a summary about the mesh.\n"                  "Prints out a summary about the mesh.\n"
274                  ":param full: whether to output additional data\n:type full: ``bool``")                  ":param full: whether to output additional data\n:type full: ``bool``")
275            .def("writeBinaryGrid", &ripley::RipleyDomain::writeBinaryGrid)
276    
277          .def("dump", &ripley::RipleyDomain::dump, args("filename"),          .def("dump", &ripley::RipleyDomain::dump, args("filename"),
278                  "Dumps the mesh to a file with the given name.")                  "Dumps the mesh to a file with the given name.")
279            .def("getGridParameters", &ripley::RipleyDomain::getGridParameters,
280    "Returns the tuple (origin, spacing, elements) where the entries are tuples:\n"
281    "``origin``=the coordinates of the domain's global origin,\n"
282    "``spacing``=the element size (=node spacing) of the domain,\n"
283    "``elements``=the global number of elements in all dimensions\n\n"
284    ":rtype: ``tuple``")
285          .def("getDescription", &ripley::RipleyDomain::getDescription,          .def("getDescription", &ripley::RipleyDomain::getDescription,
286  ":return: a description for this domain\n:rtype: ``string``")  ":return: a description for this domain\n:rtype: ``string``")
287          .def("getDim", &ripley::RipleyDomain::getDim, ":rtype: ``int``")          .def("getDim", &ripley::RipleyDomain::getDim, ":rtype: ``int``")

Legend:
Removed from v.3791  
changed lines
  Added in v.4357

  ViewVC Help
Powered by ViewVC 1.1.26