File: MacroFrictionCalculatorPy.cpp

package info (click to toggle)
esys-particle 2.3.5%2Bdfsg2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 13,132 kB
  • sloc: cpp: 81,480; python: 5,872; makefile: 1,259; sh: 313; perl: 225
file content (282 lines) | stat: -rw-r--r-- 7,918 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/////////////////////////////////////////////////////////////
//                                                         //
// Copyright (c) 2003-2017 by The University of Queensland //
// Centre for Geoscience Computing                         //
// http://earth.uq.edu.au/centre-geoscience-computing      //
//                                                         //
// Primary Business: Brisbane, Queensland, Australia       //
// Licensed under the Open Software License version 3.0    //
// http://www.apache.org/licenses/LICENSE-2.0              //
//                                                         //
/////////////////////////////////////////////////////////////


#include <boost/version.hpp>
#include <boost/python.hpp>
#include <boost/shared_ptr.hpp>
#include "Tools/MacroFrictionCalculator/MacroFrictionCalculator.h"
#include "Tools/MacroFrictionCalculator/LinearWindowAverager.h"
#include "Tools/MacroFrictionCalculator/WallForceReader.h"
#include "Foundation/StlIterator.h"
#include "Foundation/StringUtil.h"
#include "Python/BoostPythonUtil/ListConverter.h"
#include "Python/esys/lsm/tools/MacroFrictionCalculatorPy.h"
#include "Python/esys/lsm/util/Vec3Py.h"

using namespace boost::python;
using namespace esys::lsm;

#include <vector>
#include <fstream>
#include <stdexcept>

namespace esys
{
  namespace lsm
  {
    class WallForcePairPy : public std::pair<Vec3Py, Vec3Py>
    {
    public:
      typedef std::pair<Vec3Py, Vec3Py> WallForcePair;
      WallForcePairPy(const Vec3Py &v1, const Vec3Py &v2)
        : WallForcePair(v1, v2)
      {
      }

      WallForcePairPy(const WallForcePairPy &pair)
        : WallForcePair(pair.first, pair.second)
      {
      }

      WallForcePairPy(const Vec3 &v1, const Vec3 &v2)
        : WallForcePair(v1, v2)
      {
      }

      WallForcePairPy(const WallForceReader::WallForcePair &pair)
        : WallForcePair(pair.first, pair.second)
      {
      }

      WallForcePairPy(const list &l1, const list &l2)
        : WallForcePair()
      {
        const int llen = Vec3Py().len();
        if ((bpu::len(l1) == llen) && (bpu::len(l2) == llen))
        {
          const Vec3Py first1 = 
            Vec3Py(
              extract<double>(l1[0])(),
              extract<double>(l1[1])(),
              extract<double>(l1[2])()
            );
          const Vec3Py second1 = 
            Vec3Py(
              extract<double>(l2[0])(),
              extract<double>(l2[1])(),
              extract<double>(l2[2])()
            );
          first = first1;
          second = second1;
        }
        else
        {
          std::stringstream msg;
          msg 
            << "Lists "
            << extract<std::string>(boost::python::str(l1))()
            << " (len=" << bpu::len(l1) << ")"
            << " and " << extract<std::string>(boost::python::str(l2))()
             << " (len=" << bpu::len(l2) << ")"
            << " do not both have length " << llen;
          throw std::runtime_error(msg.str());
        }
      }

      int len() const
      {
        return 2;
      }

      const Vec3 &getItem(int i) const
      {
        if (i == 0)
        {
          return first;
        }
        else if (i == 1)
        {
          return second;
        }
        throwIndexOutOfRange(i);
        return Vec3::ZERO;
      }

      void setItem(int i, const Vec3 &v)
      {
        if (i == 0)
        {
          first = v;
        }
        else if (i == 1)
        {
          second = v;
        }
        throwIndexOutOfRange(i);
      }

      std::string toString() const
      {
        return 
          extract<std::string>(boost::python::str(first))()
          +
          extract<std::string>(boost::python::str(second))();
      }

    protected:
      void throwIndexOutOfRange(int i) const
      {
        std::stringstream msg;
        msg << "Index i = " << i << " out of range (0,2).";
        throw std::runtime_error(msg.str());        
      }
    };

    class WallForceReaderPy
    {
    public:
      WallForceReaderPy(int wallId1, int wallId2, const std::string &fileName)
        : m_iFStreamPtr(new std::ifstream(fileName.c_str())),
          m_it(wallId1, wallId2, *m_iFStreamPtr)
      {
      }

      bool hasNext() const
      {
        return m_it.hasNext();
      }

      WallForceReader::WallForcePair next()
      {
        return m_it.next();
      }
    private:
      typedef boost::shared_ptr<std::ifstream> IFStreamPtr;
      IFStreamPtr     m_iFStreamPtr;
      WallForceReader m_it;
    };

    class MacroFrictionCalculatorPy
    {
    public:
      MacroFrictionCalculatorPy(
        int normalDimIndex,
        int shearDimIndex
      )
        : m_fricnCalker(normalDimIndex, shearDimIndex)
      {
      }

      class Extractor
      {
      public:
        MacroFrictionCalculator::WallForcePair operator()(object pyOb)
        {
          extract<WallForcePairPy> pair(pyOb);
          if (pair.check())
          {
            return pair();
          }
          else
          {
            std::stringstream msg;
            msg 
              << "Could not extract C++ WallForcePairPy object from list element: "
              << extract<std::string>(str(pyOb))();
            throw runtime_error(msg.str());
          }          
        }

      };

      void addList(const list &wallForceList)
      {
        typedef MacroFrictionCalculator::WallForcePair WallForcePair;
        typedef std::vector<WallForcePair>             WallForceVector;
        
        const WallForceVector 
          wallForceVector(
            bpu::listToVector<MacroFrictionCalculator::WallForcePair, Extractor>(wallForceList)
          );
        m_fricnCalker.add(ForwardConstIterator<WallForceVector>(wallForceVector));
      }

      void addReader(const WallForceReaderPy &wallForceReader)
      {
        m_fricnCalker.add(wallForceReader);
      }

      list getFrictionList() const
      {
        return bpu::vectorToList(m_fricnCalker.getFrictionVector());
      }
      
    private:
      MacroFrictionCalculator m_fricnCalker;
    };

    class LinearWindowAveragerPy
    {
    public:
      LinearWindowAveragerPy(
        const list &valList,
        int halfWindowSize,
        int beginIndex,
        int endIndex,
        int skipSize
      ) : m_linearAverager(
            bpu::listToVector<double>(valList),
            halfWindowSize,
            beginIndex,
            endIndex,
            skipSize
          )
      {
      }

      list getAveragedList()
      {
        return bpu::vectorToList(m_linearAverager.getAveragedVector());
      }
    private:
      LinearWindowAverager m_linearAverager;
    };

    void exportMacroFrictionCalculator()
    {
      // Disable autogeneration of C++ signatures (Boost 1.34.0 and higher)
      // for Epydoc which stumbles over indentation in the automatically generated strings.
      boost::python::docstring_options no_autogen(true,false);

      class_<WallForceReaderPy>("WallForceReader", init<int,int, const std::string & >());
      class_<WallForcePairPy>("WallForcePair", init<const Vec3Py &, const Vec3Py &>())
        .def(init<const list &,const list &>())
        .def(init<const WallForcePairPy &>())
        .def(self == self)
        .def_readwrite("first",  &WallForcePairPy::first)
        .def_readwrite("second", &WallForcePairPy::second)
        .def("__str__", &WallForcePairPy::toString)
      ;
      class_<MacroFrictionCalculatorPy>("MacroFrictionCalculator", init<int,int>())
        .def("getFrictionList", &MacroFrictionCalculatorPy::getFrictionList)
        .def("add", &MacroFrictionCalculatorPy::addList)
        .def("add", &MacroFrictionCalculatorPy::addReader)
      ;
    
      class_<LinearWindowAveragerPy>("LinearWindowAverager", init<const list &,int,int,int,int>())
        .def("getAveragedList", &LinearWindowAveragerPy::getAveragedList)
      ;
    }
  }
}