/[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 4340 by caltinay, Fri Mar 22 04:38:36 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  * 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    void writeBinaryGrid(std::string filename, const escript::Data& d,
31            const char byteorder, const char datatype, const char datasize)
32    {
33        const RipleyDomain* dom=dynamic_cast<const RipleyDomain*>(d.getDomain().get());
34        if (!dom)
35            throw RipleyException("Function space must be on a ripley domain");
36    
37        if (datatype != 'f')
38            throw RipleyException("only float data supported");
39    
40        if (datasize != '4')
41            throw RipleyException("only single-precision data supported");
42    
43        switch (byteorder) {
44            case '=':
45                dom->writeBinaryGrid(d, filename, RIPLEY_BYTE_ORDER);
46                break;
47            case '<':
48                dom->writeBinaryGrid(d, filename, RIPLEY_LITTLE_ENDIAN);
49                break;
50            case '>':
51                dom->writeBinaryGrid(d, filename, RIPLEY_BIG_ENDIAN);
52                break;
53            default:
54                throw RipleyException("unrecognized byte order argument");
55        }
56    }
57    
58  escript::Data readBinaryGrid(std::string filename, escript::FunctionSpace fs,  escript::Data readBinaryGrid(std::string filename, escript::FunctionSpace fs,
59          const object& pyFirst, const object& pyNum, const object& pyShape,          const object& pyFirst, const object& pyNum, const object& pyMultiplier,
60          double fill=0.)          const object& pyShape, double fill=0.)
61    {
62        int dim=fs.getDim();
63        std::vector<int> first(dim), numValues(dim), multiplier(dim), shape;
64    
65        if (extract<tuple>(pyFirst).check() || extract<list>(pyFirst).check()) {
66            if (len(pyFirst)==dim) {
67                for (int i=0; i<dim; i++) {
68                    first[i]=extract<int>(pyFirst[i]);
69                }
70            } else
71                throw RipleyException("Argument 'first' has wrong length");
72        } else
73            throw RipleyException("Argument 'first' must be a tuple or list");
74    
75        if (extract<tuple>(pyNum).check() || extract<list>(pyNum).check()) {
76            if (len(pyNum)==dim) {
77                for (int i=0; i<dim; i++) {
78                    numValues[i]=extract<int>(pyNum[i]);
79                }
80            } else
81                throw RipleyException("Argument 'numValues' has wrong length");
82        } else
83            throw RipleyException("Argument 'numValues' must be a tuple or list");
84    
85        if (extract<tuple>(pyShape).check() || extract<list>(pyShape).check()) {
86            for (int i=0; i<len(pyShape); i++) {
87                shape.push_back(extract<int>(pyShape[i]));
88            }
89        } else
90            throw RipleyException("Argument 'shape' must be a tuple or list");
91    
92        if (extract<tuple>(pyMultiplier).check() || extract<list>(pyMultiplier).check()) {
93            if (len(pyMultiplier)==dim) {
94                for (int i=0; i<dim; i++) {
95                    multiplier[i]=extract<int>(pyMultiplier[i]);
96                }
97            } else
98                throw RipleyException("Argument 'multiplier' has wrong length");
99        } else
100            throw RipleyException("Argument 'multiplier' must be a tuple or list");
101    
102        const RipleyDomain* dom=dynamic_cast<const RipleyDomain*>(fs.getDomain().get());
103        if (!dom)
104            throw RipleyException("Function space must be on a ripley domain");
105    
106    
107        escript::Data res(fill, shape, fs, true);
108    
109        dom->readBinaryGrid(res, filename, first, numValues, multiplier);
110        return res;
111    }
112    
113    escript::Data readNcGrid(std::string filename, std::string varname,
114            escript::FunctionSpace fs, const object& pyFirst, const object& pyNum,
115            const object& pyMultiplier, const object& pyShape, double fill=0.)
116  {  {
117      int dim=fs.getDim();      int dim=fs.getDim();
118      std::vector<int> first(dim), numValues(dim), shape;      std::vector<int> first(dim), numValues(dim), multiplier(dim), shape;
119    
120      if (extract<tuple>(pyFirst).check() || extract<list>(pyFirst).check()) {      if (extract<tuple>(pyFirst).check() || extract<list>(pyFirst).check()) {
121          if (len(pyFirst)==dim) {          if (len(pyFirst)==dim) {
# Line 54  escript::Data readBinaryGrid(std::string Line 137  escript::Data readBinaryGrid(std::string
137      } else      } else
138          throw RipleyException("Argument 'numValues' must be a tuple or list");          throw RipleyException("Argument 'numValues' must be a tuple or list");
139    
140        if (extract<tuple>(pyMultiplier).check() || extract<list>(pyMultiplier).check()) {
141            if (len(pyMultiplier)==dim) {
142                for (int i=0; i<dim; i++) {
143                    multiplier[i]=extract<int>(pyMultiplier[i]);
144                }
145            } else
146                throw RipleyException("Argument 'multiplier' has wrong length");
147        } else
148            throw RipleyException("Argument 'multiplier' must be a tuple or list");
149    
150      if (extract<tuple>(pyShape).check() || extract<list>(pyShape).check()) {      if (extract<tuple>(pyShape).check() || extract<list>(pyShape).check()) {
151          for (int i=0; i<len(pyShape); i++) {          for (int i=0; i<len(pyShape); i++) {
152              shape.push_back(extract<int>(pyShape[i]));              shape.push_back(extract<int>(pyShape[i]));
# Line 68  escript::Data readBinaryGrid(std::string Line 161  escript::Data readBinaryGrid(std::string
161    
162      escript::Data res(fill, shape, fs, true);      escript::Data res(fill, shape, fs, true);
163    
164      dom->readBinaryGrid(res, filename, first, numValues);      dom->readNcGrid(res, filename, varname, first, numValues, multiplier);
165      return res;      return res;
166  }  }
167    
# Line 168  BOOST_PYTHON_MODULE(ripleycpp) Line 261  BOOST_PYTHON_MODULE(ripleycpp)
261    
262      register_exception_translator<ripley::RipleyException>(&(esysUtils::esysExceptionTranslator));      register_exception_translator<ripley::RipleyException>(&(esysUtils::esysExceptionTranslator));
263    
264        scope().attr("__doc__") = "To use this module, please import esys.ripley";
265    
266      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),
267  "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"
268  ":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 284  BOOST_PYTHON_MODULE(ripleycpp)
284  ":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"
285  ":param d1: number of subdivisions in direction 1\n:type d1: ``int``");  ":param d1: number of subdivisions in direction 1\n:type d1: ``int``");
286      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`");  
287    
288      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.));
289              "Reads a ripley domain from a file created by write().\n\n"  
290              ":rtype: `RipleyDomain`\n:param filename:\n:type filename: ``string``\n");      def("_readNcGrid", &ripley::readNcGrid, (arg("filename"), arg("varname"), arg("functionspace"), arg("first"), arg("numValues"), arg("multiplier"), arg("shape"), arg("fill")=0.));
291    
292      def("_readBinaryGrid", &ripley::readBinaryGrid, (arg("filename"), arg("functionspace"), arg("first"), arg("numValues"), arg("shape"), arg("fill")=0.));      def("_writeBinaryGrid", &ripley::writeBinaryGrid);
293    
294      class_<ripley::RipleyDomain, bases<escript::AbstractContinuousDomain> >      class_<ripley::RipleyDomain, bases<escript::AbstractContinuousDomain>, boost::noncopyable >
295          ("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().")  
296          .def("print_mesh_info", &ripley::RipleyDomain::Print_Mesh_Info, (arg("full")=false),          .def("print_mesh_info", &ripley::RipleyDomain::Print_Mesh_Info, (arg("full")=false),
297                  "Prints out a summary about the mesh.\n"                  "Prints out a summary about the mesh.\n"
298                  ":param full: whether to output additional data\n:type full: ``bool``")                  ":param full: whether to output additional data\n:type full: ``bool``")
299          .def("dump", &ripley::RipleyDomain::dump, args("filename"),          .def("dump", &ripley::RipleyDomain::dump, args("filename"),
300                  "Dumps the mesh to a file with the given name.")                  "Dumps the mesh to a file with the given name.")
301            .def("getGridParameters", &ripley::RipleyDomain::getGridParameters,
302    "Returns the tuple (origin, spacing, elements) where the entries are tuples:\n"
303    "``origin``=the coordinates of the domain's global origin,\n"
304    "``spacing``=the element size (=node spacing) of the domain,\n"
305    "``elements``=the global number of elements in all dimensions\n\n"
306    ":rtype: ``tuple``")
307          .def("getDescription", &ripley::RipleyDomain::getDescription,          .def("getDescription", &ripley::RipleyDomain::getDescription,
308  ":return: a description for this domain\n:rtype: ``string``")  ":return: a description for this domain\n:rtype: ``string``")
309          .def("getDim", &ripley::RipleyDomain::getDim, ":rtype: ``int``")          .def("getDim", &ripley::RipleyDomain::getDim, ":rtype: ``int``")

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

  ViewVC Help
Powered by ViewVC 1.1.26