File: DistConnections.hpp

package info (click to toggle)
esys-particle 2.1-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 7,284 kB
  • sloc: cpp: 77,304; python: 5,647; makefile: 1,176; sh: 10
file content (275 lines) | stat: -rw-r--r-- 7,680 bytes parent folder | download
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
/////////////////////////////////////////////////////////////
//                                                         //
// Copyright (c) 2003-2011 by The University of Queensland //
// Earth Systems Science Computational Centre (ESSCC)      //
// http://www.uq.edu.au/esscc                              //
//                                                         //
// Primary Business: Brisbane, Queensland, Australia       //
// Licensed under the Open Software License version 3.0    //
// http://www.opensource.org/licenses/osl-3.0.php          //
//                                                         //
/////////////////////////////////////////////////////////////


#include "Foundation/StringUtil.h"
#include <limits>

namespace esys
{
  namespace lsm
  {
    template <typename TmplParticle, typename TmplConnection>
    DistConnections<TmplParticle,TmplConnection>::DistConnections(
      double maxDist,
      Tag defaultTag,
      const BoundingBox &bBox,
      const BoolVector &circDimensions
    )
      : m_connectionPoolPtr(new ConnectionPool(4096)),
        m_connectionSet(),
        m_nTablePtr(),
        m_minRadius(std::numeric_limits<double>::max()),
        m_maxRadius(-std::numeric_limits<double>::max()),
        m_maxDist(maxDist),
        m_minPt(bBox.getMinPt()),
        m_maxPt(bBox.getMaxPt()),
        m_defaultTag(defaultTag)
    {
      const double gridSize =
        (
          max(
            bBox.getSizes()[0],
            max(
              bBox.getSizes()[1],
              bBox.getSizes()[2]
            )
          )
        )/5.0;
      m_nTablePtr =
        NTablePtr(
          new NTable(
            bBox,
            gridSize,
            circDimensions,
            2*gridSize
          )
      );
    }

    template <typename TmplParticle, typename TmplConnection>
    DistConnections<TmplParticle,TmplConnection>::~DistConnections()
    {
    }

    template <typename TmplParticle, typename TmplConnection>
    int DistConnections<TmplParticle,TmplConnection>::getNumParticles() const
    {
      return m_nTablePtr->size();
    }

    template <typename TmplParticle, typename TmplConnection>
    int DistConnections<TmplParticle,TmplConnection>::getNumConnections() const
    {
      return m_connectionSet.size();
    }

    template <typename TmplParticle, typename TmplConnection>
    double DistConnections<TmplParticle,TmplConnection>::getMinRadius() const
    {
      return m_minRadius;
    }

    template <typename TmplParticle, typename TmplConnection>
    double DistConnections<TmplParticle,TmplConnection>::getMaxRadius() const
    {
      return m_maxRadius;
    }

    template <typename TmplParticle, typename TmplConnection>
    typename DistConnections<TmplParticle,TmplConnection>::ParticleConstIterator
    DistConnections<TmplParticle,TmplConnection>::getParticleIterator() const
    {
      return m_nTablePtr->getIterator();
    }

    template <typename TmplParticle, typename TmplConnection>
    void
    DistConnections<TmplParticle,TmplConnection>::createConnection(
      const Particle &p1,
      const Particle &p2,
      Tag tag
    )
    {
      m_connectionSet.insert(
        m_connectionPoolPtr->construct(p1.getId(), p2.getId(), tag)
      );
    }
    template <typename TmplParticle>
    class CmpParticleId
    {
    public:
      bool operator()(const TmplParticle &p1, const TmplParticle &p2) const
      {
        return (p1.getId() < p2.getId());
      }

      bool operator()(const TmplParticle *p1, const TmplParticle *p2) const
      {
        return (p1->getId() < p2->getId());
      }
    };

    template <typename TmplParticle, typename TmplConnection>
    template <typename TmplParticleIterator>
    void
    DistConnections<TmplParticle,TmplConnection>::create(
      TmplParticleIterator it,
      Tag tag
    )
    {
      typedef std::set<Particle *, CmpParticleId<Particle> > ParticleSet;
      ParticleSet pSet;
      while (it.hasNext())
      {
        Particle &p = it.next();
        insert(p);
        pSet.insert(&p);
      }
      m_nTablePtr->resize(getParticleBBox(), 4.1*getMinRadius(), 2.1*getMaxRadius());

      for (
        typename ParticleSet::const_iterator pIt = pSet.begin();
        pIt != pSet.end();
        pIt++
      )
      {
        typename NTable::ParticleVector nVector =
          m_nTablePtr->getNeighbourVector(
            (*pIt)->getPos(),
            (*pIt)->getRad() + m_maxDist
          );
        for (
          typename NTable::ParticleVector::const_iterator nIt = nVector.begin();
          nIt != nVector.end();
          nIt++
        )
        {
          Particle *p1 = (*pIt);
          Particle *p2 = (*nIt);

          if (
            (
              (pSet.find(p1) != pSet.end())
              &&
              (pSet.find(p2) != pSet.end())
              &&
              (p1->getId() < p2->getId())
            )
            ||
            (
              ((pSet.find(p1)==pSet.end()) && (pSet.find(p2)!= pSet.end()))
              ||
              ((pSet.find(p1)!=pSet.end()) && (pSet.find(p2)== pSet.end()))
            )
          )
          {
            p1 =
              ((*pIt)->getId() < (*nIt)->getId())
              ?
              (*pIt)
              :
              (*nIt);
            p2 =
              ((*pIt)->getId() < (*nIt)->getId())
              ?
              (*nIt)
              :
              (*pIt);
            const double radiusSum =
              m_maxDist + p1->getRad() + p2->getRad();
            const double radiusSumSqrd = radiusSum*radiusSum;
  
            if (
              (p1->getPos() - p2->getPos()).norm2()
              <=
              (radiusSumSqrd)
            )
            {
#if 0
              console.Debug()
                << "creating connection: \n"
                << StringUtil::toString(*p1)
                << "->"
                << StringUtil::toString(*p2) << "\n";
#endif
              createConnection(*p1, *p2, tag);
            }
          }
        }
      }
    }

    template <typename TmplParticle, typename TmplConnection>
    template <typename TmplParticleIterator>
    void
    DistConnections<TmplParticle,TmplConnection>::create(
      TmplParticleIterator it
    )
    {
      create(it, getDefaultTag());
    }
    
    template <typename TmplParticle, typename TmplConnection>
    void
    DistConnections<TmplParticle,TmplConnection>::insert(Particle &p)
    {
      if (p.getRad() < m_minRadius)
      {
        m_minRadius = p.getRad();
      }
      if (p.getRad() > m_maxRadius)
      {
        m_maxRadius = p.getRad();
      }

      m_nTablePtr->insert(p);

      for (int i = 0; i < 3; i++)
      {
        if (!(m_nTablePtr->getPeriodicDimensions()[i]))
        {
          if (p.getPos()[i]-p.getRad() < m_minPt[i])
          {
            m_minPt[i] = p.getPos()[i]-p.getRad();
          }
          if (p.getPos()[i]+p.getRad() > m_maxPt[i])
          {
            m_maxPt[i] = p.getPos()[i]+p.getRad();
          }
        }
      }
    }

    template <typename TmplParticle, typename TmplConnection>
    typename DistConnections<TmplParticle,TmplConnection>::Tag
    DistConnections<TmplParticle,TmplConnection>::getDefaultTag() const
    {
      return m_defaultTag;
    }

    template <typename TmplParticle, typename TmplConnection>
    void
    DistConnections<TmplParticle,TmplConnection>::setDefaultTag(Tag defaultTag)
    {
      m_defaultTag = defaultTag;
    }

    template <typename TmplParticle, typename TmplConnection>
    BoundingBox
    DistConnections<TmplParticle,TmplConnection>::getParticleBBox() const
    {
      return BoundingBox(m_minPt, m_maxPt);
    }

  }
}