/[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 4615 by caltinay, Mon Jan 13 05:05:33 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 28  using namespace boost::python; Line 28  using namespace boost::python;
28  namespace ripley {  namespace ripley {
29    
30  escript::Data readBinaryGrid(std::string filename, escript::FunctionSpace fs,  escript::Data readBinaryGrid(std::string filename, escript::FunctionSpace fs,
31          const object& pyFirst, const object& pyNum, const object& pyShape,          const object& pyFirst, const object& pyNum, const object& pyMultiplier,
32          double fill=0.)          const object& pyShape, double fill, int byteOrder, int dataType)
33  {  {
34      int dim=fs.getDim();      int dim=fs.getDim();
35      std::vector<int> first(dim), numValues(dim), shape;      GridParameters params;
36        std::vector<int> shape;
37    
38      if (extract<tuple>(pyFirst).check() || extract<list>(pyFirst).check()) {      if (extract<tuple>(pyFirst).check() || extract<list>(pyFirst).check()) {
39          if (len(pyFirst)==dim) {          if (len(pyFirst)==dim) {
40              for (int i=0; i<dim; i++) {              for (int i=0; i<dim; i++) {
41                  first[i]=extract<int>(pyFirst[i]);                  params.first.push_back(extract<int>(pyFirst[i]));
42              }              }
43          } else          } else
44              throw RipleyException("Argument 'first' has wrong length");              throw RipleyException("Argument 'first' has wrong length");
# Line 47  escript::Data readBinaryGrid(std::string Line 48  escript::Data readBinaryGrid(std::string
48      if (extract<tuple>(pyNum).check() || extract<list>(pyNum).check()) {      if (extract<tuple>(pyNum).check() || extract<list>(pyNum).check()) {
49          if (len(pyNum)==dim) {          if (len(pyNum)==dim) {
50              for (int i=0; i<dim; i++) {              for (int i=0; i<dim; i++) {
51                  numValues[i]=extract<int>(pyNum[i]);                  params.numValues.push_back(extract<int>(pyNum[i]));
52              }              }
53          } else          } else
54              throw RipleyException("Argument 'numValues' has wrong length");              throw RipleyException("Argument 'numValues' has wrong length");
# Line 61  escript::Data readBinaryGrid(std::string Line 62  escript::Data readBinaryGrid(std::string
62      } else      } else
63          throw RipleyException("Argument 'shape' must be a tuple or list");          throw RipleyException("Argument 'shape' must be a tuple or list");
64    
65        if (extract<tuple>(pyMultiplier).check() || extract<list>(pyMultiplier).check()) {
66            if (len(pyMultiplier)==dim) {
67                for (int i=0; i<dim; i++) {
68                    params.multiplier.push_back(extract<int>(pyMultiplier[i]));
69                }
70            } else
71                throw RipleyException("Argument 'multiplier' has wrong length");
72        } else
73            throw RipleyException("Argument 'multiplier' must be a tuple or list");
74    
75        const RipleyDomain* dom=dynamic_cast<const RipleyDomain*>(fs.getDomain().get());
76        if (!dom)
77            throw RipleyException("Function space must be on a ripley domain");
78    
79    
80        escript::Data res(fill, shape, fs, true);
81    
82        dom->readBinaryGrid(res, filename, params);
83        return res;
84    }
85    
86    escript::Data readNcGrid(std::string filename, std::string varname,
87            escript::FunctionSpace fs, const object& pyFirst, const object& pyNum,
88            const object& pyMultiplier, const object& pyShape, double fill=0.)
89    {
90        int dim=fs.getDim();
91        GridParameters params;
92        std::vector<int> shape;
93    
94        if (extract<tuple>(pyFirst).check() || extract<list>(pyFirst).check()) {
95            if (len(pyFirst)==dim) {
96                for (int i=0; i<dim; i++) {
97                    params.first.push_back(extract<int>(pyFirst[i]));
98                }
99            } else
100                throw RipleyException("Argument 'first' has wrong length");
101        } else
102            throw RipleyException("Argument 'first' must be a tuple or list");
103    
104        if (extract<tuple>(pyNum).check() || extract<list>(pyNum).check()) {
105            if (len(pyNum)==dim) {
106                for (int i=0; i<dim; i++) {
107                    params.numValues.push_back(extract<int>(pyNum[i]));
108                }
109            } else
110                throw RipleyException("Argument 'numValues' has wrong length");
111        } else
112            throw RipleyException("Argument 'numValues' must be a tuple or list");
113    
114        if (extract<tuple>(pyMultiplier).check() || extract<list>(pyMultiplier).check()) {
115            if (len(pyMultiplier)==dim) {
116                for (int i=0; i<dim; i++) {
117                    params.multiplier.push_back(extract<int>(pyMultiplier[i]));
118                }
119            } else
120                throw RipleyException("Argument 'multiplier' has wrong length");
121        } else
122            throw RipleyException("Argument 'multiplier' must be a tuple or list");
123    
124        if (extract<tuple>(pyShape).check() || extract<list>(pyShape).check()) {
125            for (int i=0; i<len(pyShape); i++) {
126                shape.push_back(extract<int>(pyShape[i]));
127            }
128        } else
129            throw RipleyException("Argument 'shape' must be a tuple or list");
130    
131      const RipleyDomain* dom=dynamic_cast<const RipleyDomain*>(fs.getDomain().get());      const RipleyDomain* dom=dynamic_cast<const RipleyDomain*>(fs.getDomain().get());
132      if (!dom)      if (!dom)
133          throw RipleyException("Function space must be on a ripley domain");          throw RipleyException("Function space must be on a ripley domain");
# Line 68  escript::Data readBinaryGrid(std::string Line 135  escript::Data readBinaryGrid(std::string
135    
136      escript::Data res(fill, shape, fs, true);      escript::Data res(fill, shape, fs, true);
137    
138      dom->readBinaryGrid(res, filename, first, numValues);      dom->readNcGrid(res, filename, varname, params);
139      return res;      return res;
140  }  }
141    
# Line 155  escript::Domain_ptr _rectangle(double _n Line 222  escript::Domain_ptr _rectangle(double _n
222  }  }
223  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];}
224    
225  } // end of namespace  } // end of namespace ripley
226    
227    
228  BOOST_PYTHON_MODULE(ripleycpp)  BOOST_PYTHON_MODULE(ripleycpp)
# Line 168  BOOST_PYTHON_MODULE(ripleycpp) Line 235  BOOST_PYTHON_MODULE(ripleycpp)
235    
236      register_exception_translator<ripley::RipleyException>(&(esysUtils::esysExceptionTranslator));      register_exception_translator<ripley::RipleyException>(&(esysUtils::esysExceptionTranslator));
237    
238        scope().attr("__doc__") = "To use this module, please import esys.ripley";
239        scope().attr("BYTEORDER_NATIVE") = (int)ripley::BYTEORDER_NATIVE;
240        scope().attr("BYTEORDER_LITTLE_ENDIAN") = (int)ripley::BYTEORDER_LITTLE_ENDIAN;
241        scope().attr("BYTEORDER_BIG_ENDIAN") = (int)ripley::BYTEORDER_BIG_ENDIAN;
242        scope().attr("DATATYPE_INT32") = (int)ripley::DATATYPE_INT32;
243        scope().attr("DATATYPE_FLOAT32") = (int)ripley::DATATYPE_FLOAT32;
244        scope().attr("DATATYPE_FLOAT64") = (int)ripley::DATATYPE_FLOAT64;
245    
246      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),
247  "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"
248  ":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 264  BOOST_PYTHON_MODULE(ripleycpp)
264  ":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"
265  ":param d1: number of subdivisions in direction 1\n:type d1: ``int``");  ":param d1: number of subdivisions in direction 1\n:type d1: ``int``");
266      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`");  
267    
268      def("ReadMesh", ripley::RipleyDomain::readMesh, (arg("filename")),      def("_readBinaryGrid", &ripley::readBinaryGrid, (arg("filename"), arg("functionspace"), arg("first"), arg("numValues"), arg("multiplier"), arg("shape"), arg("fill")=0., arg("byteOrder"), arg("dataType")));
             "Reads a ripley domain from a file created by write().\n\n"  
             ":rtype: `RipleyDomain`\n:param filename:\n:type filename: ``string``\n");  
269    
270      def("_readBinaryGrid", &ripley::readBinaryGrid, (arg("filename"), arg("functionspace"), arg("first"), arg("numValues"), arg("shape"), arg("fill")=0.));      def("_readNcGrid", &ripley::readNcGrid, (arg("filename"), arg("varname"), arg("functionspace"), arg("first"), arg("numValues"), arg("multiplier"), arg("shape"), arg("fill")=0.));
271    
272      class_<ripley::RipleyDomain, bases<escript::AbstractContinuousDomain> >      class_<ripley::RipleyDomain, bases<escript::AbstractContinuousDomain>, boost::noncopyable >
273          ("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().")  
274          .def("print_mesh_info", &ripley::RipleyDomain::Print_Mesh_Info, (arg("full")=false),          .def("print_mesh_info", &ripley::RipleyDomain::Print_Mesh_Info, (arg("full")=false),
275                  "Prints out a summary about the mesh.\n"                  "Prints out a summary about the mesh.\n"
276                  ":param full: whether to output additional data\n:type full: ``bool``")                  ":param full: whether to output additional data\n:type full: ``bool``")
277            .def("writeBinaryGrid", &ripley::RipleyDomain::writeBinaryGrid)
278    
279          .def("dump", &ripley::RipleyDomain::dump, args("filename"),          .def("dump", &ripley::RipleyDomain::dump, args("filename"),
280                  "Dumps the mesh to a file with the given name.")                  "Dumps the mesh to a file with the given name.")
281            .def("getGridParameters", &ripley::RipleyDomain::getGridParameters,
282    "Returns the tuple (origin, spacing, elements) where the entries are tuples:\n"
283    "``origin``=the coordinates of the domain's global origin,\n"
284    "``spacing``=the element size (=node spacing) of the domain,\n"
285    "``elements``=the global number of elements in all dimensions\n\n"
286    ":rtype: ``tuple``")
287          .def("getDescription", &ripley::RipleyDomain::getDescription,          .def("getDescription", &ripley::RipleyDomain::getDescription,
288  ":return: a description for this domain\n:rtype: ``string``")  ":return: a description for this domain\n:rtype: ``string``")
289          .def("getDim", &ripley::RipleyDomain::getDim, ":rtype: ``int``")          .def("getDim", &ripley::RipleyDomain::getDim, ":rtype: ``int``")
# Line 310  args("solver", "preconditioner", "packag Line 387  args("solver", "preconditioner", "packag
387          .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`");
388    
389      class_<ripley::Brick, bases<ripley::RipleyDomain> >("RipleyBrick", "", no_init);      class_<ripley::Brick, bases<ripley::RipleyDomain> >("RipleyBrick", "", no_init);
390      class_<ripley::Rectangle, bases<ripley::RipleyDomain> >("RipleyRectangle", "", no_init);      class_<ripley::Rectangle, bases<ripley::RipleyDomain> >("RipleyRectangle", "", no_init)
391        .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"
392    ":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"
393    "guassian blur and s is the sigma parameter."
394        );
395  }  }
396    

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

  ViewVC Help
Powered by ViewVC 1.1.26